Changeset 25819
- Timestamp:
- 12/03/20 05:51:00 (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
r25484 r25819 75 75 if verbose > 1: 76 76 print('===Now treating {}==='.format(group)) 77 if group == 'qmu': 78 print("qmu is skipped until it is more stable") 79 continue 77 80 NCgroup = NCData.createGroup(str(group)) 78 81 # In each group gather the fields of the class … … 88 91 #returns False for exotic types (typicaly results) 89 92 StdList = type(md.__dict__[group].__dict__[field][0]) in typelist 90 NCgroup.__setattr__('classtype', md.__dict__[group].__class__.__name__) 93 klass = type(md.__dict__[group]).__module__ + '.' + type(md.__dict__[group]).__name__ 94 NCgroup.__setattr__('classtype', klass) 91 95 if StdList: # this is a standard or empty list just proceed 92 96 if verbose > 4: 93 print("=££=creating var for {}.{} ".format(group, field))97 print("=££=creating var for {}.{} with classtype : {}".format(group, field, klass)) 94 98 Var = md.__dict__[group].__dict__[field] 95 99 Var = SqueezeVar(Var) … … 99 103 else: # this is a list of fields, specific treatment needed (usually results or outputdefinitions) 100 104 if verbose > 4: 101 print("list of fields happens for {}.{} ".format(group, field))105 print("list of fields happens for {}.{} with classtype : {}".format(group, field, klass)) 102 106 Listsize = len(md.__dict__[group].__dict__[field]) 103 107 if group == 'results': #for results we reshape the datas following time rather than subgrouping … … 105 109 try: 106 110 #take the class of the first element to define nc class and get the list of variables 107 Subgroup.__setattr__('classtype', md.__dict__[group].__dict__[field][0].__class__.__name__) 111 klass = type(md.__dict__[group].__dict__[field][0]).__module__ + '.' + type(md.__dict__[group].__dict__[field][0]).__name__ 112 Subgroup.__setattr__('classtype', klass) 108 113 subfields = dict.keys(md.__dict__[group].__dict__[field][0].__dict__) 109 114 except (IndexError, AttributeError): 110 Subgroup.__setattr__('classtype', md.__dict__[group].__dict__[field].__class__.__name__) 115 klass = type(md.__dict__[group].__dict__[field]).__module__ + '.' + type(md.__dict__[group].__dict__[field]).__name__ 116 Subgroup.__setattr__('classtype', klass) 111 117 subfields = dict.keys(md.__dict__[group].__dict__[field].__getitem__(0)) 112 118 for subfield in subfields: … … 137 143 Subgroupname = str(md.__dict__[group].__dict__[field][listindex].definitionstring) 138 144 Subgroup = NCgroup.createGroup(Subgroupname) 139 Subgroup.__setattr__('classtype', md.__dict__[group].__dict__[field][listindex].__class__.__name__) 145 klass = type(md.__dict__[group].__dict__[field][listindex]).__module__ + '.' + type(md.__dict__[group].__dict__[field][listindex]).__name__ 146 Subgroup.__setattr__('classtype', klass) 140 147 subfields = dict.keys(md.__dict__[group].__dict__[field][listindex].__dict__) 141 148 for subfield in subfields: … … 150 157 else: 151 158 print("WARNING: unknown treatment for md.{}".format(group)) 152 153 159 # No subgroup, we directly treat the variable 154 160 elif type(md.__dict__[group].__dict__[field]) in typelist or field == 'bamg': 155 NCgroup.__setattr__('classtype', md.__dict__[group].__class__.__name__) 161 klass = type(md.__dict__[group]).__module__ + '.' + type(md.__dict__[group]).__name__ 162 NCgroup.__setattr__('classtype', klass) 156 163 Var = md.__dict__[group].__dict__[field] 157 164 Var = SqueezeVar(Var) … … 161 168 if ncvar is not None: 162 169 FillVar(ncvar, Var) 163 # empty fiel , do nothing170 # empty field, do nothing 164 171 elif md.__dict__[group].__dict__[field] is None: 165 172 print('field md.{}.{} is None'.format(group, field)) 166 173 # if it is a masked array 167 174 elif type(md.__dict__[group].__dict__[field]) is np.ma.core.MaskedArray: 168 NCgroup.__setattr__('classtype', md.__dict__[group].__class__.__name__) 175 klass = type(md.__dict__[group]).__module__ + '.' + type(md.__dict__[group]).__name__ 176 NCgroup.__setattr__('classtype', klass) 169 177 Var = md.__dict__[group].__dict__[field].data 170 178 Var = SqueezeVar(Var) … … 176 184 # this is an issm class 177 185 elif isclass(type(md.__dict__[group].__dict__[field])): 178 classtype = getmodule(type(md.__dict__[group])).__name__ 179 NCgroup.__setattr__('classtype', classtype) 180 Subgroup = NCgroup.createGroup(str(field)) 181 classname = type(md.__dict__[group].__dict__[field]).__name__ 182 modulename = getmodule(type(md.__dict__[group].__dict__[field])).__name__ 183 if classname == modulename: 184 #when module and class name have the same name 185 classtype = classname 186 if type(md.__dict__[group].__dict__[field]).__name__ == 'solution': 187 #for results we reshape the datas following time rather than subgrouping 188 Listsize = len(md.__dict__[group].__dict__[field]) 189 Subgroup = NCgroup.createGroup(str(field)) 190 try: 191 #take the class of the first element to define nc class and get the list of variables 192 klass = type(md.__dict__[group].__dict__[field][0]).__module__ + '.' + type(md.__dict__[group].__dict__[field][0]).__name__ 193 Subgroup.__setattr__('classtype', klass) 194 subfields = dict.keys(md.__dict__[group].__dict__[field][0].__dict__) 195 except (IndexError, AttributeError): 196 klass = type(md.__dict__[group].__dict__[field]).__module__ + '.' + type(md.__dict__[group].__dict__[field]).__name__ 197 Subgroup.__setattr__('classtype', klass) 198 subfields = dict.keys(md.__dict__[group].__dict__[field].__getitem__(0)) 199 for subfield in subfields: 200 if subfield not in['errlog', 'outlog']: 201 StackedVar = ResTable() 202 for listindex in range(0, Listsize): 203 try: 204 Var = md.__dict__[group].__dict__[field].__getitem__(listindex).__dict__[subfield] 205 lastindex = listindex + 1 206 except AttributeError: 207 Var = md.__dict__[group].__dict__[field].__getitem__(listindex)[subfield] 208 except KeyError: 209 #Some fields only exist for the first step 210 lastindex = listindex 211 continue 212 Var = SqueezeVar(Var) 213 StackedVar.update(Var) 214 if verbose > 4: 215 print("=$$=creating var for {}.{}.{}".format(group, field, subfield)) 216 print("last index of the list is {}".format(lastindex)) 217 StackedVar = SqueezeVar(StackedVar.finalize(int(lastindex))) 218 DimDict, ncvar = CreateVar(NCData, StackedVar, subfield, Subgroup, DimDict) 219 #and fill it up 220 if ncvar is not None: 221 FillVar(ncvar, StackedVar) 186 222 else: 187 classtype = modulename + '.' + classname 188 Subgroup.__setattr__('classtype', classtype) 189 subfields = dict.keys(md.__dict__[group].__dict__[field].__dict__) 190 for subfield in subfields: 191 if str(subfield) not in ['errlog', 'outlog']: 192 Var = md.__dict__[group].__dict__[field].__dict__[subfield] 193 Var = SqueezeVar(Var) 194 if verbose > 4: 195 print("+==+creating var for {}.{}.{}".format(group, field, subfield)) 196 DimDict, ncvar = CreateVar(NCData, Var, subfield, Subgroup, DimDict) 197 if ncvar is not None: 198 FillVar(ncvar, Var) 223 klass = type(md.__dict__[group]).__module__ + '.' + type(md.__dict__[group]).__name__ 224 NCgroup.__setattr__('classtype', klass) 225 Subgroup = NCgroup.createGroup(str(field)) 226 klass = type(md.__dict__[group].__dict__[field]).__module__ + '.' + type(md.__dict__[group].__dict__[field]).__name__ 227 Subgroup.__setattr__('classtype', klass) 228 subfields = dict.keys(md.__dict__[group].__dict__[field].__dict__) 229 for subfield in subfields: 230 if str(subfield) not in ['errlog', 'outlog']: 231 Var = md.__dict__[group].__dict__[field].__dict__[subfield] 232 Var = SqueezeVar(Var) 233 if verbose > 4: 234 print("+==+creating var for {}.{}.{}".format(group, field, subfield)) 235 DimDict, ncvar = CreateVar(NCData, Var, subfield, Subgroup, DimDict) 236 if ncvar is not None: 237 FillVar(ncvar, Var) 199 238 else: 200 239 print("WARNING, md.{}.{} is not treated as it does not fall in one of the existing cases.".format(str(group), str(field))) -
issm/trunk-jpl/src/m/io/loadvars.py
r25591 r25819 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] [0]))96 if classtype[mod][0] == 'results ' and 'Time' in NCFile.dimensions: #Treating results {{{95 print("===> {} is of class {}".format(mod, classtype[mod])) 96 if classtype[mod][0] == 'results.solutionstep': #Treating results {{{ 97 97 keylist = [key for key in curclass.groups] 98 98 # this is related to the old structure of NC files where every steps of results had its own group 99 if len(keylist) > 0: 100 #this is kept for compatibility 101 #and treatment of list of dicts?? 102 try: 103 #group are named after their step 104 steplist = [int(key) for key in curclass.groups] 105 except ValueError: 106 #or a number is appended at the end of the name 107 steplist = [int(findall(r'\d + ', key)[0]) for key in keylist] 108 indexlist = [int(index * (len(curclass.groups) - 1) / max(1, max(steplist))) for index in steplist] 109 listtype = curclass.groups[keylist[0]].classtype 110 #discriminate between dict and results 111 if listtype == 'dict': 112 nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]] = [OrderedDict() for i in range(max(1, len(curclass.groups)))] 113 else: 114 if onlylast: #we load only the last result to save on time and memory 115 nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]] = [getattr(__import__(listtype), listtype)()] 116 Tree = nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]] 117 else: 118 nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]] = [getattr(__import__(listtype), listtype)() for i in range(max(1, len(curclass.groups)))] 119 Tree = nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]][:] 99 #that is the current treatment 100 #here we have a more NC approach with time being a dimension 101 listtype = split(r'\.', classtype[mod][0])[0] 102 print("listtype is {}".format(listtype)) 103 if len(NCFile.dimensions['Time']) == 1: 104 nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]] = getattr(classtype[mod][1], listtype)() 105 Tree = nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]] 120 106 else: 121 #that is the current treatment 122 #here we have a more NC approach with time being a dimension 123 listtype = curclass.classtype 124 if len(NCFile.dimensions['Time']) == 1: 125 nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]] = getattr(__import__(listtype), listtype)() 107 #Time dimension is in all the variables so we take that as stepnumber for the results 108 if onlylast: #we load only the last result to save on time and memory 109 nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]] = [getattr(classtype[mod][1], listtype)()] 126 110 Tree = nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]] 127 111 else: 128 #Time dimension is in all the variables so we take that as stepnumber for the results 129 if onlylast: #we load only the last result to save on time and memory 130 nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]] = [getattr(__import__(listtype), listtype)()] 131 Tree = nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]] 132 else: 133 nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]] = [getattr(__import__(listtype), listtype)() for i in range(max(1, len(NCFile.dimensions['Time'])))] 134 Tree = nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]][:] 135 #}}} 136 elif classtype[mod][0] == 'list': #Treating the old style of results {{{ 137 keylist = [key for key in curclass.groups] 138 # this is related to the old structure of NC files where every steps of results had its own group 139 if len(keylist) > 0: 140 #this is kept for compatibility 141 #and treatment of list of dicts?? 142 try: 143 #group are named after their step 144 steplist = [int(key) for key in curclass.groups] 145 except ValueError: 146 #or a number is appended at the end of the name 147 steplist = [int(findall(r'\d + ', key)[0]) for key in keylist] 148 indexlist = [int(index * (len(curclass.groups) - 1) / max(1, max(steplist))) for index in steplist] 149 listtype = curclass.groups[keylist[0]].classtype 150 #discriminate between dict and results 151 if listtype == 'dict': 152 nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]] = [OrderedDict() for i in range(max(1, len(curclass.groups)))] 153 else: 154 if onlylast: #we load only the last result to save on time and memory 155 nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]] = [getattr(__import__(listtype), listtype)()] 156 Tree = nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]] 157 else: 158 nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]] = [getattr(__import__(listtype), listtype)() for i in range(max(1, len(curclass.groups)))] 159 Tree = nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]][:] 160 else: 161 print("WARNING: we don't get the expected format, there might be an issue somewhere") 112 #nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]] = [getattr(classtype[mod][1], listtype)() for i in range(max(1, len(NCFile.dimensions['Time'])))] 113 #Tree = nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]][:] 114 setattr(nvdict['md'].__dict__[classtree[mod][0]], classtree[mod][1], getattr(classtype[mod][1], 'solution')([])) 115 for i in range(max(1, len(NCFile.dimensions['Time']))): 116 nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]].steps.append(getattr(classtype[mod][1], 'solutionstep')()) 117 Tree = nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]][:] 118 print(nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]].steps[0]) 162 119 # }}} 163 120 elif classtype[mod][0] == 'massfluxatgate': #this is for output definitions {{{ … … 170 127 if debug: 171 128 print("Using the default for md.{}.{}, is that right??".format(classtree[mod][0], classtree[mod][1])) 172 nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]] = getattr(classtype[mod][1], classtype[mod][0])() 129 try: 130 modulename = split(r'\.', classtype[mod][0])[0] 131 if debug: 132 print("trying to import {} from {}".format(classtype[mod][0], modulename)) 133 nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]] = getattr(classtype[mod][1], modulename)() 134 except AttributeError: 135 print("WARNING: md.{}.{} is not initialized, hopefully that was done in the main group:".format(classtree[mod][0], classtree[mod][1])) 173 136 Tree = nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]] 174 137 else: 175 138 curclass = NCFile.groups[classtree[mod][0]] 176 nvdict['md'].__dict__[mod] = getattr(classtype[mod][1], classtype[mod][0])() 139 modulename = split(r'\.', classtype[mod][0])[0] 140 nvdict['md'].__dict__[mod] = getattr(classtype[mod][1], modulename)() 177 141 Tree = nvdict['md'].__dict__[classtree[mod][0]] 178 142 if debug: … … 351 315 for subgroup in NCData.groups[group].groups: 352 316 classe = str(group) + '.' + str(subgroup) 353 submodule = str(getattr(NCData.groups[group].groups[subgroup], 'classtype')).split('.')[-1]354 class_dict[classe] = [ submodule, ]317 grpclass = str(getattr(NCData.groups[group].groups[subgroup], 'classtype')) 318 class_dict[classe] = [grpclass, ] 355 319 if class_dict[classe][0] not in ['dict', 'list', 'cell']: 356 320 try: 357 class_dict[classe].append(__import__(class_dict[classe][0])) 321 modulename = split(r'\.', class_dict[classe][0])[0] 322 #class_dict[classe].append(__import__(class_dict[classe][0])) 323 class_dict[classe].append(__import__(modulename)) 358 324 except ModuleNotFoundError: 359 325 #submodule probably has a different name 360 modulename = str(getattr(NCData.groups[group].groups[subgroup], 'classtype')).split('.')[0] 326 modulename = str(getattr(NCData.groups[group].groups[subgroup], 'classtype')) 327 print("WARNING importing {} rather than {}".format(modulename, class_dict[classe][0])) 361 328 class_dict[classe].append(__import__(modulename)) 362 329 class_tree[classe] = [group, subgroup] … … 366 333 class_dict[classe] = [str(getattr(NCData.groups[group], 'classtype')), ] 367 334 if class_dict[classe][0] not in ['dict', 'list', 'cell']: 368 class_dict[classe].append(__import__(class_dict[classe][0])) 335 modulename = split(r'\.', class_dict[classe][0])[0] 336 class_dict[classe].append(__import__(modulename)) 369 337 class_tree[classe] = [group, ] 370 338 except AttributeError:
Note:
See TracChangeset
for help on using the changeset viewer.