Exemplo n.º 1
0
def grid_expand(data, emis_grid):
    """
    Emission data grid expand to avoid NaN values in interpolation process.

    :param data: (*array*) emission data array (2D).
    :param emis_grid: (*GridSet*) emission grid set.

    :returns: Expanded data array and emission grid set.
    """
    ny, nx = data.shape
    rdata = np.zeros((ny + 2, nx + 2))
    rdata[1:-1, 1:-1] = data
    rdata[0, 1:-1] = data[0, :]
    rdata[-1, 1:-1] = data[-1, :]
    rdata[1:-1, 0] = data[:, 0]
    rdata[1:-1, -1] = data[:, -1]
    rdata[0, 0] = data[0, 0]
    rdata[-1, 0] = data[-1, 0]
    rdata[0, -1] = data[0, -1]
    rdata[-1, -1] = data[-1, -1]

    x = emis_grid.x_orig - emis_grid.x_delta
    y = emis_grid.y_orig - emis_grid.y_delta
    rgrid = GridDesc(emis_grid.proj,
                     x_orig=x,
                     x_cell=emis_grid.x_cell,
                     x_num=emis_grid.x_num + 2,
                     y_orig=y,
                     y_cell=emis_grid.y_cell,
                     y_num=emis_grid.y_num + 2)

    return rdata, rgrid
Exemplo n.º 2
0
def get_emis_grid(sector):
    """
    Get emission grid set

    :param sector: (*Sector*) The sector.

    :returns: (*GridSet*) Emission grid set.
    """
    if sector == SectorEnum.AIR:
        emis_grid = GridDesc(geolib.projinfo(),
                             x_orig=-179.75,
                             x_cell=0.5,
                             x_num=720,
                             y_orig=-89.75,
                             y_cell=0.5,
                             y_num=360)
    elif sector == SectorEnum.BIOMASS:
        emis_grid = GridDesc(geolib.projinfo(),
                             x_orig=-180.0,
                             x_cell=0.25,
                             x_num=1440,
                             y_orig=-90.0,
                             y_cell=0.25,
                             y_num=720)
    elif sector == SectorEnum.SHIPS:
        emis_grid = GridDesc(geolib.projinfo(),
                             x_orig=-180.0,
                             x_cell=0.25,
                             x_num=1440,
                             y_orig=-89.75,
                             y_cell=0.25,
                             y_num=720)
    else:
        emis_grid = GridDesc(geolib.projinfo(),
                             x_orig=-179.95,
                             x_cell=0.1,
                             x_num=3600,
                             y_orig=-89.95,
                             y_cell=0.1,
                             y_num=1800)

    return emis_grid
Exemplo n.º 3
0
def lonpivot(data, pivot, emis_grid):
    """
    Pivots an array about a user-specified longitude.

    :param data: (*array*) array data.
    :param pivot: (*float*) The longitude value around which to pivot. 
    :param emis_grid: (*GridSet*) emission grid set.

    :returns: result data array and emission grid.
    """
    rdata = data.lonpivot(pivot)
    lon = emis_grid.get_x_value(pivot)
    rgrid = GridDesc(emis_grid.proj,
                     x_orig=lon,
                     x_cell=emis_grid.x_cell,
                     x_num=emis_grid.x_num,
                     y_orig=emis_grid.y_orig,
                     y_cell=emis_grid.y_cell,
                     y_num=emis_grid.y_num)
    return rdata, rgrid
Exemplo n.º 4
0
                    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()

if __name__ == '__main__':
    #Set current working directory
    from inspect import getsourcefile
    dir_run = os.path.dirname(os.path.abspath(getsourcefile(lambda:0)))
    if not dir_run in sys.path:
        sys.path.append(dir_run)    
    import emission_meic_2017 as emission

    #Set year month
    year = 2017
    month = 1
    dir_inter = r'F:\emips_data\MEIC\2017\{}{:>02d}'.format(year, month)
    if not os.path.exists(dir_inter):
        os.mkdir(dir_inter)

    #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)
    model_grid = GridDesc(proj, x_orig=0., x_cell=0.25, x_num=1440,
        y_orig=-89.875, y_cell=0.25, y_num=720)

    #Run
    run(year, month, dir_inter, emission, model_grid)
Exemplo n.º 5
0
from emips.utils import Sector, SectorEnum
from emips.spatial_alloc import GridDesc
import os
from mipylib.dataset import addfile
from mipylib import geolib

__all__ = [
    'dir_emission', 'emis_grid', 'grid_areas', 'get_emis_fn', 'read_emis'
]

dir_emission = r'M:\Data\Emission\MEIC\MEIC_2017'

emis_grid = GridDesc(geolib.projinfo(),
                     x_orig=70.05,
                     x_cell=0.1,
                     x_num=800,
                     y_orig=10.05,
                     y_cell=0.1,
                     y_num=500)

#Calculate emission grid areas
grid_areas = emis_grid.grid_areas()  #square meters (array)


def get_emis_fn(sector, pollutant, month):
    """
    Get emission file path.

    :param sector: (*Sector*) The emission sector.
    :param pollutant: (*Pollutant*) The pollutant.
    :param month: (*int*) The month.
Exemplo n.º 6
0
from emips.utils import SectorEnum
from emips.spatial_alloc import GridDesc
import os
from mipylib.dataset import addfile
from mipylib import geolib

__all__ = [
    'dir_emission', 'emis_grid', 'grid_areas', 'get_emis_fn', 'read_emis'
]

dir_emission = r'M:\Data\Emission\EDGAR_HTAP\2010'

emis_grid = GridDesc(geolib.projinfo(),
                     x_orig=0.05,
                     x_cell=0.1,
                     x_num=3600,
                     y_orig=-89.95,
                     y_cell=0.1,
                     y_num=1800)

#Calculate emission grid areas
grid_areas = emis_grid.grid_areas()  #square meters


def get_emis_fn(sector, pollutant, year, month):
    """
    Get emission file path.

    :param sector: (*Sector*) The emission sector.
    :param pollutant: (*Pollutant*) The pollutant.
    :param year: (*int*) The year.
Exemplo n.º 7
0
        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!')


if __name__ == '__main__':
    #set parameter
    year = 2015
    month = 2
    dir_inter = r'D:\chen\MEIC_data\radm2_wrfchem_test\{}{:>02d}'.format(
        year, month)
    model_grid = GridDesc(geolib.projinfo(),
                          x_orig=70.,
                          x_cell=0.15,
                          x_num=502,
                          y_orig=15.,
                          y_cell=0.15,
                          y_num=330)
    #run
    run_merge(year, month, dir_inter, model_grid)
Exemplo n.º 8
0
import proj

#set file path
year = 2017
month = 1
mechanism_name = 'radm2'
dir_inter = os.path.join(
    r'G:\test_data', mechanism_name,
    r'region_0.15\merge\{0:}\{0:}{1:>02d}'.format(year, month))

#set origin projection and grid
model_proj = geolib.projinfo()
model_grid = GridDesc(model_proj,
                      x_orig=70.,
                      x_cell=0.15,
                      x_num=502,
                      y_orig=15.,
                      y_cell=0.15,
                      y_num=330)

#set target projection and grid
target_proj = geolib.projinfo(proj='lcc',
                              lon_0=103.5,
                              lat_0=36.500008,
                              lat_1=30.0,
                              lat_2=60.0,
                              a=6370000,
                              b=6370000)
target_grid = GridDesc(target_proj,
                       x_orig=-2497499.597352108,
                       x_cell=15000.0,
Exemplo n.º 9
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()
Exemplo n.º 10
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()
Exemplo n.º 11
0
from emips.utils import Sector, SectorEnum
from emips.spatial_alloc import GridDesc
import os
from mipylib import dataset
from mipylib import geolib

__all__ = [
    'dir_emission', 'emis_grid', 'grid_areas', 'get_emis_fn', 'read_emis'
]

dir_emission = r'S:\Data\Emission\MEIC\MEIC_2015\asc'

emis_grid = GridDesc(geolib.projinfo(),
                     x_orig=70.125,
                     x_cell=0.25,
                     x_num=320,
                     y_orig=10.125,
                     y_cell=0.25,
                     y_num=200)

#Calculate emission grid areas
grid_areas = emis_grid.grid_areas()  #square meters


def get_emis_fn(sector, pollutant, month):
    """
    Get emission file path.

    :param sector: (*Sector*) The emission sector.
    :param pollutant: (*Pollutant*) The pollutant.
    :param month: (*int*) The month.
Exemplo n.º 12
0
                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()


if __name__ == '__main__':
    #Set year, month and data path
    year = 2016
    month = 6
    dir_inter = r'G:\emips_data\global_1\MEIC\2016\{}{:>02d}'.format(
        year, month)
    if not os.path.exists(dir_inter):
        os.mkdir(dir_inter)

    #Set model grids
    proj = geolib.projinfo()
    model_grid = GridDesc(proj,
                          x_orig=0.,
                          x_cell=1,
                          x_num=360,
                          y_orig=-89.75,
                          y_cell=1,
                          y_num=180)

    #Run
    run(year, month, dir_inter, model_grid)
Exemplo n.º 13
0
                print('{} no data!'.format(out_specie))
                dd = f_in['E_ISO'][num:num+12]
                dd[:, :, :, :] = 0
                dd = transform(dd, model_grid, target_grid)
                #Set the fourth dimension 
                #dd = dd.reshape(12, 1, ydim.length, xdim.length)
                #Set default values
                dd[dd==np.nan] = 0
            data[:, :, :, :] = dd
            ncfile.write(out_specie, data)
        ncfile.close()
    print('Convert projection finished and split into two files finished!')
    
if __name__ == '__main__':
    #set target projection
    f_example = addfile(r'Z:\chen\Emission_Inventory\model_data\radm2\wrfchemi_d01_radm2_201701')
    #set parameter 
    year = 2017
    month = 1
    dir_inter = r'E:\emips_data\merge\region_0.1\2017_tw\{}{:>02d}'.format(year, month)
    model_grid = GridDesc(geolib.projinfo(), x_orig=70., x_cell=0.1, x_num=751,
        y_orig=15., y_cell=0.1, y_num=501)
    target_grid = GridDesc(f_example.proj, x_orig=-2497499.597352108, x_cell=15000.0, x_num=334,
        y_orig=-2047499.8096037393, y_cell=15000.0, y_num=274)

    z = 8
    #run
    run_proj(year, month, dir_inter, model_grid, target_grid, z)
    

Exemplo n.º 14
0
            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()
    
if __name__ == '__main__':  
    #Using RADM2 chemical mechanism
    from emips.chem_spec import RADM2

    #Set year, month and data path
    year = 2015
    month = 1
    dir_inter = r'D:\run_data\emips\run_meic\inter_data\{}{:>02d}'.format(year, month)
    if not os.path.exists(dir_inter):
        os.mkdir(dir_inter)

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

    #Run
    run(year, month, dir_inter, RADM2(), model_grid)
Exemplo n.º 15
0
        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, data)
    f_in.close()
    ncfile.close()
    print('Distribution of particulate matter and change unit finised!')


if __name__ == '__main__':
    #set parameter
    year = 2017
    month = 1
    dir_inter = r'G:\emips_data\region_0.1\MEIC\2017\{}{:>02d}'.format(
        year, month)
    model_grid = GridDesc(geolib.projinfo(),
                          x_orig=70.,
                          x_cell=0.1,
                          x_num=751,
                          y_orig=15.,
                          y_cell=0.1,
                          y_num=501)
    #run
    run_transform(year, month, dir_inter, model_grid)
Exemplo n.º 16
0
    ncfile.close()
    print('Conversion projection finished!')


if __name__ == '__main__':
    #set target projection
    f_example = addfile(
        r'D:\chen\Emission_Inventory\model_data\radm2\wrfchemi_d01_radm2_201701'
    )
    #set parameter
    year = 2017
    month = 1
    dir_inter = r'D:\chen\MEIC_data\radm2_wrfchem_test\{}{:>02d}'.format(
        year, month)
    model_grid = GridDesc(geolib.projinfo(),
                          x_orig=70.,
                          x_cell=0.15,
                          x_num=502,
                          y_orig=15.,
                          y_cell=0.15,
                          y_num=330)
    target_grid = GridDesc(f_example.proj,
                           x_orig=-2505000.0,
                           x_cell=15000.0,
                           x_num=334,
                           y_orig=-2055000.0,
                           y_cell=15000.0,
                           y_num=274)
    #run
    run_proj(year, month, dir_inter, model_grid, target_grid)
Exemplo n.º 17
0
def run(year, month, dir_inter, chem_mech):
    """
    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.
    """
    #Set sectors and pollutants
    sectors = [SectorEnum.INDUSTRY, SectorEnum.AGRICULTURE, SectorEnum.ENERGY, \
        SectorEnum.RESIDENTIAL, SectorEnum.TRANSPORT, SectorEnum.SHIPS, \
        SectorEnum.AIR]
    pollutant = 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)

        #Set input file
        infn = os.path.join(dir_inter, \
            '{}_emis_{}_{}_{}_hour.nc'.format(pollutant.name, sector, year, month))
        if not os.path.exists(infn):
            print('Alarm! File not exists: {}'.format(infn))
            continue

        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, 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')
            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:
                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()