1
- from abc import ABC , abstractmethod
1
+ from abc import ABC , abstractmethod
2
2
from copy import copy
3
3
import numpy as np
4
4
import xarray as xr
5
5
from subprocess import call
6
6
import os , glob
7
7
import time
8
+
8
9
# Import objects
9
10
from ..grd .grd_mod import Grid
10
11
13
14
14
15
# Import aux_funcsiliry functions
15
16
from .. import msg
16
- from ..aux_funcs import create_time_stamps , u_v_from_dir , expand_area , lon_in_km , pyfimex
17
+ from ..aux_funcs import (
18
+ create_time_stamps ,
19
+ u_v_from_dir ,
20
+ expand_area ,
21
+ lon_in_km ,
22
+ pyfimex ,
23
+ )
17
24
18
25
19
26
class NorKyst800 (OceanCurrentReader ):
@@ -27,7 +34,14 @@ class NorKyst800(OceanCurrentReader):
27
34
No. 1 : User Manual and technical descriptions.
28
35
"""
29
36
30
- def __init__ (self , stride : int = 24 , hours_per_file : int = 24 , last_file : str = '' , lead_time : int = 0 , program : str = 'pyfimex' ):
37
+ def __init__ (
38
+ self ,
39
+ stride : int = 24 ,
40
+ hours_per_file : int = 24 ,
41
+ last_file : str = "" ,
42
+ lead_time : int = 0 ,
43
+ program : str = "pyfimex" ,
44
+ ):
31
45
"""The data is currently in daily files. Do not change the default
32
46
setting unless you have a good reason to do so.
33
47
"""
@@ -39,18 +53,27 @@ def __init__(self, stride: int=24, hours_per_file: int=24, last_file: str='', le
39
53
self .program = program
40
54
return
41
55
42
- def __call__ (self , grid : Grid , start_time : str , end_time : str , expansion_factor : float ):
56
+ def __call__ (
57
+ self , grid : Grid , start_time : str , end_time : str , expansion_factor : float
58
+ ):
43
59
"""Reads in all grid points between the given times and at for the given indeces"""
44
60
self .start_time = start_time
45
61
self .end_time = end_time
46
62
47
63
start_times , end_times , file_times = create_time_stamps (
48
- start_time , end_time , self .stride , self .hours_per_file , self .last_file , self .lead_time )
64
+ start_time ,
65
+ end_time ,
66
+ self .stride ,
67
+ self .hours_per_file ,
68
+ self .last_file ,
69
+ self .lead_time ,
70
+ )
49
71
50
72
msg .info (
51
- f"Getting ocean_current forcing from Norkyst800 from { self .start_time } to { self .end_time } " )
73
+ f"Getting ocean_current forcing from Norkyst800 from { self .start_time } to { self .end_time } "
74
+ )
52
75
53
- temp_folder = ' dnora_ocr_temp'
76
+ temp_folder = " dnora_ocr_temp"
54
77
if not os .path .isdir (temp_folder ):
55
78
os .mkdir (temp_folder )
56
79
print ("Creating folder %s..." % temp_folder )
@@ -59,70 +82,110 @@ def __call__(self, grid: Grid, start_time: str, end_time: str, expansion_factor:
59
82
for f in glob .glob ("dnora_ocr_temp/*MetNo_Norkyst800.nc" ):
60
83
os .remove (f )
61
84
62
-
63
85
# Define area to search in
64
- lon_min , lon_max , lat_min , lat_max = expand_area (min (grid .lon ()), max (grid .lon ()), min (grid .lat ()), max (grid .lat ()), expansion_factor )
86
+ lon_min , lon_max , lat_min , lat_max = expand_area (
87
+ min (grid .lon ()),
88
+ max (grid .lon ()),
89
+ min (grid .lat ()),
90
+ max (grid .lat ()),
91
+ expansion_factor ,
92
+ )
65
93
66
94
# Setting resolution to roughly 0.8 km
67
- dlat = 0.8 / 111
68
- mean_lon_in_km = (lon_in_km (grid .lat ()[0 ])+ lon_in_km (grid .lat ()[- 1 ]))* 0.5
69
- dlon = 0.8 / mean_lon_in_km
95
+ dlat = 0.8 / 111
96
+ mean_lon_in_km = (lon_in_km (grid .lat ()[0 ]) + lon_in_km (grid .lat ()[- 1 ])) * 0.5
97
+ dlon = 0.8 / mean_lon_in_km
70
98
71
99
ocr_list = []
72
- print (' Apply >>> ' + self .program )
100
+ print (" Apply >>> " + self .program )
73
101
for n in range (len (file_times )):
74
102
url = self .get_url (file_times [n ])
75
103
76
104
msg .from_file (url )
77
105
msg .plain (
78
- f"Reading ocean_current forcing data: { start_times [n ]} -{ end_times [n ]} " )
106
+ f"Reading ocean_current forcing data: { start_times [n ]} -{ end_times [n ]} "
107
+ )
79
108
80
- nc_fimex = f' dnora_ocr_temp/ocean_current_{ n :04.0f} _MetNo_Norkyst800.nc'
109
+ nc_fimex = f" dnora_ocr_temp/ocean_current_{ n :04.0f} _MetNo_Norkyst800.nc"
81
110
# Apply pyfimex or fimex
82
- if self .program == 'pyfimex' :
83
- pyfimex (input_file = url ,output_file = nc_fimex ,
111
+ if self .program == "pyfimex" :
112
+ pyfimex (
113
+ input_file = url ,
114
+ output_file = nc_fimex ,
84
115
projString = "+proj=latlong +ellps=sphere +a=6371000 +e=0" ,
85
- xAxisValues = np .arange (lon_min ,lon_max + dlon ,dlon ),
86
- yAxisValues = np .arange (lat_min ,lat_max + dlat ,dlat ),
87
- selectVariables = ['u' , 'v' ],
88
- reduceTime_start = start_times [n ].strftime ('%Y-%m-%dT%H:%M:%S' ),
89
- reduceTime_end = end_times [n ].strftime ('%Y-%m-%dT%H:%M:%S' ))
90
- elif self .program == 'fimex' :
91
- fimex_command = ['fimex' , '--input.file=' + url ,
92
- '--interpolate.method=bilinear' ,
93
- '--interpolate.projString=+proj=latlong +ellps=sphere +a=6371000 +e=0' ,
94
- '--interpolate.xAxisValues=' + str (lon_min )+ ',' + str (lon_min + dlon )+ ',...,' + str (lon_max )+ '' ,
95
- '--interpolate.yAxisValues=' + str (lat_min )+ ',' + str (lat_min + dlat )+ ',...,' + str (lat_max )+ '' ,
96
- '--interpolate.xAxisUnit=degree' , '--interpolate.yAxisUnit=degree' ,
97
- '--process.rotateVector.all' ,
98
- '--extract.selectVariables=u' , '--extract.selectVariables=v' ,
99
- '--extract.reduceTime.start=' + \
100
- start_times [n ].strftime ('%Y-%m-%dT%H:%M:%S' ),
101
- '--extract.reduceTime.end=' + \
102
- end_times [n ].strftime ('%Y-%m-%dT%H:%M:%S' ),
103
- '--process.rotateVector.direction=latlon' ,
104
- #'--extract.reduceDimension.name=depth',
105
- #'--extract.reduceDimension.start=0',
106
- #'--extract.reduceDimension.end=0',
107
- '--output.file=' + nc_fimex ]
116
+ xAxisValues = np .arange (lon_min , lon_max + dlon , dlon ),
117
+ yAxisValues = np .arange (lat_min , lat_max + dlat , dlat ),
118
+ selectVariables = ["u" , "v" ],
119
+ reduceTime_start = start_times [n ].strftime ("%Y-%m-%dT%H:%M:%S" ),
120
+ reduceTime_end = end_times [n ].strftime ("%Y-%m-%dT%H:%M:%S" ),
121
+ )
122
+ elif self .program == "fimex" :
123
+ fimex_command = [
124
+ "fimex" ,
125
+ "--input.file=" + url ,
126
+ "--interpolate.method=bilinear" ,
127
+ "--interpolate.projString=+proj=latlong +ellps=sphere +a=6371000 +e=0" ,
128
+ "--interpolate.xAxisValues="
129
+ + str (lon_min )
130
+ + ","
131
+ + str (lon_min + dlon )
132
+ + ",...,"
133
+ + str (lon_max )
134
+ + "" ,
135
+ "--interpolate.yAxisValues="
136
+ + str (lat_min )
137
+ + ","
138
+ + str (lat_min + dlat )
139
+ + ",...,"
140
+ + str (lat_max )
141
+ + "" ,
142
+ "--interpolate.xAxisUnit=degree" ,
143
+ "--interpolate.yAxisUnit=degree" ,
144
+ "--process.rotateVector.all" ,
145
+ "--extract.selectVariables=u" ,
146
+ "--extract.selectVariables=v" ,
147
+ "--extract.reduceTime.start="
148
+ + start_times [n ].strftime ("%Y-%m-%dT%H:%M:%S" ),
149
+ "--extract.reduceTime.end="
150
+ + end_times [n ].strftime ("%Y-%m-%dT%H:%M:%S" ),
151
+ "--process.rotateVector.direction=latlon" ,
152
+ #'--extract.reduceDimension.name=depth',
153
+ #'--extract.reduceDimension.start=0',
154
+ #'--extract.reduceDimension.end=0',
155
+ "--output.file=" + nc_fimex ,
156
+ ]
108
157
call (fimex_command )
109
158
ocr_list .append (xr .open_dataset (nc_fimex ).squeeze ())
110
159
111
160
oceancurrent_forcing = xr .concat (ocr_list , dim = "time" )
112
161
# Rename X/Y to lon/lat
113
- oceancurrent_forcing = oceancurrent_forcing .rename_dims ({'Y' : 'lat' , 'X' : 'lon' })
114
- oceancurrent_forcing = oceancurrent_forcing .rename_vars ({'Y' : 'lat' , 'X' : 'lon' })
162
+ oceancurrent_forcing = oceancurrent_forcing .rename_dims (
163
+ {"Y" : "lat" , "X" : "lon" }
164
+ )
165
+ oceancurrent_forcing = oceancurrent_forcing .rename_vars (
166
+ {"Y" : "lat" , "X" : "lon" }
167
+ )
115
168
# Select depth = 0 m
116
169
oceancurrent_forcing = oceancurrent_forcing .sel (depth = 0 )
117
170
118
- oceancurrent_forcing ['u' ] = oceancurrent_forcing ['u' ].fillna (0 )
119
- oceancurrent_forcing ['v' ] = oceancurrent_forcing ['v' ].fillna (0 )
171
+ oceancurrent_forcing ["u" ] = oceancurrent_forcing ["u" ].fillna (0 )
172
+ oceancurrent_forcing ["v" ] = oceancurrent_forcing ["v" ].fillna (0 )
120
173
121
- oceancurrent_forcing = oceancurrent_forcing .transpose (' time' , ' lat' , ' lon' )
174
+ oceancurrent_forcing = oceancurrent_forcing .transpose (" time" , " lat" , " lon" )
122
175
123
176
return oceancurrent_forcing
124
177
125
178
def get_url (self , time_stamp ):
126
- filename = 'NorKyst-800m_ZDEPTHS_his.an.' + time_stamp .strftime ('%Y%m%d' )+ '00.nc'
127
- url = 'https://thredds.met.no/thredds/dodsC/sea/norkyst800mv0_1h/' + filename
179
+ filename = (
180
+ "NorKyst-800m_ZDEPTHS_his.an." + time_stamp .strftime ("%Y%m%d" ) + "00.nc"
181
+ )
182
+ if time_stamp .year < 2018 :
183
+
184
+ url = (
185
+ "https://thredds.met.no/thredds/dodsC/sea/norkyst800mv0_1h/" + filename
186
+ )
187
+ else :
188
+ url = (
189
+ "https://thredds.met.no/thredds/dodsC/fou-hi/norkyst800m-1h/" + filename
190
+ )
128
191
return url
0 commit comments