#!/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 glob
import os

'''
Script in order to rename pdf or html to the name conventions of C3S
'''

def rename_c3s_files(res_folder='/shared/QAR_dqc/results', extension='*.pdf'):

    # Naming translation
    ds_names = {
        'reanalysis-era5-land-monthly-means': 'reanalysis_era5_land_monthly_means',
        'reanalysis-era5-single-levels-monthly-means': 'reanalysis_era5_single_levels_monthly_means',
        'reanalysis-era5-pressure-levels-monthly-means': 'reanalysis_era5_pressure_levels_monthly_means',
        'reanalysis-uerra-europe-pressure-levels': 'reanalysis_uerra_europe_pressure_levels',
        'reanalysis-uerra-europe-single-levels': 'reanalysis_uerra_europe_single_levels',
        'seasonal-monthly-single-levels': 'seasonal_monthly_single_levels',
        'seasonal-original-single-levels': 'seasonal_original_single_levels',
        'seasonal-monthly-pressure-levels': 'seasonal_monthly_pressure_levels',
        'seasonal-original-pressure-levels': 'seasonal_original_pressure_levels',
        'seasonal-postprocessed-pressure-levels': 'seasonal_postprocessed_pressure_levels',
        'seasonal-postprocessed-single-levels': 'seasonal_postprocessed_single_levels',
        'reanalysis-era5-single-levels': 'reanalysis_era5_single_levels',
        'reanalysis-era5-pressure-levels': 'reanalysis_era5_pressure_levels',
        'reanalysis-era5-land': 'reanalysis_era5_land'
    }

    for res_file in sorted(glob.glob(os.path.join(res_folder, extension))):

        print(res_file)
        out_file = None

        for k,v in ds_names.items():
            if k in res_file:
                out_file = res_file.replace(k,v)

        print(out_file)
        if not(out_file is None):
            os.system('mv {} {}'.format(res_file, out_file))
        print('--------------------')