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

705 re enable cmems download per period #706

Merged
merged 2 commits into from
Dec 8, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 25 additions & 11 deletions dfm_tools/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def cds_client_withargs():

def download_CMEMS(varkey,
longitude_min, longitude_max, latitude_min, latitude_max,
date_min, date_max, #freq='D',
date_min, date_max, freq='D',
dataset_id=None, buffer=None,
dir_output='.', file_prefix='', overwrite=False):
"""
Expand All @@ -189,6 +189,8 @@ def download_CMEMS(varkey,
copernicusmarine_remove_manual_credentials_file()
copernicusmarine_credentials()

# We floor/ceil the input timestamps to make sure we
# subset enough data in case of data with daily timesteps.
date_min = pd.Timestamp(date_min).floor('1d')
date_max = pd.Timestamp(date_max).ceil('1d')

Expand All @@ -203,14 +205,6 @@ def download_CMEMS(varkey,
latitude_min -= buffer
latitude_max += buffer

Path(dir_output).mkdir(parents=True, exist_ok=True)
date_str = f"{date_min.strftime('%Y%m%d')}_{date_max.strftime('%Y%m%d')}" #TODO: later maybe add subsetting per day/month
name_output = f'{file_prefix}{varkey}_{date_str}.nc'
output_filename = Path(dir_output,name_output)
if output_filename.is_file() and not overwrite:
print(f'"{name_output}" found and overwrite=False, continuing.')
return

dataset = cmc.open_dataset(
dataset_id = dataset_id,
variables = [varkey],
Expand All @@ -222,9 +216,29 @@ def download_CMEMS(varkey,
end_datetime = date_max,
)

print(f'xarray writing netcdf file: {name_output}')
Path(dir_output).mkdir(parents=True, exist_ok=True)

dataset.to_netcdf(output_filename)
if freq is None:
date_str = f"{date_min.strftime('%Y%m%d')}_{date_max.strftime('%Y%m%d')}"
name_output = f'{file_prefix}{varkey}_{date_str}.nc'
output_filename = Path(dir_output,name_output)
if output_filename.is_file() and not overwrite:
print(f'"{name_output}" found and overwrite=False, returning.')
return
print(f'xarray writing netcdf file: {name_output}')
dataset.to_netcdf(output_filename)
else:
period_range = pd.period_range(date_min,date_max,freq=freq)
for date in period_range:
date_str = str(date)
name_output = f'{file_prefix}{varkey}_{date_str}.nc'
output_filename = Path(dir_output,name_output)
if output_filename.is_file() and not overwrite:
print(f'"{name_output}" found and overwrite=False, continuing.')
continue
dataset_perperiod = dataset.sel(time=slice(date_str, date_str))
print(f'xarray writing netcdf file: {name_output}')
dataset_perperiod.to_netcdf(output_filename)


def copernicusmarine_get_product(date_min, date_max):
Expand Down
Loading