Skip to content

Commit

Permalink
new keyword for meteo output_format
Browse files Browse the repository at this point in the history
  • Loading branch information
joelfiddes committed Sep 12, 2024
1 parent fd2adc4 commit 3cb0b49
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
20 changes: 12 additions & 8 deletions TopoPyScale/fetch_era5.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from multiprocessing.dummy import Pool as ThreadPool
from datetime import datetime, timedelta

def retrieve_era5(product, startDate, endDate, eraDir, latN, latS, lonE, lonW, step, num_threads=10, surf_plev='surf', plevels=None, realtime=False):
def retrieve_era5(product, startDate, endDate, eraDir, latN, latS, lonE, lonW, step, num_threads=10, surf_plev='surf', plevels=None, realtime=False, output_format='netcdf'):
""" Sets up era5 surface retrieval.
* Creates list of year/month pairs to iterate through.
* MARS retrievals are most efficient when subset by time.
Expand Down Expand Up @@ -75,6 +75,7 @@ def retrieve_era5(product, startDate, endDate, eraDir, latN, latS, lonE, lonW, s
df['time_steps'] = df.step.apply(lambda x: time_step_dict.get(x))
df['bbox'] = df.step.apply(lambda x: bbox)
df['product_type'] = product
df['output_format'] = output_format

print("Start = ", df.dates[0].strftime('%Y-%b'))
print("End = ", df.dates[len(df.dates) - 1].strftime('%Y-%b'))
Expand All @@ -99,7 +100,8 @@ def retrieve_era5(product, startDate, endDate, eraDir, latN, latS, lonE, lonW, s
list(download.bbox),
list(download.target_file),
list(download.product_type),
list(download.time_steps)))
list(download.time_steps),
list(download.output_format)))
pool.close()
pool.join()
elif surf_plev == 'plev':
Expand All @@ -111,7 +113,8 @@ def retrieve_era5(product, startDate, endDate, eraDir, latN, latS, lonE, lonW, s
list(download.target_file),
list(download.product_type),
list(download.time_steps),
list(download.plevels)))
list(download.plevels),
list(download.output_format)))
pool.close()
pool.join()
else:
Expand All @@ -128,7 +131,7 @@ def retrieve_era5(product, startDate, endDate, eraDir, latN, latS, lonE, lonW, s
# redownload current month to catch missing days in realtime mode.
era5_realtime_plev(eraDir, df.dataset[0], df.bbox[0], df.product_type[0], df.plevels[0])

def era5_request_surf(dataset, year, month, bbox, target, product, time):
def era5_request_surf(dataset, year, month, bbox, target, product, time, output_format= "netcdf"):
"""CDS surface api call
Args:
Expand All @@ -138,7 +141,8 @@ def era5_request_surf(dataset, year, month, bbox, target, product, time):
bbox (list): bonding box in lat-lon
target (str): filename
product (str): type of model run. defaul: reanalysis
time (str or list): hours for which to download data
time (str or list): hours for which to download data
format (str): "grib" or "netcdf"
Returns:
Store to disk dataset as indicated
Expand Down Expand Up @@ -170,12 +174,12 @@ def era5_request_surf(dataset, year, month, bbox, target, product, time):
],
'time': time,
'grid': [0.25, 0.25],
'format': 'netcdf'
'format': output_format
},
target)
print(target + " complete")

def era5_request_plev(dataset, year, month, bbox, target, product, time, plevels):
def era5_request_plev(dataset, year, month, bbox, target, product, time, plevels, output_format= "netcdf"):
"""CDS plevel api call
Args:
Expand All @@ -197,7 +201,7 @@ def era5_request_plev(dataset, year, month, bbox, target, product, time, plevels
dataset,
{
'product_type': product,
'format': 'netcdf',
'format': output_format,
"area": bbox,
'variable': [
'geopotential', 'temperature', 'u_component_of_wind',
Expand Down
16 changes: 14 additions & 2 deletions TopoPyScale/topoclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,16 @@ def get_era5(self):
os.makedirs(self.config.outputs.path / "downscaled")

realtime = False # always false now as redownload of current month handled elsewhere ? key required only to dynamically set config.project.end
output_format = self.config.climate[self.config.project.climate].output_format

# if keyword exists in config set out_format to value or default to netcdf
if self.config.climate[self.config.project.climate].output_format:
output_format = self.config.climate[self.config.project.climate].output_format
# else set realtime to False
else:
output_format = 'netcdf'


# retreive ERA5 surface data
fe.retrieve_era5(
self.config.climate[self.config.project.climate].product,
Expand All @@ -692,7 +702,8 @@ def get_era5(self):
self.config.climate[self.config.project.climate].timestep,
self.config.climate[self.config.project.climate].download_threads,
surf_plev='surf',
realtime=realtime
realtime=realtime,
output_format=output_format
)
# retrieve era5 plevels
fe.retrieve_era5(
Expand All @@ -705,7 +716,8 @@ def get_era5(self):
self.config.climate[self.config.project.climate].download_threads,
surf_plev='plev',
plevels=self.config.climate[self.config.project.climate].plevels,
realtime=realtime
realtime=realtime,
output_format=output_format
)

def get_ifs_forecast(self):
Expand Down

0 comments on commit 3cb0b49

Please sign in to comment.