import glob
from netCDF4 import Dataset
import numpy as np
import copy

species_dirs = sorted(glob.glob('daily/*'))

network = 'EBAS'
year = 2017
GHOST_version = '1.3.2'
max_stations_to_print = 20
resolutions = ['hourly','daily','monthly']

selected_qa = np.array([0,1,2,3,6],dtype=np.uint32)

maxes = {}
medians = {}
mins = {}

for resolution in resolutions:

    species_dirs = sorted(glob.glob('/gpfs/projects/bsc32/AC_cache/obs/ghost/{}/{}/*'.format(network,GHOST_version,resolution)))

    for species_dir in species_dirs:

        species = species_dir.split('/')[-1]
        print(species)    

        if ('pm10' in species) & (species not in ['pm10','pm2p5','p1']):
            species_to_print = species.split('pm10')[-1]
        elif ('pm2p5' in species) & (species not in ['pm10','pm2p5','p1']):
            species_to_print = species.split('pm2p5')[-1]
        elif ('pm1' in species) & (species not in ['pm10','pm2p5','p1']):
            species_to_print = species.split('pm1')[-1]
        elif 'sconc' in species:
            species_to_print = species.split('sconc')[-1]
        else:
            species_to_print = copy.deepcopy(species)

        if species_to_print not in maxes:
            maxes[species_to_print] = []
            medians[species_to_print] = []
            mins[species_to_print] = []

        for month in ['01','02','03','04','05','06','07','08','09','10','11','12']:

            try:
                a = Dataset('{}/{}_{}{}.nc'.format(species_dir,species,year,month))
                filtered_data = a[species][:]
                filtered_data[np.isin(a['qa'][:,:,:],selected_qa).any(axis=2)] = np.NaN 
                maxes[species_to_print] = np.append(maxes[species_to_print], np.array(np.nanmax(filtered_data,axis=1), dtype=np.float32))
                medians[species_to_print] = np.append(medians[species_to_print], np.array(np.nanmedian(filtered_data,axis=1), dtype=np.float32))
                mins[species_to_print] = np.append(mins[species_to_print], np.array(np.nanmin(filtered_data,axis=1), dtype=np.float32))
            except:
                pass
 
for species in maxes.keys()
    if len(maxes[species]) > 0:
        print('MAX: ',-np.sort(-maxes[species])[:max_stations_to_print])
        print('MEDIAN: ',-np.sort(-medians[species])[:max_stations_to_print])
        print('MIN: ',np.sort(mins[species])[:max_stations_to_print])