|
12 | 12 | #
|
13 | 13 | # Login to your account here to get these : https://cds.climate.copernicus.eu
|
14 | 14 |
|
15 |
| -class ECMWF_tools: |
16 | 15 |
|
17 |
| - def __init__(self): |
18 |
| - # https://cds.climate.copernicus.eu/cdsapp#!/dataset/reanalysis-era5-single-levels-monthly-means?tab=form |
19 |
| - # https://confluence.ecmwf.int/pages/viewpage.action?pageId=82870405#ERA5:datadocumentation-Table4 |
20 |
| - # Check data availability: http://apps.ecmwf.int/datasets/ |
21 |
| - |
22 |
| - self.config_ecmwf = ECMWF_query.ECMWF_query() |
23 |
| - self.server = cdsapi.Client(debug=self.config_ecmwf.debug) |
24 |
| - |
25 |
| - def create_requests(self): |
26 |
| - years = [self.config_ecmwf.start_year + y for y in |
27 |
| - range(self.config_ecmwf.end_year - self.config_ecmwf.start_year)] |
28 |
| - |
29 |
| - if not os.path.exists(self.config_ecmwf.resultsdir): |
30 |
| - os.mkdir(self.config_ecmwf.resultsdir) |
31 |
| - for year in years: |
32 |
| - |
33 |
| - print("=> Downloading for year {}".format(year)) |
34 |
| - |
35 |
| - for parameter in self.config_ecmwf.parameters: |
36 |
| - print("=> getting data for : {} ".format(parameter)) |
37 |
| - metadata = self.config_ecmwf.get_parameter_metadata(parameter) |
38 |
| - |
39 |
| - out_filename = "{}{}_{}_year_{}.nc".format(self.config_ecmwf.resultsdir, |
40 |
| - self.config_ecmwf.dataset, |
41 |
| - metadata["short_name"], |
42 |
| - year) |
43 |
| - if os.path.exists(out_filename): |
44 |
| - os.remove(out_filename) |
45 |
| - |
46 |
| - self.submit_request(parameter, str(year), out_filename) |
47 |
| - |
48 |
| - def submit_request(self, parameter, year, out_filename): |
49 |
| - |
50 |
| - times = [ |
51 |
| - '00:00', '01:00', '02:00', |
52 |
| - '03:00', '04:00', '05:00', |
53 |
| - '06:00', '07:00', '08:00', |
54 |
| - '09:00', '10:00', '11:00', |
55 |
| - '12:00', '13:00', '14:00', |
56 |
| - '15:00', '16:00', '17:00', |
57 |
| - '18:00', '19:00', '20:00', |
58 |
| - '21:00', '22:00', '23:00', |
59 |
| - ] |
60 |
| - |
61 |
| - if self.config_ecmwf.extract_data_every_6_hours is True: |
62 |
| - times = ['00:00', '06:00', '12:00', '18:00'] |
63 |
| - |
64 |
| - options = { |
65 |
| - 'product_type': 'reanalysis', |
66 |
| - "year": year, |
67 |
| - "month": ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"], |
68 |
| - 'day': [ |
69 |
| - '01', '02', '03', |
70 |
| - '04', '05', '06', |
71 |
| - '07', '08', '09', |
72 |
| - '10', '11', '12', |
73 |
| - '13', '14', '15', |
74 |
| - '16', '17', '18', |
75 |
| - '19', '20', '21', |
76 |
| - '22', '23', '24', |
77 |
| - '25', '26', '27', |
78 |
| - '28', '29', '30', |
79 |
| - '31', |
80 |
| - ], |
81 |
| - 'time': times, |
82 |
| - "variable": [parameter], |
83 |
| - 'format': "netcdf", |
84 |
| - "area": self.config_ecmwf.area, |
85 |
| - "verbose": self.config_ecmwf.debug, |
86 |
| - } |
87 |
| - |
88 |
| - # Add more specific options for variables on pressure surfaces |
89 |
| - if parameter == "specific_humidity": |
90 |
| - self.config_ecmwf.reanalysis = "reanalysis-era5-pressure-levels" |
91 |
| - options["levtype"] = 'pl' |
92 |
| - options["pressure_level"] = '1000' |
93 |
| - else: |
94 |
| - self.config_ecmwf.reanalysis = 'reanalysis-era5-single-levels' |
95 |
| - |
96 |
| - try: |
97 |
| - # Do the request |
98 |
| - self.server.retrieve(self.config_ecmwf.reanalysis, options, out_filename) |
99 |
| - except Exception as e: |
100 |
| - print(e) |
101 |
| - print("[!] -------------------------- PROBLEMS WITH {0}".format(out_filename)) |
102 |
| - |
103 |
| - metadata = self.config_ecmwf.get_parameter_metadata(parameter) |
104 |
| - |
105 |
| - converter = ECMWF_convert_to_ROMS.ECMWF_convert_to_ROMS() |
106 |
| - converter.convert_to_ROMS_units_standards(out_filename, metadata, parameter, self.config_ecmwf) |
107 |
| - |
108 |
| - |
109 |
| -if __name__ == '__main__': |
110 |
| - tool = ECMWF_tools() |
111 |
| - tool.create_requests() |
| 16 | +class ECMWF_tools: |
| 17 | + def __init__(self): |
| 18 | + # https://cds.climate.copernicus.eu/cdsapp#!/dataset/reanalysis-era5-single-levels-monthly-means?tab=form |
| 19 | + # https://confluence.ecmwf.int/pages/viewpage.action?pageId=82870405#ERA5:datadocumentation-Table4 |
| 20 | + # Check data availability: http://apps.ecmwf.int/datasets/ |
| 21 | + |
| 22 | + self.config_ecmwf = ECMWF_query.ECMWF_query() |
| 23 | + self.server = cdsapi.Client(debug=self.config_ecmwf.debug) |
| 24 | + |
| 25 | + def create_requests(self): |
| 26 | + years = [ |
| 27 | + self.config_ecmwf.start_year + y |
| 28 | + for y in range(self.config_ecmwf.end_year - self.config_ecmwf.start_year) |
| 29 | + ] |
| 30 | + |
| 31 | + if not os.path.exists(self.config_ecmwf.resultsdir): |
| 32 | + os.mkdir(self.config_ecmwf.resultsdir) |
| 33 | + for year in years: |
| 34 | + |
| 35 | + print("=> Downloading for year {}".format(year)) |
| 36 | + |
| 37 | + for parameter in self.config_ecmwf.parameters: |
| 38 | + print("=> getting data for : {} ".format(parameter)) |
| 39 | + metadata = self.config_ecmwf.get_parameter_metadata(parameter) |
| 40 | + |
| 41 | + out_filename = "{}{}_{}_year_{}.nc".format( |
| 42 | + self.config_ecmwf.resultsdir, |
| 43 | + self.config_ecmwf.dataset, |
| 44 | + metadata["short_name"], |
| 45 | + year, |
| 46 | + ) |
| 47 | + roms_outfile = f"{out_filename[0:-3]}_roms.nc" |
| 48 | + if os.path.exists(roms_outfile): |
| 49 | + if not self.config_ecmwf.skip_existing_files: |
| 50 | + os.remove(out_filename) |
| 51 | + self.submit_request(parameter, str(year), out_filename) |
| 52 | + else: |
| 53 | + print(f"Skipping existing file: {out_filename}") |
| 54 | + else: |
| 55 | + self.submit_request(parameter, str(year), out_filename) |
| 56 | + |
| 57 | + def submit_request(self, parameter, year, out_filename): |
| 58 | + |
| 59 | + times = [ |
| 60 | + "00:00", |
| 61 | + "01:00", |
| 62 | + "02:00", |
| 63 | + "03:00", |
| 64 | + "04:00", |
| 65 | + "05:00", |
| 66 | + "06:00", |
| 67 | + "07:00", |
| 68 | + "08:00", |
| 69 | + "09:00", |
| 70 | + "10:00", |
| 71 | + "11:00", |
| 72 | + "12:00", |
| 73 | + "13:00", |
| 74 | + "14:00", |
| 75 | + "15:00", |
| 76 | + "16:00", |
| 77 | + "17:00", |
| 78 | + "18:00", |
| 79 | + "19:00", |
| 80 | + "20:00", |
| 81 | + "21:00", |
| 82 | + "22:00", |
| 83 | + "23:00", |
| 84 | + ] |
| 85 | + |
| 86 | + if self.config_ecmwf.extract_data_every_N_hours is True: |
| 87 | + times = times = [ |
| 88 | + "00:00", |
| 89 | + "02:00", |
| 90 | + "04:00", |
| 91 | + "06:00", |
| 92 | + "08:00", |
| 93 | + "10:00", |
| 94 | + "12:00", |
| 95 | + "14:00", |
| 96 | + "16:00", |
| 97 | + "18:00", |
| 98 | + "20:00", |
| 99 | + "22:00", |
| 100 | + ] |
| 101 | + |
| 102 | + options = { |
| 103 | + "product_type": "reanalysis", |
| 104 | + "year": year, |
| 105 | + "month": [ |
| 106 | + "01", |
| 107 | + "02", |
| 108 | + "03", |
| 109 | + "04", |
| 110 | + "05", |
| 111 | + "06", |
| 112 | + "07", |
| 113 | + "08", |
| 114 | + "09", |
| 115 | + "10", |
| 116 | + "11", |
| 117 | + "12", |
| 118 | + ], |
| 119 | + "day": [ |
| 120 | + "01", |
| 121 | + "02", |
| 122 | + "03", |
| 123 | + "04", |
| 124 | + "05", |
| 125 | + "06", |
| 126 | + "07", |
| 127 | + "08", |
| 128 | + "09", |
| 129 | + "10", |
| 130 | + "11", |
| 131 | + "12", |
| 132 | + "13", |
| 133 | + "14", |
| 134 | + "15", |
| 135 | + "16", |
| 136 | + "17", |
| 137 | + "18", |
| 138 | + "19", |
| 139 | + "20", |
| 140 | + "21", |
| 141 | + "22", |
| 142 | + "23", |
| 143 | + "24", |
| 144 | + "25", |
| 145 | + "26", |
| 146 | + "27", |
| 147 | + "28", |
| 148 | + "29", |
| 149 | + "30", |
| 150 | + "31", |
| 151 | + ], |
| 152 | + "time": times, |
| 153 | + "variable": [parameter], |
| 154 | + "format": "netcdf", |
| 155 | + "area": self.config_ecmwf.area, |
| 156 | + "verbose": self.config_ecmwf.debug, |
| 157 | + } |
| 158 | + |
| 159 | + # Add more specific options for variables on pressure surfaces |
| 160 | + if parameter == "specific_humidity": |
| 161 | + self.config_ecmwf.reanalysis = "reanalysis-era5-pressure-levels" |
| 162 | + options["levtype"] = "pl" |
| 163 | + options["pressure_level"] = "1000" |
| 164 | + else: |
| 165 | + self.config_ecmwf.reanalysis = "reanalysis-era5-single-levels" |
| 166 | + |
| 167 | + try: |
| 168 | + # Do the request |
| 169 | + self.server.retrieve(self.config_ecmwf.reanalysis, options, out_filename) |
| 170 | + except Exception as e: |
| 171 | + print(e) |
| 172 | + print( |
| 173 | + "[!] -------------------------- PROBLEMS WITH {0}".format(out_filename) |
| 174 | + ) |
| 175 | + |
| 176 | + metadata = self.config_ecmwf.get_parameter_metadata(parameter) |
| 177 | + |
| 178 | + converter = ECMWF_convert_to_ROMS.ECMWF_convert_to_ROMS() |
| 179 | + converter.convert_to_ROMS_units_standards( |
| 180 | + out_filename, metadata, parameter, self.config_ecmwf |
| 181 | + ) |
| 182 | + |
| 183 | + |
| 184 | +if __name__ == "__main__": |
| 185 | + tool = ECMWF_tools() |
| 186 | + tool.create_requests() |
0 commit comments