#!/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
from hermesv3_gr.config import settings, log_message
from .gfas_emission_inventory import GfasEmissionInventory


class GfasHourlyEmissionInventory(GfasEmissionInventory):

    def __init__(self, options, grid, current_date, inventory_name, source_type, sector, pollutants, inputs_path,
                 frequency, vertical_output_profile,
                 reference_year=2010, coverage='global', factors=None, regrid_mask=None, p_vertical=None, p_month=None,
                 p_week=None, p_day=None, p_hour=None, p_speciation=None, countries_shapefile=None):

        super(GfasHourlyEmissionInventory, self).__init__(
            options, grid, current_date, inventory_name, source_type, sector, pollutants, inputs_path, frequency,
            vertical_output_profile,
            reference_year=reference_year, coverage=coverage, factors=factors, regrid_mask=regrid_mask,
            p_vertical=p_vertical, p_month=None, p_week=None, p_day=None, p_hour=None,
            p_speciation=p_speciation, countries_shapefile=countries_shapefile)

    @staticmethod
    def print_ini_message():
        """
        Print initial message
        """
        log_message('Creating GFAS hourly area source emission inventory.', level=3)
        return None

    def get_input_path(self, pollutant=None, extension='nc'):
        """
        Completes the path of the NetCDF that contains the needed information of the given pollutant.

        Parameters
        ----------
        pollutant : str , None
            Name of the pollutant of the NetCDF.
        extension : str
            Extension of the input file.

        Returns
        -------
        str
            Full path of the needed NetCDF.
        """
        netcdf_path = os.path.join(self.inputs_path, 'multivar', 'ga_{0}.{1}'.format(
            self.date.strftime('%Y%m%d_%H'), extension))
        return netcdf_path

    def get_4d_emissions(self):
        self.src_emis_file_list = self.get_src_file_list(None)
        self.prepare_input_emissions(write_aux_file=False)

        if self.speciation is not None:
            self.emissions = self.speciation.do_speciation(self.emissions)

        return super(GfasHourlyEmissionInventory, self).get_4d_emissions()
