Changeset 1525 in lmdz_wrf for trunk/tools/nc_var_tools.py
- Timestamp:
- Apr 13, 2017, 12:32:54 AM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/nc_var_tools.py
r1523 r1525 3921 3921 """ Function to mask a variable using another variable to mask it. Only need to share 3922 3922 at least 1 dimension with the same size (no need same name) 3923 values= [maskfilename]:[maskvarn]:[dimsmask]:[ timename]3923 values= [maskfilename]:[maskvarn]:[dimsmask]:[maskvalue] 3924 3924 [maskfilename]= file with the mask 3925 3925 [maskvarn]= name of the variable with the mask … … 3931 3931 * [beg]@[end] slice from [beg] to [end] 3932 3932 * NOTE: no value provided is for all dimension range 3933 [maskvalue]= value to use for maskking 3933 [maskvalue]= value to use for maskking ('mask' value for using an already masked variable) 3934 3934 filename= netCF file name 3935 3935 varparameters= [varn]|[dimsmask] … … 3944 3944 quit() 3945 3945 3946 expectargs = '[maskfilename]:[maskvarn]:[dimsmask]:[ timename]'3946 expectargs = '[maskfilename]:[maskvarn]:[dimsmask]:[maskvalue]' 3947 3947 gen.check_arguments(fname,values,expectargs,':') 3948 3948 … … 4033 4033 quit(-1) 4034 4034 4035 maskval = gen.retype(maskvalue,varinf.dtype) 4035 if maskvalue != 'mask': 4036 maskval = gen.retype(maskvalue,varinf.dtype) 4037 else: 4038 print infmsg 4039 print ' ' + fname + ': maskgin with already masked variable' 4040 varmaskv = maskvalues.mask 4041 4036 4042 newvar = ncfnew.createVariable(varn, varinf.dtype, varinf.dimns, \ 4037 4043 fill_value=fillValue) … … 4040 4046 attr = newvar.setncattr(attrn, varobj.getncattr(attrn)) 4041 4047 4042 attr = newvar.setncattr('mask', 'variable masked using ' + values.split(':')[2] +\4048 attr = newvar.setncattr('mask', 'variable masked using ' + values.split(':')[2]+ \ 4043 4049 ' of variable ' + maskvarn + ' from file ' + maskfilename) 4044 4050 newvar[:] = varobj[:] … … 4063 4069 # Masking following that dimensions which are not coincident with the mask 4064 4070 4065 slices = gen.provide_slices(varinf.dimns, varinf.dims, list(varmaskinf.dimns)) 4066 4067 maskTOuse = ma.masked_equal(maskvalues, maskval) 4071 # Check if there are some coincident dimensional names between both variables 4072 vdimns = varinf.dimns 4073 mdimns = maskdims 4074 4075 for dimn in vdimns: 4076 if gen.searchInlist(maskdims,dimn): mdimns.remove(dimn) 4077 4078 #slices = gen.provide_slices(varinf.dimns, varinf.dims, list(varmaskinf.dimns)) 4079 slices = gen.provide_slices(varinf.dimns, varinf.dims, list(maskdims)) 4080 4081 if maskvalue != 'mask': 4082 maskTOuse = ma.masked_equal(maskvalues, maskval) 4083 maskTOusev = maskTOuse.mask 4084 else: 4085 maskTOusev = maskvalues.mask 4068 4086 4069 4087 for ir in range(runningsize): 4070 4088 islice = slices[ir] 4071 4089 varvals = varobj[tuple(islice)] 4072 maskvarvals = np.where(maskTOuse .mask, fillValue, varvals)4090 maskvarvals = np.where(maskTOusev, fillValue, varvals) 4073 4091 newvar[tuple(islice)] = maskvarvals 4074 4092 … … 4109 4127 filexist(newfile, ermsg, 'new') 4110 4128 4111 print 'origfile: ',origfile4112 4129 ncofile = NetCDFFile(origfile,'a') 4113 4130 varinfile(ncofile, origfile, ermsg, 'change', varN)
Note: See TracChangeset
for help on using the changeset viewer.