

import os
import numpy as np
import cartopy.crs as ccrs
import shapely.geometry

import c2sm


HOST = os.environ['HOST']

if HOST.startswith('daint'):
    HOST = 'daint'
elif HOST.startswith('ddm'):
    HOST = 'ddm'
else:
    HOST = None
    print('Warning: Host "%d" unknown. Data paths need to be given explicit.' % HOST )


# data path should contain the following data:
#  - cosmo_2015-01/ (currently not on ddm)
#  - cosmo_2015-07/ (currently not on ddm)
#  - cosmo2D_2015-01/
#  - cosmo2D_2015-07/ 
#  - level2/

DATA_PATH = {
    'daint': '/project/d73/output',
    'ddm':   '/project/smartcarb/data1'
}.get(HOST, None)


WIND_PATH = '/project/smartcarb/flux_inversion/'



BG_STATS_FILENAME = { # TODO: update for daint
        'Berlin': {
            'CO2': os.path.join(DATA_PATH, 'ts', 'ts_XCO2_background_Berlin_100.dat'),
            'NO2': os.path.join(DATA_PATH, 'ts', 'ts_NO2_background_Berlin_100.dat'),
        },
        'Janschwalde': {
            'CO2': os.path.join(DATA_PATH, 'ts', 'ts_XCO2_background_Janschwalde_50.dat'),
            'NO2': os.path.join(DATA_PATH, 'ts', 'ts_NO2_background_Janschwalde_50.dat'),
        },
        'Lippendorf': {
            'CO2': os.path.join(DATA_PATH, 'ts', 'ts_XCO2_background_Lippendorf_50.dat'),
            'NO2': os.path.join(DATA_PATH, 'ts', 'ts_NO2_background_Lippendorf_50.dat'),
        }
}

# location of point sources in model grid
POINT_SOURCES = {
    'Janschwalde':    (14.4534902573, 51.841545105),
    'Schwarze Pumpe': (14.3610925674, 51.534286499),
    'Boxberg':        (14.5746955872, 51.4164924622),
    'Lippendorf':     (12.3712453842, 51.1874504089),
    'Turow':          (14.9112815857, 50.9428253174),
    'Patnow':         (18.2375602722, 52.3044433594)
}

CITIES = {
    'Berlin': (13.407696, 52.516984)
}

# used for x and y coords
EPSG = 3006


# settings for berlin 2 (big) domain - smartcarb production run
ie       = 700
je       = 600
ke       = 60
pollon   = -170.0  # origin lon: 10.0
pollat   =   43.0  # origin lat: 47.0
dlon     = 0.01
dlat     = 0.01
startlat =  2.5
startlon = -1.4

stoplat = startlat + (je-1) * dlat
stoplon = startlon + (ie-1) * dlon

DOMAIN = shapely.geometry.Polygon([
    (startlon - 0.5 * dlon, startlat - 0.5 * dlat),
    (stoplon  + 0.5 * dlon, startlat - 0.5 * dlat),
    (stoplon  + 0.5 * dlon, stoplat  + 0.5 * dlat),
    (startlon - 0.5 * dlon, stoplat  + 0.5 * dlat)
])


INPUT_ORG_FNAME = '/project/kug/cosmo/python-cosmo/tests/INPUT_ORG'
DOMAINS = {
    'smartcarb': c2sm.cosmo.Domain.from_nml(INPUT_ORG_FNAME),
    'europe':    c2sm.cosmo.Domain('europe', startlon=-12, stoplon=42,
                                   startlat=33, stoplat=72),
    'globe':     c2sm.cosmo.Domain('globe', startlon=-180, stoplon=180,
                                   startlat=-80, stoplat=80),
    'berlin':    c2sm.cosmo.Domain('berlin', startlon=1.8, stoplon=2.4,
                                   startlat=5.35, stoplat=5.75,
                                   pollon=pollon, pollat=pollat),
}

# center coords
rlon = np.linspace(startlon, stoplon, ie)
rlat = np.linspace(startlat, stoplat, je)

rlon2, rlat2 = np.meshgrid(rlon, rlat)

# corner coords
rlonc = np.linspace(startlon - 0.5 * dlon, stoplon + 0.5 * dlon, ie+1)
rlatc = np.linspace(startlat - 0.5 * dlat, stoplat + 0.5 * dlat, je+1)



WGS84 = ccrs.PlateCarree()
ROTPOLE = ccrs.RotatedPole(pole_latitude=pollat, pole_longitude=pollon)


tracer2long_name = {
 'XCO2_A': 'XCO2_from_time-varying_emissions_for_all_sources_in_domain_excl_Berli',
 'XCO2_B0': 'XCO2_from_time-varying_emissions_for_industry_in_Berlin',
 'XCO2_B1': 'XCO2_from_time-varying_emissions_for_heating_in_Berlin',
 'XCO2_B2': 'XCO2_from_time-varying_emissions_for_traffic_in_Berlin',
 'XCO2_BC': 'XCO2_from_time-constant_emissions_for_all_sources_in_Berlin',
 'XCO2_BG': 'time-varying_XCO2_at_lateral_boundary',
 'XCO2_BV': 'XCO2_from_time-varying_emissions_for_all_sources_in_Berlin',
 'XCO2_GPP': 'XCO2_from_time-varying_surface_flux_from_gross_photosynthetic_product',
 'XCO2_JC': 'XCO2_from_time-constant_emissions_for_Jaenschwalde',
 'XCO2_JV': 'XCO2_from_time-varying_emissions_for_Jaenschwalde',
 'XCO2_NPR': 'XCO2_from_time-varying_emissions_without_plume_rise',
 'XCO2_RA': 'XCO2_from_time-varying_surface_flux_from_respiration',
 'XCO2_SURF': 'XCO2_from_time-varying_emissions_for_all_sources_in_domain_but_emitte',
 'XCO2_T00': 'XCO2_from_time-varying_emissions_in_Berlin_between_00-03_UTC',
 'XCO2_T01': 'XCO2_from_time-varying_emissions_in_Berlin_between_03-06_UTC',
 'XCO2_T02': 'XCO2_from_time-varying_emissions_in_Berlin_between_06-09_UTC',
 'XCO2_T03': 'XCO2_from_time-varying_emissions_in_Berlin_between_09-12_UTC',
 'XCO2_T04': 'XCO2_from_time-varying_emissions_in_Berlin_between_12-15_UTC',
 'XCO2_T05': 'XCO2_from_time-varying_emissions_in_Berlin_between_15-18_UTC',
 'XCO2_T06': 'XCO2_from_time-varying_emissions_in_Berlin_between_18-21_UTC',
 'XCO2_T07': 'XCO2_from_time-varying_emissions_in_Berlin_between_21-24_UTC',
 'XCO2_TOT': 'XCO2_from_time-varying_emissions_for_all_sources_in_domain',
 'CO_A_column': 'CO_from_time-varying_emissions_for_all_sources_in_domain_excl_Berlin',
 'CO_B0_column': 'CO_from_time-varying_emissions_for_industry_in_Berlin',
 'CO_B1_column': 'CO_from_time-varying_emissions_for_heating_in_Berlin',
 'CO_B2_column': 'CO_from_time-varying_emissions_for_traffic_in_Berlin',
 'CO_BC_column': 'CO_from_time-constant_emissions_for_all_sources_in_Berlin',
 'CO_BG_column': 'time-varying_CO_at_lateral_boundary',
 'CO_BV_column': 'CO_from_time-varying_emissions_for_all_sources_in_Berlin',
 'CO_JC_column': 'CO_from_time-constant_emissions_for_Jaenschwalde',
 'CO_JV_column': 'CO_from_time-varying_emissions_for_Jaenschwalde',
 'CO_NPR_column': 'CO_from_time-varying_emissions_without_plume_rise',
 'CO_SURF_column': 'CO_from_time-varying_emissions_for_all_sources_in_domain_but_emitted',
 'CO_TOT_column': 'CO_from_time-varying_emissions_for_all_sources_in_domain',
 'NO2_A_column': 'NO2_from_time-varying_emissions_for_all_sources_in_domain_excl_Berli',
 'NO2_B0_column': 'NO2_from_time-varying_emissions_for_industry_in_Berlin',
 'NO2_B1_column': 'NO2_from_time-varying_emissions_for_heating_in_Berlin',
 'NO2_B2_column': 'NO2_from_time-varying_emissions_for_traffic_in_Berlin',
 'NO2_BC_column': 'NO2_from_time-constant_emissions_for_all_sources_in_Berlin',
 'NO2_BG_column': 'time-varying_NO2_at_lateral_boundary',
 'NO2_BV_column': 'NO2_from_time-varying_emissions_for_all_sources_in_Berlin',
 'NO2_JC_column': 'NO2_from_time-constant_emissions_for_Jaenschwalde',
 'NO2_JV_column': 'NO2_from_time-varying_emissions_for_Jaenschwalde',
 'NO2_NPR_column': 'NO2_from_time-varying_emissions_for_Jaenschwalde_without_plume_rise',
 'NO2_R00_column': 'NO2_from_time-varying_emissions_for_all_sources_without_decay',
 'NO2_R02_column': 'NO2_from_time-varying_emissions_for_all_sources_with_decay_time_from_2',
 'NO2_R12_column': 'NO2_from_time-varying_emissions_for_all_sources_with_decay_time_from_1',
 'NO2_R24_column': 'NO2_from_time-varying_emissions_for_all_sources_with_decay_time_from_2',
 'NO2_SURF_column': 'NO2_from_time-varying_emissions_for_all_sources_in_domain_but_emitte',
 'NO2_TOT_column': 'NO2_from_time-varying_emissions_for_all_sources_in_domain',
 'CLCT': 'total cloud cover as cloud area fraction'
}
