- Timestamp:
- Apr 4, 2018, 3:07:59 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/nc_var_tools.py
r1887 r1888 23302 23302 # Adding p-dimension 23303 23303 if action == 'new_p_dim': 23304 newdim = CFactions[1] 23305 newvdim = CFactions[2] 23306 if CFactions[3].find('FROMfile') == -1: 23307 newvvar = np.float(CFactions[3]) 23304 # Only doing it if variables does not have already dimension 'pres' 23305 if gen.searchInlist(vardims,'pres'): 23306 print infmsg 23307 print ' ' + fname + ": variable already has 'pres' dimension " +\ 23308 "doing nothing !!" 23309 return None, None 23308 23310 else: 23309 vn = CFactions[3].split(',')[1] 23310 if not innc.variables.has_key(vn): 23311 print errormsg 23312 print ' ' + fname + ": input file does not have variable '" + vn\ 23313 + "' !!" 23314 print ' available ones:', innc.variables.keys() 23315 quit(-1) 23316 ovar = innc.variables[vn] 23317 # Assuming all the same value! 23318 var1D = ovar[:].flatten() 23319 newvvar = var1D[0] 23320 outnc.createDimension(newdim, 1) 23321 dimvals = gen.CFcorValues(newdim) 23322 if not gen.searchInlist(outnc.dimensions, newdim): 23323 newdim = outnc.createDimension(newdim, 1) 23324 if not outnc.variables.has_key(newvdim): 23325 newvar = outnc.createVariable(newvdim, 'f8', (newdim)) 23326 basicvardef(newvar, dimvals['stdn'], dimvals['longname'], \ 23327 dimvals['units']) 23328 newvar[:] = newvvar 23329 for ivn in dimvals.keys(): 23330 if ivn != 'dimn' and ivn != 'stdn' and ivn != 'longname' and \ 23331 ivn != 'units' and ivn != 'maxrank' and ivn != 'length': 23332 set_attribute(newvar,ivn,dimvals[ivn]) 23333 newvdn = [] 23334 newvshape = [] 23335 idim = 0 23336 for vdn in vardims: 23337 if vdn == 'time': 23338 newvdn.append(vdn) 23339 newvshape.append(varvalues.shape[idim]) 23340 newvdn.append(newdim) 23341 newvshape.append(1) 23311 newdim = CFactions[1] 23312 newvdim = CFactions[2] 23313 if CFactions[3].find('FROMfile') == -1: 23314 newvvar = np.float(CFactions[3]) 23342 23315 else: 23343 newvdn.append(vdn) 23344 newvshape.append(varvalues.shape[idim]) 23345 idim = idim + 1 23346 newvarvalues = np.zeros(tuple(newvshape), dtype=np.float) 23347 newvarvalues[:,0,:,:] = varvalues 23316 vn = CFactions[3].split(',')[1] 23317 if not innc.variables.has_key(vn): 23318 print errormsg 23319 print ' ' + fname + ": input file does not have variable '"+\ 23320 vn + "' !!" 23321 print ' available ones:', innc.variables.keys() 23322 quit(-1) 23323 ovar = innc.variables[vn] 23324 # Assuming all the same value! 23325 var1D = ovar[:].flatten() 23326 newvvar = var1D[0] 23327 outnc.createDimension(newdim, 1) 23328 dimvals = gen.CFcorValues(newdim) 23329 if not gen.searchInlist(outnc.dimensions, newdim): 23330 newdim = outnc.createDimension(newdim, 1) 23331 if not outnc.variables.has_key(newvdim): 23332 newvar = outnc.createVariable(newvdim, 'f8', (newdim)) 23333 basicvardef(newvar, dimvals['stdn'], dimvals['longname'], \ 23334 dimvals['units']) 23335 newvar[:] = newvvar 23336 for ivn in dimvals.keys(): 23337 if ivn != 'dimn' and ivn != 'stdn' and ivn != 'longname' and \ 23338 ivn != 'units' and ivn != 'maxrank' and ivn != 'length': 23339 set_attribute(newvar,ivn,dimvals[ivn]) 23340 newvdn = [] 23341 newvshape = [] 23342 idim = 0 23343 for vdn in vardims: 23344 if vdn == 'time': 23345 newvdn.append(vdn) 23346 newvshape.append(varvalues.shape[idim]) 23347 newvdn.append(newdim) 23348 newvshape.append(1) 23349 else: 23350 newvdn.append(vdn) 23351 newvshape.append(varvalues.shape[idim]) 23352 idim = idim + 1 23353 newvarvalues = np.zeros(tuple(newvshape), dtype=np.float) 23354 newvarvalues[:,0,:,:] = varvalues 23348 23355 23349 23356 # Adding z-dimension 23350 23357 elif action == 'new_z_dim': 23351 newdim = CFactions[1] 23352 newvdim = CFactions[2] 23353 if CFactions[3].find('FROMfile') == -1: 23354 newvvar = np.float(CFactions[3]) 23358 # Only doing it if variables does not have already dimension 'pres' 23359 if gen.searchInlist(vardims,'pres'): 23360 print infmsg 23361 print ' ' + fname + ": variable already has 'pres' dimension " +\ 23362 "doing nothing !!" 23363 return None, None 23355 23364 else: 23356 vn = CFactions[3].split(',')[1] 23357 if not innc.variables.has_key(vn): 23358 print errormsg 23359 print ' ' + fname + ": input file does not have variable '" + vn\ 23360 + "' !!" 23361 print ' available ones:', innc.variables.keys() 23362 quit(-1) 23363 ovar = innc.variables[vn] 23364 # Assuming all the same value! 23365 var1D = ovar.flatten() 23366 newvvar = var1D[0] 23367 outnc.createDimension(newdim, 1) 23368 dimvals = gen.CFcorValues(newdim) 23369 if not gen.searchInlist(outnc.dimensions, newdim): 23370 newdim = outnc.createDimension(newdim, 1) 23371 if not outnc.variables.has_key(newvdim): 23372 newvar = outnc.createVariable(newvdim, 'f8', (newdim)) 23373 basicvardef(newvar, dimvals['stdn'], dimvals['longname'], \ 23374 dimvals['units']) 23375 newvar[:] = newvvar 23376 for ivn in dimvals.keys(): 23377 if ivn != 'dimn' and ivn != 'stdn' and ivn != 'longname' and \ 23378 ivn != 'units' and ivn != 'maxrank' and ivn != 'length': 23379 set_attribute(newvar,ivn,dimvals[ivn]) 23380 newvdn = [] 23381 newvshape = [] 23382 idim = 0 23383 for vdn in vardims: 23384 if vdn == 'time': 23385 newvdn.append(vdn) 23386 newvshape.append(varvalues.shape[idim]) 23387 newvdn.append(newdim) 23388 newvshape.append(1) 23365 newdim = CFactions[1] 23366 newvdim = CFactions[2] 23367 if CFactions[3].find('FROMfile') == -1: 23368 newvvar = np.float(CFactions[3]) 23389 23369 else: 23390 newvdn.append(vdn) 23391 newvshape.append(varvalues.shape[idim]) 23392 idim = idim + 1 23393 newvarvalues = np.zeros(tuple(newvshape), dtype=np.float) 23394 newvarvalues[:,0,:,:] = varvalues 23370 vn = CFactions[3].split(',')[1] 23371 if not innc.variables.has_key(vn): 23372 print errormsg 23373 print ' ' + fname + ": input file does not have variable '"+\ 23374 vn + "' !!" 23375 print ' available ones:', innc.variables.keys() 23376 quit(-1) 23377 ovar = innc.variables[vn] 23378 # Assuming all the same value! 23379 var1D = ovar[:].flatten() 23380 newvvar = var1D[0] 23381 outnc.createDimension(newdim, 1) 23382 dimvals = gen.CFcorValues(newdim) 23383 if not gen.searchInlist(outnc.dimensions, newdim): 23384 newdim = outnc.createDimension(newdim, 1) 23385 if not outnc.variables.has_key(newvdim): 23386 newvar = outnc.createVariable(newvdim, 'f8', (newdim)) 23387 basicvardef(newvar, dimvals['stdn'], dimvals['longname'], \ 23388 dimvals['units']) 23389 newvar[:] = newvvar 23390 for ivn in dimvals.keys(): 23391 if ivn != 'dimn' and ivn != 'stdn' and ivn != 'longname' and \ 23392 ivn != 'units' and ivn != 'maxrank' and ivn != 'length': 23393 set_attribute(newvar,ivn,dimvals[ivn]) 23394 newvdn = [] 23395 newvshape = [] 23396 idim = 0 23397 for vdn in vardims: 23398 if vdn == 'time': 23399 newvdn.append(vdn) 23400 newvshape.append(varvalues.shape[idim]) 23401 newvdn.append(newdim) 23402 newvshape.append(1) 23403 else: 23404 newvdn.append(vdn) 23405 newvshape.append(varvalues.shape[idim]) 23406 idim = idim + 1 23407 newvarvalues = np.zeros(tuple(newvshape), dtype=np.float) 23408 newvarvalues[:,0,:,:] = varvalues 23395 23409 else: 23396 23410 print errormsg
Note: See TracChangeset
for help on using the changeset viewer.