11
11
import cdsapi
12
12
# Import aux_funcsiliry functions
13
13
from .. import msg
14
- from ..aux_funcs import create_time_stamps , expand_area , int_list_of_days , int_list_of_months , int_list_of_years
14
+ from ..aux_funcs import create_time_stamps , expand_area , day_list
15
15
16
16
17
17
def renormalize_era5_spec (bnd_spec ):
@@ -25,53 +25,73 @@ def reshape_bnd_spec(bnd_spec):
25
25
pass
26
26
27
27
28
- def download_era5_from_cds (start_time , end_time , lon , lat , dlon , dlat , folder = 'dnora_wnd_temp ' ) -> str :
29
- """Downloads ERA5 10 m wind data from the Copernicus Climate Data Store for a
28
+ def download_era5_from_cds (start_time , end_time , lon , lat , dlon , dlat , folder = 'dnora_bnd_temp ' ) -> str :
29
+ """Downloads ERA5 spectral data from the Copernicus Climate Data Store for a
30
30
given area and time period"""
31
31
start_time = pd .Timestamp (start_time )
32
32
end_time = pd .Timestamp (end_time )
33
33
c = cdsapi .Client ()
34
34
35
- filename = f'{ folder } /EC_ERA5.nc'
35
+ days = day_list (start_time , end_time )
36
+ #tt = pd.date_range(start=start_time,end=end_time)
36
37
37
- years = [f'{ y :4.0f} ' for y in int_list_of_years (start_time , end_time )]
38
- months = [f'{ m :02.0f} ' for m in int_list_of_months (start_time , end_time )]
39
- days = [f'{ d :02.0f} ' for d in int_list_of_days (start_time , end_time )]
38
+ filename = f'{ folder } /EC_ERA5.nc'
40
39
41
40
# Create string for dates
42
- dates = []
43
- for y in years :
44
- for m in months :
45
- for d in days :
46
- dates .append (f'{ y } -{ m } -{ d } ' )
47
- dates = '/' .join (dates )
41
+ #dates = [days[0].strftime('%Y-%m-%d'), days[-1].strftime('%Y-%m-%d')]
42
+ #dates = '/'.join(dates)
43
+ dates = f'{ str (start_time )[0 :10 ]} /to/{ str (end_time )[0 :10 ]} '
48
44
49
45
50
46
cds_command = {
51
47
'class' : 'ea' ,
52
48
'date' : dates ,
53
49
'direction' : '1/2/3/4/5/6/7/8/9/10/11/12/13/14/15/16/17/18/19/20/21/22/23/24' ,
54
50
'domain' : 'g' ,
51
+ 'area' : f'{ lat [1 ]} /{ lon [0 ]} /{ lat [0 ]} /{ lon [1 ]} ' , # north, west, south, east
52
+ 'grid' : f'{ dlat } /{ dlon } ' ,
55
53
'expver' : '1' ,
56
54
'frequency' : '1/2/3/4/5/6/7/8/9/10/11/12/13/14/15/16/17/18/19/20/21/22/23/24/25/26/27/28/29/30' ,
57
55
'param' : '251.140' ,
58
56
'stream' : 'wave' ,
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' ,
60
- 'area' : f'{ lat [1 ]+ 0.0001 } /{ lon [0 ]} /{ lat [0 ]} /{ lon [1 ]+ 0.0001 } ' , # north, west, south, east
61
- 'grid' : f'{ dlon } /{ dlat } ' ,
57
+ 'time' : '00:00:00/01:00:00/02:00:00/03:00:00/04:00:00/05:00:00/06:00:00/07:00:00/08:00:00/09:00:00/10:00:00/11:00:00/12:00:00/13:00:00/14:00:00/15:00:00/16:00:00/17:00:00/18:00:00/19:00:00/20:00:00/21:00:00/22:00:00/23:00:00' ,
62
58
'type' : 'an' ,
63
59
'format' : 'netcdf' ,
64
- }
60
+ }
61
+ #print(cds_command)
62
+
63
+ # cds_command ={
64
+ # 'class': 'ea',
65
+ # 'date': dates,
66
+ # 'direction': '/'.join([f'{n+1:01.0f}' for n in range(24)]),
67
+ # 'domain': 'g',
68
+ # 'expver': '1',
69
+ # 'frequency': '/'.join([f'{n+1:01.0f}' for n in range(30)]),
70
+ # 'param': '251.140',
71
+ # 'stream': 'wave',
72
+ # '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',
73
+ # 'area': f'{lat[1]}/{lon[0]}/{lat[0]}/{lon[1]}', # north, west, south, east
74
+ # 'grid': f'{dlon}/{dlat}',
75
+ # 'type': 'an',
76
+ # 'format': 'netcdf',
77
+ # }
65
78
66
79
c .retrieve ('reanalysis-era5-complete' , cds_command , filename )
67
80
return filename
68
81
class ERA5 (BoundaryReader ):
82
+
83
+ def __init__ (self ):
84
+ self .dlon = 0.5
85
+ self .dlat = 0.5
86
+
69
87
def convention (self ) -> str :
70
88
return 'Ocean'
71
89
72
90
def get_coordinates (self , start_time ) -> Tuple :
73
91
"""Reads first time instance of first file to get longitudes and latitudes for the PointPicker"""
74
- point_list = self .get_restricted_area ()._point_list ()
92
+ restricted_area = self .get_restricted_area ()
93
+ restricted_area .set_spacing (dlon = self .dlon , dlat = self .dlat )
94
+ point_list = restricted_area ._point_list ()
75
95
lon_all = point_list [:,0 ]
76
96
lat_all = point_list [:,1 ]
77
97
@@ -87,23 +107,31 @@ def __call__(self, start_time, end_time, inds) -> Tuple:
87
107
os .mkdir (temp_folder )
88
108
print ("Creating folder %s..." % temp_folder )
89
109
90
- msg .plain ("Removing old files from temporary folder..." )
91
- for f in glob .glob (f"{ temp_folder } /EC_ERA5.nc" ):
92
- os .remove (f )
110
+ local_read = False
111
+
112
+ if not local_read :
113
+ msg .plain ("Removing old files from temporary folder..." )
114
+ for f in glob .glob (f"{ temp_folder } /EC_ERA5.nc" ):
115
+ os .remove (f )
93
116
94
117
restricted_area = self .get_restricted_area ()
118
+ lon = np .floor (np .array (restricted_area .lon_edges ())/ self .dlon )* self .dlon
119
+ lat = np .floor (np .array (restricted_area .lat_edges ())/ self .dlat )* self .dlat
95
120
96
- nc_file = download_era5_from_cds (start_time , end_time ,
97
- lon = restricted_area .lon_edges (),
98
- lat = restricted_area .lat_edges (),
99
- dlon = restricted_area .dlon (),
100
- dlat = restricted_area .dlat (),
101
- folder = temp_folder )
102
121
103
122
123
+ if local_read :
124
+ nc_file = f'{ temp_folder } /EC_ERA5.nc'
125
+ else :
126
+ nc_file = download_era5_from_cds (start_time , end_time ,
127
+ lon = (lon [0 ]- self .dlon , lon [1 ]+ self .dlon ),
128
+ lat = (lat [0 ]- self .dlat , lat [1 ]+ self .dlat ),
129
+ dlon = self .dlon ,
130
+ dlat = self .dlat ,
131
+ folder = temp_folder )
104
132
105
133
bnd_spec = xr .open_dataset (nc_file )
106
-
134
+ bnd_spec = bnd_spec . sortby ( "time" )
107
135
bnd_spec = renormalize_era5_spec (bnd_spec )
108
136
109
137
lon , lat = np .meshgrid (bnd_spec .longitude .values , bnd_spec .latitude .values [::- 1 ])
0 commit comments