- Timestamp:
- Mar 26, 2018, 4:46:27 PM (7 years ago)
- Location:
- trunk/tools
- Files:
-
- 1 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/nc_var_tools.py
r1862 r1863 31 31 # CDO_toCF: Function to pass a CDO output file to CF-conventions 32 32 # CFmorzization: Function to provide a CF-compilation version of a variable within a file 33 # CFvars: Function to adapt a given variable and file following CF-standards 33 34 # changevartype: Function to change the type of a variable (when possible) 34 35 # chdimname: Changing the name of the dimension … … 23090 23091 return newdim, newvar, newvarv, nonCFattr 23091 23092 23093 def CFvars(cfvarn, vardims, varvalues, outnc): 23094 """ Function to adapt a given variable and file following CF-standards 23095 NOTE: reading information from 'CFvariables.dat' 23096 cfvarn= provided CF name of the variabale 23097 vardims= dimensions of the variable 23098 varvalues= values of the variable 23099 outnc= netCDF object for the output 23100 """ 23101 fname = 'CFvars' 23102 inCFvarfile = 'CFvariables.dat' 23103 availactions = ['new_z_dim'] 23104 23105 oCFf = open(inCFvarfile, 'r') 23106 CFvars = {} 23107 for line in oCFf: 23108 if line[0:1] != '#' and len(line) > 1: 23109 linevals = line.replace('\n','').replace('\t','').split(' ') 23110 CFvars[linevals[0]] = linevals[1:len(linevals)] 23111 23112 if not CFvars.has_key(cfvarn): 23113 print errormsg 23114 print ' ' + fname + ": file '" + inCFvarfile + "' does not have CF " + \ 23115 "variable: '" + cfvarn + "' !!" 23116 print ' available ones:', CFvars.keys() 23117 print ' Nothing will be changed' 23118 return None, None 23119 else: 23120 # Processing variable and take actions 23121 print infmsg 23122 print ' ' + fname + ": processing variale '" + cfvarn + "' with values:", \ 23123 CFvars[cfvarn] 23124 CFactions = CFvars[cfvarn] 23125 action = CFactions[0] 23126 23127 # Adding z-dimension 23128 if action == 'new_z_dim': 23129 newdim = CFactions[1] 23130 newvdim = CFactions[2] 23131 newvvar = np.float(CFactions[3]) 23132 outnc.createDimension(newdim, 1) 23133 dimvals = gen.CFcorValues(newdim) 23134 if not gen.searchInlist(outnc.dimensions, newdim): 23135 newdim = outnc.createDimension(newdim, 1) 23136 if not outnc.variables.has_key(newvdim): 23137 newvar = outnc.createVariable(newvdim, 'f8', (newdim)) 23138 basicvardef(newvar, dimvals['stdn'], dimvals['longname'], \ 23139 dimvals['units']) 23140 newvar[:] = newvvar 23141 for ivn in dimvals.keys(): 23142 if ivn != 'dimn' and ivn != 'stdn' and ivn != 'longname' and \ 23143 ivn != 'units' and ivn != 'maxrank' and ivn != 'length': 23144 set_attribute(newvar,ivn,dimvals[ivn]) 23145 newvdn = [] 23146 newvshape = [] 23147 idim = 0 23148 for vdn in vardims: 23149 if vdn == 'time': 23150 newvdn.append(vdn) 23151 newvshape.append(varvalues.shape[idim]) 23152 newvdn.append(newdim) 23153 newvshape.append(1) 23154 else: 23155 newvdn.append(vdn) 23156 newvshape.append(varvalues.shape[idim]) 23157 idim = idim + 1 23158 newvarvalues = np.zeros(tuple(newvshape), dtype=np.float) 23159 newvarvalues[:,0,:,:] = varvalues 23160 else: 23161 print errormsg 23162 print ' ' + fname + ": action '" + action + "' not ready !!" 23163 print ' available onees:', availactions 23164 quit(-1) 23165 23166 return newvdn, newvarvalues 23167 23092 23168 def CFmorzization(values, ncfile, variable): 23093 23169 """ Function to provide a CF-compilation version of a variable within a file … … 23562 23638 print ' ' + fname + ": creation of variable '" + cfvarn + "(" + \ 23563 23639 ', '.join(CFvardims) + ")' ..." 23564 newvar=onewnc.createVariable(cfvarn, 'f4', tuple(CFvardims), \23565 fill_value=gen.fillValueF)23566 basicvardef(newvar, stdvarn, longvarn, utsvarn)23567 23568 23640 # Providing the right coordinates 23569 23641 coorv = [] … … 23573 23645 elif cdn == 'time': print ' ' 23574 23646 else: coorv.append(cdn) 23647 newVdims, newVvalues = CFvars(cfvarn, CFvardims, ovar[:], onewnc) 23648 if newVdims is None: 23649 newvar=onewnc.createVariable(cfvarn, 'f4', tuple(CFvardims), \ 23650 fill_value=gen.fillValueF) 23651 basicvardef(newvar, stdvarn, longvarn, utsvarn) 23652 # Setting values, but taking into account pre-existing masked values 23653 varv = ovar[:] 23654 if type(varv) == type(gen.mamat): 23655 imaskv = varv.fill_value 23656 varv = np.where(varv == fill_value, gen.fillValueF, varv) 23657 else: 23658 newvar=onewnc.createVariable(cfvarn, 'f4', tuple(newVdims), \ 23659 fill_value=gen.fillValueF) 23660 basicvardef(newvar, stdvarn, longvarn, utsvarn) 23661 # Setting values, but taking into account pre-existing masked values 23662 if type(newVvalues) == type(gen.mamat): 23663 imaskv = varv.fill_value 23664 varv = np.where(varv == fill_value, gen.fillValueF, newVvalues) 23665 else: 23666 varv = newVvalues 23667 23668 newvar[:] = varv[:] 23575 23669 set_attribute(newvar, 'coordinates', ' '.join(coorv)) 23576 23670 23577 # Setting values, but taking into account pre-existing masked values23578 varv = ovar[:]23579 if type(varv) == type(gen.mamat):23580 imaskv = varv.fill_value23581 varv = np.where(varv == fill_value, gen.fillValueF, varv)23582 23583 newvar[:] = varv[:]23584 23671 set_attribute(newvar, 'grid_mapping', gattr['grid']) 23585 23672 onewnc.sync()
Note: See TracChangeset
for help on using the changeset viewer.