Changeset 2284 in lmdz_wrf for trunk/tools
- Timestamp:
- Jan 23, 2019, 9:59:23 PM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/nc_var_tools.py
r2281 r2284 27760 27760 dimvars0 = values.split('@')[1] 27761 27761 sliceremovedim = values.split('@')[2] 27762 slicebndsdim0 = values.split('@')[ 2]27762 slicebndsdim0 = values.split('@')[3] 27763 27763 slicestatsdim = values.split('@')[4].split(',') 27764 27764 27765 27765 varvalues = varvalues0.split(';') 27766 27766 27767 # Dimensions to remove from slicing variables 27767 27768 if sliceremovedim != 'None': 27768 27769 sliceremovedim = gen.str_list(sliceremovedim, ',') 27769 27770 else: 27770 27771 sliceremovdim = None 27772 27773 # Dimensions with boundaries from slicing variables 27771 27774 if slicebndsdim0 != 'None': 27772 27775 slicebndsdim0 = gen.str_list(slicebndsdim0, ',') … … 27818 27821 # Dictionary with the dimension-variables with bounds 27819 27822 vardimbndsvars = {} 27820 # Dictionary with the result of slicing a dimension-variable with bounds27821 vardimbndslice = {}27822 27823 # Dimension variables of 2D 27823 27824 dimvar2D = [] … … 27851 27852 ',', slicevar, ')' 27852 27853 27853 # Slices var27854 sliceshape = [Nslices] + list(ovar.shape)27855 slcvar = np.zeros(tuple(sliceshape), dtype=bool)27856 slcvalsc = np.zeros((Nslices), dtype=np.float)27857 slcvals = np.zeros((Nslices,2), dtype=np.float)27858 27859 27854 # Removing undesired dimensions from slicing variable 27860 27855 if sliceremovedim is not None: 27856 print infmsg 27857 print ' ' + fname + ": removing dimension '", sliceremovedim, \ 27858 "' from slice ..." 27861 27859 varv, rmdims, rmshape = ovar_reducedims(ovar, sliceremovedim) 27862 27860 else: … … 27864 27862 rmdims = dimnv 27865 27863 rmshape = list(ovar.shape) 27864 27865 # Slices var 27866 sliceshape = [Nslices] + list(rmshape) 27867 slcvar = np.zeros(tuple(sliceshape), dtype=bool) 27868 slcvalsc = np.zeros((Nslices), dtype=np.float) 27869 slcvals = np.zeros((Nslices,2), dtype=np.float) 27866 27870 27867 27871 for islc in range(Nslices): … … 27913 27917 onewnc.sync() 27914 27918 27915 # Looking for boundaries 27916 varbnds = [] 27917 # dn = dimvars[varn] 27918 dn = varn 27919 vdarattrs = ovar.ncattrs() 27919 # Dictionary with the result of slicing a dimension-variable with bounds 27920 vardimbndslice = {} 27921 27922 for varn in slcvarns: 27923 ovar = onc.variables[varn] 27924 vu = ovar.units 27925 dimnv = list(ovar.dimensions) 27920 27926 dv = ovar[:] 27921 27927 27922 if gen.searchInlist(vdarattrs,'bounds'): 27923 boundsv = ovar.getncattr('bounds') 27928 print 'varn:', varn, ' dims:', dimnv 27929 27930 dimbnds = [] 27931 for dn in dimnv: 27932 if slicebndsdim.has_key(dn): dimbnds.append(dn) 27933 27934 if len(dimbnds) != 0: 27924 27935 print ' ' + infmsg 27925 print ' ' +fname+ ": slicing variable '"+varn+ "' with dimension '" + \ 27926 dn + "' bounds !!" 27927 print ' bounds found:', boundsv 27936 print ' ' +fname+ ": slicing variable '"+varn+ "' with bounded " + \ 27937 " dimensions:", dimbnds, " !!" 27928 27938 print ' getting them to retrieve the slices' 27929 bndvarns = boundsv 27930 varbnds.append(dn) 27931 ovarbnds = onc.variables[boundsv] 27932 varbnds = ovarbnds[:] 27933 27934 # Slicing following boundaries, dimensions are 1D thus expand to 2D 27935 # filling as NW, NE, SE, SW 27939 27940 dimvbnds = {} 27941 dimdbnds = {} 27942 dimsbnds = {} 27943 for dn in dimbnds: 27944 if not onc.variables.has_key(slicebndsdim[dn]): 27945 print errormsg 27946 print ' ' + fname + ": file '" + ncfile + "' does not have " + \ 27947 "bounds variable '" + slicebndsdim[dn]+ "' !!" 27948 avar = list(onc.variables.keys()) 27949 avar.sort() 27950 print ' avaialable variables:', avar 27951 quit(-1) 27952 27953 ovarbnds = onc.variables[slicebndsdim[dn]] 27954 27955 # Removing undesired dimensions from slicing variable 27956 if sliceremovedim is not None: 27957 varv, rmdims, rmshape = ovar_reducedims(ovarbnds, sliceremovedim) 27958 else: 27959 varv = ovarbnds[:] 27960 rmdims = dimnv 27961 rmshape = list(ovarbnds.shape) 27962 27963 dimvbnds[dn] = varv 27964 dimvdnds[dn] = rmdims 27965 dimvsnds[dn] = rmshape 27966 27936 27967 if gen.searchInlist(slcvarns, dn) and not vardimbndslice.has_key(dn): 27937 print infmsg27938 print ' ' + fname + ": slicing dimension '" + dn + "' ... "27939 varslcv = slicesinf[dn]27940 Nslices = varslcv[0]27941 ovardims = varslcv[1]27942 slcvar = varslcv[2]27943 27944 27968 if len(dv.shape) == 1: 27969 print infmsg 27970 print ' ' + fname + ": slicing dimension '" + dn + "' ... " 27971 varslcv = slicesinf[dn] 27972 Nslices = varslcv[0] 27973 ovardims = varslcv[1] 27974 slcvar = varslcv[2] 27975 lcvalsc = varslcv[3] 27976 slcvals = varslcv[4] 27977 27978 # Slicing following boundaries, dimensions are 1D thus expand to 27979 # 2D filling as NW, NE, SE, SW 27945 27980 # Shapping 2D slices 27946 27981 reflon = np.zeros((1,Nslices), dtype=np.float) … … 27977 28012 dxget = dd 27978 28013 dyget = 1 27979 28014 27980 28015 elif len(dv.shape) == 2: 28016 xdim = rmdims[1] 28017 ydim = rmdims[0] 28018 getdims = dimvsnds[xdim] 28019 # ref values 28020 if slicesinf.has_key(getdims[1]): 28021 varslcv = slicesinf[getdims[1]] 28022 dxref = varslcv[0] 28023 reflon1D = varslcv[4] 28024 else: 28025 print infmsg 28026 print ' ' + fname + ": 2D slicing varibale '" + varn + \ 28027 "' with x-dimension '" + getdims[1] + "' without related "+\ 28028 " slicing-variable !!" 28029 quit(-1) 28030 if slicesinf.has_key(getdims[0]): 28031 varslcv = slicesinf[getdims[0]] 28032 dyref = varslcv[0] 28033 reflat1D = varslcv[4] 28034 else: 28035 print infmsg 28036 print ' ' + fname + ": 2D slicing varibale '" + varn + \ 28037 "' with y-dimension '" + getdims[0] + "' without related "+\ 28038 " slicing-variable !!" 28039 quit(-1) 28040 # Slicing following boundaries, dimensions are 1D thus expand to 28041 # 2D filling as NW, NE, SE, SW 28042 # Shapping 2D slices 28043 reflon = np.zeros((dyref,dxref), dtype=np.float) 28044 reflat = np.zeros((dyref,dxref), dtype=np.float) 28045 xslice2D = np.zeros((4,dyref,dxref), dtype=np.float) 28046 yslice2D = np.zeros((4,dyref,dxref), dtype=np.float) 28047 for j in range(dyref): 28048 for i in range(dxref): 28049 reflon[j,i] = slcvalsc 28050 reflat[j,i] = slcvalsc 28051 xslice2D[0,j,i] = reflon1D[i,0] 28052 xslice2D[1,j,i] = reflon1D[i,1] 28053 xslice2D[2,j,i] = reflon1D[i,1] 28054 xslice2D[3,j,i] = reflon1D[i,0] 28055 yslice2D[0,j,i] = reflat1D[j,1] 28056 yslice2D[1,j,i] = reflat1D[j,1] 28057 yslice2D[2,j,i] = reflat1D[j,0] 28058 yslice2D[3,j,i] = reflat1D[j,0] 28059 28060 # get values 28061 if dimvars.has_key(getdims[1]): getlon = dimvars[getdims[1]] 28062 else: 28063 print infmsg 28064 print ' ' + fname + ": 2D slicing varibale '" + varn + \ 28065 "' with x-dimension '" + getdims[1] + "' without related "+\ 28066 " dimension-variable !!" 28067 quit(-1) 28068 if dimvars.has_key(getdims[0]): getlat = dimvars[getdims[0]] 28069 else: 28070 print infmsg 28071 print ' ' + fname + ": 2D slicing varibale '" + varn + \ 28072 "' with y-dimension '" + getdims[0] + "' without related "+\ 28073 " dimension-variable !!" 28074 quit(-1) 28075 28076 getshape = dimvsnds[xdim] 28077 dxget = getshape[1] 28078 dyget = getshape[0] 28079 xdimvarbnds2D = dimvbnds[xdim] 28080 ydimvarbnds2D = dimvbnds[ydim] 28081 28082 reflon = np.zeros((1,Nslices), dtype=np.float) 28083 reflon[0,:] = slcvalsc 28084 reflat = np.zeros((1,Nslices), dtype=np.float) 28085 xslice2D = np.zeros((4,1,Nslices), dtype=np.float) 28086 xslice2D[0,0,:] = slcvals[:,1] 28087 xslice2D[1,0,:] = slcvals[:,1] 28088 xslice2D[2,0,:] = slcvals[:,0] 28089 xslice2D[3,0,:] = slcvals[:,0] 28090 yslice2D = np.zeros((4,1,Nslices), dtype=np.float) 28091 yslice2D[0,0,:] = -1. 28092 yslice2D[1,0,:] = 1. 28093 yslice2D[2,0,:] = 1. 28094 yslice2D[3,0,:] = -1. 28095 28096 dd = len(onc.dimensions[dn]) 28097 getlon = np.zeros((1,dd), dtype=np.float) 28098 getlon[0,:] = dv 28099 getlat = np.zeros((1,dd), dtype=np.float) 28100 xdimvarbnds2D = np.zeros((4,1,dd), dtype=np.float) 28101 xdimvarbnds2D[0,0,:] = ovarbnds[:,1] 28102 xdimvarbnds2D[1,0,:] = ovarbnds[:,1] 28103 xdimvarbnds2D[2,0,:] = ovarbnds[:,0] 28104 xdimvarbnds2D[3,0,:] = ovarbnds[:,0] 28105 ydimvarbnds2D = np.zeros((4,1,dd), dtype=np.float) 28106 ydimvarbnds2D[0,0,:] = -1. 28107 ydimvarbnds2D[1,0,:] = 1. 28108 ydimvarbnds2D[2,0,:] = 1. 28109 ydimvarbnds2D[3,0,:] = -1. 28110 28111 dxref = Nslices 28112 dyref = 1 28113 dxget = dd 28114 dyget = 1 28115 27981 28116 dimvar2D.append(dn) 27982 28117 continue … … 28452 28587 #compute_slices_stats_areaweighted(values, '/media/lluis/ExtDiskC_ext3/DATA/estudios/Andes/DATA/concatenated/historical/tasmin/tasmin_Amon_ACCESS1-0_historical_r1i1p1_185001-200512_Andes_19600101000000-19900101000000.nc', 'tasmin') 28453 28588 values='XLONG,-74.,-36.4,4.;XLAT,-63.,19.,4.;HGT,250.,7000.,500.@Time|WRFtime:' + \ 28454 'west_east|XLONG:south_north|XLAT@ south_north|lat_bnds,west_east|lon_bnds@Time@Time'28589 'west_east|XLONG:south_north|XLAT@Time@south_north|lat_bnds,west_east|lon_bnds@Time' 28455 28590 compute_slices_stats_areaweighted(values, '/home/lluis/PY/wrfout_d01_1995-01-01_00:00:00', 'T2') 28456 28591
Note: See TracChangeset
for help on using the changeset viewer.