Changeset 26872
- Timestamp:
- 02/10/22 04:53:02 (3 years ago)
- Location:
- issm/trunk-jpl/src/m/io
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
issm/trunk-jpl/src/m/io/loadmodel.py
r26857 r26872 9 9 10 10 11 def loadmodel(path, singletime=None ):11 def loadmodel(path, singletime=None, singleres=None): 12 12 """LOADMODEL - load a model 13 13 … … 31 31 # try: 32 32 #recover model on file and name it md 33 struc = loadvars(path, singletime=singletime )33 struc = loadvars(path, singletime=singletime, singleres=singleres) 34 34 name = [key for key in list(struc.keys())] 35 35 if len(name) > 1: -
issm/trunk-jpl/src/m/io/loadvars.py
r26871 r26872 31 31 filename = '' 32 32 nvdict = {} 33 debug = False # print messages if true33 debug = True # print messages if true 34 34 35 35 if len(args) >= 1 and isinstance(args[0], str): … … 58 58 if key == 'singletime': 59 59 timeindex = value 60 if key == 'singleres': 61 resname = value 60 62 61 63 if whichdb(filename): #We used python pickle for the save … … 184 186 for groupindex, listclass in enumerate(groupclass): 185 187 for var in listclass.variables: 186 if var not in ['errlog', 'outlog']: 187 varval = listclass.variables[str(var)] 188 vardim = varval.ndim 189 if debug: 190 print(" ==> treating var {} of dimension {}".format(var, vardim)) 191 #There is a special treatment for results to account for its specific structure 192 #that is the new export version where time is a named dimension 193 NewFormat = 'Time' in NCFile.dimensions 194 if type(Tree) == list: # and NewFormat: 195 if timeindex: 196 if NewFormat: 197 if vardim == 1: 188 if not resname or var == resname: 189 if var not in ['errlog', 'outlog']: 190 varval = listclass.variables[str(var)] 191 vardim = varval.ndim 192 if debug: 193 print(" ==> treating var {} of dimension {}".format(var, vardim)) 194 #There is a special treatment for results to account for its specific structure 195 #that is the new export version where time is a named dimension 196 NewFormat = 'Time' in NCFile.dimensions 197 if type(Tree) == list: # and NewFormat: 198 if timeindex: 199 if NewFormat: 200 if vardim == 1: 201 try: 202 Tree[0].__dict__[str(var)] = varval[timeindex].data 203 except IndexError: 204 print('WARNING: No data on index {} for {} reverting to last time.'.format(timeindex, str(var))) 205 Tree[0].__dict__[str(var)] = varval[-1].data 206 elif vardim == 2: 207 Tree[0].__dict__[str(var)] = varval[timeindex, :].data 208 elif vardim == 3: 209 Tree[0].__dict__[str(var)] = varval[timeindex, :, :].data 210 else: 211 print('table dimension greater than 3 not implemented yet') 212 else: #old format had step sorted in difeerent group so last group is last time 213 Tree[0].__dict__[str(var)] = varval[:].data 214 else: 215 if NewFormat: 216 incomplete = 'Time' not in varval.dimensions 217 if incomplete: 218 try: 219 chosendim = varval.dimensions[0] 220 timelist = np.arange(0, len(NCFile.dimensions[chosendim])) 221 print('WARNING, {} is not present on every times, we chose {}({}) as the dimension to write it with'.format(var, chosendim, len(NCFile.dimensions[chosendim]))) 222 except IndexError: 223 #just one step, so no dimension, we just put it on the first solutionstep 224 timelist = [0] 225 else: 226 timelist = np.arange(0, len(NCFile.dimensions['Time'])) 227 for t in timelist: 228 if debug: 229 print("filing step {} for {}".format(t, var)) 230 if vardim == 0: 231 Tree[t].__dict__[str(var)] = varval[:].data 232 elif vardim == 1: 233 Tree[t].__dict__[str(var)] = varval[t].data 234 elif vardim == 2: 235 Tree[t].__dict__[str(var)] = varval[t, :].data 236 elif vardim == 3: 237 Tree[t].__dict__[str(var)] = varval[t, :, :].data 238 else: 239 print('table dimension greater than 3 not implemented yet') 240 else: 241 if debug: 242 print("filing step {} for {}".format(groupindex, var)) 243 Tree[groupindex].__dict__[str(var)] = varval[:].data 244 else: 245 if vardim == 0: #that is a scalar 246 if str(varval[0]) in ['', '--', 'emptycell']: #no value 247 Tree.__dict__[str(var)] = [] 248 elif varval[0] == 'True': #treatin bool 249 Tree.__dict__[str(var)] = True 250 elif varval[0] == 'False': #treatin bool 251 Tree.__dict__[str(var)] = False 252 else: 253 Tree.__dict__[str(var)] = varval[0].item() 254 255 elif vardim == 1: #that is a vector 256 if debug: 257 print(" for variable {} type is {}".format(str(var), varval.dtype)) 258 if varval.dtype == str: 259 if varval.shape[0] == 1: 260 Tree.__dict__[str(var)] = [str(varval[0]), ] 261 elif 'True' in varval[:] or 'False' in varval[:]: 262 Tree.__dict__[str(var)] = np.asarray([V == 'True' for V in varval[:]], dtype=bool) 263 else: 264 Tree.__dict__[str(var)] = [str(vallue) for vallue in varval[:]] 265 elif varval.dtype == "|S1": #that is for matlab chararcter arrays 266 stringlist = chartostring(varval[:]) 267 Tree.__dict__[str(var)] = [stringlist.tolist(), ] 268 else: 198 269 try: 199 Tree[0].__dict__[str(var)] = varval[timeindex].data 200 except IndexError: 201 print('WARNING: No data on index {} for {} reverting to last time.'.format(timeindex, str(var))) 202 Tree[0].__dict__[str(var)] = varval[-1].data 203 204 elif vardim == 2: 205 Tree[0].__dict__[str(var)] = varval[timeindex, :].data 206 elif vardim == 3: 207 Tree[0].__dict__[str(var)] = varval[timeindex, :, :].data 270 #some thing specifically require a list 271 mdtype = type(Tree.__dict__[str(var)]) 272 except KeyError: 273 mdtype = float 274 if mdtype == list: 275 Tree.__dict__[str(var)] = [mdval for mdval in varval[:]] 276 else: 277 Tree.__dict__[str(var)] = varval[:].data 278 279 elif vardim == 2: 280 #dealling with dict 281 if debug: 282 print(" for variable {} type is {}".format(str(var), varval.dtype)) 283 if varval.dtype == str: #that is for dictionaries 284 if any(varval[:, 0] == 'toolkit'): #toolkit definition have to be first 285 Tree.__dict__[str(var)] = OrderedDict([('toolkit', str(varval[np.where(varval[:, 0] == 'toolkit')[0][0], 1]))]) 286 strings1 = [str(arg[0]) for arg in varval if arg[0] != 'toolkits'] 287 strings2 = [str(arg[1]) for arg in varval if arg[0] != 'toolkits'] 288 Tree.__dict__[str(var)].update(list(zip(strings1, strings2))) 289 else: 290 strings1 = [str(arg[0]) for arg in varval] 291 strings2 = [str(arg[1]) for arg in varval] 292 Tree.__dict__[str(var)] = OrderedDict(list(zip(strings1, strings2))) 293 elif varval.dtype == "|S1": #that is for matlab chararcter arrays 294 stringlist = chartostring(varval[:, :]) 295 stringlist = [string.strip() for string in stringlist] 296 Tree.__dict__[str(var)] = stringlist 208 297 else: 209 print('table dimension greater than 3 not implemented yet') 210 else: #old format had step sorted in difeerent group so last group is last time 211 Tree[0].__dict__[str(var)] = varval[:].data 212 else: 213 if NewFormat: 214 incomplete = 'Time' not in varval.dimensions 215 if incomplete: 216 try: 217 chosendim = varval.dimensions[0] 218 timelist = np.arange(0, len(NCFile.dimensions[chosendim])) 219 print('WARNING, {} is not present on every times, we chose {}({}) as the dimension to write it with'.format(var, chosendim, len(NCFile.dimensions[chosendim]))) 220 except IndexError: 221 #just one step, so no dimension, we just put it on the first solutionstep 222 timelist = [0] 223 else: 224 timelist = np.arange(0, len(NCFile.dimensions['Time'])) 225 for t in timelist: 226 if debug: 227 print("filing step {} for {}".format(t, var)) 228 if vardim == 0: 229 Tree[t].__dict__[str(var)] = varval[:].data 230 elif vardim == 1: 231 Tree[t].__dict__[str(var)] = varval[t].data 232 elif vardim == 2: 233 Tree[t].__dict__[str(var)] = varval[t, :].data 234 elif vardim == 3: 235 Tree[t].__dict__[str(var)] = varval[t, :, :].data 236 else: 237 print('table dimension greater than 3 not implemented yet') 298 if type(Tree) == list: 299 t = indexlist[i] 300 if listtype == 'dict': 301 Tree[t][str(var)] = varval[:, :].data 302 else: 303 Tree[t].__dict__[str(var)] = varval[:, :].data 304 else: 305 Tree.__dict__[str(var)] = varval[:, :].data 306 elif vardim == 3: 307 Tree.__dict__[str(var)] = varval[:, :, :].data 238 308 else: 239 if debug: 240 print("filing step {} for {}".format(groupindex, var)) 241 Tree[groupindex].__dict__[str(var)] = varval[:].data 242 else: 243 if vardim == 0: #that is a scalar 244 if str(varval[0]) in ['', '--', 'emptycell']: #no value 245 Tree.__dict__[str(var)] = [] 246 elif varval[0] == 'True': #treatin bool 247 Tree.__dict__[str(var)] = True 248 elif varval[0] == 'False': #treatin bool 249 Tree.__dict__[str(var)] = False 250 else: 251 Tree.__dict__[str(var)] = varval[0].item() 252 253 elif vardim == 1: #that is a vector 254 if debug: 255 print(" for variable {} type is {}".format(str(var), varval.dtype)) 256 if varval.dtype == str: 257 if varval.shape[0] == 1: 258 Tree.__dict__[str(var)] = [str(varval[0]), ] 259 elif 'True' in varval[:] or 'False' in varval[:]: 260 Tree.__dict__[str(var)] = np.asarray([V == 'True' for V in varval[:]], dtype=bool) 261 else: 262 Tree.__dict__[str(var)] = [str(vallue) for vallue in varval[:]] 263 elif varval.dtype == "|S1": #that is for matlab chararcter arrays 264 stringlist = chartostring(varval[:]) 265 Tree.__dict__[str(var)] = [stringlist.tolist(), ] 266 else: 267 try: 268 #some thing specifically require a list 269 mdtype = type(Tree.__dict__[str(var)]) 270 except KeyError: 271 mdtype = float 272 if mdtype == list: 273 Tree.__dict__[str(var)] = [mdval for mdval in varval[:]] 274 else: 275 Tree.__dict__[str(var)] = varval[:].data 276 277 elif vardim == 2: 278 #dealling with dict 279 if debug: 280 print(" for variable {} type is {}".format(str(var), varval.dtype)) 281 if varval.dtype == str: #that is for dictionaries 282 if any(varval[:, 0] == 'toolkit'): #toolkit definition have to be first 283 Tree.__dict__[str(var)] = OrderedDict([('toolkit', str(varval[np.where(varval[:, 0] == 'toolkit')[0][0], 1]))]) 284 strings1 = [str(arg[0]) for arg in varval if arg[0] != 'toolkits'] 285 strings2 = [str(arg[1]) for arg in varval if arg[0] != 'toolkits'] 286 Tree.__dict__[str(var)].update(list(zip(strings1, strings2))) 287 else: 288 strings1 = [str(arg[0]) for arg in varval] 289 strings2 = [str(arg[1]) for arg in varval] 290 Tree.__dict__[str(var)] = OrderedDict(list(zip(strings1, strings2))) 291 elif varval.dtype == "|S1": #that is for matlab chararcter arrays 292 stringlist = chartostring(varval[:, :]) 293 stringlist = [string.strip() for string in stringlist] 294 Tree.__dict__[str(var)] = stringlist 295 else: 296 if type(Tree) == list: 297 t = indexlist[i] 298 if listtype == 'dict': 299 Tree[t][str(var)] = varval[:, :].data 300 else: 301 Tree[t].__dict__[str(var)] = varval[:, :].data 302 else: 303 Tree.__dict__[str(var)] = varval[:, :].data 304 elif vardim == 3: 305 Tree.__dict__[str(var)] = varval[:, :, :].data 306 else: 307 print('table dimension greater than 3 not implemented yet') 309 print('table dimension greater than 3 not implemented yet') 308 310 # }}} 309 311 #==== And with atribute {{{
Note:
See TracChangeset
for help on using the changeset viewer.