- Timestamp:
- Mar 26, 2018, 3:53:59 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/nc_var_tools.py
r1859 r1861 114 114 # Partialmap_EntiremapForExact: Function to transform from a partial global map (e.g.: only land points) to an entire one using Fortran code with exact location 115 115 # pinterp: Function to vertically interpolate using subroutines from the p_interp.F90 NCAR program 116 # projection2D_information: Function to get the basic information of the 2D-projection: new dimensions, variables, ... 116 117 # put_variable_slice: Function to add a variable from a netcdf object to a new one following a slice 117 118 # reconstruct_matrix_from_vector: Function to reconstruct a 2D matrix from a pair of syncronized vectors with the positions on x and y coordinates … … 475 476 var[i,:,:] = gen.valmodoper(varvals, values) 476 477 elif Nvars == 4: 477 478 for i in range(varshape[0]): 478 479 for j in range(varshape[1]): 479 480 varvals[:] = var[i,j,:,:] 480 481 var[i,j,:,:] = gen.valmodoper(varvals, values) 481 482 elif Nvars == 5: 482 483 for i in range(varshape[0]): 483 484 for j in range(varshape[1]): 484 485 for k in range(varshape[2]): … … 486 487 var[i,j,k,:,:] = gen.valmodoper(varvals, values) 487 488 elif Nvars == 6: 488 489 for i in range(varshape[0]): 489 490 for j in range(varshape[1]): 490 491 for k in range(varshape[2]): … … 22864 22865 #quit() 22865 22866 22867 def projection2D_information(PROJn, AXinf, projinf, dx, dy, onc): 22868 """ Function to get the basic information of the 2D-projection: new dimensions, variables, ... 22869 Reproducing: class Projection from wrfncxnj.py 22870 PROJn= name of the projection 22871 AXinf: dictionary with the informatino of the axes 'X', 'Y', 'Z', 'T' 22872 projinf: dictionary with all the information of the projection 22873 onc= netCDF object from which retrieve the information 22874 Returns: 22875 newdim: dictionary new dimensions to be added {dimn1: dimlength1, ..., dimnN: dimlengthN} 22876 newvar: dictionary new variables to be added {varn1: [stdn1, longn1, units1], ..., varnM: [stdnM, longnM, unitsM]} 22877 newvarv: dictionary values new variables to be added {varn1: [values,...], ..., varnM: [values,...]} 22878 nonCFattr: list of attributes from PROJn non-CF and non to be added to the file 22879 """ 22880 fname = 'projection2D_information' 22881 22882 availproj = ['Lambert_Conformal', 'Rotated_Pole', 'Mercator'] 22883 22884 # Explanation of certain attributes in projection file 22885 projfileexpl = {'x_resolution': 'Resolution in file alon x axis', \ 22886 'y_resolution': 'Resolution in file alon y axis', \ 22887 'proj_units': 'units of the projection (e.g.: km, m, degrees,...)', \ 22888 'fileXvals': 'variable with the reference values for the projection ' + \ 22889 'along X axis (e.g.: values for rlon)', \ 22890 'fileYvals': 'variable with the reference values for the projection ' + \ 22891 'along Y axis (e.g.: values for rlat)'} 22892 CFprojweb = 'http://cfconventions.org/Data/cf-conventions/cf-conventions-1.7/' + \ 22893 'build/ch05s06.html' 22894 22895 isrotated = False 22896 if PROJn.find('rotated') != -1 or PROJn.find('Rotated') != -1: 22897 isrotated = True 22898 projpiec = PROJn.split('_') 22899 if PROJn.find('rotated') != -1: 22900 projpiec.remove('rotated') 22901 else: 22902 projpiec.remove('Rotated') 22903 projn = '_'.join(projpiec) 22904 22905 if PROJn == "Lambert_Conformal": 22906 proj_attr = ["grid_mapping_name", "cone_type", "northern_parallel", \ 22907 "southern_parallel", "longitude_of_central_meridian", \ 22908 "latitude_of_projection_origin", 'x_resolution', 'y_resolution', \ 22909 'proj_units'] 22910 # Removing the non-CF attributes 22911 nonCFattr = ['x_resolution', 'y_resolution', 'proj_units'] 22912 for atn in proj_attr: 22913 if not projinf.has_key(atn): 22914 print errormsg 22915 print ' ' + fname + ": to process projection '" + PROJn + \ 22916 "' is required to provide argument '" + atn + "' in the " + \ 22917 "'[projectfile]' !!" 22918 if projfileexpl.has_key(atn): 22919 print ' ', atn, ':', projfileexpl[atn] 22920 else: 22921 print ' ', atn, ': CF-attribute, please visit: ', CFprojweb 22922 print ' values provided:', projinf 22923 quit(-1) 22924 newdim = {} 22925 newvar = {} 22926 newvarv = {} 22927 22928 newdim['x'] = dx 22929 newvar['x'] = ['projection_x_coordinate', 'x coordinate of projection', 'km'] 22930 Xres = np.float(projinf['x_resolution'][0]) 22931 if projinf['proj_units'][0] == 'km': 22932 scale = 1. 22933 elif projinf['proj_units'][0] == 'm': 22934 scale = 1000. 22935 else: 22936 print errormsg 22937 print ' ' + fname + ": Resolution units '" + projinf['proj_units'][0] + \ 22938 "' not ready !!" 22939 print ' available ones:', ['km', 'm'] 22940 quit(-1) 22941 newvarv['x'] = (np.arange(1,dx+1)-dx/2)*Xres/scale 22942 # updating values on axes 22943 axv = AXinf['X'] 22944 axv['dimn'] = 'x' 22945 axv['vdimn'] = 'lon' 22946 AXinf['X'] = axv 22947 22948 newdim['y'] = dy 22949 newvar['y'] = ['projection_y_coordinate', 'y coordinate of projection', 'km'] 22950 Yres = np.float(projinf['y_resolution'][0]) 22951 newvarv['y'] = (np.arange(1,dy+1)-dy/2)*Yres/scale 22952 # updating values on axes 22953 axv = AXinf['Y'] 22954 axv['dimn'] = 'y' 22955 axv['vdimn'] = 'lat' 22956 AXinf['Y'] = axv 22957 22958 elif PROJn == "Rotated_Pole": 22959 proj_attr = ["grid_mapping_name", "grid_north_pole_latitude", \ 22960 "grid_north_pole_longitude", 'fileXvals', 'fileYvals'] 22961 # Removing the non-CF attributes 22962 nonCFattr = ['fileXvals', 'fileYvals'] 22963 for atn in proj_attr: 22964 if not projinf.has_key(atn): 22965 print errormsg 22966 print ' ' + fname + ": to process projection '" + PROJn + \ 22967 "' is required to provide argument '" + atn + "' in the " + \ 22968 "'[projectfile]' !!" 22969 if projfileexpl.has_key(atn): 22970 print ' ', atn, ':', projfileexpl[atn] 22971 else: 22972 print ' ', atn, ': CF-attribute, please visit: ', CFprojweb 22973 print ' values provided:', projinf 22974 quit(-1) 22975 newdim = {} 22976 newvar = {} 22977 newvarv = {} 22978 22979 newdim['rlon'] = dx 22980 newvar['rlon'] = ['grid_longitude', 'longitude in rotated pole grid', \ 22981 'degrees'] 22982 if not gen.searchInlist(onc.variables.keys(),projinf['fileXrefvals'][0]): 22983 print errormsg 22984 print ' ' + fname + ": file does not have variable with X ref values '"+\ 22985 projinf['fileXrefvals'][0] + "' !!" 22986 print ' available ones:', onc.variables.keys() 22987 quit(-1) 22988 oXvar = onc.variables[projinf['fileXvals'][0]] 22989 if len(oXvar.shape) == 3: 22990 newvarv['rlon'] = oXvar[0,0,:] 22991 elif len(oXvar.shape) == 2: 22992 newvarv['rlon'] = oXvar[0,:] 22993 elif len(oXvar.shape) == 1: 22994 newvarv['rlon'] = oXvar[:] 22995 else: 22996 print errormsg 22997 print ' ' + fname + ": rank ", oXvar.shape, " of values for X ref " + \ 22998 "not ready !!" 22999 quit(-1) 23000 # updating values on axes 23001 axv = AXinf['X'] 23002 axv['dimn'] = 'rlon' 23003 axv['vdimn'] = 'lon' 23004 AXinf['X'] = axv 23005 23006 newdim['rlat'] = dy 23007 newvar['rlat'] = ['grid_latitude', 'latitude in rotated pole grid', 'degrees'] 23008 if not gen.searchInlist(onc.variables.keys(),projinf['fileYrefvals'][0]): 23009 print errormsg 23010 print ' ' + fname + ": file does not have variable with Y ref values '"+\ 23011 projinf['fileYrefvals'][0] + "' !!" 23012 print ' available ones:', onc.variables.keys() 23013 quit(-1) 23014 oYvar = onc.variables[projinf['fileYvals'][0]] 23015 if len(oYvar.shape) == 3: 23016 newvarv['rlat'] = oYvar[0,0,:] 23017 elif len(oYvar.shape) == 2: 23018 newvarv['rlat'] = oYvar[0,:] 23019 elif len(oYvar.shape) == 1: 23020 newvarv['rlat'] = oYvar[:] 23021 else: 23022 print errormsg 23023 print ' ' + fname + ": rank ", oYvar.shape, " of values for Y ref " + \ 23024 "not ready !!" 23025 quit(-1) 23026 # updating values on axes 23027 axv = AXinf['Y'] 23028 axv['dimn'] = 'rlat' 23029 axv['vdimn'] = 'lat' 23030 AXinf['Y'] = axv 23031 23032 elif PROJn == "Mercator": 23033 proj_attr = ["grid_mapping_name", "longitude_of_projection_origin", \ 23034 "standard_parallel", 'x_resolution', 'y_resolution', 'proj_units'] 23035 # Removing the non-CF attributes 23036 nonCFattr = ['x_resolution', 'y_resolution', 'proj_units'] 23037 for atn in proj_attr: 23038 if not projinf.has_key(atn): 23039 print errormsg 23040 print ' ' + fname + ": to process projection '" + PROJn + \ 23041 "' is required to provide argument '" + atn + "' in the " + \ 23042 "'[projectfile]' !!" 23043 if projfileexpl.has_key(atn): 23044 print ' ', atn, ':', projfileexpl[atn] 23045 else: 23046 print ' ', atn, ': CF-attribute, please visit: ', CFprojweb 23047 print ' values provided:', projinf 23048 quit(-1) 23049 23050 newdim = {} 23051 newvar = {} 23052 newvarv = {} 23053 23054 newdim['x'] = dx 23055 newvar['x'] = ['projection_x_coordinate', 'x coordinate of projection', 'km'] 23056 Xres = np.float(projinf['x_resolution'][0]) 23057 if projinf['proj_units'][0] == 'km': 23058 scale = 1. 23059 elif projinf['proj_units'][0] == 'm': 23060 scale = 1000. 23061 else: 23062 print errormsg 23063 print ' ' + fname + ": Resolution units '" + projinf['proj_units'][0] + \ 23064 "' not ready !!" 23065 print ' available ones:', ['km', 'm'] 23066 quit(-1) 23067 newvarv['x'] = (np.arange(1,dx+1)-dx/2)*Xres/scale 23068 # updating values on axes 23069 axv = AXinf['X'] 23070 axv['dimn'] = 'x' 23071 axv['vdimn'] = 'lon' 23072 AXinf['X'] = axv 23073 23074 newdim['y'] = dy 23075 newvar['y'] = ['projection_y_coordinate', 'y coordinate of projection', 'km'] 23076 Yres = np.float(projinf['y_resolution'][0]) 23077 newvarv['y'] = (np.arange(1,dy+1)-dy/2)*Yres/scale 23078 # updating values on axes 23079 axv = AXinf['Y'] 23080 axv['dimn'] = 'y' 23081 axv['vdimn'] = 'lat' 23082 AXinf['Y'] = axv 23083 23084 else: 23085 print errormsg 23086 print ' ' + fname + ": projection '" + + "' not ready !!" 23087 print ' available ones:', availproj 23088 quit(-1) 23089 23090 return newdim, newvar, newvarv, nonCFattr 23091 22866 23092 def CFmorzization(values, ncfile, variable): 22867 23093 """ Function to provide a CF-compilation version of a variable within a file … … 23190 23416 CFaxisvardimvals[axn] = CFvardimvalues 23191 23417 23192 # Renaming dimensions for 2D lon,lat variable-dimensions23418 # Getting further projection information 23193 23419 if len(filedimvals['X'].shape) == 2 or len(filedimvals['Y'].shape) == 2: 23194 print infmsg 23195 print ' because Xvals have rank 2:', filedimvals['X'].shape, \ 23196 'renaming lon,lat dimensions to x,y' 23420 newDim, newVar, newVARv, nonCFproj = projection2D_information(gattr['grid'], \ 23421 CFdimvals, pattr, filedimvals['X'].shape[1], filedimvals['X'].shape[0], onc) 23422 23423 if len(newDim.keys()) > 0: 23424 print ' Dealing with 2D lon,lat dimensions ...' 23197 23425 addCFdimvals = {} 23198 axv = CFdimvals['X'] 23199 axv['dimn'] = 'x' 23200 axv['vdimn'] = 'lon' 23201 CFdimvals['X'] = axv 23202 addCFdimvals['x'] = {'dimn': 'x', 'vdimn': 'x', 23203 'stdn': 'projection_x_coordinate','longname': 'x coordinate of projection',\ 23204 'units': pattr['proj_units'][0], 'maxrank': 1, \ 23205 'length': filedimvals['X'].shape[1]} 23206 Xres = np.float(pattr['x_resolution'][0]) 23207 filedimvals['x'] = np.arange((filedimvals['X'].shape[1]),dtype=np.float)*Xres 23208 axv = CFdimvals['Y'] 23209 axv['dimn'] = 'y' 23210 axv['vdimn'] = 'lat' 23211 CFdimvals['Y'] = axv 23212 addCFdimvals['y'] = {'dimn': 'y', 'vdimn': 'y', 23213 'stdn': 'projection_y_coordinate','longname': 'y coordinate of projection',\ 23214 'units': pattr['proj_units'][0], 'maxrank': 1, \ 23215 'length': filedimvals['X'].shape[0]} 23216 Yres = np.float(pattr['y_resolution'][0]) 23217 filedimvals['y'] = np.arange((filedimvals['X'].shape[0]),dtype=np.float)*Yres 23218 #axiv = axisinf['X'] 23219 #filecfdimn[axiv[0]] = 'x' 23220 #axiv = axisinf['Y'] 23221 #filecfdimn[axiv[0]] = 'y' 23426 for newdn in newDim.keys(): 23427 addCFdimvals[newdn] = {'dimn': newdn, 'vdimn': newdn, \ 23428 'stdn': newVar[newdn][0], 'longname': newVar[newdn][1], \ 23429 'units': newVar[newdn][2], 'maxrank': 1, \ 23430 'length': filedimvals['X'].shape[1]} 23431 filedimvals[newdn] = newVARv[newdn] 23222 23432 23223 23433 print 'Values for variables-axes from file ________' … … 23456 23666 newvar = onewnc.createVariable(gattr['grid'], 'i') 23457 23667 for attrn in ppn: 23458 pvattr = pattr[attrn] 23459 set_attributek(newvar, attrn, pvattr[0], pvattr[1]) 23460 onewnc.sync() 23668 if not gen.searchInlist(nonCFproj, attrn): 23669 pvattr = pattr[attrn] 23670 set_attributek(newvar, attrn, pvattr[0], pvattr[1]) 23671 onewnc.sync() 23461 23672 23462 23673 # Global attributes
Note: See TracChangeset
for help on using the changeset viewer.