Skip to content

Commit c911114

Browse files
committed
era5 boundary reader now runs
1 parent 5bc7b8e commit c911114

File tree

2 files changed

+35
-31
lines changed

2 files changed

+35
-31
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ dnora/trg/__pycache__
77
dnora/grd/__pycache__
88
dnora/mdl/__pycache__
99
dnora/examples/output
10+
dnora/output
11+
dnora/jobs

dnora/bnd/read_ec.py

+33-31
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,19 @@
1212
# Import aux_funcsiliry functions
1313
from .. import msg
1414
from ..aux_funcs import create_time_stamps, expand_area, int_list_of_days, int_list_of_months, int_list_of_years
15+
16+
17+
def renormalize_era5_spec(bnd_spec):
18+
bnd_spec = bnd_spec.assign_coords(direction=np.arange(7.5, 352.5 + 15, 15))
19+
bnd_spec = bnd_spec.assign_coords(frequency=np.full(30, 0.03453) * (1.1 ** np.arange(0, 30)))
20+
bnd_spec = 10 ** bnd_spec
21+
bnd_spec = bnd_spec.fillna(0)
22+
return bnd_spec
23+
24+
def reshape_bnd_spec(bnd_spec):
25+
pass
26+
27+
1528
def download_era5_from_cds(start_time, end_time, lon, lat, dlon, dlat, folder='dnora_wnd_temp') -> str:
1629
"""Downloads ERA5 10 m wind data from the Copernicus Climate Data Store for a
1730
given area and time period"""
@@ -45,7 +58,7 @@ def download_era5_from_cds(start_time, end_time, lon, lat, dlon, dlat, folder='d
4558
'stream': 'wave',
4659
'time': '00:00:00/03:00:00/06:00:00/09:00:00/12:00:00/15:00:00/18:00:00/21:00:00',
4760
'area': f'{lat[1]}/{lon[0]}/{lat[0]}/{lon[1]}', # north, west, south, east
48-
'grid': f'{dlat:.4f}/{dlon:.4f}', # latitude/longitude
61+
'grid': f'{dlon:.4f}/{dlat:.4f}',
4962
'type': 'an',
5063
'format': 'netcdf',
5164
}
@@ -79,50 +92,39 @@ def __call__(self, start_time, end_time, inds) -> Tuple:
7992
os.remove(f)
8093

8194
restricted_area = self.get_restricted_area()
82-
breakpoint()
95+
8396
nc_file = download_era5_from_cds(start_time, end_time,
8497
lon=restricted_area.lon_edges(),
8598
lat=restricted_area.lat_edges(),
8699
dlon=restricted_area.dlon(),
87100
dlat=restricted_area.dlat(),
88101
folder=temp_folder)
89-
breakpoint()
90-
wind_forcing = xr.open_dataset(nc_file)
91-
wind_forcing = wind_forcing.rename_dims({'longitude': 'lon', 'latitude': 'lat'})
92-
wind_forcing = wind_forcing.rename_vars({'longitude': 'lon', 'latitude': 'lat'})
93-
wind_forcing = wind_forcing.rename_vars({'u10': 'u', 'v10': 'v'})
94-
95-
96102

97103

98104

105+
bnd_spec = xr.open_dataset(nc_file)
99106

107+
bnd_spec = renormalize_era5_spec(bnd_spec)
108+
breakpoint()
100109

110+
lon, lat = np.meshgrid(bnd_spec.longitude.values, bnd_spec.latitude.values[::-1])
111+
lon = lon.ravel()
112+
lat = lat.ravel()
101113

102-
self.start_time = start_time
103-
self.end_time = end_time
114+
# This spec is time, freq, dir, lat, lon
115+
spec = bnd_spec.d2fd.values
116+
# Latitude was flipped to be ascending, so flip that dimension
117+
spec = np.flip(spec, 3)
104118

105-
start_times, end_times, file_times = create_time_stamps(start_time, end_time, stride = self.stride, hours_per_file = self.hours_per_file, last_file = self.last_file, lead_time = self.lead_time)
106-
#days = day_list(start_time = self.start_time, end_time = self.end_time)
107-
msg.info(f"Getting boundary spectra from NORA3 from {self.start_time} to {self.end_time}")
108-
bnd_list = []
109-
for n in range(len(file_times)):
110-
url = self.get_url(file_times[n])
111-
msg.from_file(url)
112-
msg.plain(f"Reading boundary spectra: {start_times[n]}-{end_times[n]}")
113-
with xr.open_dataset(url) as f:
114-
this_ds = f.sel(time = slice(start_times[n], end_times[n]), x = (inds+1))[['SPEC', 'longitude', 'latitude', 'time', 'freq', 'direction']].copy()
115-
bnd_list.append(this_ds)
116-
#bnd_list.append(xr.open_dataset(url).sel(time = slice(start_times[n], end_times[n]), x = (inds+1)))
117-
bnd=xr.concat(bnd_list, dim="time").squeeze('y')
119+
# This is time, freq, dir, station
120+
spec = np.reshape(spec, (len(bnd_spec.time),len(bnd_spec.frequency),len(bnd_spec.direction),len(lon)))
121+
# This is time, station, freq, dir (as we want it)
122+
spec = np.moveaxis(spec,3,1)
118123

119-
time = bnd.time.values
120-
freq = bnd.freq.values
121-
dirs = bnd.direction.values
122-
spec = bnd.SPEC.values
123-
lon = bnd.longitude.values[0,:]
124-
lat = bnd.latitude.values[0,:]
124+
freq = bnd_spec.frequency.values
125+
dirs = bnd_spec.direction.values
126+
time = bnd_spec.time.values
125127

126-
source = f"{bnd.title}, {bnd.institution}"
128+
source = 'ECMWF-ERA5 from Copernicus Climate Data Store'
127129

128130
return time, freq, dirs, spec, lon, lat, source

0 commit comments

Comments
 (0)