Changeset 25190
- Timestamp:
- 07/01/20 00:39:40 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
TabularUnified issm/trunk-jpl/src/m/contrib/defleurian/netCDF/export_netCDF.py ¶
r25063 r25190 6 6 7 7 8 class ResTable: 9 10 def __init__(self): 11 self.data = [] 12 13 def update(self, row): 14 if len(np.shape(row)) == 0: 15 self.data.append(row) 16 else: 17 for r in row: 18 self.data.append(r) 19 20 def finalize(self, rows): 21 print("data of len {} with {} rows".format(len(self.data) ,rows)) 22 if len(self.data) > rows: 23 return np.reshape(self.data, newshape=(rows, int(len(self.data) / rows))) 24 else: 25 return np.asarray(self.data) 26 27 8 28 def export_netCDF(md, filename): # {{{ 9 debug = False 29 #verbosity of the code, 0 is no messages, 5 is chatty 30 verbose = 0 10 31 if path.exists(filename): 11 32 print('File {} allready exist'.format(filename)) … … 35 56 dimlist = [2, md.mesh.numberofelements, md.mesh.numberofvertices, np.shape(md.mesh.elements)[1]] 36 57 dimnames = ['DictDummy', 'EltNum', 'VertNum', 'VertPerElt'] 37 print('===Creating dimensions ===') 58 if verbose > 0: 59 print('===Creating dimensions ===') 38 60 for i, newdim in enumerate(dimlist): 39 61 if newdim not in list(DimDict.keys()): … … 48 70 # get all model classes and create respective groups 49 71 groups = dict.keys(md.__dict__) 50 print('===Creating and populating groups===') 72 if verbose > 0: 73 print('===Creating and populating groups===') 51 74 for group in groups: 75 if verbose > 1: 76 print('===Now treating {}==='.format(group)) 52 77 NCgroup = NCData.createGroup(str(group)) 53 78 # In each group gather the fields of the class … … 65 90 NCgroup.__setattr__('classtype', md.__dict__[group].__class__.__name__) 66 91 if StdList: # this is a standard or empty list just proceed 92 if verbose > 4: 93 print("=££=creating var for {}.{}".format(group, field)) 67 94 Var = md.__dict__[group].__dict__[field] 68 95 Var = SqueezeVar(Var) 69 if debug:70 print("=££=creating var for {}.{}".format(group, field))71 96 DimDict, ncvar = CreateVar(NCData, Var, field, NCgroup, DimDict) 72 97 if ncvar is not None: 73 98 FillVar(ncvar, Var) 74 99 else: # this is a list of fields, specific treatment needed 75 if debug:100 if verbose > 4: 76 101 print("list of fields happens for {}.{}".format(group, field)) 77 102 Listsize = len(md.__dict__[group].__dict__[field]) … … 81 106 Subgroup.__setattr__('classtype', md.__dict__[group].__dict__[field][0].__class__.__name__) 82 107 subfields = dict.keys(md.__dict__[group].__dict__[field][0].__dict__) 83 except IndexError:108 except (IndexError, AttributeError): 84 109 Subgroup.__setattr__('classtype', md.__dict__[group].__dict__[field].__class__.__name__) 85 except AttributeError:86 110 subfields = dict.keys(md.__dict__[group].__dict__[field].__getitem__(0)) 87 111 88 112 for subfield in subfields: 89 113 if subfield not in['errlog', 'outlog']: 90 try: 91 Var = md.__dict__[group].__dict__[field].__getitem__(0).__dict__[subfield] 92 except AttributeError: 93 Var = md.__dict__[group].__dict__[field].__getitem__(0)[subfield] 94 StackedVar = SqueezeVar(Var) 95 96 for listindex in range(1, Listsize): 114 StackedVar = ResTable() 115 for listindex in range(0, Listsize): 97 116 try: 98 117 Var = md.__dict__[group].__dict__[field].__getitem__(listindex).__dict__[subfield] 118 lastindex = listindex + 1 99 119 except AttributeError: 100 120 Var = md.__dict__[group].__dict__[field].__getitem__(listindex)[subfield] 101 121 except KeyError: 102 122 #Some fields only exist for the first step 123 lastindex = listindex 103 124 continue 104 125 Var = SqueezeVar(Var) 105 StackedVar = np.vstack((StackedVar, Var))106 if debug:126 StackedVar.update(Var) 127 if verbose > 4: 107 128 print("=$$=creating var for {}.{}.{}".format(group, field, subfield)) 108 StackedVar = SqueezeVar(StackedVar) 129 print(lastindex) 130 StackedVar = SqueezeVar(StackedVar.finalize(int(lastindex))) 109 131 DimDict, ncvar = CreateVar(NCData, StackedVar, subfield, Subgroup, DimDict) 110 132 #and fill it up … … 117 139 Var = md.__dict__[group].__dict__[field] 118 140 Var = SqueezeVar(Var) 119 if debug:141 if verbose > 4: 120 142 print("====creating var for {}.{}".format(group, field)) 121 143 DimDict, ncvar = CreateVar(NCData, Var, field, NCgroup, DimDict) … … 130 152 Var = md.__dict__[group].__dict__[field].data 131 153 Var = SqueezeVar(Var) 132 if debug:154 if verbose > 4: 133 155 print("=++=creating var for {}.{}".format(group, field)) 134 156 DimDict, ncvar = CreateVar(NCData, Var, field, NCgroup, DimDict) … … 144 166 Var = md.__dict__[group].__dict__[field].__dict__[subfield] 145 167 Var = SqueezeVar(Var) 146 if debug:168 if verbose > 4: 147 169 print("+==+creating var for {}.{}.{}".format(group, field, subfield)) 148 170 DimDict, ncvar = CreateVar(NCData, Var, subfield, Subgroup, DimDict) … … 273 295 except TypeError: # type does not accept nan, get vallue of the variable 274 296 naned = invar 297 275 298 if UnlimIndex: 276 299 if len(val_shape) == 0: … … 328 351 return Var 329 352 # }}} 353 354 355 def grow(self, row): # {{{ 356 np.append(self.data, row) 357 # }}}
Note:
See TracChangeset
for help on using the changeset viewer.