Ejemplo n.º 1
0
 def testNCF2UAMIV(self):
     from PseudoNetCDF.camxfiles.Memmaps import uamiv
     uamivfile=uamiv(self.uamivpath)
     from PseudoNetCDF.pncgen import pncgen
     pncgen(uamivfile,self.uamivpath + '.check', inmode = 'r', outmode = 'w', format = 'uamiv', verbose = False)
     check = True
     uamivfile2=uamiv(self.uamivpath + '.check')
     for k, v in uamivfile.variables.items():
         nv = uamivfile2.variables[k]
         check = check & bool((nv[...] == v[...]).all())
     assert(check)
     os.remove(self.uamivpath+'.check')
Ejemplo n.º 2
0
def main():
    # Include functions
    from CAMxtools.write.set_attr import set_attr

    ## define user inputs here ##
    outfile = str(sys.argv[1])
    infile = str(sys.argv[2])

    # Check the first day file and read
    if not os.path.exists(infile):
        print("{} does not exist!".format(infile))
        print('Program exits from scan_timezones')
        exit()
    try:
        fin = uamiv(infile)
    except:
        try:
            fin = ncdf4.Dataset(infile)
        except:
            print("Unrecognized file type")
            print("infile = {}".format(infile))
            exit()

    # File attributes from an argument
    lfin0 = True
    fin0 = fin
    attr_in = set_attr(lfin0, fin0, {})
    loutf = True

    scan_timezones(outfile, attr_in, loutf=loutf)
Ejemplo n.º 3
0
 def testNCF2UAMIV(self):
     from PseudoNetCDF.camxfiles.Memmaps import uamiv
     uamivfile = uamiv(self.uamivpath)
     from PseudoNetCDF.pncgen import pncgen
     pncgen(uamivfile,
            self.uamivpath + '.check',
            inmode='r',
            outmode='w',
            format='uamiv',
            verbose=False)
     check = True
     uamivfile2 = uamiv(self.uamivpath + '.check')
     for k, v in uamivfile.variables.items():
         nv = uamivfile2.variables[k]
         check = check & bool((nv[...] == v[...]).all())
     assert (check)
     os.remove(self.uamivpath + '.check')
def get_mod_var_model ( infile , var , ijkdh , npts, sfactor) :
    i = ijkdh[:,0] ; j = ijkdh[:,1] ; k = ijkdh[:,2] ; h = ijkdh[:,4]
    data_var4d = uamiv ( infile ).variables[var]
    data_varpts = np.zeros(npts)-999
    for ipt in range (npts):
      if np.min(ijkdh[ipt,:]) >= 0:
        data_varpts[ipt] = data_var4d [int(h[ipt]),int(k[ipt]),int(j[ipt]),int(i[ipt])] * sfactor
    del data_var4d
    return data_varpts
Ejemplo n.º 5
0
def main():
    # Include functions to call
    import netCDF4 as ncdf4
    from PseudoNetCDF.camxfiles.Memmaps import uamiv
    from CAMxtools.write.set_attr import set_attr
    import numpy as np

    # Check LONLAT_IN environment variable
    llonlat = False
    try:
      ll_flag = os.environ['LONLAT_IN']
    except:
      ll_flag = 'F'
    if (ll_flag == 'T') or (ll_flag == 't') or (ll_flag == 'Y') or (ll_flag == 'y') :
      llonlat = True
    # Arguments
    outfile = str(sys.argv[1])
    infile = str(sys.argv[2])
    xref = str(sys.argv[3])

    # Open the input file
    try:
      fin = uamiv(infile)
      nt = len(fin.dimensions['TSTEP'])
    except:
      try:
        fin = ncdf4.Dataset(infile)
        nt = len(fin.dimensions['TSTEP'])
      except:
        print ("Unrecognized file type or file does not exist")
        exit ("infile = {}".format(infile))

    # Set attr_in, input file attributes
    lfin0 = True
    attr_fed = {}
    attr_in = set_attr(lfin0,fin,attr_fed)

    # Construct indata[ntracers,ny,nx]
    tracernames = list(fin.variables.keys())
    tracernames.remove('TFLAG')
    nspc = len(tracernames)
    ny = attr_in['NROWS']
    nx = attr_in['NCOLS']
    nz = attr_in['NLAYS']
    indata = np.zeros((nspc,nt,nz,ny,nx))
    for varname in tracernames:
      s = tracernames.index(varname)
      indata[s,:,:,:,:] = fin.variables[varname][:,:,:,:]

    # Extract indata[ntracers,nt,nz,ny,nx] at the location in xref and write to csv
    extcells(outfile, attr_in, tracernames, indata, xref, llonlat = llonlat)
Ejemplo n.º 6
0
def main():
    # Include functions to call
    import netCDF4 as ncdf4
    from PseudoNetCDF.camxfiles.Memmaps import uamiv
    from CAMxtools.write.set_attr import set_attr

    # Check TZONE environment variable
    try:
        tz = int(os.environ['TIMEZONE'])
    except:
        tz = None
    # Check OUT2UAM environment variable
    l2uam = False
    try:
        uamflag = os.environ['OUT2UAM']
    except:
        uamflag = 'F'
    if (uamflag == 'T') or (uamflag == 't') or (uamflag == 'Y') or (uamflag
                                                                    == 'y'):
        l2uam = True
    # Check MDAZ_OUTF environment variable
    try:
        tmpoutf = os.environ['MDAZ_OUTF']
    except:
        tmpoutf = None
    if not tmpoutf == None:
        print("  MDAZ_OUTF will be created as {}".format(tmpoutf))

    # Check arguments
    outfile = str(sys.argv[1])
    csvfile = str(sys.argv[2])
    avg_hr = int(sys.argv[3])
    rank = int(sys.argv[4])
    jdbeg = int(sys.argv[5])
    jdend = int(sys.argv[6])
    lyyyyjjj = False
    if str(sys.argv[7]).lower() == "true":
        lyyyyjjj = True  # CAMx file name has yyyyjjj
    comb = str(sys.argv[8])
    nifiles = int(sys.argv[9])
    infile_h = []
    infile_t = []
    # Build input file header and tail lists
    nargs2drop = 10  # No. of arguments up to "infile1 header" (inclusive)
    for ifile in range(0, nifiles):
        infile_h.append(str(sys.argv[nargs2drop + (2 * ifile)]))
        infile_t.append(str(sys.argv[nargs2drop + 1 + (2 * ifile)]))

    # Check NEW_MDA8_METHOD environment variable
    lnew_mda8 = False
    try:
        new_mda8_flag = os.environ['NEW_MDA8_METHOD']
    except:
        new_mda8_flag = 'F'
    if (new_mda8_flag == 'T') or (new_mda8_flag == 't') or (
            new_mda8_flag == 'Y') or (new_mda8_flag == 'y'):
        lnew_mda8 = True
    else:
        if avg_hr == 8:
            print(
                "  WARNING: You ARE USING OLD MDA8 METHOD!\n 24 Running 8 hour averages will be considered to calculate MDA8 O3 instead of 17 averages"
            )

    # Check the first day file and read
    jdate = str(jdbeg)
    gdate = int(
        datetime.datetime.strptime(str(jdate), "%Y%j").strftime("%Y%m%d"))
    if lyyyyjjj:
        infile0 = infile_h[0] + '.' + str(jdate) + '.' + infile_t[0]
    else:
        infile0 = infile_h[0] + '.' + str(gdate) + '.' + infile_t[0]
    if not os.path.exists(infile0):
        print("{} does not exist!".format(infile0))
        print('Program exits from hxmdaz_naaqs')
        exit()
    try:
        fin0 = uamiv(infile0)
    except:
        try:
            fin0 = ncdf4.Dataset(infile0)
        except:
            print("Unrecognized file type")
            print("infile1 = {}".format(infile0))
            exit()

    # Set file attributes
    lfin0 = True
    attr_fed = {}
    attr_in = set_attr(lfin0, fin0, attr_fed)

    # Do the main process
    hxmdaz_naaqs(jdbeg,
                 jdend,
                 outfile,
                 csvfile,
                 comb,
                 attr_in,
                 nifiles,
                 infile_h,
                 infile_t,
                 avg_hr=avg_hr,
                 rank=rank,
                 lyyyyjjj=lyyyyjjj,
                 tzone=tz,
                 l2uam=l2uam,
                 lnew_mda8=lnew_mda8,
                 tmpoutf=tmpoutf)
def main():
    # Include functions to call
    import netCDF4 as ncdf4
    from PseudoNetCDF.camxfiles.Memmaps import uamiv
    from CAMxtools.write.set_attr import set_attr

    # Check TZONE environment variable
    try:
        tz = int(os.environ['TIMEZONE'])
    except:
        tz = None
    # Check OUT2UAM environment variable
    l2uam = False
    try:
        uamflag = os.environ['OUT2UAM']
    except:
        uamflag = 'F'
    if (uamflag == 'T') or (uamflag == 't') or (uamflag == 'Y') or (uamflag
                                                                    == 'y'):
        l2uam = True
    # Check DAVG_OUTF environment variable
    try:
        tmpoutf = os.environ['DAVG_OUTF']
    except:
        tmpoutf = None
    if not tmpoutf == None:
        print("  DAVG_OUTF will be created as {}".format(tmpoutf))

    # Check arguments
    out_anndavg = str(sys.argv[1])
    out_2nddavg = str(sys.argv[2])
    csv_anndavg = str(sys.argv[3])
    csv_2nddavg = str(sys.argv[4])
    jdbeg = int(sys.argv[5])
    jdend = int(sys.argv[6])
    lyyyyjjj = False
    if str(sys.argv[7]).lower() == "true":
        lyyyyjjj = True  # CAMx file name has yyyyjjj
    comb = str(sys.argv[8])
    nifiles = int(sys.argv[9])
    infile_h = []
    infile_t = []
    # Build input file header and tail lists
    nargs2drop = 10  # No. of arguments up to "infile1 header" (inclusive)
    for ifile in range(0, nifiles):
        infile_h.append(str(sys.argv[nargs2drop + (2 * ifile)]))
        infile_t.append(str(sys.argv[nargs2drop + 1 + (2 * ifile)]))

    # Check the first day file and read
    jdate = str(jdbeg)
    gdate = int(
        datetime.datetime.strptime(str(jdate), "%Y%j").strftime("%Y%m%d"))
    if lyyyyjjj:
        infile0 = infile_h[0] + '.' + str(jdate) + '.' + infile_t[0]
    else:
        infile0 = infile_h[0] + '.' + str(gdate) + '.' + infile_t[0]
    if not os.path.exists(infile0):
        print("{} does not exist!".format(infile0))
        print('Program exits from psd_pm10_2nddavg_annavg')
        exit()
    try:
        fin0 = uamiv(infile0)
    except:
        try:
            fin0 = ncdf4.Dataset(infile0)
        except:
            print("Unrecognized file type")
            print("infile1 = {}".format(infile0))
            exit()

    # Set file attributes
    lfin0 = True
    attr_fed = {}
    attr_in = set_attr(lfin0, fin0, attr_fed)

    # Do the main process
    psd_pm10_2nddavg_annavg(jdbeg,
                            jdend,
                            out_anndavg,
                            out_2nddavg,
                            csv_anndavg,
                            csv_2nddavg,
                            comb,
                            attr_in,
                            nifiles,
                            infile_h,
                            infile_t,
                            lyyyyjjj=lyyyyjjj,
                            tzone=tz,
                            l2uam=l2uam,
                            tmpoutf=tmpoutf)
Ejemplo n.º 8
0
def combine(outfile,
            comb,
            nifiles,
            infile,
            *,
            lsurf=True,
            lverbose=False,
            loutf=True,
            l2uam=False,
            lovarunits=False):
    """
  Linearly combine multiple variables in single or multiple input files
     - Input file can be UAM or IOAPI.
     - Output file format is IOAPI.
  Arguments:
     outfile - IOAPI or UAMIV formatted output file
     comb - species definition text file which has output species name, unit, and the equation of linear combination from species in input files. Follow the same syntax of CMAQ species definition file.
     nifiles - no. of input files
     infile - python list of multiple input files
     lsurf - if True, create only surface layer regardless of no. of layers from input file.
     lverbose - if True, print more messages to screen
     loutf - if True, create an output file
     l2uam - if True, output file format is UAM. Otherwise, IOAPI.
     lovarunits - if True, return output variable units, ovarunits.
  """
    # Include functions to call
    from CAMxtools.combine._exprparse import ExpressionEvaluator
    from CAMxtools.write.wrt_ioapi import wrt_ioapi
    from CAMxtools.write.wrt_uamiv import wrt_uamiv
    from CAMxtools.write.set_attr import set_attr
    from CAMxtools._cnvt._data2fin import _data2fin

    # Handle input files
    if lverbose: print("no. of input files = {}".format(nifiles))
    fin = []
    for ifile in range(0, nifiles):
        try:
            fin.append(uamiv(infile[ifile]))
            if ifile == 0: ftype = 'uam'
        except:
            try:
                fin.append(xr.open_dataset(infile[ifile]))
                if ifile == 0: ftype = 'netcdf'
            except:
                print("Check whether your input file exists")
                print(infile[ifile])
                exit()

    # Used for variable names to recognize variables from equations
    NAME = r'(?P<NAME>[a-zA-Z_][a-zA-Z_0-9\[\]]*)'

    # Outmost block begins with opening species definition file, comb.
    with open(comb) as csvfile:
        # Initialize tracernames which will be used for 'VAR-LIST' of output file at the end of this function.
        tracernames = []
        ovarunits = []

        # Scan the species defnition file to know no. of output variables.
        lines = csv.reader(csvfile, delimiter=',', quotechar='|')
        nspc = sum(1 for row in lines
                   if not ((''.join(row).strip().replace(" ", "") == '') or
                           (row[0][0] == '/') or (row[0][0] == '#') or
                           (row[0][0] == '!')))  # no. of output variables
        csvfile.seek(0)  # rewind the species definition file

        # If an output file does not exist, get fin0 and nsteps
        lnew = False  # flag for the new output
        if loutf:
            if not os.path.exists(outfile): lnew = True
        if ftype == 'uam':
            fin0 = fin[0]
            nsteps = len(fin[0].dimensions['TSTEP'])
        else:
            fin0 = ncdf4.Dataset(infile[0])
            nsteps = fin[0].dims['TSTEP']
        lfin0 = True
        attr_in = set_attr(lfin0, fin0, {})

        # Set data2sav
        lgrdded = False
        if attr_in['FTYPE'] == 1: lgrdded = True
        ny = attr_in['NROWS']
        nx = attr_in['NCOLS']
        nz = attr_in['NLAYS']
        if lsurf:
            nz = 1
        if lgrdded:  # GRIDDED
            data2sav = np.zeros((nspc, nsteps, nz, ny, nx))
        else:  # BOUNDARY CONDITION
            ncells = 2 * (nx + ny) + 4
            data2sav = np.zeros((nspc, nsteps, nz, ncells))

        # Main loop - process the species definition file line by line
        lines = csv.reader(csvfile, delimiter=',', quotechar='|')
        for line in lines:

            # Skip unnecessary lines
            if ''.join(line).strip().replace(" ", "") == '':
                continue  # skip blank line
            if ((line[0][0] == '/') or (line[0][0] == '#')
                    or (line[0][0] == '!')):
                continue  # skip the lines starting with /, #, or blank

            # Set ovar, ovarunit, and formula. Append tracernames.
            ovar = line[0].split()[0]
            ovarunit = line[1].split()[0]
            formula = line[2].split()[0]
            tracernames.append(ovar)
            ovarunits.append(ovarunit)

            # Find vars from formula, which are either an input file or ovar from previous lines, (i.e. [0])
            p = re.compile(
                NAME
            )  # Declare pattern match that include alphabet and/or number and ends with "[ number ]"
            vars = p.findall(
                formula
            )  # Find continuous blocks of p defined above. For example, if formula = 'NO_DD[1]+NO_DD[2]+NO2_DD[1]', vars = ['NO_DD[1]', 'NO_DD[2]', 'NO2_DD[1]']

            # Loop through individual var in vars
            for var in vars:
                # Delimit by [ or ] and store the first element to varname and the second element to fins. For example, varname[0] = NO_DD and fins[0] = 1. Do this for varname[1] and fins[1], and so on.
                varname = (re.findall(r"\w+", var)[0])
                findx = (int(re.findall(r"\w+", var)[1]))

                # If the var in formula is not defined, do sanity check fins and define. For example, NO_DD_1 = fin.variables[NO_DD][0:nstamps,:,:,:]
                var_findx = "".join([varname, "_", str(findx)])
                if not var_findx in locals():
                    if findx < 0:
                        print('File index is negative! {}'.format(findx))
                        exit()
                    elif findx == 0:  # if [0] in the var, use a variable that is already calculated.
                        s = tracernames.index(varname)
                        if lgrdded:  # GRIDDED
                            exec("".join(
                                [var_findx, " = data2sav[s,:,0:nz,:,:]"]))
                        else:  # BOUNDARY CONDITION
                            exec("".join(
                                [var_findx, " = data2sav[s,:,0:nz,:]"]))
                    elif findx <= nifiles:
                        exec("".join([
                            var_findx,
                            " = fin[int(findx)-1].variables[varname][0:nsteps,0:nz,:,:]"
                        ]))
                    else:
                        print(
                            'File index is larger than no. of input files! {}'.
                            format(findx))
                        print('no. of input files {}'.format(nifiles))
                        exit()

            # Change formular to an easy expression to deal with. For exmaple, NO_DD[1]+NO_DD[2]+NO2_DD[1] to NO_DD_1+NO_DD_2+NO2_DD_1
            for i in range(0, nifiles +
                           1):  # replace [] to _. (e.g. NO_DD[1] -> NO_DD_1)
                formula = formula.replace("".join(["[", str(i), "]"]),
                                          "".join(["_", str(i)]))

            # Construct dict_vars such as {'NO_DD_1':NO_DD_1, 'NO_DD_2':NO_DD_2, 'NO2_DD_1':NO2_DD_1}
            dict_vars = {}
            vars = p.findall(formula)  # return all the variables in formula
            for var in vars:
                exec("".join(["dict_vars['", var, "']=", var]))

            # Generate class e. To declare variables in dic_vars in the ExpressionEvaluator class. dic_vars is passed as an argument.
            e = ExpressionEvaluator(dict_vars)

            # Evaluate formula
            s = tracernames.index(ovar)
            if lgrdded:  # GRIDDED
                data2sav[s, 0:nsteps, 0:nz, :, :] = e.parse(formula)
            else:  # BOUNDARY CONDITION
                data2sav[s, 0:nsteps, 0:nz, :] = e.parse(formula)

        # Del files in fin lists
        if ftype == 'netcdf':
            for ifile in range(0, nifiles):
                fin[ifile].close()
        del fin

        # Return results
        if loutf:  # if creating an output file
            fout = _data2fin(data2sav, tracernames, attr_in)
            lounit = True
            if l2uam:
                wrt_uamiv(outfile,
                          fout,
                          lsurf=lsurf,
                          lapp=not lnew,
                          ounits=ovarunits)
            else:
                wrt_ioapi(outfile,
                          fout,
                          lsurf=lsurf,
                          lapp=not lnew,
                          ounits=ovarunits)
        else:
            if lovarunits:
                return tracernames, data2sav, ovarunits
            else:
                return tracernames, data2sav
Ejemplo n.º 9
0
def main():
    # Include functions to call
    import netCDF4 as ncdf4
    from PseudoNetCDF.camxfiles.Memmaps import uamiv
    from CAMxtools.write.set_attr import set_attr

    # Check TZONE environment variable
    try :
      tz = int(os.environ['TIMEZONE'])
    except :
      tz = None
    # Check INCLUDE_BUFFER_CELLS environment variable
    lno_edges = True
    try:
      bfflag = os.environ['INCLUDE_BUFFER_CELLS']
    except:
      bfflag = 'F'
    if (bfflag.lower() == 't') or (bfflag.lower() == 'y'):
      lno_edges = False
    # Check CONVFAC environment variable
    try :
      convfac = float(os.environ['CONVFAC'])
    except :
      convfac = 1.

    # Check arguments
    outfile = str(sys.argv[1])
    jdbeg = int(sys.argv[2])
    jdend = int(sys.argv[3])
    lyyyyjjj = False
    if str(sys.argv[4]).lower() == "true" :
      lyyyyjjj = True # CAMx file name has yyyyjjj
    comb = str(sys.argv[5])
    nifiles = int(sys.argv[6])
    infile_h = []
    infile_t = []
    # Build input file header and tail lists
    nargs2drop = 7 # No. of arguments up to "infile1 header" (inclusive)
    for ifile in range(0,nifiles):
      infile_h.append(str(sys.argv[nargs2drop+(2*ifile)]))
      infile_t.append(str(sys.argv[nargs2drop+1+(2*ifile)]))

    # Check the first day file and read
    jdate  = str(jdbeg)
    gdate  = int(datetime.datetime.strptime(str(jdate),"%Y%j").strftime("%Y%m%d"))
    if lyyyyjjj:
      infile0 = infile_h[0] + '.' + str(jdate) + '.' + infile_t[0]
    else:
      infile0 = infile_h[0] + '.' + str(gdate) + '.' + infile_t[0]
    if not os.path.exists(infile0):
      print ("{} does not exist!".format(infile0))
      print ('Program exits from prep_pm_mats')
      exit()
    try:
      fin0 = uamiv(infile0)
    except:
      try:
        fin0 = ncdf4.Dataset(infile0)
      except:
        print ("Unrecognized file type")
        print ("infile1 = {}".format(infile0))
        exit()

    # Check whether buffer cell is 0. when lno_edge is off.
    spc0 = getattr(fin0,'VAR-LIST').split()[0]
    val0 = fin0.variables[spc0][0,0,0,0]
    if lno_edges:
      if val0 != 0.0:
        print("WARNING: The value at the SW corner cell is not zero while you want to exclude buffer cells.")
    else:
      assert val0 != 0.0, "The value at the SW corner cell is zero while you want to include buffer cells. Program exits."

    # Set file attributes
    lfin0 = True
    attr_fed = {}
    attr_in = set_attr(lfin0,fin0,attr_fed)

    # Do the main process
    prep_pm_mats(jdbeg,jdend,outfile,comb,attr_in,nifiles,infile_h,infile_t,lyyyyjjj=lyyyyjjj,tzone=tz,lno_edges=lno_edges,convfac=convfac)
Ejemplo n.º 10
0
def main():
    # Include functions to call
    import netCDF4 as ncdf4
    from PseudoNetCDF.camxfiles.Memmaps import uamiv
    from CAMxtools.write.set_attr import set_attr

    # Check INCLUDE_BUFFER_CELLS environment variable
    lno_edges = True
    try:
        bfflag = os.environ['INCLUDE_BUFFER_CELLS']
    except:
        bfflag = 'F'
    if (bfflag.lower() == 't') or (bfflag.lower() == 'y'):
        lno_edges = False
    # Check OUT2UAM environment variable
    l2uam = False
    try:
        uamflag = os.environ['OUT2UAM']
    except:
        uamflag = 'F'
    if (uamflag == 'T') or (uamflag == 't') or (uamflag == 'Y') or (uamflag
                                                                    == 'y'):
        l2uam = True
    # Check NEAREST environment variable
    lnearest = False
    try:
        nearflag = os.environ['NEAREST_CELL']
    except:
        nearflag = 'F'
    if (nearflag.lower() == 't') or (nearflag.lower() == 'y'):
        lnearest = True
    # Check IJFILE environment variable
    ijfile = None
    try:
        ijfile = os.environ['IJFILE']
    except:
        ijfile = None

    # Check arguments
    outfile = str(sys.argv[1])
    project = str(sys.argv[2])  # Output projection
    proj_list = ['LAMBERT', 'LATLON']
    if not project in proj_list:
        exit('Available output projections are {}.'.format(proj_list))
    utmzon = int(sys.argv[3])  # UTM zone, MUST be 0 for now.
    plon = float(sys.argv[4])  # Center/Pole lat
    plat = float(sys.argv[5])  # Center/Pole lon
    tlat1 = float(sys.argv[6])  # True lat1
    tlat2 = float(sys.argv[7])  # True lat2
    xorg = float(sys.argv[8])  # XORG
    yorg = float(sys.argv[9])  # YORG
    dxy = float(sys.argv[10])  # DX or DY
    nx = int(sys.argv[11])  # NX
    ny = int(sys.argv[12])  # NY
    comb = str(sys.argv[13])
    nifiles = int(sys.argv[14])
    infile = []
    # Build an infile list
    nargs2drop = 15  # No. of arguments up to "infile1 header" (inclusive)
    for ifile in range(0, nifiles):
        infile.append(str(sys.argv[nargs2drop + ifile]))

    # Check the first file and read
    infile0 = infile[0]
    if not os.path.exists(infile0):
        print("{} does not exist!".format(infile0))
        print('Program exits from regrid')
        exit()
    try:
        fin0 = uamiv(infile0)
        nstep = len(fin0.dimensions['TSTEP'])
    except:
        try:
            fin0 = ncdf4.Dataset(infile0)
            nstep = fin0.dims['TSTEP']
        except:
            print("Unrecognized file type")
            print("infile1 = {}".format(infile0))
            exit()

    # If NEAREST_CELL is on and INCLUDE_BUFFER_CELLS is on, check
    spc0 = getattr(fin0, 'VAR-LIST').split()[0]
    val0 = fin0.variables[spc0][0, 0, 0, 0]
    if lnearest:
        if lno_edges:
            if val0 != 0.0:
                print(
                    "WARNING: The value at the SW corner cell is not zero while you want to exclude buffer cells."
                )
        else:
            assert val0 != 0.0, "The value at the SW corner cell is zero while you want to include buffer cells. Program exits."

    # Set file attributes
    lfin0 = True
    attr_fed = {}
    attr_in = set_attr(lfin0, fin0, attr_fed)

    # Do the main process
    regrid(outfile,
           project,
           utmzon,
           plon,
           plat,
           tlat1,
           tlat2,
           xorg,
           yorg,
           dxy,
           nx,
           ny,
           comb,
           attr_in,
           nstep,
           nifiles,
           infile,
           lno_edges=lno_edges,
           l2uam=l2uam,
           lnearest=lnearest,
           ijfile=ijfile)
Ejemplo n.º 11
0
def calc_W126(outfile,
              fileh,
              filet,
              year,
              smo,
              emo,
              tz,
              *,
              lyyyyjjj=True,
              loutf=True,
              lverbose=True):
    # Include functions
    from CAMxtools.tzone.get_local_hrs import get_ozone_12hr
    from CAMxtools.write.wrt_ioapi import wrt_ioapi
    from CAMxtools.write.wrt_uamiv import wrt_uamiv
    from CAMxtools.write.set_attr import set_attr
    from CAMxtools._cnvt._data2fin import _data2fin

    # Check the first day file to figure out nx and ny
    jdate = datetime.date(year, smo, 1).strftime("%Y%j")
    gdate = year * 10000 + smo * 100 + 1
    if lyyyyjjj:
        infile = fileh + '.' + str(jdate) + '.' + filet
    else:
        infile = fileh + '.' + str(gdate) + '.' + filet
    if not os.path.exists(infile):
        print("{} does not exist!".format(infile))
        print('Program exits from calc_W126 at point 1')
        exit()
    try:
        fin = uamiv(infile)
        ftype = 'avg'
    except:
        try:
            fin = ncdf4.Dataset(infile)
            ftype = 'netcdf'
        except:
            print("Unrecognized file type")
            print("infile = {}".format(infile))
            exit()
    nx = len(fin.dimensions['COL'])
    ny = len(fin.dimensions['ROW'])

    # Loop to get monthly sums of W126
    nmo = emo - smo + 1
    monsum = np.zeros((nmo, ny, nx))
    imo = 0
    mo = smo
    print("Calculating monthly sum of W126")
    while imo < nmo:
        print("Processing month :{}".format(calendar.month_name[mo]))
        jbdate = int(datetime.date(year, mo, 1).strftime("%Y%j"))
        edd = calendar.monthrange(year, mo)[1]
        jedate = int(datetime.date(year, mo, edd).strftime("%Y%j"))
        print("jedate={}".format(str(jedate)))
        jdate = jbdate
        while (jdate <= jedate):
            jdatep1 = jdate + 1
            if lyyyyjjj:
                infile1 = fileh + '.' + str(jdate) + '.' + filet
                infile2 = fileh + '.' + str(jdatep1) + '.' + filet
            else:
                gdate = year * 10000 + mo * 100 + (jdate - jbdate + 1)
                infile1 = fileh + '.' + str(gdate) + '.' + filet
                if (jdate < jedate):
                    gdatep1 = gdate + 1
                else:
                    gdatep1 = year * 10000 + (mo + 1) * 100 + 1
                infile2 = fileh + '.' + str(gdatep1) + '.' + filet

            for infile in [infile1, infile2]:
                if not os.path.exists(infile):
                    print("{} does not exist!".format(infile))
                    print('Program exits from calc_W126 at point 2')
                    exit()

            if ftype == 'avg':
                fin1 = uamiv(infile1)
                fin2 = uamiv(infile2)
            else:
                fin1 = ncdf4.Dataset(infile1)
                fin2 = ncdf4.Dataset(infile2)

            # get ozone conc. from 8am-8pm local time
            if (lverbose):
                print("  Processing Julian day :{}".format(str(jdate)))
            o3_48_utc = np.append(fin1.variables["O3"],
                                  fin2.variables["O3"],
                                  axis=0)
            o3 = get_ozone_12hr(o3_48_utc, tz, nx, ny)

            # calculate W126
            W126 = calc_daily_W126(o3, nx, ny)

            # calculate monthly sum of W126
            for i in range(nx):
                for j in range(ny):
                    monsum[imo, j, i] += W126[j, i]

            # increase day by one
            date = datetime.datetime.strptime(str(jdate), "%Y%j")
            date += datetime.timedelta(days=1)
            jdate = int(date.strftime("%Y%j"))

        # increase month by one
        imo += 1  # increase month counter
        mo += 1  # increase the order of month in the year

    # Loop to calculate three months sum
    nmo = emo - smo - 1  # remove two from the total number of months
    mon3sum = np.zeros((nmo, ny, nx))
    imo = 0
    mo = smo
    print("Calculating three monthly sum of W126")
    while imo < nmo:
        print("Processing month :{}".format(calendar.month_name[mo]))
        for i in range(nx):
            for j in range(ny):
                mon3sum[imo, j,
                        i] = monsum[imo, j, i] + monsum[imo + 1, j,
                                                        i] + monsum[imo + 2, j,
                                                                    i]
        # increase month by one
        imo += 1  # increase month counter
        mo += 1  # increase the order of month in the year

    # Find the highest three monthly sum
    h1mon3sum = np.zeros((ny, nx))
    imo = 0
    print("Finding the highest three monthly sum of W126")
    while imo < nmo:
        for i in range(nx):
            for j in range(ny):
                if h1mon3sum[j, i] < mon3sum[imo, j, i]:
                    h1mon3sum[j, i] = mon3sum[imo, j, i]
        # increase month by one
        imo += 1  # increase month counter
        mo += 1  # increase the order of month in the year
    # write output to netcdf
    fin0 = fin  # to copy file header
    nspc = 1
    nz = 1
    nsteps = 1
    data2sav = np.zeros((nspc, nsteps, nz, ny, nx))
    data2sav[0, 0, 0, :, :] = h1mon3sum
    tracernames = "O3".split()
    beghr = 8

    #file attributes from an argument
    lfin0 = True
    attr_fed = {}
    attr_in = set_attr(lfin0, fin0, attr_fed, beghr=beghr)

    # Write output to a binary file
    fout = _data2fin(data2sav, tracernames, attr_in)
    l2uam = False  # Force to output to IOAPI format
    if loutf:
        if l2uam:
            wrt_uamiv(outfile, fout)
        else:
            wrt_ioapi(outfile, fout)
    else:
        return tracernames, data2sav
Ejemplo n.º 12
0
def diag_plane():

    lemis = False
    if str(sys.argv[1]).lower() == 'true':
        lemis = True
    outfile = str(sys.argv[2])
    met3d_a = str(sys.argv[3])
    met3d_z = str(sys.argv[4])
    file_met2d = str(sys.argv[5])
    file_plane = str(sys.argv[6])
    itzon = int(sys.argv[7])
    date8c = str(sys.argv[8])
    file_met3d = met3d_a + date8c + met3d_z

    #   Read the aircraft flight track coordinates ( lat , lon , height , julian time )
    yyyy = int(date8c[:4])
    mm = int(date8c[4:6])
    dd = int(date8c[6:8])

    lats, lons, height, jtimes = get_plane_xyht(file_plane, itzon, yyyy, mm,
                                                dd)

    npts = len(lats)
    print('no. of data points = {}'.format(npts))

    ijkdhs = np.zeros((npts, 6))

    # Get the model grid cell that includes the current point on track at the hour
    data_met3d = uamiv(file_met3d)
    data_met2d = uamiv(file_met2d)

    # Extract domain defintion from data_met3d
    nx = len(data_met3d.dimensions['COL'])
    ny = len(data_met3d.dimensions['ROW'])
    nl = len(data_met3d.dimensions['LAY'])
    dxy = float(getattr(data_met3d, 'XCELL'))  # Simply assume XCELL = YCELL
    lon0 = str(getattr(data_met3d, 'XCENT'))
    lat0 = str(getattr(data_met3d, 'YCENT'))
    lat1 = str(getattr(data_met3d, 'P_ALP'))
    lat2 = str(getattr(data_met3d, 'P_BET'))
    x0 = str(-getattr(data_met3d, 'XORIG'))
    y0 = str(-getattr(data_met3d, 'YORIG'))
    lcc = Proj('+proj=lcc +a=6370000, +b=6370000, +lon_0=' + lon0 +
               ' +lat_0=' + lat0 + ' +lat_1=' + lat1 + ' +lat_2=' + lat2 +
               ' +x_0=' + x0 + ' +y_0=' + y0)

    # Read height variables
    height0 = np.asarray(data_met3d.variables['ZGRID_M'])  #meter
    topo = np.asarray(data_met2d.variables['TOPO_M'])  #meter
    heights_mod = np.zeros(data_met3d.variables['ZGRID_M'][0, :, :, :].shape,
                           data_met3d.variables['ZGRID_M'].dtype)  #meter

    #   Loop through all the data points along flight tracks
    for i in range(npts):
        lat_plane = lats[i]
        lon_plane = lons[i]
        height_plane = height[i]
        jtime = jtimes[i]
        #print ('jtime= {}'.format(jtime))
        yyyymmddhh = jtime2yyyymmddhh(jtime)
        yyyymmdd = yyyymmddhh[0]
        hour = int(yyyymmddhh[1])

        for il in range(nl):
            heights_mod[il, :, :] = height0[hour, il, :, :] + topo[0, 0, :, :]

        ijk = get_ijk(lcc, dxy, nx, ny, heights_mod, lat_plane, lon_plane,
                      height_plane, lemis)
        ii = ijk[0]
        jj = ijk[1]
        kk = ijk[2]
        #if np.min (ijk) >0 :
        # grid index reduced by one intentionally as python starts from 0. (2015-5-18, jjung)
        #print (i , yyyymmdd , ii , jj , kk , hour , lat_plane , lon_plane , height_plane ,heights_mod[kk,jj,ii])
        ijkdhs[i, :] = ii, jj, kk, yyyymmdd, hour, int(date8c)

    np.savetxt(outfile, ijkdhs, fmt="%d")
Ejemplo n.º 13
0
#####################

# Generate list of group numbers
grps=[i+1 for i in range(groups)]

# Get indices of map
grdXs,grdYs=indexPts(readMap(mapfile,t_rows,t_cols))

# 
allCases={}
for case in cases:
	spatialAvg={}
	for grp in grps:
		filename="{0}-{1:02d}.2005{2}.ddm.grd01".format(case,grp,doi)
		filecall="{0}/{1}/GRP{2:02d}/{3}".format(inputdir,case,grp,filename)
		ddm=uamiv(filecall)
		for key in ddm.variables.keys():
			if key.startswith("{}E".format(species)):
				newKey=renameSens(key,grp)
				srcPt=getRegion(newKey)
				if getRegion(key)=='01':
					print "Skipping group {}".format(srcPt)
				else:
					sens=ddm.variables[key]
					spatialAvg[newKey]=[avgSens(sens[hr,0],grdXs,grdYs) for hr in range(24)]
	allCases[case]=spatialAvg
for akey in sorted(allCases.iterkeys()):
	output=open('{}_hrs{}-{}_DDMavg.out'.format(akey,starthour,endhour),'w')
	headers="Case,Sensitivity,"+','.join(['Hour {0:02d}'.format(hr) for hr in range(24)])+"\n"
	output.write(headers)
	for key in sorted(allCases[akey].iterkeys()):
Ejemplo n.º 14
0
def main():
    # Include functions to call
    import netCDF4 as ncdf4
    from PseudoNetCDF.camxfiles.Memmaps import uamiv
    from CAMxtools.write.set_attr import set_attr

    # CHECK USER INPUTS
    nargs2drop = 9  # No. of arguments upto "infile1 header" (inclusive)
    # Check TZONE and SURFACE_LAYER environment variables
    try:
        tz = int(os.environ['TIMEZONE'])
        if (tz < 0) or (tz > 12):
            print('CURRENT VERSION ONLY SUPPORT 0 <= time zone <= 12.')
            list_args_exit(int((len(sys.argv) - nargs2drop) / 2),
                           nargs2drop,
                           lyyyyjjj=lyyyyjjj)
    except:
        tz = None
    lsurf = False
    try:
        surflag = os.environ['SURFACE_LAYER_ONLY']
    except:
        surflag = 'F'
    if (surflag == 'T') or (surflag == 't') or (surflag == 'Y') or (surflag
                                                                    == 'y'):
        lsurf = True
    print("Output only surface? {}".format(lsurf))
    # Check arguments
    outname = str(sys.argv[1])
    outf_ddv = outname + '.ddv.csv'
    outf_cnt = outname + '.cnt.csv'
    if Path(outf_ddv).exists(): os.remove(outf_ddv)
    if Path(outf_cnt).exists(): os.remove(outf_cnt)
    # Read lyyyyjjj first as it is an argument for list_args_exit
    lyyyyjjj = False
    if str(sys.argv[7]).lower() == "true":
        lyyyyjjj = True  # CAMx file name has yyyyjjj
    comb = str(sys.argv[2])
    if not (Path(comb).exists()):
        print('COMBINE SPECIES DEFINITION FILE DOES NOT EXIST!')
        list_args_exit(int((len(sys.argv) - nargs2drop) / 2),
                       nargs2drop,
                       lyyyyjjj=lyyyyjjj)
    xref = str(sys.argv[3])
    if not (Path(xref).exists()):
        print('CROSS REFERENCE FILE DOES NOT EXIST!')
        list_args_exit(int((len(sys.argv) - nargs2drop) / 2),
                       nargs2drop,
                       lyyyyjjj=lyyyyjjj)
    class_lst = str(sys.argv[4])
    if not (Path(xref).exists()):
        print(
            'CLASS12 FILE MAP TO IMPROVE SITE FOR VISIBILITY DOES NOT EXIST!')
        list_args_exit(int((len(sys.argv) - nargs2drop) / 2),
                       nargs2drop,
                       lyyyyjjj=lyyyyjjj)
    jdbeg = int(sys.argv[5])
    try:
        datetime.datetime.strptime(str(jdbeg), "%Y%j")
    except:
        print('BEGINNING JULIAN DATE IS INVALID!')
        list_args_exit(int((len(sys.argv) - nargs2drop) / 2),
                       nargs2drop,
                       lyyyyjjj=lyyyyjjj)
    jdend = int(sys.argv[6])
    try:
        datetime.datetime.strptime(str(jdend), "%Y%j")
    except:
        print('ENDING JULIAN DATE IS INVALID!')
        list_args_exit(int((len(sys.argv) - nargs2drop) / 2),
                       nargs2drop,
                       lyyyyjjj=lyyyyjjj)
    if (jdbeg > jdend):
        print('BEGINNING JULIAN IS LARGER THAN ENDING!')
        list_args_exit(int((len(sys.argv) - nargs2drop) / 2),
                       nargs2drop,
                       lyyyyjjj=lyyyyjjj)
    nifiles = int(sys.argv[8])  # no. of input files
    infile_h = []
    infile_t = []
    if nifiles < (len(sys.argv) - nargs2drop) / 2:
        print(
            'NO. OF INPUT FILES IS SET LESS THAN NO. OF INPUT FILE ARGUMENTS PROVIDED!'
        )
        list_args_exit(int((len(sys.argv) - nargs2drop) / 2),
                       nargs2drop,
                       lyyyyjjj=lyyyyjjj)
    if nifiles > (len(sys.argv) - nargs2drop) / 2:
        print(
            'NO. OF INPUT FILES IS SET LARGER THAN NO. OF INPUT FILE ARGUMENTS PROVIDED!'
        )
        list_args_exit(int((len(sys.argv) - nargs2drop) / 2),
                       nargs2drop,
                       lyyyyjjj=lyyyyjjj)

    # Build input file header and tail lists
    for ifile in range(0, nifiles):
        infile_h.append(str(sys.argv[nargs2drop + (2 * ifile)]))
        infile_t.append(str(sys.argv[nargs2drop + 1 + (2 * ifile)]))

    # Check the first day file and read
    jdate = str(jdbeg)
    gdate = int(
        datetime.datetime.strptime(str(jdate), "%Y%j").strftime("%Y%m%d"))
    if lyyyyjjj:
        infile0 = infile_h[0] + '.' + str(jdate) + '.' + infile_t[0]
    else:
        infile0 = infile_h[0] + '.' + str(gdate) + '.' + infile_t[0]
    if not os.path.exists(infile0):
        print("{} does not exist!".format(infile0))
        print('Program exits from run_calc_W126')
        exit()
    try:
        fin0 = uamiv(infile0)
    except:
        try:
            fin0 = ncdf4.Dataset(infile0)
        except:
            print("Unrecognized file type")
            print("infile1 = {}".format(infile0))
            exit()

    # File attributes from an argument
    lfin0 = True
    attr_fed = {}
    attr_in = set_attr(lfin0, fin0, attr_fed)

    # Get annual visibility analysis results at Class I and II areas
    get_vis_at_cells(jdbeg,
                     jdend,
                     outf_ddv,
                     outf_cnt,
                     xref,
                     comb,
                     nifiles,
                     infile_h,
                     infile_t,
                     class_lst,
                     lyyyyjjj=lyyyyjjj,
                     tzone=tz,
                     attr_in=attr_in)
Ejemplo n.º 15
0
def main():
    # Include functions to call
    import netCDF4 as ncdf4
    from PseudoNetCDF.camxfiles.Memmaps import uamiv
    from CAMxtools.write.set_attr import set_attr
    from CAMxtools.tzone.scan_timezones import scan_timezones
    import datetime

    # CHECK USER INPUTS
    # Check TZONE environment variables
    try:
        tzone = int(os.environ['TIMEZONE'])
    except:
        tzone = None
    # Check TZONE environment variables
    try:
        tzfile = str(os.environ['TZFILE'])
    except:
        tzfile = None
    # If a user specified a time zone, do not use tzfile.
    if not tzone == None:
        tzfile = None  # If a user specified a time zone, do not use tzfile.
    # Arguments
    outfile = str(sys.argv[1])
    infile1 = str(sys.argv[2])
    infile2 = str(sys.argv[3])
    xref = str(sys.argv[4])

    # Begin the script
    try:
        fin1 = uamiv(infile1)
        fin2 = uamiv(infile2)
    except:
        try:
            fin1 = ncdf4.Dataset(infile1)
            fin2 = ncdf4.Dataset(infile2)
        except:
            print("Unrecognized file type")
            print("infile1 = {}".format(infile1))
            print("infile2 = {}".format(infile2))
            exit()
    tracernames = list(fin1.variables.keys())
    tracernames.remove('TFLAG')

    nx = len(fin1.dimensions['COL'])
    ny = len(fin1.dimensions['ROW'])
    nx2 = len(fin2.dimensions['COL'])
    ny2 = len(fin2.dimensions['ROW'])
    if (nx != nx2) or (ny != ny2):
        print("infile1 and infile2 have different no. of columns or rows")
        print("infile1 x = {}, y = {}".format(nx, ny))
        print("infile2 x = {}, y = {}".format(nx2, ny2))
        exit()

    # Prepare tzone 2d data array
    lfin0 = True
    attr_fed = {}
    attr_in = set_attr(lfin0, fin1, attr_fed)
    l1tzone = False
    if tzone != None:
        l1tzone = True  # If users specify a specific time zone, vis is calculated based on the time zone.
    if not tzfile == None:
        print("Reading the tzfile")
        print("tzfile = {}".format(tzfile))
        ftz = ncdf4.Dataset(tzfile)
        tzone_ji = ftz.variables["TZONE"][0, 0, :, :].astype(np.int)
    else:
        # Scan time zone over the domain
        tzfile = None
        dum, tzone_stlji = scan_timezones(tzfile, attr_in, loutf=False)
        tzone_ji = tzone_stlji[0, 0, 0, :, :].astype(np.int)
        tzones = np.unique(tzone_ji)
        if l1tzone:
            print("A SINGLE TIMEZONE, {} will be applied".format(tzone))
            if tzone in tzones:
                ny = attr_in['NROWS']
                nx = attr_in['NCOLS']
                tzone_ji = np.zeros((ny, nx)).astype(np.int) + tzone
                tzones = [tzone]
            else:
                exit("YOUR TIMEZONE SPECIFIED IS OUT OF DOMAIN")

    # Set indata
    ntracers = len(tracernames)
    nt = len(fin1.dimensions['TSTEP'])
    nz = attr_in['NLAYS']
    indata1 = np.zeros((ntracers, nt, nz, ny, nx))
    indata2 = np.zeros((ntracers, nt, nz, ny, nx))
    for varname in tracernames:
        s = tracernames.index(varname)
        indata1[s, :, :, :, :] = fin1.variables[varname][:, :, :, :]
        indata2[s, :, :, :, :] = fin2.variables[varname][:, :, :, :]
    indata = np.append(indata1, indata2, axis=1)

    # Main process
    yyyyjjj = getattr(fin1, 'SDATE')
    get_davg_at_cells(tracernames,
                      indata,
                      outfile,
                      xref,
                      yyyyjjj,
                      tzone=tzone_ji)
Ejemplo n.º 16
0
##	Create dictionary for array that links all sensitivites
##		to their given lat/lon

from PseudoNetCDF.camxfiles.Memmaps import uamiv
from collections import defaultdict
import AnDDM
import numpy as np
import sys

def importDDM(ddmFile,species,grp)
# importDDM will import the given DDM average file using PseudoNetCDF
# and assign the species values to a temporary group array
#	ddmFile: location of the DDM grid file
#	species: the number of the species in question (eg. 05 for ozone)
#	grp: group number
	ddm = uamiv(ddmFile)
	varList=[]
	for sens in ddm.variables.keys():	# Create a list of desired sensitivities
		if sens.startswith(grp+"E"):
			varList.append(sens)
	#TODO: Pull appropriate species sensitivites
	#TODO: Create group array
	#TODO: Add sensitivities to group array

def linkLocations(grpArray,locationFile,grp)
# linkLocations will assign X/Y locations to entries in the given temporary
# group array from the given location file.
#	grpArray: Array of sensitivities generated by importDDM
#	locationFile: location of the text file containing the X/Y coordinates
#		corresponding to each sensitivity region per group. Based on 
#		the outputs from kcellPrep (pykcellout files)