1818from copy import deepcopy
1919
2020import netCDF4
21- import numpy as np
2221
23-
24- def validate_dict (o , name ):
25- """
26- Check that the given object is a dictionary. If not, raise TypeError.
27-
28- :param o: The object to validate.
29- :param str name: The name of the object to use in the error message.
30- :return: None
31- """
32-
33- if not isinstance (o , dict ):
34- raise TypeError ("{name} should be a dictionary (got {o})" .format (name = name , o = repr (o )))
22+ from schema import validate_dimensions , validate_variables , validate_attributes
3523
3624
3725class NetCDFGroupDict (object ):
@@ -54,11 +42,11 @@ def __init__(self,
5442 Example:
5543 dmn = {'lon':360,'lat':210}
5644 var = {}
57- var['water'] = {'type':'double','dims ':['lat','lon']}
45+ var['water'] = {'type':'double','dimensions ':['lat','lon']}
5846 w1 = NetCDFGroupDict(dimensions=dmn,variables=var)
5947 dmn2 = {'time':300,'lon':720,'lat':330}
6048 var2 = {}
61- var2['temp'] = {'type':'double','dims ':['time','lat','lon']}
49+ var2['temp'] = {'type':'double','dimensions ':['time','lat','lon']}
6250 w2 = NetCDFGroupDict(dimensions=dmn2,variables=var2)
6351 w3 = w1+w2
6452 #w3.variables.keys() = ['water','temp']
@@ -106,7 +94,7 @@ def dimensions(self):
10694
10795 @dimensions .setter
10896 def dimensions (self , value ):
109- validate_dict (value , 'dimensions' )
97+ validate_dimensions (value )
11098 self ._dimensions = value
11199
112100 @property
@@ -115,7 +103,7 @@ def variables(self):
115103
116104 @variables .setter
117105 def variables (self , value ):
118- validate_dict (value , 'variables' )
106+ validate_variables (value )
119107 self ._variables = value
120108
121109 @property
@@ -124,7 +112,7 @@ def global_attributes(self):
124112
125113 @global_attributes .setter
126114 def global_attributes (self , value ):
127- validate_dict (value , 'global_attributes' )
115+ validate_attributes (value )
128116 self ._global_attributes = value
129117
130118 def is_dim_consistent (self ):
@@ -133,26 +121,26 @@ def is_dim_consistent(self):
133121 checkdims = set ()
134122 for k in self .variables .keys ():
135123 try :
136- for d in self .variables [k ]['dims ' ]:
124+ for d in self .variables [k ]['dimensions ' ]:
137125 checkdims .add (d )
138126 except KeyError :
139127 print ("Variable %s missing dimension information `dims`" % k )
140128
141129 except TypeError :
142- if self .variables [k ]['dims ' ] is None :
130+ if self .variables [k ]['dimensions ' ] is None :
143131 continue
144132
145- missing = ['dims ' ]
133+ missing = ['dimensions ' ]
146134
147135 try :
148136 self .variables ['k' ]['vtype' ]
149137 except KeyError :
150138 missing += ['type' ]
151139
152140 try :
153- self .variables ['k' ]['attr ' ]
141+ self .variables ['k' ]['attributes ' ]
154142 except KeyError :
155- missing += ['attr ' ]
143+ missing += ['attributes ' ]
156144
157145 errstr = "Variable %s is missing information for: "
158146 for _ in missing :
@@ -171,7 +159,7 @@ def search_time_in_vars(self):
171159 tvars = set ()
172160 for v in self .variables :
173161 try :
174- tvars .add (self .variables [v ]['attr ' ]['time' ]['value' ])
162+ tvars .add (self .variables [v ]['attributes ' ]['time' ]['value' ])
175163 except KeyError :
176164 None
177165
@@ -217,11 +205,11 @@ def change_time(self, var, timevar):
217205 if not v_included :
218206 for k in self .variables .keys ():
219207 if v in k :
220- self .variables [k ]['dims ' ][0 ] = t
221- self .variables [k ]['attr ' ]['time' ]['value' ] = t
208+ self .variables [k ]['dimensions ' ][0 ] = t
209+ self .variables [k ]['attributes ' ]['time' ]['value' ] = t
222210 else :
223- self .variables [v ]['dims ' ][0 ] = t
224- self .variables [v ]['attr ' ]['time' ]['value' ] = t
211+ self .variables [v ]['dimensions ' ][0 ] = t
212+ self .variables [v ]['attributes ' ]['time' ]['value' ] = t
225213
226214 @classmethod
227215 def check_dims (self , dimdict ):
@@ -239,9 +227,9 @@ def check_var(self, vardict, name=None):
239227 name = 'input'
240228
241229 vkeys = vardict .keys ()
242- have_dims = 'dims ' in vkeys
230+ have_dims = 'dimensions ' in vkeys
243231 have_type = 'type' in vkeys
244- have_att = 'attr ' in vkeys
232+ have_att = 'attributes ' in vkeys
245233 have_one = have_dims | have_type | have_att
246234 have_none = not have_one
247235
@@ -250,14 +238,14 @@ def check_var(self, vardict, name=None):
250238 self .check_var (vardict [k ], name = k )
251239
252240 if have_dims :
253- notnone = vardict ['dims ' ] is not None
254- notlist = vardict ['dims ' ] is not list
241+ notnone = vardict ['dimensions ' ] is not None
242+ notlist = vardict ['dimensions ' ] is not list
255243 if notnone and notlist :
256244 ValueError (
257245 "Dim for %s should be a None or a list object" % name )
258246
259247 if have_att :
260- notdict = vardict ['attr ' ] is not dict
248+ notdict = vardict ['attributes ' ] is not dict
261249 if notdict :
262250 ValueError ("Attr for %s should be a dictionary object" % name )
263251 if have_type :
@@ -283,7 +271,7 @@ def check_consistency(self, dimdict, vdict):
283271 alldims = dimdict .keys ()
284272 allvars = vdict .keys ()
285273 for k in allvars :
286- vardims = vdict [k ].get ('dims ' )
274+ vardims = vdict [k ].get ('dimensions ' )
287275 if vardims is None :
288276 continue
289277 else :
@@ -346,7 +334,7 @@ def update_dimensinos(self):
346334 continue
347335
348336 var_shape = values .shape
349- var_dims = var .get ('dims ' , [])
337+ var_dims = var .get ('dimensions ' , [])
350338 if len (var_shape ) != len (var_dims ):
351339 raise ValueError (
352340 "Variable '{name}' has {ndim} dimensions, but value array has {nshape} dimensions." .format (
@@ -382,7 +370,7 @@ def createVariables(self, **kwargs):
382370 """
383371 for varname , var in self .variables .iteritems ():
384372 datatype = var ['type' ]
385- dimensions = var ['dims ' ]
373+ dimensions = var ['dimensions ' ]
386374 cwargs = kwargs .copy ()
387375 if dimensions is None : # no kwargs in createVariable
388376 ncvar = self .ncobj .createVariable (varname , datatype )
@@ -422,8 +410,8 @@ def createVariables(self, **kwargs):
422410 ncvar [:] = var ['values' ]
423411
424412 # add variable attributes
425- if var .get ('attr ' ):
426- attrs = var ['attr ' ].copy ()
413+ if var .get ('attributes ' ):
414+ attrs = var ['attributes ' ].copy ()
427415 for not_attr in self ._create_var_opts (attrs ):
428416 attrs .pop (not_attr )
429417 ncvar .setncatts (attrs )
0 commit comments