-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
369 Make a proper selection for either CMEMS reanalysis or forecast product #388
369 Make a proper selection for either CMEMS reanalysis or forecast product #388
Conversation
dfm_tools/download.py
Outdated
global product #set product as global variable, so it only has to be retreived once per download run (otherwise once per variable) | ||
if 'product' not in globals(): | ||
print('retrieving time range of CMEMS reanalysis product') | ||
dataset_url = 'https://my.cmems-du.eu/thredds/dodsC/cmems_mod_glo_phy_my_0.083_P1D-m' #assuming here that physchem and bio reanalyisus/multiyear datasets have the same enddate, this seems safe | ||
ds = open_OPeNDAP_xr(dataset_url=dataset_url, credentials=credentials) | ||
my_times = ds.time.to_series() | ||
global my_datemax | ||
my_datemax = my_times.iloc[-1] | ||
|
||
if pd.Timestamp(date_max) <= my_datemax: | ||
product = 'reanalysis' | ||
elif pd.Timestamp(date_min) > my_datemax: | ||
product = 'analysisforecast' | ||
else: | ||
raise ValueError(f'Requested timerange is {date_min} to {date_max}. Currently, it is only possible to query periods before OR after the multiyear/reanalysis enddate ({my_datemax}).') | ||
reanalysis_t0, reanalysis_tend = my_times.iloc[0], my_times.iloc[-1] | ||
if (date_min >= reanalysis_t0) & (date_max <= reanalysis_tend): | ||
product = 'reanalysis' | ||
|
||
if 'product' not in globals(): #apparently, the reanalysis is not sufficient. Let's check forecast product. | ||
print('retrieving time range of CMEMS forecast product') | ||
dataset_url = 'https://nrt.cmems-du.eu/thredds/dodsC/cmems_mod_glo_phy_anfc_0.083deg_P1D-m' #assuming here that physchem and bio reanalyisus/multiyear datasets have the same enddate, this seems safe | ||
ds = open_OPeNDAP_xr(dataset_url=dataset_url, credentials=credentials) | ||
my_times = ds.time.to_series() | ||
forecast_t0, forecast_tend = my_times.iloc[0], my_times.iloc[-1] | ||
if (date_min >= forecast_t0) & (date_max <= forecast_tend): | ||
product = 'analysisforecast' | ||
|
||
if 'product' not in globals(): | ||
raise ValueError(f'Requested timerange ({date_min} to {date_max}) is not fully within timerange of reanalysis product ({reanalysis_t0} to {reanalysis_tend}) or forecast product ({forecast_t0} to {forecast_tend}).') | ||
else: | ||
print(f"The CMEMS '{product}'-product will be used.") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest a different approach. Retrieve start/stoptime for both multiyear/forecast, save as variables. Then do the check and decide the product. It will make the code shorter and easier to understand.
Kudos, SonarCloud Quality Gate passed! 0 Bugs No Coverage information |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approved after additional commits with conflict resolutions and some minor improvements.
dfm_tools/download.py
Outdated
date_min = pd.Timestamp(date_min)-pd.Timedelta(days=1) #CMEMS has daily noon values (not midnight), so subtract one day from date_min to cover desired time extent | ||
date_max = pd.Timestamp(date_max) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to be replaced by function (maybe first lets merge PR 390 and then update branch), see #390 (comment)
dfm_tools/download.py
Outdated
def get_time_range_from_dataset(dataset_url, credentials): | ||
ds = open_OPeNDAP_xr(dataset_url=dataset_url, credentials=credentials) | ||
my_times = ds.time.to_series() | ||
global my_datemax | ||
my_datemax = my_times.iloc[-1] | ||
|
||
if pd.Timestamp(date_max) <= my_datemax: | ||
product = 'reanalysis' | ||
elif pd.Timestamp(date_min) > my_datemax: | ||
product = 'analysisforecast' | ||
else: | ||
raise ValueError(f'Requested timerange is {date_min} to {date_max}. Currently, it is only possible to query periods before OR after the multiyear/reanalysis enddate ({my_datemax}).') | ||
return [my_times.iloc[0], my_times.iloc[-1]] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- move new function out of download_CMEMS function (e.g. bottom of script), I also suggest to add
OPeNDAP
in the name my
inmy_times
was for multiyear, maybe rename itds_times
- square brackets can be left out:
return my_times.iloc[0], my_times.iloc[-1]
fixes #369