|
12 | 12 | # Import aux_funcsiliry functions
|
13 | 13 | from .. import msg
|
14 | 14 | 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 | + |
15 | 28 | def download_era5_from_cds(start_time, end_time, lon, lat, dlon, dlat, folder='dnora_wnd_temp') -> str:
|
16 | 29 | """Downloads ERA5 10 m wind data from the Copernicus Climate Data Store for a
|
17 | 30 | given area and time period"""
|
@@ -45,7 +58,7 @@ def download_era5_from_cds(start_time, end_time, lon, lat, dlon, dlat, folder='d
|
45 | 58 | 'stream': 'wave',
|
46 | 59 | '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',
|
47 | 60 | '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}', |
49 | 62 | 'type': 'an',
|
50 | 63 | 'format': 'netcdf',
|
51 | 64 | }
|
@@ -79,50 +92,39 @@ def __call__(self, start_time, end_time, inds) -> Tuple:
|
79 | 92 | os.remove(f)
|
80 | 93 |
|
81 | 94 | restricted_area = self.get_restricted_area()
|
82 |
| - breakpoint() |
| 95 | + |
83 | 96 | nc_file = download_era5_from_cds(start_time, end_time,
|
84 | 97 | lon=restricted_area.lon_edges(),
|
85 | 98 | lat=restricted_area.lat_edges(),
|
86 | 99 | dlon=restricted_area.dlon(),
|
87 | 100 | dlat=restricted_area.dlat(),
|
88 | 101 | 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 |
| - |
96 | 102 |
|
97 | 103 |
|
98 | 104 |
|
| 105 | + bnd_spec = xr.open_dataset(nc_file) |
99 | 106 |
|
| 107 | + bnd_spec = renormalize_era5_spec(bnd_spec) |
| 108 | + breakpoint() |
100 | 109 |
|
| 110 | + lon, lat = np.meshgrid(bnd_spec.longitude.values, bnd_spec.latitude.values[::-1]) |
| 111 | + lon = lon.ravel() |
| 112 | + lat = lat.ravel() |
101 | 113 |
|
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) |
104 | 118 |
|
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) |
118 | 123 |
|
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 |
125 | 127 |
|
126 |
| - source = f"{bnd.title}, {bnd.institution}" |
| 128 | + source = 'ECMWF-ERA5 from Copernicus Climate Data Store' |
127 | 129 |
|
128 | 130 | return time, freq, dirs, spec, lon, lat, source
|
0 commit comments