コード例 #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, ""
コード例 #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, ""
コード例 #3
0
def gen_input_geos(run_step,\
                   YYYY,\
                   DOY,\
                   member_start =1,\
                   member_end = 40,\
                   tmpfile="input.geos.temp",\
                   newfile="input.geos" ,\
                   temp_path = './temp/',\
                   run_path='./',\
                   data_path='./',\
                   out_path='./Config_files',\
                   diagn_path='./Diagn_files',\
                   n_regs = 20,\
                   n_sous = 2,\
                   sous_name = ['CH4_TAG_AN','CH4_TAG_NA'],\
                   temporal_res = 30,\
                   **keywords \
                          ):
    """ create the new input to drive the ensemble run
    YYYY: years of the time serier
    DOY:  doys of the time serier
    em_doy: the date need emission
    temp_path: the path of input.geos.temp
    n_regs: the number of regions
    n_sous: the number of emission sources(defualt incloude anthr and nature)
    sous_name: the name of emission sources     
    temporal_res: ensemble run temperal resolution (unit: days)
    
    """

    ntime = size(YYYY)
    if ('time_start' in keywords):
        ts = keywords['time_start']
        syyyy = ts[0:4]
        yst = int(syyyy)
        dst = ts[4:7]
        dst = int(dst)

        # mmst=int(smm)
        # sdd=ts[6:8]
        # ddst=int(sdd)

        # dst=tm.day_of_year(yst, mmst, ddst)
    else:
        yst = YYYY[0]
        dst = DOY[0]
    # list for start and ending simulation time

    tst = tm.doy_to_utc(dst, sec=0, yyyy=yst)
    tst = tst.replace('-', '')
    tst = tst.replace(':', '')
    print(tst)
    #  if (em_step==None):
    #      iday0=DOY[0]
    #      for iday in DOY[1:]:
    #          if (iday>iday0):
    #              em_step=iday-iday0
    #              break
    #          else:
    #              iday0=iday

    if ('time_end' in keywords):
        te = keywords['time_end']
        syyyy = te[0:4]
        yed = int(syyyy)
        smm = te[4:6]
        mmst = int(smm)
        sdd = te[6:8]
        ddst = int(sdd)
        ded = tm.day_of_year(yst, mmst, ddst)
    else:
        yed = YYYY[1]  #-1
        ded = DOY[1]  #-1

    # the endtime should be + timestep
    # yed, ded=tm.next_doy(yed, ded, em_step)

    tend = tm.doy_to_utc(ded, sec=0, yyyy=yed)
    tend = tend.replace('-', '')
    tend = tend.replace(':', '')
    print(tend)

    # the number of tracter
    #  n_regs = gcdf.nregs # divided regions
    #  n_sous = gcdf.nemis  # emission sources
    #  sous_name = gcdf.eminm
    diag_species = '  '
    for i_spc in range(n_sous * n_regs + 1):
        diag_species = diag_species + str(i_spc + 1) + ' ' * 2
    diag_species = diag_species + '513'
    enafix = r'ST%3.3d.EN%4.4d-EN%4.4d' % (run_step, member_start, member_end)
    temp_res_d = r'%08d' % (temporal_res)

    input_file = open(temp_path + tmpfile, 'r')
    output_file = open(out_path + newfile, 'w')
    ts = input_file.read().split(
        '------------------------+------------------------------------------------------'
    )

    for t in ts:
        lines = t.strip('\n').split('\n')
        if lines[0] == '%%% SIMULATION MENU %%% :':
            for line in lines:
                line = line.replace('$RUNPATH', run_path)
                line = line.replace('$DATAPATH', data_path)
                if (line.split(':')[0].strip(' ') == 'Start YYYYMMDD, hhmmss'):
                    output_file.write(line.split(':')[0] + ': ' + tst + '\n')
                elif (line.split(':')[0].strip(' ') == 'End   YYYYMMDD, hhmmss'
                      ):
                    output_file.write(line.split(':')[0] + ': ' + tend + '\n')
                else:
                    output_file.write(line + '\n')

        elif lines[0] == '%%% CH4 MENU %%%        :':
            output_file.write(lines[0] + '\n')
            for line in lines:
                if (line.split(':')[0].strip(' ') == 'Use TCCON obs operator?'
                    ):
                    output_file.write(line + '\n')
                if (line.split(':')[0].strip(' ') == 'Use GOSAT obs operator?'
                    ):
                    output_file.write(line + '\n')
                if (line.split(':')[0].strip(' ') == 'Ensemble Temporal Res'):
                    output_file.write(
                        line.split(':')[0] + ': ' + str(temp_res_d) + ' ' +
                        '0' * 6 + '\n')
                if (line.split(':')[0].strip(' ') == 'The Number of Regions'):
                    output_file.write(
                        line.split(':')[0] + ': ' + str(n_regs) + '\n')
                if (line.split(':')[0].strip(' ') == 'The Number of Sources'):
                    output_file.write(
                        line.split(':')[0] + ': ' + str(n_sous) + '\n')
                if (line.split(':')[0].strip(' ') == 'CH4 Sources Entries --->'
                    ):
                    output_file.write(line + '\n')
                    if (len(sous_name) == n_sous):
                        for i_sous in range(n_sous):
                            output_file.write('Sources name            : ' +
                                              sous_name[i_sous] + '\n')

        elif lines[0] == '%%% ADVECTED SPECIES MENU %%%:':
            output_file.write(lines[0] + '\n')
            output_file.write('Type of simulation      : 9' + '\n')
            output_file.write('Species Entries ------->: Name' + '\n')
            output_file.write('Species name            : CH4' + '\n')
            for i_sous in range(n_sous):
                for i_regs in range(n_regs):
                    output_file.write('Species name            : ' +
                                      sous_name[i_sous] + str(i_regs + 1) +
                                      '\n')

        elif 'ND51' in lines[0]:
            for line in lines:
                if (line.split(':')[0].strip(' ') == 'Tracers to include'):
                    output_file.write(
                        line.split(':')[0] + ': ' + diag_species + '\n')
                else:
                    line = line.replace('$DATAPATH', diagn_path + '/ND51')
                    line = line.replace('STYYY.ENXXXX-ENXXXX', enafix)
                    output_file.write(line + '\n')

                #output_file.write(line+'\n')
        else:
            for line in lines:
                line = line.replace('$DATAPATH', diagn_path)
                line = line.replace('STYYY.ENXXXX-ENXXXX', enafix)

                output_file.write(line + '\n')
        output_file.write(
            '------------------------+------------------------------------------------------'
            + '\n')
    print('reach the end')
    output_file.close()
    input_file.close()
コード例 #4
0
# observation related
# obs_path=run_path+'/oco_obs/'
inv_path = run_path + '/oco_inv/'  # inversion directory
oco_orbit_path = run_path + '/gosat_orbit/'  # OCO orbit
oco_ak_path = run_path + '/gosat_ak/'  # OCO averaging kernel

# for multiple observation
view_type = 'new_iss'  # ???
view_mode = "glint"  # 'glint', 'nadir' or 'n16g16'
obs_path = run_path + '/' + view_type + '_obs/'  # observation directory

def_input_file = run_path + '/input.geos'  # GEOS-Chem model input file

#  starting time for geos chem simulation
st_yyyy, st_mm, st_dd = 2009, 1, 1
st_doy = tm.day_of_year(st_yyyy, st_mm, st_dd)  # day of year

# time resultion in day
use_fixed_mod_err = True  # ???
temporal_resolution = 32  # temporal resolution for inversions in days

# ??? the ending time for geos chem simulation
inv_lag_window = 4
# inversion lag window = temporal_resolution*n_inv_window

ntime_geos_chem = inv_lag_window
# 2*inv_lag_window if used the moving window # the last day of geos-chem simulation is given by ntime * temporal_resolution
inv_ntime = inv_lag_window

# spatial resolution for land and ocean
n_land_lat_div, n_land_lon_div = 3, 3
コード例 #5
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)
コード例 #6
0
def convert_geos_chem_field(yyyy, mm, dd, \
                                step,\
                                em_st,\
                                em_end,\
                                modtype=5,\
                                datapath="../enkf_new_2010/enkf_output/" \
                                ):

    # read in the GEOS-Chem model data

    sdate = r'%4.4d%2.2d%2.2d' % (yyyy, mm, dd)
    figdoy = tm.day_of_year(yyyy, mm, dd)
    # sdoy=r'%3.3d' % (doy)
    sem = r'ST%3.3d.EN%4.4d-EN%4.4d' % (step, em_st, em_end)
    full_flnm = datapath + "/" + "ts_satellite" + "." + sem + "." + sdate + ".bpch"
    ncflnm = datapath + "/" + "ts_satellite" + "." + sdate + ".nc"

    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 co2
    # CO2 profile

    # extract co2 data set

    categorys = None
    tracers = None
    tranames = ['CO2']
    taus = None
    data_list = None
    data_list, founded = bpch2_ts.get_data(categorys, tracers, taus, tranames)

    # print len(data_list)
    bpdata = data_list[0]
    rlat = bpdata.grid.get_lat()
    rlon = bpdata.grid.get_lon()
    rz = bpdata.grid.get_z()

    all_data = list()
    for bpdata in data_list:

        all_data.append(bpdata.data)

    all_data = npy.array(all_data)
    # print npy.shape(all_data)
    # print 'save'

    co2 = all_data

    categorys = None
    tracers = None
    tranames = ['PSURF']
    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()

        gp = bpdata.data

        if (gpname == 'PSURF'):
            sp = array(gp)
        elif (gpname == 'AVGW'):
            h2o = array(gp)
    h2o = array(co2[0, :, :, :])
    h2o[:, :, :] = 1.0e-8

    # write to netcdf

    return rlon, rlat, rz, co2, h2o, sp
コード例 #7
0
def convert_geos_chem_field(yyyy, mm, dd, \
                         modtype=5,\
                         datapath="./enkf_output_g5" \
                            ):

    # read in the GEOS-Chem model data

    sdate = r'%4.4d%2.2d%2.2d' % (yyyy, mm, dd)
    figdoy = tm.day_of_year(yyyy, mm, dd)
    # sdoy=r'%3.3d' % (doy)
    em_st = 1
    em_end = 2
    sem = r'EN%4.4d-EN%4.4d' % (em_st, em_end)
    full_flnm = datapath + "/" + "ts_satellite" + "." + sem + "." + sdate + ".bpch"
    ncflnm = datapath + "/" + "ts_satellite" + "." + sdate + ".nc"

    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 co2
    # CO2 profile

    categorys = None
    tracers = None
    tranames = ['CO2']
    taus = None
    data_list = None
    data_list, founded = bpch2_ts.get_data(categorys, tracers, taus, tranames)

    print founded, len(data_list)
    # how change tracer number
    #
    # for example, we choose  northern american region 3
    # bpdata=data_list[3]
    # the 100 ppm backgroundfrom tracer '1'
    #
    # bpdata2=data_list[1]
    # co2=bpdata.data-bpdata2.data
    # or simply
    # co2=bpdata.data-100.0e-6

    # the following is a special case when tracer one is coming from posteriori flux adjustment

    bpdata = data_list[0]
    bpdata2 = data_list[1]

    # co2=bpdata.data+bpdata2.data-100.0e-6

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

    categorys = None
    tracers = None
    tranames = ['PSURF', 'AVGW']
    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()

        gp = bpdata.data

        if (gpname == 'PSURF'):
            sp = array(gp)
            levels = rz[:]
            levels = array(levels)
            levels = levels + 1
            levels = levels.astype(int)
            pres = pm.get_pres_mod_2d(sp, levels, 5, 1)
        elif (gpname == 'AVGW'):
            h2o = array(gp)

    # write to netcdf

    return figdoy, rlon, rlat, rz, co2, pres, sp
コード例 #8
0
    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

    # add the standard one


if (__name__ == '__main__'):
    yyyy = 2006
    for mm in range(1, 13):

        doy_st = tm.day_of_year(yyyy, mm, 1)
        if (mm < 12):
            doy_end = tm.day_of_year(yyyy, mm + 1, 1)
            doy_end = doy_end - 1

        else:
            doy_end = tm.day_of_year(yyyy, mm, 31)
            doy_end = doy_end

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

        ndays = doy_end - doy_st + 1
        em_st, em_end = 1, 2

        datapath = "/home/lfeng/local_disk_2/tag_co2/run4_nep_cp/run_" + str(
            yyyy) + "_new/"
コード例 #9
0
def convert_geos_chem_field(yyyy, mm, dd, \
                                step,\
                                em_st,\
                                em_end,\
                                modtype=5,\
                                datapath="../enkf_new_2010/enkf_output/" \
                                ):

    # read in the GEOS-Chem model data

    sdate = r'%4.4d%2.2d%2.2d' % (yyyy, mm, dd)
    figdoy = tm.day_of_year(yyyy, mm, dd)
    # sdoy=r'%3.3d' % (doy)
    sem = r'ST%3.3d.EN%4.4d-EN%4.4d' % (step, em_st, em_end)
    full_flnm = datapath + "/" + "ts_satellite" + "." + sem + "." + sdate + ".bpch"
    ncflnm = datapath + "/" + "ts_satellite" + "." + sdate + ".nc"

    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 co2
    # CO2 profile

    # extract co2 data set

    categorys = None
    tracers = None
    tranames = ['CO2']
    taus = None
    data_list = None
    data_list, founded = bpch2_ts.get_data(categorys, tracers, taus, tranames)

    all_data = list()
    for bpdata in data_list:

        all_data.append(bpdata.data)

    all_data = npy.array(all_data)
    print npy.shape(all_data)
    print 'save'

    all_data = 1.0e10 * all_data
    all_data = all_data - 100.0e4
    all_data = all_data.astype(integer)
    fd = gzip.open('test2.bin', 'wb')
    fd.write(all_data)

    npy.save('test_npy', all_data)
    print 'Done 1'

    svt.save_array(all_data)
    print 'Done 2'
コード例 #10
0
def create_new_input_file(run_step,\
                          YYYY,\
                          DOY,\
                          pbuse,\
                          pb_start=1,\
                          member_start=1, \
                          member_end=12, \
                          co2flnm='CO2_EMISSION_EN',\
                          tmpfile="input.geos.temp", \
                          newfile="input.geos.new" , \
                          em_step=None, \
                          em_doy=None,\
                          em_yyyy=None,\
                          em_pbuse=None,\
                          do_bk_run=4,\
                          data_path=gcdf.data_path,\
                          run_path=gcdf.run_path,\
                          **keywords \
                          ):
    """ create the new input to drive the ensemble run
    YYYY: years of the time serier
    DOY:  doys of the time serier
    em_doy: the date need emission
    """

    ntime = size(YYYY)
    if ('time_start' in keywords):
        ts = keywords['time_start']
        syyyy = ts[0:4]
        yst = int(syyyy)
        dst = ts[4:7]
        dst = int(dst)

        # mmst=int(smm)
        # sdd=ts[6:8]
        # ddst=int(sdd)
        # dst=tm.day_of_year(yst, mmst, ddst)
    else:
        yst = YYYY[0]
        dst = DOY[0]

    tst = tm.doy_to_utc(dst, sec=0, yyyy=yst)
    tst = tst.replace('-', '')
    tst = tst.replace(':', '')
    print(tst)
    if (em_step == None):
        iday0 = DOY[0]
        for iday in DOY[1:]:
            if (iday > iday0):
                em_step = iday - iday0
                break
            else:
                iday0 = iday

    if ('time_end' in keywords):
        te = keywords['time_end']
        syyyy = te[0:4]
        yed = int(syyyy)
        smm = te[4:6]
        mmst = int(smm)
        sdd = te[6:8]
        ddst = int(sdd)
        ded = tm.day_of_year(yst, mmst, ddst)
    else:
        yed = YYYY[1]  #-1
        ded = DOY[1]  #-1

    # the endtime should be + timestep
    # yed, ded=tm.next_doy(yed, ded, em_step)

    tend = tm.doy_to_utc(ded, sec=0, yyyy=yed)
    tend = tend.replace('-', '')
    tend = tend.replace(':', '')
    print(tend)

    fin = open(tmpfile, "r")  # open a temporary input.geos
    lines = fin.readlines()
    print(len(lines))
    fin.close()
    fout = open(newfile, "w")  # write to new input.geos file
    section_start = 0
    colwidth = 25
    line_count = 0
    ntracer = member_end - member_start + 1
    # enafix=r'EN%4.4d-EN%4.4d' % (member_start, member_end)
    enafix = r'ST%3.3d.EN%4.4d-EN%4.4d' % (run_step, member_start, member_end)

    for line in lines:
        if ("SIMULATION MENU" in line):
            section_start = 1
            line_count = 0
            fout.write(line)
        elif ("TRACER MENU" in line):
            section_start = 2
            line_count = 0
            fout.write(line)
        elif ("ND51 MENU" in line):
            section_start = 3
            line_count = 0
            fout.write(line)
        elif ("OUTPUT MENU" in line):
            section_start = 10
            line_count = 0
            fout.write(line)
        elif ("ENSEMBLE MENU" in line):
            section_start = 4
            line_count = 0
            fout.write(line)
        elif ("DIAGNOSTIC MENU" in line):
            section_start = 5
            line_count = 0
            fout.write(line)
        elif ("GAMAP MENU" in line):
            section_start = 6
            line_count = 0
            fout.write(line)
        elif ("PROD & LOSS MENU" in line):
            section_start = 7
            line_count = 0
            fout.write(line)
        elif ("ND50 MENU" in line):
            section_start = 8
            line_count = 0
            fout.write(line)

        elif (section_start > 0):
            line_count = line_count + 1
            line_head = line[0:colwidth]
            line_left = line[colwidth:]
            if (section_start == 1):
                if (line_count == 1):
                    new_line = line_head + " " + tst + "   "
                    fout.write(new_line + "\n")
                elif (line_count == 2):
                    line_head = line[0:colwidth]
                    new_line = line_head + " " + tend + "   "
                    fout.write(new_line + "\n")
                elif (line_count == 3):
                    print(line_left)
                    line_left = line_left.replace('$RUNPATH', run_path)
                    new_line = line_head + line_left
                    fout.write(new_line)
                elif (line_count == 4):
                    print(line_left)
                    line_left = line_left.replace('STYYY.ENXXXX-ENXXXX',
                                                  enafix)
                    new_line = line_head + line_left
                    fout.write(new_line)
                elif (line_count == 6):
                    line_left = line_left.replace('$DATAPATH', data_path)
                    line_left = line_left.replace('STYYY.ENXXXX-ENXXXX',
                                                  enafix)
                    new_line = line_head + line_left
                    fout.write(new_line)
                elif (line_count == 13):
                    line_left = line_left.replace('$RUNPATH', run_path)
                    new_line = line_head + line_left
                    fout.write(new_line)
                elif (line[0:2] == '--'):
                    line_count = 0
                    section_start = 0
                    fout.write(line)
                else:
                    fout.write(line)
            elif (section_start == 2):  # tracer menu
                if (line_count == 1):
                    fout.write(line)
                elif (line_count == 2):
                    line_head = line[0:colwidth]
                    snum = r'%4d' % (ntracer)
                    new_line = line_head + " " + snum + "   " + "\n"
                    fout.write(new_line)
                elif (line_count == 3):
                    fout.write(line)
                elif (line_count == 4):
                    line_head = line[0:colwidth]
                    tracer_no = line[colwidth:colwidth + 5]
                    line_left = line[colwidth + 5:]
                    tracer_id = 1
                    tracers = list()

                    for itracer in range(member_end - member_start + 1):
                        st1 = r'Tracer #%3.3d ENSEM' % (itracer + member_start)
                        lst1 = len(st1)
                        new_head = st1[:] + ' ' * (colwidth - 1 - lst1) + ":"
                        st1 = r'%5d' % (tracer_id)
                        lst1 = len(st1)
                        new_tracer_no = (5 - lst1) * ' ' + st1
                        new_line = new_head + new_tracer_no + line_left
                        fout.write(new_line)
                        tracers.append(tracer_id)
                        tracer_id = tracer_id + 1

                elif (line[0:2] == '--'):
                    line_count = 0
                    section_start = 0
                    fout.write(line)
                else:
                    pass
            elif (section_start == 3):  # ND51 menu
                if (line_count == 1):
                    fout.write(line)
                elif (line_count == 2):
                    line_left = line_left.replace('$DATAPATH', data_path)
                    line_left = line_left.replace('STYYY.ENXXXX-ENXXXX',
                                                  enafix)
                    new_line = line_head + line_left
                    fout.write(new_line)
                elif (line_count == 4):
                    line_left = ""

                    for itracer in [1, member_end - member_start + 1]:
                        stracer = r' %d' % (itracer)
                        line_left = line_left + stracer

                    line_left = line_left + ' ' + '199'  # 196 198 199 200 201 output other information
                    new_line = line_head + line_left + ' \n'
                    print(new_line)
                    # tx=raw_input()

                    fout.write(new_line)

                elif (line[0:2] == '--'):
                    line_count = 0
                    section_start = 0
                    fout.write(line)
                else:
                    fout.write(line)
            elif (section_start == 4):  #ensemble  menu
                if (line_count == 1):
                    st1 = r' %d  ' % member_start
                    new_line = line_head + st1 + ' \n'
                    fout.write(new_line)
                elif (line_count == 2):
                    st1 = r' %d  ' % member_end
                    new_line = line_head + st1 + ' \n'
                    fout.write(new_line)
                elif (line_count == 3):
                    st1 = r' %d  ' % do_bk_run
                    new_line = line_head + st1 + ' \n'
                    fout.write(new_line)

                elif (line_count == 4):
                    st1 = r' %d ' % ntime
                    new_line = line_head + st1 + ' \n'
                    fout.write(new_line)
                elif (line_count == 5):
                    st1 = ""
                    if (em_doy == None):
                        em_doy = DOY
                    for doy in em_doy:
                        sdoy = r'%d' % doy
                        st1 = st1 + ' ' + sdoy
                    st1 = st1 + ' '
                    new_line = line_head + st1 + ' \n'
                    fout.write(new_line)
                elif (line_count == 6):
                    st1 = ""
                    if (em_yyyy == None):

                        for yyyy in YYYY:
                            syyyy = r'%d' % yyyy
                            st1 = st1 + ' ' + syyyy

                    else:

                        for yyyy in em_yyyy:
                            syyyy = r'%d' % yyyy
                            st1 = st1 + ' ' + syyyy

                    st1 = st1 + ' '
                    new_line = line_head + st1 + ' \n'
                    fout.write(new_line)

                elif (line_count == 7):
                    st1 = r' %d  ' % pb_start
                    new_line = line_head + st1 + ' \n'
                    fout.write(new_line)

                elif (line_count == 8):
                    st1 = ""
                    if (em_pbuse == None):
                        for pbuse in pbuse:
                            spbuse = r'%d' % pbuse
                            st1 = st1 + ' ' + spbuse

                    else:

                        for pbuse in em_pbuse:
                            spbuse = r'%d' % pbuse
                            st1 = st1 + ' ' + spbuse

                    st1 = st1 + ' '
                    new_line = line_head + st1 + ' \n'
                    fout.write(new_line)

                elif (line_count == 9):  # 'flnm'
                    new_line = line_head + ' ' + co2flnm.strip() + '\n'
                    fout.write(new_line)
                elif (line[0:2] == '--'):
                    line_count = 0
                    section_start = 0
                    fout.write(line)
            elif (section_start == 47):  #P/L  menu
                line_head = line[0:colwidth]
                tracer_no = line[colwidth:colwidth + 5]
                line_left = line[colwidth + 5:]
                tracer_id = 1
                tracers = list()

                if (line_count <= 3):
                    #  print line_count, line
                    fout.write(line)
                elif (line_count == 4):
                    st1 = r'%d' % (member_end - member_start + 1 + 5)
                    new_line = line_head + ' ' + st1 + ' \n'
                    fout.write(new_line)
                elif (line_count == 5):
                    fout.write(line)
                    for itracer in range(1, member_end - member_start + 1 + 5):
                        st1 = r'Prod/Loss Family #%d' % (itracer +
                                                         member_start)
                        lst1 = len(st1)
                        new_head = st1[:] + ' ' * (colwidth - 1 - lst1) + ":"
                        st1 = r'%5d' % (tracer_id)
                        lst1 = len(st1)
                        new_tracer_no = (5 - lst1) * ' ' + st1
                        line_left = 'PCO: CO%3.3d' % (itracer)
                        new_line = new_head + ' ' + line_left + '\n'
                        fout.write(new_line)
                elif (line[0:2] == '--'):
                    line_count = 0
                    section_start = 0
                    fout.write(line)
                else:
                    pass

            elif (section_start == 5):  #diagnostic  menu
                if (line_count == 1):
                    line_left = line_left.replace('$DATAPATH', data_path)
                    line_left = line_left.replace('STYYY.ENXXXX-ENXXXX',
                                                  enafix)
                    new_line = line_head + line_left
                    fout.write(new_line)
                elif (line[0:2] == '--'):
                    line_count = 0
                    section_start = 0
                    fout.write(line)
                else:
                    fout.write(line)
            elif (section_start == 6):  #gamap  menu
                if (line_count == 1):
                    line_left = line_left.replace('$DATAPATH', data_path)
                    line_left = line_left.replace('STYYY.ENXXXX-ENXXXX',
                                                  enafix)
                    new_line = line_head + line_left
                    fout.write(new_line)
                elif (line_count == 2):
                    line_left = line_left.replace('$DATAPATH', data_path)
                    line_left = line_left.replace('STYYY.ENXXXX-ENXXXX',
                                                  enafix)
                    new_line = line_head + line_left
                    fout.write(new_line)
                elif (line[0:2] == '--'):
                    line_count = 0
                    section_start = 0
                    fout.write(line)
                else:
                    fout.write(line)

            elif (section_start == 8):  # ND50 menu
                if (line_count == 1):
                    fout.write(line)
                elif (line_count == 2):
                    line_left = line_left.replace('$DATAPATH', data_path)
                    line_left = line_left.replace('STYYY.ENXXXX-ENXXXX',
                                                  enafix)
                    new_line = line_head + line_left
                    fout.write(new_line)
                elif (line_count == 4):
                    line_left = ""
                    for itracer in [1, member_end - member_start + 1]:
                        stracer = r' %d' % (itracer)
                        line_left = line_left + stracer

                    line_left = line_left + ' ' + '399'  # output other information
                    new_line = line_head + line_left + ' \n'
                    fout.write(new_line)

                elif (line[0:2] == '--'):
                    line_count = 0
                    section_start = 0
                    fout.write(line)
                else:
                    fout.write(line)

            elif (section_start == 10):  #output menu

                if (line_count == 1):
                    id_cnt = 0
                    yyyy_list = list()
                    mm_list = list()
                    dd_list = list()

                    for idoy in DOY:
                        cyyyy, cmm, cdd = tm.doy_to_time_array(
                            idoy, YYYY[id_cnt])
                        yyyy_list.append(cyyyy)
                        mm_list.append(cmm)
                        dd_list.append(cdd)
                        id_cnt = id_cnt + 1

                    yyyy_list = array(yyyy_list)
                    mm_list = array(mm_list)
                    dd_list = array(dd_list)

                if ((line_count >= 1) and (line_count < 13)):
                    line_left = line_left.replace('3', '0')
                    sel_mm = where(mm_list == line_count)
                    # print size(sel_mm), sel_mm
                    if (size(sel_mm) > 0):
                        if (size(sel_mm) == 1):
                            sel_mm = [squeeze(sel_mm)]
                        else:
                            sel_mm = squeeze(sel_mm)

                        for isel in sel_mm:

                            sel_dd = dd_list[isel]
                            ispace = 0
                            # skip the line

                            for ichar in line_left:
                                if (ichar != ' '):
                                    break
                                else:
                                    ispace = ispace + 1

                            sel_dd = sel_dd + ispace

                            print(line_left)

                            if (sel_dd >= len(line_left)):
                                sel_dd = len(line_left) - 1

                            if (sel_dd == 1):
                                tline_1 = '3'

                            else:
                                tline_1 = line_left[0:sel_dd - 1]
                                tline_1 = tline_1 + '3'

                                tline_1 = tline_1 + line_left[sel_dd:]
                            line_left = tline_1

                    new_line = line_head + line_left
                    fout.write(new_line)
                elif (line[0:2] == '--'):  # the section end line
                    line_count = 0
                    section_start = 0
                    fout.write(line)

            else:
                fout.write(line)

        else:
            fout.write(line)

    fout.close()

    print('reach the end')