Changeset 26063
- Timestamp:
- 03/10/21 01:09:38 (4 years ago)
- Location:
- issm/trunk-jpl/src/m
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
issm/trunk-jpl/src/m/contrib/defleurian/netCDF/export_netCDF.py
r25819 r26063 3 3 import time 4 4 import collections 5 from inspect import isclass , getmodule5 from inspect import isclass 6 6 from os import path, remove 7 7 … … 47 47 except AttributeError: #no transient so just one timestep 48 48 StepNum = 1 49 except TypeError: #this isnot a result so no results in there 50 StepNum = 0 49 51 TimeDim = NCData.createDimension('Time', StepNum) # time is first 50 52 DimDict = {len(TimeDim): 'Time'} … … 80 82 NCgroup = NCData.createGroup(str(group)) 81 83 # In each group gather the fields of the class 82 fields = dict.keys(md.__dict__[group].__dict__) 84 try: 85 fields = dict.keys(md.__dict__[group].__dict__) 86 except AttributeError: 87 print("WARNING: md.{} as no fields, we skip it.".format(group)) 88 continue 83 89 # looping on fields in each group 84 90 for field in fields: … … 220 226 if ncvar is not None: 221 227 FillVar(ncvar, StackedVar) 228 elif type(md.__dict__[group].__dict__[field]).__name__ == 'dict': 229 # designed for a dict in dummy but might be used elsewhere 230 # there is no subgroup 231 klass = type(md.__dict__[group]).__module__ + '.' + type(md.__dict__[group]).__name__ 232 NCgroup.__setattr__('classtype', klass) 233 Var = md.__dict__[group].__dict__[field] 234 Var = SqueezeVar(Var) 235 if verbose > 4: 236 print("=WW=creating var for {}.{}".format(group, field)) 237 DimDict, ncvar = CreateVar(NCData, Var, field, NCgroup, DimDict) 238 if ncvar is not None: 239 FillVar(ncvar, Var) 222 240 else: 223 241 klass = type(md.__dict__[group]).__module__ + '.' + type(md.__dict__[group]).__name__ … … 258 276 # grab dimension 259 277 if val_type in [collections.OrderedDict, dict]: 260 val_shape = len(var )278 val_shape = len(var.keys()) 261 279 val_dim = 2 262 280 else: … … 304 322 ncvar = Group.createVariable(str(field), nctype, dimensions=dimensions, zlib=True) 305 323 # treating bool tables and dict as string tables 306 elif val_type in [ 'bool', collections.OrderedDict, dict]:324 elif val_type in [collections.OrderedDict, dict, 'bool']: 307 325 if val_shape in [(), (0,), 0]: 308 326 ncvar = Group.createVariable(str(field), str, zlib=True) … … 310 328 dimensions, DimDict = GetDim(NCData, val_shape, val_type, DimDict, val_dim) 311 329 ncvar = Group.createVariable(str(field), str, dimensions=dimensions, zlib=True) 312 330 # Now dealing with numeric variables 313 331 elif val_type in [float, 'float64', np.float64, int, 'int64']: 314 332 if val_shape in [(), (0,), 0] and not SupDim: -
issm/trunk-jpl/src/m/io/loadvars.py
r25819 r26063 88 88 #==== First we create the model structure {{{ 89 89 if debug: 90 print(' -Now treating classtype {}'.format(mod))90 print(' ==== Now treating classtype {}'.format(mod)) 91 91 if np.size(classtree[mod]) > 1: 92 92 # this points to a subclass (results.TransientSolution for example) 93 93 curclass = NCFile.groups[classtree[mod][0]].groups[classtree[mod][1]] 94 94 if debug: 95 print(" ===> {} is of class {}".format(mod, classtype[mod]))95 print(" ==> {} is of class {}".format(mod, classtype[mod])) 96 96 if classtype[mod][0] == 'results.solutionstep': #Treating results {{{ 97 97 keylist = [key for key in curclass.groups] … … 100 100 #here we have a more NC approach with time being a dimension 101 101 listtype = split(r'\.', classtype[mod][0])[0] 102 print("listtype is {}".format(listtype))103 102 if len(NCFile.dimensions['Time']) == 1: 104 103 nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]] = getattr(classtype[mod][1], listtype)() … … 126 125 else: 127 126 if debug: 128 print(" Using the default for md.{}.{}, is that right??".format(classtree[mod][0], classtree[mod][1]))127 print(" Using the default for md.{}.{}, is that right??".format(classtree[mod][0], classtree[mod][1])) 129 128 try: 130 129 modulename = split(r'\.', classtype[mod][0])[0] 131 130 if debug: 132 print(" trying to import {} from {}".format(classtype[mod][0], modulename))131 print(" trying to import {} from {}".format(classtype[mod][0], modulename)) 133 132 nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]] = getattr(classtype[mod][1], modulename)() 134 133 except AttributeError: … … 141 140 Tree = nvdict['md'].__dict__[classtree[mod][0]] 142 141 if debug: 143 print(" for {} Tree is a {}".format(mod, Tree.__class__.__name__))142 print(" for {} Tree is a {}".format(mod, Tree.__class__.__name__)) 144 143 # }}} 145 144 #==== Then we populate it {{{ … … 152 151 for var in listclass.variables: 153 152 if debug: 154 print(" treating var {}".format(var))153 print(" ==> treating var {}".format(var)) 155 154 if var not in ['errlog', 'outlog']: 156 155 varval = listclass.variables[str(var)] … … 234 233 elif vardim == 2: 235 234 #dealling with dict 236 if varval.dtype == str: #that is for toolkits wich needs to be ordered235 if varval.dtype == str: #that is for dictionaries 237 236 if any(varval[:, 0] == 'toolkit'): #toolkit definition have to be first 238 237 Tree.__dict__[str(var)] = OrderedDict([('toolkit', str(varval[np.where(varval[:, 0] == 'toolkit')[0][0], 1]))]) … … 240 239 strings2 = [str(arg[1]) for arg in varval if arg[0] != 'toolkits'] 241 240 Tree.__dict__[str(var)].update(list(zip(strings1, strings2))) 241 else: 242 strings1 = [str(arg[0]) for arg in varval] 243 strings2 = [str(arg[1]) for arg in varval] 244 Tree.__dict__[str(var)] = OrderedDict(list(zip(strings1, strings2))) 242 245 else: 243 246 if type(Tree) == list: … … 264 267 for attr in listclass.ncattrs(): 265 268 if debug: 266 print(" treating attribute {}".format(attr))269 print(" ==> treating attribute {}".format(attr)) 267 270 if attr != 'classtype': #classtype is for treatment, don't get it back 268 271 attribute = str(attr).swapcase() #there is a reason for swapcase, no sure what it isanymore … … 271 274 if type(Tree) == list and NewFormat: 272 275 if debug: 273 print(" printing with index 0")276 print(" printing with index 0") 274 277 if listtype == 'dict': 275 278 Tree[0][attribute] = str(listclass.getncattr(attr)) … … 279 282 t = int(indexlist[i]) 280 283 if debug: 281 print(" printing with index {]".format(t))284 print(" printing with index {}".format(t)) 282 285 if listtype == 'dict': 283 286 Tree[t][attribute] = str(listclass.getncattr(attr))
Note:
See TracChangeset
for help on using the changeset viewer.