Skip to content
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

Conversation

JulienGroenenboom
Copy link
Collaborator

fixes #369

Comment on lines 90 to 112
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.")
Copy link
Collaborator

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.

@sonarcloud
Copy link

sonarcloud bot commented Jun 22, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 1 Code Smell

No Coverage information No Coverage information
0.0% 0.0% Duplication

Copy link
Collaborator

@veenstrajelmer veenstrajelmer left a 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.

Comment on lines 93 to 94
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)
Copy link
Collaborator

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)

Comment on lines 88 to 91
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]]
Copy link
Collaborator

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 in my_times was for multiyear, maybe rename it ds_times
  • square brackets can be left out: return my_times.iloc[0], my_times.iloc[-1]

@veenstrajelmer veenstrajelmer merged commit 64d1553 into main Jun 22, 2023
6 of 7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve CMEMS download function (reanalysis/forecast decision)
2 participants