Skip to content

Commit

Permalink
update era5 with new CDSAPI changes (#145)
Browse files Browse the repository at this point in the history
  • Loading branch information
cuill authored Feb 6, 2025
1 parent 00b1137 commit 592b2fd
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions pyschism/forcing/nws/nws2/era5.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import os
from datetime import datetime, timedelta
import tempfile
import pathlib
from typing import Union
import logging
from zipfile import ZipFile

import appdirs
import numpy as np
import cdsapi
import xarray as xr
import netCDF4 as nc4
from netCDF4 import Dataset
import pandas as pd
Expand All @@ -33,7 +36,7 @@ def __init__(self, start_date=None, rnday: Union[float, timedelta] = 4, bbox=Non
if tmpdir is not None:
self.tmpdir = tmpdir

filename = self.tmpdir / f"era5_{self.start_date.strftime('%Y%m%d')}.nc"
filename = self.tmpdir / f"era5_{self.start_date.strftime('%Y%m%d')}.zip"

if filename.is_file() == False:

Expand All @@ -59,7 +62,20 @@ def __init__(self, start_date=None, rnday: Union[float, timedelta] = 4, bbox=Non
)

r.download(filename)


#unzip file
with ZipFile(filename, 'r') as zip_ref:
zip_ref.extractall(self.tmpdir)
ds = xr.merge(
[xr.open_dataset(self.tmpdir / fname)
for fname in zip_ref.namelist()]
).rename({'avg_tprate': 'mtpr', 'avg_sdlwrf': 'msdwlwrf', 'avg_sdswrf': 'msdwswrf'})

ds.to_netcdf(str(filename).split('.')[0] + '.nc')

[os.remove(fname) for fname in zip_ref.namelist()]
os.remove(filename)

@property
def tmpdir(self):
if not hasattr(self, '_tmpdir'):
Expand Down

0 comments on commit 592b2fd

Please sign in to comment.