Changeset 690 in lmdz_wrf for trunk/tools/nc_var_tools.py
- Timestamp:
- Feb 4, 2016, 5:48:14 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/nc_var_tools.py
r689 r690 126 126 return vals 127 127 128 def check_arguments(funcname, Nargs,args,char,expectargs):128 def check_arguments(funcname,args,expectargs,char): 129 129 """ Function to check the number of arguments if they are coincident 130 130 check_arguments(funcname,Nargs,args,char) 131 131 funcname= name of the function/program to check 132 Nargs= theoretical number of arguments133 132 args= passed arguments 133 expectargs= expected arguments 134 134 char= character used to split the arguments 135 135 """ … … 138 138 139 139 Nvals = len(args.split(char)) 140 Nargs = len(expectargs.split(char)) 141 140 142 if Nvals != Nargs: 141 143 print errormsg 142 144 print ' ' + fname + ': wrong number of arguments:',Nvals," passed to '", \ 143 145 funcname, "' which requires:",Nargs,'!!' 144 print ' given arguments:',args.split(char) 145 print ' expected arguments:',expectargs 146 print ' # given expected _______' 147 Nmax = np.max([Nvals, Nargs]) 148 for ia in range(Nmax): 149 if ia > Nvals-1: 150 aval = ' ' 151 else: 152 aval = args.split(char)[ia] 153 if ia > Nargs-1: 154 aexp = ' ' 155 else: 156 aexp = expectargs.split(char)[ia] 157 158 print ' ', ia, aval, aexp 146 159 quit(-1) 147 160 … … 7646 7659 'newlatdim]|[oldlonname,oldlatname]|[oldlondim,oldlatdim]' 7647 7660 7648 check_arguments(fname, len(expectargs.split('|')),values,'|',expectargs)7661 check_arguments(fname,values,expectargs,'|') 7649 7662 7650 7663 newprojectionfile = values.split('|')[0] … … 11215 11228 arguments = '[trajfile]@[Tbeg],[lonname],[latname],[zname],[timename],' + \ 11216 11229 '[timekind],[boxsize],[circler]]' 11217 check_arguments(fname, len(arguments.split(',')),arguments,',',8)11230 check_arguments(fname,values,arguments,',') 11218 11231 11219 11232 trajfile = values.split(',')[0].split('@')[0] … … 13680 13693 13681 13694 expectargs = '[sectionname],[kout]' 13682 check_arguments(fname, len(expectargs.split(',')),values,',',expectargs)13695 check_arguments(fname,values,expectargs,',') 13683 13696 13684 13697 secname = values.split(',')[0] … … 13855 13868 expectargs = expectargs + '[geogres]' 13856 13869 13857 check_arguments(fname, len(expectargs.split(',')),values,',',expectargs)13870 check_arguments(fname,values,expectargs,',') 13858 13871 13859 13872 namelist = values.split(',')[0] … … 13990 14003 expectargs = '[Ngrid],[dimnh1],[dimnh2],[dimvnh1],[dimvnh2]' 13991 14004 13992 check_arguments(fname, len(expectargs.split(',')),values,',',expectargs)14005 check_arguments(fname,values,expectargs,',') 13993 14006 13994 14007 Ngrid = int(values.split(',')[0]) … … 14177 14190 expectargs = '[kind],[kindn],[netcdffileref]' 14178 14191 14179 check_arguments(fname, len(expectargs.split(',')),values,',',expectargs)14192 check_arguments(fname,values,expectargs,',') 14180 14193 14181 14194 kind = values.split(',')[0] … … 14304 14317 expectargs = '[tdimn],[tvarn]' 14305 14318 14306 check_arguments(fname, len(expectargs.split(',')),values,',',expectargs)14319 check_arguments(fname,values,expectargs,',') 14307 14320 14308 14321 tdimn = values.split(',')[0] … … 14406 14419 expectargs = '[dimensions]|[varattributes]|[kind]' 14407 14420 14408 check_arguments(fname, len(expectargs.split('|')),values,'|',expectargs)14421 check_arguments(fname,values,expectargs,'|') 14409 14422 14410 14423 dimensions = values.split('|')[0].split(',') … … 14494 14507 expectargs = '[dimensions]|[varattributes]|[kind]' 14495 14508 14496 check_arguments(fname, len(expectargs.split('|')),values,'|',expectargs)14509 check_arguments(fname,values,expectargs,'|') 14497 14510 14498 14511 dimensions = values.split('|')[0].split(',') … … 14651 14664 expectargs = '[dimname]:[size]:[position]:[unlimited]' 14652 14665 14653 check_arguments(fname, len(expectargs.split(':')),values,':',expectargs)14666 check_arguments(fname,values,expectargs,':') 14654 14667 14655 14668 dimname = values.split(':')[0] … … 15462 15475 arguments = '[trajfile]@[Tbeg],[lonname],[latname],[zname],[timename],' + \ 15463 15476 '[timekind],[Nangle],[radii]]' 15464 check_arguments(fname, len(arguments.split(',')),arguments,',',8)15477 check_arguments(fname,values,arguments,',') 15465 15478 15466 15479 trajfile = values.split(',')[0].split('@')[0] … … 16290 16303 16291 16304 arguments = '[dates],[fold]' 16292 check_arguments(fname, len(values.split(',')), arguments, ',', \ 16293 len(arguments.split(','))) 16305 check_arguments(fname, values, arguments, ',') 16294 16306 16295 16307 dates = values.split(',')[0].split(':') … … 16384 16396 16385 16397 arguments = '[tdim]:[tvdim]:[seltimes]' 16386 check_arguments(fname, len(values.split(':')), arguments, ':', \ 16387 len(arguments.split(','))) 16398 check_arguments(fname, values, arguments, ':') 16388 16399 16389 16400 tdim = values.split(':')[0] … … 16785 16796 16786 16797 arguments = '[longitude]:[latitude]:[dnames]:[vdnames]' 16787 check_arguments(fname, len(values.split(':')), arguments, ':', \ 16788 len(arguments.split(','))) 16798 check_arguments(fname, values, arguments, ':') 16789 16799 16790 16800 longitude = np.float(values.split(':')[0]) … … 17289 17299 if weightk == 'varnames': 17290 17300 arguments = '[weightk],[varname],[oper],[xdimname],[ydimname],[addvars]' 17291 check_arguments(fname, len(values.split(',')), arguments, ',', \ 17292 len(arguments.split(','))) 17301 check_arguments(fname, values, arguments, ',') 17293 17302 varname = values.split(',')[1] 17294 17303 oper = values.split(',')[2] … … 17299 17308 quit(-1) 17300 17309 elif weightk == 'reglonlat': 17301 arguments = '[weightk],[lonname],[latname],[xdimname],[ydimname]' 17302 check_arguments(fname, len(values.split(',')), arguments, ',', \ 17303 len(arguments.split(','))) 17310 arguments = '[weightk],[lonname],[latname],[xdimname],[ydimname],[addvars]' 17311 check_arguments(fname, values, arguments, ',') 17304 17312 lonname = values.split(',')[1] 17305 17313 latname = values.split(',')[2] … … 17488 17496 17489 17497 ivarwgtv = np.abs(np.cos(latv*np.pi/180.))/(lonv.shape[0]*lonv.shape[1]) 17490 TOTsumwgt = np.sum(ivarwgt )17498 TOTsumwgt = np.sum(ivarwgtv) 17491 17499 if len(loopshape) == 1: 17492 17500 newvals = np.ones((loopshape[0]), dtype=np.float)*fillValueF … … 17734 17742 if project == 'lonlat': 17735 17743 Nlon = int(2. * np.pi * EarthR / dxres) + 1 17736 Nlat = int(np.pi * EarthR / d xres) + 117744 Nlat = int(np.pi * EarthR / dyres) + 1 17737 17745 17738 17746 lonProj = np.zeros((Nlon), dtype=np.float) … … 17750 17758 lonProj = [] 17751 17759 latProj = [] 17760 lonProjcirc = [] 17761 latProjcirc = [] 17762 Nlonmaxcirc = -10. 17763 17752 17764 lonProj.append(0.) 17753 17765 latProj.append(-90.) 17766 lonProjcirc.append(0.) 17767 latProjcirc.append(-90.) 17754 17768 # Differential on latitudes 17755 latarc = dyres / (np.pi * EarthR) 17769 Nlat = int(np.pi * EarthR / dyres) + 3 17770 latarc = np.pi / (Nlat - 1) 17771 17772 Nloncirc = np.zeros((Nlat), dtype=int) 17773 Nloncirc[0] = 1 17756 17774 17757 17775 lat=-np.pi/2. 17776 ilat = 1 17758 17777 while lat + latarc <= np.pi/2.: 17759 17778 lon=0. 17760 17779 lat = lat + latarc 17761 17780 lonarc = dxres / (EarthR * np.cos(lat)) 17781 lonscirc = [] 17782 latProjcirc.append(lat*RadDeg) 17762 17783 while lon <= 2.*np.pi: 17763 17784 # print lonarc, latarc, lon, lat, [lon*RadDeg, lat*RadDeg] 17764 lonProj.append(-180. + lon*RadDeg) 17785 lonProj.append(-180. + lon*RadDeg) 17765 17786 latProj.append(lat*RadDeg) 17787 lonscirc.append(-180. + lon*RadDeg) 17766 17788 lon = lon + lonarc 17767 17789 Npts = Npts + 1 17790 lonProjcirc.append(lonscirc) 17791 Nloncirc[ilat] = len(lonscirc) 17792 17793 if Nloncirc[ilat] > Nlonmaxcirc: Nlonmaxcirc = Nloncirc[ilat] 17794 ilat = ilat + 1 17795 17768 17796 lonProj.append(0.) 17769 17797 latProj.append(90.) 17798 lonProjcirc.append(0.) 17799 latProjcirc.append(90.) 17800 Nloncirc[ilat] = 1 17770 17801 Npts = Npts + 1 17771 17802 … … 17788 17819 newdim = onewnc.createDimension('Npts', Npts) 17789 17820 newdim = onewnc.createDimension('Ncoord', 2) 17821 newdim = onewnc.createDimension('xcirc', Nlonmaxcirc) 17822 newdim = onewnc.createDimension('ycirc', Nlat) 17790 17823 17791 17824 # Variables 17792 17825 newvar = onewnc.createVariable('lon', 'f8', ('x')) 17793 basicvardef(newvar, 'longitude', 'Longitude', 'degre ss_East')17826 basicvardef(newvar, 'longitude', 'Longitude', 'degrees_East') 17794 17827 newvar[:] = lonProj 17795 17828 newvar.setncattr('axis', 'X') 17796 17829 newvar.setncattr('_CoordinateAxisType', 'Lon') 17797 17830 17831 # Longitudes sorted by latitudes 17832 if len(Npoints.shape) == 1: 17833 newvar = onewnc.createVariable('Nloncirc', 'i', ('ycirc')) 17834 newvar[:] = Nloncirc 17835 basicvardef(newvar, 'Nloncirc', 'Number of longitudes by latitude circs', '-') 17836 17837 newvar = onewnc.createVariable('loncirc', 'f8', ('ycirc','xcirc')) 17838 basicvardef(newvar, 'longitude', 'Longitude by latitude circs', 'degrees_East') 17839 for iL in range(Nlat): 17840 newvar[iL,0:Nloncirc[iL]] = np.asarray(lonProjcirc[iL]) 17841 17798 17842 newvar = onewnc.createVariable('lat', 'f8', ('y')) 17799 basicvardef(newvar, 'latitude', 'Latitude', 'degre ss_North')17843 basicvardef(newvar, 'latitude', 'Latitude', 'degrees_North') 17800 17844 newvar[:] = latProj 17801 17845 newvar.setncattr('axis', 'Y') 17802 17846 newvar.setncattr('_CoordinateAxisType', 'Lat') 17847 17848 newvar = onewnc.createVariable('latcirc', 'f8', ('ycirc')) 17849 basicvardef(newvar, 'latitude', 'Latitude', 'degrees_North') 17850 newvar[:] = latProjcirc 17803 17851 17804 17852 if len(Npoints.shape) == 2: … … 17810 17858 newvar[:] = Npoints 17811 17859 newvar.setncattr('coordinates', 'lon lat') 17812 print Npoints.shape17813 17814 17860 17815 17861 # Global attributes … … 17827 17873 print fname + "Successfull written of '" + ofile + "' !!'" 17828 17874 17829 return lonProj, latProj 17830 17831 #lonlatProj(100000.,100000.,'lonlat',True) 17875 if len(Npoints.shape) == 2: 17876 return lonProj, latProj 17877 else: 17878 return lonProjcirc, latProj 17879 17880 #lonlatProj(100000.,100000.,'lonlat_dxdyFix',True) 17832 17881 #quit() 17833 17882 … … 17848 17897 """ 17849 17898 import numpy.ma as ma 17899 import subprocess as sub 17900 17850 17901 fname = 'Partialmap_Entiremap' 17851 17902 … … 17856 17907 17857 17908 ofile = 'EntireGlobalMap.nc' 17909 17910 lonlatProjfile = filen.split('.')[0] + '_lonlatProj.nc' 17858 17911 17859 17912 onc = NetCDFFile(filen, 'r') … … 17867 17920 latname = values.split(',')[1] 17868 17921 fval0 = values.split(',')[2] 17869 resol tuion = np.float(values.split(',')[3])17922 resolution = np.float(values.split(',')[3]) 17870 17923 17871 17924 if not onc.variables.has_key(lonname): … … 17892 17945 minlon = np.min(np.abs(lonvs)) 17893 17946 17947 # mindiff = np.min([minlat, minlon])/1. 17948 mindiff = np.min([minlat, minlon])*10.e6 17949 17894 17950 print ' ' + fname + ': Closest latitude to Equator:', minlat 17895 17951 print ' ' + fname + ': Closest longitude to Greenwich Meridian:', minlon 17952 print ' ' + fname + ': Minium distance for point coincidence:', mindiff 17896 17953 17897 17954 # Existing longitudes along/closest to the Equator … … 17926 17983 mindgrlat = None 17927 17984 17928 # Global sorting17929 17930 # TOTlons = lonvs.shape[0]17931 # TOTlats = lonvs.shape[0]17932 17933 # sortlons = np.sort(lonvs)17934 # sortlats = np.sort(latvs)17935 17936 # difflons = sortlons[1:TOTlons-1] - sortlons[0:TOTlons-2]17937 # difflats = sortlats[1:TOTlons-1] - sortlats[0:TOTlons-2]17938 17939 # madifflons = ma.masked_equal(difflons, 0.)17940 # madifflats = ma.masked_equal(difflats, 0.)17941 17942 # print 'Nmadifflons:',len(madifflons.compressed()),'Nmadifflats:',len(madifflats.compressed())17943 17944 # sortdifflons = np.sort(madifflons)17945 # sortdifflats = np.sort(madifflats)17946 17947 # print 'sortdifflons:',sortdifflons[0:10]17948 # print 'sortdifflats:',sortdifflats[0:10]17949 17950 17985 # Fixing in case it has not worked 17951 17986 if mindeqlon is not None and mindgrlat is None: mindgrlat = mindeqlon … … 17956 17991 quit(-1) 17957 17992 17958 if nlon < 0.: 17959 lonmap = np.arange(-180.+mindeqlon,180.,mindeqlon) 17993 if not os.path.isfile(lonlatProjfile): 17994 print lonlatProjfile 17995 quit(-1) 17996 lonmap, latmap = lonlatProj(resolution, resolution, 'lonlat_dxdyFix', True) 17997 sub.call(['mv','lonlatProj.nc',lonlatProjfile], shell=True) 17998 Ntotvals = len(lonmap) 17960 17999 else: 17961 lonmap = np.arange(0.,360.-mindeqlon,mindeqlon)17962 17963 if nlat < 0.:17964 latmap = np.arange(-90.+mindgrlat,90.,mindgrlat)17965 else:17966 latmap = np.arange(0.,180.-mindgrlat,mindgrlat)17967 17968 print ' ' + fname + '_________'17969 print ' final longitudes:',lonmap17970 print ' final latitudes:',latmap17971 17972 Nmaplon = len(lonmap)17973 Nmaplat = len(latmap) 17974 print ' ' + fname + ': desired map will be:',Nmaplon,'x',Nmaplat 17975 17976 quit()18000 oproj = NetCDFFile(lonlatProjfile, 'r') 18001 oprojlon = oproj.variables['loncirc'] 18002 oprojlat = oproj.variables['latcirc'] 18003 oNprojlon = oproj.variables['Nloncirc'] 18004 projlat = oprojlat[:] 18005 Nprojlon = oNprojlon[:] 18006 Ntotvals = len(oproj.dimensions['Npts']) 18007 olon = oproj.variables['lon'] 18008 olat = oproj.variables['lat'] 18009 lonmap = olon[:] 18010 latmap = olat[:] 18011 omapvals = oproj.variables['points'] 18012 18013 # Ntotvals = len(lonmap) 18014 Nmaplon = Ntotvals 18015 Nmaplat = Ntotvals 17977 18016 17978 18017 # Matrix map creation … … 17985 18024 fval = fill_value(vartype, fval0) 17986 18025 17987 mapval = np.ones((Nmaplat,Nmaplon), dtype=np.float)*fval 17988 17989 Ntotvals = lonvs.shape[0] 18026 # Final map creation 18027 ## 18028 newnc = NetCDFFile(ofile, 'w') 18029 18030 # dimensions 18031 newdim = newnc.createDimension('lon',Nmaplon) 18032 newdim = newnc.createDimension('lat',Nmaplat) 18033 newdim = newnc.createDimension('Npts', Ntotvals) 18034 18035 # Variables 18036 newvar = newnc.createVariable('lon','f8',('lon')) 18037 basicvardef(newvar, 'lon', 'Longitudes','degrees East') 18038 newvar[:] = lonmap 18039 newvar.setncattr('axis', 'X') 18040 newvar.setncattr('_CoordinateAxisType', 'Lon') 18041 18042 newvar = newnc.createVariable('lat','f8',('lat')) 18043 basicvardef(newvar, 'lat', 'Latitudes','degrees North') 18044 newvar[:] = latmap 18045 newvar.setncattr('axis', 'Y') 18046 newvar.setncattr('_CoordinateAxisType', 'Lat') 18047 18048 # map variable 18049 Lmapvalsshape = len(omapvals.shape) 18050 if Lmapvalsshape == 1: 18051 newvar = newnc.createVariable(varn,vartype,('Npts'), fill_value=fval) 18052 else: 18053 newvar = newnc.createVariable(varn,vartype,('lat','lon'), fill_value=fval) 18054 18055 basicvardef(newvar, varn, varlongname, varunits) 18056 newvar.setncattr('coordinates', 'lon lat') 18057 18058 print ' ' + fname + ': re-locating:',Ntotvals,'points...' 17990 18059 for iv in range(Ntotvals): 17991 if np.mod(iv*100./Ntotvals, 10.) == 0: print ' done:',iv*100./Ntotvals,'%' 17992 difflon = np.abs(lonmap - lonvs[iv]) 17993 difflat = np.abs(latmap - latvs[iv]) 17994 17995 # print np.min(difflon), ':', np.min(difflat) 17996 17997 ilon = index_mat(difflon, np.min(difflon)) 17998 ilat = index_mat(difflat, np.min(difflat)) 17999 if np.min(difflon) != 0. or np.min(difflat) !=0.: 18000 print 'No zero diffs!!:', lonvs[iv],latvs[iv],':',np.min(difflon),np.min(difflat),'i j',ilon,ilat 18001 print lonmap[ilon], latmap[ilat] 18060 # for iv in range(15): 18061 # print 'Lluis:',iv,'lon lat:',lonvs[iv],',',latvs[iv] 18062 difflat = np.abs(projlat - latvs[iv]) 18063 mindiffL = np.min(difflat) 18064 ilat = index_mat(difflat, mindiffL) 18065 # print ' Lluis mindiffL:',mindiffL,'<> ilat:',ilat,'lat_ilat:',projlat[ilat],'Nprojlon:',Nprojlon[ilat],'shape oporjlon:',oprojlon[ilat,:].shape, type(oprojlon[ilat,:]) 18066 loncirc = np.zeros((Nprojlon[ilat]), dtype=np.float) 18067 loncirc[:] = np.asarray(oprojlon[ilat,0:int(Nprojlon[ilat])].flatten()) 18068 difflon = np.abs(loncirc - lonvs[iv]) 18069 mindiffl = difflon.min() 18070 ilon = index_mat(difflon, mindiffl) 18071 # print ' difflon:',type(difflon),'shape difflon',difflon.shape,'shape loncirc:', loncirc.shape 18072 # print ' Lluis mindiffl:',mindiffl,'<> ilon:',ilon,'oprojlon:', loncirc[ilon] 18073 18074 percendone(iv,Ntotvals,0.5,'done:') 18075 if mindiffl > mindiff or mindiffL > mindiff: 18076 print errormsg 18077 print ' ' + fname + ': for point #', iv,'lon,lat in incomplet map:', \ 18078 lonvs[iv], ',', latvs[iv], 'there is not a set of lon,lat in the ' + \ 18079 'completed map closer than: ', mindiff, '!!' 18080 print ' minimum difflon:', np.min(difflon), 'difflat:', np.min(difflat) 18002 18081 quit() 18003 18082 18004 18083 if ilon > 0 and ilat > 0: 18005 mapval[ilat,ilon] = ovar[iv] 18084 # print 'Lluis shapes:',newvar.shape,'i lon/lat:',ilon,ilat 18085 if Lmapvalsshape ==1: 18086 newvar[iv] = ovar[iv] 18087 else: 18088 newvar[ilat,ilon] = ovar[iv] 18089 18090 newnc.sync() 18006 18091 else: 18007 18092 print errormsg 18008 18093 print ' ' + fname + ': point:',lonvs[iv],',',latvs[iv],' not relocated !!' 18009 18094 quit(-1) 18010 18011 # Final map creation18012 ##18013 newnc = NetCDFFile(ofile, 'w')18014 18015 # dimensions18016 newdim = newnc.createDimension('lon',Nmaplon)18017 newdim = newnc.createDimension('lat',Nmaplat)18018 18019 # Variables18020 newvar = newnc.createVariable('lon','f8',('lon'))18021 basicvardef(newvar, 'lon', 'Longitudes','degrees East')18022 nevar[:] = lonmap18023 18024 newvar = newnc.createVariable('lat','f8',('lat'))18025 basicvardef(newvar, 'lat', 'Latitudes','degrees North')18026 nevar[:] = latmap18027 18028 # map variable18029 newvar = newnc.createVariable(varn,'f4',('lat','lon'), fill_value=fval)18030 basicvardef(newvar, varn, 'Longitudes','degrees East')18031 nevar[:] = lonmap18032 18095 18033 18096 newnc.sync() … … 18052 18115 return 18053 18116 18054 #Partialmap_Entiremap('longitude,latitude,std ', '/home/lluis/etudes/DYNAMICO/ORCHIDEE/interpolation/data/carteveg5km.nc', 'vegetation_map')18117 #Partialmap_Entiremap('longitude,latitude,std,5000.', '/home/lluis/etudes/DYNAMICO/ORCHIDEE/interpolation/data/carteveg5km.nc', 'vegetation_map') 18055 18118 #quit() 18056 18119
Note: See TracChangeset
for help on using the changeset viewer.