#!/usr/bin/env python
#
# Data Quality Checker also known as DQC
#
# Read the README.md for more detailed information
#
#       Barcelona Supercomputing Center - Centro Nacional de Supercomputacion
#       Earth Sciences Department - Computational Earth Sciences
#
#     Authors:
#       Joan Sala Calero (joan.sala@bsc.es)

import os
import json

# Custom packages
from C3S512.cds import downloader

"""
Downloader
"""
def call_download(cred, outfile, info, prod, logdir):
    # Download or skip (do not overwrite)
    if not os.path.exists(outfile):
        print("DOWN = {}".format(os.path.basename(outfile)))
        print(info)
        downloader(prod['dataset'], info, outfile, logpath=logdir, key=cred['key'], user=cred['url'])
    else:
        print("SKIP = {}".format(os.path.basename(outfile)))

"""
Return full constraints/mapping object
"""
def get_constraints(dataset_name, cds_forms_c3s_location, json_name):

    try:
        json_path = cds_forms_c3s_location.joinpath("cds-forms-c3s", dataset_name, json_name + ".json")
        with open(json_path, "r") as cj:
            json_data = json.load(cj)
    except:
        print('Could not open: {}'.json_path)

    return json_data

"""
Return filtered constraints/mapping object
"""
def get_filtered_constraints(dataset_name, cds_forms_c3s_location, json_name, var, origin, system):

    res = []
    try:
        json_path = cds_forms_c3s_location.joinpath("cds-forms-c3s", dataset_name, json_name + ".json")
        with open(json_path, "r") as cj:
            json_data = json.load(cj)

        # Filter out variable
        for c in json_data:
            if var in c['variable'] and origin in c['originating_centre'] and system in c['system']:
                c['variable'] = var
                res.append(c)

    except:
        print('Could not open: {}'.json_path)

    return res

"""
Get netcdf sample from the C3s converter
"""
def get_netcdf_sample(info, cred, prod, outfile, logdir):

    # Hard-coded of course
    info['format'] = 'netcdf'
    outfile = outfile.replace('.grib', '.nc')

    # List of product types (we need to pick one)
    if 'product_type' in info:
        if isinstance(info['product_type'], type([])):
            info['product_type'] = info['product_type'][0]
        else:
            info['product_type'] = info['product_type']

    call_download(cred, outfile, info, prod, logdir)

"""
Get row (month, year)
"""
def get_row_dates(row):

    years = row['year']
    months = row['month']
    if isinstance(years, str):
        years = [years]
    if isinstance(months, str):
        months = [months]

    return years, months