#!/usr/bin/env python

# Copyright 2018 Earth Sciences Department, BSC-CNS
#
# This file is part of HERMESv3_GR.
#
# HERMESv3_GR is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# HERMESv3_GR is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with HERMESv3_GR. If not, see <http://www.gnu.org/licenses/>.


import os
import timeit
from netCDF4 import Dataset

# ============== README ======================
"""
downloading website: ¿¿¿???
reference: ¿¿¿???
Besides citing HERMESv3_GR, users must also acknowledge the use of the corresponding emission inventories in their works
"""

# ============== CONFIGURATION PARAMETERS ======================
INPUT_PATH = '/esarchive/recon/uchile/inema_v1/original_files/<year>/<sector>/inema_<pollutant>_<subsector>_0.01x0.01_<year>.nc'
OUTPUT_PATH = '/esarchive/recon/uchile/inema_v1/'
SECTOR_LIST = {'Transport': None,
               'Energy': None,
               'Industry': None,
               'Mining': None,
               'Residential': ['Residential', 'Rural', 'Urban']}
POLLUTANT_INFO = {'bc': 'BC', 'c6h6': 'C6H6', 'ch4': 'CH4', 'co2': 'CO2', 'co': 'CO', 'hg': 'Hg', 'nh3': 'NH3',
                  'nmvoc': 'NMVOC', 'no': 'NO', 'no2': 'NO2', 'nox_no2': 'NOx', 'pb': 'Pb', 'pcddf': 'PCDDF',
                  'pm10': 'PM10', 'pm25': 'PM25', 'pm': 'PM', 'so2': 'SO2', 'tol': 'Toluene', 'voc': 'VOC'}

YEAR_LIST = [2016, 2017]
# ==============================================================


def do_transformation():
    from hermesv3_gr.tools.netcdf_tools import write_netcdf
    from hermesv3_gr.tools.coordinates_tools import create_bounds
    import calendar
    import numpy as np
    from datetime import datetime

    for sector in SECTOR_LIST.keys():
        if SECTOR_LIST[sector] is None:
            subsector_list = [sector]
        else:
            subsector_list = SECTOR_LIST[sector]
        for subsector in subsector_list:
            for year in YEAR_LIST:
                for pollutant in POLLUTANT_INFO.keys():
                    out_file_name = os.path.join(OUTPUT_PATH, 'yearly_mean',
                                                 '{0}_{1}'.format(pollutant, subsector),
                                                 "{0}_{1}.nc".format(pollutant, year))
                    file_name = INPUT_PATH.replace('<year>', str(year)).replace('<sector>', sector).replace(
                        '<pollutant>', POLLUTANT_INFO[pollutant]).replace('<subsector>', subsector)
                    try:
                        netcdf = Dataset(file_name, mode='r')
                        lat_nc = netcdf.variables['lat'][:]
                        lon_nc = netcdf.variables['lon'][:]
                        lat_inc = round(lat_nc[1] - lat_nc[0], 5)
                        lon_inc = round(lon_nc[1] - lon_nc[0], 5)

                        lats = np.linspace(lat_nc[0], lat_nc[0] + (lat_inc * (len(lat_nc) - 1)), len(lat_nc),
                                           dtype=np.float)
                        lons = np.linspace(lon_nc[0], lon_nc[0] + (lon_inc * (len(lon_nc) - 1)), len(lon_nc),
                                           dtype=np.float)
                        if lons[-1] < lons[0]:
                            flip = True
                            lons = np.flip(lons)
                        else:
                            flip = False

                        del lat_nc, lon_nc
                        blats = create_bounds(lats)
                        blons = create_bounds(lons)

                        var = netcdf.variables[POLLUTANT_INFO[pollutant]][:]
                        var = var.data
                        var[var == netcdf.variables[POLLUTANT_INFO[pollutant]].getncattr('_FillValue')] = 0
                        if flip:
                            var = np.flip(var, axis=1)

                        if calendar.isleap(year):
                            days = 366
                        else:
                            days = 365

                        # from ktonne/km2 year to kg/m2.s
                        var = var * (1000000 / (days * 24 * 60 * 60 * 1000000))

                        data = [{'name': pollutant, 'units': 'kg.m-2.s-1', 'data': var.reshape((1,) + var.shape)}]

                        if not os.path.exists(os.path.dirname(out_file_name)):
                            os.makedirs(os.path.dirname(out_file_name))
                        print(out_file_name)
                        write_netcdf(out_file_name, lats, lons, data, date=datetime(year=year, month=1, day=1),
                                     boundary_latitudes=blats, boundary_longitudes=blons, cell_area=cell_area,
                                     regular_latlon=True)
                    except FileNotFoundError:
                        print("Variable {0} not found in {1}: {2}".format(pollutant, subsector, file_name))


if __name__ == '__main__':
    starting_time = timeit.default_timer()

    do_transformation()

    print('Time(s):', timeit.default_timer() - starting_time)
