10
10
11
11
12
12
class CFDataset (EnhancedDataset ):
13
-
14
13
default_fill_value = False
15
- default_time_unit = ' seconds since 1990-01-01 00:00:00Z'
14
+ default_time_unit = " seconds since 1990-01-01 00:00:00Z"
16
15
17
16
@classmethod
18
17
def load (cls , path ):
@@ -45,38 +44,38 @@ def load(cls, path):
45
44
try :
46
45
dsg = cls (path )
47
46
for klass in subs :
48
- logger .debug (f' Trying { klass .__name__ } ...' )
49
- if hasattr (klass , ' is_mine' ):
47
+ logger .debug (f" Trying { klass .__name__ } ..." )
48
+ if hasattr (klass , " is_mine" ):
50
49
if klass .is_mine (dsg ):
51
50
return klass (path )
52
51
except OSError :
53
52
raise
54
53
finally :
55
- if hasattr (dsg , ' close' ):
54
+ if hasattr (dsg , " close" ):
56
55
dsg .close ()
57
56
58
- subnames = ', ' .join ([ s .__name__ for s in subs ])
59
- raise ValueError (
60
- 'Could not open {} as any type of CF Dataset. Tried: {}.' .format (
61
- path ,
62
- subnames
63
- )
64
- )
57
+ subnames = ", " .join ([s .__name__ for s in subs ])
58
+ raise ValueError (f"Could not open { path } as any type of CF Dataset. Tried: { subnames } ." )
65
59
66
60
def axes (self , name ):
67
- return getattr (self , f' { name .lower ()} _axes' )()
61
+ return getattr (self , f" { name .lower ()} _axes" )()
68
62
69
63
def t_axes (self ):
70
-
71
64
# If there is only one variable with the axis parameter, return it
72
- hasaxis = self .filter_by_attrs (axis = lambda x : x and str (x ).lower () == 't' )
65
+ hasaxis = self .filter_by_attrs (axis = lambda x : x and str (x ).lower () == "t" )
73
66
if len (hasaxis ) == 1 :
74
67
return hasaxis
75
68
76
- tvars = list (set (itertools .chain (
77
- hasaxis ,
78
- self .filter_by_attrs (standard_name = lambda x : x in ['time' , 'forecast_reference_time' ])
79
- )))
69
+ tvars = list (
70
+ set (
71
+ itertools .chain (
72
+ hasaxis ,
73
+ self .filter_by_attrs (
74
+ standard_name = lambda x : x in ["time" , "forecast_reference_time" ]
75
+ ),
76
+ )
77
+ )
78
+ )
80
79
return tvars
81
80
82
81
def x_axes (self ):
@@ -87,76 +86,74 @@ def x_axes(self):
87
86
* The `standard_name` property is one of ``'longitude'``,
88
87
``'projection_x_coordinate'`` or ``'grid_longitude'``
89
88
"""
90
- xnames = ['longitude' , 'grid_longitude' , 'projection_x_coordinate' ]
91
- xunits = [
92
- 'degrees_east' ,
93
- 'degree_east' ,
94
- 'degree_E' ,
95
- 'degrees_E' ,
96
- 'degreeE' ,
97
- 'degreesE'
98
- ]
89
+ xnames = ["longitude" , "grid_longitude" , "projection_x_coordinate" ]
90
+ xunits = ["degrees_east" , "degree_east" , "degree_E" , "degrees_E" , "degreeE" , "degreesE" ]
99
91
100
92
# If there is only one variable with the axis parameter, return it
101
- hasaxis = self .filter_by_attrs (axis = lambda x : x and str (x ).lower () == 'x' )
93
+ hasaxis = self .filter_by_attrs (axis = lambda x : x and str (x ).lower () == "x" )
102
94
if len (hasaxis ) == 1 :
103
95
return hasaxis
104
96
105
- xvars = list (set (itertools .chain (
106
- hasaxis ,
107
- self .filter_by_attrs (standard_name = lambda x : x and str (x ).lower () in xnames ),
108
- self .filter_by_attrs (units = lambda x : x and str (x ).lower () in xunits )
109
- )))
97
+ xvars = list (
98
+ set (
99
+ itertools .chain (
100
+ hasaxis ,
101
+ self .filter_by_attrs (standard_name = lambda x : x and str (x ).lower () in xnames ),
102
+ self .filter_by_attrs (units = lambda x : x and str (x ).lower () in xunits ),
103
+ )
104
+ )
105
+ )
110
106
return xvars
111
107
112
108
def y_axes (self ):
113
- ynames = ['latitude' , 'grid_latitude' , 'projection_y_coordinate' ]
114
- yunits = [
115
- 'degrees_north' ,
116
- 'degree_north' ,
117
- 'degree_N' ,
118
- 'degrees_N' ,
119
- 'degreeN' ,
120
- 'degreesN'
121
- ]
109
+ ynames = ["latitude" , "grid_latitude" , "projection_y_coordinate" ]
110
+ yunits = ["degrees_north" , "degree_north" , "degree_N" , "degrees_N" , "degreeN" , "degreesN" ]
122
111
123
112
# If there is only one variable with the axis parameter, return it
124
- hasaxis = self .filter_by_attrs (axis = lambda x : x and str (x ).lower () == 'y' )
113
+ hasaxis = self .filter_by_attrs (axis = lambda x : x and str (x ).lower () == "y" )
125
114
if len (hasaxis ) == 1 :
126
115
return hasaxis
127
116
128
- yvars = list (set (itertools .chain (
129
- hasaxis ,
130
- self .filter_by_attrs (standard_name = lambda x : x and str (x ).lower () in ynames ),
131
- self .filter_by_attrs (units = lambda x : x and str (x ).lower () in yunits )
132
- )))
117
+ yvars = list (
118
+ set (
119
+ itertools .chain (
120
+ hasaxis ,
121
+ self .filter_by_attrs (standard_name = lambda x : x and str (x ).lower () in ynames ),
122
+ self .filter_by_attrs (units = lambda x : x and str (x ).lower () in yunits ),
123
+ )
124
+ )
125
+ )
133
126
return yvars
134
127
135
128
def z_axes (self ):
136
129
znames = [
137
- ' atmosphere_ln_pressure_coordinate' ,
138
- ' atmosphere_sigma_coordinate' ,
139
- ' atmosphere_hybrid_sigma_pressure_coordinate' ,
140
- ' atmosphere_hybrid_height_coordinate' ,
141
- ' atmosphere_sleve_coordinate' ,
142
- ' ocean_sigma_coordinate' ,
143
- ' ocean_s_coordinate' ,
144
- ' ocean_s_coordinate_g1' ,
145
- ' ocean_s_coordinate_g2' ,
146
- ' ocean_sigma_z_coordinate' ,
147
- ' ocean_double_sigma_coordinate'
130
+ " atmosphere_ln_pressure_coordinate" ,
131
+ " atmosphere_sigma_coordinate" ,
132
+ " atmosphere_hybrid_sigma_pressure_coordinate" ,
133
+ " atmosphere_hybrid_height_coordinate" ,
134
+ " atmosphere_sleve_coordinate" ,
135
+ " ocean_sigma_coordinate" ,
136
+ " ocean_s_coordinate" ,
137
+ " ocean_s_coordinate_g1" ,
138
+ " ocean_s_coordinate_g2" ,
139
+ " ocean_sigma_z_coordinate" ,
140
+ " ocean_double_sigma_coordinate" ,
148
141
]
149
142
150
143
# If there is only one variable with the axis parameter, return it
151
- hasaxis = self .filter_by_attrs (axis = lambda x : x and str (x ).lower () == 'z' )
144
+ hasaxis = self .filter_by_attrs (axis = lambda x : x and str (x ).lower () == "z" )
152
145
if len (hasaxis ) == 1 :
153
146
return hasaxis
154
147
155
- zvars = list (set (itertools .chain (
156
- hasaxis ,
157
- self .filter_by_attrs (positive = lambda x : x and str (x ).lower () in ['up' , 'down' ]),
158
- self .filter_by_attrs (standard_name = lambda x : x and str (x ).lower () in znames )
159
- )))
148
+ zvars = list (
149
+ set (
150
+ itertools .chain (
151
+ hasaxis ,
152
+ self .filter_by_attrs (positive = lambda x : x and str (x ).lower () in ["up" , "down" ]),
153
+ self .filter_by_attrs (standard_name = lambda x : x and str (x ).lower () in znames ),
154
+ )
155
+ )
156
+ )
160
157
return zvars
161
158
162
159
def is_valid (self , * args , ** kwargs ):
@@ -169,34 +166,32 @@ def data_vars(self):
169
166
standard_name = lambda x : x is not None ,
170
167
flag_values = lambda x : x is None ,
171
168
flag_masks = lambda x : x is None ,
172
- flag_meanings = lambda x : x is None
169
+ flag_meanings = lambda x : x is None ,
173
170
)
174
171
175
172
def ancillary_vars (self ):
176
173
ancillary_variables = []
177
- for rv in self .filter_by_attrs (
178
- ancillary_variables = lambda x : x is not None
179
- ):
174
+ for rv in self .filter_by_attrs (ancillary_variables = lambda x : x is not None ):
180
175
# Space separated ancillary variables
181
- for av in rv .ancillary_variables .split (' ' ):
176
+ for av in rv .ancillary_variables .split (" " ):
182
177
if av in self .variables :
183
178
ancillary_variables .append (self .variables [av ])
184
179
return list (set (ancillary_variables ))
185
180
186
181
def nc_attributes (self ):
187
182
return {
188
- ' global' : {
189
- ' Conventions' : ' CF-1.6' ,
190
- ' date_created' : datetime .utcnow ().strftime ("%Y-%m-%dT%H:%M:00Z" ),
183
+ " global" : {
184
+ " Conventions" : " CF-1.6" ,
185
+ " date_created" : datetime .utcnow ().strftime ("%Y-%m-%dT%H:%M:00Z" ),
191
186
}
192
187
}
193
188
194
189
195
190
def cf_safe_name (name ):
196
191
if isinstance (name , str ):
197
- if re .match (' ^[0-9_]' , name ):
192
+ if re .match (" ^[0-9_]" , name ):
198
193
# Add a letter to the front
199
194
name = f"v_{ name } "
200
- return re .sub (r' [^_a-zA-Z0-9]' , "_" , name )
195
+ return re .sub (r" [^_a-zA-Z0-9]" , "_" , name )
201
196
202
197
raise ValueError (f'Could not convert "{ name } " to a safe name' )
0 commit comments