Changeset 316 in lmdz_wrf
- Timestamp:
- Feb 26, 2015, 3:08:23 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/nc_var_tools.py
r315 r316 11969 11969 dimensions of the resultant operation 11970 11970 ncfile= netCDF file 11971 varn= variable name11971 varn= ',' list of variable names ('all' for all) 11972 11972 """ 11973 11973 … … 11984 11984 dimvn = values.split(',')[3] 11985 11985 11986 objnc = NetCDFFile(ncfile, 'r') 11987 11988 if varn.find(',') != -1: 11989 if varn == 'all': 11990 varns = objnc.variables 11991 else: 11992 varns = varn.split(',') 11993 else: 11994 varns = [varn] 11995 11986 11996 ofile = 'file_oper_alongdims_' + operkind + '.nc' 11987 11997 11988 objnc = NetCDFFile(ncfile, 'r') 11989 11990 if not objnc.variables.has_key(varn): 11991 print errormsg 11992 print ' ' + fname + ': netCDF file "' + ncfile + \ 11993 '" does not have variable "' + varn + '" !!!!' 11994 quit(-1) 11995 11996 objvar = objnc.variables[varn] 11997 11998 newvarv, newvardimns = operation_alongdims(objvar,dimvals,dimsoper,operkind) 11999 12000 print 'dimsoper:',dimsoper 12001 print 'newvar shape',newvarv.shape,'newvardims:',newvardimns 11998 if dimsoper.find(':') != -1: 11999 dimstouse = dimsoper.split(':') 12000 else: 12001 dimstouse = [dimsoper] 12002 12002 12003 12003 # Creation of output file … … 12006 12006 objnewnc = NetCDFFile(ofile, 'w') 12007 12007 12008 print ' ' + fname + ": '" + operkind + "' with:",dimsoper,'_______' 12009 for vn in varns: 12010 print "'" + vn + "' ... .. ." 12011 12012 if not objnc.variables.has_key(vn): 12013 print errormsg 12014 print ' ' + fname + ': netCDF file "' + ncfile + \ 12015 '" does not have variable "' + vn + '" !!!!' 12016 quit(-1) 12017 12018 # Variable 12019 objvar = objnc.variables[vn] 12020 12021 allfound = True 12022 for idimt in dimstouse: 12023 if not searchInlist(objvar.dimensions, idimt): 12024 allfound = False 12025 break 12026 12027 if not allfound: 12028 print warnmsg 12029 print ' ' + fname + ": variable '" + vn + "' (", objvar.dimensions, \ 12030 ') does not have all the equired dimensions:', dimstouse, '!!' 12031 print ' skipping it' 12032 12033 else: 12034 newvarv, newvardimns=operation_alongdims(objvar,dimvals,dimsoper,operkind) 12035 12008 12036 # Dimensions 12009 for idim in range(len(newvardimns)): 12010 objdim = objnc.dimensions[newvardimns[idim]] 12011 if objdim.isunlimited(): 12012 objnewnc.createDimension(newvardimns[idim], None) 12013 else: 12014 objnewnc.createDimension(newvardimns[idim], len(objdim)) 12037 for idim in range(len(newvardimns)): 12038 objdim = objnc.dimensions[newvardimns[idim]] 12039 if not objnewnc.dimensions.has_key(newvardimns[idim]): 12040 if objdim.isunlimited(): 12041 objnewnc.createDimension(newvardimns[idim], None) 12042 else: 12043 objnewnc.createDimension(newvardimns[idim], len(objdim)) 12015 12044 12016 12045 # Variables with dimension values 12017 vardims = dimvn.split(':') 12018 for ivdim in range(len(vardims)): 12019 objvdim = objnc.variables[vardims[ivdim]] 12020 dimsvdim = objvdim.dimensions 12021 dimvslice = [] 12022 dimvdims = [] 12023 for idmvd in range(len(dimsvdim)): 12024 found = False 12025 for idimv in range(len(newvardimns)): 12026 if dimsvdim[idmvd] == newvardimns[idimv]: 12027 dimvslice.append(slice(0,objvdim.shape[idmvd])) 12028 dimvdims.append(dimsvdim[idmvd]) 12029 found = True 12030 break 12031 if not found: dimvslice.append(len(objnc.dimensions[dimsvdim[idmvd]])/2) 12032 12033 newvar = objnewnc.createVariable(vardims[ivdim], 'f8', tuple(dimvdims)) 12034 newvar[:] = objvdim[tuple(dimvslice)] 12035 dimvattrs = objvdim.ncattrs() 12036 for attrn in dimvattrs: 12037 attrv = objvdim.getncattr(attrn) 12038 newattr = set_attribute(newvar,attrn,attrv) 12046 vardims = dimvn.split(':') 12047 for ivdim in range(len(vardims)): 12048 if not objnewnc.variables.has_key(vardims[ivdim]): 12049 objvdim = objnc.variables[vardims[ivdim]] 12050 dimsvdim = objvdim.dimensions 12051 dimvslice = [] 12052 dimvdims = [] 12053 for idmvd in range(len(dimsvdim)): 12054 found = False 12055 for idimv in range(len(newvardimns)): 12056 if dimsvdim[idmvd] == newvardimns[idimv]: 12057 dimvslice.append(slice(0,objvdim.shape[idmvd])) 12058 dimvdims.append(dimsvdim[idmvd]) 12059 found = True 12060 break 12061 if not found: 12062 dimvslice.append(len(objnc.dimensions[dimsvdim[idmvd]])/2) 12063 12064 newvar = objnewnc.createVariable(vardims[ivdim], 'f8', 12065 tuple(dimvdims)) 12066 newvar[:] = objvdim[tuple(dimvslice)] 12067 dimvattrs = objvdim.ncattrs() 12068 for attrn in dimvattrs: 12069 attrv = objvdim.getncattr(attrn) 12070 newattr = set_attribute(newvar,attrn,attrv) 12039 12071 12040 12072 # new variable 12041 oldvarattr = objvar.ncattrs()12042 12043 varname = variables_values(varn)[0]12044 if searchInlist(oldvarattr, 'standard_name'):12045 stdname = objvar.getncattr('standard_name')12046 else:12047 stdname = variables_values(varn)[1]12048 12049 if searchInlist(oldvarattr, 'long_name'):12050 lname = objvar.getncattr('long_name')12051 else:12052 lname = variables_values(varn)[4].replace('|',' ')12053 12054 if searchInlist(oldvarattr, 'units'):12055 uname = objvar.getncattr('units')12056 else:12057 uname = variables_values(varn)[5]12058 12059 dimsoperS = numVector_String(dimsoper.split(':'),', ')12060 12061 newvar = objnewnc.createVariable(varname + operkind, 'f4', tuple(newvardimns))12062 newattr = basicvardef(newvar, stdname + operkind, lname + ' '+ operkind + \12063 ' along ' + dimsoperS, uname)12064 12065 newvar[:] = newvarv 12066 for idim in range(len(objvar.shape)):12067 dimn = dimvals.split('|')[idim].split(':')[0]12068 dimv = '{:d}'.format(len(objnc.dimensions[dimn]))12069 if idim == 0:12070 origdimsS = dimn + '(' + dimv + ')'12071 else:12072 origdimsS = origdimsS + ' ' + dimn + '(' + dimv + ')'12073 12074 12075 newattr = set_attribute(newvar, 'orig_dimensions', origdimsS)12076 12073 oldvarattr = objvar.ncattrs() 12074 12075 varname = variables_values(vn)[0] 12076 if searchInlist(oldvarattr, 'standard_name'): 12077 stdname = objvar.getncattr('standard_name') 12078 else: 12079 stdname = variables_values(vn)[1] 12080 12081 if searchInlist(oldvarattr, 'long_name'): 12082 lname = objvar.getncattr('long_name') 12083 else: 12084 lname = variables_values(vn)[4].replace('|',' ') 12085 12086 if searchInlist(oldvarattr, 'units'): 12087 uname = objvar.getncattr('units') 12088 else: 12089 uname = variables_values(vn)[5] 12090 12091 dimsoperS = numVector_String(dimsoper.split(':'),', ') 12092 12093 newvar = objnewnc.createVariable(varname + operkind, 'f4', 12094 tuple(newvardimns)) 12095 newattr = basicvardef(newvar, stdname + operkind, lname + ' '+ operkind +\ 12096 ' along ' + dimsoperS, uname) 12097 12098 newvar[:] = newvarv 12099 for idim in range(len(objvar.shape)): 12100 dimn = dimvals.split('|')[idim].split(':')[0] 12101 dimv = '{:d}'.format(len(objnc.dimensions[dimn])) 12102 if idim == 0: 12103 origdimsS = dimn + '(' + dimv + ')' 12104 else: 12105 origdimsS = origdimsS + ' ' + dimn + '(' + dimv + ')' 12106 12107 newattr = set_attribute(newvar, 'orig_dimensions', origdimsS) 12108 12077 12109 # global attributes 12078 12110 objnewnc.setncattr('author', 'L. Fita') … … 12100 12132 12101 12133 return 12134 12135 #file_oper_alongdims('Time:-1|bottom_top:-1|south_north:-1|west_east:-1,bottom_top,sum,Times:XLAT:XLONG', 12136 # '/home/lluis/PY/wrfout_d01_2001-11-11_00:00:00.tests', 'QVAPOR') 12102 12137 12103 12138 #file_oper_alongdims('time:-1|z:-1|x:-1|y:-1,time:y,mean,pressure:lat', '/home/lluis/etudes/WRF_LMDZ/WaquaL/WRF_LMDZ/AR40/vertical_interpolation_WRFp.nc', 'WRFt')
Note: See TracChangeset
for help on using the changeset viewer.