Changeset 26127
- Timestamp:
- 03/23/21 04:56:33 (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
issm/trunk-jpl/src/m/io/loadvars.py
r26096 r26127 98 98 if classtype[mod][0] == 'results.solutionstep': #Treating results {{{ 99 99 keylist = [key for key in curclass.groups] 100 # this is related to the old structure of NC files where every steps of results had its own group101 100 #that is the current treatment 102 101 #here we have a more NC approach with time being a dimension … … 111 110 Tree = nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]] 112 111 else: 113 #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'])))]114 #Tree = nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]][:]115 112 setattr(nvdict['md'].__dict__[classtree[mod][0]], classtree[mod][1], getattr(classtype[mod][1], 'solution')([])) 116 113 for i in range(max(1, len(NCFile.dimensions['Time']))): … … 118 115 Tree = nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]][:] 119 116 # }}} 117 elif "results" in mod and classtype[mod][0] == 'list': #this is the old style of results where every step has a group{{{ 118 keylist = [key for key in curclass.groups] 119 #one group per step so use that in place of time 120 stepnum = len(NCFile.groups[classtree[mod][0]].groups[classtree[mod][1]].groups) 121 #we need to redefine classtype from list to result 122 listtype = 'results' 123 classtype[mod].append(__import__(listtype)) 124 if stepnum == 1: 125 nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]] = getattr(classtype[mod][1], listtype)() 126 Tree = nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]] 127 else: 128 if onlylast: #we load only the last result to save on time and memory 129 nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]] = [getattr(classtype[mod][1], listtype)()] 130 Tree = nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]] 131 else: 132 #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'])))] 133 #Tree = nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]][:] 134 setattr(nvdict['md'].__dict__[classtree[mod][0]], classtree[mod][1], getattr(classtype[mod][1], 'solution')([])) 135 for i in range(max(1, stepnum)): 136 nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]].steps.append(getattr(classtype[mod][1], 'solutionstep')()) 137 Tree = nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]][:] 138 #}}} 120 139 elif classtype[mod][0] == 'massfluxatgate': #this is for output definitions {{{ 121 140 defname = split('Output|[0-9]+', classtree[mod][1])[1] + 's' … … 141 160 Tree = nvdict['md'].__dict__[classtree[mod][0]] 142 161 if debug: 143 print(" for {} Tree is a {} ".format(mod, Tree.__class__.__name__))162 print(" for {} Tree is a {} with len {}".format(mod, Tree.__class__.__name__, len(curclass.groups))) 144 163 # }}} 145 164 #==== Then we populate it {{{ 146 for i in range(0, max(1, len(curclass.groups))): 147 if len(curclass.groups) > 0: 148 listclass = curclass.groups[keylist[i]] 165 #for i in range(0, max(1, len(curclass.groups))): 166 if len(curclass.groups) > 0: #that is presumably only for old style NC where each result step had its own group 167 if onlylast: 168 listclass = curclass.groups[keylist[len(curclass.groups) - 1]] 149 169 else: 150 listclass = curclass 151 #==== We deal with Variables {{{ 152 for var in listclass.variables: 153 if debug: 154 print(" ==> treating var {}".format(var)) 155 if var not in ['errlog', 'outlog']: 156 varval = listclass.variables[str(var)] 157 vardim = varval.ndim 158 #There is a special treatment for results to account for its specific structure 159 #that is the new export version where time is a named dimension 160 NewFormat = 'Time' in NCFile.dimensions 161 if type(Tree) == list and NewFormat: 162 if onlylast: 170 listclass = curclass.groups[keylist[0]] 171 else: 172 listclass = curclass 173 #==== We deal with Variables {{{ 174 for var in listclass.variables: 175 if debug: 176 print(" ==> treating var {}".format(var)) 177 if var not in ['errlog', 'outlog']: 178 varval = listclass.variables[str(var)] 179 vardim = varval.ndim 180 #There is a special treatment for results to account for its specific structure 181 #that is the new export version where time is a named dimension 182 NewFormat = 'Time' in NCFile.dimensions 183 if type(Tree) == list: # and NewFormat: 184 if onlylast: 185 if NewFormat: 163 186 if vardim == 1: 164 187 Tree[0].__dict__[str(var)] = varval[-1].data … … 169 192 else: 170 193 print('table dimension greater than 3 not implemented yet') 194 else: #old format had step sorted in difeerent group so last group is last time 195 Tree[0].__dict__[str(var)] = varval[:].data 196 else: 197 incomplete = 'Time' not in varval.dimensions and NewFormat 198 if incomplete: 199 chosendim = varval.dimensions[0] 200 timelist = np.arange(0, len(NCFile.dimensions[chosendim])) 201 print('WARNING, {} is not present on every times, we chose {}({}) as the dimension to write it with'.format(var, chosendim, len(NCFile.dimensions[chosendim]))) 202 elif not NewFormat: 203 timelist = len(curclass.groups) 171 204 else: 172 incomplete = 'Time' not in varval.dimensions 173 if incomplete: 174 chosendim = varval.dimensions[0] 175 timelist = np.arange(0, len(NCFile.dimensions[chosendim])) 176 print('WARNING, {} is not present on every times, we chose {}({}) as the dimension to write it with'.format(var, chosendim, len(NCFile.dimensions[chosendim]))) 177 else: 178 timelist = np.arange(0, len(NCFile.dimensions['Time'])) 179 for t in timelist: 180 print("filing step {} for {}".format(t, var)) 181 if vardim == 0: 182 Tree[t].__dict__[str(var)] = varval[:].data 183 if vardim == 1: 184 Tree[t].__dict__[str(var)] = varval[t].data 185 elif vardim == 2: 186 Tree[t].__dict__[str(var)] = varval[t, :].data 187 elif vardim == 3: 188 Tree[t].__dict__[str(var)] = varval[t, :, :].data 189 else: 190 print('table dimension greater than 3 not implemented yet') 191 else: 192 if vardim == 0: #that is a scalar 193 if type(Tree) == list: 194 t = indexlist[i] 195 if listtype == 'dict': 196 Tree[t][str(var)] = varval[0].data 197 else: 198 Tree[t].__dict__[str(var)] = varval[0].data 199 else: 200 if str(varval[0]) == '': #no value 201 Tree.__dict__[str(var)] = [] 202 elif varval[0] == 'True': #treatin bool 203 Tree.__dict__[str(var)] = True 204 elif varval[0] == 'False': #treatin bool 205 Tree.__dict__[str(var)] = False 206 else: 207 Tree.__dict__[str(var)] = varval[0].item() 208 209 elif vardim == 1: #that is a vector 210 if varval.dtype == str: 211 if varval.shape[0] == 1: 212 Tree.__dict__[str(var)] = [str(varval[0]), ] 213 elif 'True' in varval[:] or 'False' in varval[:]: 214 Tree.__dict__[str(var)] = np.asarray([V == 'True' for V in varval[:]], dtype=bool) 215 else: 216 Tree.__dict__[str(var)] = [str(vallue) for vallue in varval[:]] 217 else: 218 if type(Tree) == list: 219 t = indexlist[i] 220 if listtype == 'dict': 221 Tree[t][str(var)] = varval[:].data 222 else: 223 Tree[t].__dict__[str(var)] = varval[:].data 224 else: 225 try: 226 #some thing specifically require a list 227 mdtype = type(Tree.__dict__[str(var)]) 228 except KeyError: 229 mdtype = float 230 if mdtype == list: 231 Tree.__dict__[str(var)] = [mdval for mdval in varval[:]] 232 else: 233 Tree.__dict__[str(var)] = varval[:].data 234 235 elif vardim == 2: 236 #dealling with dict 237 if varval.dtype == str: #that is for dictionaries 238 if any(varval[:, 0] == 'toolkit'): #toolkit definition have to be first 239 Tree.__dict__[str(var)] = OrderedDict([('toolkit', str(varval[np.where(varval[:, 0] == 'toolkit')[0][0], 1]))]) 240 strings1 = [str(arg[0]) for arg in varval if arg[0] != 'toolkits'] 241 strings2 = [str(arg[1]) for arg in varval if arg[0] != 'toolkits'] 242 Tree.__dict__[str(var)].update(list(zip(strings1, strings2))) 243 else: 244 strings1 = [str(arg[0]) for arg in varval] 245 strings2 = [str(arg[1]) for arg in varval] 246 Tree.__dict__[str(var)] = OrderedDict(list(zip(strings1, strings2))) 247 else: 248 if type(Tree) == list: 249 t = indexlist[i] 250 if listtype == 'dict': 251 Tree[t][str(var)] = varval[:, :].data 252 else: 253 Tree[t].__dict__[str(var)] = varval[:, :].data 254 else: 255 Tree.__dict__[str(var)] = varval[:, :].data 256 elif vardim == 3: 257 if type(Tree) == list: 258 t = int(indexlist[i]) 259 if listtype == 'dict': 260 Tree[t][str(var)] = varval[:, :, :].data 261 else: 262 Tree[t].__dict__[str(var)] = varval[:, :, :] 263 else: 264 Tree.__dict__[str(var)] = varval[:, :, :].data 205 timelist = np.arange(0, len(NCFile.dimensions['Time'])) 206 for t in timelist: 207 print("filing step {} for {}".format(t, var)) 208 if vardim == 0: 209 Tree[t].__dict__[str(var)] = varval[:].data 210 if vardim == 1: 211 Tree[t].__dict__[str(var)] = varval[t].data 212 elif vardim == 2: 213 Tree[t].__dict__[str(var)] = varval[t, :].data 214 elif vardim == 3: 215 Tree[t].__dict__[str(var)] = varval[t, :, :].data 216 else: 217 print('table dimension greater than 3 not implemented yet') 218 else: 219 if vardim == 0: #that is a scalar 220 if str(varval[0]) == '': #no value 221 Tree.__dict__[str(var)] = [] 222 elif varval[0] == 'True': #treatin bool 223 Tree.__dict__[str(var)] = True 224 elif varval[0] == 'False': #treatin bool 225 Tree.__dict__[str(var)] = False 265 226 else: 266 print('table dimension greater than 3 not implemented yet') 267 # }}} 268 #==== And with atribute {{{ 269 for attr in listclass.ncattrs(): 270 if debug: 271 print(" ==> treating attribute {}".format(attr)) 272 if attr != 'classtype': #classtype is for treatment, don't get it back 273 attribute = str(attr).swapcase() #there is a reason for swapcase, no sure what it isanymore 274 if attr == 'VARNAME': 275 attribute = 'name' 276 if type(Tree) == list and NewFormat: 277 if debug: 278 print(" printing with index 0") 279 if listtype == 'dict': 280 Tree[0][attribute] = str(listclass.getncattr(attr)) 227 Tree.__dict__[str(var)] = varval[0].item() 228 229 elif vardim == 1: #that is a vector 230 if varval.dtype == str: 231 if varval.shape[0] == 1: 232 Tree.__dict__[str(var)] = [str(varval[0]), ] 233 elif 'True' in varval[:] or 'False' in varval[:]: 234 Tree.__dict__[str(var)] = np.asarray([V == 'True' for V in varval[:]], dtype=bool) 235 else: 236 Tree.__dict__[str(var)] = [str(vallue) for vallue in varval[:]] 281 237 else: 282 Tree[0].__dict__[attribute] = str(listclass.getncattr(attr)) 283 elif type(Tree) == list: 284 t = int(indexlist[i]) 285 if debug: 286 print(" printing with index {}".format(t)) 287 if listtype == 'dict': 288 Tree[t][attribute] = str(listclass.getncattr(attr)) 289 else: 290 Tree[t].__dict__[attribute] = str(listclass.getncattr(attr)) 291 else: 292 Tree.__dict__[attribute] = str(listclass.getncattr(attr)) 293 if listclass.getncattr(attr) == 'True': 294 Tree.__dict__[attribute] = True 295 elif listclass.getncattr(attr) == 'False': 296 Tree.__dict__[attribute] = False 238 try: 239 #some thing specifically require a list 240 mdtype = type(Tree.__dict__[str(var)]) 241 except KeyError: 242 mdtype = float 243 if mdtype == list: 244 Tree.__dict__[str(var)] = [mdval for mdval in varval[:]] 245 else: 246 Tree.__dict__[str(var)] = varval[:].data 247 248 elif vardim == 2: 249 #dealling with dict 250 if varval.dtype == str: #that is for dictionaries 251 if any(varval[:, 0] == 'toolkit'): #toolkit definition have to be first 252 Tree.__dict__[str(var)] = OrderedDict([('toolkit', str(varval[np.where(varval[:, 0] == 'toolkit')[0][0], 1]))]) 253 strings1 = [str(arg[0]) for arg in varval if arg[0] != 'toolkits'] 254 strings2 = [str(arg[1]) for arg in varval if arg[0] != 'toolkits'] 255 Tree.__dict__[str(var)].update(list(zip(strings1, strings2))) 256 else: 257 strings1 = [str(arg[0]) for arg in varval] 258 strings2 = [str(arg[1]) for arg in varval] 259 Tree.__dict__[str(var)] = OrderedDict(list(zip(strings1, strings2))) 260 elif vardim == 3: 261 Tree.__dict__[str(var)] = varval[:, :, :].data 262 else: 263 print('table dimension greater than 3 not implemented yet') 264 # }}} 265 #==== And with atribute {{{ 266 for attr in listclass.ncattrs(): 267 if debug: 268 print(" ==> treating attribute {}".format(attr)) 269 if attr != 'classtype': #classtype is for treatment, don't get it back 270 attribute = str(attr).swapcase() #there is a reason for swapcase, no sure what it isanymore 271 if attr == 'VARNAME': 272 attribute = 'name' 273 if type(Tree) == list: 274 if debug: 275 print(" printing with index 0") 276 if listtype == 'dict': 277 Tree[0][attribute] = str(listclass.getncattr(attr)) 278 else: 279 Tree[0].__dict__[attribute] = str(listclass.getncattr(attr)) 280 else: 281 Tree.__dict__[attribute] = str(listclass.getncattr(attr)) 282 if listclass.getncattr(attr) == 'True': 283 Tree.__dict__[attribute] = True 284 elif listclass.getncattr(attr) == 'False': 285 Tree.__dict__[attribute] = False 297 286 # }}} 298 287 # }}} … … 322 311 grpclass = str(getattr(NCData.groups[group].groups[subgroup], 'classtype')) 323 312 class_dict[classe] = [grpclass, ] 313 print(class_dict[classe][0]) 324 314 if class_dict[classe][0] not in ['dict', 'list', 'cell']: 325 315 try: 326 316 modulename = split(r'\.', class_dict[classe][0])[0] 327 #class_dict[classe].append(__import__(class_dict[classe][0]))328 317 class_dict[classe].append(__import__(modulename)) 329 318 except ModuleNotFoundError:
Note:
See TracChangeset
for help on using the changeset viewer.