Changeset 1499 in lmdz_wrf
- Timestamp:
- Apr 8, 2017, 9:59:57 PM (8 years ago)
- Location:
- trunk/tools
- Files:
-
- 1 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/documentation/ncmanage/ncmanage.html
r1496 r1499 11 11 <A CLASS="lc" HREF="compute_opersvarsfiles.html" TARGET="value">compute_opersvarsfiles</A><BR> 12 12 <A CLASS="lc" HREF="curve_section.html" TARGET="value">curve_section</A><BR> 13 <A CLASS="lc" HREF="DataSection_multivars.html" TARGET="value">DataSection_multivars</A><BR> 13 14 <A CLASS="lc" HREF="field_stats.html" TARGET="value">field_stats</A><BR> 14 15 <A CLASS="lc" HREF="lonlat_polygon.html" TARGET="value">lonlat_polygon</A><BR> 15 16 <A CLASS="lc" HREF="maskvar.html" TARGET="value">maskvar</A><BR> 16 17 <A CLASS="lc" HREF="setvar_asciivalues.html" TARGET="value">setvar_asciivalues</A><BR> 17 <A CLASS="lc" HREF=" varaddattr.html" TARGET="value">varaddattr</A><BR>18 <A CLASS="lc" HREF=" varaddattrk.html" TARGET="value">varaddatrk</A><BR>18 <A CLASS="lc" HREF="addvarattr.html" TARGET="value">varaddattr</A><BR> 19 <A CLASS="lc" HREF="addvarattrk.html" TARGET="value">varaddatrk</A><BR> 19 20 </DIV> 20 21 <DIV CLASS="valmenu"> -
trunk/tools/nc_var_tools.py
r1493 r1499 169 169 errormsg = 'ERROR -- error -- ERROR -- error' 170 170 warnmsg = 'WARNING -- warning -- WARNING -- warning' 171 infmsg = 'INFORMATION -- information -- INFORMATION -- information' 171 172 172 173 ## Constants … … 13806 13807 vn= variable name to include 13807 13808 sliced= dictionary with the dimension name as key and [beg, end, int] as values 13809 if beg=-9, means that values for the dimension corresponds for a series of individual values 13808 13810 """ 13809 13811 fname = 'put_variable_slice' … … 13819 13821 dnvs = [] 13820 13822 varslice = [] 13823 13824 # Dictionary with the size of the sliced dimensions 13825 newdimsizes = {} 13821 13826 # Getting variable's dimensions 13827 Nslices = 1 13822 13828 for dnv in ov.dimensions: 13823 13829 if gen.searchInlist(sliced.keys(),dnv): 13824 newdimsize = sliced[dnv][1] - sliced[dnv][0] 13825 else: 13826 newdimsize = 10 13830 if sliced[dnv][0] == -9: 13831 newdimsize = len(sliced[dnv])-1 13832 Nslices = newdimsize 13833 else: 13834 if sliced[dnv][2] != 0: 13835 newdimsize = len(range(sliced[dnv][0], sliced[dnv][1], \ 13836 sliced[dnv][2])) 13837 else: 13838 newdimsize = 0 13839 newdimsizes[dnv] = newdimsize 13840 # Creation of dimensions according to the slice 13841 if not gen.searchInlist(onc.dimensions, dnv): 13842 od = inc.dimensions[dnv] 13843 if od.isunlimited(): 13844 newdim = onc.createDimension(dnv, None) 13845 else: 13846 if newdimsize > 0: newdim = onc.createDimension(dnv, newdimsize) 13827 13847 if not gen.searchInlist(onc.dimensions,dnv): 13828 # Avoiding mon tone dimensions!13848 # Avoiding monotone dimensions! 13829 13849 if newdimsize > 0: 13830 13850 od = inc.dimensions[dnv] … … 13834 13854 dsize = len(od) 13835 13855 newdim = onc.createDimension(dnv, dsize) 13836 if gen.searchInlist(sliced.keys(),dnv): 13837 if newdimsize > 0: 13838 varslice.append(slice(sliced[dnv][0], sliced[dnv][1], sliced[dnv][2])) 13839 dnvs.append(dnv) 13840 else: 13841 varslice.append(sliced[dnv][0]) 13842 else: 13843 varslice.append(slice(0,len(inc.dimensions[dnv]))) 13844 dnvs.append(dnv) 13845 # Getting variable 13856 newdimsizes[dnv] = newdimsize 13857 13858 # Getting variable 13846 13859 varattrs = ov.ncattrs() 13847 13860 if gen.searchInlist(varattrs, '_FillValue'): … … 13851 13864 vartype = ov.dtype 13852 13865 13853 newvar = onc.createVariable(vn, vartype, tuple(dnvs), fill_value=varfil) 13854 finalvals = np.squeeze(ov[tuple(varslice)]) 13855 newvar[:] = finalvals 13866 # List of slices 13867 for idind in range(Nslices): 13868 newvarslice = [] 13869 for dnv in ov.dimensions: 13870 if gen.searchInlist(sliced.keys(),dnv): 13871 if sliced[dnv][0] != -9: 13872 if newdimsizes[dnv] > 0: 13873 varslice.append(slice(sliced[dnv][0], sliced[dnv][1], \ 13874 sliced[dnv][2])) 13875 dnvs.append(dnv) 13876 newvarslice.append(slice(sliced[dnv][0], sliced[dnv][1], \ 13877 sliced[dnv][2])) 13878 else: 13879 varslice.append(sliced[dnv][0]) 13880 else: 13881 varslice.append(sliced[dnv][idind]) 13882 newvarslice.append(idind) 13883 else: 13884 varslice.append(slice(0,len(inc.dimensions[dnv]))) 13885 dnvs.append(dnv) 13886 newvarslice.append(slice(0,len(inc.dimensions[dnv]))) 13887 13888 if idind == 0: 13889 newvar = onc.createVariable(vn, vartype, tuple(dnvs), fill_value=varfil) 13890 13891 finalvals = np.squeeze(ov[tuple(varslice)]) 13892 newvar[tuple(newvarslice)] = finalvals 13856 13893 13857 13894 for attrs in varattrs: … … 13865 13902 """ Function to get a section (values along multiple variables) of a given data-set 13866 13903 values= [varn1],[beg1],[end1],[int1]@[...[[varnM],[begM],[endM],[intM]]] 13867 [varni]: name of the variable ('WRFtime', for WRF time varibale) 13904 [varni]: name of the variable with exception of: 13905 'dist;[var1],[val1]|[...,[varN],val2]] distance computed from sqrt[sum_i([vari]-[vali])^2)] 13906 variables have to be of the same shape and share dimensions 13868 13907 [begi],[endi],[inti]: beginning, end and interval along the variable 13869 13908 [endi] = -1, maximum value 13870 [inti] = -1, all the values within the range 13909 [inti] = -1, all the values within the range (default) 13871 13910 = 0, single value. [begi] will be taken as the reference value 13872 13911 NOTE: variables without section by the variables are taken allover their size 13873 13912 NOTE2: if inti = 'str', it is assumed that the variable is a string-list of values 13874 then [begi] must be the string to be used ('!', for spaces)13913 then [begi] must be the string to be used for the search ('!', for spaces) 13875 13914 filen= netCDF with the data-set 13876 13915 varn= ',' list of variables ('all', for all variables) 13877 13916 """ 13917 # Not working 'WRFtime', for WRF time varibale 13918 13878 13919 import numpy.ma as ma 13879 13920 fname = 'DataSetSection_multivars' 13880 13921 13881 13922 # Variables not to check in the file 13882 NOcheck = ['WRFtime' ]13923 NOcheck = ['WRFtime','dist'] 13883 13924 13884 13925 if values == 'h': … … 13904 13945 fintvs = np.zeros((Nvars), dtype=np.float) 13905 13946 13947 # Dictionary with the indices with coincident values 13948 dimvals = {} 13949 13906 13950 for ivar in range(Nvars): 13907 13951 val = values.split('@')[ivar] 13908 13952 vn = val.split(',')[0] 13909 if not gen.searchInlist(nciobj.variables,vn): 13953 if not gen.searchInlist(nciobj.variables,vn) and \ 13954 not gen.searchInlist(NOcheck,vn) and vn[0:4] != 'dist': 13910 13955 print errormsg 13911 13956 print ' ' + fname + ": file '" + filen + "' does not have variable '" + \ … … 13913 13958 print ' it has:', list(nciobj.variables) 13914 13959 quit(-1) 13915 13916 ostrvar = nciobj.variables[vn] 13917 varns.append(vn) 13918 if val.split(',')[3] == 'str': 13919 strvalue = val.split(',')[1].replace('!',' ') 13960 if val[0:4] == 'dist': 13961 # Selection by distance 13962 varns.append('dist') 13963 vardist = val.split(';')[1].split('|') 13964 varsdist = {} 13965 iiv = 0 13966 distv = 0. 13967 # Computing distance and looking for the minimal distance 13968 for vdn in vardist: 13969 dvarn = vdn.split(',')[0] 13970 dvalS = vdn.split(',')[1] 13971 if not gen.searchInlist(nciobj.variables,dvarn): 13972 print errormsg 13973 print ' '+fname+": file '"+filen+"' does not have variable '" + \ 13974 dvarn + "' !!" 13975 print ' it has:', list(nciobj.variables) 13976 quit(-1) 13977 oval = nciobj.variables[dvarn] 13978 Vvals = oval[:] 13979 dval = gen.retype(dvalS,oval.dtype) 13980 13981 if iiv == 0: 13982 valsfirst = Vvals 13983 valsdims = oval.dimensions 13984 if dvalS[0:1] == '-': 13985 LdvalS = len(dvalS) 13986 distS = '('+ dvarn + '+' + dvalS[1:LdvalS+1] + ')^2' 13987 else: 13988 distS = '('+ dvarn + '-' + dvalS + ')^2' 13989 distv = (Vvals - dval)**2 13990 else: 13991 # Checking for consistency among 13992 gen.same_shape(valsfirst, Vvals) 13993 if dvalS[0:1] == '-': 13994 LdvalS = len(dvalS) 13995 distS = distS + ' + ('+ dvarn + '+' + dvalS[1:LdvalS+1] + ')^2' 13996 else: 13997 distS = distS + ' + ('+ dvarn + '-' + dvalS + ')^2' 13998 13999 distv = distv + (Vvals - dval)**2 14000 iiv = iiv + 1 14001 mindist = np.min(distv) 14002 iDistmindist = gen.multi_index_mat(distv, mindist) 14003 14004 print infmsg 14005 print ' ' + fname + ": found a distance searching as dist=sqrt(" + \ 14006 distS + ")" 14007 varsdist[dvarn] = dval 14008 print ' found:', len(iDistmindist),'values at distance:', mindist 14009 iid = 0 14010 for dimn in valsdims: 14011 dvvs = [] 14012 for ifound in range(len(iDistmindist)): 14013 foundvals = iDistmindist[ifound] 14014 if len(dvvs) == 0: dvvs = [int(foundvals[iid])] 14015 # getting indices for a given dimension including non-repeated 14016 if not gen.searchInlist(dvvs, foundvals[iid]): 14017 dvvs.append(int(foundvals[iid])) 14018 iid = iid + 1 14019 # Including only that indices coincident with previous founds 14020 if dimvals.has_key(dimn): 14021 foundvals = dimvals[dimn] 14022 coincvals = list(set(foundvals).intersection(set(dvvs))) 14023 if len(coincvals) == 0: 14024 print errormsg 14025 print ' ' + fname + ': no coincidencies among searches ' + \ 14026 "found for dimension '" + dimn + "' !!" 14027 print ' revise criteria of selection of values' 14028 quit(-1) 14029 else: 14030 dimvals[dimn] = coincvals 14031 else: 14032 dimvals[dimn] = dvvs 14033 14034 elif val.split(',')[3] == 'str': 14035 # String search 14036 ostrvar = nciobj.variables[vn] 14037 ostrvardims = ostrvar.dimensions 14038 varns.append(vn) 14039 istrvalue = val.split(',')[1].replace('!',' ') 14040 estrvalue = val.split(',')[2].replace('!',' ') 13920 14041 print warnmsg 13921 14042 print ' ' + fname + ": String value for variable '" + val + "' !!" 13922 14043 print ' assuming string variable as [Nvalues, Lstring]' 13923 print " from which only is required '" + strvalue + "'" 14044 if istrvalue != estrvalue: 14045 print " from which is required from '" + istrvalue + "' to '" + \ 14046 estrvalue + "'" 14047 else: 14048 print " from which only is required '" + istrvalue + "'" 13924 14049 strvals = get_str_nc(ostrvar, ostrvar.shape[1]) 13925 if not gen.searchInlist(strvals, strvalue):14050 if not gen.searchInlist(strvals, istrvalue): 13926 14051 print errormsg 13927 14052 print ' ' + fname + ": variable '" + val + "' does not have " + \ 13928 "value= '" + strvalue + "' !!"14053 "value= '" + istrvalue + "' !!" 13929 14054 print ' values:', strvals 13930 14055 quit(-1) 13931 indstrvalue = strvals.index(strvalue) 14056 if istrvalue != estrvalue: 14057 if not gen.searchInlist(strvals, estrvalue): 14058 print errormsg 14059 print ' ' + fname + ": variable '" + val + "' does not have " + \ 14060 "value= '" + estrvalue + "' !!" 14061 print ' values:', strvals 14062 quit(-1) 14063 14064 dimvals[ostrvardims[1]] = range(ostrvar.shape[1]) 14065 dimn = ostrvardims[0] 14066 if istrvalue == estrvalue: 14067 Nfound = strvals.count(istrvalue) 14068 print infmsg 14069 print ' '+fname+ ": variable '" + vn + "' has:", Nfound, "times " + \ 14070 "value '" + istrvalue + "'" 14071 14072 # Values found for each dimension 14073 14074 # Assuming shape as said 14075 copyvals = list(strvals) 14076 dvvs = [] 14077 for iif in range(Nfound): 14078 iidstr = copyvals.index(strvalue) 14079 dvvs.append(iidstr+iif) 14080 copyvals.pop(iidstr) 14081 else: 14082 ibeg = strvals.index(istrvalue) 14083 iend = strvals.index(estrvalue) 14084 dvvs = range(ibeg,iend) 14085 14086 # Including only that indices coincident with previous founds 14087 if dimvals.has_key(dimn): 14088 foundvals = dimvals[dimn] 14089 coincvals = list(set(foundvals).intersection(set(dvvs))) 14090 if len(coincvals) == 0: 14091 print errormsg 14092 print ' ' + fname + ': no coincidencies among searches found '+ \ 14093 "for dimension '" + dimn + "' !!" 14094 print ' revise criteria of selection of values' 14095 quit(-1) 14096 else: 14097 dimvals[dimn] = coincvals 14098 14099 indstrvalue = strvals.index(istrvalue) 14100 endstrvalue = strvals.index(estrvalue) 13932 14101 begvs[ivar] = np.float(indstrvalue) 13933 14102 endvs[ivar] = np.float(indstrvalue) 13934 14103 intvs[ivar] = -99. 13935 14104 else: 13936 begvs[ivar] = np.float(val.split(',')[1]) 13937 endvs[ivar] = np.float(val.split(',')[2]) 13938 intvs[ivar] = np.float(val.split(',')[3]) 14105 # Range search 14106 ovar = nciobj.variables[vn] 14107 vals = ovar[:] 14108 valsdims = ovar.dimensions 14109 varns.append(vn) 14110 begvs[ivar] = gen.retype(val.split(',')[1], ovar.dtype) 14111 endvs[ivar] = gen.retype(val.split(',')[2], ovar.dtype) 14112 intvs[ivar] = -1 13939 14113 fbegvs[ivar] = np.float(val.split(',')[1]) 13940 14114 fendvs[ivar] = np.float(val.split(',')[2]) 13941 14115 fintvs[ivar] = np.float(val.split(',')[3]) 14116 14117 if endvs[ivar] == -1: endvs[ivar] = np.max(vals) 14118 maskinf = ma.masked_less(vals, begvs[ivar]) 14119 masksup = ma.masked_greater(vals, endvs[ivar]) 14120 14121 #if intvs[ivar] != -1: 14122 # print errormsg 14123 # print ' ' + fname + ': non-consecutive slices not ready!!' 14124 # quit(-1) 14125 finalmask = maskinf.mask + masksup.mask 14126 print 'Lluis: found;', np.sum(~finalmask) 14127 # Indices of the found values 14128 iDistmindist = gen.multi_index_mat(finalmask, False) 14129 14130 print ' found:', len(iDistmindist),"values in variable '" + vn + \ 14131 "' as; beginning:", begvs[ivar], 'ending:', endvs[ivar], 'interval:', \ 14132 intvs[ivar] 14133 iid = 0 14134 for dimn in valsdims: 14135 dvvs = [] 14136 for ifound in range(len(iDistmindist)): 14137 foundvals = iDistmindist[ifound] 14138 # getting indices for a given dimension 14139 dvvs.append(int(foundvals[iid])) 14140 iid = iid + 1 14141 # Including only that indices coincident with previous founds 14142 if dimvals.has_key(dimn): 14143 foundvals = dimvals[dimn] 14144 coincvals = list(set(foundvals).intersection(set(dvvs))) 14145 if len(coincvals) == 0: 14146 print errormsg 14147 print ' ' + fname + ': no coincidencies among searches ' + \ 14148 "found for dimension '" + dimn + "' !!" 14149 print ' revise criteria of selection of values' 14150 quit(-1) 14151 else: 14152 dimvals[dimn] = coincvals 14153 else: 14154 dimvals[dimn] = dvvs 14155 13942 14156 if intvs[ivar] != -1: 13943 14157 slicevalS = slicevalS + varns[ivar] + ' (' + str(fbegvs[ivar]) + ',' + \ … … 13947 14161 str(fendvs[ivar]) + ',1); ' 13948 14162 14163 print infmsg 14164 print ' ' + fname + ': Indices found for each dimensions ____' 14165 finalslice = {} 14166 for dimn in dimvals.keys(): 14167 ddvals = dimvals[dimn] 14168 print ' ' + dimn + ':', ddvals 14169 if len(dimvals[dimn]) > 1: 14170 Lddvals = len(ddvals) 14171 if np.all(np.array(ddvals[1:Lddvals])-np.array(ddvals[0:Lddvals-1])) == 1: 14172 finalslice[dimn] = [ddvals[0], ddvals[Lddvals-1], 1] 14173 else: 14174 finalslice[dimn] = [-9] + ddvals 14175 else: 14176 finalslice[dimn] = [ddvals[0], ddvals[0], 0] 14177 14178 print 'Lluis finalslice:', finalslice 14179 13949 14180 ncoobj = NetCDFFile(ofile,'w') 13950 13951 14181 # Output variables 13952 14182 ## … … 13959 14189 variables = [varn] 13960 14190 13961 # Looking for limits due to the variables 13962 ## 13963 slicedims = {} 13964 for ivar in range(Nvars): 13965 print ' ' + fname + ": masking with '" + varns[ivar] + "':", begvs[ivar], \ 13966 ',', endvs[ivar], ',', intvs[ivar] 13967 if not gen.searchInlist(nciobj.variables,varns[ivar]) and not \ 13968 gen.searchInlist(NOcheck,varns[ivar]): 14191 for varn in variables: 14192 if not gen.searchInlist(nciobj.variables, varn): 13969 14193 print errormsg 13970 print ' ' + fname + ": variable '" + varns[ivar] + "' is not in ' + \ 13971 'input file!!" 14194 print ' ' + fname + ": file '" + filen + "' does not have variable '" + \ 14195 dvarn + "' !!" 14196 print ' it has:', list(nciobj.variables) 13972 14197 quit(-1) 13973 14198 13974 if varns[ivar] == 'WRFtime': 13975 ovar = nciobj.variables['Times'][:] 13976 vals = [] 13977 for it in range(ovar.shape[0]): 13978 vals.append(datetimeStr_conversion(ovar[it,:], 'WRFdatetime', \ 13979 'YmdHMS')) 13980 vals[it] = int(vals[it]) 13981 begvs[ivar] = int(begvs[ivar]) 13982 endvs[ivar] = int(endvs[ivar]) 13983 intvs[ivar] = int(intvs[ivar]) 13984 13985 vals = np.array(vals) 13986 vardims = ['Time'] 13987 else: 13988 ovar = nciobj.variables[varns[ivar]] 13989 vals = ovar[:] 13990 vardims = ovar.dimensions 13991 13992 if intvs[ivar] == -99.: 13993 ostrvar = nciobj.variables[varns[ivar]] 13994 finalmask = np.ones((ostrvar.shape), dtype= bool) 13995 finalmask[int(begvs[ivar]),:] = False 13996 elif intvs[ivar] == 0.: 13997 # Looking in the non efficient way 13998 diffvar = np.abs(vals-fbegvs[ivar]) 13999 mindiffvar = np.min(diffvar) 14000 imindiff = gen.index_mat(diffvar, mindiffvar) 14001 if np.all(imindiff == -1): 14002 print errmsg 14003 print ' ' + fname + ": no exact value '" + varns[ivar] + "'=", \ 14004 fbegvs[ivar], 'has been found!!' 14005 quit(-1) 14006 print ' ' + fname + ": '" + varns[ivar] + "'= ", fbegvs[ivar], \ 14007 'indexes:', imindiff, 'distance:', mindiffvar 14008 finalmask = np.ones(vals.shape, dtype=np.float) 14009 14010 finalmask[tuple(imindiff)] = False 14011 else: 14012 if endvs[ivar] == -1: endvs[ivar] = np.max(vals) 14013 maskinf = ma.masked_less(vals, begvs[ivar]) 14014 masksup = ma.masked_greater(vals, endvs[ivar]) 14015 14016 if intvs[ivar] != -1: 14017 print errormsg 14018 print ' ' + fname + ': non-consecutive slices not ready!!' 14019 quit(-1) 14020 finalmask = maskinf.mask + masksup.mask 14021 idn = 0 14022 14023 iidim = 0 14024 for dn in vardims: 14025 ddn = len(nciobj.dimensions[dn]) 14026 dinds = np.arange(ddn) 14027 rightvals = ~ma.array(vals, mask=finalmask).mask 14028 14029 # Checking if dimension is already cut: 14030 if slicedims.has_key(dn): 14031 slicedval = slicedims[dn] 14032 if slicedval[0] == 0 and slicedval[1] == ddn: 14033 fulldim = True 14034 else: 14035 fulldim = False 14036 else: 14037 fulldim = True 14038 14039 if not slicedims.has_key(dn) or fulldim: 14040 if intvs[ivar] == -99.: 14041 if iidim == 0: 14042 slicedims[dn]=[int(begvs[ivar]), int(begvs[ivar]), None] 14043 else: 14044 slicedims[dn]=[0, ddn, None] 14045 elif intvs[ivar] == 0.: 14046 if imindiff[iidim] == 0: 14047 slicedims[dn]=[0, ddn, None] 14048 else: 14049 slicedims[dn]=[imindiff[iidim], imindiff[iidim], None] 14050 else: 14051 slicedims[dn]=[np.min(dinds), np.max(dinds)+1, None] 14052 14053 iidim = iidim + 1 14054 14055 print ' ' + fname + ': slicing variables with______' 14056 print slicedims 14057 14058 # Creating dimensions 14059 ## 14060 print ' ' + fname + ': adding dimensions...' 14061 for nd in slicedims.keys(): 14062 objdim = nciobj.dimensions[nd] 14063 if objdim.isunlimited(): 14064 dimsize = None 14065 else: 14066 dimsize = slicedims[nd][1] - slicedims[nd][0] 14067 if dimsize > 0: newdim = ncoobj.createDimension(nd, dimsize) 14068 ncoobj.sync() 14069 14070 for var in variables: 14071 put_variable_slice(nciobj, ncoobj, var, slicedims) 14199 ovar = nciobj.variables[varn] 14200 vardims = ovar.dimensions 14201 14202 put_variable_slice(nciobj, ncoobj, varn, finalslice) 14072 14203 14073 14204 # Global attributes … … 14078 14209 14079 14210 attr = set_attribute(ncoobj, 'sliced_variables', slicevalS) 14211 add_global_PyNCplot(ncoobj, main, fname, '1.1') 14080 14212 14081 14213 nciobj.close()
Note: See TracChangeset
for help on using the changeset viewer.