Exemple #1
0
def load_ts_field(bpchflnm, ftracerinfo, fdiaginfo):
    # print ftraceinfo
    # print  fdiaginfo
    bpch2_ts=brw.bpch2_file_rw(bpchflnm, "r", \
                               do_read=1,  ftracerinfo=ftracerinfo,\
                               fdiaginfo=fdiaginfo)
    return bpch2_ts
 def __init__(self, org_flnm=None):
     self.ntracers=0
     if (org_flnm!=None):
          bpch2=brw.bpch2_file_rw(org_flnm, 'r', do_read=1)
          print(bpch2.stat)
          self.org_flnm=org_flnm
          self.bpch2=bpch2
     else:
          self.org_flnm=None
          self.bpch2=None
def get_ts_val(yyyy, mm, dd, \
               categorys=None, \
               tracers=None, \
               taus=None, \
               tranames=None,\
               dfile_prefix="ts_satellite",\
               as_gpf=True, \
               dpath=gcfs.data_path, \
               dfile_affix="bpch"):
    """ read the data into the one gp_field
    # we read in all the data
    """
    sdate = r'%4.4d%2.2d%2.2d' % (yyyy, mm, dd)
    full_flnm = gcfs.data_path + "/" + dfile_prefix.strip(
    ) + "." + sdate + "." + dfile_affix.strip()
    bpch2 = brw.bpch2_file_rw(full_flnm, "r", do_read=1)

    if (bpch2.stat == 0):
        ds = bpch2.get_data(categorys, tracers, None, None)

    gpf.add_gp(sname, rdata)
    if (len(ds)) == 0:
        lds = list()
        lds.append(ds)

    ngp = 0
    if (as_gpf):
        for bpdata in ds:
            # force delete
            if (ngp == 0):
                rlat = bpdata.grid.get_lat()
                rlon = bpdata.grid.get_lon()
                rz = bpdata.grid.get_z()
                ax_lon = gax.gp_axis('lon', rlon)
                ax_lat = gax.gp_axis('lat', rlat)
                ax_z = gax.gp_axis('level', rz)
                data_collect = gf.gp_field(full_flnm,
                                           axis_set=[ax_lon, ax_lat, ax_z])
                add_bpdata_to_gpf(bpdata, gpf)
            ngp = ngp + 1
        bpch2 = None
        return data_collect
    else:
        bpch2 = None
        return ds
    def copy_restart_file(self,sel_idx, newfile, ntracers=1,newtau=None, do_regrid=False, new_lon=None, new_lat=None):
        """ copy to new_restart file and shift the time to a new time"""

        data_list, found=self.bpch2.get_data(None, sel_idx, None, None)
        if (found[0]==0):
            print('No record found')
            return None
        
        bpdata=data_list[0]
        bpch2_w=brw.bpch2_file_rw(newfile, 'w')
        ifunit=95
        bpch2_w.open_bpch2_w(ifunit,'test')
        
        if (newtau!=None):
            tau1, tau0=bpdata.get_attr(['tau0', 'tau1'])
            tau1=newtau+tau1-tau0
            tau0=newtau
            bpdata.set_attr('tau0', tau0)
            bpdata.set_attr('tau', tau1)
        sl=r'write restart data for %d tracers into file ' % ntracers
        print('='*80)
        print(sl+newfile.strip())
        if (do_regrid):
            print('I am do regrid', size(new_lon), size(new_lat))
            
            bpdata.regrid_data(new_lon, new_lat)
        
        for i in range(ntracers):
            bpdata.ntracer=i+1
            traname=bpdata.get_attr(['name'])
            print(i, traname[0].strip(), bpdata.category.strip(), bpdata.ntracer, bpdata.unit.strip(), shape(bpdata.data), max(bpdata.data.flat), min(bpdata.data.flat))
            
            
            bpdata.write_to_bpch2_file(ifunit)
            
        bpch2_w.close_bpch2_file()
        print('-'*30+'Done'+'-'*30)
        print('='*80)
def setup_field(yyyy, mm, dd):

    sdate = r'%4.4d%2.2d%2.2d' % (yyyy, mm, dd)
    full_flnm = gcfs.data_path.strip(
    ) + "/" + "ts_satellite" + "." + sdate + ".bpch"
    bpch2_ts = brw.bpch2_file_rw(full_flnm, "r", do_read=1)
    # get the total co2
    categorys = None
    tracers = [1]
    tranames = ['CO2']
    taus = None
    data_list = None
    data_list, founded = bpch2_ts.get_data(categorys, tracers, taus, tranames)
    # bpch2_ts.print_datainfo()

    print founded, len(data_list)
    # set up gpfield
    bpdata = data_list[0]
    rlat = bpdata.grid.get_lat()
    rlon = bpdata.grid.get_lon()
    rz = bpdata.grid.get_z()
    ax_lon = gax.gp_axis('lon', rlon)
    ax_lat = gax.gp_axis('lat', rlat)
    ax_z = gax.gp_axis('level', rz)
    gpf = gf.gp_field('CO2', axis_set=[ax_lon, ax_lat, ax_z])
    add_bpdata_to_gpf(bpdata, gpf, gpname='CO2')

    # get the total SURF
    categorys = None
    tracers = None
    tranames = ['PS', 'AVGW', 'CF', 'CThgt']
    taus = None
    data_list, founded = bpch2_ts.get_data(categorys, tracers, taus, tranames)
    for bpdata in data_list:
        tn = bpdata.get_attr(['name'])
        gpname = tn[0].strip()

        add_bpdata_to_gpf(bpdata, gpf, gpname=gpname)

    # get the land-water and surface information

    bpch2_ctm = brw.bpch2_file_rw('../ctm.bpch', "r", do_read=1)
    utc = tm.time_array_to_utc(yyyy, mm, dd)
    # print utc
    # bpch2_ctm.print_datainfo()

    tau0 = tm.utc_to_tai85(utc)
    tau0 = tau0 / (3600.0)
    # print tau0

    # get the total SURF
    categorys = None
    tracers = None
    tranames = ['LWI']
    taus = [tau0]
    # taus=None
    data_list, founded = bpch2_ctm.get_data(categorys, tracers, taus, tranames)
    bpdata = data_list[0]
    tau0, tau1 = bpdata.get_attr(['tau0', 'tau1'])
    add_bpdata_to_gpf(bpdata, gpf, gpname='LWI')

    return gpf
def setup_field(yyyy,
                mm,
                dd,
                em_st,
                em_end,
                datapath=gcdf.data_path,
                ctm_date0=None):
    sdate = r'%4.4d%2.2d%2.2d' % (yyyy, mm, dd)
    sem = r'EN%4.4d-EN%4.4d' % (em_st, em_end)
    full_flnm = datapath + "/" + "ts" + "." + sem + "." + sdate + ".bpch"
    if (ctm_date0 == None):
        ctmflnm = datapath + "/" + "ctm" + "." + sem + ".bpch"
    else:
        ctmflnm = datapath + "/" + "ctm" + "." + sem + "." + ctm_date0 + ".bpch"

    ftraceinfo = datapath + "/" + "tracerinfo" + "." + sem + ".dat"
    fdiaginfo = datapath + "/" + "diaginfo" + "." + sem + ".dat"

    print 'Read tracer data from', full_flnm
    # print ftraceinfo
    # print  fdiaginfo
    bpch2_ts=brw.bpch2_file_rw(full_flnm, "r", \
                               do_read=1,  ftracerinfo=ftraceinfo,\
                               fdiaginfo=fdiaginfo)
    print 'tracer number', bpch2_ts.ntracers
    #  bpch2_ts.print_datainfo()
    # get the total co
    categorys = None
    tracers = None
    tranames = ['CO']
    taus = None
    data_list = None

    data_list, founded = bpch2_ts.get_data(categorys, tracers, taus, tranames)
    # bpch2_ts.print_datainfo()

    print founded, len(data_list)
    # set up gpfield
    bpdata = data_list[0]
    rlat = bpdata.grid.get_lat()
    rlon = bpdata.grid.get_lon()
    rz = bpdata.grid.get_z()
    ax_lon = gax.gp_axis('lon', rlon)
    ax_lat = gax.gp_axis('lat', rlat)
    ax_z = gax.gp_axis('level', rz)
    gpf = gf.gp_field(sem, axis_set=[ax_lon, ax_lat, ax_z])
    iem = em_st
    for idata in range(len(data_list)):
        bpdata = data_list[idata]
        if bpdata == None:
            pass
        else:
            gpname = r'CO.%4.4d' % iem
            # gpname=r'CO.%4.4d' % bpdata.ntracer
            # print gpname
            add_bpdata_to_gpf(bpdata, gpf, gpname=gpname)
            iem = iem + 1

    # get the total SURF
    categorys = None
    tracers = None
    tranames = ['PS', 'AVGW', 'CF', 'CThgt', 'ODCOL']
    taus = None
    data_list, founded = bpch2_ts.get_data(categorys, tracers, taus, tranames)
    for bpdata in data_list:
        tn = bpdata.get_attr(['name'])
        gpname = tn[0].strip()

        add_bpdata_to_gpf(bpdata, gpf, gpname=gpname)

    # get the land-water and surface information
    print 'Read additional data LWI from ', ctmflnm

    utc = tm.time_array_to_utc(yyyy, mm, dd)
    # print utc
    # bpch2_ctm.print_datainfo()

    tau0 = tm.utc_to_tai85(utc)
    tau0 = tau0 / (3600.0)

    ### this part is not necessary for CO
    founded = False
    if (found):


        bpch2_ctm=brw.bpch2_file_rw(ctmflnm, "r", do_read=2,\
                                    sel_categorys=['LANDMAP', 'TOTAOD'],sel_tracers=[1,1], sel_taus=[tau0, tau0])

        # print tau0

        # get the total SURF
        print 'tau0', tau0
        categorys = None
        tracers = None
        # tranames=['LWI']
        taus = [tau0]
        # taus=None
        data_list, founded = bpch2_ctm.get_data(categorys, tracers, taus,
                                                tranames)
        print founded

        bpdata = data_list[0]
        tau0, tau1 = bpdata.get_attr(['tau0', 'tau1'])
        add_bpdata_to_gpf(bpdata, gpf, gpname='LWI')
        cld_lon, cld_lat, cld_z, cld_prob = rcm.read_cloud_map(mm)
        add_map_to_gpf(cld_lon, cld_lat, cld_z, cld_prob, 'CLDF', gpf)
        od_lon, od_lat, od_z, od_prob = rcm.read_od_map(mm)
        add_map_to_gpf(od_lon, od_lat, od_z, od_prob, 'ODP', gpf)
    return gpf
Exemple #7
0
    #for flnm in flnm_list:
    ps_filename = 'ts_24h_avg.EN0001-EN0002.20040101.bpch'

    for flnm in flnm_list:

        full_flnm = datapath + "/" + flnm

        outfile = datapath + "/x" + flnm

        print '1.============= Read tracer data from', full_flnm
        print ftraceinfo
        print fdiaginfo

        bpch2_ps=brw.bpch2_file_rw(ps_filename, "r", \
                                   do_read=1, \
                                   ftracerinfo=ftraceinfo,\
                                   fdiaginfo=fdiaginfo)

        # surface pressure
        # careful check needed for different GEOS-Chem version

        categorys = ['PS-PTOP']
        tracers = None

        tranames = None
        taus = None
        data_list = None
        data_list, founded = bpch2_ps.get_data(categorys, tracers, taus,
                                               tranames)
        if (len(data_list) < 1):
            ps = None
def read_daily_field(yyyy, mm, dd, \
                     em_st, em_end,\
                     gpcategory=["IJ-AVG-$"],\
                     gpnames=None,\
                     gptracers=None,\
                     datapath=gcdf.data_path, \
                     prefix="ts_24h_avg"):
    """ set up  the model fields from time-series files and ctm files
    
    yyyy, mm, dd            ----in-----    year, month, day
    
    em_st_list, em_end_list ----in---  list of the output tagged tracers.
    The name for time series files is in the format of ts.EN[emst]-EN[emend].[date].bpch

    
    datapath  ------ in -----------   directory of the model outputs
    
    gf ----- out  ----- collection of the tagged tracers + surface pressure (SP) + Land MAP(LWI)+Aux data
    
    nem ----- out -----  the number of tagged tracers
    
    """

    sdate = r'%4.4d%2.2d%2.2d' % (yyyy, mm, dd)
    iem = 1

    syyyy = r'%4.4d' % yyyy

    sem = r'EN%4.4d-EN%4.4d' % (em_st, em_end)
    full_datapath = datapath

    full_flnm = full_datapath + "/" + prefix + "." + sem + "." + sdate + ".bpch"

    ftraceinfo = full_datapath + "/" + "tracerinfo" + "." + sem + ".dat"
    fdiaginfo = full_datapath + "/" + "diaginfo" + "." + sem + ".dat"

    bpch2_ts=brw.bpch2_file_rw(full_flnm, "r", \
                               do_read=1,  ftracerinfo=ftraceinfo,\
                               fdiaginfo=fdiaginfo)

    # temperature

    categorys = ["DAO-3D-$"]
    tracers = None
    tranames = None
    taus = None
    data_list = None

    data_list, founded = bpch2_ts.get_data(categorys, tracers, taus, tranames)
    print 'found temperature', founded, len(data_list)

    bpdata = data_list[0]

    rlat = bpdata.grid.get_lat()
    rlon = bpdata.grid.get_lon()
    rz = bpdata.grid.get_z()

    t = array(bpdata.data)

    # sp

    categorys = ['PS-PTOP']

    tracers = None
    tranames = None
    taus = None
    data_list = None

    data_list, founded = bpch2_ts.get_data(categorys, tracers, taus, tranames)
    print 'found sp', founded, len(data_list)

    bpdata = data_list[0]
    sp = array(bpdata.data)

    # gp

    tracers = gptracers
    tranames = gpnames

    categorys = gpcategory

    taus = None
    data_list = None
    data_list, founded = bpch2_ts.get_data(categorys, tracers, taus, tranames)

    print 'found gp', founded, len(data_list)

    gp_list = list()
    nusd_data = len(data_list)

    for idata in range(0, nusd_data):
        bpdata = data_list[idata]
        gp_list.append(bpdata.data)

    return rlon, rlat, rz, t, sp, gp_list
    inputs.update({vname: vstr})

if ('file' in inputs):
    flnm = inputs['file']
else:
    while True:
        print 'give the name of bpch2 file'
        tt = raw_input()
        flnm = tt.strip()
        print flnm

        try:
            ftest = open(flnm, 'r')
            ftest.close()

        except IOError:
            print 'file ' + flnm + 'not found'

        print 'file exists'
        break

bpch2 = bpch2_mod.bpch2_file_rw(flnm, 'r', do_read=1)

sel_tracer = -1

if ('tracer' in inputs):
    sel_tracer = inputs['tracer']

if (sel_tracer == -1):
    bpch2.print_datainfo()