Changeset 21247 for issm/trunk-jpl/src/m/io
- Timestamp:
- 10/06/16 09:22:31 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
issm/trunk-jpl/src/m/io/loadvars.py
r21236 r21247 78 78 NCFile=Dataset(filename,mode='r') 79 79 NCFile.close() 80 classtype,classtree=netCDFread(filename)81 nvdict['md']=model()82 module=map(__import__,dict.values(classtype))83 for i,mod in enumerate(dict.keys(classtype)):84 # print('treating md.{}'.format(mod))85 if np.size(classtree[mod])>1:86 if classtree[mod][0]=='results':87 #treating results (Dimension4 is time)88 resdim=len(NCFile.dimensions['Dimension4'])89 curclass=NCFile.groups[classtree[mod][0]].groups[classtree[mod][1]]90 nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]] = [getattr(module[i],classtype[mod])()]91 if resdim>1:92 for t in range(1,resdim):93 nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]].append(getattr(module[i],classtype[mod])())94 Tree=nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]][:]95 else:96 curclass=NCFile.groups[classtree[mod][0]].groups[classtree[mod][1]]97 nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]] = getattr(module[i],classtype[mod])()98 Tree=nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]]99 else:100 curclass=NCFile.groups[classtree[mod][0]]101 nvdict['md'].__dict__[mod] = getattr(module[i],classtype[mod])()102 Tree=nvdict['md'].__dict__[classtree[mod][0]]103 for var in curclass.variables:104 #print(' treating {}'.format(var))105 varval=curclass.variables[str(var)]106 vardim=varval.ndim107 try:108 val_type=str(varval.dtype)109 except AttributeError:110 val_type=type(varval)111 if vardim==0:112 try:113 Tree.__dict__[str(var)]=varval.getValue()114 if varval.getValue()=='True':115 Tree.__dict__[str(var)]=True116 elif varval.getValue()=='False':117 Tree.__dict__[str(var)]=False118 except IndexError:119 Tree.__dict__[str(var)]=[]120 elif vardim==1:121 if varval.dtype==str:122 if varval.shape==1:123 Tree.__dict__[str(var)]=str(varval[0])124 if 'True' in varval[:] or 'False' in varval[:]:125 Tree.__dict__[str(var)]=np.asarray(varval[:],dtype=bool)126 else:127 if classtree[mod][0]=='results' and resdim>1:128 for t in range(0,resdim):129 Tree[t].__dict__[str(var)]=varval[t]130 else:131 Tree.__dict__[str(var)]=varval[:]132 elif vardim==2:133 #dealling with dict134 if varval.dtype==str:135 Tree.__dict__[str(var)]=dict(zip(varval[:,0], varval[:,1]))136 else:137 if classtree[mod][0]=='results' and resdim>1:138 for t in range(0,resdim):139 Tree[t].__dict__[str(var)]=varval[:,t]140 else:141 Tree.__dict__[str(var)]=varval[:,:]142 elif vardim==3:143 if classtree[mod][0]=='results' and resdim>1:144 for t in range(0,resdim):145 Tree[t].__dict__[str(var)]=varval[:,:,t]146 else:147 Tree.__dict__[str(var)]=varval[:,:,:]148 else:149 print 'table dimension greater than 3 not implemented yet'150 for attr in curclass.ncattrs():151 #print(' treating {}'.format(attr))152 if classtree[mod][0]!='results' and attr!='classtype': #no attributes in results153 Tree.__dict__[str(attr)]=str(curclass.getncattr(attr))154 if curclass.getncattr(attr)=='True':155 Tree.__dict__[str(attr)]=True156 elif curclass.getncattr(attr)=='False':157 Tree.__dict__[str(attr)]=False158 159 80 except RuntimeError: 160 81 raise IOError("File '%s' not found." % filename) 161 82 83 classtype,classtree=netCDFread(filename) 84 nvdict['md']=model() 85 NCFile=Dataset(filename,mode='r') 86 for mod in dict.keys(classtype): 87 if np.size(classtree[mod])>1: 88 curclass=NCFile.groups[classtree[mod][0]].groups[classtree[mod][1]] 89 if classtype[mod][0]=='list': 90 keylist=[key for key in curclass.groups] 91 listtype=curclass.groups[keylist[0]].classtype 92 if listtype=='dict': 93 nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]] = [{} for i in range(max(1,len(curclass.groups)))] 94 else: 95 nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]] = [getattr(__import__(listtype),listtype)() for i in range(max(1,len(curclass.groups)))] 96 Tree=nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]][:] 97 else: 98 nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]] = getattr(classtype[mod][1],classtype[mod][0])() 99 Tree=nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]] 100 else: 101 curclass=NCFile.groups[classtree[mod][0]] 102 nvdict['md'].__dict__[mod] = getattr(classtype[mod][1],classtype[mod][0])() 103 Tree=nvdict['md'].__dict__[classtree[mod][0]] 104 #treating groups that are lists 105 for i in range(0,max(1,len(curclass.groups))): 106 if len(curclass.groups)>0: 107 listclass=curclass.groups[keylist[i]] 108 else: 109 listclass=curclass 110 for var in listclass.variables: 111 if var not in ['errlog','outlog']: 112 varval=listclass.variables[str(var)] 113 vardim=varval.ndim 114 try: 115 val_type=str(varval.dtype) 116 except AttributeError: 117 val_type=type(varval) 118 if vardim==0: 119 if type(Tree)==list: 120 t=int(keylist[i][-1])-1 121 if listtype=='dict': 122 Tree[t][str(var)]=varval.getValue() 123 else: 124 Tree[t].__dict__[str(var)]=varval.getValue() 125 else: 126 try: 127 Tree.__dict__[str(var)]=varval.getValue() 128 if varval.getValue()=='True': 129 Tree.__dict__[str(var)]=True 130 elif varval.getValue()=='False': 131 Tree.__dict__[str(var)]=False 132 except IndexError: 133 Tree.__dict__[str(var)]=[] 134 elif vardim==1: 135 if varval.dtype==str: 136 if varval.shape[0]==1: 137 Tree.__dict__[str(var)]=[str(varval[0]),] 138 elif 'True' in varval[:] or 'False' in varval[:]: 139 Tree.__dict__[str(var)]=np.asarray([V=='True' for V in varval[:]],dtype=bool) 140 else: 141 Tree.__dict__[str(var)]=[str(vallue) for vallue in varval[:]] 142 else: 143 if type(Tree)==list: 144 t=int(keylist[i][-1])-1 145 if listtype=='dict': 146 Tree[t][str(var)]=varval[:] 147 else: 148 Tree[t].__dict__[str(var)]=varval[:] 149 else: 150 try: 151 #some thing specifically require a list 152 mdtype=type(Tree.__dict__[str(var)]) 153 except KeyError: 154 print 'not defined yet' 155 mdtype=float 156 if mdtype==list: 157 Tree.__dict__[str(var)]=[mdval for mdval in varval[:]] 158 else: 159 Tree.__dict__[str(var)]=varval[:] 160 elif vardim==2: 161 #dealling with dict 162 if varval.dtype==str: 163 Tree.__dict__[str(var)]=dict(zip(varval[:,0], varval[:,1])) 164 else: 165 if type(Tree)==list: 166 t=int(keylist[i][-1])-1 167 if listtype=='dict': 168 Tree[t][str(var)]=varval[:,:] 169 else: 170 Tree[t].__dict__[str(var)]=varval[:,:] 171 else: 172 Tree.__dict__[str(var)]=varval[:,:] 173 elif vardim==3: 174 if type(Tree)==list: 175 t=int(keylist[i][-1])-1 176 if listtype=='dict': 177 Tree[t][str(var)]=varval[:,:,:] 178 else: 179 Tree[t].__dict__[str(var)]=varval[:,:,:] 180 else: 181 Tree.__dict__[str(var)]=varval[:,:,:] 182 else: 183 print 'table dimension greater than 3 not implemented yet' 184 for attr in listclass.ncattrs(): 185 if attr!='classtype': #classtype is for treatment, don't get it back 186 if type(Tree)==list: 187 t=int(keylist[i][-1])-1 188 if listtype=='dict': 189 Tree[t][str(attr)]=str(listclass.getncattr(attr)) 190 else: 191 Tree[t].__dict__[str(attr)]=str(listclass.getncattr(attr)) 192 else: 193 Tree.__dict__[str(attr)]=str(listclass.getncattr(attr)) 194 if listclass.getncattr(attr)=='True': 195 Tree.__dict__[str(attr)]=True 196 elif listclass.getncattr(attr)=='False': 197 Tree.__dict__[str(attr)]=False 198 NCFile.close() 162 199 if len(args) >= 2 and isinstance(args[1],(str,unicode)): # (value) 163 200 value=[nvdict[name] for name in args[1:]] … … 173 210 174 211 def netCDFread(filename): 175 def walktree(data):176 keys = data.groups.keys()177 for key in keys:178 yield [str(key)]179 for children in walktree(data.groups[str(key)]):180 child=[str(key)]181 child.append(str(children[0]))182 yield child183 212 print ('Opening {} for reading '.format(filename)) 184 213 NCData=Dataset(filename, 'r') 185 214 class_dict={} 186 215 class_tree={} 216 217 for group in NCData.groups: 218 if len(NCData.groups[group].groups)>0: 219 for subgroup in NCData.groups[group].groups: 220 classe=str(group)+'.'+str(subgroup) 221 class_dict[classe]=[str(getattr(NCData.groups[group].groups[subgroup],'classtype')),] 222 if class_dict[classe][0] not in ['dict','list']: 223 class_dict[classe].append(__import__(class_dict[classe][0])) 224 class_tree[classe]=[group,subgroup] 225 else: 226 classe=str(group) 227 class_dict[classe]=[str(getattr(NCData.groups[group],'classtype')),] 228 if class_dict[classe][0] not in ['dict','list']: 229 class_dict[classe].append(__import__(class_dict[classe][0])) 230 class_tree[classe]=[group,] 187 231 188 for children in walktree(NCData): 189 classe=str(children[0]) 190 if np.size(children)>1: 191 for name in children[1:]: 192 classe=classe+'.'+name 193 class_dict[classe]=str(getattr(NCData.groups[children[0]].groups[children[1]],'classtype')) 194 else: 195 class_dict[classe]=str(getattr(NCData.groups[classe],'classtype')) 196 class_tree[classe]=children 197 232 NCData.close() 198 233 return class_dict,class_tree
Note:
See TracChangeset
for help on using the changeset viewer.