- Timestamp:
- Mar 25, 2018, 4:53:59 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/nc_var_tools.py
r1849 r1853 994 994 attrvalue = value of the attribute 995 995 atrtrkind = kind of attribute: 'S', string ('!' as spaces); 'U', unicode ('!' as spaces); 'I', integer; 996 'Inp32', numpy integer 32; 'R', real; ' npfloat', np.float; 'D', np.float64996 'Inp32', numpy integer 32; 'R', ot 'F' real; ' npfloat', np.float; 'D', np.float64 997 997 """ 998 998 fname = 'set_attributek' 999 999 validk = {'S': 'string', 'U': 'unicode', 'I': 'integer', \ 1000 'Inp32': 'integer long (np.int32)', ' R': 'float', 'npfloat': 'np.float',\1001 ' D': 'float long (np.float64)'}1000 'Inp32': 'integer long (np.int32)', 'F': 'float', 'R': 'float', \ 1001 'npfloat': 'np.float', 'D': 'float long (np.float64)'} 1002 1002 1003 1003 if type(attrkind) == type('s'): … … 1008 1008 elif attrkind == 'I': 1009 1009 attrvalue = np.int(attrval) 1010 elif attrkind == 'R' :1010 elif attrkind == 'R' or attrkind == 'F' : 1011 1011 attrvalue = float(attrval) 1012 1012 elif attrkind == 'npfloat': … … 22722 22722 (...) (specific projection attributes and values) 22723 22723 [name] [value] [kind] ('I': integer, 'R': float, 'D': double float, 'S': string) 22724 Add three more attributes for the resolution of the projection: 22725 x_resolution [value] (resolution along x-axis) 22726 y_resoltuion [value] (resolution along y-axis) 22727 proj_units [value] (units of the resoltion of the projection) 22724 22728 ncfile= netCDF file to use 22725 22729 variable= ',' list of variables to CF transform ('all' for all variables). A separated file will be created … … 22787 22791 22788 22792 pattr = {} 22793 ppn = [] 22789 22794 opinff = open(projfilen, 'r') 22790 22795 for line in opinff: … … 22800 22805 quit(-1) 22801 22806 pattr[linevals[0]] = [linevals[1].replace('!',' '), linevals[2]] 22807 ppn.append(linevals[0]) 22802 22808 opinff.close() 22809 print ' ' + fname + ": values for projection '" + gattr['grid'] + "' _______" 22810 for iatn in ppn: 22811 pattrv = pattr[iatn] 22812 print iatn + ': ', gen.typemod(pattrv[0], pattrv[1]) 22803 22813 22804 22814 # Getting variables to process … … 22916 22926 quit(-1) 22917 22927 if CFdimvalues['length'] == -1: CFdimvalues['length'] = None 22918 CFdimvals[axn] = CFdimvalues22919 filedimvals[axn] = axsiv[:]22920 CFaxisvardimvals[axn] = CFvardimvalues22921 22928 22922 22929 # Some extra axis-stuff... … … 22931 22938 tunits = urefvals 22932 22939 tcal = 'standard' 22940 if gattr['calendar'] == '[unknown]': gattr['calendar']= tcal 22941 CFdimvalues['units'] = tunits 22942 22933 22943 Sinit = gen.datetimeStr_conversion(str(CFitime), 'cfTime,'+tunits, \ 22934 22944 'Y-m-dTH:M:SZ') … … 22970 22980 Sfreq = gattr['frequency'] 22971 22981 22982 CFdimvals[axn] = CFdimvalues 22983 filedimvals[axn] = axsiv[:] 22984 CFaxisvardimvals[axn] = CFvardimvalues 22985 22972 22986 # Renaming dimensions for 2D lon,lat variable-dimensions 22973 22987 if len(filedimvals['X'].shape) == 2 or len(filedimvals['Y'].shape) == 2: … … 22975 22989 print ' because Xvals have rank 2:', filedimvals['X'].shape, \ 22976 22990 'renaming lon,lat dimensions to x,y' 22991 addCFdimvals = {} 22977 22992 axv = CFdimvals['X'] 22978 22993 axv['dimn'] = 'x' 22994 axv['vdimn'] = 'lon' 22979 22995 CFdimvals['X'] = axv 22996 addCFdimvals['x'] = {'dimn': 'x', 'vdimn': 'x', 22997 'stdn': 'projection_x_coordinate','longname': 'x coordinate of projection',\ 22998 'units': pattr['proj_units'][0], 'maxrank': 1, \ 22999 'length': filedimvals['X'].shape[1]} 23000 Xres = np.float(pattr['x_resolution'][0]) 23001 filedimvals['x'] = np.arange((filedimvals['X'].shape[1]),dtype=np.float)*Xres 22980 23002 axv = CFdimvals['Y'] 22981 23003 axv['dimn'] = 'y' 23004 axv['vdimn'] = 'lat' 22982 23005 CFdimvals['Y'] = axv 23006 addCFdimvals['y'] = {'dimn': 'y', 'vdimn': 'y', 23007 'stdn': 'projection_y_coordinate','longname': 'y coordinate of projection',\ 23008 'units': pattr['proj_units'][0], 'maxrank': 1, \ 23009 'length': filedimvals['X'].shape[0]} 23010 Yres = np.float(pattr['y_resolution'][0]) 23011 filedimvals['y'] = np.arange((filedimvals['X'].shape[0]),dtype=np.float)*Yres 22983 23012 #axiv = axisinf['X'] 22984 23013 #filecfdimn[axiv[0]] = 'x' … … 23076 23105 for axn in varaxes: 23077 23106 dvals = CFdimvals[axn] 23078 if not gen.searchInlist(onewnc.variables.keys(), dvals['dimn']): 23079 newvar = onewnc.createVariable(dvals['dimn'], 'f8', \ 23080 tuple(CFaxisvardimvals[axn])) 23081 basicvardef(newvar, dvals['stdn'], dvals['longname'], dvals['units']) 23082 for ivn in dvals.keys(): 23083 if ivn != 'dimn' and ivn != 'stdn' and ivn != 'longname' and \ 23084 ivn != 'units' and ivn != 'maxrank' and ivn != 'length': 23085 set_attribute(newvar,ivn,dvals[ivn]) 23107 if dvals['dimn'] == dvals['vdimn']: 23108 if not gen.searchInlist(onewnc.variables.keys(), dvals['dimn']): 23109 newvar = onewnc.createVariable(dvals['dimn'], 'f8', \ 23110 tuple(CFaxisvardimvals[axn])) 23111 basicvardef(newvar, dvals['stdn'], dvals['longname'], \ 23112 dvals['units']) 23113 newvar[:] = filedimvals[axn] 23114 for ivn in dvals.keys(): 23115 if ivn != 'dimn' and ivn != 'stdn' and ivn != 'longname' and \ 23116 ivn != 'units' and ivn != 'maxrank' and ivn != 'length': 23117 set_attribute(newvar,ivn,dvals[ivn]) 23118 else: 23119 # Dimensions with a different name for the variable dimension 23120 if not gen.searchInlist(onewnc.variables.keys(), dvals['dimn']): 23121 ddvals = addCFdimvals[dvals['dimn']] 23122 newvar = onewnc.createVariable(dvals['dimn'], 'f8', \ 23123 tuple(dvals['dimn'])) 23124 basicvardef(newvar, ddvals['stdn'], ddvals['longname'], \ 23125 ddvals['units']) 23126 newvar[:] = filedimvals[dvals['dimn']] 23127 if not gen.searchInlist(onewnc.variables.keys(), dvals['vdimn']): 23128 newvar = onewnc.createVariable(dvals['vdimn'], 'f4', \ 23129 tuple(CFaxisvardimvals[axn])) 23130 basicvardef(newvar, dvals['stdn'], dvals['longname'], \ 23131 dvals['units']) 23132 newvar[:] = filedimvals[axn] 23133 for ivn in dvals.keys(): 23134 if ivn != 'dimn' and ivn != 'stdn' and ivn != 'longname' and \ 23135 ivn != 'units' and ivn != 'maxrank' and ivn != 'length': 23136 set_attribute(newvar,ivn,dvals[ivn]) 23086 23137 23087 23138 # Variable … … 23093 23144 fill_value=gen.fillValueF) 23094 23145 basicvardef(newvar, stdvarn, longvarn, utsvarn) 23095 set_attribute(newvar, 'coordinates', ' '.join(CFvardims)) 23146 23147 # Providing the right coordinates 23148 coorv = [] 23149 for cdn in CFvardims: 23150 if cdn == 'x': coorv.append('lon') 23151 elif cdn == 'y': coorv.append('lat') 23152 else: coorv.append(cdn) 23153 set_attribute(newvar, 'coordinates', ' '.join(coorv)) 23154 23096 23155 # Setting values, but taking into account pre-existing masked values 23097 23156 varv = ovar[:] … … 23101 23160 23102 23161 newvar[:] = varv[:] 23103 onewnc.sync() 23162 set_attribute(newvar, 'grid_mapping', gattr['grid']) 23163 onewnc.sync() 23164 23165 # Adding projection variable 23166 print infmsg 23167 print ' '+fname+": adding projection information of '" + gattr['grid'] + "'" 23168 newvar = onewnc.createVariable(gattr['grid'], 'i') 23169 for attrn in ppn: 23170 pvattr = pattr[attrn] 23171 set_attributek(newvar, attrn, pvattr[0], pvattr[1]) 23172 onewnc.sync() 23104 23173 23105 23174 # Global attributes
Note: See TracChangeset
for help on using the changeset viewer.