Ejemplo n.º 1
0
def read_emis(sector, pollutant, year, month):
    """
    Read emission data array.

    :param sector: (*Sector*) The sector.
    :param pollutant: (*Pollutant*) The pollutant.
    :param year: (*int*) The year.
    :param month: (*int*) The month.
    :returns: (*array*) Emission data array.
    """
    fn = get_emis_fn(sector, pollutant, year, month)
    if os.path.exists(fn):
        print('Emission data file: {}'.format(fn))
        f = addfile(fn)
        varnames = get_varnames(sector)
        if len(varnames) == 0:
            return None

        t = datetime.datetime(year, month, 15)
        data = None
        for vname in varnames:
            if sector == SectorEnum.AIR:
                dd = f[vname][month - 1]
                dd = dd.sum(axis=0)
            else:
                dd = f[vname][month - 1]
            if data is None:
                data = dd
            else:
                data = data + dd
        return data
    else:
        print('Alarm! Emission data file not exists: {}'.format(fn))
        return None
Ejemplo n.º 2
0
def read_emis(sector, pollutant, month):
    """
    Read emission data array.

    :param sector: (*Sector*) The sector.
    :param pollutant: (*Pollutant*) The pollutant.
    :param month: (*int*) The month.
    :returns: (*array*) Emission data array.
    """
    fn = get_emis_fn(sector, pollutant, month)
    f = addfile(fn)
    data = f['z'][:]
    nx = 800
    ny = 500
    data = data.reshape(ny, nx)
    data = data[::-1, :]
    return data
Ejemplo n.º 3
0
def read_emis(sector, pollutant, year, month):
    """
    Read emission data array.

    :param sector: (*Sector*) The sector.
    :param pollutant: (*Pollutant*) The pollutant.
    :param year: (*int*) The year.
    :param month: (*int*) The month.
    :returns: (*array*) Emission data array.
    """
    fn = get_emis_fn(sector, pollutant, year, month)
    if os.path.exists(fn):
        print('Emission data file: {}'.format(fn))
        f = addfile(fn)
        pollutant_name = pollutant.name.lower()
        if pollutant == PollutantEnum.PM2_5:
            pollutant_name = 'pm2.5'
        vname = 'emi_{}'.format(pollutant_name)
        data = f[vname][:]
        return data
    else:
        print('Alarm! Emission data file not exists: {}'.format(fn))
        return None
Ejemplo n.º 4
0
			fn_meic = os.path.join(dir_meic, 'emis_{}_{}_{}_hour.nc'.format(sector.name, year, month))
			#CAMS data file
			fn_cams = os.path.join(dir_cams, 'emis_{}_{}_{}_hour.nc'.format(sector.name, year, month))
			#HTAP data file
			fn_htap = os.path.join(dir_htap, 'emis_{}_{}_{}_hour.nc'.format(sector.name, 2010, month))
			#Output data file
			fn_out = os.path.join(dir_out, 'emis_{}_{}_{}_hour.nc'.format(sector.name, year, month))
		
			if os.path.exists(fn_meic) and (not os.path.exists(fn_cams)):
    			shutil.copyfile(fn_meic, fn_out)
                print('Data from MEIC...')
			elif os.path.exists(fn_cams) and (not os.path.exists(fn_meic)):
				shutil.copyfile(fn_cams, fn_out)
                print('Data from CAMS...')
			else:
				f_meic = addfile(fn_meic)
				f_cams = addfile(fn_cams)
				f_htap = addfile(fn_htap)
				#Create output netcdf file
				ncfile = addfile(fn_out, 'c', largefile=True)
				#Set global attribute
				gattrs = dict(Conventions='CF-1.6', Tools='Created using MeteoInfo')
				#Get all variable
				dimvars = []
				varnames = []
				for var in f_meic.variables():
					if var.ndim == 3:
						varnames.append(var.name)
						dimvar = DimVariable()
						dimvar.name = var.name
						dimvar.dtype = var.dtype
Ejemplo n.º 5
0
def run(year, month, dir_inter, emission, model_grid):
    """
    Process emission data by spatial allocation, temporal allocation
    and chemical speciation except VOC pollution.

    :param year: (*int*) Year.
    :param month: (*int*) Month.
    :param dir_inter: (*string*) Data output path.
    :param emission: (*module*) Emission module.
    :param model_grid: (*GridDesc*) Model data grid describe.
    """
    #Set profile files
    temp_profile_fn = os.path.join(ge_data_dir, 'amptpro.m3.default.us+can.txt')
    temp_ref_fn = os.path.join(ge_data_dir, 'amptref.m3.us+can.cair.txt')
    spec_profile_fn = os.path.join(ge_data_dir, 'gspro.cmaq.radm2p25_rev.txt')
    spec_ref_fn = os.path.join(ge_data_dir, 'gsref.cmaq.radm2p25.txt')       
    
    #Set dimensions
    tdim = np.dimension(np.arange(24), 'hour')
    ydim = np.dimension(model_grid.y_coord, 'lat', 'Y')
    xdim = np.dimension(model_grid.x_coord, 'lon', 'X')
    dims = [tdim, ydim, xdim]
    
    #Set sectors and pollutants
    sectors = [SectorEnum.INDUSTRY, SectorEnum.AGRICULTURE, SectorEnum.ENERGY,
        SectorEnum.RESIDENTIAL, SectorEnum.TRANSPORT]
    pollutants = [PollutantEnum.BC, PollutantEnum.CO, PollutantEnum.NH3, \
        PollutantEnum.NOx, PollutantEnum.OC, PollutantEnum.PM2_5, \
        PollutantEnum.SO2, PollutantEnum.PMcoarse, PollutantEnum.PM10more]
    out_species = [SpeciesEnum.PEC, SpeciesEnum.CO, SpeciesEnum.NH3, \
        None, SpeciesEnum.POA, None, SpeciesEnum.SO2, SpeciesEnum.PMC, \
        SpeciesEnum.PMC]
    
    #Loop
    for sector in sectors:
        print('####################################')
        print(sector)
        print('####################################')
        
        #Get SCC
        scc = emis_util.get_scc(sector)
        
        #Get pollutant profiles
        pollutant_profiles = emips.chem_spec.read_file(spec_ref_fn, spec_profile_fn, scc)
        for pollutant, out_spec in zip(pollutants, out_species):
            print(pollutant)
    
            print('Read emission data...')
            emis_data = emission.read_emis(sector, pollutant, month)
            
            #### Spatial allocation        
            print('Convert emission data untis from Mg/grid/month to g/m2/month...')
            emis_data = emis_data * 1e6 / emission.grid_areas
            
            print('Spatial allocation of emission grid to model grid...')
            emis_data = transform(emis_data, emission.emis_grid, model_grid)
            
            #### Temporal allocation
            print('Temporal allocation...')
            month_profile, week_profile, diurnal_profile, diurnal_profile_we = \
                emips.temp_alloc.read_file(temp_ref_fn, temp_profile_fn, scc)
            print('To daily emission (g/m2/day)...')
            weekday_data, weekend_data = emips.temp_alloc.week_allocation(emis_data, week_profile, year, month)
            print('To hourly emission (g/m2/s)...')
            hour_data = emips.temp_alloc.diurnal_allocation(weekday_data, diurnal_profile) / 3600        
    
            #### Chemical speciation
            poll_prof = emips.chem_spec.get_pollutant_profile(pollutant_profiles, pollutant)
            if (pollutant == PollutantEnum.NOx) and (poll_prof is None):
                poll_prof = PollutantProfile(pollutant)
                poll_prof.append(SpeciesProfile(pollutant, Species('NO'), 0.9, 1.0, 0.9))
                poll_prof.append(SpeciesProfile(pollutant, Species('NO2'), 0.1, 1.0, 0.1))
            outfn = os.path.join(dir_inter, \
                '{}_emis_{}_{}_{}_hour.nc'.format(pollutant.name, sector.name, year, month))
            print outfn
            if poll_prof is None:
                #### Save hourly emission data
                print('Save hourly emission data...')  
                if out_spec.molar_mass is None:                      
                    attrs = dict(units='g/m2/s')
                else:
                    attrs = dict(units='mole/m2/s')
                    print('To (mole/m2/s)')
                    hour_data = hour_data / out_spec.molar_mass
                dataset.ncwrite(outfn, hour_data, out_spec.name, dims, attrs)
            else:
                print('Chemical speciation...')
                specs = poll_prof.get_species()
                gattrs = dict(Conventions='CF-1.6', Tools='Created using MeteoInfo')
                dimvars = []
                for spec in specs:
                    dimvar = dataset.DimVariable()
                    dimvar.name = spec.name
                    dimvar.dtype = np.dtype.float
                    dimvar.dims = dims
                    if spec.molar_mass is None:
                        dimvar.addattr('units', 'g/m2/s')
                    else:
                        dimvar.addattr('units', 'mole/m2/s')
                    dimvars.append(dimvar)
                ncfile = dataset.addfile(outfn, 'c')
                ncfile.nc_define(dims, gattrs, dimvars)
                for spec_prof,dimvar,spec in zip(poll_prof.species_profiles, dimvars, specs):
                    print(dimvar.name)
                    spec_data = hour_data * spec_prof.mass_fraction
                    if not spec.molar_mass is None:
                        print('To (mole/m2/s)')
                        spec_data = spec_data / spec.molar_mass
                    ncfile.write(dimvar.name, spec_data)
                ncfile.close()
Ejemplo n.º 6
0
def run_transform(year, month, dir_inter, model_grid, out_species,
                  out_species_aer):
    """
    Distribution of particulate matter and change unit.

    :param year: (*int*) Year.
    :param month: (*int*) Month.
    :param dir_inter: (*string*) The directory where data is stored.
    :param model_grid: (*GridDesc*) Model data grid describe.
    :param out_species: (*list*) The name of the output species(gases and aerosol).
    :param out_species_aer: (*list*) The name of the output species(aerosol).
    """
    print('Add input file...')
    fn_in = dir_inter + '\emis_{}_{}_hour.nc'.format(year, month)
    f_in = dataset.addfile(fn_in)
    #Set example data
    rdata = f_in[f_in.varnames()[4]][:, :, :]
    rdata[rdata != np.nan] = 0.

    #Set dimension
    print('Define dimensions and global attributes...')
    tdim = np.dimension(np.arange(24), 'Time')
    ydim = np.dimension(model_grid.y_coord, 'south_north', 'Y')
    xdim = np.dimension(model_grid.x_coord, 'west_east', 'X')
    dims = [tdim, ydim, xdim]
    gattrs = OrderedDict()
    gattrs['Conventions'] = 'CF-1.6'
    gattrs['Tools'] = 'Created using MeteoInfo'

    #Set the definition of the output variable and ncfile
    fn_out = dir_inter + '\emis_{}_{}_hour_transform.nc'.format(year, month)
    print('Define variables...')
    dimvars = []
    for out_specie in out_species:
        dimvar = dataset.DimVariable()
        dimvar.name = out_specie
        dimvar.dtype = np.dtype.float
        dimvar.dims = dims
        dimvar.addattr('FieldType', '104')
        dimvar.addattr('MemoryOrder', "XYZ")
        dimvar.addattr('description', "EMISSION_{}".format(out_specie[2:]))
        if out_specie in out_species_aer:
            #g/m2/s to ug/m^3 m/s
            dimvar.addattr('units', 'ug/m3 m/s')
        else:
            #mole/m2/s to mol/km^2/hr
            dimvar.addattr('units', 'mol km^-2 hr^-1')
        dimvar.addattr('stagger', "")
        dimvar.addattr('coordinates', "XLONG XLAT XTIME")
        #dimvar.addattr('_ChunkSizes', '1U, 3U, 137U, 167U')
        dimvars.append(dimvar)
    ncfile = dataset.addfile(fn_out, 'c', largefile=True)
    ncfile.nc_define(dims, gattrs, dimvars)

    #add data to ncfile
    print('Process data and write to file...')
    for name in out_species:
        data = None
        sname = name[2:]
        print(sname)
        if sname in f_in.varnames():
            data = f_in[sname][:, :, :]
            data = data * 3600 * 1e6
            ncfile.write(name, data)
        elif sname == 'PM25I':
            data = f_in['PMFINE'][:, :, :]
            data = data * 1e6
            ncfile.write(name, data * 0.2)
        elif sname == 'PM25J':
            data = f_in['PMFINE'][:, :, :]
            data = data * 1e6
            ncfile.write(name, data * 0.8)
        elif sname == 'PM_10':
            data = f_in['PMC'][:, :, :]
            data = data * 1e6
            ncfile.write(name, data)
        elif sname == 'ECI':
            data = f_in['PEC'][:, :, :]
            data = data * 1e6
            ncfile.write(name, data * 0.2)
        elif sname == 'ECJ':
            data = f_in['PEC'][:, :, :]
            data = data * 1e6
            ncfile.write(name, data * 0.8)
        elif sname == 'ORGI':
            data = f_in['POA'][:, :, :]
            data = data * 1e6
            ncfile.write(name, data * 0.2)
        elif sname == 'ORGJ':
            data = f_in['POA'][:, :, :]
            data = data * 1e6
            ncfile.write(name, data * 0.8)
        elif sname == 'SO4I':
            data = f_in['PSO4'][:, :, :]
            data = data * 1e6
            ncfile.write(name, data * 0.2)
        elif sname == 'SO4J':
            data = f_in['PSO4'][:, :, :]
            data = data * 1e6
            ncfile.write(name, data * 0.8)
        elif sname == 'NO3I':
            data = f_in['PNO3'][:, :, :]
            data = data * 1e6
            ncfile.write(name, data * 0.2)
        elif sname == 'NO3J':
            data = f_in['PNO3'][:, :, :]
            data = data * 1e6
            ncfile.write(name, data * 0.8)
        else:
            ncfile.write(name, rdata)
    ncfile.close()
    print('Distribution of particulate matter and change unit finised!')
Ejemplo n.º 7
0
def run_proj(year, month, dir_inter, model_grid, target_grid, out_species,
             out_species_aer, global_attributes, z):
    """
    Write Times variable, add global attributes, convert data's projection.
	io_style_emissions = 2

    :param year: (*int*) Year.
    :param month: (*int*) Month.
    :param dir_inter: (*string*) The directory where data is stored.
    :param model_grid: (*GridDesc*) Model data grid describe.
    :param target_grid: (*GridDesc*) Target data grid describe.
    :param out_species: (*list*) The name of the output species(gases and aerosol).
    :param out_species_aer: (*list*) The name of the output species(aerosol).
    :param global_attributes: (*OrderedDict*) The global attributes of the output file.
    :param z: (*int*) The zdim of the output data.
    """
    print('Add input file...')
    fn_in = dir_inter + '\emis_{}_{}_hour_transform.nc'.format(year, month)
    print(fn_in)
    f_in = dataset.addfile(fn_in)
    #set dimension
    tdim = np.dimension(np.arange(24), 'Time')
    ydim = np.dimension(target_grid.y_coord, 'south_north', 'Y')
    xdim = np.dimension(target_grid.x_coord, 'west_east', 'X')
    zdim = np.dimension(np.arange(z), 'emissions_zdim')
    sdim = np.dimension(np.arange(19), 'DateStrLen')
    dims = [tdim, zdim, ydim, xdim]
    all_dims = [tdim, sdim, xdim, ydim, zdim]

    fn_out = dir_inter + '\wrfchemi_d01_{}-{:0>2d}'.format(year, month)

    #set variables
    dimvars = []

    dimvar = dataset.DimVariable()
    dimvar.name = 'Times'
    dimvar.dtype = np.dtype.char
    dimvar.dims = [tdim, sdim]
    #dimvar.addattr('_ChunkSizes', [1, 19])
    dimvars.append(dimvar)

    for out_specie in out_species:
        dimvar = dataset.DimVariable()
        dimvar.name = out_specie
        dimvar.dtype = np.dtype.float
        dimvar.dims = dims
        dimvar.addattr('FieldType', 104)
        dimvar.addattr('MemoryOrder', "XYZ")
        dimvar.addattr('description', "EMISSION_{}".format(out_specie[2:]))
        if out_specie in out_species_aer:
            #g/m2/s to ug/m^3 m/s
            dimvar.addattr('units', 'ug/m3 m/s')
        else:
            #mole/m2/s to mol/km^2/hr
            dimvar.addattr('units', 'mol km^-2 hr^-1')
        dimvar.addattr('stagger', "")
        dimvar.addattr('coordinates', "XLONG XLAT XTIME")
        #dimvar.addattr('_ChunkSizes', [1, 3, 137, 167])
        dimvars.append(dimvar)

    print('Create output data file...')
    print(fn_out)
    ncfile = dataset.addfile(fn_out, 'c', largefile=True)
    print('Define dimensions, global attributes and variables...')
    ncfile.nc_define(all_dims, global_attributes, dimvars, write_dimvars=False)

    #Times
    print('Write Times variable...')
    s_out = []
    for i in range(24):
        s = '{}-{:0>2d}-01_{:0>2d}:00:00'.format(year, month, i)
        s_out.append(s)
    s_out = np.array(s_out, dtype=np.dtype.char)
    ncfile.write('Times', s_out)

    print('Write variable data except times...')
    for out_specie in out_species:
        data = np.zeros((tdim.length, zdim.length, ydim.length, xdim.length))
        if out_specie in f_in.varnames():
            print(out_specie)
            dd = f_in[out_specie][:]
            #Conversion proj
            dd = transform(dd, model_grid, target_grid)
            #Set default values
            dd[dd == np.nan] = 0
            data[:, :, :, :] = dd
            ##########test############
            #data[:, 1:8, :, :] = 0
            ##########test############
            ncfile.write(out_specie, data)
    f_in.close()
    ncfile.close()
    print('Convert projection finished!')
Ejemplo n.º 8
0
def run_allocate(year, month, dir_inter, model_grid, sectors, z):
    """
    Assign data to different heights.
    If not specified, data is allocated to the first layer.

    :param year: (*int*) Year.
    :param month: (*int*) Month.
    :param dir_inter: (*string*) The directory where data is stored.
    :param model_grid: (*GridDesc*) Model data grid describe.
    :param sectors: (*GridDesc*) The sectors need to be processed.
    :param z: (*int*) The zdim of the output data.
    """
    print('Define dimension and global attributes...')
    tdim = np.dimension(np.arange(24), 'hour')
    ydim = np.dimension(model_grid.y_coord, 'lat', 'Y')
    xdim = np.dimension(model_grid.x_coord, 'lon', 'X')
    zdim = np.dimension(np.arange(z), 'emissions_zdim')
    dims = [tdim, zdim, ydim, xdim]

    gattrs = OrderedDict()
    gattrs['Conventions'] = 'CF-1.6'
    gattrs['Tools'] = 'Created using MeteoInfo'

    for sector in sectors:
        fn = dir_inter + '\emis_{}_{}_{}_hour.nc'.format(sector, year, month)
        print('File input: {}'.format(fn))
        dimvars = []
        if os.path.exists(fn):
            f = dataset.addfile(fn)
            for var in f.varnames():
                if var == 'lat' or var == 'lon':
                    continue
                else:
                    dimvar = dataset.DimVariable()
                    dimvar.name = var
                    dimvar.dtype = np.dtype.float
                    dimvar.dims = dims
                    dimvar.addattr('description', "EMISSION_{}".format(var))
                    if var in out_species_unit:
                        dimvar.addattr('units', 'g/m2/s')
                    else:
                        dimvar.addattr('units', 'mole/m2/s')
                    dimvars.append(dimvar)

            print('Create output data file...')
            out_fn = dir_inter + '\emis_{}_{}_{}_hour_height.nc'.format(
                sector, year, month)
            ncfile = dataset.addfile(out_fn, 'c', largefile=True)
            ncfile.nc_define(dims, gattrs, dimvars)

            data = np.zeros((tdim.length, z, ydim.length, xdim.length))
            dd = np.zeros((tdim.length, z, ydim.length, xdim.length))
            #read, merge and output
            if sector in sectors_al:
                print('Allocating: {}'.format(sector))
            else:
                print('Do not need to be allocated: {}'.format(sector))
            print('Write data to file...')
            for var in f.varnames():
                if var == 'lat' or var == 'lon':
                    continue
                else:
                    print(var)
                    dd[:, 0, :, :] = f[var][:]
                    if sector in sectors_al:
                        if sector == 'energy':
                            data[:, 1, :, :] = dd[:, 0, :, :] * 0.1
                            data[:, 2, :, :] = dd[:, 0, :, :] * 0.1
                            data[:, 3, :, :] = dd[:, 0, :, :] * 0.3
                            data[:, 4, :, :] = dd[:, 0, :, :] * 0.2
                            data[:, 5, :, :] = dd[:, 0, :, :] * 0.2
                            data[:, 6, :, :] = dd[:, 0, :, :] * 0.1
                        if sector == 'industry':
                            data[:, 0, :, :] = dd[:, 0, :, :] * 0.5
                            data[:, 1, :, :] = dd[:, 0, :, :] * 0.3
                            data[:, 2, :, :] = dd[:, 0, :, :] * 0.2
                    else:
                        data[:, 0, :, :] = dd[:, 0, :, :]
                    #Turn nan to zero
                    data[data == np.nan] = 0
                    ###test###
                    '''
                    if sector == 'energy':
                        data[:, 0, :, :] = 0  
                    '''

###test###
                ncfile.write(var, data)
            ncfile.close()
            f.close()
        else:
            print('File not exist: {}'.format(fn))
            continue
    print('Allocate of height finished!')
Ejemplo n.º 9
0
def run(year, month, dir_inter):
    """
    Merge all pollutant emission files in one file for each sector.

    :param year: (*int*) Year.
    :param month: (*int*) Month.
    :param dir_inter: (*string*) Data input and output path.
    """
    #Set sectors and pollutants
    sectors = [SectorEnum.INDUSTRY, SectorEnum.AGRICULTURE, SectorEnum.ENERGY, \
        SectorEnum.RESIDENTIAL, SectorEnum.TRANSPORT, SectorEnum.SHIPS, \
        SectorEnum.AIR]
    pollutants = [PollutantEnum.BC, PollutantEnum.CH4, PollutantEnum.CO, \
        PollutantEnum.NH3, PollutantEnum.NOx, PollutantEnum.OC, PollutantEnum.PM2_5, \
        PollutantEnum.SO2, PollutantEnum.PM10, PollutantEnum.NMVOC]

    #Set model grids
    proj = geolib.projinfo()
    model_grid = GridDesc(proj,
                          x_orig=70.,
                          x_cell=0.15,
                          x_num=502,
                          y_orig=15.,
                          y_cell=0.15,
                          y_num=330)
    #Set dimensions
    tdim = np.dimension(np.arange(24), 'hour')
    ydim = np.dimension(model_grid.y_coord, 'lat', 'Y')
    xdim = np.dimension(model_grid.x_coord, 'lon', 'X')
    dims = [tdim, ydim, xdim]

    #Sector loop
    for sector in sectors:
        print('Sector: {}'.format(sector))

        #Set output sector emission file name
        outfn = os.path.join(dir_inter, \
            'emis_{}_{}_{}_hour.nc'.format(sector, year, month))
        print('Sector emission file: {}'.format(outfn))

        #Pollutant loop
        dimvars = []
        dict_spec = {}
        for pollutant in pollutants:
            #Read data in pollutant file
            if pollutant == PollutantEnum.NMVOC:
                fn = os.path.join(dir_inter, \
                    '{}_emis_lump_{}_{}_{}_hour.nc'.format(pollutant.name, \
                    sector.name, year, month))
            else:
                fn = os.path.join(dir_inter, \
                    '{}_emis_{}_{}_{}_hour.nc'.format(pollutant.name, \
                    sector.name, year, month))
            if not os.path.exists(fn):  #No emission data
                print('\tAlarm! The file not exists: {}'.format(fn))
                continue

            print('\t{}'.format(fn))
            f = dataset.addfile(fn)
            for var in f.variables():
                if var.ndim == 3:
                    if dict_spec.has_key(var.name):
                        dict_spec[var.name].append(fn)
                    else:
                        dimvars.append(var)
                        dict_spec[var.name] = [fn]

        #Create output merged netcdf data file
        gattrs = dict(Conventions='CF-1.6', Tools='Created using MeteoInfo')
        ncfile = dataset.addfile(outfn, 'c')
        ncfile.nc_define(dims, gattrs, dimvars)
        for sname, fns in dict_spec.iteritems():
            spec_data = None
            for fn in fns:
                f = dataset.addfile(fn)
                if spec_data is None:
                    spec_data = f[sname][:]
                else:
                    spec_data = spec_data + f[sname][:]
                ncfile.write(sname, spec_data)
        ncfile.close()
Ejemplo n.º 10
0
def run_merge(year, month, dir_inter, model_grid, sectors):
    """
    Combine all sectors into one file

    :param year: (*int*) Year.
    :param month: (*int*) Month.
    :param dir_inter: (*string*) The directory where data is stored.
    :param model_grid: (*GridDesc*) Model data grid describe.
    :param sectors: (*list*) Sectors that needs to be merged.
    """
    #Set the definition of the output variable
    print('Define variables...')
    dimvars = []
    dict_spec = {}
    for sector in sectors:
        fn = dir_inter + '\emis_{}_{}_{}_hour.nc'.format(sector, year, month)
        if os.path.exists(fn):
            f = dataset.addfile(fn)
            for var in f.variables():
                if var.ndim == 3:
                    if dict_spec.has_key(var.name):
                        dict_spec[var.name].append(fn)
                    else:
                        dimvars.append(var)
                        dict_spec[var.name] = [fn]
            f.close()
        else:
            print('File not exist: {}'.format(fn))
            continue
    #Set dimension and define ncfile
    print('Define dimension and global attributes...')
    tdim = np.dimension(np.arange(24), 'hour')
    ydim = np.dimension(model_grid.y_coord, 'lat', 'Y')
    xdim = np.dimension(model_grid.x_coord, 'lon', 'X')
    dims = [tdim, ydim, xdim]
    out_fn = dir_inter + '\emis_{}_{}_hour.nc'.format(year, month)
    gattrs = OrderedDict()
    gattrs['Conventions'] = 'CF-1.6'
    gattrs['Tools'] = 'Created using MeteoInfo'
    print('Create output data file...')
    ncfile = dataset.addfile(out_fn, 'c', largefile=True)
    ncfile.nc_define(dims, gattrs, dimvars)
    #read, merge and output
    print('Write variable data...')
    for sname, fns in dict_spec.iteritems():
        print(sname)
        spec_data = None
        dd = None
        for fn in fns:
            f = dataset.addfile(fn)
            dd = f[sname][:]
            #turn nan to zero
            dd[dd == np.nan] = 0
            if spec_data is None:
                spec_data = dd
            else:
                spec_data = spec_data + dd
            f.close()
        ncfile.write(sname, spec_data)
    ncfile.close()
    print('Merge sector data finished!')
Ejemplo n.º 11
0
def run(year, month, dir_inter, emission, model_grid):
    """
    Process VOC emission data by spatial allocation, temporal allocation
    and chemical speciation.

    :param year: (*int*) Year.
    :param month: (*int*) Month.
    :param dir_inter: (*string*) Data output path.
    :param emission: (*module*) Emission module.
    :param model_grid: (*GridDesc*) Model data grid describe.
    """
    #Set profile files
    temp_profile_fn = os.path.join(ge_data_dir,
                                   'amptpro.m3.default.us+can.txt')
    temp_ref_fn = os.path.join(ge_data_dir, 'amptref.m3.us+can.cair.txt')

    #Set dimensions
    tdim = np.dimension(np.arange(24), 'hour')
    ydim = np.dimension(model_grid.y_coord, 'lat', 'Y')
    xdim = np.dimension(model_grid.x_coord, 'lon', 'X')
    dims = [tdim, ydim, xdim]

    #Set sectors and pollutants
    sectors = [
        SectorEnum.INDUSTRY, SectorEnum.AGRICULTURE, SectorEnum.ENERGY,
        SectorEnum.RESIDENTIAL, SectorEnum.TRANSPORT
    ]
    fn_sectors = ['inc', 'agr', 'pow', 'res', 'tra']
    pollutant = PollutantEnum.NMVOC
    pollutant.units = Units(Weight.MG, Area.GRID, Period.MONTH)

    #Loop
    for sector, fn_sector in zip(sectors, fn_sectors):
        print('####################################')
        print(sector)
        print('####################################')

        #Get SCC
        scc = emis_util.get_scc(sector)

        print('Read emission data...')
        emis_data = emission.read_emis(sector, pollutant, month)

        #### Spatial allocation
        print(
            'Convert emission data untis from Mg/grid/month to g/m2/month...')
        emis_data = emis_data * 1e6 / emission.grid_areas

        print('Spatial allocation of emission grid to model grid...')
        emis_data = transform(emis_data, emission.emis_grid, model_grid)

        #### Temporal allocation
        print('Temporal allocation...')
        month_profile, week_profile, diurnal_profile, diurnal_profile_we = \
            emips.temp_alloc.read_file(temp_ref_fn, temp_profile_fn, scc)
        print('To daily emission (g/m2/day)...')
        weekday_data, weekend_data = emips.temp_alloc.week_allocation(
            emis_data, week_profile, year, month)
        print('To hourly emission (g/m2/s)...')
        hour_data = emips.temp_alloc.diurnal_allocation(
            weekday_data, diurnal_profile) / 3600

        #### Chemical speciation
        print('Chemical speciation...')
        outfn = os.path.join(dir_inter, \
            '{}_emis_{}_{}_{}_hour.nc'.format(pollutant.name, sector, year, month))
        print('Output file: {}'.format(outfn))

        print('Set grid speciation data...')
        fn = r'Z:\chen\MEIC_data\Grid_speciation_data(VOC)\retro_nmvoc_ratio_{}_2000_0.1deg.nc'.format(
            fn_sector)
        print('Grid speciation file: {}'.format(fn))
        f = dataset.addfile(fn)

        #Create output netcdf file and define dimensions, global attributes and variables
        gattrs = dict(Conventions='CF-1.6', Tools='Created using MeteoInfo')
        dimvars = []
        for var in f.variables():
            if var.ndim == 2:
                dimvar = dataset.DimVariable()
                dimvar.name = var.name
                dimvar.dtype = np.dtype.float
                dimvar.dims = dims
                dimvar.addattr('units', 'g/m2/s')
                dimvars.append(dimvar)
        ncfile = dataset.addfile(outfn, 'c')
        ncfile.nc_define(dims, gattrs, dimvars)

        #Write variable values
        ratio_grid = GridDesc(x_orig=0.05,
                              x_cell=0.1,
                              x_num=3600,
                              y_orig=-89.95,
                              y_cell=0.1,
                              y_num=1800)
        for dimvar in dimvars:
            print(dimvar.name)
            rdata = f[dimvar.name][:]
            rdata = transform(rdata, ratio_grid, model_grid)
            spec_data = hour_data * rdata
            ncfile.write(dimvar.name, spec_data)

        #Close output netcdf file
        ncfile.close()
Ejemplo n.º 12
0
def run_merge(year, month, dir_inter, model_grid, sectors, z):
    """
    Combine all sectors into one file

    :param year: (*int*) Year.
    :param month: (*int*) Month.
    :param dir_inter: (*string*) The directory where data is stored.
    :param model_grid: (*GridDesc*) Model data grid describe.
    :param sectors: (*list*) Sectors that needs to be merged.
    :param z: (*int*) The zdim of the output data.
    """
    print('Define dimension and global attributes...')
    tdim = np.dimension(np.arange(24), 'hour')
    zdim = np.dimension(np.arange(z), 'emissions_zdim')
    ydim = np.dimension(model_grid.y_coord, 'lat', 'Y')
    xdim = np.dimension(model_grid.x_coord, 'lon', 'X')
    dims = [tdim, zdim, ydim, xdim]
    #Set the definition of the output variable
    print('Define variables...')
    dimvars = []
    dict_spec = {}
    print('Add files...')
    for sector in sectors:
        fn = dir_inter + '\emis_{}_{}_{}_hour_height.nc'.format(sector, year, month)
        if os.path.exists(fn):
            print(fn)
            f = dataset.addfile(fn)
            for var in f.variables():
                if var.ndim == 4:
                    if dict_spec.has_key(var.name):
                        dict_spec[var.name].append(fn)
                    else:
                        dict_spec[var.name] = [fn]
            for var in f.varnames():
                if var == 'lat' or var == 'lon':
                        continue
                else:
                    dimvar = dataset.DimVariable()
                    dimvar.name = var
                    dimvar.dtype = np.dtype.float
                    dimvar.dims = dims
                    dimvar.addattr('description', "EMISSION_{}".format(var))
                    if var in out_species_aer:
                        dimvar.addattr('units', 'g/m2/s')
                    else:
                        dimvar.addattr('units', 'mole/m2/s')
                dimvars.append(dimvar)   
            f.close()
        else:
            print('File not exist: {}'.format(fn))
            continue
            
    #Set dimension and define ncfile 
    out_fn = dir_inter + '\emis_{}_{}_hour.nc'.format(year, month)
    gattrs = OrderedDict()
    gattrs['Conventions'] = 'CF-1.6'
    gattrs['Tools'] = 'Created using MeteoInfo'
    print('Create output data file...')
    ncfile = dataset.addfile(out_fn, 'c', largefile=True)
    ncfile.nc_define(dims, gattrs, dimvars)
    #read, merge and output
    print('Write variables data...')
    for sname, fns in dict_spec.iteritems():
        print(sname)
        spec_data = np.zeros((tdim.length, z, ydim.length, xdim.length))
        dd = np.zeros((tdim.length, z, ydim.length, xdim.length))
        for fn in fns:
            f = dataset.addfile(fn)
            dd = f[sname][:]
            #turn nan to zero
            dd[dd==np.nan] = 0.0
            if spec_data.sum() == 0.0:
                spec_data = dd
            else:
                spec_data = spec_data + dd
            f.close()
        ncfile.write(sname, spec_data)
    ncfile.close()
    print('Merge data finished!')
Ejemplo n.º 13
0
def run(year, month, dir_inter, model_grid):
    """
    Merge all pollutant emission files in one file for each sector.

    :param year: (*int*) Year.
    :param month: (*int*) Month.
    :param dir_inter: (*string*) Data input and output path.
    :param model_grid: (*GridDesc*) Model data grid describe.
    """
    #Set sectors and pollutants
    sectors = [
        SectorEnum.INDUSTRY, SectorEnum.AGRICULTURE, SectorEnum.ENERGY,
        SectorEnum.RESIDENTIAL, SectorEnum.TRANSPORT
    ]
    pollutants = [PollutantEnum.BC, PollutantEnum.CO, PollutantEnum.NH3, \
        PollutantEnum.NOx, PollutantEnum.OC, PollutantEnum.PM2_5, \
        PollutantEnum.SO2, PollutantEnum.PMcoarse, PollutantEnum.PM10more, \
        PollutantEnum.NMVOC]

    #Set dimensions
    tdim = np.dimension(np.arange(24), 'hour')
    ydim = np.dimension(model_grid.y_coord, 'lat', 'Y')
    xdim = np.dimension(model_grid.x_coord, 'lon', 'X')
    dims = [tdim, ydim, xdim]

    #Sector loop
    for sector in sectors:
        print('Sector: {}'.format(sector))

        #Set output sector emission file name
        outfn = os.path.join(dir_inter, \
            'emis_{}_{}_{}_hour.nc'.format(sector.name, year, month))
        print('Sector emission file: {}'.format(outfn))

        #Pollutant loop
        dimvars = []
        dict_spec = {}
        for pollutant in pollutants:
            #Read data in pollutant file
            if pollutant == PollutantEnum.NMVOC:
                fn = os.path.join(dir_inter, \
                    '{}_emis_lump_{}_{}_{}_hour.nc'.format(pollutant.name, \
                    sector.name, year, month))
            else:
                fn = os.path.join(dir_inter, \
                    '{}_emis_{}_{}_{}_hour.nc'.format(pollutant.name, \
                    sector.name, year, month))
            print('\t{}'.format(fn))
            #Determine whether the PM10more file exists
            if os.path.exists(fn):
                f = dataset.addfile(fn)
            else:
                print('File not exist: {}'.format(fn))
                continue

            for var in f.variables():
                if var.ndim == 3:
                    if dict_spec.has_key(var.name):
                        dict_spec[var.name].append(fn)
                    else:
                        dimvars.append(var)
                        dict_spec[var.name] = [fn]

        #Create output merged netcdf data file
        gattrs = dict(Conventions='CF-1.6', Tools='Created using MeteoInfo')
        ncfile = dataset.addfile(outfn, 'c', largefile=True)
        ncfile.nc_define(dims, gattrs, dimvars)
        for sname, fns in dict_spec.iteritems():
            spec_data = None
            for fn in fns:
                f = dataset.addfile(fn)
                if spec_data is None:
                    spec_data = f[sname][:]
                else:
                    spec_data = spec_data + f[sname][:]
            ncfile.write(sname, spec_data)
        ncfile.close()
Ejemplo n.º 14
0
def run(year, month, dir_inter, chem_mech, model_grid):
    """
    Lump VOC species according chemical mechanism.

    :param year: (*int*) Year.
    :param month: (*int*) Month.
    :param dir_inter: (*string*) Data input and output path.
    :param chem_mech: (*ChemicalMechanism*) Chemical mechanism.
    :param model_grid: (*GridDesc*) Model data grid describe.
    """
    #Set sectors and pollutants
    sectors = [SectorEnum.INDUSTRY, SectorEnum.AGRICULTURE, SectorEnum.ENERGY,
        SectorEnum.RESIDENTIAL, SectorEnum.TRANSPORT]
    pollutant = PollutantEnum.NMVOC

    #Set dimensions
    tdim = np.dimension(np.arange(24), 'hour')
    ydim = np.dimension(model_grid.y_coord, 'lat', 'Y')
    xdim = np.dimension(model_grid.x_coord, 'lon', 'X')
    dims = [tdim, ydim, xdim]
    
    #Sector loop
    for sector in sectors:
        print('####################################')
        print(sector)
        print('####################################')
        
        #Set input file
        infn = os.path.join(dir_inter, \
            '{}_emis_{}_{}_{}_hour.nc'.format(pollutant.name, sector.name, year, month))
        print('Input file: {}'.format(infn))
        #Open input file
        inf = dataset.addfile(infn)
        #Read a reference data
        vname = inf.varnames()[4]
        rdata = inf[vname][:]
        rdata[rdata!=np.nan] = 0.
    
        #Set output file
        outfn = os.path.join(dir_inter, \
            '{}_emis_lump_{}_{}_{}_hour.nc'.format(pollutant.name, sector.name, year, month))
        print('Output file: {}'.format(outfn))
        #Create output netcdf file
        ncfile = dataset.addfile(outfn, 'c')
        #Set global attribute
        gattrs = dict(Conventions='CF-1.6', Tools='Created using MeteoInfo')
        #Set variables
        dimvars = []
        for spec in chem_mech.nmvoc_species():
            dimvar = dataset.DimVariable()
            dimvar.name = spec.name
            dimvar.dtype = np.dtype.float
            dimvar.dims = dims
            #dimvar.addattr('units', 'mol/m2/s')
            if spec.molar_mass is None:
                dimvar.addattr('units', 'g/m2/s')
            else:
                dimvar.addattr('units', 'mole/m2/s')
            dimvars.append(dimvar)
        #Define dimensions, global attributes and variables
        ncfile.nc_define(dims, gattrs, dimvars)
    
        #Write variable values
        for spec, dimvar in zip(chem_mech.nmvoc_species(), dimvars):    
            print('{} species: {}'.format(chem_mech.name, spec))
            rspecs = chem_mech.lump_RETRO(spec)
            print('RETRO species: {}'.format(rspecs))
            data = None
            for rspec, ratio in rspecs.iteritems():
                if rspec.name in inf.varnames():
                    if data is None:
                        data = inf[rspec.name][:] * ratio
                    else:
                        data = data + inf[rspec.name][:] * ratio
            if data is None:
                print('No RETRO species!')
                ncfile.write(dimvar.name, rdata)
            else:
                if spec.molar_mass is not None:
                    print('Convert (g/m2/s) to (mole/m2/s)')
                    data = data / spec.molar_mass
                ncfile.write(dimvar.name, data)
    
        #Close output netcdf file
        ncfile.close()
Ejemplo n.º 15
0
def merge_output(year, months, model_grid, mechanism_name):
    """
    Merge EMIPS output emission data of MEIC CAMS and HTAP data
    Applicable to the situation that MEIC data are nan outside China, and 0 in Taiwan!!!

    :param year: (*int*) Year.
    :param months: (*list*) Months.
    :param model_grid: (*GridDesc*) Model data grid describe.
    :param mechanism_name: (*string*) mechanism's name.
    """
    print('-----------------------------------')
    print('----Merge output data(tw).....-----')
    print('-----------------------------------')
    #Set directories
    dir_meic1 = os.path.join(r'G:\test_data', mechanism_name, r'region_0.15\MEIC', str(year))
    dir_cams1 = os.path.join(r'G:\test_data', mechanism_name, r'region_0.15\CAMS', str(year))
    dir_htap1 = os.path.join(r'G:\test_data', mechanism_name, r'region_0.15\HTAP\2010')
    dir_out1 = os.path.join(r'G:\test_data', mechanism_name, r'region_0.15\merge', str(year))
    
    #Set sectors
    sectors = [SectorEnum.INDUSTRY, SectorEnum.AGRICULTURE, SectorEnum.ENERGY, \
        SectorEnum.RESIDENTIAL, SectorEnum.TRANSPORT, SectorEnum.SHIPS, \
        SectorEnum.AIR]
        
    #Set dimensions
    tdim = np.dimension(np.arange(24), 'hour')
    ydim = np.dimension(model_grid.y_coord, 'lat', 'Y')
    xdim = np.dimension(model_grid.x_coord, 'lon', 'X')
    dims = [tdim, ydim, xdim]
    
	#Add data of taiwan area use CAMS/HTAP data
    tw = geolib.shaperead('taiwan.shp')

    for month in months:
        print('##########')
        print('Month: {}'.format(month))
        print('##########')
    
        dir_meic = os.path.join(dir_meic1, '{}{:>02d}'.format(year, month))
        dir_cams = os.path.join(dir_cams1, '{}{:>02d}'.format(year, month))
        dir_htap = os.path.join(dir_htap1, '{}{:>02d}'.format(2010, month))
        dir_out = os.path.join(dir_out1, '{}{:>02d}'.format(year, month))
        if not os.path.exists(dir_out):
            os.mkdir(dir_out)
        print('------------------Filepath------------------')
        print('dir_meic: {}\ndir_cams: {}\ndir_htap: {}\ndir_out: {}'.format(dir_meic, dir_cams, dir_htap, dir_out))
        #Sector loop
        for sector in sectors:
            print('############')
            print(sector)
            print('############')
        
            #MEIC data file
            fn_meic = os.path.join(dir_meic, 'emis_{}_{}_{}_hour.nc'.format(sector.name, year, month))
            #CAMS data file
            fn_cams = os.path.join(dir_cams, 'emis_{}_{}_{}_hour.nc'.format(sector.name, year, month))
            #HTAP data file
            fn_htap = os.path.join(dir_htap, 'emis_{}_{}_{}_hour.nc'.format(sector.name, 2010, month))
            #Output data file
            fn_out = os.path.join(dir_out, 'emis_{}_{}_{}_hour.nc'.format(sector.name, year, month))
        
            if os.path.exists(fn_meic) and (not os.path.exists(fn_cams)):
                shutil.copyfile(fn_meic, fn_out)
                print('Data from MEIC...')
            elif os.path.exists(fn_cams) and (not os.path.exists(fn_meic)):
                shutil.copyfile(fn_cams, fn_out)
                print('Data from CAMS...')
            else:
                f_meic = addfile(fn_meic)
                f_cams = addfile(fn_cams)
                f_htap = addfile(fn_htap)
                #Create output netcdf file
                ncfile = addfile(fn_out, 'c', largefile=True)
                #Set global attribute
                gattrs = dict(Conventions='CF-1.6', Tools='Created using MeteoInfo')
                #Get all variable
                dimvars = []
                varnames = []
                for var in f_meic.variables():
                    if var.ndim == 3:
                        varnames.append(var.name)
                        dimvar = DimVariable()
                        dimvar.name = var.name
                        dimvar.dtype = var.dtype
                        dimvar.dims = dims
                        dimvar.attributes = var.attributes
                        dimvars.append(dimvar)
                for var in f_htap.variables():
                    if var.ndim == 3 and (not var.name in varnames):
                        varnames.append(var.name)
                        dimvar = DimVariable()
                        dimvar.dtype = var.dtype
                        dimvar.name = var.name
                        dimvar.dims = dims
                        dimvar.attributes = var.attributes
                        dimvars.append(dimvar)
                #Define dimensions, global attributes and variables
                ncfile.nc_define(dims, gattrs, dimvars)
        
                #Write variable values
                for varname in varnames:
                    print('\t{}'.format(varname))
                    data = None
                    tda = np.zeros((24, len(model_grid.y_coord), len(model_grid.x_coord)))
                    if varname in f_meic.varnames():
                        data = f_meic[varname][:]
                    if varname in f_cams.varnames():
                        data1 = f_cams[varname][:]
                        for i in range(24):
                            tda[i] = data1[i].maskout(tw.shapes())
                        tda[tda==np.nan] = 0
                        if data is None:
                            data = data1
                        else:
                            mask = data.copy()
                            mask[mask!=np.nan] = 0
                            mask[mask==np.nan] = 1
                            data[data==np.nan] = 0
                            data = data1 * mask + data
                            
                            data = data + tda
                    elif varname in f_htap.varnames():
                        data1 = f_htap[varname][:]
                        for i in range(24):
                            tda[i] = data1[i].maskout(tw.shapes())
                        tda[tda==np.nan] = 0
                        if data is None:
                            data = data1
                        else:
                            mask = data.copy()
                            mask[mask!=np.nan] = 0
                            mask[mask==np.nan] = 1
                            data[data==np.nan] = 0
                            data = data1 * mask + data
                            data = data + tda
                            
                    ncfile.write(varname, data)
        
                #Close file
                f_meic.close()
                f_cams.close()
                f_htap.close()
                ncfile.close()
    
    print('---------------------------------------')
    print('-----Merge output data completed!------')
    print('---------------------------------------')
Ejemplo n.º 16
0
def run_proj(year, month, dir_inter, model_grid, target_grid):
    print('Add input file...')
    fn_in = dir_inter + '\emis_{}_{}_hour_transform.nc'.format(year, month)
    print(fn_in)
    f_in = dataset.addfile(fn_in)
    #set dimension
    tdim = np.dimension(np.arange(24), 'Time')
    ydim = np.dimension(target_grid.y_coord, 'south_north', 'Y')
    xdim = np.dimension(target_grid.x_coord, 'west_east', 'X')
    zdim = np.dimension(np.arange(3), 'emissions_zdim')
    sdim = np.dimension(np.arange(19), 'DateStrLen')
    dims = [tdim, zdim, ydim, xdim]
    all_dims = [tdim, sdim, xdim, ydim, zdim]

    #set global attributes
    gattrs = OrderedDict()
    #gattrs['Conventions'] = 'CF-1.6'
    #gattrs['Tools'] = 'Created using MeteoInfo'
    gattrs[
        'TITLE'] = ' OUTPUT FROM *             PROGRAM:WRF-Chem V4.1.5 MODEL'
    gattrs['START_DATE'] = "2017-01-01_00:00:00"
    gattrs['WEST-EAST_GRID_DIMENSION'] = 335
    gattrs['SOUTH-NORTH_GRID_DIMENSION'] = 275
    gattrs['BOTTOM-TOP_GRID_DIMENSION'] = 28
    gattrs['DX'] = 15000.0
    gattrs['DY'] = 15000.0
    gattrs['AERCU_OPT'] = 0
    gattrs['AERCU_FCT'] = 1.0
    gattrs['IDEAL_CASE'] = 0
    gattrs['DIFF_6TH_SLOPEOPT'] = 0
    gattrs['AUTO_LEVELS_OPT'] = 2
    gattrs['DIFF_6TH_THRESH'] = 0.1
    gattrs['DZBOT'] = 50.0
    gattrs['DZSTRETCH_S'] = 1.3
    gattrs['DZSTRETCH_U'] = 1.1
    gattrs['GRIDTYPE'] = "C"
    gattrs['DIFF_OPT'] = 1
    gattrs['KM_OPT'] = 4
    gattrs['DAMP_OPT'] = 3
    gattrs['DAMPCOEF'] = 0.2
    gattrs['KHDIF'] = 0.0
    gattrs['KVDIF'] = 0.0
    gattrs['MP_PHYSICS'] = 2
    gattrs['RA_LW_PHYSICS'] = 4
    gattrs['RA_SW_PHYSICS'] = 4
    gattrs['SF_SFCLAY_PHYSICS'] = 2
    gattrs['SF_SURFACE_PHYSICS'] = 2
    gattrs['BL_PBL_PHYSICS'] = 2
    gattrs['CU_PHYSICS'] = 10
    gattrs['SF_LAKE_PHYSICS'] = 0
    gattrs['SURFACE_INPUT_SOURCE'] = 3
    gattrs['SST_UPDATE'] = 0
    gattrs['GRID_FDDA'] = 1
    gattrs['GFDDA_INTERVAL_M'] = 0
    gattrs['GFDDA_END_H'] = 0
    gattrs['GRID_SFDDA'] = 0
    gattrs['SGFDDA_INTERVAL_M'] = 0
    gattrs['SGFDDA_END_H'] = 0
    gattrs['HYPSOMETRIC_OPT'] = 2
    gattrs['USE_THETA_M'] = 1
    gattrs['GWD_OPT'] = 1
    gattrs['SF_URBAN_PHYSICS'] = 0
    gattrs['SF_SURFACE_MOSAIC'] = 0
    gattrs['SF_OCEAN_PHYSICS'] = 0
    gattrs['WEST-EAST_PATCH_START_UNSTAG '] = 1
    gattrs['WEST-EAST_PATCH_END_UNSTAG'] = 334
    gattrs['WEST-EAST_PATCH_START_STAG'] = 1
    gattrs['WEST-EAST_PATCH_END_STAG'] = 335
    gattrs['SOUTH-NORTH_PATCH_START_UNSTAG'] = 1
    gattrs['SOUTH-NORTH_PATCH_END_UNSTAG'] = 274
    gattrs['SOUTH-NORTH_PATCH_START_STAG'] = 1
    gattrs['SOUTH-NORTH_PATCH_END_STAG'] = 275
    gattrs['BOTTOM-TOP_PATCH_START_UNSTAG'] = 1
    gattrs['BOTTOM-TOP_PATCH_END_UNSTAG'] = 27
    gattrs['BOTTOM-TOP_PATCH_START_STAG'] = 1
    gattrs['BOTTOM-TOP_PATCH_END_STAG'] = 28
    gattrs['GRID_ID'] = 1
    gattrs['PARENT_ID'] = 0
    gattrs['I_PARENT_START'] = 1
    gattrs['J_PARENT_START'] = 1
    gattrs['PARENT_GRID_RATIO'] = 1
    gattrs['DT'] = 90.0
    gattrs['CEN_LAT'] = 36.500008
    gattrs['CEN_LON'] = 103.5
    gattrs['TRUELAT1'] = 30.0
    gattrs['TRUELAT2'] = 60.0
    gattrs['MOAD_CEN_LAT'] = 36.500008
    gattrs['STAND_LON'] = 103.5
    gattrs['POLE_LAT'] = 90.0
    gattrs['POLE_LON'] = 0.0
    gattrs['GMT'] = 0.0
    gattrs['JULYR'] = 2016
    gattrs['JULDAY'] = 365
    gattrs['MAP_PROJ'] = 1
    gattrs['MAP_PROJ_CHAR'] = 'Lambert Conformal'
    gattrs['MMINLU'] = 'MODIFIED_IGBP_MODIS_NOAH'
    gattrs['NUM_LAND_CAT'] = 21
    gattrs['ISWATER'] = 17
    gattrs['ISLAKE'] = 21
    gattrs['ISICE'] = 15
    gattrs['ISURBAN'] = 13
    gattrs['ISOILWATER'] = 14
    gattrs['HYBRID_OPT'] = 2
    gattrs['ETAC'] = 0.15

    fn_out = dir_inter + '\emis_{}_{}_hour_proj_nc4.nc'.format(year, month)
    #fn_out = dir_inter + '\emis_{}_{}_hour_proj_chunk.nc'.format(year, month)

    #set variables
    dimvars = []

    dimvar = dataset.DimVariable()
    dimvar.name = 'Times'
    dimvar.dtype = np.dtype.char
    dimvar.dims = [tdim, sdim]
    dimvar.addattr('_ChunkSizes', np.array([1, 19], dtype=np.dtype.uint))
    #dimvar.addattr('_ChunkSizes', [1, 19])
    dimvars.append(dimvar)

    for out_specie in out_species:
        dimvar = dataset.DimVariable()
        dimvar.name = out_specie
        dimvar.dtype = np.dtype.float
        dimvar.dims = dims
        dimvar.addattr('FieldType', 104)
        dimvar.addattr('MemoryOrder', "XYZ")
        dimvar.addattr('description', "EMISSION_{}".format(out_specie[2:]))
        if out_specie in out_species_unit:
            #g/m2/s to ug/m^3 m/s
            dimvar.addattr('units', 'ug/m3 m/s')
        else:
            #mole/m2/s to mol/km^2/hr
            dimvar.addattr('units', 'mol km^-2 hr^-1')
        dimvar.addattr('stagger', "")
        dimvar.addattr('coordinates', "XLONG XLAT XTIME")
        dimvar.addattr('_ChunkSizes',
                       array([1, 3, 137, 167], dtype=np.dtype.uint))
        #dimvar.addattr('_ChunkSizes', [1, 3, 137, 167])
        dimvars.append(dimvar)

    print('Create output data file...')
    print(fn_out)
    ncfile = dataset.addfile(fn_out, 'c', version='netcdf4', largefile=True)
    #ncfile = dataset.addfile(fn_out, 'c', largefile=True)
    print('Define dimensions, global attributes and variables...')
    ncfile.nc_define(all_dims, gattrs, dimvars, write_dimvars=False)

    #Times
    print('Write Times variable...')
    s_out = []
    for i in range(24):
        s = '{}-{:0>2d}-01_{:0>2d}:00:00'.format(year, month, i)
        s_out.append(s)
    s_out = np.array(s_out, dtype=np.dtype.char)
    ncfile.write('Times', s_out)

    print('Write variable data except times...')
    for out_specie in out_species:
        if out_specie in f_in.varnames():
            print(out_specie)
            data = f_in[out_specie][:]
            #Conversion
            data = transform(data, model_grid, target_grid)
            #Set the fourth dimension
            data = data.reshape(24, 1, 274, 334)
            #Set default values
            data[data == np.nan] = 0
            ncfile.write(out_specie, data)
    ncfile.close()
    print('Conversion projection finished!')
Ejemplo n.º 17
0
def run(year, month, dir_inter, emission, model_grid):
    """
    Process emission data by spatial allocation, temporal allocation
    and chemical speciation except VOC pollution.

    :param year: (*int*) Year.
    :param month: (*int*) Month.
    :param dir_inter: (*string*) Data output path.
    :param emission: (*module*) Emission module.
    :param model_grid: (*GridDesc*) Model data grid describe.
    """
    #Set profile files
    temp_profile_fn = os.path.join(ge_data_dir,
                                   'amptpro.m3.default.us+can.txt')
    temp_ref_fn = os.path.join(ge_data_dir, 'amptref.m3.us+can.cair.txt')
    spec_profile_fn = os.path.join(ge_data_dir, 'gspro.cmaq.radm2p25_rev.txt')
    spec_ref_fn = os.path.join(ge_data_dir, 'gsref.cmaq.radm2p25.txt')

    #Set data dimensions
    tdim = np.dimension(np.arange(24), 'hour')
    ydim = np.dimension(model_grid.y_coord, 'lat', 'Y')
    xdim = np.dimension(model_grid.x_coord, 'lon', 'X')
    dims = [tdim, ydim, xdim]

    #Set sectors and pollutants
    sectors = [SectorEnum.INDUSTRY, SectorEnum.AGRICULTURE, SectorEnum.ENERGY, \
        SectorEnum.RESIDENTIAL, SectorEnum.TRANSPORT, SectorEnum.SHIPS, \
        SectorEnum.AIR]

    pollutants = [PollutantEnum.BC, PollutantEnum.CH4, PollutantEnum.CO, \
        PollutantEnum.NH3, PollutantEnum.NOx, PollutantEnum.OC, PollutantEnum.PM2_5, \
        PollutantEnum.SO2, PollutantEnum.PM10]
    out_species = [SpeciesEnum.PEC, SpeciesEnum.CH4, SpeciesEnum.CO, SpeciesEnum.NH3, \
        None, SpeciesEnum.POA, None, SpeciesEnum.SO2, SpeciesEnum.PMC]

    #Read Mongolia shape and mask data
    lmongolia = geolib.shaperead('mongolia.shp')

    #Loop
    for sector in sectors:
        print('####################################')
        print(sector)
        print('####################################')

        #Get SCC
        scc = emis_util.get_scc(sector)

        #Get pollutant profiles
        pollutant_profiles = emips.chem_spec.read_file(spec_ref_fn,
                                                       spec_profile_fn, scc)
        for pollutant, out_spec in zip(pollutants, out_species):
            print(pollutant)

            print('Read emission data (kg/m2/s)...')
            emis_data = emission.read_emis(sector, pollutant, year, month)
            if emis_data is None:  #No emission of a pollutant for some sectors
                continue

            #### Decrease PM2.5 emission of Energy sector in Mongolia
            if sector == SectorEnum.ENERGY and pollutant == PollutantEnum.PM2_5:
                mdata = emis_data.maskout(lmongolia.shapes())
                mdata[mdata != np.nan] = 0.05
                mdata[mdata == np.nan] = 1
                emis_data = emis_data * mdata

            #### Spatial allocation
            print('Spatial allocation of emission grid to model grid...')
            emis_data = transform(emis_data, emission.emis_grid, model_grid)

            #### Temporal allocation
            print('Temporal allocation...')
            month_profile, week_profile, diurnal_profile, diurnal_profile_we = \
                emips.temp_alloc.read_file(temp_ref_fn, temp_profile_fn, scc)
            if pollutant == PollutantEnum.CH4 or sector == SectorEnum.AIR or \
                sector == SectorEnum.SHIPS:
                print('To (kg/m2/year)')
                emis_data = emis_data * 3600 * 24 * emis_util.get_year_days(
                    year)
                print('To monthly emission (kg/m2/month)...')
                emis_data = emips.temp_alloc.month_allocation(
                    emis_data, month_profile)
                emis_data = emis_data[month - 1]
            else:
                print('To (kg/m2/month)')
                emis_data = emis_data * 3600 * 24 * emis_util.get_month_days(
                    year, month)

            print('To daily emission (kg/m2/day)...')
            weekday_data, weekend_data = emips.temp_alloc.week_allocation(
                emis_data, week_profile, year, month)
            print('To hourly emission (g/m2/s)...')
            hour_data = emips.temp_alloc.diurnal_allocation(
                weekday_data, diurnal_profile) / 3.6

            #### Chemical speciation
            poll_prof = emips.chem_spec.get_pollutant_profile(
                pollutant_profiles, pollutant)
            if (pollutant == PollutantEnum.NOx) and (poll_prof is None):
                poll_prof = PollutantProfile(pollutant)
                poll_prof.append(
                    SpeciesProfile(pollutant, SpeciesEnum.NO, 0.9, 1.0, 0.9))
                poll_prof.append(
                    SpeciesProfile(pollutant, SpeciesEnum.NO2, 0.1, 1.0, 0.1))

            #### Set output netcdf file path
            outfn = os.path.join(dir_inter, \
                '{}_emis_{}_{}_{}_hour.nc'.format(pollutant.name, sector.name, year, month))
            print outfn
            if poll_prof is None:
                #### Save hourly emission data
                print('Save hourly emission data...')
                if out_spec.molar_mass is None:
                    attrs = dict(units='g/m2/s')
                else:
                    attrs = dict(units='mole/m2/s')
                    hour_data = hour_data / out_spec.molar_mass
                dataset.ncwrite(outfn, hour_data, out_spec.name, dims, attrs)
            else:
                print('Chemical speciation...')
                specs = poll_prof.get_species()
                gattrs = dict(Conventions='CF-1.6',
                              Tools='Created using MeteoInfo')
                dimvars = []
                for spec in specs:
                    dimvar = dataset.DimVariable()
                    dimvar.name = spec.name
                    dimvar.dtype = np.dtype.float
                    dimvar.dims = dims
                    if spec.molar_mass is None:
                        dimvar.addattr('units', 'g/m2/s')
                    else:
                        dimvar.addattr('units', 'mole/m2/s')
                    dimvars.append(dimvar)
                ncfile = dataset.addfile(outfn, 'c')
                ncfile.nc_define(dims, gattrs, dimvars)
                for spec_prof, dimvar, spec in zip(poll_prof.species_profiles,
                                                   dimvars, specs):
                    print(dimvar.name)
                    if spec.molar_mass is None:
                        spec_data = hour_data * spec_prof.mass_fraction
                    else:
                        spec_data = hour_data * spec_prof.mass_fraction / spec.molar_mass
                    ncfile.write(dimvar.name, spec_data)
                ncfile.close()