Changeset 25478
- Timestamp:
- 08/26/20 07:07:33 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
issm/trunk-jpl/src/m/io/loadvars.py
r25460 r25478 5 5 except ImportError: 6 6 from whichdb import whichdb # Python 2 7 from re import findall 7 from re import findall, split 8 8 import shelve 9 10 9 from netCDF4 import Dataset 11 10 import numpy as np 12 13 11 from model import * 14 12 … … 95 93 curclass = NCFile.groups[classtree[mod][0]].groups[classtree[mod][1]] 96 94 if debug: 97 print(" now checking {} for list : {}".format(mod, classtype[mod][0] == 'list' or (classtype[mod][0] == 'results' and 'Time' in NCFile.dimensions)))98 if classtype[mod][0] == ' list' or (classtype[mod][0] == 'results' and 'Time' in NCFile.dimensions): #We have a list of variables95 print("===> {} is of class {}".format(mod, classtype[mod][0])) 96 if classtype[mod][0] == 'results' and 'Time' in NCFile.dimensions: #Treating results 99 97 keylist = [key for key in curclass.groups] 100 98 # this is related to the old structure of NC files where every steps of results had its own group … … 123 121 #that is the current treatment 124 122 #here we have a more NC approach with time being a dimension 125 keylist = [key for key in curclass.variables]126 dimlist = [curclass.variables[key].dimensions for key in keylist]127 indexlist = np.arange(0, len(NCFile.dimensions['Time']))128 AllHaveTime = np.all(['Time' in dimtuple for dimtuple in dimlist])129 123 listtype = curclass.classtype 130 if AllHaveTime: 124 if len(NCFile.dimensions['Time']) == 1: 125 nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]] = getattr(__import__(listtype), listtype)() 126 Tree = nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]] 127 else: 131 128 #Time dimension is in all the variables so we take that as stepnumber for the results 132 129 if onlylast: #we load only the last result to save on time and memory … … 136 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'])))] 137 134 Tree = nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]][:] 138 elif len(NCFile.dimensions['Time']) == 1: 139 nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]] = getattr(__import__(listtype), listtype)()140 Tree = nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]]141 else:142 143 print("ERROR: Time dimension is not in all results. That has been overlooked for now but your resulat are not saved.")135 136 elif classtype[mod][0] == 'massfluxatgate': #this is for output definitions 137 defname = split('Output|[0-9]+', classtree[mod][1])[1] + 's' 138 defindex = int(findall('[0-9]+', classtree[mod][1])[0]) 139 nvdict['md'].__dict__[classtree[mod][0]].__dict__[defname].append(getattr(classtype[mod][1], classtype[mod][0])()) 140 Tree = nvdict['md'].__dict__[classtree[mod][0]].__dict__[defname][defindex - 1] 144 141 145 142 else: 143 if debug: 144 print("Using the default for md.{}.{}, is that right??".format(classtree[mod][0], classtree[mod][1])) 146 145 nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]] = getattr(classtype[mod][1], classtype[mod][0])() 147 146 Tree = nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]] … … 180 179 print('table dimension greater than 3 not implemented yet') 181 180 else: 181 incomplete = 'Time' not in varval.dimensions 182 if incomplete: 183 chosendim = varval.dimensions[0] 184 indexlist = np.arange(0, len(NCFile.dimensions[chosendim])) 185 print('WARNING, {} is not present on every times, we chose {}({}) as the dimension to write it with'.format(var, chosendim, len(NCFile.dimensions[chosendim]))) 186 else: 187 indexlist = np.arange(0, len(NCFile.dimensions['Time'])) 182 188 for t in indexlist: 183 189 if vardim == 0: … … 266 272 #==== And with atribute {{{ 267 273 for attr in listclass.ncattrs(): 274 if debug: 275 print("treating attribute {}".format(attr)) 268 276 if attr != 'classtype': #classtype is for treatment, don't get it back 277 attribute = str(attr).swapcase() #there is a reason for swapcase, no sure what it isanymore 278 if attr == 'VARNAME': 279 attribute = 'name' 269 280 if type(Tree) == list: 270 281 t = int(indexlist[i]) 271 282 if listtype == 'dict': 272 Tree[t][ str(attr).swapcase()] = str(listclass.getncattr(attr))273 else: 274 Tree[t].__dict__[ str(attr).swapcase()] = str(listclass.getncattr(attr))283 Tree[t][attribute] = str(listclass.getncattr(attr)) 284 else: 285 Tree[t].__dict__[attribute] = str(listclass.getncattr(attr)) 275 286 else: 276 Tree.__dict__[ str(attr).swapcase()] = str(listclass.getncattr(attr))287 Tree.__dict__[attribute] = str(listclass.getncattr(attr)) 277 288 if listclass.getncattr(attr) == 'True': 278 Tree.__dict__[ str(attr).swapcase()] = True289 Tree.__dict__[attribute] = True 279 290 elif listclass.getncattr(attr) == 'False': 280 Tree.__dict__[ str(attr).swapcase()] = False291 Tree.__dict__[attribute] = False 281 292 # }}} 282 293 # }}}
Note:
See TracChangeset
for help on using the changeset viewer.