Changeset 1525 in lmdz_wrf for trunk/tools/nc_var_tools.py


Ignore:
Timestamp:
Apr 13, 2017, 12:32:54 AM (8 years ago)
Author:
lfita
Message:

Fixing `maskvar' and including the option to use an already masked variable

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/nc_var_tools.py

    r1523 r1525  
    39213921    """ Function to mask a variable using another variable to mask it. Only need to share
    39223922      at least 1 dimension with the same size (no need same name)
    3923     values= [maskfilename]:[maskvarn]:[dimsmask]:[timename]
     3923    values= [maskfilename]:[maskvarn]:[dimsmask]:[maskvalue]
    39243924      [maskfilename]= file with the mask
    39253925      [maskvarn]= name of the variable with the mask
     
    39313931          * [beg]@[end] slice from [beg] to [end]
    39323932        * 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)
    39343934    filename= netCF file name
    39353935    varparameters= [varn]|[dimsmask]
     
    39443944        quit()
    39453945
    3946     expectargs = '[maskfilename]:[maskvarn]:[dimsmask]:[timename]'
     3946    expectargs = '[maskfilename]:[maskvarn]:[dimsmask]:[maskvalue]'
    39473947    gen.check_arguments(fname,values,expectargs,':')
    39483948
     
    40334033        quit(-1)
    40344034
    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
    40364042    newvar = ncfnew.createVariable(varn, varinf.dtype, varinf.dimns,                 \
    40374043      fill_value=fillValue)
     
    40404046            attr = newvar.setncattr(attrn, varobj.getncattr(attrn))
    40414047
    4042     attr = newvar.setncattr('mask', 'variable masked using ' + values.split(':')[2] +\
     4048    attr = newvar.setncattr('mask', 'variable masked using ' + values.split(':')[2]+ \
    40434049      ' of variable ' + maskvarn + ' from file ' + maskfilename)
    40444050    newvar[:] = varobj[:]
     
    40634069    # Masking following that dimensions which are not coincident with the mask
    40644070
    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
    40684086
    40694087    for ir in range(runningsize):
    40704088        islice = slices[ir]
    40714089        varvals = varobj[tuple(islice)]
    4072         maskvarvals = np.where(maskTOuse.mask, fillValue, varvals)
     4090        maskvarvals = np.where(maskTOusev, fillValue, varvals)
    40734091        newvar[tuple(islice)] = maskvarvals
    40744092
     
    41094127    filexist(newfile, ermsg, 'new')
    41104128
    4111     print 'origfile: ',origfile
    41124129    ncofile = NetCDFFile(origfile,'a')
    41134130    varinfile(ncofile, origfile, ermsg, 'change', varN)
Note: See TracChangeset for help on using the changeset viewer.