Exemplo n.º 1
0
def prep_pm_mats(jdbeg,jdend,outfile,comb,attr_in,nifiles,infile_h,infile_t,*,lyyyyjjj=True,tzone=None,lno_edges=True,convfac=1.):
    # Include functions
    from CAMxtools.combine.combine import combine
    import netCDF4 as ncdf4
    from PseudoNetCDF.camxfiles.Memmaps import uamiv
    from CAMxtools.write.set_attr import set_attr
    from CAMxtools.tzone.scan_timezones import get_lcc
    from CAMxtools.regrid.projection import ll2latlon
    from CAMxtools.regrid.projection import lcp2latlon
    from CAMxtools.tzone.scan_timezones import scan_timezones
    from CAMxtools.tzone.get_local_hrs import get_davg
    from CAMxtools.MATS.wrt_csv_for_pm_mats import wrt_csv_for_pm_mats
    import numpy as np

    # Check arguments
    l1tzone = False
    if tzone != None: l1tzone = True # If users specify a specific time zone, MATS MDA8 O3 is calculated based on the time zone.

    # 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")

    # Get attribute
    dxy = attr_in['XCELL']
    nx  = attr_in['NCOLS']
    ny  = attr_in['NROWS']
    x0 = attr_in['XORIG']
    y0 = attr_in['YORIG']

    # Get lat and lon at the center of grid cells
    if attr_in['GDTYP'] == 1:
      lats, lons = ll2latlon (x0, y0, dxy, nx, ny, lno_edges = lno_edges)
    elif attr_in['GDTYP'] == 2:
      lcc = get_lcc(attr_in)
      lats, lons = lcp2latlon (lcc, dxy, nx, ny, lno_edges = lno_edges)
    else:
      print("Your GDTYP is {}".format(attr_in['GDTYP']))
      exit("This program currently supports LATLON (GDTYP = 1) and LCP (GDTYP = 2) only.")

    # Daily loop
    jdays = []
    jdate = jdbeg
    while (jdate <= jdend):
      jdays.append(jdate)
      print ("Processing {}".format(jdate))
      gdate = int(datetime.datetime.strptime(str(jdate),"%Y%j").strftime("%Y%m%d"))
      infile = []
      for ifile in range(0,nifiles):
        if lyyyyjjj:
          infile.append(infile_h[ifile]+'.'+str(jdate)+'.'+infile_t[ifile])
        else:
          infile.append(infile_h[ifile]+'.'+str(gdate)+'.'+infile_t[ifile])
      print ("  1. PROCESSING COMBINE")
      tracernames, indata2 = combine(None,comb,nifiles,infile,lverbose=False,loutf=False)
      if (jdate > jdbeg):
        print ("  2. PROCESSING DAILY AVERAGE FOR PREVIOUS DAY")
        concs_48_utc = np.append(indata1[:,:,0,:,:],indata2[:,:,0,:,:],axis=1)
        davg_1day = get_davg(concs_48_utc, tzone_ji, nx, ny)
        if jdate == jdbeg + 1: davgs = np.array([davg_1day]) #davgs[nd,nspc,ny,nx]
        if jdate > jdbeg + 1: davgs = np.append(davgs,np.array([davg_1day]),axis=0)
      indata1 = indata2
      jdate = int((datetime.datetime.strptime(str(jdate),"%Y%j") + datetime.timedelta(days=1)).strftime("%Y%j"))
    del indata2
    del indata1
    
    # Exclude buffer cells if needed
    if lno_edges:
      davgs_chked  = davgs[:,:,1:-1,1:-1]
    else:
      davgs_chked  = davgs

    # Write csv file
    wrt_csv_for_pm_mats(outfile,davgs_chked,jdays,lats,lons,tracernames,convfac)
def psd_pm10_2nddavg_annavg(jdbeg,
                            jdend,
                            out_anndavg,
                            out_2nddavg,
                            csv_anndavg,
                            csv_2nddavg,
                            comb,
                            attr_in,
                            nifiles,
                            infile_h,
                            infile_t,
                            *,
                            lyyyyjjj=True,
                            tzone=None,
                            l2uam=False,
                            tmpoutf=None):
    # Include functions
    from CAMxtools.combine.combine import combine
    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
    from CAMxtools.tzone.get_local_hrs import get_davg
    from CAMxtools._cnvt._data2fin import _data2fin
    from CAMxtools.write.wrt_ioapi import wrt_ioapi
    from CAMxtools.write.wrt_uamiv import wrt_uamiv
    from CAMxtools.psd.wrt_csv_for_psd import wrt_csv_for_psd
    import numpy as np

    # Check arguments
    l1tzone = False
    if tzone != None:
        l1tzone = True  # If users specify a specific time zone, MATS MDA8 O3 is calculated based on the time zone.

    # 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")

    # Get attribute
    nx = attr_in['NCOLS']
    ny = attr_in['NROWS']

    # Daily loop
    jdate = jdbeg
    while (jdate <= jdend):
        print("Processing {}".format(jdate))
        gdate = int(
            datetime.datetime.strptime(str(jdate), "%Y%j").strftime("%Y%m%d"))
        infile = []
        for ifile in range(0, nifiles):
            if lyyyyjjj:
                infile.append(infile_h[ifile] + '.' + str(jdate) + '.' +
                              infile_t[ifile])
            else:
                infile.append(infile_h[ifile] + '.' + str(gdate) + '.' +
                              infile_t[ifile])
        print("  1. PROCESSING COMBINE")
        tracernames, indata2, ovarunits = combine(None,
                                                  comb,
                                                  nifiles,
                                                  infile,
                                                  lverbose=False,
                                                  loutf=False,
                                                  lovarunits=True)
        if (jdate > jdbeg):
            print("  2. PROCESSING DAILY AVERAGE FOR PREVIOUS DAY")
            concs_48_utc = np.append(indata1[:, :, 0, :, :],
                                     indata2[:, :, 0, :, :],
                                     axis=1)
            davg_1day = get_davg(concs_48_utc, tzone_ji, nx, ny)
            if jdate == jdbeg + 1:
                davgs = np.array([davg_1day])  #davgs[nd,nspc,ny,nx]
            if jdate > jdbeg + 1:
                davgs = np.append(davgs, np.array([davg_1day]), axis=0)
        indata1 = indata2
        jdate = int((datetime.datetime.strptime(str(jdate), "%Y%j") +
                     datetime.timedelta(days=1)).strftime("%Y%j"))
    del indata2
    del indata1

    # Prepare DAVG if asked
    nspc = len(tracernames)
    lout_davg = False
    if not tmpoutf == None: lout_davg = True
    if lout_davg:
        nd = jdend - jdbeg
        data2sav = np.zeros((nspc, nd, 1, ny, nx))
        for ispc in range(nspc):
            data2sav[ispc, :, 0, :, :] = davgs[:, ispc, :, :]
        # Write a binary file for DAVG
        attr_in['TSTEP'] = 240000
        fout = _data2fin(data2sav, tracernames, attr_in)
        if l2uam:
            wrt_uamiv(tmpoutf, fout, lsurf=True, ounits=ovarunits)
        else:
            wrt_ioapi(tmpoutf, fout, lsurf=True, ounits=ovarunits)

    # Calculate 2nd highest daily average
    print("  3. PROCESSING 2ND HIGHEST DAILY AVERAGE")
    data2sav = np.zeros((nspc, 1, 1, ny, nx))
    rank = 2
    #data2sav[:,0,0,:,:] = np.sort(davgs,axis=0)[davgs.shape[0]-rank,...] #data2sav[nspc,nt,nz,ny,nx], davgs[nd,nspc,ny,nx]
    ind = np.argsort(davgs, axis=0)[
        davgs.shape[0] - rank,
        ...]  #data2sav[nspc,nt,nz,ny,nx], davgs[nd,nspc,ny,nx], ind[nsp,ny,nx]
    gr = np.ogrid[0:davgs.shape[0], 0:davgs.shape[1], 0:davgs.shape[2],
                  0:davgs.shape[3]]
    gr[0] = ind
    data2sav[:, 0, 0, :, :] = davgs[gr]
    #data2sav[:,0,0,:,:] = np.mean(davgs,axis=0) #data2sav[nspc,nt,nz,ny,nx], davgs[nd,nspc,ny,nx]
    # Write a csv file
    jdays = attr_in['SDATE'] + ind[
        0, :, :]  # Annual average, set the beginning date
    hours = np.zeros((ny, nx))  # daily average, set zeros
    data2csv = np.zeros((nspc, ny, nx))
    data2csv = data2sav[:, 0, 0, :, :]
    wrt_csv_for_psd(csv_2nddavg, tracernames, data2csv, jdays, hours)
    # Write a binary file
    fout = _data2fin(data2sav, tracernames, attr_in)
    if l2uam:
        wrt_uamiv(out_2nddavg, fout, lsurf=True, ounits=ovarunits)
    else:
        wrt_ioapi(out_2nddavg, fout, lsurf=True, ounits=ovarunits)

    # Calculate grand average
    print("  4. PROCESSING GRAND AVERAGE")
    data2sav = np.zeros((nspc, 1, 1, ny, nx))
    data2sav[:, 0, 0, :, :] = np.mean(
        davgs, axis=0)  #data2sav[nspc,nt,nz,ny,nx], davgs[nd,nspc,ny,nx]
    # Write a csv file
    jdays = np.zeros(
        (ny, nx)) + attr_in['SDATE']  # Annual average, set the beginning date
    hours = np.zeros((ny, nx))  # Annual average, set zeros
    data2csv = np.zeros((nspc, ny, nx))
    data2csv = data2sav[:, 0, 0, :, :]
    wrt_csv_for_psd(csv_anndavg, tracernames, data2csv, jdays, hours)
    # Write a binary file
    fout = _data2fin(data2sav, tracernames, attr_in)
    if l2uam:
        wrt_uamiv(out_anndavg, fout, lsurf=True, ounits=ovarunits)
    else:
        wrt_ioapi(out_anndavg, fout, lsurf=True, ounits=ovarunits)
Exemplo n.º 3
0
def hxdavg_naaqs(jdbeg,
                 jdend,
                 outfile,
                 csvfile,
                 comb,
                 attr_in,
                 nifiles,
                 infile_h,
                 infile_t,
                 *,
                 rank=8,
                 lyyyyjjj=True,
                 tzone=None,
                 l2uam=False,
                 tmpoutf=None):
    # Include functions
    from CAMxtools.combine.combine import combine
    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
    from CAMxtools.tzone.get_local_hrs import get_davg
    from CAMxtools._cnvt._data2fin import _data2fin
    from CAMxtools.write.wrt_ioapi import wrt_ioapi
    from CAMxtools.write.wrt_uamiv import wrt_uamiv
    from CAMxtools.write.wrt_uamiv import wrt_uamiv
    from CAMxtools.write.wrt_uamiv import wrt_uamiv
    from CAMxtools.write.wrt_uamiv import wrt_uamiv
    from CAMxtools.write.wrt_uamiv import wrt_uamiv
    from CAMxtools.naaqs.wrt_csv_for_naaqs import wrt_csv_for_naaqs
    import numpy as np
    import gc

    # Check arguments
    l1tzone = False
    if tzone != None:
        l1tzone = True  # If users specify a specific time zone, MATS MDA8 O3 is calculated based on the time zone.

    # 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")

    # Get attribute
    nx = attr_in['NCOLS']
    ny = attr_in['NROWS']

    # Set a variable name
    spec = "PM25"

    # Daily loop
    jdate = jdbeg
    while (jdate <= jdend):
        print("Processing {}".format(jdate))
        gdate = int(
            datetime.datetime.strptime(str(jdate), "%Y%j").strftime("%Y%m%d"))
        infile = []
        for ifile in range(0, nifiles):
            if lyyyyjjj:
                infile.append(infile_h[ifile] + '.' + str(jdate) + '.' +
                              infile_t[ifile])
            else:
                infile.append(infile_h[ifile] + '.' + str(gdate) + '.' +
                              infile_t[ifile])
        print("  1. PROCESSING COMBINE")
        tracernames, indata2, ovarunits = combine(None,
                                                  comb,
                                                  nifiles,
                                                  infile,
                                                  lverbose=False,
                                                  loutf=False,
                                                  lovarunits=True)
        if (jdate > jdbeg):
            # One time execution - Check 'O3' is the first output species and index output trcnames which excludes O3 from tracernames
            if jdate == jdbeg + 1:
                s = tracernames.index(spec)
                if s != 0:
                    exit('PM25 must be the first species in combine spec_def')
                ntracers = len(tracernames)
                trcnames = tracernames[1:]
            print("  2. PROCESSING METRICS FOR PREVIOUS DAY")
            concs_48_utc = np.append(indata1[:, :, 0, :, :],
                                     indata2[:, :, 0, :, :],
                                     axis=1)
            davg_1day = get_davg(concs_48_utc, tzone_ji, nx, ny)
            if jdate == jdbeg + 1: np.array([davg_1day])  #davgs[nd,nspc,ny,nx]
            if jdate > jdbeg + 1:
                davgs = np.append(davgs, np.array([davg_1day]), axis=0)
        indata1 = indata2
        jdate = int((datetime.datetime.strptime(str(jdate), "%Y%j") +
                     datetime.timedelta(days=1)).strftime("%Y%j"))
        gc.collect()
    del indata2
    del indata1
    gc.collect()

    # Prepare MAVG if asked
    nspc = len(tracernames)
    lout_davg = False
    if not tmpoutf == None: lout_davg = True
    if lout_davg:
        nd = jdend - jdbeg
        data2sav = np.zeros((ntracers, nd, 1, ny, nx))
        data2sav[:, :, 0, :, :] = np.einsum('jikl->ijkl', davgs)
        # Write a binary file for DAVG
        attr_in['TSTEP'] = 240000
        fout = _data2fin(data2sav, tracernames, attr_in)
        if l2uam:
            wrt_uamiv(tmpoutf, fout, lsurf=True, ounits=ovarunits)
        else:
            wrt_ioapi(tmpoutf, fout, lsurf=True, ounits=ovarunits)
        gc.collect()

    # Find X highest
    if rank == 1:
        rank_name = "FIRST"
    elif rank == 8:
        rank_name = "EIGHTH"
    elif rank == 0:
        rank_name = "AVERAGE"
    else:
        exit("rank must be either 1 or 8 or 0.")
    if rank == 0:
        print("  3. PROCESSING AVERAGE")
    else:
        print("  3. PROCESSING {} HIGHEST".format(rank_name))
    data2sav = np.zeros((ntracers, 1, 1, ny, nx))
    nd = jdend - jdbeg
    if (nd < rank):
        print('Your no. of days is less than the rank you specified.')
        print('No. of days from input files = {}'.format(nd))
        print('Rank you select is = {}'.format(rank))
        exit(
            'Either reduce your rank or increase no. of days from input files')
    if rank == 0:
        data2sav[:, 0, 0, :, :] = np.mean(
            davgs, axis=0)  #data2sav[nspc,nt,nz,ny,nx], davgs[nd,nspc,ny,nx]
    else:
        davg_pm25 = davgs[:,
                          0, :, :]  #davg_pm25[nd,ny,nx], davgs[nd,nspc,ny,nx]
        ind = np.argsort(davg_pm25,
                         axis=0)[nd - rank,
                                 ...]  #davgs[nd,nspc,ny,nx], ind[ny,nx]
        gr = np.ogrid[0:davg_pm25.shape[0], 0:davg_pm25.shape[1],
                      0:davg_pm25.shape[2]]
        gr[0] = ind
        data2sav[0, 0, 0, :, :] = davg_pm25[gr]  #data2sav[nspc,nt,nz,ny,nx]
        for i in range(nx):
            for j in range(ny):
                data2sav[1:, 0, 0, j, i] = davgs[ind[j, i], 1:, j, i]
    # Write a csv file
    hours = np.zeros((ny, nx))  # Average of DAVG, set zeros.
    if rank == 0:
        jdays = np.zeros(
            (ny,
             nx)) + attr_in['SDATE']  # Average of DAVG, set the beginning date
    else:
        jdays = attr_in['SDATE'] + ind[:, :]
    data2csv = np.zeros((ntracers, ny, nx))
    data2csv = data2sav[:, 0, 0, :, :]
    wrt_csv_for_naaqs(csvfile, tracernames, data2csv, jdays, hours)
    gc.collect()
    # Write a binary file for HXDAVG
    fout = _data2fin(data2sav, tracernames, attr_in)
    if l2uam:
        wrt_uamiv(outfile, fout, lsurf=True, ounits=ovarunits)
    else:
        wrt_ioapi(outfile, fout, lsurf=True, ounits=ovarunits)
    gc.collect()