
#define TRACEBACK write (gol,'("in ",a," (",a,i6,")")') rname, __FILE__, __LINE__ ; call goErr
#define IF_NOTOK_RETURN(action) if (status/=0) then; TRACEBACK; action; return; end if
#define IF_ERROR_RETURN(action) if (status> 0) then; TRACEBACK; action; return; end if
!
#include "tm5.inc"
!
!-----------------------------------------------------------------------------
!                    TM5                                                     !
!-----------------------------------------------------------------------------
!BOP
!
! !MODULE:      EMISSION_READ
!
! !DESCRIPTION: This module provides objects and methods related to
!               CMIP6, IPCC-AR5, EDGAR-4, RETRO_FIRES, LPJ and MACC emissions.
!               
!    AR5 netCDF files are provided by FZ-Juelich and IIASA: 
!        ftp://ftp-ipcc.fz-juelich.de/pub/emissions/gridded_netcdf/ 
!        http://www.iiasa.ac.at/web-apps/tnt/RcpDb/
!    
!    These data sets are not covering natural emissions. For these 
!    sources additional data from the MACC project are used (sectors 
!    ocean, soil, biogenic and natural, see below). 
!   
!    There are a few keys in the rc-file which control the behaviour of
!    this module and the data used: 
!    # specify the (main) provider of emission sets
!       input.emis.provider        :  AR5
!    #  where to find the emissions (this will be used by install-emis-ar5)
!       input.emis.dir             :  ${TEMP}/EMIS/AR5
!    #  year of emissions (AR5 emissions will be linearly interpolated)
!       input.emis.year            :  2000
!    #  choose RCP out of RCP26, RCP45, RCP60, RCP85
!       input.emis.AR5.RCP : RCP45 
!  
!\\
!\\
! !INTERFACE: 
!
MODULE EMISSION_READ  
  !
  ! !USES:
  !
  use GO,              only : gol, goErr, goPr, goLabel
  use emission_data,   only : emis_input_dir_cmip6
  use emission_data,   only : emis_input_dir_ar5
  use emission_data,   only : emis_input_dir_ed4, emis_input_dir_mac
  use emission_data,   only : vd_class_name_len
  use dims,            only : nlon360, nlat180, iglbsfc
  use chem_param,      only : ncb5
  use chem_param,      only : xmc, xmh, xmch2o, xmeth, xmgly, xmhcooh, xmmcooh, xmacet
  use chem_param,      only : xmole, xmald2, xmpar, xmo
  use Dims,            only : okdebug

  USE MDF,             ONLY : MDF_Open,      MDF_NETCDF,  MDF_READ
  USE MDF,             ONLY : MDF_Inq_VarID, MDF_Get_Var, MDF_Close

  implicit none
  private
  !
  ! !PUBLIC MEMBER FUNCTIONS:
  !
  public :: emission_read_init, emission_read_done
!!$  public :: emission_ar5_readcategory 
  public :: emission_ar5_regrid_aircraft
  public :: numb_2dsec, numb_3dsec
  public :: numb_sectors, sectors_def
  public :: numb_providers, providers_def
  public :: sector_name_len
  public :: read_cmip6_zch4
  public :: emis_cmip6_voc_name
  public :: emission_cmip6_readsector
  public :: emission_cmip6bmb_readsector
  public :: emission_ar5_readsector
  public :: emission_macc_readsector
  public :: emission_ed4_readsector
  public :: emission_gfed_readsector
  public :: emission_retro_readsector
  public :: emission_lpj_readsector
  public :: emission_hymn_readsector
  public :: emission_megan_readsector
  public :: ar5_dim_3ddata
  public :: emis_ar5_voc2cbm5_default
  public :: emis_ar5_voc2cbm5_biomassb
  public :: emis_ar5_voc2cbm5_biogenic
  public :: emis_ar5_voc_name, emis_ar5_nvoc
  public :: ar5_cat_ant, ar5_cat_shp, ar5_cat_air, ar5_cat_bmb

  ! CMIP6 aircraft
  public :: emis_cmip6_aircraft_tot2voc
  public :: emis_cmip6_aircraft_tl_tot2voc

  ! CMIP6BMB
  public :: emis_cmip6bmb_voc2cbm5
  public :: emis_cmip6bmb_voc_name, emis_cmip6bmb_nvoc

  ! MACC
  public :: emis_macc_voc2cbm5_default
  public :: emis_macc_voc2cbm5_biogenic
  public :: emis_macc_voc2cbm5_biomassb
  public :: emis_macc_voc_name, emis_macc_nvoc

  ! MEGAN
  public :: emis_megan_voc2cbm5_biogenic
  public :: emis_megan_nvoc, emis_megan_voc_name
  
  public :: emis_gfed_voc_name, emis_gfed_nvoc
  public :: emis_voc2cbm5_gfed
  public :: emis_retro_voc_name
  
  public :: sector_type, provider_type
  
  ! EDGAR 4.2
  public ::  ed42_nsect_co,    ed42_nsect_ch4 
  public ::  ed42_nsect_nox,   ed42_nsect_hc 
  public ::  ed42_nsect_nh3,   ed42_nsect_so2
  public ::  ed42_co_sectors,  ed42_ch4_sectors
  public ::  ed42_nox_sectors, ed42_hc_sectors
  public ::  ed42_nh3_sectors, ed42_so2_sectors

  !
  ! !PRIVATE DATA MEMBERS:
  !
  character(len=*), parameter ::  mname = 'emission_read'  

  ! ------------------------------
  ! global characteristics
  ! ------------------------------
  integer, parameter           :: nlat360         = 360    ! number of latitudes for CMIP6, AR5, MACC, EDGAR, GFED, RETRO data  (0.5deg)
  integer, parameter           :: nlon720         = 720    ! number of longitudes for CMIP6, AR5, MACC, EDGAR, GFED, RETRO data (0.5deg)
  integer, parameter           :: nlat720         = 720    ! number of latitudes for CMIP6BMB (0.25deg)
  integer, parameter           :: nlon1440        = 1440   ! number of longitudes for CMIP6BMB (0.25deg)
  integer, parameter           :: lpj_dim_nlat    = 150    ! number of latitudes  (1deg), no emissions 60S-90S
  integer, parameter           :: lpj_dim_nlon    = 360    ! number of longitudes (1deg)
  integer, parameter           :: sector_name_len = 18     ! length of sector descriptor
  integer, parameter           :: categ_name_len  = 14     ! length of category descriptor
  integer, parameter           :: numb_sectors    = 82     ! number of sectors (All providers!)
  integer, parameter           :: numb_2dsec      = 67     ! number of 2d sectors (all except aircraft)
  integer, parameter           :: numb_3dsec      = 2      ! number of 3d sectors (aircraft)
  ! In contrast to AR5, CMIP6 and CMIP6BMB are counted as separate providers
  integer, parameter           :: numb_providers  = 11     ! CMIP6, CMIP6BMB, AR5, MACC, ED41, ED42, LPJ, HYMN, GFEDv3, RETRO, MEGAN

  ! Since CMIP6 emissions from aviation are provided on the same vertical grid
  ! as for AR5, this variable is also used for CMIP6:
  integer, parameter           :: ar5_dim_3ddata  = 25     ! number of layers for aircraft data

  ! full list of providers
  character(10), dimension(numb_providers), parameter :: all_providers = &
       & (/ 'CMIP6     ', 'CMIP6BMB  ', 'RETRO     ', 'AR5       ', 'MACC      ', &
            'ED41      ', 'ED42      ', 'LPJ       ', 'HYMN      ', 'GFEDv3    ','MEGAN     '/)

  ! List of providers effectively used
  character(10), PUBLIC, allocatable :: used_providers(:)       ! general: CO, NMVOC, NOX, SOx, NH3
  character(10), PUBLIC, allocatable :: used_providers_isop(:)  ! ISOP
  character(10), PUBLIC, allocatable :: used_providers_terp(:)  ! TERP
  character(10), PUBLIC, allocatable :: used_providers_ch4(:)   ! CH4
  character(10), PUBLIC, allocatable :: used_providers_aer(:)   ! BC and POM

  ! flag for degenerated cases
  logical, PUBLIC :: has_aer_emis  = .true.
  logical, PUBLIC :: has_ch4_emis  = .true.
  logical, PUBLIC :: has_isop_emis = .true.
  logical, PUBLIC :: has_terp_emis = .true.
  logical, PUBLIC :: has_emis      = .true.
  
  ! extra params EDGAR 4.2: sectors used per species (here we already take out BMB and transport) 
  integer, parameter           :: ed42_nsect_co   =  8 ! (all=11) number of sectors for CO
  integer, parameter           :: ed42_nsect_ch4  = 13 ! (all=16) number of sectors for CH4
  integer, parameter           :: ed42_nsect_nox  = 10 ! (all=13) number of sectors for NOx
  integer, parameter           :: ed42_nsect_hc   =  9 ! (all=12) number of sectors for NMVOC
  integer, parameter           :: ed42_nsect_nh3  =  9 ! (all=11) number of sectors for NH3
  integer, parameter           :: ed42_nsect_so2  =  8 ! (all=11) number of sectors for SO2
  character(len=sector_name_len), dimension(ed42_nsect_co)      :: ed42_co_sectors   ! CO sectors in EDGAR 4.2
  character(len=sector_name_len), dimension(ed42_nsect_ch4)     :: ed42_ch4_sectors  ! CH4 sectors in EDGAR 4.2
  character(len=sector_name_len), dimension(ed42_nsect_nox)     :: ed42_nox_sectors  ! NOx sectors in EDGAR 4.2
  character(len=sector_name_len), dimension(ed42_nsect_hc)      :: ed42_hc_sectors   ! NMVOC sectors in EDGAR 4.2
  character(len=sector_name_len), dimension(ed42_nsect_nh3)     :: ed42_nh3_sectors  ! NH3 sectors in EDGAR 4.2
  character(len=sector_name_len), dimension(ed42_nsect_so2)     :: ed42_so2_sectors  ! SO2 sectors in EDGAR 4.2
  
  ! ------------------------------
  ! data used to construct filenames 
  ! ------------------------------
  character(len=15), parameter :: filestr_common_pre  = 'IPCC_emissions'
  character(len=25), parameter :: filestr_common_post = '0.5x0.5.nc'

  ! ------------------------------
  ! identifier of RCPs (RCP26, RCP45,...)
  ! ------------------------------
  character(len=5)             :: filestr_rcpiden  

  !------------------------------
  ! SSP scenario name
  !------------------------------
  character(len=14)             :: ssp_name
  logical                       :: LSSP370_LowNTCF, LSSP370_LowAer, LSSP370_LowO3
  logical                       :: LSSP370_LowBC, LSSP370_LowOC, LSSP370_LowSOx, LSSP370_LowNH3
  logical                       :: LSSP370_LowNOx, LSSP370_LowCO, LSSP370_LowNMVOC
  logical                       :: LSSP370_LowCH4

  ! ------------------------------
  ! available years and related parameters/variables
  ! ------------------------------
  ! availability (min, max years) - Limit MACC and MEGAN to one year for EC-Earth
  integer, dimension(2), parameter :: cmip6_avail = (/1850, 2100/)
  integer, dimension(2), parameter :: retro_avail = (/1960, 2000/)
  integer, dimension(2), parameter :: ar5_avail   = (/1850, 2100/)
  integer, dimension(2), parameter :: macc_avail  = (/1998, 1998/)
  integer, dimension(2), parameter :: ed41_avail  = (/2005, 2005/)
  integer, dimension(2), parameter :: ed42_avail  = (/1970, 2008/)
  integer, dimension(2), parameter :: lpj_avail   = (/1990, 2008/)
  integer, dimension(2), parameter :: hymn_avail  = (/ 999,  999/) ! not used
  integer, dimension(2), parameter :: gfed3_avail = (/1997, 2010/)  
  integer, dimension(2), parameter :: megan_avail = (/2000, 2000/)

  integer, parameter               :: ar5_nr_avail_yrs = 27
  integer, dimension(ar5_nr_avail_yrs), parameter :: &
       ar5_avail_yrs = (/ 1850, 1860, 1870, 1880, 1890, 1900, &
                          1910, 1920, 1930, 1940, 1950, 1960, &
                          1970, 1980, 1990, &
                          2000, 2005, 2010, 2020, 2030, 2040, &
                          2050, 2060, 2070, 2080, 2090, 2100 /)

  integer, parameter                 :: ed41_nr_avail_yrs = 12
  integer, dimension(ed41_nr_avail_yrs), parameter :: &
       ed41_avail_yrs = (/ 1970, 1975, 1980, 1985, 1990, 1995, 2000, &
                           2001, 2002, 2003, 2004, 2005 /)

  logical, dimension(:), allocatable :: ltimeind

  logical, save                       :: lpj_fractions_found
  real, dimension(:,:,:), allocatable :: lpj_frac_wetlands
  real, dimension(:,:,:), allocatable :: lpj_frac_rice
  real, dimension(:,:),   allocatable :: lpj_frac_peatlands

  character(len=7)        :: ar5_coverage = 'monthly'
  character(len=7)        :: ed4_coverage = 'yearly '
  character(len=7)        :: lpj_coverage = 'monthly'

  ! AR5 list of species available for each category
  character(len=26), target, dimension(31) :: ar5_cat_ant=(/ &
       'Acids                     ', 'alcohols                  ', 'BC                        ', &
       'benzene                   ', 'butanes                   ', 'CH4                       ', &
       'chlorinated_HC            ', 'CO                        ', 'esters                    ', &
       'ethane                    ', 'ethene                    ', 'ethers                    ', &
       'ethyne                    ', 'formaldehyde              ', 'hexanes_and_higher_alkanes', &
       'ketones                   ', 'NH3                       ', 'NMVOC                     ', &
       'NO                        ', 'OC                        ', 'other_alkanals            ', &
       'other_alkenes_and_alkynes ', 'other_aromatics           ', 'other_VOC                 ', &
       'pentanes                  ', 'propane                   ', 'propene                   ', &
       'SO2                       ', 'toluene                   ', 'trimethyl_benzenes        ', &
       'xylene                    '/)

  character(len=26), target, dimension(3)  :: ar5_cat_air=(/ &
       'BC                        ', 'NO2                       ', 'NO                        '/)

  character(len=26), target, dimension(22) :: ar5_cat_shp=(/ &
       'BC                        ', 'benzene                   ', 'butanes                   ', &
       'CH4                       ', 'CO                        ', 'ethane                    ', &
       'ethene                    ', 'ethyne                    ', 'hexanes_and_higher_alkanes', &
       'NH3                       ', 'NMVOC                     ', 'NO                        ', &
       'OC                        ', 'other_alkenes_and_alkynes ', 'other_aromatics           ', &
       'pentanes                  ', 'propane                   ', 'propene                   ', &
       'SO2                       ', 'toluene                   ', 'trimethyl_benzenes        ', &
       'xylene                    '/)
  
  character(len=26), target, dimension(31) :: ar5_cat_bmb=(/ &
       'acids                     ', 'alcohols                  ', 'BC                        ', &
       'benzene                   ', 'butanes                   ', 'CH4                       ', &
       'chlorinated_HC            ', 'CO                        ', 'ethane                    ', &
       'ethene                    ', 'ethers                    ', 'ethyne                    ', &
       'formaldehyde              ', 'hexanes_and_higher_alkanes', 'isoprene                  ', &
       'ketones                   ', 'NH3                       ', 'NMVOC                     ', &
       'NO                        ', 'OC                        ', 'other_alkanals            ', &
       'other_alkenes_and_alkynes ', 'other_aromatics           ', 'other_VOC                 ', &
       'pentanes                  ', 'propane                   ', 'propene                   ', &
       'SO2                       ', 'terpenes                  ', 'toluene                   ', &
       'xylene                    '/)
  
  ! and number of sectors in each category
  integer, public :: n_ar5_ant_sec, n_ar5_shp_sec, n_ar5_air_sec, n_ar5_bmb_sec

  ! reduced ar5 species available per anthro-sector (to screen out unavailable VOC)
  character(len=26), target, dimension(28) :: ar5_cat_ant_ene_ind=(/ &
       'acids                     ', 'alcohols                  ', 'BC                        ', &
       'benzene                   ', 'butanes                   ', 'CH4                       ', &
       'CO                        ', 'ethane                    ', 'ethene                    ', &
       'ethyne                    ', 'formaldehyde              ', 'hexanes_and_higher_alkanes', &
       'ketones                   ', 'NH3                       ', 'NMVOC                     ', &
       'NO                        ', 'OC                        ', 'other_alkanals            ', &
       'other_alkenes_and_alkynes ', 'other_aromatics           ', 'other_VOC                 ', &
       'pentanes                  ', 'propane                   ', 'propene                   ', &
       'SO2                       ', 'toluene                   ', 'trimethyl_benzenes        ', &
       'xylene                    '/)

  character(len=26), target, dimension(29) :: ar5_cat_ant_dom=(/&
       'acids                     ', 'alcohols                  ', 'BC                        ', &
       'benzene                   ', 'butanes                   ', 'CH4                       ', &
       'CO                        ', 'ethane                    ', 'ethene                    ', &
       'ethers                    ', 'ethyne                    ', 'formaldehyde              ', &
       'hexanes_and_higher_alkanes', 'ketones                   ', 'NH3                       ', &
       'NMVOC                     ', 'NO                        ', 'OC                        ', &
       'other_alkanals            ', 'other_alkenes_and_alkynes ', 'other_aromatics           ', &
       'other_VOC                 ', 'pentanes                  ', 'propane                   ', &
       'propene                   ', 'SO2                       ', 'toluene                   ', &
       'trimethyl_benzenes        ', 'xylene                    '/)
  
  character(len=26), target, dimension(27) :: ar5_cat_ant_agr=(/ &
       'acids                     ', 'alcohols                  ', 'BC                        ', &
       'benzene                   ', 'butanes                   ', 'CH4                       ', &
       'CO                        ', 'ethane                    ', 'ethene                    ', &
       'ethers                    ', 'ethyne                    ', 'formaldehyde              ', &
       'hexanes_and_higher_alkanes', 'ketones                   ', 'NH3                       ', &
       'NMVOC                     ', 'NO                        ', 'OC                        ', &
       'other_alkanals            ', 'other_alkenes_and_alkynes ', 'other_aromatics           ', &
       'pentanes                  ', 'propane                   ', 'propene                   ', &
       'SO2                       ', 'toluene                   ', 'xylene                    '/)

  character(len=26), target, dimension(25) :: ar5_cat_ant_awb=(/&
       'acids                     ', 'alcohols                  ', 'BC                        ', &
       'benzene                   ', 'butanes                   ', 'CH4                       ', &
       'CO                        ', 'ethane                    ', 'ethene                    ', &
       'ethyne                    ', 'formaldehyde              ', 'hexanes_and_higher_alkanes', &
       'ketones                   ', 'NH3                       ', 'NMVOC                     ', &
       'NO                        ', 'OC                        ', 'other_alkanals            ', &
       'other_alkenes_and_alkynes ', 'pentanes                  ', 'propane                   ', &
       'propene                   ', 'SO2                       ', 'toluene                   ', &
       'xylene                    '/)

  character(len=26), target, dimension(18) :: ar5_cat_ant_slv=(/&
       'alcohols                  ', 'BC                        ', 'CH4                       ', &
       'chlorinated_HC            ', 'CO                        ', 'esters                    ', &
       'ethers                    ', 'hexanes_and_higher_alkanes', 'ketones                   ', &
       'NH3                       ', 'NMVOC                     ', 'NO                        ', &
       'OC                        ', 'other_aromatics           ', 'other_VOC                 ', &
       'SO2                       ', 'toluene                   ', 'xylene                    '/)

  character(len=26), target, dimension(29) :: ar5_cat_ant_tra=(/&
       'BC                        ', 'benzene                   ', 'butanes                   ', &
       'CH4                       ', 'chlorinated_HC            ', 'CO                        ', &
       'esters                    ', 'ethane                    ', 'ethene                    ', &
       'ethers                    ', 'ethyne                    ', 'formaldehyde              ', &
       'hexanes_and_higher_alkanes', 'ketones                   ', 'NH3                       ', &
       'NMVOC                     ', 'NO                        ', 'OC                        ', &
       'other_alkanals            ', 'other_alkenes_and_alkynes ', 'other_aromatics           ', &
       'other_VOC                 ', 'pentanes                  ', 'propane                   ', &
       'propene                   ', 'SO2                       ', 'toluene                   ', &
       'trimethyl_benzenes        ', 'xylene                    '/) 

  ! ------------------------------
  ! gridbox area (to be read only once per proc)
  ! ------------------------------
  character(len=25),parameter       :: cmip6_filestr_gridboxarea = 'gridbox_area.nc          '
  character(len=25),parameter       :: ar5_filestr_gridboxarea = 'gridbox_area.nc          '                  
  character(len=25),parameter       :: ed4_filestr_gridboxarea = 'gridbox_area.nc          '
  character(len=25),parameter       :: lpj_filestr_gridboxarea = 'maps/lpj_gridcell_area.nc'
  logical, save                     :: area_found_025, area_found_05   
  logical, save                     :: lpj_area_found
  real, dimension(:,:), allocatable :: gridbox_area_025 ! gridbox area on 0.25x0.25 deg - used for MACCBMB
  real, dimension(:,:), allocatable :: gridbox_area_05  ! gridbox area on 0.5x0.5 deg - used for AR5, MACC, EDGAR, GFED, RETRO, MEGAN
  real, dimension(:,:), allocatable :: lpj_gridbox_area ! stored as double in file


  ! -----------------------
  ! data type for sectors
  ! -----------------------
  type sector_type
     sequence
     character(len=sector_name_len)    :: name      ! name of sector
     character(len=categ_name_len)     :: catname   ! name of category to be found in
     logical                           :: f3d       ! 3d-data y/n
     character(len=vd_class_name_len)  :: vdisttype ! vertical distribution type (equal to "classes" still to be defined)
     character(len=8)                  :: prov      ! provider of information (AR5, MACC, ED4)
     character(len=26), dimension(:), pointer :: species ! list of species available for that sector (use for AR5 only)
  end type sector_type

  type provider_type
     character(len=8)          :: name
     integer                   :: nsect2d, nsect3d
  end type provider_type

  type(sector_type),   dimension(numb_sectors)   :: sectors_def
  type(provider_type), dimension(numb_providers) :: providers_def
  

  ! ----------------------------------------------------------------------------------
  !                                 NMVOC settings
  ! ----------------------------------------------------------------------------------

  ! ----------------------------------------------------------------------------------
  !   A R 5  ( following GEMS setup ) - also used for CMIP6 anthropogenic and EDGAR
  ! ----------------------------------------------------------------------------------
  !
  ! Quotation of emission_tools_gems.F90: 
  ! -------------------------------------
  ! Distribution of NMV over the CBM-4 components (kg C/kg NMV) .
  ! VOC numbering according to TNO/RETRO speciation.
  ! Anthropogenic emissions of isoprene (ivoc=10), monoterpenes(ivoc=11) and others(ivoc=25) 
  ! are not used (set to zero in voc2c_tno and voc2c_fires) .
  ! The speciation is slightly different for biomass burning emissions, where: 
  !   voc_1  is methanol (which does not contribute) instead of alcohols, 
  !   voc_23 is acetone instead of ketones and 
  !   acetaldehyde is given instead of 'other aldehydes' (voc_22)
  ! For biomass burning isoprene (ivoc_10) and monoterpene (ivoc_11) are nonzero 
  ! and need to be included separately.
  !
  integer, parameter  ::  emis_ar5_nvoc = 25
  !
  ! AR5 components                     TM4-RETRO                         Available EDGAR41              UNavailable EDGAR42 from ED42_HC_SECTORS
  !                                                                      Transport sectors (1A3*)       (ignoring 1A3* transport, and 5A_C_D_F_4E fires)
  ! --------------------------------------------------------------------------------------------------------------------------
  ! alcohols                           1) alcohols                       1A3b_c_e (2005 only)           1A4 
  ! ethane                             2) ethane                         1A3b_c_e 1A3d1 1A3d_SHIP = id.   3 
  ! propane                            3) propane                        id.                              3 
  ! butanes                            4) butanes                        id.                              3 
  ! pentanes                           5) pentanes                       id.                              3 
  ! hexanes and higher alkanes         6) hexanes and higher alkanes     id.                              
  ! ethene                             7) ethene                         id.                              3 
  ! propene                            8) propene                        id.                              3 
  ! ethyne                             9) ethyne                         id.                              3 
  ! isoprene                          10) isoprene                       --none--                       all
  ! terpenes                          11) terpenes                       --none--                       all
  ! other alkenes and alkynes         12) lumped alkenes                 id.                              3     
  ! benzene                           13) benzene                        id.                              3     
  ! toluene                           14) toluene                        id.                                   
  ! xylene                            15) xylene                         id.                                   
  ! trimethyl benzenes                16) trimethylbenzene               id.                            3; 4F 
  ! other aromatics                   17) other_aromatics                id.                               4F 
  ! esters                            18) esters                         1A3b_c_e                       1A1a, 1A1b_c_1B_2C1_2C2, 1A2, 2A_B_D_E_F_G, 4F; 7A
  ! ethers                            19) ethers                         1A3b_c_e                       1A1a, 1A1b_c_1B_2C1_2C2, 1A2, 2A_B_D_E_F_G, 4F; 7A
  ! chlorinated HC                    20) Cl HC                          1A3b_c_e                       1A1a, 1A1b_c_1B_2C1_2C2, 1A2, 2A_B_D_E_F_G, 4F; 7A
  ! formaldehyde                      21) formaldehyde                   1A3b_c_e                       3
  ! other alkanal                     22) acetaldehyde                   1A3b_c_e                       3
  ! ketones                           23) acetone/ketones                1A3b_c_e                        
  ! acids                             24) acids                          1A3b_c_e (2005 only)           1A4; 3
  ! other VOC                         25) othervoc                       1A3b_c_e                       4F
  ! 
  character(len=26), parameter  ::  emis_ar5_voc_name(emis_ar5_nvoc) = (/ &
       'alcohols                  ', &  !  1
       'ethane                    ', &  !  2
       'propane                   ', &  !  3
       'butanes                   ', &  !  4
       'pentanes                  ', &  !  5
       'hexanes_and_higher_alkanes', &  !  6
       'ethene                    ', &  !  7
       'propene                   ', &  !  8
       'ethyne                    ', &  !  9
       'isoprene                  ', &  ! 10
       'terpenes                  ', &  ! 11
       'other_alkenes_and_alkynes ', &  ! 12
       'benzene                   ', &  ! 13
       'toluene                   ', &  ! 14
       'xylene                    ', &  ! 15
       'trimethyl_benzenes        ', &  ! 16
       'other_aromatics           ', &  ! 17
       'esters                    ', &  ! 18
       'ethers                    ', &  ! 19
       'chlorinated_HC            ', &  ! 20
       'formaldehyde              ', &  ! 21
       'other_alkanals            ', &  ! 22
       'ketones                   ', &  ! 23
       'acids                     ', &  ! 24
       'other_VOC                 ' /)  ! 25

    ! For CMIP6, use same species as for AR5
    ! but with different names
    character(len=26), parameter  ::  emis_cmip6_voc_name(emis_ar5_nvoc) = (/ &
       'VOC01-alcohols            ', & 
       'VOC02-ethane              ', &
       'VOC03-propane             ', &
       'VOC04-butanes             ', &
       'VOC05-pentanes            ', &
       'VOC06-hexanes-pl          ', &
       'VOC07-ethene              ', &
       'VOC08-propene             ', &
       'VOC09-ethyne              ', &
       'VOC10-isoprene            ', &
       'VOC11-terpenes            ', &
       'VOC12-other-alke          ', &
       'VOC13-benzene             ', &
       'VOC14-toluene             ', &
       'VOC15-xylene              ', &
       'VOC16-trimethylb          ', &
       'VOC17-other-arom          ', &
       'VOC18-esters              ', &
       'VOC19-ethers              ', &
       'VOC20-chlorinate          ', &
       'VOC21-methanal            ', &
       'VOC22-other-alka          ', &
       'VOC23-ketones             ', &
       'VOC24-acids               ', &
       'VOC25-other-voc           ' /)

  !
  ! voc_to_cbm5: Table to convert kg(nmvoc) to 'kg(cbm5) for variable cbm5
  ! JEW: the addition of some components is reassessd for eg butanes/pentanes
  ! JEW: see xl file : TAR_VOC_emissions_speciation-TM-2010
  ! JEW: for other voc species 20% of total C is assumed to be PAR
  real, parameter     ::  emis_ar5_voc2cbm5_default(emis_ar5_nvoc*ncb5) = (/ &
 ! PAR
    0.0        , 0.0                , 0.0         , 8.2685E-01  , 8.3264E-01  , & 
    8.4049E-01 , 0.0                , 0.0         , 4.6157E-01  , 0.0         , & 
    0.0        , 4.9970E-01         , 1.5386E-01  , 0.0         , 1.2451E-01  , & 
    2.0997E-01 , 2.5229E-01         , 3.6635E-01  , 3.8643E-01  , 2.6354E-01  , & 
    0.0        , 3.0081E-01         , 0.0         , 0.0         , 1.85E-01    , & 
 ! ETH
    0.0        , 0.0                , 0.0         , 0.0         , 0.0         , & 
    0.0        , xmeth/(xmc*2+xmh*4), 0.0         , 0.0         , 0.0         , & 
    0.0        , 0.0                , 0.0         , 0.0         , 0.0         , & 
    0.0        , 0.0                , 0.0         , 0.0         , 0.0         , & 
    0.0        , 0.0                , 0.0         , 0.0         , 0.0         , & 
 ! OLE
    0.0        , 0.0                , 0.0         , 0.0         , 0.0         , & 
    0.0        , 0.0                , 0.0         , 0.0         , 0.0         , & 
    0.0        , 3.5668E-01         , 0.0         , 0.0         , 0.0         , & 
    0.0        , 0.0                , 0.0         , 0.0         , 0.0         , & 
    0.0        , 0.0                , 0.0         , 0.0         , 0.0         , & 
 ! ALD2
    0.0        , 0.0                , 0.0         , 0.0         , 0.0         , & 
    0.0        , 0.0                , 0.0         , 0.0         , 0.0         , & 
    0.0        , 0.0                , 0.0         , 0.0         , 0.0         , & 
    0.0        , 0.0                , 0.0         , 0.0         , 0.0         , & 
    0.0        , 3.5389E-01         , 0.0         , 0.0         , 0.0         , & 
 ! MGLY
    0.0        , 0.0                , 0.0         , 0.0         , 0.0         , & 
    0.0        , 0.0                , 0.0         , 0.0         , 0.0         , & 
    0.0        , 0.0                , 0.0         , 0.0         , 2.7167E-01  , & 
    2.3978E-01 , 2.2409E-01         , 0.0         , 0.0         , 0.0         , & 
    0.0        , 0.0                , 0.0         , 0.0         , 0.0         , & 
 ! CH2O
    0.0        , 0.0                , 0.0         , 0.0         , 0.0         , & 
    0.0        , 0.0                , 0.0         , 0.0         , 0.0         , & 
    0.0        , 0.0                , 0.0         , 0.0         , 0.0         , & 
    0.0        , 0.0                , 0.0         , 0.0         , 0.0         , & 
    1.0        , 0.0                , 0.0         , 0.0         , 0.0         , &
!CH3OH   (how to attribute alcohols to CH3OH? distribute over CH3OH (90%) and ETHOH (10%))
    0.9        , 0.0                ,  0.0        , 0.0         , 0.0         , &
    0.0        , 0.0                ,  0.0        , 0.0         , 0.0         , &
    0.0        , 0.0                ,  0.0        , 0.0         , 0.0         , &
    0.0        , 0.0                ,  0.0        , 0.0         , 0.0         , &
    0.0        , 0.0                ,  0.0        , 0.0         , 0.0         , &
!HCOOH
    0.0        , 0.0                ,  0.0        , 0.0         , 0.0         , &
    0.0        , 0.0                ,  0.0        , 0.0         , 0.0         , &
    0.0        , 0.0                ,  0.0        , 0.0         , 0.0         , &
    0.0        , 0.0                ,  0.0        , 0.0         , 0.0         , &
    0.0        , 0.0                ,  0.0        , 0.5         , 0.0         , & 
!MCOOH
    0.0        , 0.0                ,  0.0        , 0.0         , 0.0         , &
    0.0        , 0.0                ,  0.0        , 0.0         , 0.0         , &
    0.0        , 0.0                ,  0.0        , 0.0         , 0.0         , &
    0.0        , 0.0                ,  0.0        , 0.0         , 0.0         , &
    0.0        , 0.0                ,  0.0        , 0.5         , 0.0         , & 
!C2H6
    0.0        , 1.0                ,  0.0        , 0.0         , 0.0         , &
    0.0        , 0.0                ,  0.0        , 0.0         , 0.0         , &
    0.0        , 0.0                ,  0.0        , 0.0         , 0.0         , &
    0.0        , 0.0                ,  0.0        , 0.0         , 0.0         , &
    0.0        , 0.0                ,  0.0        , 0.0         , 0.0         , &
!ETHOH (attribute 10% alcohol emisions to ETHOH! )
    0.1        , 0.0                ,  0.0        , 0.0         , 0.0         , &
    0.0        , 0.0                ,  0.0        , 0.0         , 0.0         , &
    0.0        , 0.0                ,  0.0        , 0.0         , 0.0         , &
    0.0        , 0.0                ,  0.0        , 0.0         , 0.0         , &
    0.0        , 0.0                ,  0.0        , 0.0         , 0.0         , &   
!C3H8
    0.0        , 0.0                ,  1.0        , 0.0         , 0.0         , &
    0.0        , 0.0                ,  0.0        , 0.0         , 0.0         , &
    0.0        , 0.0                ,  0.0        , 0.0         , 0.0         , &
    0.0        , 0.0                ,  0.0        , 0.0         , 0.0         , &
    0.0        , 0.0                ,  0.0        , 0.0         , 0.0         , &
!C3H6
    0.0        , 0.0                ,  0.0        , 0.0         , 0.0         , &
    0.0        , 0.0                ,  1.0        , 0.0         , 0.0         , &
    0.0        , 0.0                ,  0.0        , 0.0         , 0.0         , &
    0.0        , 0.0                ,  0.0        , 0.0         , 0.0         , &
    0.0        , 0.0                ,  0.0        , 0.0         , 0.0         , &                    
! Acetone
    0.0        , 0.0                ,  0.0        , 0.0         , 0.0         , &
    0.0        , 0.0                ,  0.0        , 0.0         , 0.0         , &
    0.0        , 0.0                ,  0.0        , 0.0         , 0.0         , &
    0.0        , 0.0                ,  0.0        , 0.0         , 0.0         , &
    0.0        , 0.0                ,  1.0        , 0.0         , 0.0  /)
  !
  ! The table below is used for the RETRO FIRES inventory only.
  ! It differs from the default AR5 table above in that
  ! methanol and acetaldehyde are provided directly.
  ! Remaining differences are either small or 
  ! occur for species that are not provided.
  real, parameter     ::  emis_ar5_voc2cbm5_biomassb(emis_ar5_nvoc*ncb5) = (/ &
! PAR
       0.0        , 0.0         ,        0.0  , 8.2685E-01  , 8.3264E-01  , & 
       8.4049E-01 , 0.0         ,        0.0  , 4.6157E-01  , 0.0         , & 
       0.0        , 4.9970E-01  , 1.5386E-01  , 0.0         , 1.2451E-01  , & 
       2.0997E-01 , 2.5229E-01  , 3.6635E-01  , 3.8643E-01  , 2.6354E-01  , & 
       0.0        , 0.0         ,        0.0  , 0.0         , 0.0         , & 
! ETH
       0.0        , 0.0                , 0.0         , 0.0         , 0.0         , & 
       0.0        , 8.5663E-01         , 0.0         , 0.0         , 0.0         , & 
       0.0        , 0.0                , 0.0         , 0.0         , 0.0         , & 
       0.0        , 0.0                , 0.0         , 0.0         , 0.0         , & 
       0.0        , 0.0                , 0.0         , 0.0         , 0.0         , & 
! OLE
       0.0        , 0.0                , 0.0         , 0.0         , 0.0         , & 
       0.0        , 0.0                , 0.0         , 0.0         , 0.0         , & 
       0.0        , 3.5693E-01         , 0.0         , 0.0         , 0.0         , & 
       0.0        , 0.0                , 0.0         , 0.0         , 0.0         , & 
       0.0        , 0.0                , 0.0         , 0.0         , 0.0         , & 
! ALD2 (RETRO FIRES inventory provides acetaldehyde directly)
       0.0        , 0.0                , 0.0         , 0.0         , 0.0         , & 
       0.0        , 0.0                , 0.0         , 0.0         , 0.0         , & 
       0.0        , 0.0                , 0.0         , 0.0         , 0.0         , & 
       0.0        , 0.0                , 0.0         , 0.0         , 0.0         , & 
       0.0        , 5.4541E-01         , 0.0         , 0.0         , 0.0         , & 
! MGLY
       0.0        , 0.0                , 0.0         , 0.0         , 0.0         , & 
       0.0        , 0.0                , 0.0         , 0.0         , 0.0         , & 
       0.0        , 0.0                , 0.0         , 0.0         , 2.7167E-01  , & 
       2.3996E-01 , 2.2426E-01         , 0.0         , 0.0         , 0.0         , & 
       0.0        , 0.0                , 0.0         , 0.0         , 0.0         , & 
! CH2O
       0.0        , 0.0                , 0.0         , 0.0         , 0.0         , & 
       0.0        , 0.0                , 0.0         , 0.0         , 0.0         , & 
       0.0        , 0.0                , 0.0         , 0.0         , 0.0         , & 
       0.0        , 0.0                , 0.0         , 0.0         , 0.0         , & 
       1.0        , 0.0                , 0.0         , 0.0         , 0.0         , &
! CH3OH (RETRO FIRES inventory provides methanol directly)
       1.0       , 0.0                 , 0.0        , 0.0          , 0.0        , &
       0.0       , 0.0                 , 0.0        , 0.0          , 0.0        , &
       0.0       , 0.0                 , 0.0        , 0.0          , 0.0        , &
       0.0       , 0.0                 , 0.0        , 0.0          , 0.0        , &
       0.0       , 0.0                 , 0.0        , 0.0          , 0.0        , & 
! HCOOH
       0.0       , 0.0                 , 0.0        , 0.0          , 0.0        , &
       0.0       , 0.0                 , 0.0        , 0.0          , 0.0        , &
       0.0       , 0.0                 , 0.0        , 0.0          , 0.0        , &
       0.0       , 0.0                 , 0.0        , 0.0          , 0.0        , &
       0.0       , 0.0                 , 0.0        , 0.0          , 0.0        , & 
! MCOOH
       0.0       , 0.0                 , 0.0        , 0.0          , 0.0       , &
       0.0       , 0.0                 , 0.0        , 0.0          , 0.0       , &
       0.0       , 0.0                 , 0.0        , 0.0          , 0.0       , &
       0.0       , 0.0                 , 0.0        , 0.0          , 0.0       , &
       0.0       , 0.0                 , 0.0        , 0.0          , 0.0       , & 
! C2H6
      0.0       , 1.0                  , 0.0        , 0.0          , 0.0       , &
      0.0       , 0.0                  , 0.0        , 0.0          , 0.0       , &
      0.0       , 0.0                  , 0.0        , 0.0          , 0.0       , &
      0.0       , 0.0                  , 0.0        , 0.0          , 0.0       , &
      0.0       , 0.0                  , 0.0        , 0.0          , 0.0       , & 
! ETHOH
      0.0       , 0.0                 , 0.0        , 0.0           , 0.0       , &
      0.0       , 0.0                 , 0.0        , 0.0           , 0.0       , &
      0.0       , 0.0                 , 0.0        , 0.0           , 0.0       , &
      0.0       , 0.0                 , 0.0        , 0.0           , 0.0       , &
      0.0       , 0.0                 , 0.0        , 0.0           , 0.0       , & 
! C3H8
     0.0        , 0.0                 , 1.0        , 0.0           , 0.0       , &
     0.0        , 0.0                 , 0.0        , 0.0           , 0.0       , &
     0.0        , 0.0                 , 0.0        , 0.0           , 0.0       , &
     0.0        , 0.0                 , 0.0        , 0.0           , 0.0       , &
     0.0        , 0.0                 , 0.0        , 0.0           , 0.0       , & 
! C3H6
    0.0         , 0.0                 , 0.0        , 0.0           , 0.0       , &
    0.0         , 0.0                 , 1.0        , 0.0           , 0.0       , &
    0.0         , 0.0                 , 0.0        , 0.0           , 0.0       , &
    0.0         , 0.0                 , 0.0        , 0.0           , 0.0       , &
    0.0         , 0.0                 , 0.0        , 0.0           , 0.0       , &
! Acetone
    0.0        , 0.0                  , 0.0        , 0.0           , 0.0       , &
    0.0        , 0.0                  , 0.0        , 0.0           , 0.0       , &
    0.0        , 0.0                  , 0.0        , 0.0           , 0.0       , &
    0.0        , 0.0                  , 0.0        , 0.0           , 0.0       , &
    0.0        , 0.0                  , 1.0        , 0.0           , 0.0       /)
  ! voc_to_cbm5: Table to convert kg(nmvoc) to kg(cbm5) for variable cbm5
  ! specific for biogenic emissions
  ! For alcohols methanol is used to account for ethanol. Therefore
  ! use 0.1 scaling factor. For other species the ratio of xmpar/xmspecies is adopted,
  ! using molecular weights for each species given in Schultz and Stein (2004)
  ! For species that have no biogenic contribution we adopt a value of 0.
  real, parameter     ::  emis_ar5_voc2cbm5_biogenic(emis_ar5_nvoc*ncb5) = (/ &
  ! PAR
       0.0        , 0.0         , 0.0         , 0.206897    , 8.3264E-01  , & 
       8.4049E-01 , 0.0         , 0.0         , 0.0         , 0.0         , & 
       0.0        , 4.9970E-01  , 0.0         , 0.0         , 0.0         , & 
       0.0        , 0.0         , 0.0         , 0.0         , 0.0         , & 
       0.0        , 0.0         , 0.0         , 0.0         , 0.0         , & 
  ! ETH
       0.0        , 0.0                , 0.0         , 0.0         , 0.0         , & 
       0.0        , xmeth/(xmc*2+xmh*4), 0.0         , 0.0         , 0.0         , & 
       0.0        , 0.0                , 0.0         , 0.0         , 0.0         , & 
       0.0        , 0.0                , 0.0         , 0.0         , 0.0         , & 
       0.0        , 0.0                , 0.0         , 0.0         , 0.0         , & 
  ! OLE
       0.0        , 0.0                , 0.0         , 0.0         , 0.0         , & 
       0.0        , 0.0                , 0.0         , 0.0         , 0.0         , & 
       0.0        , 3.5668E-01         , 0.0         , 0.0         , 0.0         , & 
       0.0        , 0.0                , 0.0         , 0.0         , 0.0         , & 
       0.0        , 0.0                , 0.0         , 0.0         , 0.0         , & 
  ! ALD2
       0.0        , 0.0                , 0.0         , 0.0         , 0.0         , & 
       0.0        , 0.0                , 0.0         , 0.0         , 0.0         , & 
       0.0        , 0.0                , 0.0         , 0.0         , 0.0         , & 
       0.0        , 0.0                , 0.0         , 0.0         , 0.0         , & 
       0.0        , 5.4541E-01         , 0.0         , 0.0         , 0.0         , & 
  ! MGLY
       0.0        , 0.0                , 0.0         , 0.0         , 0.0         , & 
       0.0        , 0.0                , 0.0         , 0.0         , 0.0         , & 
       0.0        , 0.0                , 0.0         , 0.0         , 0.0         , & 
       0.0        , 0.0                , 0.0         , 0.0         , 0.0         , & 
       0.0        , 0.0                , 0.0         , 0.0         , 0.0         , & 
  ! CH2O
       0.0        , 0.0                , 0.0         , 0.0         , 0.0         , & 
       0.0        , 0.0                , 0.0         , 0.0         , 0.0         , & 
       0.0        , 0.0                , 0.0         , 0.0         , 0.0         , & 
       0.0        , 0.0                , 0.0         , 0.0         , 0.0         , & 
       1.0        , 0.0                , 0.0         , 0.0         , 0.0         , &
  ! CH3OH   (how to attribute alcohols to CH3OH? distribute over CH3OH (90%) and ETHOH (10%))
       0.9       , 0.0                 , 0.0         , 0.0         , 0.0       , &
       0.0       , 0.0                 , 0.0         , 0.0         , 0.0       , &
       0.0       , 0.0                 , 0.0         , 0.0         , 0.0       , &
       0.0       , 0.0                 , 0.0         , 0.0         , 0.0       , &
       0.0       , 0.0                 , 0.0         , 0.0         , 0.0       , & 
  ! HCOOH
       0.0       , 0.0                 , 0.0         , 0.0         , 0.0       , &
       0.0       , 0.0                 , 0.0         , 0.0         , 0.0       , &
       0.0       , 0.0                 , 0.0         , 0.0         , 0.0       , &
       0.0       , 0.0                 , 0.0         , 0.0         , 0.0       , &
       0.0       , 0.0                 , 0.0         , 1.0         , 0.0       , & 
  ! MCOOH
       0.0       , 0.0                 , 0.0         , 0.0         , 0.0       , &
       0.0       , 0.0                 , 0.0         , 0.0         , 0.0       , &
       0.0       , 0.0                 , 0.0         , 0.0         , 0.0       , &
       0.0       , 0.0                 , 0.0         , 0.0         , 0.0       , &
       0.0       , 0.0                 , 0.0         , 1.0         , 0.0       , & 
  ! C2H6
       0.0       , 1.0                 , 0.0         , 0.0         , 0.0       , &
       0.0       , 0.0                 , 0.0         , 0.0         , 0.0       , &
       0.0       , 0.0                 , 0.0         , 0.0         , 0.0       , &
       0.0       , 0.0                 , 0.0         , 0.0         , 0.0       , &
       0.0       , 0.0                 , 0.0         , 0.0         , 0.0       , & 
  ! ETHOH (attribute 10% alcohol emisions to ETHOH! )
       0.1       , 0.0                 , 0.0         , 0.0         , 0.0       , &
       0.0       , 0.0                 , 0.0         , 0.0         , 0.0       , &
       0.0       , 0.0                 , 0.0         , 0.0         , 0.0       , &
       0.0       , 0.0                 , 0.0         , 0.0         , 0.0       , &
       0.0       , 0.0                 , 0.0         , 0.0         , 0.0       , & 
  ! C3H8
       0.0       , 0.0                 , 1.0         , 0.0         , 0.0       , &
       0.0       , 0.0                 , 0.0         , 0.0         , 0.0       , &
       0.0       , 0.0                 , 0.0         , 0.0         , 0.0       , &
       0.0       , 0.0                 , 0.0         , 0.0         , 0.0       , &
       0.0       , 0.0                 , 0.0         , 0.0         , 0.0       , & 
  ! C3H6
       0.0       , 0.0                 , 0.0         , 0.0         , 0.0       , &
       0.0       , 0.0                 , 1.0         , 0.0         , 0.0       , &
       0.0       , 0.0                 , 0.0         , 0.0         , 0.0       , &
       0.0       , 0.0                 , 0.0         , 0.0         , 0.0       , &
       0.0       , 0.0                 , 0.0         , 0.0         , 0.0       , &                       
   ! Acetone
       0.0       , 0.0                 , 0.0         , 0.0         , 0.0       , &
       0.0       , 0.0                 , 0.0         , 0.0         , 0.0       , &
       0.0       , 0.0                 , 0.0         , 0.0         , 0.0       , &
       0.0       , 0.0                 , 0.0         , 0.0         , 0.0       , &
       0.0       , 0.0                 , 1.0         , 0.0         , 0.0/)

  ! -------------------------------------------------------------------
  !   M A C C 
  ! -------------------------------------------------------------------

  !
  ! Distribution of NMV over the CBM-4 components (kg C/kg NMV) .
  ! VOC numbering according to TNO/RETRO speciation.
  ! Anthropogenic emissions of isoprene (ivoc=10), monoterpenes(ivoc=11) and others(ivoc=25) 
  ! are not used (set to zero in voc2c_tno and voc2c_fires) .
  ! The speciation is slightly different for biomass burning emissions, where: 
  !   voc_1  is methanol (which does not contribute) instead of alcohols, 
  !   voc_23 is acetone instead of ketones and 
  !   acetaldehyde is given instead of 'other aldehydes' (voc_22)
  ! For biomass burning isoprene (ivoc_10) and monoterpene (ivoc_11) are nonzero 
  ! and need to be included separately.
  !
  integer, parameter  ::  emis_macc_nvoc = 15
  !
  !  TM4-RETRO names                      MACC/grg names
  !  ----------------------------------   ------------------------
  !                                       2.1   CO
  !                                       2.2   NO
  !   1  alcohols                         2.x   CH3OH <- no C-C bond: not included TM5
  !                                       2.4   C2H5OH <-1 PAR 
  !   2  ethane                           2.5   C2H6 
  !   3  propane                          2.6   C3H8 
  !                                       2.7   BIGALK: lumped_alkanes:  (58 g/mole = butane)
  !   4  butanes                                 incl- c4h10  
  !   5  pentanes                                    
  !   6  hexanes_plus_higher_alkanes                  
  !   7  ethene                           2.8   C2H4
  !   8  propene                          2.9   C3H6
  !   9  
  !  10                                   2.10  ISOPRENE       
  !  11                                   2.11  TERPENES             
  !  12  other_alkenes_and_alkynes        2.12  BIKENE: lumped_alkenes (56 g/mole -> CBM-5 speciation gives one OLE. (1 double bond)
  !                                       2.13  TOLUENE: lumped_aromatics:
  !  13                                          incl?- benzene
  !  14                                               - toluene
  !  15  xylene                                       - xylene
  !  16  trimethylbenzenes
  !  17  other_aromatics
  !  18  esters
  !  19  ethers
  !  20  chlorinated_hydrocarbons
  !  21  methanal                         2.14  CH2O     (formaldehyde,methanal)
  !  22  alkanals                         2.15  CH3CHO   (acetaldehyde,acetal)
  !  23  ketones                          2.16  CH3COCH3: acetone
  !  24  acids
  !                                       2.17  h2
  !
  ! vh. According to Angelika Heil (FZ Juelich) the mol. weights for lumped species in biomass burning are 
  ! vh different from the anthropogenic emissions, as follows:
  ! vh Ankelika Heil:
  ! We updated these molecular weights for biomass burning for the MOZART
  ! lumped groups as follows:

  ! Higher_Alkanes:  58 g/mole            ==> 78.8 g/mole
  ! Higher_Alkenes:  56 g/mole            ==> 64.0 g/mole
  ! Toluene_lump: 92 g/mole               ==> 85.7 g/mole

  ! The MOZART lumped groups were formed as follows:

  ! Toluene_lump (C7H8+ C6H6 + C8H10): MOZART lumped toluene species,
  ! incorporating benzene, toluene, xylene

  !  Higher Alkenes (CnH2n, C>=4): all alkenes (C>=4) specified in Andreae
  !  and Merlet (2001) which are not contained in the Toluene_lump group.
  !  These are: Butenes (1-butene + i-butene + tr-2-butene + cis-2-butene)
  !  (C4H8), Pentenes  (1-pentene + 2-pentene) (C5H10), Hexene  (C6H12),
  !  Octene  (C8H16)

  ! Higher Alkanes (CnH2n+2, C>=4): all alkanes (C>=4) specified in Andreae
  ! and Merlet (2001). These are: Butanes (n-butane + i-butane) (C4H10),
  ! Pentanes (n-pentane + i-pentane) (C5H12), Hexane (n-hexane + i-hexane)
  ! (C6H14), Heptane (C7H16)

  ! We calculated the molecular weight of these lumped groups from weighting
  ! the molecular mass of the individual species with their relative mass
  ! contribution to the total fire emissions of each group during 2001-2006
  ! (calculated from GFEDv2 data).

  ! JEW: looking at the decomposition of TOL reveals ther is a product yield of 0.2MGLY
  !      which should be added analogous to the xylene > MGLY link in Houweling et al. (1998)


  character(len=26), parameter  ::  emis_macc_voc_name(emis_macc_nvoc) = (/ &
       'C2H5OH                    ', &  !   1
       'C2H6                      ', &  !   2
       'C3H8                      ', &  !   3
       'BIGALK                    ', &  !   4 
       'C2H4                      ', &  !   5
       'C3H6                      ', &  !   6 
       'ISOPRENE                  ', &  !   7
       'TERPENES                  ', &  !   8
       'BIGENE                    ', &  !   9
       'TOLUENE                   ', &  !  10
       'CH2O                      ', &  !  11
       'CH3CHO                    ', &  !  12
       'CH3COCH3                  ', &  !  13
       'MEK                       ', &  !  14
       'CH3OH                     '/)   !  15

  ! voc_to_cbm5: Table to convert kg(nmvoc) to 'kg(cbm5) for variable cbm5
  real, parameter     ::  emis_macc_voc2cbm5_default(emis_macc_nvoc*ncb5) = (/ &
       ! PAR
       0.0                     , 0.0                 ,  0.0                     , 4.0*xmpar/(4*xmc+10*xmh), &
       0.0                     , 0.0                 ,  0.0                     , 0.0              , &
       2*xmpar/(4*xmc+8*xmh)   , xmpar/92.           ,  0.0                     , 0.0              , &
       0.0                     , 4*xmpar/72.         ,  0.0                     ,                    &
       ! ETH
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       xmeth/(xmc*2+xmh*4)     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      ,                    & 
       ! OLE
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       xmole/(4*xmc+8*xmh)     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      ,                    & 
       ! ALD2
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      , xmald2/44.       , & 
       0.0                     , 0.0                 , 0.0                      ,                    & 
       ! MGLY
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.2*xmgly/(92.0)    , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      ,                    & 
       ! CH2O
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 1.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      ,                    &
       ! CH3OH
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 1.0                      ,                    & 
       ! HCOOH 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      ,                    & 
      ! MCOOH 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      ,                    & 
      !C2H6
       0.0                     , 1.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      ,                    & 
      !ETHOH
       1.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      ,                    &
      !C3H8 
       0.0                     , 0.0                 , 1.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      ,                    &        
      !C3H6 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 1.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      ,                    & 
      ! Acetone 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       1.0                     , 0.0                 , 0.0/)
  ! For Biomass burning, use a the same table
  real, parameter     ::  emis_macc_voc2cbm5_biomassb(emis_macc_nvoc*ncb5) = (/ &
        ! PAR
       0.0                     , 0.0                 ,  0.0                     , 4.0*xmpar/(4*xmc+10*xmh) , &
       0.0                     , 0.0                 ,  0.0                     , 0.0              , &
       2*xmpar/(4*xmc+8*xmh)   , xmpar/92.           ,  0.0                     , 0.0              , &
       0.0                     , 4*xmpar/72.         ,  0.0                     ,                    &
       ! ETH
       0.0                     , 0.0                 ,  0.0                     , 0.0              , & 
       xmeth/(xmc*2+xmh*4)     , 0.0                 ,  0.0                     , 0.0              , & 
       0.0                     , 0.0                 ,  0.0                     , 0.0              , & 
       0.0                     , 0.0                 ,  0.0                     ,                    & 
       ! OLE
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       xmole/(4*xmc+8*xmh)     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      ,                    & 
       ! ALD2
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      , xmald2/44.       , & 
       0.0                     , 0.0                 , 0.0                      ,                    & 
       ! MGLY
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.2*xmgly/(92.0)    , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      ,                    & 
       ! CH2O
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 1.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      ,                    &
       ! CH3OH
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 1.0                      ,                    & 
       ! HCOOH 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      ,                    & 
      ! MCOOH 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      ,                    & 
      !C2H6
       0.0                     , 1.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      ,                    &
      !ETHOH
       1.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      ,                    &
      !C3H8 
       0.0                     , 0.0                 , 1.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      ,                    &        
      !C3H6 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 1.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      ,                    & 
      ! Acetone 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       1.0                     , 0.0                 , 0.0/)
  ! specific for biogenic emissions
  real, parameter     ::  emis_macc_voc2cbm5_biogenic(emis_macc_nvoc*ncb5) = (/ &
        ! PAR
       0.0                     , 0.0                 ,  0.0                     , 4.0*xmpar/(4*xmc+10*xmh), &
       0.0                     , 0.0                 ,  0.0                     , 0.0              , &
       2*xmpar/(4*xmc+8*xmh)   , xmpar/92.           ,  0.0                     , 0.0              , &
       0.0                     , 4*xmpar/72.         ,  0.0                     ,                    &
       ! ETH
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       xmeth/(xmc*2+xmh*4)     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      ,                    & 
       ! OLE
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       xmole/(4*xmc+8*xmh)     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      ,                    & 
       ! ALD2
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      , xmald2/44.       , & 
       0.0                     , 0.0                 , 0.0                      ,                    & 
       ! MGLY
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.2*xmgly/(92.0)    , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      ,                    & 
       ! CH2O
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 1.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      ,                    &
       ! CH3OH
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 1.0                      ,                    & 
       ! HCOOH 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      ,                    & 
      ! MCOOH 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      ,                    & 
      !C2H6
       0.0                     , 1.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      ,                    & 
      !ETHOH
       1.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      ,                    &
      !C3H8 
       0.0                     , 0.0                 , 1.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      ,                    &        
      !C3H6 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 1.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      ,                    & 
      ! Acetone 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       0.0                     , 0.0                 , 0.0                      , 0.0              , & 
       1.0                     , 0.0                 , 0.0/)

  ! -------------------------------------------------------------------
  !   M A C C - M E G A N
  ! -------------------------------------------------------------------
  !
  ! Distribution of NMV over the CBM-4 components (kg C/kg NMV) .
  ! VOC numbering according to TNO/RETRO speciation.
  ! The speciation is slightly different for biomass burning emissions, where: 
  !   voc_1  is methanol (which does not contribute) instead of alcohols, 
  !   voc_23 is acetone instead of ketones and 
  !   acetaldehyde is given instead of 'other aldehydes' (voc_22)
  ! For biomass burning isoprene (ivoc_10) and monoterpene (ivoc_11) are nonzero 
  ! and need to be included separately.
  !
  !
  !  TM4-RETRO names                      MEGAN/grg names
  !  ----------------------------------   ------------------------
  !                                       2.1   CO
  !   1  alcohols                         2.2   methanol <- no C-C bond: not included TM5
  !                                       2.3   ethanol <-1 PAR 
  !   2  ethane                           2.4   ethane 
  !   3  propane                          2.5   propane 
  !   4  butanes                          2.6   butanes_and_higher_alkanes  (58 g/mole = butane)
  !                                             incl- c4h10  
  !   5  pentanes                                    
  !   6  hexanes_plus_higher_alkanes                  
  !   7  ethene                           2.7   ethene
  !   8  propene                          2.8   propene
  !   9  
  !  10                                   2.10  ISOPRENE       
  !  11                                   2.11  monoterpenes             
  !  12  other_alkenes_and_alkynes        2.12  butenes_and_higher_alkenes -> (56g/mole = butene)
  !  13                                   2.13  toluene (92g/mole=toluene)  <- 0.2 MGLY     
  !  14                                     
  !  15  xylene                               
  !  16  trimethylbenzenes
  !  17  other_aromatics
  !  18  esters
  !  19  ethers
  !  20  chlorinated_hydrocarbons
  !  21  methanal                         2.14  formaldehyde
  !  22  alkanals                         2.15  acetaldehyde
  !                                       2.16  other_aldehydes (44g/mole=C2 and above aldehydes): ignore other double count
  !  23  ketones                          2.17  acetone
  !                                       2.18  other_ketones   (72g/mole=other ketones except acetone): <-4*PAR as lower limit
  !  24  acids                            2.19  formic acid <- no C-C bond: not included TM5
  !                                       2.20  acetic acid <-1 PAR
  !

  integer, parameter  ::  emis_megan_nvoc = 18
  ! the same as MACC, except no MEK

  character(len=26), parameter  ::  emis_megan_voc_name(emis_megan_nvoc) = (/ &
       'methanol                  ', &  !   1
       'ethanol                   ', &  !   2
       'ethane                    ', &  !   3 
       'propane                   ', &  !   4
       'butanes_and_higher_alkanes', &  !   5 
       'ethene                    ', &  !   6
       'propene                   ', &  !   7
       'isoprene                  ', &  !   8
       'monoterpenes              ', &  !   9
       'butenes_and_higher_alkenes', &  !  10
       'toluene                   ', &  !  11
       'formaldehyde              ', &  !  12
       'acetaldehyde              ', &  !  13
       'other_aldehydes           ', &  !  14
       'acetone                   ', &  !  15
       'other_ketones             ', &  !  16
       'formic_acid               ', &  !  17
       'acetic_acid               '/)   !  18
       
  ! specific for MEGAN biogenic emissions
  real, parameter     ::  emis_megan_voc2cbm5_biogenic(emis_megan_nvoc*ncb5) = (/ &
       ! PAR
                               0.0                 , 0.0                , 0.0                , 0.0    , &
       4.0*xmpar/(4*xmc+10*xmh), 0.0               , 0.0                , 0.0                , 0.0    , &
       2*xmpar/(4*xmc+8*xmh) , xmpar/(92.0)        , 0.0                , 0.0                , 0.0    , &
       0.0                   , 4.0*xmpar/(72.0)    , 0.0                , 0.0                ,  &
       ! ETH
                               0.0                 , 0.0                , 0.0                , 0.0    , & 
       0.0                   , xmeth/(2*xmc+4*xmh) , 0.0                , 0.0                , 0.0    , & 
       0.0                   , 0.0                 , 0.0                , 0.0                , 0.0    , & 
       0.0                   , 0.0                 , 0.0                , 0.0                ,  & 
       ! OLE
                               0.0                 , 0.0                , 0.0                , 0.0    , & 
       0.0                   , 0.0                 , 0.0                , 0.0                , 0.0    , & 
       xmole/(4*xmc+8*xmh)   , 0.0                 , 0.0                , 0.0                , 0.0    , & 
       0.0                   , 0.0                 , 0.0                , 0.0                ,  & 
       ! ALD2
                               0.0                 , 0.0                , 0.0                , 0.0    , & 
       0.0                   , 0.0                 , 0.0                , 0.0                , 0.0    , & 
       0.0                   , 0.0                 , 0.0                , xmald2/44.         , 0.0    , & 
       0.0                   , 0.0                 , 0.0                , 0.0                , & 
       ! MGLY
                               0.0                 , 0.0                , 0.0                , 0.0    , & 
       0.0                   , 0.0                 , 0.0                , 0.0                , 0.0    , & 
       0.0                   , 0.2*xmgly/(92.0)    , 0.0                , 0.0                , 0.0    , & 
       0.0                   , 0.0                 , 0.0                , 0.0                , & 
       ! CH2O
                               0.0                 , 0.0                , 0.0                , 0.0    , & 
       0.0                   , 0.0                 , 0.0                , 0.0                , 0.0    , & 
       0.0                   , 0.0                 , 1.0                , 0.0                , 0.0    , & 
       0.0                   , 0.0                 , 0.0                , 0.0                , &
       ! CH3OH
                               1.0                 , 0.0                , 0.0                , 0.0    , & 
       0.0                   , 0.0                 , 0.0                , 0.0                , 0.0    , & 
       0.0                   , 0.0                 , 0.0                , 0.0                , 0.0    , & 
       0.0                   , 0.0                 , 0.0                , 0.0                , & 
       ! HCOOH 
                               0.0                 , 0.0                , 0.0                , 0.0    , & 
       0.0                   , 0.0                 , 0.0                , 0.0                , 0.0    , & 
       0.0                   , 0.0                 , 0.0                , 0.0                , 0.0    , & 
       0.0                   , 0.0                 , 1.0                , 0.0                , & 
       ! MCOOH 
                               0.0                 , 0.0                , 0.0                , 0.0    , & 
       0.0                   , 0.0                 , 0.0                , 0.0                , 0.0    , & 
       0.0                   , 0.0                 , 0.0                , 0.0                , 0.0    , & 
       0.0                   , 0.0                 , 0.0                , 1.0                , & 
       !C2H6
                               0.0                 , 0.0                , 1.0                , 0.0    , & 
       0.0                   , 0.0                 , 0.0                , 0.0                , 0.0    , & 
       0.0                   , 0.0                 , 0.0                , 0.0                , 0.0    , & 
       0.0                   , 0.0                 , 0.0                , 0.0                , & 
       !ETHOH
                               0.0                 , 1.0                , 0.0                , 0.0    , & 
       0.0                   , 0.0                 , 0.0                , 0.0                , 0.0    , & 
       0.0                   , 0.0                 , 0.0                , 0.0                , 0.0    , & 
       0.0                   , 0.0                 , 0.0                , 0.0                , & 
       !C3H8 
                               0.0                 , 0.0                , 0.0                , 1.0    , & 
       0.0                   , 0.0                 , 0.0                , 0.0                , 0.0    , & 
       0.0                   , 0.0                 , 0.0                , 0.0                , 0.0    , & 
       0.0                   , 0.0                 , 0.0                , 0.0                , &      
       !C3H6 
                               0.0                 , 0.0                , 0.0                , 0.0    , & 
       0.0                   , 0.0                 , 1.0                , 0.0                , 0.0    , & 
       0.0                   , 0.0                 , 0.0                , 0.0                , 0.0    , & 
       0.0                   , 0.0                 , 0.0                , 0.0                , &    
       ! Acetone
                               0.0                 , 0.0                , 0.0                , 0.0    , & 
       0.0                   , 0.0                 , 0.0                , 0.0                , 0.0    , & 
       0.0                   , 0.0                 , 0.0                , 0.0                , 0.0    , & 
       1.0                   , 0.0                 , 0.0                , 0.0/)

  !--------------------------------------------------------------------
  !   CMIP6 - Aircraft emissions
  !--------------------------------------------------------------------

  ! For the aviation sector only the total NMVOC emissions are provided
  ! due to the relatively low rate of emissions 
  ! and the large size of the files that would result
  ! in case speciated emissions were provided.
  ! In order to include these, we follow the recommendations
  ! from README-CEDS-VOC-speciation_06_30_2016.txt:

  ! "A suggested speciation for aircraft emissions is provided below 
  ! as used in EDGAR (thanks to Greet Maenhout). 
  ! Two profiles are provided, Takeoff_landing, and Aircraft_Exhaust. 
  ! We suggest, for simplicity, applying the Takeoff_landing profile 
  ! to the lowest two layers (0.305 km and 0.915 km) in the aircraft emissions file."

  ! It is assumed here that the species in the profiles given below
  ! have a one-to-one correspondence with the species
  ! used for the other anthropogenic sectors for AR5/CMIP6.

  ! Profiles for aircraft emissions        
  ! data source: USEPA SPECIATE 4.4 database       
  ! http://cfpub.epa.gov/si/speciate/     

  ! This profile can be used for aircraft exhausts
  ! VOC_species    weight (%)
  ! Pentanes       0.19801
  ! Trimethylbenzenes      0.510026
  ! Methylbenzene  0.642032
  ! Ethane 0.521026
  ! Dimethylbenzenes       0.448022
  ! Benzene        1.681084
  ! Alkanones      0.369018
  ! Alkanols       1.80509
  ! Other alk(adi)enes/alkynes     13.35667
  ! Other alkanals 21.9461
  ! Ethene 15.46177
  ! Propane        0.078004
  ! Propene        4.534227
  ! Ethyne 3.939197
  ! Hexanes and higher     18.11991
  ! Other aromatics        4.079204
  ! Methanal       12.31062
  real, parameter     ::  emis_cmip6_aircraft_tot2voc(emis_ar5_nvoc) = (/ &
       1.80509               , 0.521026            , 0.078004           , 0.0                , 0.19801             , &
       18.11991              , 15.46177            , 4.534227           , 3.939197           , 0.0                 , &
       0.0                   , 13.35667            , 1.681084           , 0.642032           , 0.448022            , &
       0.510026              , 4.079204            , 0.0                , 0.0                , 0.0                 , &
       12.31062              , 21.9461             , 0.369018           , 0.0                , 0.0/) / 100.

  ! This profile can be used for Aircraft Landing/Takeoff  
  ! VOC_species    weight (%)
  ! Methanal       15.87872
  ! Methylbenzene  0.550253
  ! Propene        5.154408
  ! Propane        0.224593
  ! Pentanes       0.213363
  ! Other aromatics        2.930938
  ! Other alkanals 12.5772
  ! Ethyne 4.143739
  ! Other  13.21729
  ! Ethane 1.033127
  ! Hexanes and higher     10.66816
  ! Ethene 17.40595
  ! Dimethylbenzenes       0.494104
  ! Alkanones      3.290286
  ! Alkanols       2.032566
  ! Other alk(adi)enes/alkynes     8.175182
  ! Benzene        2.010107 
  
  real, parameter     ::  emis_cmip6_aircraft_tl_tot2voc(emis_ar5_nvoc) = (/ &
       2.032566              , 1.033127            , 0.224593           , 0.0                , 0.213363            , &
       10.66816              , 17.40595            , 5.154408           , 4.143739           , 0.0                 , &
       0.0                   , 8.175182            , 2.010107           , 0.550253           , 0.494104            , &
       0.0                   , 2.930938            , 0.0                , 0.0                , 0.0                 , &
       15.87872              , 12.5772             , 3.290286           , 0.0                , 13.21729/) / 100.

  !--------------------------------------------------------------------
  !   CMIP6 - Biomass Burning
  ! -------------------------------------------------------------------
  
  ! Copied from MACC-MEGAN VOC split, but with some modifications:
  ! - Ketones replaced by MEK with contribution factor from MACC (same as for ketones).
  ! - Lumped toluene replaced by benzene (C6H6), toluene (C7H8) and xylene (C8H10)
  !   with contribution factors from AR5.
  ! - CH3COCHO (MGLY) added
  ! - 'other_aldehydes' doesn't contribute and is not provided, 
  !   therefore replaced by 'not provided'
  ! - Molar mass of higher alkanes and higher alkenes is 12 g/mol.
  !
  ! Also provided are ethyne (C2H2), hydroxyacetaldehyde (HOCH2CHO),
  ! and hydrogen cyanide (HCN): 
  ! but these are not included:
  ! - C2H2 assumed not to contribute (in contrast to split applied to AR5)
  ! - HOCH2CHO assumed not to contribute (like 'other_aldehydes')
  ! - HCN assumed not to contribute

  integer, parameter  ::  emis_cmip6bmb_nvoc = 21

  character(len=26), parameter  ::  emis_cmip6bmb_voc_name(emis_cmip6bmb_nvoc) = (/ &
       'CH3OH                     ', &  !   1
       'C2H5OH                    ', &  !   2
       'C2H6                      ', &  !   3 
       'C3H8                      ', &  !   4
       'Higher-Alkanes            ', &  !   5 
       'C2H4                      ', &  !   6
       'C3H6                      ', &  !   7
       'C5H8                      ', &  !   8
       'C10H16                    ', &  !   9
       'Higher-Alkenes            ', &  !  10
       'C6H6                      ', &  !  11
       'C7H8                      ', &  !  12
       'C8H10                     ', &  !  13
       'CH2O                      ', &  !  14
       'C2H4O                     ', &  !  15
       'not provided              ', &  !  16
       'C3H6O                     ', &  !  17
       'MEK                       ', &  !  18
       'HCOOH                     ', &  !  19
       'CH3COOH                   ', &  !  20
       'CH3COCHO                  '/)   !  21

  real, parameter     ::  emis_cmip6bmb_voc2cbm5(emis_cmip6bmb_nvoc*ncb5) = (/ &
       ! PAR
       0.0                   , 0.0                 , 0.0                , 0.0                , 4.0*xmpar/(4*xmc), &
       0.0                   , 0.0                 , 0.0                , 0.0                , 2.*xmpar/(4*xmc), &
       1.5386E-01            , 0.0                 , 1.2451E-01         , 0.0                , 0.0                 , &
       0.0                   , 0.0                 , 4.0*xmpar/(72.0)   , 0.0                , 0.0                 , &
       0.0                   , &
       ! ETH
       0.0                   , 0.0                 , 0.0                , 0.0                , 0.0                 , & 
       xmeth/(xmc*2+xmh*4)   , 0.0                 , 0.0                , 0.0                , 0.0                 , & 
       0.0                   , 0.0                 , 0.0                , 0.0                , 0.0                 , & 
       0.0                   , 0.0                 , 0.0                , 0.0                , 0.0                 , & 
       0.0                   , &
       ! OLE
       0.0                   , 0.0                 , 0.0                , 0.0                , 0.0                 , & 
       0.0                   , 0.0                 , 0.0                , 0.0                , xmole/(4*xmc)       , & 
       0.0                   , 0.0                 , 0.0                , 0.0                , 0.0                 , & 
       0.0                   , 0.0                 , 0.0                , 0.0                , 0.0                 , & 
       0.0                   , &
       ! ALD2
       0.0                   , 0.0                 , 0.0                , 0.0                , 0.0                 , & 
       0.0                   , 0.0                 , 0.0                , 0.0                , 0.0                 , & 
       0.0                   , 0.0                 , 0.0                , 0.0                , xmald2/44.          , &          
       0.0                   , 0.0                 , 0.0                , 0.0                , 0.0                 , & 
       0.0                   , &
       ! MGLY
       0.0                   , 0.0                 , 0.0                , 0.0                , 0.0                 , &
       0.0                   , 0.0                 , 0.0                , 0.0                , 0.0                 , & 
       0.0                   , 0.0                 , 2.7167E-01         , 0.0                , 0.0                 , & 
       0.0                   , 0.0                 , 0.0                , 0.0                , 0.0                 , & 
       1.0                   , &
       ! CH2O
       0.0                   , 0.0                 , 0.0                , 0.0                , 0.0                 , & 
       0.0                   , 0.0                 , 0.0                , 0.0                , 0.0                 , & 
       0.0                   , 0.0                 , 0.0                , 1.0                , 0.0                 , & 
       0.0                   , 0.0                 , 0.0                , 0.0                , 0.0                 , &
       0.0                   , &
       ! CH3OH
       1.0                   , 0.0                 , 0.0                , 0.0                , 0.0                 , & 
       0.0                   , 0.0                 , 0.0                , 0.0                , 0.0                 , & 
       0.0                   , 0.0                 , 0.0                , 0.0                , 0.0                 , & 
       0.0                   , 0.0                 , 0.0                , 0.0                , 0.0                 , & 
       0.0                   , &
       ! HCOOH 
       0.0                   , 0.0                 , 0.0                , 0.0                , 0.0                 , & 
       0.0                   , 0.0                 , 0.0                , 0.0                , 0.0                 , & 
       0.0                   , 0.0                 , 0.0                , 0.0                , 0.0                 , & 
       0.0                   , 0.0                 , 0.0                , 1.0                , 0.0                 , & 
       0.0                   , &
       ! MCOOH 
       0.0                   , 0.0                 , 0.0                , 0.0                , 0.0                 , & 
       0.0                   , 0.0                 , 0.0                , 0.0                , 0.0                 , & 
       0.0                   , 0.0                 , 0.0                , 0.0                , 0.0                 , & 
       0.0                   , 0.0                 , 0.0                , 0.0                , 1.0                 , & 
       0.0                   , &
       !C2H6
       0.0                   , 0.0                 , 1.0                , 0.0                , 0.0                 , &
       0.0                   , 0.0                 , 0.0                , 0.0                , 0.0                 , & 
       0.0                   , 0.0                 , 0.0                , 0.0                , 0.0                 , & 
       0.0                   , 0.0                 , 0.0                , 0.0                , 0.0                 , & 
       0.0                   , &
       !ETHOH
       0.0                   , 1.0                 , 0.0                , 0.0                , 0.0                 , & 
       0.0                   , 0.0                 , 0.0                , 0.0                , 0.0                 , & 
       0.0                   , 0.0                 , 0.0                , 0.0                , 0.0                 , & 
       0.0                   , 0.0                 , 0.0                , 0.0                , 0.0                 , & 
       0.0                   , &
       !C3H8 
       0.0                   , 0.0                 , 0.0                , 1.0                , 0.0                 , & 
       0.0                   , 0.0                 , 0.0                , 0.0                , 0.0                 , & 
       0.0                   , 0.0                 , 0.0                , 0.0                , 0.0                 , & 
       0.0                   , 0.0                 , 0.0                , 0.0                , 0.0                 , &      
       0.0                   , &
       !C3H6 
       0.0                   , 0.0                 , 0.0                , 0.0                , 0.0                 , & 
       0.0                   , 1.0                 , 0.0                , 0.0                , 0.0                 , & 
       0.0                   , 0.0                 , 0.0                , 0.0                , 0.0                 , & 
       0.0                   , 0.0                 , 0.0                , 0.0                , 0.0                 , &    
       0.0                   , &
       ! Acetone
       0.0                   , 0.0                 , 0.0                , 0.0                , 0.0                 , & 
       0.0                   , 0.0                 , 0.0                , 0.0                , 0.0                 , & 
       0.0                   , 0.0                 , 0.0                , 0.0                , 0.0                 , & 
       0.0                   , 1.0                 , 0.0                , 0.0                , 0.0                 , &
       0.0/) 

  ! -------------------------------------------------------------------
  !   GFED biomass burning
  ! -------------------------------------------------------------------
  integer, parameter  ::  emis_gfed_nvoc = 13
  ! the same as MACC, except CH3CHO, CH3COCH3, and MEK  !

  character(len=26), parameter  ::  emis_gfed_voc_name(emis_gfed_nvoc) = (/ &
       'ch3oh                     ', &  !   1  
       'c2h5oh                    ', &  !   2
       'c2h6                      ', &  !   3
       'c3h8                      ', &  !   4
       'higher_alkanes            ', &  !   5 
       'c2h4                      ', &  !   6
       'c3h6                      ', &  !   7 
       'isoprene                  ', &  !   8
       'terpenes                  ', &  !   9
       'higher_alkenes            ', &  !  10
       'toluenes                  ', &  !  11
       'ch2o                      ', &  !  12
       'nmhc                      '/)   !  13   
  ! For Biomass burning, use a the same table
  ! The conversion to CBM5 species is defined using the speciation given in Hoor et al, ACP, 2009
  ! species explicily accounted for : CH3OH, CH2O, C2H4, C2H6, C2H5OH, C3H8 and C3H6 
  ! MGLY is released from BB for toluene
  ! In Hoor et al. there is a 13% fraction of any organics from burning which are unspeciated (i.e.) lost
  ! species accounted for : HCOOH (4.04954E-2), CH3CHO(4.83586E-2), CH3COOH(1.13623E-01) and Acetone (5.1278E-02)
  ! JEW 2014 : Assume the higher alkanes are butanes, higher alkenes are iso-butene (1 CH2O, 1OLE)
  !
  real, parameter     ::  emis_voc2cbm5_gfed(emis_gfed_nvoc*ncb5) = (/ &
  ! PAR
       0.0                     , 0.0                    ,  0.0               , 0.0   , &
       4.0*xmpar/(xmc*4+xmh*10), 0.0                    ,  0.0               , 0.0   , &
       0.0                     , 2.0*xmpar/(xmc*4+xmh*8),  xmc/92.           , 0.0   , & 
       0.0                     ,                                                       &
   ! ETH
       0.0                     , 0.0                   , 0.0                 , 0.0   , & 
       0.0                     , 0.0                   , xmeth/(xmc*2+xmh*4) , 0.0   , & 
       0.0                     , 0.0                   , 0.0                 , 0.0   , &
       0.0                     ,                                                       & 
   ! OLE
       0.0                     , 0.0                   , 0.0                 , 0.0   , & 
       0.0                     , 0.0                   , 0.0                 , 0.0   , & 
       0.0                     , xmole/(xmc*4+xmh*8)   , 0.0                 , 0.0   , &
       0.0                     ,                                                       & 
   ! ALD
       0.0                     , 0.0                   , 0.0                 , 0.0   , & 
       0.0                     , 0.0                   , 0.0                 , 0.0   , & 
       0.0                     , 0.0                   , 0.0                 , 0.0   , &
       (xmald2/xmc)*4.83586E-2 ,                                                       & 
   ! MGLY
       0.0                     , 0.0                   , 0.0                 , 0.0   , & 
       0.0                     , 0.0                   , 0.0                 , 0.0   , & 
       0.0                     , 0.0                   , 0.2*xmgly/(92.0)    , 0.0   , &
       0.0                     ,                                                       & 
   ! CH2O
       0.0                     , 0.0                   , 0.0                 , 0.0   , & 
       0.0                     , 0.0                   , 0.0                 , 0.0   , & 
       0.0                     , 0.0                   , 0.0                 , 1.0   , &
       0.0                     ,                                                       &
   ! CH3OH
       1.0                     , 0.0                   , 0.0                 , 0.0   , & 
       0.0                     , 0.0                   , 0.0                 , 0.0   , & 
       0.0                     , 0.0                   , 0.0                 , 0.0   , &
       0.0                     ,                                                       & 
   ! HCOOH 
       0.0                     , 0.0                   , 0.0                 , 0.0   , & 
       0.0                     , 0.0                   , 0.0                 , 0.0   , & 
       0.0                     , 0.0                   , 0.0                 , 0.0   , &
       (xmhcooh/xmc)*4.04954E-2,                                                       & 
   ! MCOOH 
       0.0                     , 0.0                   , 0.0                 , 0.0   , & 
       0.0                     , 0.0                   , 0.0                 , 0.0   , & 
       0.0                     , 0.0                   , 0.0                 , 0.0   , &
       (xmmcooh/xmc)*1.13623E-01,                                                      & 
   ! C2H6
       0.0                     , 0.0                   , 1.0                 , 0.0   , & 
       0.0                     , 0.0                   , 0.0                 , 0.0   , & 
       0.0                     , 0.0                   , 0.0                 , 0.0   , &
       0.0                     ,                                                       & 
   ! ETHOH
       0.0                     , 1.0                   , 0.0                 , 0.0   , & 
       0.0                     , 0.0                   , 0.0                 , 0.0   , & 
       0.0                     , 0.0                   , 0.0                 , 0.0   , &
       0.0                     ,                                                       &
   ! C3H8
       0.0                     , 0.0                   , 0.0                 , 1.0   , & 
       0.0                     , 0.0                   , 0.0                 , 0.0   , & 
       0.0                     , 0.0                   , 0.0                 , 0.0   , &
       0.0                     ,                                                       &        
    ! C3H6 
       0.0                     , 0.0                   , 0.0                 , 0.0   , & 
       0.0                     , 0.0                   , 1.0                 , 0.0   , & 
       0.0                     , 0.0                   , 0.0                 , 0.0   , &
       0.0                     ,                                                       & 
    ! Acetone
       0.0                     , 0.0                   , 0.0                 , 0.0   , & 
       0.0                     , 0.0                   , 0.0                 , 0.0   , & 
       0.0                     , 0.0                   , 0.0                 , 0.0   , &
       (xmacet/xmc)*5.01278E-2 /)

  ! NB: base the RETRO voc speciation on AR5 (retro species are a subset of AR5)
  character(len=26), parameter  ::  emis_retro_voc_name(emis_ar5_nvoc) = (/ &
       'CH3OH                     ', &  !  1    ! only methanol provided, we do not include it
       'ETHANE                    ', &  !  2
       'PROPANE                   ', &  !  3
       'not provided              ', &  !  4
       'not provided              ', &  !  5
       'not provided              ', &  !  6
       'ETHENE                    ', &  !  7
       'PROPENE                   ', &  !  8
       'ETHYNE                    ', &  !  9
       'ISOPRENE                  ', &  ! 10
       'MONOTERPENES              ', &  ! 11
       'not provided              ', &  ! 12
       'BENZENE                   ', &  ! 13
       'TOLUENE                   ', &  ! 14
       'XYLENE                    ', &  ! 15
       'not provided              ', &  ! 16
       'not provided              ', &  ! 17
       'not provided              ', &  ! 18
       'not provided              ', &  ! 19
       'not provided              ', &  ! 20
       'CH2O                      ', &  ! 21
       'CH3CHO                    ', &  ! 22
       'ACETONE                   ', &  ! 23
       'not provided              ', &  ! 24
       'not provided              ' /)  ! 25
  !
  ! !REVISION HISTORY: 
  !    1 Oct 2010 - Achim Strunk  - v0 for AR5, MACC
  !    1 Dec 2011 - Narcisa Banda - added EDGAR 4.1 and 4.2
  !   19 Jun 2012 - P. Le Sager   - cosmetic for lon-lat MPI domain decomposition
  !                                 (all reading/regridding on root for now)
  !   20 Nov 2012 - Ph. Le Sager  - defined and build lists of used providers
  !                               - deal with inventories years availability
  !                               - switch to MDF interface to read data
  !
  ! !TODO:
  !   - should be renamed something like "emission_inventories" or "emiss_providers"
  !   - and need to get a **SEPARATE** module for each inventories, before it
  !     becomes unmanageable again
  !
  !EOP
  !------------------------------------------------------------------------

CONTAINS

  !--------------------------------------------------------------------------
  !                    TM5                                                  !
  !--------------------------------------------------------------------------
  !BOP
  !
  ! !IROUTINE:    EMISSION_READ_INIT
  !
  ! !DESCRIPTION: Initialise reading related parameters and 
  !               allocate needed arrays
  !
  !\\
  ! !INTERFACE:
  !
  SUBROUTINE EMISSION_READ_INIT( rcF, status )
    !
    ! !USES:
    !
    use GO,            only : TrcFile, ReadRc
    use partools,      only : isRoot
    use emission_data, only : LCMIP6, LCMIP6BMB
    use emission_data, only : LAR5, LEDGAR4, LRETROF, LGFED3, LMACC, LLPJ, LHYMN, LAR5BMB, LMACCITY, LMEGAN
    use meteodata,     only : set, gph_dat
    use dims,          only : im, jm, lm, nregions
    !
    ! !INPUT PARAMETERS:
    !
    type(TrcFile)        ::  rcF
    !
    ! !OUTPUT PARAMETERS:
    !
    integer, intent(out) ::  status
    !
    ! !REVISION HISTORY: 
    !    1 Oct 2010 - Achim Strunk  - v0 for AR5
    !    1 Dec 2011 - Narcisa Banda - added EDGAR 4.1 and 4.2
    !   20 Nov 2012 - Ph. Le Sager  - build lists of used providers
    !   29 Nov 2014 - Jason Williams - Introduced yearly specific biogenic emissions
    !
    ! !REMARKS:
    !
    !EOP
    !------------------------------------------------------------------------
    !BOC

    character(len=*), parameter :: rname=mname//'/emission_read_init'
    integer                     :: isect, iprov, nused, region
    logical                     :: mask(numb_providers), LEDGARCH4

    ! --- begin --------------------------------------

    if (LCMIP6) then 
      ! The SSP scenario data are provided for the years 2015-2100.
      ! However, when running December 2014,
      ! the zonal mean surface methane field for January 2015 is already needed.
      ! For that reason, we set the default to 'SSP3-7.0'
      ! when the scenario name is not provided.
      ! This is consistent with the treatment for ozone,
      ! for which the last entry of the 2014 input file
      ! also corresponds to SSP3-7.0.
      ! Note however that the difference in ozone and methane
      ! between SSP3-7.0 and SSP3-lowNTCF in January 2015
      ! are negligibly small.
      call ReadRc( rcF, 'input.CMIP6.SSP', ssp_name, status, default = 'SSP3-7.0' )
      IF_ERROR_RETURN(status=1)
      
      if (trim(ssp_name) == 'SSP3-lowNTCF') then
        write(gol,*) 'ERROR - SSP3-lowNTCF is not part of AerChemMIP. You should use the' ; call goPr
        write(gol,*) 'ERROR - input.CMIP6.SSP370_LowNTCF.* keys to activate AerChemMIP' ; call goPr
        write(gol,*) 'ERROR - lowNTCF experiments.' ; call goPr
        write(gol,*) 'ERROR - Uncomment this trap if you really want to use the general' ; call goPr
        write(gol,*) 'ERROR - SSP3-lowNTCF scenario' ; call goPr
        write(gol,*) 'Returning...' ; call goErr
        status=1; TRACEBACK; return
      endif
      
      write(gol,'("SSP CMIP6 future scenario for emissions: ",a)') trim(ssp_name); call goPr

      call ReadRc( rcF, 'input.CMIP6.SSP370_LowNTCF.ntcf', LSSP370_LowNTCF, status, default=.FALSE. )
      IF_ERROR_RETURN(status=1)

      call ReadRc( rcF, 'input.CMIP6.SSP370_LowNTCF.aer', LSSP370_LowAer, status, default=LSSP370_LowNTCF )
      IF_ERROR_RETURN(status=1)

      call ReadRc( rcF, 'input.CMIP6.SSP370_LowNTCF.bc', LSSP370_LowBC, status, default=LSSP370_LowAer )
      IF_ERROR_RETURN(status=1)
      IF (LSSP370_LowBC) THEN
         write(gol,'("... with SSP370-lowNTCF emissions for BC")'); call goPr
      ENDIF

      call ReadRc( rcF, 'input.CMIP6.SSP370_LowNTCF.oc', LSSP370_LowOC, status, default=LSSP370_LowAer )
      IF_ERROR_RETURN(status=1)
      IF (LSSP370_LowOC) THEN
         write(gol,'("... with SSP370-lowNTCF emissions for OC")'); call goPr
      ENDIF

      call ReadRc( rcF, 'input.CMIP6.SSP370_LowNTCF.sox', LSSP370_LowSOx, status, default=LSSP370_LowAer )
      IF_ERROR_RETURN(status=1)
      IF (LSSP370_LowSOx) THEN
         write(gol,'("... with SSP370-lowNTCF emissions for SOx")'); call goPr
      ENDIF

      call ReadRc( rcF, 'input.CMIP6.SSP370_LowNTCF.nh3', LSSP370_LowNH3, status, default=LSSP370_LowAer )
      IF_ERROR_RETURN(status=1)
      IF (LSSP370_LowNH3) THEN
         write(gol,'("... with SSP370-lowNTCF emissions for NH3")'); call goPr
      ENDIF

      call ReadRc( rcF, 'input.CMIP6.SSP370_LowNTCF.o3', LSSP370_LowO3, status, default=LSSP370_LowNTCF )
      IF_ERROR_RETURN(status=1)

      call ReadRc( rcF, 'input.CMIP6.SSP370_LowNTCF.nox', LSSP370_LowNOx, status, default=LSSP370_LowO3 )
      IF_ERROR_RETURN(status=1)
      IF (LSSP370_LowNOx) THEN
         write(gol,'("... with SSP370-lowNTCF emissions for NOx")'); call goPr
      ENDIF

      call ReadRc( rcF, 'input.CMIP6.SSP370_LowNTCF.co', LSSP370_LowCO, status, default=LSSP370_LowO3 )
      IF_ERROR_RETURN(status=1)
      IF (LSSP370_LowCO) THEN
         write(gol,'("... with SSP370-lowNTCF emissions for CO")'); call goPr
      ENDIF

      call ReadRc( rcF, 'input.CMIP6.SSP370_LowNTCF.nmvoc', LSSP370_LowNMVOC, status, default=LSSP370_LowO3 )
      IF_ERROR_RETURN(status=1)
      IF (LSSP370_LowNMVOC) THEN
         write(gol,'("... with SSP370-lowNTCF emissions for NMVOCs")'); call goPr
      ENDIF

      call ReadRc( rcF, 'input.CMIP6.SSP370_LowNTCF.ch4', LSSP370_LowCH4, status, default=.FALSE. )
      IF_ERROR_RETURN(status=1)
      IF (LSSP370_LowCH4) THEN
         write(gol,'("... with SSP370-lowNTCF emissions for CH4")'); call goPr
      ENDIF

    else if (LAR5) then
      call ReadRc( rcF, 'input.emis.AR5.RCP', filestr_rcpiden, status )
      IF_ERROR_RETURN(status=1)
    endif

    ! ------------------
    ! build list of used providers
    ! ------------------
    
    ! LMACCITY does not provide anthropogenic CH4, then use EDGAR instead
    if (LMACCITY) then
       LEDGARCH4=.true.
    else
       LEDGARCH4=LEDGAR4
    end if
    
    ! CH4 (any inventory, but skip MACC and MEGAN since they have no CH4)
    mask = (/ LCMIP6, LCMIP6BMB, LRETROF, LAR5, .false., LEDGARCH4, LEDGARCH4, LLPJ, LHYMN, LGFED3, .false. /)
    nused = count(mask)
    if (nused /= 0) then
       allocate( used_providers_ch4(nused) )
       used_providers_ch4 = pack( all_providers, mask)
    else
       has_ch4_emis = .false.
    end if

    ! ISOPRENE/TERPENES (anything except EDGAR, LPJ and HYMN; CMIP6/AR5 : only if fires requested)
    mask = (/ .false., LCMIP6BMB, LRETROF, LAR5BMB, LMACC, .false., .false., .false., .false., LGFED3, LMEGAN /)
    nused = count(mask)
    if (nused /= 0) then
       allocate( used_providers_isop(nused) )
       used_providers_isop = pack( all_providers, mask)
       allocate( used_providers_terp(nused) )
       used_providers_terp = pack( all_providers, mask)       
    else
       has_isop_emis = .false.
       has_terp_emis = .false.
    end if
    
    ! Others gases (anything except LPJ and HYMN)
    mask = (/ LCMIP6, LCMIP6BMB, LRETROF, LAR5, LMACC, LEDGAR4, LEDGAR4, .false., .false., LGFED3, LMEGAN /)
    nused = count(mask)
    if (nused /= 0) then
       allocate( used_providers(nused) )
       used_providers = pack( all_providers, mask)
    else
       has_emis = .false.
    end if

    ! BC and POM (anything except EDGAR, LPJ, HYMN and MEGAN)
    mask = (/ LCMIP6, LCMIP6BMB, LRETROF, LAR5, LMACC, .false., .false., .false., .false., LGFED3, .false. /)
    nused = count(mask)
    if (nused /= 0) then
       allocate( used_providers_aer(nused) )
       used_providers_aer = pack( all_providers, mask)
    else
       has_aer_emis = .false.
    end if

    ! info
    if (has_isop_emis) then
       write(gol,*) 'EMISS-INFO - Emissions providers used for ISOP    : ', used_providers_isop ; call goPr
    else
       write(gol,*) 'EMISS-INFO - Emissions providers used for ISOP    : NONE'                  ; call goPr
    end if
    if (has_terp_emis) then
       write(gol,*) 'EMISS-INFO - Emissions providers used for TERP    : ', used_providers_terp ; call goPr
    else
       write(gol,*) 'EMISS-INFO - Emissions providers used for TERP    : NONE'                  ; call goPr
    end if    
    if ( has_ch4_emis ) then
       write(gol,*) 'EMISS-INFO - Emissions providers used for CH4     : ', used_providers_ch4  ; call goPr
    else
       write(gol,*) 'EMISS-INFO - Emissions providers used for CH4     : NONE'                  ; call goPr
    end if
    if ( has_aer_emis ) then
       write(gol,*) 'EMISS-INFO - Emissions providers used for BC/POM  : ', used_providers_aer  ; call goPr
    else
       write(gol,*) 'EMISS-INFO - Emissions providers used for BC/POM  : NONE'                  ; call goPr
    end if
    if ( has_emis ) then 
       write(gol,*) 'EMISS-INFO - Emissions providers used for others  : ', used_providers      ; call goPr
    else
       write(gol,*) 'EMISS-INFO - Emissions providers used for others  : NONE'                  ; call goPr
    end if
       
    
    ! ------------------
    ! initialise sectors
    ! ------------------
    ! Type sequence is (name, category, is_3D_data, vdisttype, providers)
    sectors_def( 1) = sector_type('emiss_ene ', 'anthropogenic ', .false., 'combenergy  ', 'AR5     ', NULL() ) ! Energy production & distribution
    sectors_def( 2) = sector_type('emiss_dom ', 'anthropogenic ', .false., 'combrescom  ', 'AR5     ', NULL() ) ! Residential and commercial combustion
    sectors_def( 3) = sector_type('emiss_ind ', 'anthropogenic ', .false., 'industry    ', 'AR5     ', NULL() ) ! Industrial processes and combustion
    sectors_def( 4) = sector_type('emiss_wst ', 'anthropogenic ', .false., 'waste       ', 'AR5     ', NULL() ) ! Waste treatment and disposal
    sectors_def( 5) = sector_type('emiss_agr ', 'anthropogenic ', .false., 'surface     ', 'AR5     ', NULL() ) ! Agriculture
    sectors_def( 6) = sector_type('emiss_awb ', 'anthropogenic ', .false., 'nearsurface ', 'AR5     ', NULL() ) ! Agricultural waste burning
    sectors_def( 7) = sector_type('emiss_slv ', 'anthropogenic ', .false., 'nearsurface ', 'AR5     ', NULL() ) ! Solvent production and use
    sectors_def( 8) = sector_type('emiss_tra ', 'anthropogenic ', .false., 'surface     ', 'AR5     ', NULL() ) ! Land transport 
    sectors_def( 9) = sector_type('emiss_shp ', 'ships         ', .false., 'nearsurface ', 'AR5     ', NULL() ) ! Ships 
    sectors_def(10) = sector_type('emiss_air ', 'aircraft      ', .true. , 'aircraft    ', 'AR5     ', NULL() ) ! Aircraft
    sectors_def(11) = sector_type('grassfire ', 'biomassburning', .false., 'nearsurface ', 'AR5     ', NULL() ) ! Grassland Fire
    sectors_def(12) = sector_type('forestfire', 'biomassburning', .false., 'forestfire  ', 'AR5     ', NULL() ) ! Forest Fire

    ! macc sectors (-> natural, missing in AR5 and EDGAR )
    sectors_def(13) = sector_type('emiss_soil',   'natural       ', .false., 'surface     ', 'MACC    ', NULL() ) ! Natural sources (soil) - only for NO and NH3
    sectors_def(14) = sector_type('emiss_oc  ',   'natural       ', .false., 'surface     ', 'MACC    ', NULL() ) ! Natural sources (ocean)
    sectors_def(15) = sector_type('emiss_bio ',   'natural       ', .false., 'surface     ', 'MACC    ', NULL() ) ! Natural sources (biogenic)
    sectors_def(16) = sector_type('emiss_nat ',   'natural       ', .false., 'volcanic    ', 'MACC    ', NULL() ) ! Natural sources (volcanic)
    if (LMACCITY) then
       sectors_def(70) = sector_type('emiss_anthro', 'anthropogenic ', .false., 'combrescom  ', 'MACC    ', NULL() ) ! Anthropogenic (MACCITY only)
       sectors_def(71) = sector_type('emiss_air ',   'aircraft      ', .true. , 'aircraft    ', 'MACC    ', NULL() ) ! Aircraft (MACCITY only)
    else
       sectors_def(70) = sector_type('emiss_anthro', 'anthropogenic ', .false., 'combrescom  ', 'DUMMY   ', NULL() ) ! Off if MACC only to provide natural emissions
       sectors_def(71) = sector_type('emiss_air ',   'aircraft      ', .true. , 'aircraft    ', 'DUMMY   ', NULL() ) ! 
    end if

    ! MEGAN emissions from MACC    
    sectors_def(72) = sector_type('MEGAN_MACC ', 'natural       ', .false., 'surface    ', 'MEGAN  ', NULL())! Natural sources (soil)

    ! edgar 4.1 sectors
    sectors_def(17) = sector_type('1A1_ENE   ', 'anthropogenic ', .false., 'combenergy  ', 'ED41   ', NULL()) ! Energy production & distribution
    sectors_def(18) = sector_type('1A2_2     ', 'anthropogenic ', .false., 'industry    ', 'ED41   ', NULL()) ! Industrial processes and combustion
    sectors_def(19) = sector_type('1A3b_c_e  ', 'anthropogenic ', .false., 'surface     ', 'ED41   ', NULL()) ! Land transport 
    sectors_def(20) = sector_type('1A3a      ', 'aircraft      ', .true. , 'aircraft    ', 'ED41   ', NULL()) ! Aircraft
    sectors_def(21) = sector_type('1A3d_SHIP ', 'ships         ', .false., 'nearsurface ', 'ED41   ', NULL()) ! Ships domestic 
    sectors_def(22) = sector_type('1A3d1     ', 'ships         ', .false., 'nearsurface ', 'ED41   ', NULL()) ! Ships international
    sectors_def(23) = sector_type('1A4_5     ', 'anthropogenic ', .false., 'combrescom  ', 'ED41   ', NULL()) ! Residential and commercial combustion
    sectors_def(24) = sector_type('1B        ', 'anthropogenic ', .false., 'combenergy  ', 'ED41   ', NULL()) ! Fugitive emissions
    sectors_def(25) = sector_type('3         ', 'anthropogenic ', .false., 'nearsurface ', 'ED41   ', NULL()) ! Solvent emissions
    sectors_def(26) = sector_type('4_but_4E  ', 'anthropogenic ', .false., 'surface     ', 'ED41   ', NULL()) ! Agriculture (soils, waste burning, livestock)
    sectors_def(27) = sector_type('6A_6C     ', 'anthropogenic ', .false., 'waste       ', 'ED41   ', NULL()) ! Waste disposal and incineration
    sectors_def(28) = sector_type('7         ', 'anthropogenic ', .false., 'waste       ', 'ED41   ', NULL()) ! Fossil fuel fires

    ! edgar 4.2 sectors 
    sectors_def(29) = sector_type('1A1a      ',        'anthropogenic ', .false.,  'combenergy  ', 'ED42   ', NULL()) ! Energy production
    sectors_def(30) = sector_type('1A1a_6    ',        'anthropogenic ', .false.,  'combenergy  ', 'ED42   ', NULL()) ! Energy production and waste (CO & NH3 sector; waste part is small compared to energy,     so use energy vdist)
    sectors_def(31) = sector_type('1A1a_6C   ',        'anthropogenic ', .false.,  'combenergy  ', 'ED42   ', NULL()) ! Energy production and waste (SO2 sector; waste part is small compared to energy,          so use energy vdist)
    sectors_def(32) = sector_type('1A1_1A2   ',        'anthropogenic ', .false.,  'industry    ', 'ED42   ', NULL()) ! Energy and manufacturing industry (CH4 sector; energy part is small compared to industry, so use industry vdist)
    sectors_def(33) = sector_type('1A1b_c    ',        'anthropogenic ', .false.,  'combenergy  ', 'ED42   ', NULL()) ! Energy production
    sectors_def(34) = sector_type('1A1b_c_1B_2C1_2C2', 'anthropogenic ', .false.,  'combenergy  ', 'ED42   ', NULL()) ! Energy, fugitive and metal industry (NMVOC sector; metal part is small, so use energy vdist)
    sectors_def(35) = sector_type('1A2       ',        'anthropogenic ', .false.,  'industry    ', 'ED42   ', NULL()) ! Combustion in manufacturing industry 
    sectors_def(36) = sector_type('1A3       ',        'anthropogenic ', .false. , 'nearsurface',  'ED42   ', NULL()) ! Transport (including ships, aircraft!)
    sectors_def(37) = sector_type('1A3a_c_d_e',        'anthropogenic ', .false. , 'nearsurface',  'ED42   ', NULL()) ! Non-road transport (including ships, aircraft!)
    sectors_def(38) = sector_type('1A3b      ',        'anthropogenic ', .false.,  'surface     ', 'ED42   ', NULL()) ! Road transport
    sectors_def(39) = sector_type('1A4       ',        'anthropogenic ', .false.,  'combrescom  ', 'ED42   ', NULL()) ! Residential
    sectors_def(40) = sector_type('1B1       ',        'anthropogenic ', .false.,  'combenergy  ', 'ED42   ', NULL()) ! Fugitive emissions
    sectors_def(41) = sector_type('1B1_1B2_1A1b_c',    'anthropogenic ', .false.,  'combenergy  ', 'ED42   ', NULL()) ! Fugitive and energy emissions
    sectors_def(42) = sector_type('1B2a      ',        'anthropogenic ', .false.,  'combenergy  ', 'ED42   ', NULL()) ! Fugitive emissions
    sectors_def(43) = sector_type('1B2b      ',        'anthropogenic ', .false.,  'combenergy  ', 'ED42   ', NULL()) ! Fugitive emissions
    sectors_def(44) = sector_type('1B2a_c_1A1b_c',     'anthropogenic ', .false.,  'combenergy  ', 'ED42   ', NULL()) ! Fugitive and energy emissions
    sectors_def(45) = sector_type('2A_B_D_E_F_G',      'anthropogenic ', .false.,  'industry   ',  'ED42   ', NULL()) ! Industry emissions
    sectors_def(46) = sector_type('2         ',        'anthropogenic ', .false.,  'industry   ',  'ED42   ', NULL()) ! Industry emissions
    sectors_def(47) = sector_type('2A        ',        'anthropogenic ', .false.,  'industry   ',  'ED42   ', NULL()) ! Industry emissions
    sectors_def(48) = sector_type('2A_2B_2D  ',        'anthropogenic ', .false.,  'industry   ',  'ED42   ', NULL()) ! Industry emissions
    sectors_def(49) = sector_type('2B        ',        'anthropogenic ', .false.,  'industry   ',  'ED42   ', NULL()) ! Industry emissions
    sectors_def(50) = sector_type('2C        ',        'anthropogenic ', .false.,  'industry   ',  'ED42   ', NULL()) ! Industry emissions
    sectors_def(51) = sector_type('3         ',        'anthropogenic ', .false.,  'nearsurface ', 'ED42   ', NULL()) ! Solvent emissions
    sectors_def(52) = sector_type('4A        ',        'anthropogenic ', .false.,  'surface     ', 'ED42   ', NULL()) ! Agriculture
    sectors_def(53) = sector_type('4B        ',        'anthropogenic ', .false.,  'surface     ', 'ED42   ', NULL()) ! Agriculture 
    sectors_def(54) = sector_type('4C_4D     ',        'anthropogenic ', .false.,  'surface     ', 'ED42   ', NULL()) ! Agriculture 
    sectors_def(55) = sector_type('4F        ',        'anthropogenic ', .false.,  'surface     ', 'ED42   ', NULL()) ! Agricultural waste burning
    sectors_def(56) = sector_type('6A_6C     ',        'anthropogenic ', .false.,  'waste       ', 'ED42   ', NULL()) ! Waste disposal and incineration
    sectors_def(57) = sector_type('6B        ',        'anthropogenic ', .false.,  'waste       ', 'ED42   ', NULL()) ! Waste disposal and incineration
    sectors_def(58) = sector_type('7         ',        'anthropogenic ', .false.,  'waste       ', 'ED42   ', NULL()) ! Fossil fuel fires
    sectors_def(59) = sector_type('7A        ',        'anthropogenic ', .false.,  'waste       ', 'ED42   ', NULL()) ! Fossil fuel fires
    sectors_def(60) = sector_type('5A_C_D_F_4E',       'biomassburning', .false.,  'forestfire  ', 'ED42   ', NULL()) ! Large scale biomass burning

    ! natural methane emissions LPJ and HYMN project
    sectors_def(61) = sector_type('wetlands  ',      'natural       ', .false., 'surface     ', 'LPJ     ',NULL()) ! Methane from wetlands
    sectors_def(62) = sector_type('peatlands ',      'natural       ', .false., 'surface     ', 'LPJ     ',NULL()) ! Methane from peatlands
    sectors_def(63) = sector_type('wetsoils  ',      'natural       ', .false., 'surface     ', 'LPJ     ',NULL()) ! Methane from wet soils
    sectors_def(64) = sector_type('soilconsumption', 'natural       ', .false., 'surface     ', 'LPJ     ',NULL()) ! Methane soil uptake
    sectors_def(65) = sector_type('oceans    ',      'natural       ', .false., 'surface     ', 'HYMN    ',NULL()) ! Methane ocean emissions 
    sectors_def(66) = sector_type('wildanimals',     'natural       ', .false., 'surface     ', 'HYMN    ',NULL()) ! Methane emissions from wild animals
    sectors_def(67) = sector_type('termites  ',      'natural       ', .false., 'surface     ', 'HYMN    ',NULL()) ! Methane ternite emissions

    ! Biomass burning GFEDv3 monthly
    sectors_def(68) = sector_type('wildfires  ', 'biomassburning', .false., 'forestfire  ', 'GFEDv3  ', NULL())

    ! Biomass burning RETRO
    sectors_def(69) = sector_type('fire_emis  ', 'biomassburning', .false., 'forestfire  ', 'RETRO   ',NULL())

    ! CMIP6
    sectors_def(73) = sector_type('ENE       ', 'anthropogenic ', .false., 'combenergy  ', 'CMIP6   ', NULL() ) ! Energy sector
    sectors_def(74) = sector_type('RCO       ', 'anthropogenic ', .false., 'combrescom  ', 'CMIP6   ', NULL() ) ! Residential, commercial and other
    sectors_def(75) = sector_type('IND       ', 'anthropogenic ', .false., 'industry    ', 'CMIP6   ', NULL() ) ! Industrial sector
    sectors_def(76) = sector_type('WST       ', 'anthropogenic ', .false., 'waste       ', 'CMIP6   ', NULL() ) ! Waste treatment and disposal
    sectors_def(77) = sector_type('AGR       ', 'anthropogenic ', .false., 'surface     ', 'CMIP6   ', NULL() ) ! Agriculture (excl. agricultural waste burning, which is included in CMIP6 biomass burning emissions)
    sectors_def(78) = sector_type('SLV       ', 'anthropogenic ', .false., 'nearsurface ', 'CMIP6   ', NULL() ) ! Solvents production and application
    sectors_def(79) = sector_type('TRA       ', 'anthropogenic ', .false., 'surface     ', 'CMIP6   ', NULL() ) ! Transportation sector (land)
    sectors_def(80) = sector_type('SHP       ', 'ships         ', .false., 'nearsurface ', 'CMIP6   ', NULL() ) ! International shipping
    sectors_def(81) = sector_type('AIR       ', 'aircraft      ', .true. , 'aircraft    ', 'CMIP6   ', NULL() ) ! Aircraft

    ! CMIP6BMB
    sectors_def(82) = sector_type('wildfires ', 'biomassburning', .false., 'forestfire  ', 'CMIP6BMB', NULL() ) ! Forest and grassland fires; the latter were assumed 'nearsurface' in AR5
    
    ! -------------------------
    ! info per species
    ! ------------------------
    ! ED42 sectors are not available for all species, so we define a sectors list per species.
    
    !! These are the "ALL AVAILABLE". Kept for reference.
    !ed42_co_sectors = (/'1A1a_6       ', '1A2          ', '1A3a_c_d_e   ', '1A3b         ', '1A4          ', &
    !                    '1B2a_c_1A1b_c', '2A_2B_2D     ', '2C           ', '4F           ', '5A_C_D_F_4E  ', '7A           ' /)
    ! 
    !ed42_ch4_sectors = (/'1A1_1A2    ', '1A3a_c_d_e ', '1A3b       ', '1A4        ', '1B1        ', '1B2a       ', &
    !                     '1B2b       ', '2          ', '4A         ', '4B         ', '4C_4D      ', '4F         ', &
    !                     '5A_C_D_F_4E', '6A_6C      ', '6B         ', '7A         '/)
    ! 
    !ed42_nox_sectors = (/'1A1a         ', '1A2          ', '1A3a_c_d_e   ', '1A3b         ', '1A4          ',&
    !                     '1B2a_c_1A1b_c', '2            ', '4B           ', '4C_4D        ', '4F           ',&
    !                     '5A_C_D_F_4E  ', '6A_6C        ', '7A           ' /)
    ! 
    !ed42_hc_sectors = (/'1A1a             ', '1A1b_c_1B_2C1_2C2', '1A2              ', '1A3a_c_d_e       ', &
    !                    '1A3b             ', '1A4              ', '2A_B_D_E_F_G     ', '3                ', &
    !                    '4F               ', '5A_C_D_F_4E      ', '6A_6C            ', '7A               ' /)
    ! 
    !ed42_nh3_sectors = (/'1A1a_6     ', '1A1b_c     ', '1A2        ', '1A3        ', '1A4        ', '2A         ',&
    !                     '2B         ', '4B         ', '4C_4D      ', '4F         ', '5A_C_D_F_4E' /)
    ! 
    !ed42_so2_sectors = (/'1A1a_6C       ', '1A2           ', '1A3a_c_d_e    ', '1A3b          ', '1A4           ',&
    !                     '1B1_1B2_1A1b_c', '2B_2D         ', '2C            ', '4F            ', '5A_C_D_F_4E   ', '7A            '/)
    

    ! Use only non-transport sectors (they are provided by ED41), and remove biomassburning:

    ed42_co_sectors = (/ '1A1a_6            ', '1A2               ', '1A4               ', '1B2a_c_1A1b_c     ', &
                         '2A_2B_2D          ', '2C                ', '4F                ', '7A                ' /)

    ed42_ch4_sectors = (/'1A1_1A2           ', '1A4               ', '1B1               ', '1B2a              ', &
                         '1B2b              ', '2                 ', '4A                ', '4B                ', &
                         '4C_4D             ', '4F                ', '6A_6C             ', '6B                ', &
                         '7A                '/)

    ed42_nox_sectors = (/'1A1a              ', '1A2               ', '1A4               ', '1B2a_c_1A1b_c     ', &
                         '2                 ', '4B                ', '4C_4D             ', '4F                ', &
                         '6A_6C             ', '7A                ' /)

    ! Note that '5A_C_D_F_4E' is not only fire, but it is available only for the NMVOC species
    !  which we do not use (instead we loop through emis_ar5_nvoc constituents) anyway 
    ed42_hc_sectors =  (/'1A1a              ', '1A1b_c_1B_2C1_2C2 ', '1A2               ', '1A4               ', &
                         '2A_B_D_E_F_G      ', '3                 ', '4F                ', '6A_6C             ', &
                         '7A                ' /)
    
    ed42_nh3_sectors = (/'1A1a_6            ', '1A1b_c            ', '1A2               ', '1A4               ', &
                         '2A                ', '2B                ', '4B                ', '4C_4D             ', &
                         '4F                ' /)

    ed42_so2_sectors = (/'1A1a_6C           ', '1A2               ', '1A4               ', '1B1_1B2_1A1b_c    ', &
                         '2B_2D             ', '2C                ', '4F                ', '7A                '/)

    
    ! ED41 sectors are used only for the transport sector - the screening is
    !  coded in the *declare routines of each emission_*.F90 according to
    !  sector name.  It boils down to four cases:
    !  
    !  (/'1A3a', '1A3b_c_e', '1A3d1', '1A3d_SHIP'/) : NOx
    !  (/'1A3b_c_e', '1A3d1', '1A3d_SHIP'/)         : some NMVOC, CO, CH4, SOx
    !  (/'1A3b_c_e'/)                               : some NMVOC, NH3
    !  NONE                                         : some NMVOC
    
    ! AR5
    sectors_def( 1)%species => ar5_cat_ant_ene_ind
    sectors_def( 2)%species => ar5_cat_ant_dom
    sectors_def( 3)%species => ar5_cat_ant_ene_ind
    sectors_def( 4)%species => ar5_cat_ant
    sectors_def( 5)%species => ar5_cat_ant_agr
    sectors_def( 6)%species => ar5_cat_ant_awb
    sectors_def( 7)%species => ar5_cat_ant_slv
    sectors_def( 8)%species => ar5_cat_ant_tra
    sectors_def( 9)%species => ar5_cat_shp
    sectors_def(10)%species => ar5_cat_air
    sectors_def(11)%species => ar5_cat_bmb
    sectors_def(12)%species => ar5_cat_bmb

    n_ar5_ant_sec=8 ! hardcoded but could be counted
    n_ar5_shp_sec=1
    n_ar5_air_sec=1
    n_ar5_bmb_sec=2

    ! -------------------------
    ! initialise providers info
    ! ------------------------
    do iprov = 1, numb_providers
       providers_def(iprov)%name    = all_providers(iprov)
       providers_def(iprov)%nsect2d = count( (sectors_def%prov == all_providers(iprov)) .and. (sectors_def%f3d .eqv. .false.))
       providers_def(iprov)%nsect3d = count( (sectors_def%prov == all_providers(iprov)) .and. (sectors_def%f3d .eqv. .true.))
       
       if(okdebug) then
          write(gol,'("EMISS-INFO - Inventory ",a," has ",i3, " 2d-sectors, and ",i3," 3d-sectors")')&
               & all_providers(iprov), providers_def(iprov)%nsect2d, providers_def(iprov)%nsect3d ; call goPr
       endif
    end do


    ! -------------------------
    ! initialise GeopPotential Height on 1x1
    ! ------------------------
    do region=1, nregions
       call Set( gph_dat(region), status, used=.true. )
    end do

    
    ! ----------------------------------------
    ! allocate gridbox_area arrays
    ! ----------------------------------------
    allocate( gridbox_area_025( nlon1440, nlat720 ) )
    allocate( gridbox_area_05 ( nlon720 , nlat360 ) )
#ifdef with_ch4_emis
    allocate( lpj_gridbox_area   ( lpj_dim_nlon, lpj_dim_nlat     ) )
    allocate( lpj_frac_wetlands  ( lpj_dim_nlon, lpj_dim_nlat, 12 ) )
    allocate( lpj_frac_rice      ( lpj_dim_nlon, lpj_dim_nlat, 12 ) )
    allocate( lpj_frac_peatlands ( lpj_dim_nlon, lpj_dim_nlat     ) )
#endif
   ! OK 
    status = 0

  END SUBROUTINE EMISSION_READ_INIT
  !EOC


  !--------------------------------------------------------------------------
  !                    TM5                                                  !
  !--------------------------------------------------------------------------
  !BOP
  !
  ! !IROUTINE:    EMISSION_READ_DONE
  !
  ! !DESCRIPTION: Free allocated arrays.
  !\\
  !\\
  ! !INTERFACE:
  !
  subroutine emission_read_done(  status )
    !
    ! !OUTPUT PARAMETERS:
    !
    integer, intent(out) ::  status
    !
    ! !REVISION HISTORY: 
    !    1 Oct 2010 - Achim Strunk - v0
    !
    !EOP
    !------------------------------------------------------------------------
    !BOC

    character(len=*), parameter ::  rname=mname//'/emission_read_done'  

    deallocate( gridbox_area_025 )
    deallocate( gridbox_area_05  )
#ifdef with_ch4_emis
    deallocate( lpj_gridbox_area   )
    deallocate( lpj_frac_wetlands  )
    deallocate( lpj_frac_rice      )
    deallocate( lpj_frac_peatlands )
#endif

    if (allocated( used_providers     )) deallocate( used_providers      )
    if (allocated( used_providers_ch4 )) deallocate( used_providers_ch4  )
    if (allocated( used_providers_isop)) deallocate( used_providers_isop )
    if (allocated( used_providers_terp)) deallocate( used_providers_terp )
    if (allocated( used_providers_aer )) deallocate( used_providers_aer  )

    ! OK
    status = 0

  END SUBROUTINE EMISSION_READ_DONE
  !EOC

  !--------------------------------------------------------------------------
  !                    TM5                                                  !
  !--------------------------------------------------------------------------
  !BOP
  !
  ! !FUNCTION:    EMISSION_COARSEN_TO_1X1
  !
  ! !DESCRIPTION: Coarsen the gridded information to 1x1 deg.
  !               (taken from GEMS/MACC repository)
  !\\
  !\\
  ! !INTERFACE:
  !
  function emission_coarsen_to_1x1( emis_in, dim_nlon, dim_nlat, shift_lon, status )
    !
    ! !RETURN VALUE:
    !
    real, dimension(360,180) :: emission_coarsen_to_1x1
    !
    ! !INPUT PARAMETERS:
    !
    integer,  intent(in)    :: dim_nlon
    integer,  intent(in)    :: dim_nlat
    real,     intent(in)    :: emis_in(dim_nlon, dim_nlat)
    logical,  intent(in)    :: shift_lon
    !
    ! OUTPUT PARAMETERS:
    !
    integer          , intent(out)   :: status
    !
    ! !REVISION HISTORY: 
    !    1 Oct 2010 - Achim Strunk  - v0 for AR5
    !    1 Dec 2011 - Narcisa Banda - works for any input resolution lower than 1x1 
    !                                 if 1x1 can be divided into exact number of gridcells (no interpolation)
    !    1 Jul 2012 - Narcisa Banda - added the shift_lon logical flag:
    !                                 true if the data is read on longitudes [0,360] (then they need to be shifted on [-180,180])
    !                                 false if the data is read already on [-180,180]
    !
    !EOP
    !------------------------------------------------------------------------
    !BOC

    integer                 ::  i, j
    integer                 ::  nri, nrj

    ! --- begin -----------------------------------

    ! combine grid cells :
    !   from  [   0,360]x[-90,90]             001:360,361:720    001:360
    !   to    [-180,180]x[-90,90]     001:180,181:360            001:180

    if ((mod(dim_nlon, 360) /= 0 ) .or. (mod(dim_nlat, 180) /= 0)) then
       write(gol,*) 'coarsening of emissions to 1x1 does not work for this resolution' ; call goErr
       status = 1 
       return
    endif

    nri = dim_nlon/360
    nrj = dim_nlat/180

    if (shift_lon) then 
    ! combine grid cells :
    !   from  [   0,360]x[-90,90]             001:360,361:720    001:360
    !   to    [-180,180]x[-90,90]     001:180,181:360            001:180

       do j = 1, 180
          ! west half
          do i = 1, 180
             emission_coarsen_to_1x1(i,j) = sum(emis_in(nri*180+nri*i-nri+1:nri*180+nri*i,nrj*j-nrj+1:nrj*j))
          end do
          ! east half
          do i = 1, 180
             emission_coarsen_to_1x1(180+i,j) = sum(emis_in(nri*i-nri+1:nri*i,nrj*j-nrj+1:nrj*j))
          end do
       end do
    else
       do j=1, 180
          do i=1, 360
             emission_coarsen_to_1x1(i,j) = sum(emis_in(nri*i-nri+1:nri*i,nrj*j-nrj+1:nrj*j))
          end do
       end do
    endif
    ! ok
    status = 0

  end function emission_coarsen_to_1x1
  !EOC

  
  !--------------------------------------------------------------------------
  !                    TM5                                                  !
  !--------------------------------------------------------------------------
  !BOP
  !
  ! !FUNCTION:    VALID_YEAR
  !
  ! !DESCRIPTION: return a valid year for an emission inventory, based on
  !               requested year. 
  !\\
  !\\
  ! !INTERFACE:
  !
  FUNCTION VALID_YEAR( iyear, iminmax, provider_name, verbose)
    !
    ! !RETURN VALUE:
    !
    integer :: valid_year
    !
    ! !INPUT PARAMETERS:
    !
    integer,          intent(in) :: iyear, iminmax(2)
    character(len=*), intent(in) :: provider_name
    logical,          intent(in) :: verbose
    !
    ! !REVISION HISTORY: 
    !   26 Nov 2012 - Ph. Le Sager - v0
    !
    !EOP
    !------------------------------------------------------------------------
    !BOC

    valid_year = MIN(iminmax(2),MAX(iyear,iminmax(1)))

    ! info only once a year, and per inventory
    if (verbose) then
       write(gol,'(a,i4," (avail: ",i4,"-",i4,")")') ' EMISS-INFO - EMISS YEAR for '//trim(provider_name)//' : ', &
            valid_year, iminmax ; call goPr       
    end if

  END FUNCTION VALID_YEAR
  !EOC

  
  !--------------------------------------------------------------------------
  !                    TM5                                                  !
  !--------------------------------------------------------------------------
  !BOP
  !
  ! !IROUTINE:    EMISSION_CMIP6_READSECTOR
  !
  ! !DESCRIPTION: Reading one sector of the files for the requested month and 
  !               returning an interpolated 3d emission field (d3data)
  !               and, for the CMIP6 2-D sectors, an interpolated 2d field
  !               containing emissions from solid biofuel combustion (d2data_bf). 
  !\\
  !\\
  ! !INTERFACE:
  !
  subroutine emission_cmip6_ReadSector( comp, iyear, imonth, sector, d3data, status, d2data_bf )
    !
    use dims         , only          : icalendo
    !
    ! !INPUT PARAMETERS:
    !
    character(len=*) , intent(in)    :: comp
    integer          , intent(in)    :: iyear 
    integer          , intent(in)    :: imonth 
    integer          , intent(in)    :: sector
    !
    ! !OUTPUT PARAMETERS:
    !
    integer          , intent(out)   :: status
    real, dimension(nlon360,nlat180,ar5_dim_3ddata), intent(out)   :: d3data
    real, dimension(nlon360,nlat180), intent(out), optional :: d2data_bf
    !
    !EOP
    !------------------------------------------------------------------------
    !BOC

    character(len=*), parameter :: rname = mname//'/emission_cmip6_readsector'
    character(len=256)          :: fname
    character(len=256)          :: fname_gridboxarea
    character(len=256)          :: varfilename, varname, secname
    integer                     :: lt, year, startyear
    character(len=25)           :: sec_str, sec_str2 
    character(len=13)           :: time_str 
    character(len=60)           :: source_str
    character(len=50)           :: version_str
    logical                     :: existfile
    character(len=4)            :: cyear       
    logical                     :: first=.true.

    ! --- begin -----------------------------------

    ! initialise target array
    d3data = 0.0
    if (present(d2data_bf)) d2data_bf = 0.0

    ! read in gridbox-area; once per CPU
    if( .not. area_found_05 ) then
       fname_gridboxarea = trim(emis_input_dir_cmip6)//'/'//trim(cmip6_filestr_gridboxarea)
       call emission_ReadGridboxArea(fname_gridboxarea, 'gridbox_area', gridbox_area_05, &
             & nlon720, nlat360, status )
       IF_NOTOK_RETURN(status=1)
       area_found_05=.true.
    endif

    ! deal with out-of-bounds requested years
    year = valid_year( iyear, cmip6_avail, 'CMIP6', first)
    first=.false.

    if ( trim(sectors_def(sector)%catname) == 'aircraft' .and. year < 1920 ) then

      ! CMIP6 aircraft emissions before 1920 are zero and not read anymore
      d3data(:,:,:) = 0. 

    else

      ! cyear will contain strings with the years
      write(cyear,'(I4.4)') year

      ! ------------------------
      ! construct filename
      ! e.g.: <emisdir>/NOx-em-AIR-anthro_input4MIPs_emissions_CMIP_CEDS-v2016-06-18_gr_175001-179912.nc
      ! ------------------------

      if ( index(comp,'CH4') /= 1 ) then
        if (year >= 1750 .and. year < 1800) then
          time_str='175001-179912'
          startyear=1750
        else if (year >= 1800 .and. year < 1850) then
          time_str='180001-184912'
          startyear=1800
        else if (year >= 1850 .and. year < 1851) then
          time_str='185001-185012'
          startyear=1850
        else if (year >= 1851 .and. year < 1900) then
          time_str='185101-189912'
          startyear=1851
        else if (year >= 1900 .and. year < 1950) then
          time_str='190001-194912'
          startyear=1900
        else if (year >= 1950 .and. year < 2000) then
          time_str='195001-199912'
          startyear=1950
        else if (year >= 2000 .and. year < 2015) then
          time_str='200001-201412'
          startyear=2000
        else if (year >= 2015 .and. year <= 2100) then
          time_str='201501-210012'
          startyear=2015
        else
          write (gol,'("CMIP6 emissions beyond 2100 not available")') ; call goErr
          status=1; TRACEBACK; return 
        endif

        if (year >= 1750 .and. year < 2015) then
          if (trim(sectors_def(sector)%catname) == 'aircraft') then
            if ( index(comp,'SO2') /= 1 ) then
              version_str='2017-08-30'
            else
              ! SO2 aicraft emissions have had another update in Oct. 2017
              version_str='2017-10-05'
            endif
          else
            version_str='2017-05-18'
          endif
        else if (year >= 2015 .and. year <=2100) then
          version_str='1-1'
        else
          write (gol,'("CMIP6 emissions beyond 2100 not available")') ; call goErr
          status=1; TRACEBACK; return
        endif
      else
        ! CH4
        if (year >= 1750 .and. year < 1850) then
          write (gol,'("WARNING - Anthropogenic emissions of CH4 not available prior to 1850")') ; call goPr 
          write (gol,'("WARNING - 1850 values are used")') ; call goPr
          year = 1850 
        endif

        if (year >= 1850 .and. year < 1970) then 
          time_str='185001-196012'
          startyear=1850
          version_str='2017-05-18-supplemental-data'
        else if (year >= 1970 .and. year < 2015) then
          time_str='197001-201412'
          startyear=1970
          version_str='2017-05-18'
        else if (year >= 2015 .and. year <= 2100) then
          time_str='201501-210012'
          startyear=2015
          version_str='1-1'
        else
          write (gol,'("CMIP6 emissions beyond 2100 not available")') ; call goErr
          status=1; TRACEBACK; return
        endif

      endif

      if (year <= 2014 ) then
        source_str='input4MIPs_emissions_CMIP_CEDS'
      else
        select case(trim(ssp_name))
        case ("SSP3-7.0")
          source_str='input4MIPs_emissions_ScenarioMIP_IAMC-AIM-ssp370'

          ! Use SSP370-LowNTCF for specific species
          ! NMVOC component names differ for surface and aircraft emissions
          if ( ((index(comp,'BC'    ) ==1) .and. LSSP370_LowBC   ) .or. &
               ((index(comp,'OC'    ) ==1) .and. LSSP370_LowOC   ) .or. &
               ((index(comp,'SO2'   ) ==1) .and. LSSP370_LowSOx  ) .or. &
               ((index(comp,'NH3'   ) ==1) .and. LSSP370_LowNH3  ) .or. &
               ((index(comp,'NOx'   ) ==1) .and. LSSP370_LowNOx  ) .or. &
               ((index(comp,'CO'    ) ==1) .and. LSSP370_LowCO   ) .or. &
               ((index(comp,'VOC'   ) ==1) .and. LSSP370_LowNMVOC) .or. &  
               ((index(comp,'NMVOC' ) ==1) .and. LSSP370_LowNMVOC) .or. &
               ((index(comp,'CH4'   ) ==1) .and. LSSP370_LowCH4  )  ) then
            source_str='input4MIPs_emissions_AerChemMIP_IAMC-AIM-ssp370-lowNTCF'
          endif

        case ("SSP3-LowNTCF")
          source_str='input4MIPs_emissions_AerChemMIP_IAMC-AIM-ssp370-lowNTCF'
          
        case default
          write (gol,'("Emissions not implemented for scenario: ", a)') trim(ssp_name); call goErr
          status=1; TRACEBACK; return
        end select
      endif
    
      if ( trim(sectors_def(sector)%catname) == 'anthropogenic' .or. &
           trim(sectors_def(sector)%catname) == 'ships' ) then
        if ( index(comp,'VOC') == 1 ) then
          ! individual VOC species
          sec_str='em-speciated-VOC-anthro'
          sec_str2='em_speciated_VOC_anthro'
          version_str=trim(version_str)//'-supplemental-data'
        else
          sec_str='em-anthro'
          sec_str2='em_anthro'
        endif
        varname=trim(comp)//'_'//trim(sec_str2)
        ! change dash to underscore in few cases
        if ( index(varname, 'VOC') == 1 )        varname(6:6)= '_'
        if ( index(varname, 'hexanes-pl') == 7 ) varname(7:16) = 'hexanes_pl'
        if ( index(varname, 'other-') == 7 )     varname(7:12) = 'other_'
      else if ( trim(sectors_def(sector)%catname) == 'aircraft' ) then
        sec_str='em-AIR-anthro'
        sec_str2='em_AIR_anthro'
        varname=trim(comp)//'_'//trim(sec_str2)
      else
        write (gol,'("Invalid CMIP6 sector")') ; call goErr
        status=1; TRACEBACK; return
      endif
      varfilename=trim(comp)//'-'//trim(sec_str)

      ! For NO, the species name in the file name has to be set to NOx
      fname = trim(emis_input_dir_cmip6)           //'/'//   &
              trim(varfilename)                    //'_'//   &
              trim(source_str)                     //'-'//   &
              trim(version_str)                    //'_'//   &
              'gn'                                 //'_'//   &
              trim(time_str)                            //   &
              '.nc'

      ! test existence of file
      inquire( file=trim(fname), exist=existfile)
      if( .not. existfile ) then 
        write (gol,'(" CMIP6 file `",a,"` not found ")') trim(fname); call goErr
        status = 1; TRACEBACK; return 
      end if

      ! ------------------------------------------------
      ! data record is read by emission_cmip6_Read2/3DRecord
      secname = sectors_def(sector)%name
      ! distinguish 2d/3d sectors
      if( sectors_def(sector)%f3d ) then 
        d3data(:,:,:) = emission_cmip6_Read3DRecord( fname, varname, secname, imonth, year, startyear, status )
      else
        d3data(:,:,1) = emission_cmip6_Read2DRecord( fname, varname, secname, imonth, year, startyear, status )

        ! Read mass emitted by solid biofuel burning
        ! for carbonaceous aerosol.
        ! Reading of biofuel emissions is done for all 2-D sectors,
        ! even though in CMIP6 there are only contributions for RCO, IND, ENE and TRA.
        if (present(d2data_bf)) then 
          if ( (index(comp,'BC') /= 1) .and. (index(comp,'OC') /= 1) ) then
            write (gol,'("Reading solid biofuel emissions only implemented for BC and OC")'); call goErr
            status = 1; TRACEBACK; return
          endif

          sec_str='em-SOLID-BIOFUEL-anthro'
          sec_str2='em_SOLID_BIOFUEL_anthro'
          varname=trim(comp)//'_'//trim(sec_str2)
          varfilename=trim(comp)//'-'//trim(sec_str)
          fname = trim(emis_input_dir_cmip6)           //'/'//   &
                  trim(varfilename)                    //'_'//   &
                  trim(source_str)                     //'-'//   &
                  trim(version_str)                    //'-'//   &
                  'supplemental-data'                  //'_'//   &
                  'gn'                                 //'_'//   &
                  trim(time_str)                            //   &
                  '.nc'

          ! test existence of file
          inquire( file=trim(fname), exist=existfile)
          if( .not. existfile ) then
            write (gol,'(" CMIP6 file `",a,"` not found ")') trim(fname); call goErr
            status = 1; TRACEBACK; return
          end if

          d2data_bf(:,:) = emission_cmip6_Read2DRecord( fname, varname, secname, imonth, year, startyear, status )

        endif

      end if

    endif

    IF_NOTOK_RETURN(status=1)

  end subroutine emission_cmip6_ReadSector
  !EOC

  !--------------------------------------------------------------------------
  !                    TM5                                                  !
  !--------------------------------------------------------------------------
  !BOP
  !
  ! !IROUTINE:    EMISSION_CMIP6BMB_READSECTOR
  !
  ! !DESCRIPTION: Reading one sector of the files for the requested month and 
  !               returning an interpolated 3d emission field (d3data)
  !\\
  !\\
  ! !INTERFACE:
  !
  subroutine emission_cmip6bmb_ReadSector( comp, iyear, imonth, sector, d3data, status )
    !
    ! !INPUT PARAMETERS:
    !
    character(len=*) , intent(in)    :: comp
    integer          , intent(in)    :: iyear 
    integer          , intent(in)    :: imonth 
    integer          , intent(in)    :: sector
    !
    ! !OUTPUT PARAMETERS:
    !
    integer          , intent(out)   :: status
    real, dimension(nlon360,nlat180,ar5_dim_3ddata), intent(out)   :: d3data
    !
    !EOP
    !------------------------------------------------------------------------
    !BOC

    character(len=*), parameter :: rname = mname//'/emission_cmip6bmb_readsector'
    character(len=256)          :: fname
    character(len=256)          :: fname_gridboxarea
    character(len=256)          :: varfilename, varname
    integer                     :: lt, year, startyear
    integer                     :: j
    character(len=50)           :: sec_str
    character(len=13)           :: time_str
    character(len=60)           :: source_str
    character(len=50)           :: version_str
    logical                     :: existfile
    character(len=4)            :: cyear       
    logical                     :: first=.true.
    real, dimension(:,:), allocatable :: d3data_help

    ! --- begin -----------------------------------

    if ( trim(sectors_def(sector)%catname) /= 'biomassburning' ) then
       write (gol,'("Invalid CMIP6 sector")') ; call goErr
       status=1; TRACEBACK; return
    endif

    ! initialise target array
    d3data = 0.0

    ! deal with out-of-bounds requested years
    year = valid_year( iyear, cmip6_avail, 'CMIP6BMB', first)
    first=.false.

    ! cyear will contain strings with the years
    write(cyear,'(I4.4)') year

    if (year <= 2014) then
       
       sec_str='em-biomassburning'
       source_str='input4MIPs_emissions_CMIP_VUA'
       version_str='CMIP-BB4CMIP6-1-2'

       ! read in gridbox-area; once per CPU
       ! Only historical data are at 0.25x0.25 degree 
       if ( .not. area_found_025 ) then
          fname_gridboxarea = trim(emis_input_dir_cmip6)           //'/'//   &
                              'gridcellarea'                       //'-'//   &
                              trim(sec_str)                        //'_'//   &
                              trim(source_str)                     //'-'//   &
                              trim(version_str)                    //'_'//   &
                              'gn.nc'
          ! no need to swap the latitude order here
          call emission_ReadGridboxArea(fname_gridboxarea, 'gridcellarea', gridbox_area_025, &
                & nlon1440, nlat720, status )
          IF_NOTOK_RETURN(status=1)
          area_found_025=.true.
       endif

    else
       if (index(comp,'NMVOC-') == 1) then
         sec_str='em-speciated-VOC-openburning' 
       else
         sec_str='em-openburning'
       endif

       select case(trim(ssp_name))
       case ("SSP3-7.0")
         source_str='input4MIPs_emissions_ScenarioMIP_IAMC-AIM-ssp370'

         ! Use SSP370-LowNTCF for specific species         
         if ( ((index(comp,'BC'    ) ==1) .and. LSSP370_LowBC   ) .or. &
              ((index(comp,'OC'    ) ==1) .and. LSSP370_LowOC   ) .or. &
              ((index(comp,'SO2'   ) ==1) .and. LSSP370_LowSOx  ) .or. &
              ((index(comp,'NH3'   ) ==1) .and. LSSP370_LowNH3  ) .or. &
              ((index(comp,'NOx'   ) ==1) .and. LSSP370_LowNOx  ) .or. &
              ((index(comp,'CO'    ) ==1) .and. LSSP370_LowCO   ) .or. &
              ((index(comp,'NMVOC-') ==1) .and. LSSP370_LowNMVOC) .or. &
              ((index(comp,'CH4'   ) ==1) .and. LSSP370_LowCH4  )  ) then
            source_str='input4MIPs_emissions_AerChemMIP_IAMC-AIM-ssp370-lowNTCF'
         endif
         
       case ("SSP3-LowNTCF")
         source_str='input4MIPs_emissions_AerChemMIP_IAMC-AIM-ssp370-lowNTCF'

       case default
         write (gol,'("Emissions not implemented for scenario: ",a)') trim(ssp_name); call goErr
         status=1; TRACEBACK; return
       end select

       version_str='1-1'
    endif

    ! ------------------------
    ! construct filename
    ! e.g.: <emisdir>/NOx-em-biomassburning_input4MIPs_emissions_CMIP_VUA-CMIP-BB4CMIP6-1-2_gn_175001-184912.nc
    ! ------------------------
    varfilename=trim(comp)//'-'//trim(sec_str)
    if (index(comp,'NMVOC-') == 1) then
       ! change dash to underscore in few cases
       if (year <= 2014) then
         varname = trim(comp(7:))
         ! change dash to underscore in few cases
         if (varname(1:7) == 'Higher-') varname(1:7) = 'Higher_'
       else
          varname = trim(comp)
          varname(1:6) = 'NMVOC_'
          if (varname(7:13) == 'Higher-') varname(7:13) = 'Higher_'
          varname = trim(varname) // '_em_speciated_VOC_openburning'
       endif
    else
       if (year <= 2014) then
          varname=trim(comp)
       else
          varname = trim(comp) // '_em_openburning'
       endif
    endif

    if (year >= 1750 .and. year < 1850) then
       time_str='175001-184912'
       startyear=1750
    else if (year >= 1850 .and. year <= 2014) then
       time_str='185001-201512'
       startyear=1850
    else if (year >= 2015 .and. year <= 2100) then
       time_str='201501-210012'
       startyear=2015
    else
      write (gol,'("CMIP6 emissions beyond 2100 not available yet")') ; call goErr
      status=1; TRACEBACK; return 
    endif
   
    if (year <= 2014) then 
       fname = trim(emis_input_dir_cmip6)           //'/'//   & 
               trim(varfilename)                    //'_'//   &
               trim(source_str)                     //'-'//   &
               trim(version_str)                    //'_'//   &
               'gn'                                 //'_'//   &
               trim(time_str)                            //   &
               '.nc' 
    else
       if (index(comp,'NMVOC-') == 1) then
          fname = trim(emis_input_dir_cmip6)           //'/'//   &
               trim(varfilename)                    //'_'//   &
               trim(source_str)                     //'-'//   &
               trim(version_str)                    //'-'//   &
               'supplemental-data'                  //'_'//   &
               'gn'                                 //'_'//   &
               trim(time_str)                            //   &
               '.nc'
       else
          fname = trim(emis_input_dir_cmip6)           //'/'//   &
               trim(varfilename)                    //'_'//   &
               trim(source_str)                     //'-'//   &
               trim(version_str)                    //'_'//   &
               'gn'                                 //'_'//   &
               trim(time_str)                            //   &
               '.nc'
       endif
    endif

    ! test existence of file
    inquire( file=trim(fname), exist=existfile)
    if( .not. existfile ) then 
       write (gol,'(" CMIP6 file `",a,"` not found ")') trim(fname); call goErr
       status = 1; TRACEBACK; return 
    end if


    if (year <= 2014) then

      allocate(d3data_help(nlon360,nlat180))

      ! ------------------------------------------------
      ! data record is read by emission_cmip6bmb_Read2DRecord
      d3data_help(:,:) = emission_cmip6bmb_Read2DRecord( fname, varname, imonth, year, startyear, status )
      IF_NOTOK_RETURN(status=1)
      ! reverse latitudes
      do j=1,nlat180
        d3data(:,j,1) = d3data_help(:,nlat180-j+1)
      enddo

      deallocate(d3data_help)

    else
      ! ------------------------------------------------
      ! data record is read by emission_cmip6bmb_ssp_Read2DRecord
      d3data(:,:,1) = emission_cmip6bmb_ssp_Read2DRecord( fname, varname, imonth, year, startyear, status)
      IF_NOTOK_RETURN(status=1)

    endif

  end subroutine emission_cmip6bmb_ReadSector
  !EOC



  !--------------------------------------------------------------------------
  !                    TM5                                                  !
  !--------------------------------------------------------------------------
  !BOP
  !
  ! !FUNCTION:    EMISSION_CMIP6_READ2DRECORD
  !
  ! !DESCRIPTION: Read a single 2d record of a given file and 
  !               return a 2d emission field interpolated on 1x1 grid.
  !\\
  !\\
  ! !INTERFACE:
  !
  function emission_cmip6_Read2DRecord( fname, varname, secname, imonth, year, startyear, status )
    !
    ! !RETURN VALUE:
    !
    real                  :: emission_cmip6_Read2DRecord(nlon360,nlat180)
    !
    ! !INPUT PARAMETERS:
    !
    character(len=*),                intent(in) :: fname, varname
    character(len=sector_name_len),  intent(in) :: secname
    integer,                         intent(in) :: imonth, year, startyear
    !
    ! !OUTPUT PARAMETERS:
    !
    integer,                        intent(out) :: status
    !
    !EOP
    !------------------------------------------------------------------------
    !BOC

    character(len=*), parameter ::  rname = mname//'/emission_cmip6_Read2DRecord'
    character(len=256)          ::  fname2

    integer :: fid, varid, isec
    integer :: fid2, varid2, year_first, year_second
    !real    :: emis_in(nlon720, nlat360, 1)
    real    :: emis_in(nlon720, nlat360, 1, 1)
    real, allocatable :: emis_help(:,:,:,:)
    real    :: x

    ! --- begin -----------------------------------

    select case( trim(secname) )
      case ('AGR')
        isec=0
      case ('ENE')
        isec=1
      case ('IND')
        isec=2
      case ('TRA')
        isec=3
      case ('RCO')
        isec=4
      case ('SLV')
        isec=5
      case ('WST')
        isec=6
      case ('SHP')
        isec=7
      case default
        write (gol,'("EMISS - CMIP6 - no `",a,"` emissions in file ",a)') &
            secname, trim(fname) ; call goErr
       status=1; TRACEBACK; return
    end select

    ! initialise
    emission_cmip6_Read2DRecord = 0.0

    CALL MDF_Open( TRIM(fname), MDF_NETCDF, MDF_READ, fid, status )
    IF_NOTOK_RETURN(status=1)

    CALL MDF_Inq_VarID( fid, TRIM(varname), varid, status )
    IF_ERROR_RETURN(status=1)

    if ( varid < 0 ) then

       write (gol,'("EMISS - CMIP6 - no `",a,"` emissions in file ",a)') &
            varname, trim(fname) ; call goErr
       status=1; TRACEBACK; return

    else
       if( okdebug ) then
          write (gol,'("EMISS-INFO - CMIP6 - found `",a,"` emissions in file ",a)') &
               varname, trim(fname) ; call goErr 
       endif

       ! Special case for methane emissions prior to 1970
       ! which are provided at 10-year intervals,
       ! starting at 1850.
       if (index(varname,'CH4') == 1 .and. year < 1970) then
         ! First year of the decade:
         year_first = int(year/10) * 10

         CALL MDF_Get_Var( fid, varid, emis_in, status, start=(/1,1,isec+1,imonth+12*(year_first-startyear)/10/) )
         IF_NOTOK_RETURN(status=1)

         if (year /= year_first) then

            ! Also read data for the first year of the next decade
            ! and apply a linear interpolation between the two years
            allocate(emis_help(nlon720, nlat360, 1, 1))
            year_second = year_first + 10
            if (year_second == 1970) then
              ! Read 1970 data from the regular 1970-2014 file
              fname2 = trim(emis_input_dir_cmip6)//'/'// &
                       'CH4-em-anthro_input4MIPs_emissions_CMIP_CEDS-2017-05-18_gn_197001-201412.nc'

              CALL MDF_Open( TRIM(fname2), MDF_NETCDF, MDF_READ, fid2, status )
              IF_NOTOK_RETURN(status=1)

              CALL MDF_Inq_VarID( fid2, TRIM(varname), varid2, status )
              IF_ERROR_RETURN(status=1)

              if ( varid2 < 0 ) then

                 write (gol,'("EMISS - CMIP6 - no `",a,"` emissions in file ",a)') &
                      varname, trim(fname2) ; call goErr
                 status=1; TRACEBACK; return

              else

                 if ( okdebug ) then
                    write (gol,'("EMISS-INFO - CMIP6 - found `",a,"` emissions in file ",a)') &
                         varname, trim(fname2) ; call goErr
                 endif
              
                 CALL MDF_Get_Var( fid2, varid2, emis_help, status, start=(/1,1,isec+1,imonth/) )
                 IF_NOTOK_RETURN(status=1)
              endif

              CALL MDF_Close( fid2, status )
              IF_NOTOK_RETURN(status=1)
              
            else
              ! Read from the file containing the data prior to 1970,
              ! which is already open
              CALL MDF_Get_Var( fid, varid, emis_help, status, start=(/1,1,isec+1,imonth+12*(year_second-startyear)/10/) )
            endif

            ! Interpolate montly data between the two provided years
            x = float(year-year_first)/10.
            emis_in(:,:,1,1) = (1.-x) * emis_in(:,:,1,1) + x * emis_help(:,:,1,1)

            deallocate(emis_help)

         endif

       ! SSP scenario emissions are provided for 2015, 2020, 2030, ... 2100
       else if (year >= 2015 .and. year < 2020) then

         ! First year of the period 
         year_first = 2015

         CALL MDF_Get_Var( fid, varid, emis_in, status, start=(/1,1,isec+1,imonth/) )
         IF_NOTOK_RETURN(status=1)

         if (year /= year_first) then

            ! Also read data for 2020
            ! and apply a linear interpolation between the two years
            allocate(emis_help(nlon720, nlat360, 1, 1))
            year_second = 2020

            ! Read data for 2020 by raising the index by 12
            CALL MDF_Get_Var( fid, varid, emis_help, status, start=(/1,1,isec+1,imonth+12/) )

            ! Interpolate montly data between the two provided years
            x = float(year-year_first)/5.
            emis_in(:,:,1,1) = (1.-x) * emis_in(:,:,1,1) + x * emis_help(:,:,1,1)

            deallocate(emis_help)

         endif

       else if (year >= 2020) then

         ! First year of the decade:
         year_first = int(year/10) * 10

         ! Change to reference year to 2020 by raising the index by 12
         CALL MDF_Get_Var( fid, varid, emis_in, status, start=(/1,1,isec+1,imonth+12+12*(year_first-2020)/10/) )
         IF_NOTOK_RETURN(status=1)

         if (year /= year_first) then

            ! Also read data for the first year of the next decade
            ! and apply a linear interpolation between the two years
            allocate(emis_help(nlon720, nlat360, 1, 1))
            year_second = year_first + 10

            CALL MDF_Get_Var( fid, varid, emis_help, status, start=(/1,1,isec+1,imonth+12+12*(year_second-2020)/10/) )

            ! Interpolate monthly data between the two provided years
            x = float(year-year_first)/10.
            emis_in(:,:,1,1) = (1.-x) * emis_in(:,:,1,1) + x * emis_help(:,:,1,1)

            deallocate(emis_help)

         endif

       else 

         ! read yearly emissions directly from file
         CALL MDF_Get_Var( fid, varid, emis_in, status, start=(/1,1,isec+1,imonth+12*(year-startyear)/) )
         IF_NOTOK_RETURN(status=1)

       endif

       ! convert from kg(species)/m^2/s  to  kg(species)/gridbox/s
       emis_in(:,:,1,1) = emis_in(:,:,1,1) * gridbox_area_05
       
       ! now coarsen to nlon360,nlat180
       emission_cmip6_Read2DRecord = emission_coarsen_to_1x1( emis_in(:,:,1,1), nlon720, nlat360,.false., status )
       IF_NOTOK_RETURN(status=1)

    end if

    CALL MDF_Close( fid, status )
    IF_NOTOK_RETURN(status=1)
 
    status = 0
    return

  end function emission_cmip6_Read2DRecord
  !EOC

  !--------------------------------------------------------------------------
  !                    TM5                                                  !
  !--------------------------------------------------------------------------
  !BOP
  !
  ! !FUNCTION:    EMISSION_CMIP6_READ3DRECORD
  !
  ! !DESCRIPTION: read one 3D sector
  ! 
  !\\
  !\\
  ! !INTERFACE:
  !
  function emission_cmip6_Read3DRecord( fname, varname, secname, imonth, year, startyear, status )
    !
    ! !RETURN VALUE:
    !
    real :: emission_cmip6_Read3DRecord(nlon360,nlat180,ar5_dim_3ddata)
    !
    ! !INPUT/OUTPUT PARAMETERS:
    !
    character(len=*),                   intent(in)    :: fname, varname
    character(32),                      intent(in)    :: secname
    !
    ! !INPUT PARAMETERS:
    !
    integer,                            intent(in)    :: imonth, year, startyear
    !
    ! !OUTPUT PARAMETERS:
    !
    integer,                            intent(out)   :: status
    !
    ! !REVISION HISTORY: 
    !    1 Oct 2010 - Achim Strunk -
    !
    ! !REMARKS:
    !
    !EOP
    !------------------------------------------------------------------------
    !BOC

    character(len=*), parameter ::  rname = mname//'/emission_cmip6_Read3DRecord'

    integer :: fid, varid, lk
    integer :: fid2, varid2, year_first, year_second
    real, dimension(nlon720,nlat360,ar5_dim_3ddata,1) :: emis_in
    real, allocatable :: emis_help(:,:,:,:)
    real    :: x

    real, parameter      :: layer_depth = 610. ! fixed height (m) of the vertical levels
                                               ! on which the CMIP6 aircraft emissions
                                               ! are provided.
    
    ! --- begin -----------------------------------

    ! initialise
    emission_cmip6_Read3DRecord = 0.0

    CALL MDF_Open( TRIM(fname), MDF_NETCDF, MDF_READ, fid, status )
    IF_NOTOK_RETURN(status=1)

    CALL MDF_Inq_VarID( fid, TRIM(varname), varid, status )
    IF_ERROR_RETURN(status=1)

    if ( varid < 0 ) then
       write (gol,'("EMISS - CMIP6 - no `",a,"` emissions in file ",a)') &
            secname, trim(fname) ; call goErr
       status=1; TRACEBACK; return
    else
       if( okdebug ) then
          write (gol,'("EMISS-INFO - CMIP6 - found `",a,"` emissions in file ",a)') &
               secname, trim(fname) ; call goPr
       endif

       ! SSP scenario emissions are provided for 2015, 2020, 2030, ... 2100
       if (year >= 2015 .and. year < 2020) then

         ! First year of the period 
         year_first = 2015

         CALL MDF_Get_Var( fid, varid, emis_in, status, start=(/1,1,1,imonth/) )
         IF_NOTOK_RETURN(status=1)

         if (year /= year_first) then

            ! Also read data for 2020
            ! and apply a linear interpolation between the two years
            allocate(emis_help(nlon720, nlat360, ar5_dim_3ddata, 1))
            year_second = 2020

            ! Read data for 2020 by raising the index by 12
            CALL MDF_Get_Var( fid, varid, emis_help, status, start=(/1,1,1,imonth+12/) )

            ! Interpolate montly data between the two provided years
            x = float(year-year_first)/5.
            emis_in(:,:,:,1) = (1.-x) * emis_in(:,:,:,1) + x * emis_help(:,:,:,1)

            deallocate(emis_help)

         endif

       else if (year >= 2020) then

         ! First year of the decade:
         year_first = int(year/10) * 10

         ! Change to reference year to 2020 by raising the index by 12
         CALL MDF_Get_Var( fid, varid, emis_in, status, start=(/1,1,1,imonth+12+12*(year_first-2020)/10/) )
         IF_NOTOK_RETURN(status=1)

         if (year /= year_first) then

            ! Also read data for the first year of the next decade
            ! and apply a linear interpolation between the two years
            allocate(emis_help(nlon720, nlat360, ar5_dim_3ddata, 1))
            year_second = year_first + 10

            CALL MDF_Get_Var( fid, varid, emis_help, status, start=(/1,1,1,imonth+12+12*(year_second-2020)/10/) )

            ! Interpolate monthly data between the two provided years
            x = float(year-year_first)/10.
            emis_in(:,:,:,1) = (1.-x) * emis_in(:,:,:,1) + x * emis_help(:,:,:,1)

            deallocate(emis_help)

         endif

       else

         CALL MDF_Get_Var( fid, varid, emis_in, status, start=(/1,1,1,imonth+12*(year-startyear)/) )
         IF_NOTOK_RETURN(status=1)
        
       endif

       do lk = 1, ar5_dim_3ddata
          ! convert from kg(species)/m^2/s to kg(species)/m/s;
          ! Note that CMIP6 aircraft emissions are given in kg(species)/m^2/s,
          ! while AR5 aircraft emissions are given in kg(species)/m^3/s.
          ! In order to be able to use the same vertical regridding routine lateron, 
          ! we convert to the same unit and include a division by the layer depth.
          emis_in(:,:,lk,1) = emis_in(:,:,lk,1) * gridbox_area_05 / layer_depth
          
          ! now coarsen to nlon360,nlat180
          emission_cmip6_Read3DRecord(:,:,lk) = emission_coarsen_to_1x1( emis_in(:,:,lk,1) ,&
               & nlon720, nlat360, .false., status )
          IF_NOTOK_RETURN(status=1)

       end do

    end if
    
    CALL MDF_Close( fid, status )
    IF_NOTOK_RETURN(status=1)

    status = 0

    return

  end function emission_cmip6_Read3DRecord
  !EOC

  !--------------------------------------------------------------------------
  !                    TM5                                                  !
  !--------------------------------------------------------------------------
  !BOP
  !
  ! !FUNCTION:    EMISSION_CMIP6BMB_READ2DRECORD
  !
  ! !DESCRIPTION: Read a single 2d record of a given file and 
  !               return a 2d emission field interpolated on 1x1 grid.
  !\\
  !\\
  ! !INTERFACE:
  !
  function emission_cmip6bmb_Read2DRecord( fname, varname, imonth, year, startyear, status )
    !
    ! !RETURN VALUE:
    !
    real                  :: emission_cmip6bmb_Read2DRecord(nlon360,nlat180)
    !
    ! !INPUT PARAMETERS:
    !
    character(len=*),                intent(in) :: fname
    character(len=*),                intent(in) :: varname
    integer,                         intent(in) :: imonth, year, startyear
    !
    ! !OUTPUT PARAMETERS:
    !
    integer,                        intent(out) :: status
    !
    !EOP
    !------------------------------------------------------------------------
    !BOC

    character(len=*), parameter ::  rname = mname//'/emission_cmip6bmb_Read2DRecord'

    integer :: fid, varid, isec
    real    :: emis_in(nlon1440, nlat720, 1)

    ! --- begin -----------------------------------

    ! initialise
    emission_cmip6bmb_Read2DRecord = 0.0

    CALL MDF_Open( TRIM(fname), MDF_NETCDF, MDF_READ, fid, status )
    IF_NOTOK_RETURN(status=1)

    CALL MDF_Inq_VarID( fid, TRIM(varname), varid, status )
    IF_ERROR_RETURN(status=1)

    if ( varid < 0 ) then

       write (gol,'("EMISS - CMIP6 - no `",a,"` emissions in file ",a)') &
            varname, trim(fname) ; call goErr
       status=1; TRACEBACK; return

    else
       if( okdebug ) then
          write (gol,'("EMISS-INFO - CMIP6 - found `",a,"` emissions in file ",a)') &
               varname, trim(fname) ; call goErr
       endif

       CALL MDF_Get_Var( fid, varid, emis_in, status, start=(/1,1,imonth+12*(year-startyear)/) )
       IF_NOTOK_RETURN(status=1)

       ! over the oceans emissions are set to 1.e20 instead of 0.
       where ( emis_in > 1.e19 ) emis_in = 0.0

       ! convert from kg(species)/m^2/s  to  kg(species)/gridbox/s
       emis_in(:,:,1) = emis_in(:,:,1) * gridbox_area_025

       ! now coarsen to nlon360,nlat180
       emission_cmip6bmb_Read2DRecord = emission_coarsen_to_1x1( emis_in(:,:,1), nlon1440, nlat720,.false., status )
       IF_NOTOK_RETURN(status=1)

    end if

    CALL MDF_Close( fid, status )
    IF_NOTOK_RETURN(status=1)

    status = 0
    return

  end function emission_cmip6bmb_Read2DRecord


  !--------------------------------------------------------------------------
  !                    TM5                                                  !
  !--------------------------------------------------------------------------
  !BOP
  !
  ! !FUNCTION:    EMISSION_CMIP6BMB_SSP_READ2DRECORD
  !
  ! !DESCRIPTION: Read a single 2d record of a given file and 
  !               return a 2d emission field interpolated on 1x1 grid.
  !               Copied from emission_cmip6_Read2DRecord
  !               but with sector dimension removed.
  !\\
  !\\
  ! !INTERFACE:
  !
  function emission_cmip6bmb_ssp_Read2DRecord( fname, varname, imonth, year, startyear, status )
    !
    ! !RETURN VALUE:
    !
    real                  :: emission_cmip6bmb_ssp_Read2DRecord(nlon360,nlat180)
    !
    ! !INPUT PARAMETERS:
    !
    character(len=*),                intent(in) :: fname, varname
    integer,                         intent(in) :: imonth, year, startyear
    !
    ! !OUTPUT PARAMETERS:
    !
    integer,                        intent(out) :: status
    !
    !EOP
    !------------------------------------------------------------------------
    !BOC

    character(len=*), parameter ::  rname = mname//'/emission_cmip6bmb_ssp_Read2DRecord'
    character(len=256)          ::  fname2

    integer :: fid, varid
    integer :: fid2, varid2, year_first, year_second
    real    :: emis_in(nlon720, nlat360, 1)
    real, allocatable :: emis_help(:,:,:)
    real    :: x

    ! --- begin -----------------------------------

    ! initialise
    emission_cmip6bmb_ssp_Read2DRecord = 0.0

    CALL MDF_Open( TRIM(fname), MDF_NETCDF, MDF_READ, fid, status )
    IF_NOTOK_RETURN(status=1)

    CALL MDF_Inq_VarID( fid, TRIM(varname), varid, status )
    IF_ERROR_RETURN(status=1)

    if ( varid < 0 ) then

       write (gol,'("EMISS - CMIP6 - no `",a,"` emissions in file ",a)') &
            varname, trim(fname) ; call goErr
       status=1; TRACEBACK; return

    else
       if( okdebug ) then
          write (gol,'("EMISS-INFO - CMIP6 - found `",a,"` emissions in file ",a)') &
               varname, trim(fname) ; call goErr 
       endif

       ! SSP scenario emissions are provided for 2015, 2020, 2030, ... 2100
       if (year >= 2015 .and. year < 2020) then

         ! First year of the period 
         year_first = 2015

         CALL MDF_Get_Var( fid, varid, emis_in, status, start=(/1,1,imonth/) )
         IF_NOTOK_RETURN(status=1)

         if (year /= year_first) then

            ! Also read data for 2020
            ! and apply a linear interpolation between the two years
            allocate(emis_help(nlon720, nlat360, 1))
            year_second = 2020

            ! Read data for 2020 by raising the index by 12
            CALL MDF_Get_Var( fid, varid, emis_help, status, start=(/1,1,imonth+12/) )

            ! Interpolate montly data between the two provided years
            x = float(year-year_first)/5.
            emis_in(:,:,1) = (1.-x) * emis_in(:,:,1) + x * emis_help(:,:,1)

            deallocate(emis_help)

         endif

       else if (year >= 2020) then

         ! First year of the decade:
         year_first = int(year/10) * 10

         ! Change to reference year to 2020 by raising the index by 12
         CALL MDF_Get_Var( fid, varid, emis_in, status, start=(/1,1,imonth+12+12*(year_first-2020)/10/) )
         IF_NOTOK_RETURN(status=1)

         if (year /= year_first) then

            ! Also read data for the first year of the next decade
            ! and apply a linear interpolation between the two years
            allocate(emis_help(nlon720, nlat360, 1))
            year_second = year_first + 10

            CALL MDF_Get_Var( fid, varid, emis_help, status, start=(/1,1,imonth+12+12*(year_second-2020)/10/) )

            ! Interpolate monthly data between the two provided years
            x = float(year-year_first)/10.
            emis_in(:,:,1) = (1.-x) * emis_in(:,:,1) + x * emis_help(:,:,1)

            deallocate(emis_help)

         endif

       else 
         ! As this function is only used for the future period (2015-2100)
         ! the code will never get here.
         ! read yearly emissions directly from file
         CALL MDF_Get_Var( fid, varid, emis_in, status, start=(/1,1,imonth+12*(year-startyear)/) )
         IF_NOTOK_RETURN(status=1)

       endif

       ! convert from kg(species)/m^2/s  to  kg(species)/gridbox/s
       emis_in(:,:,1) = emis_in(:,:,1) * gridbox_area_05
       
       ! now coarsen to nlon360,nlat180
       emission_cmip6bmb_ssp_Read2DRecord = emission_coarsen_to_1x1( emis_in(:,:,1), nlon720, nlat360,.false., status )
       IF_NOTOK_RETURN(status=1)

    end if

    CALL MDF_Close( fid, status )
    IF_NOTOK_RETURN(status=1)
 
    status = 0
    return

  end function emission_cmip6bmb_ssp_Read2DRecord
  !EOC


  !--------------------------------------------------------------------------
  !                    TM5                                                  !
  !--------------------------------------------------------------------------
  !BOP
  !
  ! !IROUTINE:  READ_CMIP6_ZCH4
  !
  ! !DESCRIPTION: Read CMIP6 zonal mean CH4 for specified year/month
  !               and convert from 0.5 to 1 degree latitude bands
  !\\
  !\\
  ! !INTERFACE:
  !
  SUBROUTINE READ_CMIP6_ZCH4(field, year, month, status)
    !
    ! !USES:
    !
    use emission_data, only : cmip6_ch4_dirname
    !
    ! !OUTPUT PARAMETERS:
    !
    integer,                  intent(out) :: status
    ! output zonal mean field in 1 degree latitude bands:
    real, dimension(nlat180), intent(out) ::  field
    !
    ! !INPUT PARAMETERS:
    !
    integer, intent(in) :: year, month
    !
    ! !REMARKS:
    !
    !EOP
    !------------------------------------------------------------------------
    !BOC

    character(len=*), parameter :: rname = mname//'/Read_CMIP6_CH4'
    character(len=256)          :: fname_gridboxarea 
    character(len=512)          :: zch4_fname
    integer                     :: startyear
    integer                     :: fid, varid
    ! zonal mean field in CMIP6 file:
    integer                     :: j_coarse, j_fine, j_int
    real                        :: area_norm
    real*4, dimension(nlat360)    :: field05

    ! read in gridbox-area; once per CPU
    if( .not. area_found_05 ) then
       fname_gridboxarea = trim(emis_input_dir_ar5)//'/'//trim(ar5_filestr_gridboxarea)
       call emission_ReadGridboxArea(fname_gridboxarea, 'gridbox_area', gridbox_area_05, &
             & nlon720, nlat360, status )
       IF_NOTOK_RETURN(status=1)
       area_found_05=.true.
    endif

    if (year .le. 2014) then
      zch4_fname='mole-fraction-of-methane-in-air_input4MIPs_GHGConcentrations_CMIP_UoM-CMIP-1-2-0_gr-0p5x360deg_000001-201412.nc'
      startyear=0
    else
      select case (trim(ssp_name))
      case ("SSP3-7.0")
        zch4_fname='mole-fraction-of-methane-in-air_input4MIPs_GHGConcentrations_ScenarioMIP_UoM-AIM-ssp370-1-2-1_gr-0p5x360deg_201501-250012.nc'

        if (LSSP370_LowCH4) then
          zch4_fname='mole-fraction-of-methane-in-air_input4MIPs_GHGConcentrations_AerChemMIP_UoM-AIM-ssp370-lowNTCF-1-2-1_gr-0p5x360deg_201501-250012.nc'
        endif
      case ("SSP3-LowNTCF")
        zch4_fname='mole-fraction-of-methane-in-air_input4MIPs_GHGConcentrations_AerChemMIP_UoM-AIM-ssp370-lowNTCF-1-2-1_gr-0p5x360deg_201501-250012.nc' 
        
      case default
        write (gol,'("CH4 surface zonal means not implemented for scenario: ",a)') trim(ssp_name) ; call goErr
        status=1; TRACEBACK; return
      end select
      startyear=2015
    endif
    zch4_fname=trim(cmip6_ch4_dirname)//trim(zch4_fname)

    CALL MDF_Open( TRIM(zch4_fname), MDF_NETCDF, MDF_READ, fid, status )
    IF_NOTOK_RETURN(status=1)

    CALL MDF_Inq_VarID( fid, 'mole_fraction_of_methane_in_air', varid, status )
    IF_ERROR_RETURN(status=1)

    CALL MDF_Get_Var( fid, varid, field05, status, start = (/1,month+12*(year-startyear)/) )
    IF_NOTOK_RETURN(status=1)

    CALL MDF_Close( fid, status )
    IF_NOTOK_RETURN(status=1)

    ! convert from 0.5 to 1 degree latitude bands:
    field(:)=0.
    do j_coarse = 1,nlat180
      area_norm=0.
      do j_int=1,2
        ! Set longitude index to 1 (arbitrary)
        j_fine=2*(j_coarse-1)+j_int
        field(j_coarse)=field(j_coarse)+field05(j_fine)*gridbox_area_05(1,j_fine)
        area_norm=area_norm+gridbox_area_05(1,j_fine)
      enddo
      field(j_coarse)=field(j_coarse)/area_norm
    enddo

    status = 0

  END SUBROUTINE READ_CMIP6_ZCH4
  !EOC
    
  !--------------------------------------------------------------------------
  !                    TM5                                                  !
  !--------------------------------------------------------------------------
  !BOP
  !
  ! !IROUTINE:    EMISSION_AR5_READSECTOR
  !
  ! !DESCRIPTION: Reading one sector of the files to be interpolated and 
  !               returning an interpolated 3d emission field (d3data)
  !\\
  !\\
  ! !INTERFACE:
  !
  subroutine emission_ar5_ReadSector( comp, iyear, imonth, sector, d3data, status )
    !
    ! !INPUT PARAMETERS:
    !
    character(len=*) , intent(in)    :: comp
    integer          , intent(in)    :: iyear 
    integer          , intent(in)    :: imonth 
    integer          , intent(in)    :: sector
    !
    ! !OUTPUT PARAMETERS:
    !
    integer          , intent(out)   :: status
    real, dimension(nlon360,nlat180,ar5_dim_3ddata), intent(out)   :: d3data
    !
    ! !REVISION HISTORY: 
    !    1 Oct 2010 - Achim Strunk - v0
    !
    !EOP
    !------------------------------------------------------------------------
    !BOC

    character(len=*), parameter :: rname = mname//'/emission_ar5_read2dsector'
    character(len=256)          :: fname
    character(len=256)          :: fname_gridboxarea
    character(32)               :: secname
    integer                     :: lt, year
    logical                     :: existfile
    integer,          dimension(2) :: ltimes         
    character(len=4), dimension(2) :: ar5_cyears       
    real,             dimension(2) :: ar5_ipcoef_years 
    logical                     :: first=.true.
    real, dimension(nlon360,nlat180) :: d2data

    ! --- begin -----------------------------------

    ! initialise target array
    d3data = 0.0 

    ! read in gridbox-area; once per CPU
    if( .not. area_found_05 ) then
       fname_gridboxarea = trim(emis_input_dir_ar5)//'/'//trim(ar5_filestr_gridboxarea)
       call emission_ReadGridboxArea(fname_gridboxarea, 'gridbox_area', gridbox_area_05, &
             & nlon720, nlat360, status )
       IF_NOTOK_RETURN(status=1)
       area_found_05=.true.
    endif

    ! ----------------------------------------
    ! get the right times to interpolate and related coefficients
    ! (ar5_avail_yrs(ltimes))
    ! 
    ! --> result will be a linear interpolation between neighbouring files 
    ! 
    ! ----------------------------------------
    allocate( ltimeind( ar5_nr_avail_yrs ) )
    ltimeind = .false.

    ! deal with out-of-bounds requested years
    year = valid_year( iyear, ar5_avail, 'AR5', first)
    first=.false.
    
    where( ar5_avail_yrs < year ) ltimeind = .true.

    ! times(1): index representing time instance earlier than current year
    ! times(2):     -"-          -"-               later than current year
    ltimes(2) = count( ltimeind ) + 1
    ltimes(1) = max( ltimes(2) - 1, 1 )

    ! check a match with repository 
    ! (in order to copy only one file instead of two)
    if( ar5_avail_yrs(ltimes(2)) == year ) &
         ltimes(1) = ltimes(2)
    deallocate( ltimeind )

    ! ar5_cyears will contain strings with the years
    write(ar5_cyears(1),'(I4.4)') ar5_avail_yrs(ltimes(1))
    write(ar5_cyears(2),'(I4.4)') ar5_avail_yrs(ltimes(2))

    ! ar5_ipcoef_years will contain interpolation coefficients
    ! default: factors 1.0/0.0
    ar5_ipcoef_years(1) = 1.0
    ar5_ipcoef_years(2) = 0.0
    if( ltimes(2) /= ltimes(1) ) then 
       ar5_ipcoef_years(1) = (ar5_avail_yrs(ltimes(2)) - year) / &
                        real( ar5_avail_yrs(ltimes(2)) - ar5_avail_yrs(ltimes(1)) )
       ar5_ipcoef_years(2) = 1.0 - ar5_ipcoef_years(1)
    end if

 
    ! ------------------------
    ! read files (index=1: earlier file; index=2: later file)
    ! ------------------------
    do lt = 1, 2

       if (ar5_ipcoef_years(lt) == 0.) cycle
       
       ! ------------------------
       ! construct filename
       ! e.g.: <emisdir>/IPCC_emissions_RCP45_CO_biomassburning_2010_0.5x0.5.nc
       ! ------------------------
       if (trim(filestr_rcpiden)=='hist') then 
          fname = trim(emis_input_dir_ar5)             //'/'//   & 
                  trim(filestr_common_pre)             //'_'//   &
                  trim(comp)                           //'_'//   &
                  trim(sectors_def(sector)%catname)    //'_'//   &
                  ar5_cyears(lt)                       //'_'//   &
                  trim(filestr_common_post)
       else
          fname = trim(emis_input_dir_ar5)             //'/'//   & 
                  trim(filestr_common_pre)             //'_'//   &
                  trim(filestr_rcpiden)                //'_'//   &   
                  trim(comp)                           //'_'//   &
                  trim(sectors_def(sector)%catname)    //'_'//   &
                  ar5_cyears(lt)                       //'_'//   &
                  trim(filestr_common_post)
       endif


       ! test existence of file
       inquire( file=trim(fname), exist=existfile)
       if( .not. existfile ) then 
          write (gol,'(" AR5 file `",a,"` not found ")') trim(fname); call goErr
          status = 1; TRACEBACK; return 
       end if


       ! ------------------------------------------------
       ! data record is read by emission_ar5_Read2/3DRecord
       ! add data scaled by interpolation factor ar5_ipcoef_years
       secname = sectors_def(sector)%name

       ! temporary fix as CMIP6 CH4 emissions are not available yet
       ! change sector names to be able to use AR5 reading routine
       if ( trim(sectors_def(sector)%prov) == 'CMIP6' ) then 
         select case( trim(secname) )
           case ('AGR')
             secname='emiss_agr'
           case ('ENE')
             secname='emiss_ene'
           case ('IND')
             secname='emiss_ind'
           case ('TRA')
             secname='emiss_tra'
           case ('RCO')
             secname='emiss_dom'
           case ('WST')
             secname='emiss_wst'
           case ('SHP')
             secname='emiss_shp'
           case default
             write (gol,'("EMISS - CMIP6 - no `",a,"` emissions in file ",a)') &
                 secname, trim(fname) ; call goErr
             status=1; TRACEBACK; return
         end select
       endif

       ! distinguish 2d/3d sectors
       if( sectors_def(sector)%f3d ) then 
          d3data(:,:,:) = d3data(:,:,:) + ar5_ipcoef_years(lt) * &
                                          emission_ar5_Read3DRecord( fname, secname, imonth, status )
       else
          !>>> TvN
          ! Set sectoral emissions not provided in the historical AR5 files to zero
          ! for years before 2000.
          ! Note these emissions are non-zero in the RCPs.
          ! It is assumed below that the data for 2000 (and 2005) are taken from the RCP files.
          if ( ar5_avail_yrs(ltimes(lt)) .lt. 2000 .and. &
               ( ( trim(secname) .eq. 'emiss_slv' .and. (trim(comp) .eq. 'CO') ) .or. &
                 ( trim(secname) .eq. 'emiss_agr' .and. &
                    ( trim(comp) .eq. 'acids' .or. &
                      trim(comp) .eq. 'alcohols' .or. &
                      trim(comp) .eq. 'benzene' .or. &
                      trim(comp) .eq. 'butanes' .or. &
                      trim(comp) .eq. 'ethane' .or. &
                      trim(comp) .eq. 'ethene' .or. &
                      trim(comp) .eq. 'ethers' .or. &                
                      trim(comp) .eq. 'ethyne' .or. &
                      trim(comp) .eq. 'formaldehyde' .or. &
                      trim(comp) .eq. 'hexanes_and_higher_alkanes' .or. &
                      trim(comp) .eq. 'ketones' .or. &
                      trim(comp) .eq. 'other_alkanals' .or. &
                      trim(comp) .eq. 'other_alkenes_and_alkynes' .or. &
                      trim(comp) .eq. 'other_aromatics' .or. &
                      trim(comp) .eq. 'pentanes' .or. &
                      trim(comp) .eq. 'propane' .or. &
                      trim(comp) .eq. 'propene' .or. &
                      trim(comp) .eq. 'toluene' .or. &
                      trim(comp) .eq. 'xylene' ) ) ) ) then
            d2data(:,:) = 0.
          else
            d2data(:,:) = emission_ar5_Read2DRecord( fname, secname, imonth, status )
          endif
          !d3data(:,:,1) = d3data(:,:,1) + ar5_ipcoef_years(lt) * &
          !                                emission_ar5_Read2DRecord( fname, secname, imonth, status ) 
          d3data(:,:,1) = d3data(:,:,1) + ar5_ipcoef_years(lt) * d2data(:,:)
          !<<< TvN          
       end if
       IF_NOTOK_RETURN(status=1)

    end do ! lt 

  end subroutine emission_ar5_ReadSector
  !EOC

  !--------------------------------------------------------------------------
  !                    TM5                                                  !
  !--------------------------------------------------------------------------
  !BOP
  !
  ! !FUNCTION:    EMISSION_AR5_READ2DRECORD
  !
  ! !DESCRIPTION: Read a single 2d record of a given file and 
  !               return a 2d emission field interpolated on 1x1 grid.
  !\\
  !\\
  ! !INTERFACE:
  !
  function emission_ar5_Read2DRecord( fname, secname, imonth, status )
    !
    ! !RETURN VALUE:
    !
    real                  :: emission_ar5_Read2DRecord(nlon360,nlat180)
    !
    ! !INPUT PARAMETERS:
    !
    character(len=*),                intent(in) :: fname
    character(len=sector_name_len),  intent(in) :: secname
    integer,                         intent(in) :: imonth
    !
    ! !OUTPUT PARAMETERS:
    !
    integer,                        intent(out) :: status
    !
    ! !REVISION HISTORY: 
    !    1 Oct 2010 - Achim Strunk - v0
    !
    !EOP
    !------------------------------------------------------------------------
    !BOC

    character(len=*), parameter ::  rname = mname//'/emission_ar5_Read2DRecord'

    integer :: fid, varid
    real    :: emis_in(nlon720, nlat360, 1)

    ! --- begin -----------------------------------

    ! initialise
    emission_ar5_Read2DRecord = 0.0

    CALL MDF_Open( TRIM(fname), MDF_NETCDF, MDF_READ, fid, status )
    IF_NOTOK_RETURN(status=1)

    CALL MDF_Inq_VarID( fid, TRIM(secname), varid, status )
    IF_ERROR_RETURN(status=1)

    if ( varid < 0 ) then

       write (gol,'("EMISS - AR5 - no `",a,"` emissions in file ",a)') &
            secname, trim(fname) ; call goErr
       status=1; TRACEBACK; return

    else
       if( okdebug ) then
          write (gol,'("EMISS-INFO - AR5 - found `",a,"` emissions in file ",a)') &
               secname, trim(fname) ; call goPr
       endif
    
       CALL MDF_Get_Var( fid, varid, emis_in, status, start=(/1,1,imonth/) )
       IF_NOTOK_RETURN(status=1)

       ! convert from kg(species)/m^2/s  to  kg(species)/gridbox/s
       emis_in(:,:,1) = emis_in(:,:,1) * gridbox_area_05
       
       ! now coarsen to nlon360,nlat180
       emission_ar5_Read2DRecord = emission_coarsen_to_1x1( emis_in(:,:,1), nlon720, nlat360,.true., status )
       IF_NOTOK_RETURN(status=1)

    end if

    CALL MDF_Close( fid, status )
    IF_NOTOK_RETURN(status=1)
 
    status = 0
    return

  end function emission_ar5_Read2DRecord
  !EOC

  !--------------------------------------------------------------------------
  !                    TM5                                                  !
  !--------------------------------------------------------------------------
  !BOP
  !
  ! !FUNCTION:    EMISSION_AR5_READ3DRECORD
  !
  ! !DESCRIPTION: read one 3D sector
  ! 
  !\\
  !\\
  ! !INTERFACE:
  !
  function emission_ar5_Read3DRecord( fname, secname, imonth, status )
    !
    ! !RETURN VALUE:
    !
    real :: emission_ar5_Read3DRecord(nlon360,nlat180,ar5_dim_3ddata)
    !
    ! !INPUT/OUTPUT PARAMETERS:
    !
    character(len=*),                   intent(in)    :: fname
    character(32),                   intent(inout)    :: secname
    !
    ! !INPUT PARAMETERS:
    !
    integer,                            intent(in)    :: imonth
    !
    ! !OUTPUT PARAMETERS:
    !
    integer,                            intent(out)   :: status
    !
    ! !REVISION HISTORY: 
    !    1 Oct 2010 - Achim Strunk -
    !
    ! !REMARKS:
    !
    !EOP
    !------------------------------------------------------------------------
    !BOC

    character(len=*), parameter ::  rname = mname//'/emission_ar5_Read3DRecord'

    integer                 :: fid, varid, lk
    real, dimension(nlon720,nlat360,ar5_dim_3ddata,1) :: emis_in

    ! --- begin -----------------------------------

    ! initialise
    emission_ar5_Read3DRecord = 0.0

    CALL MDF_Open( TRIM(fname), MDF_NETCDF, MDF_READ, fid, status )
    IF_NOTOK_RETURN(status=1)

    CALL MDF_Inq_VarID( fid, TRIM(secname), varid, status )
    IF_ERROR_RETURN(status=1)

    if ( varid < 0 ) then
       write (gol,'("EMISS - AR5 - no `",a,"` emissions in file ",a)') &
            secname, trim(fname) ; call goErr
       status=1; TRACEBACK; return
    else
       if( okdebug ) then
          write (gol,'("EMISS-INFO - AR5 - found `",a,"` emissions in file ",a)') &
               secname, trim(fname) ; call goPr
       endif

       CALL MDF_Get_Var( fid, varid, emis_in, status, start=(/1,1,1,imonth/) )
       IF_NOTOK_RETURN(status=1)

       do lk = 1, ar5_dim_3ddata
          ! convert from kg(species)/m^3/s to kg(species)/m/s  :
          emis_in(:,:,lk,1) = emis_in(:,:,lk,1) * gridbox_area_05
          
          ! now coarsen to nlon360,nlat180
          emission_ar5_Read3DRecord(:,:,lk) = emission_coarsen_to_1x1( emis_in(:,:,lk,1) ,&
               & nlon720, nlat360, .true., status )
          IF_NOTOK_RETURN(status=1)

       end do

    end if
    
    CALL MDF_Close( fid, status )
    IF_NOTOK_RETURN(status=1)

    status = 0

    return

  end function emission_ar5_Read3DRecord
  !EOC


  !--------------------------------------------------------------------------
  !                    TM5                                                  !
  !--------------------------------------------------------------------------
  !BOP
  !
  ! !IROUTINE:   EMISSION_READGRIDBOXAREA
  !
  ! !DESCRIPTION: 
  !       reading gridbox surface areas for 0.5 x 0.5 Edgar 4
  !       needed to scale the emissions from mass/m^2 to mass/grid
  !\\
  !\\
  ! !INTERFACE:
  !
  subroutine emission_ReadGridboxArea(fname, recname, gridbox_area, dim_nlon, dim_nlat, status )
    !
    ! !INPUT PARAMETERS:
    !
    character(len=*), intent(in)    ::  fname
    character(len=*), intent(in)    ::  recname
    integer,          intent(in)    ::  dim_nlon
    integer,          intent(in)    ::  dim_nlat
    !
    ! !OUTPUT PARAMETERS:
    !
    integer,                             intent(out)  ::  status
    real, dimension(dim_nlon, dim_nlat), intent(out)  ::  gridbox_area
    !
    ! !REVISION HISTORY: 
    !
    !    1 Oct 2010 - Achim Strunk - v0
    !    1 Dec 2011 - Narcisa Banda - generalized it for any gridbox area size
    !
    ! !REMARKS:
    !
    !EOP
    !------------------------------------------------------------------------
    !BOC

    character(len=*), parameter ::  rname = mname//'/emission_ReadGridboxArea'

    integer :: fid, varid

    ! --- begin -----------------------------------

    CALL MDF_Open( TRIM(fname), MDF_NETCDF, MDF_READ, fid, status )
    IF_NOTOK_RETURN(status=1)
    
    CALL MDF_Inq_VarID( fid, TRIM(recname), varid, status )
    IF_ERROR_RETURN(status=1)

    CALL MDF_Get_Var( fid, varid, gridbox_area, status )
    IF_NOTOK_RETURN(status=1)

    CALL MDF_Close( fid, status )
    IF_NOTOK_RETURN(status=1)
    
    status = 0

  end subroutine emission_ReadGridboxArea
  !EOC


  !--------------------------------------------------------------------------
  !                    TM5                                                  !
  !--------------------------------------------------------------------------
  !BOP
  !
  ! !IROUTINE:    EMISSION_AR5_REGRID_AIRCRAFT
  !
  ! !DESCRIPTION: Vertical regridding of the AR5 aircraft data. 
  !               The vertical levels of the input data are hard coded. 
  !               (Taken from GFED_daily_AR5 (VH) and left as is)
  !\\
  !\\
  ! !INTERFACE:
  !
  subroutine emission_ar5_regrid_aircraft(region, i0, j0, field_in, field_out, status )
    !
    ! !USES:
    !
    use meteodata,       only : gph_dat
    use tm5_distgrid,    only : dgrid, get_distgrid
    use dims,            only : lm
    !
    ! !OUTPUT PARAMETERS:
    !
    integer, intent(out)                              :: status
    !
    ! !INPUT PARAMETERS:
    !
    integer,                        intent(in) :: region, i0, j0
    real, dimension(i0:, j0:, 1:),  intent(in) :: field_in
    real, dimension(i0:, j0:, 1:), intent(out) :: field_out
    !
    ! !REVISION HISTORY: 
    !    1 Oct 2010 - Achim Strunk - Taken from GFED_daily_AR5 (VH) and left as is
    !    3 Dec 2012 - Ph. Le Sager - modified for lat-lon mpi decomposition
    !                              - work with zoom regions
    !                              - mass conservation per column     
    !
    ! !REMARKS:
    !
    !EOP
    !------------------------------------------------------------------------
    !BOC

    character(len=*), parameter ::  rname = mname//'/emission_ar5_regrid_aircraft'

    integer, parameter :: lm_in=25
    
    real, dimension(:,:,:), pointer        :: gph       ! geopotential height (m)
    integer                                :: i,j,l
    real, dimension(lm_in)                 :: height_in_min, height_in_max
    real, allocatable                      :: dz(:), height(:)
    real                                   :: height_min,height_max
    real                                   :: height_out_min,height_out_max
    real, dimension(lm_in), parameter      :: height_in=(/ &  ! Height in meter
         305.,  915., 1525., 2135., 2745., 3355., 3965., 4575., 5185., 5795., &
         6405., 7015., 7625., 8235., 8845., 9455.,10065.,10675.,11285., &
         11895.,12505.,13115.,13725.,14335.,14945./)
    real    :: dz_in(25)
    integer :: l_in, i1, i2, j1, j2, lmr
    real    :: total_in, total_out, total_ratio
    
    ! --- begin --------------------------------------

    call golabel()

    gph => gph_dat(region)%data
    
    CALL GET_DISTGRID( dgrid(region), I_STRT=i1, I_STOP=i2, J_STRT=j1, J_STOP=j2 )          
    lmr = lm(region)

    allocate(dz(lmr), height(lmr+1))
    
    ! sanity check    
    if (okdebug) then
       if (i1/=i0 .or. j1/=j0) then
          status = 1 ; TRACEBACK
          return
       end if
       if (lm_in /= ubound(field_in,3) ) then
          write(gol,*)'wrong vertical input resolution'; call goErr
          status = 1 
          TRACEBACK; return
       endif
       if ((ubound(field_out,3)+1) /= ubound(gph,3)) then
          write(gol,*)'wrong vertical output resolution'; call goErr
          status = 1 
          TRACEBACK; return
       endif
    end if
    
    ! locally flat atmosphere assumed
    ! area linear in i,j 


    ! height above sea level
    height_in_min(1)=0.
    do l_in = 2,lm_in
       height_in_min(l_in)=(height_in(l_in-1)+height_in(l_in))/2.
    enddo

    height_in_max(lm_in)=15555.
    do l_in = 1,lm_in-1
       height_in_max(l_in)=(height_in(l_in)+height_in(l_in+1))/2.
    enddo

    ! init
    field_out = 0.0

    ! regrid
    do i=i1, i2
    do j=j1, j2
          
          total_in = 0.0
       
          ! calculate total input emissions
          do l_in=1, lm_in
             dz_in(l_in) = height_in_max(l_in)-height_in_min(l_in)
             total_in =total_in + field_in(i,j,l_in)*dz_in(l_in)
          enddo

          ! zero based height:
          height(1) = 0.0 
          do l=1, lmr
             dz(l) = gph(i,j,l+1) - gph(i,j,l)
             height(l+1) = height(l) + dz(l)
          enddo
          
          do l=1,lmr-1
             height_out_min=height(l)
             height_out_max=height(l+1)
             !   write(*,*)'DO AR5- regrid - C2',i,j,l,height_out_min,height_out_max
             do l_in=1,lm_in
                if (height_out_max .le. height_in_min(l_in)) exit
                if (height_out_min .lt. height_in_max(l_in)) then
                   height_max=min(height_out_max,height_in_max(l_in))
                   height_min=max(height_out_min,height_in_min(l_in))
                   ! helpfield as field_in is ordered from high to low
                   !                 field_out(i,j,l) = field_out(i,j,l) + helpfield2(i,j,lm_in-l_in+1)* &
                   !                              (height_max-height_min)/(height_in_max(l_in)-height_in_min(l_in)) 
                   ! helpfield as field_in is ordered from low to high
                   !   write(*,*)'DO AR5- regrid - C',i,j,l,l_in,height_max-height_min
                   field_out(i,j,l) = field_out(i,j,l) + field_in(i,j,l_in)* &
                        (height_max-height_min)  ! kg/m -> kg / gridbox
                endif
             enddo
          enddo

          ! conserve total exactly: not possible because units are in kg/m...
          total_out   = sum(field_out(i,j,:))
          if (total_out /= 0) then
             total_ratio = total_in/total_out
             field_out(i,j,:) = field_out(i,j,:)*total_ratio
          end if

    enddo
    enddo

    deallocate(dz, height)

    call golabel()

    ! ok
    status = 0

  end subroutine emission_ar5_regrid_aircraft
  !EOC



  !--------------------------------------------------------------------------
  !                    TM5                                                  !
  !--------------------------------------------------------------------------
  !BOP
  !
  ! !IROUTINE:    EMISSION_MACC_READSECTOR
  !
  ! !DESCRIPTION: Read an MACC sector field out of an open file. 
  !\\
  !\\
  ! !INTERFACE:
  !
  subroutine emission_macc_ReadSector( fpath, comp, iyear, imonth, fext, recname, unit, emis, status )
    !
    ! !USES:
    !
    use chem_param, only : xmn, xmno2
    !
    ! !INPUT PARAMETERS:
    !
    character(len=*), intent(in)       ::  fpath   ! dir 
    character(len=*), intent(in)       ::  comp    ! species name (as in filename)
    integer,          intent(in)       ::  iyear
    integer,          intent(in)       ::  imonth
    character(len=*), intent(in)       ::  fext    ! tail of filename
    character(len=*), intent(in)       ::  recname ! sector name
    character(len=*), intent(in)       ::  unit
    !
    ! !OUTPUT PARAMETERS:
    !
    real,    intent(out)               ::  emis(nlon360,nlat180,ar5_dim_3ddata)
    integer, intent(out)               ::  status
    !
    ! !REVISION HISTORY: 
    !    1 Oct 2010 - Achim Strunk -
    !   28 Nov 2012 - Ph. Le Sager - switch to MDF interface
    !    6 Jan 2014 - Ph. Le Sager - code for MACCity: 3D emission field to deal with IPCC aircraft file 
    !
    ! !REMARKS:
    !
    !EOP
    !------------------------------------------------------------------------
    !BOC

    character(len=*), parameter ::  rname = mname//'/emission_macc_ReadSector'

    character(len=256)      :: fname
    character(len=256)      :: fname_gridboxarea
    character(len=32)       :: fcomp

    integer                 :: fid, varid, year, lk
    real                    :: emis_in( nlon720, nlat360, ar5_dim_3ddata, 1)
    logical                 :: first=.true.
    
    ! --- begin -----------------------------------

    ! read in gridbox-area; same name as ar5 gridbox area file
    if( .not. area_found_05 ) then
        fname_gridboxarea = trim(emis_input_dir_mac)//'/'//trim(ar5_filestr_gridboxarea)
        call emission_ReadGridboxArea(fname_gridboxarea, 'gridbox_area', gridbox_area_05, &
             & nlon720, nlat360, status )
       IF_NOTOK_RETURN(status=1)
       area_found_05 = .true.
    endif

    ! file name component:
    fcomp = comp
    year  = valid_year( iyear, macc_avail, 'MACC', first)
    first = .false.

    ! zero field
    emis = 0.0


    !=== FILENAME
    if (trim(recname) == "emiss_air") then ! If MACC-CITY, specific handling of aircraft emissions
       
       ! available only for NO or BC
       if ( (trim(fcomp) /= "NO").and.(trim(fcomp) /= "BC") ) return
       write (fname,'(a,"/IPCC_emissions_",a,"_aircraft_",i4.4,"_0.5x0.5.nc")') trim(fpath), trim(fcomp), year
       
    else           
       write (fname,'(a,"/JUELICH_MACC_reanalysis_",a,"_",i4.4,"_",a)') trim(fpath), trim(fcomp), year, trim(fext)
    end if
    
    
    !=== SCREEN OUT cases without data (There are more cases, but they are already handled in calling routines.)
    
    ! ***************************************************************************
    ! ****** THIS IS MESSY. IT NEEDS CONSOLIDATION: SHOULD BUILD LIST OF ********
    ! ****** SECTORS PER SPECIES, LIKE ED42                              ********
    ! ***************************************************************************
    
    if (trim(recname) == "emiss_oc") then

       ! no "emiss_oc" for : C2H5OH, BIGALK, ISOPRENE, TERPENES, BIGENE, TOLUENE, CH2O, CH3CHO, CH3COCH3, MEK
       !          only for : C2H6, C3H8, C2H4, C3H6, NH3, and CO
       if ( (trim(fcomp) /= "C2H6").and.(trim(fcomp) /= "C3H8").and.(trim(fcomp) /= "C2H4").and.&
            (trim(fcomp) /= "C3H6").and.(trim(fcomp) /= "NH3").and.(trim(fcomp) /= "CO") ) return
      
    else if (trim(recname) == "emiss_anthro") then

       ! there is no "emiss_anthro" for : ISOPRENE, TERPENES
       if ( (trim(fcomp) == "ISOPRENE").or.(trim(fcomp) == "TERPENES") ) return
       
    end if
    
    if (trim(recname) == "emiss_bio") then
       ! We arrive here if we are not using MEGAN. No "emiss_bio" for C2H5OH, BIGALK, BIGENE:
       if ( (trim(fcomp) == "C2H5OH").or.(trim(fcomp) == "BIGALK").or.(trim(fcomp) == "BIGENE") ) return
    endif
    
    if (trim(fcomp) == "BC") then
       if ((trim(recname) /= "emiss_anthro").and.(trim(recname) /= "emiss_air") ) return
    endif
    
    if ((trim(fcomp) == "OC").and.(trim(recname) /= "emiss_anthro")) return

    
    !=== READ
    CALL MDF_Open( TRIM(fname), MDF_NETCDF, MDF_READ, fid, status )
    IF_NOTOK_RETURN(status=1)

    CALL MDF_Inq_VarID( fid, trim(recname), varid, status )
    IF_NOTOK_RETURN(status=1)

    if( okdebug ) then
       write (gol,'("EMISS-INFO - MACC - found `",a,"` emissions category in file `",a,"`")') trim(recname), trim(fname); call goPr
    endif

    ! 3D
    if (trim(recname) == "emiss_air") then

       CALL MDF_Get_Var( fid, varid, emis_in, status, start=(/1,1,1,imonth/) )
       IF_NOTOK_RETURN(status=1)

       do lk = 1, ar5_dim_3ddata
          ! convert from kg(species)/m^2/s to kg(species)/gridbox/s  :
          emis_in(:,:,lk,1) = emis_in(:,:,lk,1) * gridbox_area_05

          ! now coarsen to nlon360,nlat180
          emis(:,:,lk) = emission_coarsen_to_1x1( emis_in(:,:,lk,1) , nlon720, nlat360, .true., status )
          IF_NOTOK_RETURN(status=1)

       end do

    else !2D

       CALL MDF_Get_Var( fid, varid, emis_in(:,:,1,1), status, start=(/1,1,imonth/) )
       IF_NOTOK_RETURN(status=1)

       ! convert from  kg(species)/m^2/s  to  kg(species)/s 
       emis_in(:,:,1,1) = emis_in(:,:,1,1) * gridbox_area_05

       ! combine grid cells
       emis(:,:,1) = emission_coarsen_to_1x1( emis_in(:,:,1,1), nlon720, nlat360, .true., status )
       IF_NOTOK_RETURN(status=1)

    endif

    CALL MDF_Close( fid, status )
    IF_NOTOK_RETURN(status=1)

    status = 0

  end subroutine Emission_macc_ReadSector
  !EOC

  
  !--------------------------------------------------------------------------
  !                    TM5                                                  !
  !--------------------------------------------------------------------------
  !BOP
  !
  ! !IROUTINE:    EMISSION_ED4_READSECTOR
  !
  ! !DESCRIPTION: Read an EDAGR-4 sector field out of an open file. 
  !               
  !\\
  !\\
  ! !INTERFACE:
  !
  function emission_ed4_Read2DRecord( fname, secname, status )
    !
    ! !RETURN VALUE:
    !
    real :: emission_ed4_Read2DRecord(nlon360,nlat180)
    !
    ! !INPUT PARAMETERS:
    !
    character(len=*),     intent(in) :: fname
    character(32),        intent(in) :: secname
    !
    ! !OUTPUT PARAMETERS:
    !
    integer,             intent(out) :: status
    !
    ! !REVISION HISTORY: 
    !    1 Apr 2012 - Narcisa Banda - v0
    !
    ! !REMARKS:
    !
    !EOP
    !------------------------------------------------------------------------
    !BOC

    character(len=*), parameter ::  rname = mname//'/emission_ed4_Read2DRecord'

    integer                          :: fid, varid
    real, dimension(nlon720,nlat360) :: emis_in

    ! initialise
    emission_ed4_Read2DRecord = 0.0

    ! search for the record

    CALL MDF_Open( TRIM(fname), MDF_NETCDF, MDF_READ, fid, status )
    IF_NOTOK_RETURN(status=1)

    CALL MDF_Inq_VarID( fid, secname, varid, status )
    IF_ERROR_RETURN(status=1)
    
    if ( varid < 0 ) then

          write (gol,'("EMISS-INFO - ED41 - no `",a,"` emissions in file", a)') &
               trim(secname), trim(fname); call goPr
          status=1; TRACEBACK; return

    else
       if( okdebug ) then
          write (gol,'("EMISS-INFO - ED41 - found `",a,"` emissions in file", a)') &
               trim(secname), trim(fname); call goPr
       endif

       CALL MDF_Get_Var( fid, varid, emis_in, status )
       IF_NOTOK_RETURN(status=1)

       ! convert from  kg(species)/m^2/s  to  kg(species)/s 
       emis_in = emis_in * gridbox_area_05

       ! combine grid cells
       emission_ed4_Read2DRecord = emission_coarsen_to_1x1( emis_in , nlon720, nlat360, .true., status )
       IF_NOTOK_RETURN(status=1)

    end if

    CALL MDF_Close( fid, status )
    IF_NOTOK_RETURN(status=1)

    status = 0

    return

  end function Emission_ed4_Read2DRecord
  !EOC


  !--------------------------------------------------------------------------
  !                    TM5                                                  !
  !--------------------------------------------------------------------------
  !BOP
  !
  ! !IROUTINE:    EMISSION_ED4_READSECTOR
  !
  ! !DESCRIPTION: Reading one sector of the files to be interpolated and 
  !               returning an interpolated 3d emission field (d3data)
  !\\
  !\\
  ! !INTERFACE:
  !
  subroutine emission_ed4_ReadSector( fpath, comp, compl, iyear, imonth, sector, version, unit, d3data, status) 
    !
    ! !INPUT PARAMETERS:
    !
    character(len=*),  intent(in)  :: fpath
    character(len=*),  intent(in)  :: comp
    character(len=*),  intent(in)  :: compl
    integer,           intent(in)  :: iyear
    integer,           intent(in)  :: sector
    character(len=*),  intent(in)  :: version
    character(len=*),  intent(in)  :: unit
    integer,           intent(in)  :: imonth
    !
    ! !OUTPUT PARAMETERS:
    !
    integer,           intent(out) :: status
    real,              intent(out) :: d3data( nlon360, nlat180, ar5_dim_3ddata)
    !
    ! !REVISION HISTORY: 
    !    1 Apr 2012 - Narcisa Banda - v0
    !
    ! !REMARKS:
    !
    !EOP
    !------------------------------------------------------------------------
    !BOC

    character(len=*), parameter ::  rname = mname//'/emission_ed4_readsector'

    character(len=256)             :: fname
    character(len=256)             :: fname_gridboxarea
    character(len=sector_name_len) :: secname
    character(len=32)              :: recname
    character(len=4)               :: ver
    integer                        :: lt, year
    logical                        :: existfile
    logical                        :: first41=.true., first42=.true.
    integer,          dimension(2) :: ltimes         
    character(len=4), dimension(2) :: ed41_cyears
    real,             dimension(2) :: ed41_ipcoef_years

    ! --- begin -----------------------------------

    ! initialise target array
    d3data = 0.0 

    ! read in gridbox-area; once per CPU
    if( .not. area_found_05 ) then
       fname_gridboxarea = trim(emis_input_dir_ar5)//'/'//trim(ed4_filestr_gridboxarea)
       call emission_ReadGridboxArea(fname_gridboxarea, 'cell_area', gridbox_area_05, &
             & nlon720, nlat360, status )
       IF_NOTOK_RETURN(status=1)
       area_found_05 = .true.
    endif


    ! ------------------------
    ! target year(s)
    ! ------------------------
    if (version == 'ED41') then

       ver='41'
       year = valid_year( iyear, ed41_avail, 'EDGAR 4.1', first41)
       first41=.false.

       allocate( ltimeind( ed41_nr_avail_yrs ) )
       ltimeind = .false.

       where( ed41_avail_yrs < year ) ltimeind = .true.
       
       ! times(1): index representing time instance earlier than current year
       ! times(2):     -"-          -"-               later than current year
       ltimes(2) = count( ltimeind ) + 1
       ltimes(1) = max( ltimes(2) - 1, 1 )

       ! check a match with repository (in order to copy only one file instead of two)
       if( ed41_avail_yrs(ltimes(2)) == year ) ltimes(1) = ltimes(2)
       deallocate( ltimeind )

       ! ed41_cyears will contain strings with the years
       write(ed41_cyears(1),'(I4.4)') ed41_avail_yrs(ltimes(1))
       write(ed41_cyears(2),'(I4.4)') ed41_avail_yrs(ltimes(2))

       ! ed41_ipcoef_years will contain interpolation coefficients
       ! default: factors 1.0/0.0
       ed41_ipcoef_years(1) = 1.0
       ed41_ipcoef_years(2) = 0.0
       if( ltimes(2) /= ltimes(1) ) then 
          ed41_ipcoef_years(1) = (ed41_avail_yrs(ltimes(2)) - year) / &
                             real( ed41_avail_yrs(ltimes(2)) - ed41_avail_yrs(ltimes(1)) )
          ed41_ipcoef_years(2) = 1.0 - ed41_ipcoef_years(1)
       end if

    else if (version == 'ED42') then
       ver='42'
       year = valid_year( iyear, ed42_avail, 'EDGAR 4.2', first42)
       first42=.false.
    else
       write (gol,'("ERROR - This version of EDGAR has not been implemented ")'); call goErr
       status=1; TRACEBACK; return
    endif


    ! ------------------------
    ! read files (EDv4.1 - index=1: earlier file; index=2: later file)
    ! ------------------------
    recname = 'emi_'//trim(compl)

    if (version=='ED41') then

       do lt = 1, 2

          if (ed41_ipcoef_years(lt) == 0.) cycle


          write (fname,'(a,"/v",a,"_",a,"_",a,"_IPCC_",a,".0.5x0.5.nc")') trim(fpath), trim(ver), trim(comp), &
               & ed41_cyears(lt), trim(sectors_def(sector)%name)

          ! test existence of file
          inquire( file=trim(fname), exist=existfile)
          if( .not. existfile ) then 
             write (gol,'(" EDGAR4.1 - file `",a,"` not found ")') trim(fname); call goErr
             status=1; TRACEBACK; return
          end if
          
          ! distinguish 2d/3d sectors
          if( sectors_def(sector)%f3d ) then 
             d3data(:,:,:) = d3data(:,:,:) + ed41_ipcoef_years(lt) * &
                                             emission_ar5_Read3DRecord( trim(fname), recname, imonth, status )
          else
             d3data(:,:,1) = d3data(:,:,1) + ed41_ipcoef_years(lt) * &
                                             emission_ed4_Read2DRecord( trim(fname), recname, status )
          end if
          IF_NOTOK_RETURN(status=1)

       enddo

    else     
       write (fname,'(a,"/v",a,"_",a,"_",i4.4,"_IPCC_",a,".0.5x0.5.nc")') trim(fpath), trim(ver), trim(comp), &
            & year, trim(sectors_def(sector)%name)

       ! test existence of file
       inquire( file=trim(fname), exist=existfile)
       if( .not. existfile ) then 
          write (gol,'(" EDGAR4.2 - file `",a,"` not found ")') trim(fname); call goErr
          status=1; TRACEBACK; return
       end if

       if( sectors_def(sector)%f3d ) then 
          d3data(:,:,:) = d3data(:,:,:) + emission_ar5_Read3DRecord( trim(fname), recname, imonth, status )
       else
          d3data(:,:,1) = d3data(:,:,1) + emission_ed4_Read2DRecord( trim(fname), recname, status )
       end if
       IF_NOTOK_RETURN(status=1)

    end if

    status=0
    
  end subroutine emission_ed4_ReadSector
  !EOC

  !--------------------------------------------------------------------------
  !                    TM5                                                  !
  !--------------------------------------------------------------------------
  !BOP
  !
  ! !IROUTINE:    EMISSION_LPJ_READSECTOR
  !
  ! !DESCRIPTION: Read an LPJ sector field out of an open file. 
  !\\
  !\\
  ! !INTERFACE:
  !
  subroutine emission_LPJ_ReadSector( fpath, iyear, imonth, sector, unit, emis, status )
    !
    ! !USES:
    !
    use dims,    only : sec_year
    !
    ! !INPUT PARAMETERS:
    !
    character(len=*), intent(in)       ::  fpath
    integer, intent(in)                ::  iyear
    integer, intent(in)                ::  imonth
    character(len=*), intent(in)       ::  sector
    character(len=*), intent(in)       ::  unit
    !
    ! !OUTPUT PARAMETERS:
    !
    real, intent(out)                  ::  emis(nlon360,nlat180)
    integer, intent(out)               ::  status
    !
    ! !REVISION HISTORY: 
    !    1 Oct 2010 - Achim Strunk - v0
    !
    ! !REMARKS:
    !
    !EOP
    !------------------------------------------------------------------------
    !BOC

    character(len=*), parameter ::  rname = mname//'/emission_LPJ_ReadSector'

    ! --- local -----------------------------------

    character(len=256)      :: fname
    character(len=256)      :: fname_gridboxarea
    real                    :: emis_in(lpj_dim_nlon,lpj_dim_nlat,1)

    integer                 :: fid, varid, year, i, j
    logical                 :: first=.true.
    
    ! --- begin -----------------------------------

    ! read in gridbox-area; once per CPU
    if( .not. lpj_area_found ) then
        fname_gridboxarea = trim(fpath)//'/'//trim(lpj_filestr_gridboxarea)
        call emission_ReadGridboxArea(fname_gridboxarea, 'areaw', lpj_gridbox_area, &
             & lpj_dim_nlon, lpj_dim_nlat, status )
       lpj_area_found = .true.
       IF_NOTOK_RETURN(status=1)
    endif

      ! Following emissions are from the EU HYMN project via the LPJ model (Spahni et al., Biogeosciences, 2011)
      ! Three categories used: peatlands, wetsoils and wetlands
      ! units in inputfiles: g CH4/y/m2
      ! coverage is 60S - 90N ; no emissions south of 60S
      ! grid cells with any emission have values -999 there skip in the summing across all emission types
      !
      ! CALCULATION EXAMPLE FOR CH4 FLUXES (e.g. for the year 2004):
      ! ***************************************************************************
      ! PEATLAND FLUX per grid cell (PLFpgc):
      ! PLF = lpj_NHpeatlands_2004.nc(ch4_flux) * 0.75
      ! PLFpgc = PLF * A * PF/2.61
      ! "The PLF is corrected for microtopography of peatlands: half of the area emits only half as much thus 25% less in total [Wania et al., 2010]"
      ! "The PF map overestimates total peatland area by a factor of 2.61 compared to Prigent et al., 2007"
      !RICE AGRICULTURE FLUX per grid cell (RAFpgc) - Not Used as implicitly in EDGAR 4.0
      !RAF = lpj_rice_2004.nc(ch4_flux)
      !RAFpgc = RAF * A * RF

      !WETLAND FLUX per grid cell (WLFpgc):
      !WLF = lpj_wetlands_2004.nc(ch4_flux)
      !WLFpgc = WLF * A * IF

      !WETSOILS FLUX per grid cell (WSFpgc):
      !WSF = lpj_wetsoils_2004.nc(ch4_flux)
      !WSFpgc = WSF * A * (1-PF/2.61-IF-RF)
      !"PF is set =0 between 60°S-45°N, IF is set =0 between 45°N-90°N" 

      !TOTAL NET FLUX per grid cell (TFpgc):
      !TFpgc = PLFpgc + WLFpgc + RAFpgc + WSFpgc - SCFpgc
      !e.g. for 2004 this results in a global net source:
      !TFpgc = 28.17 + 81.31 + 43.11  + 63.16 -  25.83 Tg CH4/year
      !TFpgc = 189.92 Tg CH4/year
      !
      ! sink term for uptake of CH4 by soils given in mg CH4/ y/ m2 / ppmv(CH4)
      ! therefore store field and then apply later on using latitudinal average for CH4 
      ! 
      ! SOIL CONSUMPTION FLUX per grid cell (SCFpgc):
      ! SCF = lpj_soilconsumption_2004.nc(ch4_flux)
      ! or
      ! SCF = lpj_soilconsumption-perconc_2004.nc(ch4_flux) * atm. CH4 (in ppmv)
      ! SCFpgc = SCF * A * (1-IF-RF)  "peatland grid cells are already excluded"      

    if( .not. lpj_fractions_found ) then

       fname = trim(fpath)//'/maps/lpj_natwet_fraction.nc'

       CALL MDF_Open( TRIM(fname), MDF_NETCDF, MDF_READ, fid, status )
       IF_NOTOK_RETURN(status=1)

       CALL MDF_Inq_VarID( fid, 'inund', varid, status )
       IF_ERROR_RETURN(status=1)

       CALL MDF_Get_Var( fid, varid, lpj_frac_wetlands, status )
       IF_NOTOK_RETURN(status=1)

       CALL MDF_Close( fid, status )
       IF_NOTOK_RETURN(status=1)

       
       fname = trim(fpath)//'/maps/lpj_rice_fraction.nc'

       CALL MDF_Open( TRIM(fname), MDF_NETCDF, MDF_READ, fid, status )
       IF_NOTOK_RETURN(status=1)

       CALL MDF_Inq_VarID( fid, 'rice', varid, status )
       IF_ERROR_RETURN(status=1)

       CALL MDF_Get_Var( fid, varid, lpj_frac_rice, status )
       IF_NOTOK_RETURN(status=1)

       CALL MDF_Close( fid, status )
       IF_NOTOK_RETURN(status=1)

       
       fname = trim(fpath)//'/maps/lpj_peatland_fraction.nc'

       CALL MDF_Open( TRIM(fname), MDF_NETCDF, MDF_READ, fid, status )
       IF_NOTOK_RETURN(status=1)

       CALL MDF_Inq_VarID( fid, 'scfrac', varid, status )
       IF_ERROR_RETURN(status=1)

       CALL MDF_Get_Var( fid, varid, lpj_frac_peatlands, status )
       IF_NOTOK_RETURN(status=1)

       CALL MDF_Close( fid, status )
       IF_NOTOK_RETURN(status=1)


       lpj_fractions_found = .true.

       lpj_frac_peatlands = lpj_frac_peatlands/2.61

    endif

    year = valid_year( iyear, lpj_avail, 'LPJ', first)
    first=.false.
    
    ! target file name with year
    if (trim(sector).eq.'soilconsumption') then
       write (fname,'(a,"/",a,"/lpj_",a,"-perconc_",i4.4,".nc")') trim(fpath), trim(sector), trim(sector), year
    else 
      write (fname,'(a,"/",a,"/lpj_",a,"_",i4.4,".nc")') trim(fpath), trim(sector), trim(sector), year
    end if
   
    CALL MDF_Open( TRIM(fname), MDF_NETCDF, MDF_READ, fid, status )
    IF_NOTOK_RETURN(status=1)

    CALL MDF_Inq_VarID( fid, 'ch4_flux', varid, status )
    IF_NOTOK_RETURN(status=1)

    if( okdebug ) then
       write (gol,'("EMISS-INFO - LPJ - found ch4 emissions for sector `",a,"` in file ",a)') &
            trim(sector), trim(fname); call goPr
    endif

    emis = 0.0

    ! extract record for requested month
    CALL MDF_Get_Var( fid, varid, emis_in, status, start=(/1,1,imonth/) )
    IF_NOTOK_RETURN(status=1)

    do j=1,lpj_dim_nlat
    do i=1, lpj_dim_nlon
       if(emis_in(i,j,1)>0.) then
 
          select case( trim(sector) )
          case('wetlands')
             !convert from [g m-2 per year] to [kg per cell per sec]
             emis(i,j+30) = emis_in(i,j,1)*lpj_gridbox_area(i,j)*lpj_frac_wetlands(i,j,imonth)*(1.e-3/sec_year)
          case('peatlands')
             emis(i,j+30) = emis_in(i,j,1)*0.75*lpj_gridbox_area(i,j)*lpj_frac_peatlands(i,j)*(1.e-3/sec_year)
          case('wetsoils')
             emis(i,j+30) = emis_in(i,j,1)*lpj_gridbox_area(i,j)*&
                  &(1.-lpj_frac_wetlands(i,j,imonth)-lpj_frac_peatlands(i,j)-lpj_frac_rice(i,j,imonth))*(1.e-3/sec_year)
          case('rice')
             emis(i,j+30) = emis_in(i,j,1)*lpj_gridbox_area(i,j)*lpj_frac_rice(i,j,imonth)*(1.e-3/sec_year)
          case('soilconsumption')
             !convert from [mg m-2 per year per ppmv] to [kg per cell per sec per ppmv]
             emis(i,j+30) = emis_in(i,j,1)*lpj_gridbox_area(i,j)*&
                  &(1. - lpj_frac_wetlands(i,j,imonth) - lpj_frac_rice(i,j,imonth))*(1.e-6/sec_year)
          end select
       endif
    enddo
    enddo

    ! no coarsening needed, but used to shift the longitudes from [0, 360]
    ! to [-180, 180]
    emis = emission_coarsen_to_1x1( emis, nlon360, nlat180,.true., status )

    CALL MDF_Close( fid, status )
    IF_NOTOK_RETURN(status=1)

    status = 0

  end subroutine Emission_LPJ_ReadSector
  !EOC

  !--------------------------------------------------------------------------
  !                    TM5                                                  !
  !--------------------------------------------------------------------------
  !BOP
  !
  ! !IROUTINE:    EMISSION_HYMN_READSECTOR
  !
  ! !DESCRIPTION: Read a HYMN Non-LPJ sector field out of an open file. 
  !\\
  !\\
  ! !INTERFACE:
  !
  subroutine emission_HYMN_ReadSector( fpath, sector, unit, emis, status )
    !
    ! !USES:
    !
    use dims,     only : sec_year, dxy11
    !
    ! !INPUT PARAMETERS:
    !
    character(len=*), intent(in)       ::  fpath
    character(len=*), intent(in)       ::  sector
    character(len=*), intent(in)       ::  unit
    !
    ! !OUTPUT PARAMETERS:
    !
    real, intent(out)                  ::  emis(nlon360,nlat180)
    integer, intent(out)               ::  status
    !
    ! !REVISION HISTORY: 
    !    1 Oct 2010 - Achim Strunk - v0
    !
    ! !REMARKS:
    !
    !EOP
    !------------------------------------------------------------------------
    !BOC

    character(len=*), parameter ::  rname = mname//'/emission_HYMN_ReadSector'

    ! --- local -----------------------------------

    character(len=256)      :: fname
    character(len=256)      :: fname_gridboxarea
    integer                 :: fid, varid, i, j

    real                    :: emis_in(nlon360,nlat180,1,1)
    character(len=80)       :: title
    character(len=80)       :: units

    ! --- begin -----------------------------------

    ! target file name with year
    select case(trim(sector))
    case('oceans')
        write (fname,'(a,"/CH4-natural-nonLPJ/CH4-N40-Lambert-0000-sfc-glb100x100.nc4")'  ) trim(fpath)
    case('wildanimals')
        write (fname,'(a,"/CH4-natural-nonLPJ/CH4-N70-Olson-0000-sfc-glb100x100.nc4")'    ) trim(fpath)
    case('termites')
        write (fname,'(a,"/CH4-natural-nonLPJ/CH4-N71-Sanderson-0000-sfc-glb100x100.nc4")') trim(fpath)
    end select


    CALL MDF_Open( TRIM(fname), MDF_NETCDF, MDF_READ, fid, status )
    IF_NOTOK_RETURN(status=1)
    
    CALL MDF_Inq_VarID( fid, 'field', varid, status )
    IF_NOTOK_RETURN(status=1)

    if( okdebug ) then    
       write (gol,'("EMISS-INFO - HYMN - found ch4 emissions from sector `",a,"` in file ",a)') &
            trim(sector), trim(fname); call goPr
    endif
    
    emis = 0.0

    CALL MDF_Get_Var( fid, varid, emis_in, status )
    IF_NOTOK_RETURN(status=1)
    
    ! from [kg/cell/yr] to [kg/cell/sec]
    emis(:,:) = emis_in(:,:,1,1)/sec_year

    CALL MDF_Close( fid, status )
    IF_NOTOK_RETURN(status=1)

    status = 0

  end subroutine Emission_HYMN_ReadSector
  !EOC



  !--------------------------------------------------------------------------
  !                    TM5                                                  !
  !--------------------------------------------------------------------------
  !BOP
  !
  ! !IROUTINE:  emission_gfed_ReadSector
  !
  ! !DESCRIPTION: Read GFEDv3 out of an open file. 
  !\\
  !\\
  ! !INTERFACE:
  !
  subroutine emission_gfed_ReadSector( fpath, comp, iyear, imonth, recname, unit, emis, status )
    !
    ! !USES:
    !
    use chem_param, only : xmn, xmno2
    !
    ! !INPUT PARAMETERS:
    !
    character(len=*), intent(in) ::  fpath
    character(len=*), intent(in) ::  comp
    integer,          intent(in) ::  iyear
    integer,          intent(in) ::  imonth
    character(len=*), intent(in) ::  recname
    character(len=*), intent(in) ::  unit
    !
    ! !OUTPUT PARAMETERS:
    !
    real,      intent(out) ::  emis(nlon360,nlat180)
    integer,   intent(out) ::  status
    !
    ! !REVISION HISTORY: 
    !    1 Oct 2010 - Achim Strunk -
    !
    ! !REMARKS:
    !
    !EOP
    !------------------------------------------------------------------------
    !BOC

    character(len=*), parameter ::  rname = mname//'/emission_gfed_ReadSector'

    ! --- local -----------------------------------

    character(len=256)      ::  fname
    real                    ::  emis_in(nlon720,nlat360,1)
    real                    ::  emis_help(nlon360,nlat180)
    integer                 ::  fid, varid, year, j
    logical                 ::  first=.true.

    ! --- begin -----------------------------------

    ! target file name with year

    year = valid_year( iyear, gfed3_avail, 'GFEDv3', first)
    first=.false.

    write (fname,'(a,"/GFEDv3_surface_",a,"_",i4.4,".0.5x0.5.nc")') trim(fpath), trim(comp), year
    
    ! read in gridbox-area; once per CPU
    if( .not. area_found_05 ) then
       call emission_ReadGridboxArea(fname, 'gridbox_area', gridbox_area_05, &
             & nlon720, nlat360, status )
       IF_NOTOK_RETURN(status=1)
       area_found_05 = .true.
    endif

    CALL MDF_Open( TRIM(fname), MDF_NETCDF, MDF_READ, fid, status )
    IF_NOTOK_RETURN(status=1)

    CALL MDF_Inq_VarID( fid, trim(recname), varid, status )
    IF_ERROR_RETURN(status=1)

    if ( varid < 0 ) then

       write (gol,'("EMISS-INFO - GFEDv3 - no `",a,"` emissions for `",a,"` in file ",a)') &
            trim(recname), trim(comp), trim(fname); call goErr

       status=1; TRACEBACK; return

    else

       if( okdebug ) then
          write (gol,'("EMISS-INFO - GFEDv3 - found `",a,"` emissions for `",a,"` in file ",a)') &
               trim(recname), trim(comp), trim(fname); call goPr
       endif

       CALL MDF_Get_Var( fid, varid, emis_in, status, start=(/1,1,imonth/) )
       IF_NOTOK_RETURN(status=1)

       ! convert from  kg(species)/m^2/s  to  kg(species)/s
       emis_in(:,:,1) = emis_in(:,:,1) * gridbox_area_05

       ! combine grid cells :
       if (trim(comp)=='bc' .or. trim(comp)=='oc') then
         ! GFED3 emissions of BC and OC are correctly given in the files.
         ! They don't need a shift in the zonal direction, but latitudes need to be reversed.
         emis_help = emission_coarsen_to_1x1( emis_in(:,:,1), nlon720, nlat360, .false., status )
         IF_NOTOK_RETURN(status=1)
         do j=1,nlat180
           emis(:,j)=emis_help(:,nlat180-j+1)
         end do
       else
         ! GFED3 emissions of other components are erroneously shifted by 180 degrees in the files.
         ! This is corrected by applying a shift to the data.
         emis = emission_coarsen_to_1x1( emis_in(:,:,1), nlon720, nlat360, .true., status )
         IF_NOTOK_RETURN(status=1)
       endif

    endif

    CALL MDF_Close( fid, status )
    IF_NOTOK_RETURN(status=1)

    status = 0

  end subroutine Emission_gfed_ReadSector
  !EOC

  !--------------------------------------------------------------------------
  !                    TM5                                                  !
  !--------------------------------------------------------------------------
  !BOP
  !
  ! !IROUTINE:    emission_retro_ReadSector
  !
  ! !DESCRIPTION: Read a RETRO sector field out of an open file. 
  !\\
  !\\
  ! !INTERFACE:
  !
  subroutine emission_retro_ReadSector( fpath, comp, iyear, imonth, recname, unit, emis, status )
    !
    ! !USES:
    !
    use chem_param, only : xmn, xmno2
    !
    ! !INPUT PARAMETERS:
    !
    character(len=*), intent(in)       ::  fpath
    character(len=*), intent(in)       ::  comp
    integer, intent(in)                ::  iyear
    integer, intent(in)                ::  imonth
    character(len=*), intent(in)       ::  recname
    character(len=*), intent(in)       ::  unit
    !
    ! !OUTPUT PARAMETERS:
    !
    real, intent(out)                  ::  emis(nlon360,nlat180)
    integer, intent(out)               ::  status
    !
    ! !REVISION HISTORY: 
    !    1 Oct 2010 - Achim Strunk -
    !
    ! !REMARKS:
    !
    !EOP
    !------------------------------------------------------------------------
    !BOC

    character(len=*), parameter ::  rname = mname//'/emission_retro_ReadSector'

    character(len=256)      :: fname
    real                    :: emis_in(nlon720,nlat360,1)
    integer                 :: fid, varid, year
    logical                 :: first=.true.
    
    ! --- begin -----------------------------------

    year = valid_year( iyear, retro_avail, 'RETRO', first)
    first=.false.
    
    write (fname,'(a,"/RETRO_FIRES_V2_",i4.4,"_",a,"_aggregated.nc")') trim(fpath), year, trim(comp)

    ! read in gridbox-area
    if( .not. area_found_05 ) then
       call emission_ReadGridboxArea(fname, 'gridbox_area', gridbox_area_05, &
             & nlon720, nlat360, status )
       IF_NOTOK_RETURN(status=1)
       area_found_05 = .true.
    endif

    
    CALL MDF_Open( TRIM(fname), MDF_NETCDF, MDF_READ, fid, status )
    IF_NOTOK_RETURN(status=1)

    CALL MDF_Inq_VarID( fid, trim(recname), varid, status )
    IF_ERROR_RETURN(status=1)

    if ( varid < 0 ) then

       write (gol,'("EMISS-INFO - RETRO - no `",a,"` emissions for `",a,"` in file ", a)') &
            trim(recname), trim(comp), trim(fname); call goErr

       status=1; TRACEBACK; return

    else

       if( okdebug ) then
          write (gol,'("EMISS-INFO - RETRO - found `",a,"` emissions for `",a,"` in file ", a)') &
               trim(recname), trim(comp), trim(fname); call goPr
       endif
       
       CALL MDF_Get_Var( fid, varid, emis_in, status, start=(/1,1,imonth/) )
       IF_NOTOK_RETURN(status=1)

       !convert from  kg(species)/m^2/s  to  kg(species)/s
       emis_in(:,:,1) = emis_in(:,:,1) * gridbox_area_05

       ! combine grid cells :
       emis = emission_coarsen_to_1x1( emis_in(:,:,1), nlon720, nlat360, .true., status )
       IF_NOTOK_RETURN(status=1)

    end if  ! emis category found

    CALL MDF_Close( fid, status )
    IF_NOTOK_RETURN(status=1)

    status = 0

  end subroutine Emission_retro_ReadSector
  !EOC

  !--------------------------------------------------------------------------
  !                    TM5                                                  !
  !--------------------------------------------------------------------------
  !BOP
  !
  ! !IROUTINE:    emission_megan_ReadSector
  !
  ! !DESCRIPTION: Read a MACC-MEGAN sector field out of an open file. 
  !\\
  !\\
  ! !INTERFACE:
  !
  subroutine emission_megan_ReadSector( fpath, comp, iyear, imonth, recname, unit, emis, status )
    !
    ! !USES:
    !
    use chem_param, only : xmn, xmno2
    !
    ! !INPUT PARAMETERS:
    !
    character(len=*), intent(in)       ::  fpath
    character(len=*), intent(in)       ::  comp
    integer, intent(in)                ::  iyear
    integer, intent(in)                ::  imonth
    character(len=*), intent(in)       ::  recname
    character(len=*), intent(in)       ::  unit
    !
    ! !OUTPUT PARAMETERS:
    !
    real, intent(out)                  ::  emis(nlon360,nlat180)
    integer, intent(out)               ::  status
    integer                            ::  i,j
    !
    !   REVISION HISTORY: 
    !    29 Jan 2014 - Jason Williams -
    !
    ! !REMARKS:
    !
    !EOP
    !------------------------------------------------------------------------
    !BOC

    character(len=*), parameter ::  rname = mname//'/emission_megan_ReadSector'

    character(len=256)      :: fname
    real                    :: emis_in(nlon720,nlat360,1)
    real                    :: flip_array(360,180)
    integer                 :: fid, varid, year
    logical                 :: first=.true.

    ! --- begin -----------------------------------

    ! target file name with year
    
    year = valid_year( iyear, megan_avail, 'MACC-MEGAN', first)
    first=.false.

    write (fname,'(a,"/MEGAN-MACC_biogenic_",i4.4,"_",a,".nc")') trim(fpath), year, trim(comp)

    ! read in gridbox-area; once per CPU
    if( .not. area_found_05 ) then
       call emission_ReadGridboxArea(fname, 'gridbox_area', gridbox_area_05, nlon720, nlat360, status )
       IF_NOTOK_RETURN(status=1)
       area_found_05 = .true.
    endif

    CALL MDF_Open( TRIM(fname), MDF_NETCDF, MDF_READ, fid, status )
    IF_NOTOK_RETURN(status=1)

    CALL MDF_Inq_VarID( fid, trim(recname), varid, status )
    IF_ERROR_RETURN(status=1)

    if ( varid < 0 ) then

       write (gol,'("EMISS-INFO - MACC-MEGAN - no `",a,"` emissions for `",a,"` in file ", a)') &
            trim(recname), trim(comp), trim(fname); call goErr

       status=1; TRACEBACK; return

    else

       if (okdebug) then
          write (gol,'("EMISS-INFO - MACC-MEGAN - found `",a,"` emissions for `",a,"` in file ", a)') &
               trim(recname), trim(comp), trim(fname); call goPr
       endif

       CALL MDF_Get_Var( fid, varid, emis_in, status, start=(/1,1,imonth/) )
       IF_NOTOK_RETURN(status=1)

       !convert from  kg(species)/m^2/s  to  kg(species)/s
       emis_in(:,:,1) = emis_in(:,:,1) * gridbox_area_05

       ! combine grid cells :
       emis = emission_coarsen_to_1x1( emis_in(:,:,1), nlon720, nlat360, .true., status )
       IF_NOTOK_RETURN(status=1)

       flip_array(:,:)=emis(:,:)
       !
       !   flip the array
       !            
       do j=1,180
          emis(:,j)=flip_array(:,(180-j)+1)
       enddo

       do i=1,180
          flip_array(i,:)=emis(i+180,:)
          flip_array(i+180,:)=emis(i,:)
       enddo

       do j=1,180
          emis(:,j)=flip_array(:,j)
       enddo

    end if  ! emis category found

    CALL MDF_Close( fid, status )
    IF_NOTOK_RETURN(status=1)

    status = 0

  end subroutine Emission_megan_ReadSector
  !EOC

END MODULE EMISSION_READ
