Example #1
0
def   READ_BBIO_DIURNALCYCLE(DATA_DIR, nx, ny, \
                            MONTH, DAY, YYYY, HOUR,\
                            CO2_DIR='CO2_200508//BBIO_DIURNALCYCLE', \
                            flnmpref='nep',  tracer=2,category='CO2-SRCE'):
    #  3/4 only 2x25 for now...
    ext2d=bp.get_name_ext_2d()
    extres=bp.get_res_ext()
    DOY=tm.day_of_year(YYYY, MONTH,DAY)
    
    # if (DOY>59 and mod(YYYY,4)==0): 
    #         DOY=DOY+1for ireg in  self.regs:
                    
            
    SDOY=r'%3.3d' % DOY
    
    FILENAME = DATA_DIR.strip()+'/'+CO2_DIR+'/'+flnmpref.strip()+'.'+ext2d.strip()+'.'+extres.strip()+"."+SDOY
    # print 'read data from ', FILENAME
    
    tau0=tm.get_tau(1985,MONTH,DAY, HOUR)
    tau0=tau0/(3600.0)
    data, dunit, ios=bp.read_bpch2(FILENAME, category, tracer,tau0, \
                           nx,  ny, 1)
    
    if (ios==0):
        data=squeeze(data)
        return data,dunit
    else:
        print('error in read data ', ios)
        print(FILENAME)
        return None, ""
Example #2
0
def  READ_BBIO_DAILYAVERAGE(DATA_DIR, nx, ny, \
                            MONTH, DAY, YYYY, \
                            CO2_DIR='CO2_200508/BBIO_DAILYAVG', \
                            flnmpref='/CO2.daily',  tracer=3,category='CO2-SRCE'):
    #  3/4 only 2x25 for now...
    ext2d=bp.get_name_ext_2d()
    extres=bp.get_res_ext()
    DOY=tm.day_of_year(YYYY, MONTH,DAY)
    if (DOY>59 and mod(YYYY,4)>0): 
            DOY=DOY+1

    SDOY=r'%3.3d' % DOY
    
    
    FILENAME = DATA_DIR.strip()+'/'+CO2_DIR+'/'+flnmpref.strip()+'.'+ext2d.strip()+'.'+extres.strip()+"."+SDOY
    # print 'read data from ', FILENAME
    
    tau0=tm.get_tau(2000,MONTH,DAY)
    tau0=tau0/(3600.0)
    data, dunit, ios=bp.read_bpch2(FILENAME, category, tracer,tau0, \
                           nx,  ny, 1)
    
    if (ios==0):
        data=squeeze(data)
        return data,dunit
    else:
        print('error in read data ', ios)
        print(FILENAME)
        return None, ""
Example #3
0
def   READ_MONTH_BIOBRN_CO2(DATA_DIR, nx, ny, \
                             MONTH, YYYY,\
                             LBBSEA=True, \
                             CO2_DIR='biomass_200110', \
                             flnmpref='bioburn',  tracer=4,\
                             category='BIOBSRCE'):
    EMFACTCO2CO = 12.068
    MONTHDATES = [31, 28, 31, 30,31, 30, 31, 31,30, 31, 30, 31]

    #  3/4 only 2x25 for now...
    ext2d=bp.get_name_ext_2d()
    extres=bp.get_res_ext()
    SYYYY=r'%4.4d' % YYYY
    
    TIME=MONTHDATES[MONTH-1] * 86400.0

    if (LBBSEA):
        FILENAME = DATA_DIR.strip()+'/'+CO2_DIR+'/'+flnmpref.strip()+'.seasonal.'+ext2d.strip()+'.'+extres.strip()
        # print 'read data from ', FILENAME
        tau0=tm.get_tau(1985,MONTH,1)
        tau0=tau0/(3600.0)
    else:
        FILENAME = DATA_DIR.strip()+'/'+CO2_DIR+'/'+'bioburn'+'.interannual.'+ext2d.strip()+'.'+extres.strip()
        #        print 'read data from ', FILENAME
        tau0=tm.get_tau(YYYY,MONTH,1)
        tau0=tau0/(3600.0)

    data, dunit, ios=bp.read_bpch2(FILENAME, category, tracer,tau0, \
                                   nx,  ny, 1)
    if (ios==0):
        data=array(data)
        # Convert from [molec CO/cm2/month] to [molec CO2/cm2/month
        factor=EMFACTCO2CO/TIME
        data=factor*data
        data=squeeze(data)
        dunit='molec/cm2/s'
        return data,dunit
    else:
        print('error in read data ', ios)
        print(FILENAME)
        return None, ""
Example #4
0
def  READ_ANNUAL_BIONET_CO2(DATA_DIR, nx, ny, CO2_DIR='CO2_200508', flnmpref='//net_terr_exch_CO2',  tracer=5,category='CO2-SRCE'):
    #  3/4 only 2x25 for now...
    ext2d=bp.get_name_ext_2d()
    extres=bp.get_res_ext()
    FILENAME = DATA_DIR.strip()+'/'+CO2_DIR+'/'+flnmpref.strip()+'.'+ext2d.strip()+'.'+extres.strip()+".txt"
    # print 'read data from ', FILENAME
    
    tau0=tm.get_tau(1985,1,1)
    data, dunit, ios=bp.read_bpch2(FILENAME, category, tracer,tau0, \
                           nx,  ny, 1)
    
    if (ios==0):
        data=squeeze(data)
        return data,dunit
    
    else:
        print('error in read data ', ios)
        print(FILENAME)
        return None, ""
Example #5
0
print('step range:',list(range(gcdf.nstep_start, nstep)))

#gcdf.restart_file

pre_year = 1985
pre_restart_name = ' '
#>>/ps/loop0/over each step

for istep in range(gcdf.nstep_start, nstep): # run through the emission time period 
    npbf = em_npb[istep]
    pbflnm = em_ch4flnm[istep]
    yyyy_st = em_yyyy_st[istep]
    doy_st = em_doy_st[istep]
    yyyy, mm, dd = tm.doy_to_time_array(doy_st, yyyy_st)
    ##c/tau_st in seconds since 1985.1.1.0.0
    tau_st = tm.get_tau(yyyy,mm, dd)
	
#    print('-'*10+'starting year, month, day:',  yyyy, mm, dd)
    
    istep_end = min(istep+lag_window, len(em_doy_st)-1)
#    print('-'*10+'istep_start, istep_end:' istep, istep_end)
	
    
    if (sub_sous):
        nsub_sous = gcdf.n_sous_loop 
		# sub emission sources loop
    else:
        nsub_sous = 1
        
    if (sub_tracers):
        nsub_run = gcdf.n_tracer_loop
Example #6
0
    def gen_prior(self, timestep, ntime, yyyy0, mm0, dd0, hh0=0, mi0=0, sec0=0, \
                 update_state=True, errset=None, do_debug=False):
        """

        construct a prior surface flux used for a time period starting with yyyy0, mm0, dd0

        """
        
        tau0=tm.get_tau(yyyy0, mm0, dd0, hh0, mi0, sec0)
        
        self.ntime=ntime
        self.timestep=timestep
        
        self.prior=zeros([self.nreg, self.ntime], float)
        self.err=zeros([self.nreg, self.ntime], float)
        
        
        for itime in range(self.ntime):
            tau1=tau0+timestep
            self.tau0.append(tau0)
            self.tau1.append(tau1)
            tau=0.5*(tau0+tau1)
            date_new=tm.tai85_to_utc(tau0)
            yyyy, mm, dd, hh, mi, sec=tm.utc_to_time_array(date_new)
            # print yyyy, mm, dd, hh
            doy=tm.day_of_year(yyyy, mm, dd)
            self.yyyy.append(yyyy)
            self.doy.append(doy)
                

            stt=EMISSCO2(yyyy,mm, dd, hh, do_debug=False)
            stv=self.map_to_st(stt)
            # for test only
            # if (itime in [0, 2, 4,6,8]):
            #    stv[3]=1.80*stv[3]
            #    stv[5]=1.80*stv[5]
                
            # stv=1.8*stv
            self.prior[:,itime]=stv
            
            
            if (itime==1 and do_debug):
                subplot(3,1,1)
                gpl.plot_map(stt, self.lon, self.lat, use_pcolor=1)
                subplot(3,1,2)
                stt2=self.st_to_map(stv)
                gpl.plot_map(stt2, self.lon, self.lat, use_pcolor=1)
                subplot(3,1,3)
                gpl.plot_map(self.reg_map, self.lon, self.lat, use_pcolor=1)
                show()
            
            if (errset==None):
                # err_fact=sqrt(365.0*24*3600.0/self.timestep)
                err_factor=5.0
                self.err[1:,itime]=err_factor*self.def_err[1:]
                self.err[0, itime]=self.def_err[0]*stv[0]
                # if (itime==0):
                #     print self.err[:,0]
                #    print self.prior[:,0]
                
            elif (size(errset)==1):
                self.err[:,itime]=errset*stv[:]
            else:
                self.err[:,itime]=errset[:]
            
            tau0=tau1
        self.construct_err_cov()
        
        if (update_state):
            self.st=array(self.prior)
def gen_transcom_coef(flnm, flux_name,\
                      yyyy,\
                      inv_step,\
                      inv_path=gcdf.inv_path,\
                      modelname='GEOS4', \
                      category='CO2-SRCE', ntracer=1, \
                      do_debug=True, \
                      test_month=0):

    print flnm
    ext1 = bpy.get_name_ext_2d()
    ext1 = ext1.strip()
    model_res = bpy.get_res_ext()
    model_res = model_res.strip()

    # model information

    gc_lon = cmgrd.get_model_lon(model_res=model_res)
    ix = size(gc_lon)
    gc_lat = cmgrd.get_model_lat(model_res=model_res)
    iy = size(gc_lat)

    reg_m = rco2_f.read_region_map()
    reg_m = squeeze(reg_m)

    nreg = max(reg_m.flat) + 1
    coef_m = zeros(shape(reg_m), float)
    sstep = r'%2.2d' % (inv_step)

    resflnm = inv_path + "std_oco_assim_res" + "." + sstep + ".nc"
    print resflnm

    varnames = ['whole_x', 'whole_x0', 'dx', 'sum_xtm']
    x, x0, dx, xtm = ofb.ncf_read(resflnm, varnames)
    coef_a = x - x0

    if (test_month > 0):
        stest_month = r'%2.2d' % (test_month - 1)
        resflnm = inv_path + "/" + "std_oco_assim_res" + "." + stest_month + ".nc"
        print resflnm
        x_t, x0_t, dx_t, xtm_t = ofb.ncf_read(resflnm, varnames)
        coef_t = x_t - x0_t

    nx = nreg - 1
    nst = (yyyy - 2004) * 12 * nx

    for imm in range(1, 13):

        # read biomass_burning  in regular grid box
        # covert to ktC /y

        coef_cut = coef_a[nst:nst + nx]
        coef_m[:, :] = 0
        if (imm <= test_month):
            coef_cut = coef_t[nst:nst + nx]

        for ireg in range(1, nreg):
            sel_cells = where(reg_m == ireg)
            coef_m[sel_cells] = coef_cut[ireg - 1]

        tau0 = tm.get_tau(yyyy, imm, 1)
        tau0 = tau0 / 3600.0

        if (imm < 12):
            tau1 = tm.get_tau(yyyy, imm + 1, 1)
        else:
            tau1 = tm.get_tau(yyyy + 1, 1, 1)

        tau1 = tau1 / 3600.0
        sdate = r'%4.4d%2.2d' % (yyyy, imm)

        full_flnm = flux_name + "." + sdate
        funit = wfbp.open_flux_bpch2_file(full_flnm, title='flux')
        unit = 'unitless'


        wfbp.write_flux_record(funit, coef_m, \
                               gc_lon, gc_lat,\
                               tau0, tau1,\
                               ntracer, \
                               modelname,\
                               category,\
                               unit, \
                               do_debug=False)

        wfbp.close_flux_bpch2_file(funit)
        nst = nst + nx
def gen_transcom_coef(flux_name,\
                      yyyy,\
                      coef_a,\
                      inv_step,\
                      inv_path=gcdf.inv_path,\
                      modelname='GEOS5', \
                      category='CO2-SRCE', ntracer=1, \
                      do_debug=True, \
                      test_month=0):

    # print flnm
    ext1 = bpy.get_name_ext_2d()
    ext1 = ext1.strip()
    model_res = bpy.get_res_ext()
    model_res = model_res.strip()

    # model information

    gc_lon = cmgrd.get_model_lon(model_res=model_res)
    ix = size(gc_lon)
    gc_lat = cmgrd.get_model_lat(model_res=model_res)
    iy = size(gc_lat)

    reg_m = rco2_f.read_region_map('2006D001')
    reg_m = squeeze(reg_m)

    nreg = max(reg_m.flat) + 1
    coef_m = zeros(shape(reg_m), float)

    # nx=nreg # -1
    # nst=(yyyy-2003)*12*nx
    nst = 0
    idoy = 1
    nstep = inv_step

    print 'nstep, nreg', nstep, nreg

    inc_doy = 8

    for istep in range(0, nstep):

        # read biomass_burning  in regular grid box
        # covert to ktC /y

        coef_cut = coef_a[nst:nst + nreg]
        coef_m[:, :] = 0
        sdoy = r'%4.4dD%3.3d' % (yyyy, idoy)

        reg_flux = rco2_f.read_reg_flux_map(sdoy)
        print shape(reg_flux)

        for ireg in range(nreg):
            coef_m[:, :] = coef_m[:, :] + coef_cut[ireg] * reg_flux[:, :, ireg]

        yyyy, mm, dd = tm.doy_to_time_array(idoy, yyyy)
        tau0 = tm.get_tau(yyyy, mm, dd)
        tau0 = tau0 / 3600.0

        idoy = idoy + inc_doy
        yyyy, mm, dd = tm.doy_to_time_array(idoy, yyyy)
        tau1 = tm.get_tau(yyyy, mm, dd)
        tau1 = tau1 / 3600.0

        full_flnm = flux_name + sdoy
        funit = wfbp.open_flux_bpch2_file(full_flnm, title='flux')
        unit = 'kg/s'
        print '--------sum--------'
        print 'step:', istep

        print 'max, min adjustment (GtC/a):', ocu.kg_s_to_GtC_Y * max(
            coef_m.flat), ocu.kg_s_to_GtC_Y * min(coef_m.flat)
        print 'total adjustment (GtC/a):', ocu.kg_s_to_GtC_Y * sum(coef_m)

        print 'max, min adjustment (kg/s):', max(coef_m.flat), min(coef_m.flat)
        print 'total adjustment (kg/s):', sum(coef_m)

        print '--------end--------'

        wfbp.write_flux_record(funit, coef_m, \
                               gc_lon, gc_lat,\
                               tau0, tau1,\
                               ntracer, \
                               modelname,\
                               category,\
                               unit, \
                               do_debug=False)

        wfbp.close_flux_bpch2_file(funit)
        nst = nst + nreg