'''
read global carbon from nemo sclar output
'''
import glob
import pandas as pd
import copy
import numpy as np
import matplotlib.pyplot as plt
import netCDF4 as nc
import my_functions

save_to_csv = True
#expset = 'SynTraHalfOAE'
expset = 'SynTraHalfBOTH'
#expset = 'SynTraHalfAR'
out_dir = []
##out_dir.append('/scratch/usr/shktkeme/models/foci2.0/experiments/FOCI2.0-TK012_spinup_ESM_250y/outdata/nemo/')
#out_dir.append ('/scratch/usr/shkchien/models/foci1.20.0/experiments/FOCI1.20.0-CC104_RCP_ESM_spinup2089/outdata/nemo/ym/')
#out_dir.append ('/scratch/usr/shkchien/models/foci1.20.0/experiments/FOCI1.20.0-CC105_RCP_ESM_spinup2099/outdata/nemo/ym/')
#out_dir.append ('/scratch/usr/shkchien/models/foci1.20.0/experiments/FOCI1.20.0-CC106_RCP_ESM_spinup2079/outdata/nemo/ym/')
##out_dir.append ('/scratch/usr/shkhwwey/models/foci2.0/experiments/FOCI20.0-HW002_esm_ssp585_ocn_alk/outdata/nemo/ym/')
##out_dir.append ('/scratch/usr/shkhwwey/models/foci2.0/experiments/FOCI20.0-HW005_esm_ssp585_ocn_alk/outdata/nemo/ym/')
##out_dir.append ('/scratch/usr/shkhwwey/models/foci2.0/experiments/FOCI20.0-HW006_esm_ssp585_ocn_alk/outdata/nemo/ym/')
#out_dir.append ('/scratch/usr/shkhwwey/models/foci2.0/experiments/FOCI20.0-HW003_esm_ssp585_ssp126Lu/outdata/nemo/ym/')
#out_dir.append ('/scratch/usr/shkhwwey/models/foci2.0/experiments/FOCI20.0-HW007_esm_ssp585_ssp126Lu/outdata/nemo/ym/')
#out_dir.append ('/scratch/usr/shkhwwey/models/foci2.0/experiments/FOCI20.0-HW008_esm_ssp585_ssp126Lu/outdata/nemo/ym/')
#out_dir.append ('/scratch/usr/shkhwwey/models/foci2.0/experiments/FOCI20.0-HW004_esm_ssp585_ocn_alk_stop/outdata/nemo/ym/')
#out_dir.append ('/scratch/usr/shkhwwey/models/foci2.0/experiments/FOCI20.0-HW009_esm_ssp585_ocn_alk_stop/outdata/nemo/ym/')
#out_dir.append ('/scratch/usr/shkhwwey/models/foci2.0/experiments/FOCI20.0-HW010_esm_ssp585_ocn_alk_stop/outdata/nemo/ym/')
#expname = ['TK200']
#out_dir.append ('/scratch/usr/shktkeme/models/foci2.0-foci-mops/experiments/FOCI2.0-TK200_FMESM_SSP370_alkEU_2014CC105/outdata/nemo/ym/')
#expname = ['NM001']
#out_dir.append ('/scratch/usr/shknmehe/Models/FOCI2.0_FOCI_MOPS/experiments/FOCI2.0-NM001_FM_ESM_SSP370os_2014CC105/outdata/nemo/ym/')
#expname = ['NM004']
#out_dir.append ('/scratch/usr/shknmehe/Models/FOCI2.0_FOCI_MOPS/experiments/FOCI2.0-NM004_FM_ESM_SSP126os_2014CC105/outdata/nemo/ym/')
#expname = ['HW013','HW012','HW011']
#out_dir.append('/scratch/usr/shkhwwey/models/foci2.0/experiments/FOCI20.0-HW013_esm_ssp370_constLu/outdata/nemo/ym/')
#out_dir.append('/scratch/usr/shkhwwey/models/foci2.0/experiments/FOCI20.0-HW012_esm_ssp370_constLu/outdata/nemo/ym/')
#out_dir.append('/scratch/usr/shkhwwey/models/foci2.0/experiments/FOCI20.0-HW011_esm_ssp370_constLu/outdata/nemo/ym/')
#expname = ['HW026']
#out_dir.append('/scratch/usr/shkhwwey/models/foci2.0_maps_wh/experiments/FOCI20.0-HW026_esm_ssp370_OAE/outdata/nemo/ym/')
#expname = ['HW022','HW023','HW024']
#out_dir.append('/scratch/usr/shkhwwey/models/foci2.0_maps_wh/experiments/FOCI20.0-HW022_esm_ssp370_AR/outdata/nemo/ym/')
#out_dir.append('/scratch/usr/shkhwwey/models/foci2.0_maps_wh/experiments/FOCI20.0-HW023_esm_ssp370_AR/outdata/nemo/ym/')
#out_dir.append('/scratch/usr/shkhwwey/models/foci2.0_maps_wh/experiments/FOCI20.0-HW024_esm_ssp370_AR/outdata/nemo/ym/')
#expname = ['HW027']
#out_dir.append('/scratch/usr/shkhwwey/models/foci2.0_maps_wh/experiments/FOCI20.0-HW027_esm_ssp370_OAE/outdata/nemo/ym/')
#expname = ['HW028']
#out_dir.append('/scratch/usr/shkhwwey/models/foci2.0_maps_wh/experiments/FOCI20.0-HW028_esm_ssp370_OAE/outdata/nemo/ym/')
#expname = ['HW030']
#out_dir.append ('/scratch/usr/shkhwwey/models/foci2.0_maps_wh/experiments/FOCI20.0-HW030_esm_ssp370_AR_and_OAE/outdata/nemo/ym/')
#expname = ['HW029','HW031']
#out_dir.append ('/scratch/usr/shkhwwey/models/foci2.0_maps_wh/experiments/FOCI20.0-HW029_esm_ssp370_AR_and_OAE/outdata/nemo/ym/')
#out_dir.append ('/scratch/usr/shkhwwey/models/foci2.0_maps_wh/experiments/FOCI20.0-HW031_esm_ssp370_AR_and_OAE/outdata/nemo/ym/')
#expname = ['HW032','HW033']
#out_dir.append ('/scratch/usr/shkhwwey/models/foci2.0_maps_wh/experiments/FOCI20.0-HW032_esm_ssp370_OAE/outdata/nemo/ym/')
#out_dir.append ('/scratch/usr/shkhwwey/models/foci2.0_maps_wh/experiments/FOCI20.0-HW033_esm_ssp370_OAE/outdata/nemo/ym/')
if expset=='SynTraHalfOAE':
    expname = ['HW034','HW035','HW036']
    out_dir.append('/scratch/usr/shkhwwey/models/foci2.0_maps_wh/experiments/FOCI20.0-HW034_esm_ssp370_half_OAE/outdata/nemo/ym/')
    out_dir.append('/scratch/usr/shkhwwey/models/foci2.0_maps_wh/experiments/FOCI20.0-HW035_esm_ssp370_half_OAE/outdata/nemo/ym/')
    out_dir.append('/scratch/usr/shkhwwey/models/foci2.0_maps_wh/experiments/FOCI20.0-HW036_esm_ssp370_half_OAE/outdata/nemo/ym/')
if expset=='SynTraHalfBOTH':
    expname = ['HW037','HW038']
    #expname = ['HW039']
    out_dir.append('/scratch/usr/shkhwwey/models/foci2.0_maps_wh/experiments/FOCI20.0-HW037_esm_ssp370_half_AR_and_OAE/outdata/nemo/ym/')
    out_dir.append('/scratch/usr/shkhwwey/models/foci2.0_maps_wh/experiments/FOCI20.0-HW038_esm_ssp370_half_AR_and_OAE/outdata/nemo/ym/')
    #out_dir.append('/scratch/usr/shkhwwey/models/foci2.0_maps_wh/experiments/FOCI20.0-HW039_esm_ssp370_half_AR_and_OAE/outdata/nemo/ym/')
if expset=='SynTraHalfAR':
    expname = ['HW040','HW041','HW042']
    out_dir.append ('/scratch/usr/shkhwwey/models/foci2.0_maps_wh/experiments/FOCI20.0-HW040_esm_ssp370_half_AR/outdata/nemo/ym/')
    out_dir.append ('/scratch/usr/shkhwwey/models/foci2.0_maps_wh/experiments/FOCI20.0-HW041_esm_ssp370_half_AR/outdata/nemo/ym/')
    out_dir.append ('/scratch/usr/shkhwwey/models/foci2.0_maps_wh/experiments/FOCI20.0-HW042_esm_ssp370_half_AR/outdata/nemo/ym/')

last_year = []
var = []
for iexp in range(len(out_dir)):
    last_year.append(0)
    var.append([])
for yr  in range (2015,2100):
#for yr  in range (2015,2051):
#for yr  in range (2110,2111):
    print('{}'.format(yr))
    for iexp in range(len(out_dir)):
        #if expset=='SynTraHalfBOTH' and iexp==0 and yr==2067:
        #    var[iexp].append(-999)
        #    continue
        file_name = glob.glob(out_dir[iexp] + '*_1y_*' + str(yr) + '1231_scalar.nc')
        cOcean = np.ma.average(nc.Dataset(file_name[0]).variables['sumCnoPO4_PgC'][:,0,0],axis=0) # unit: PgC
        sumCO2flx = nc.Dataset(file_name[0]).variables['sumCO2flx'][:,0,0] # unit: kgCO2/sec
        #for imonth in range(len(sumCO2flx)):
        #    print(f'not-cpl[{imonth}]: {sumCO2flx[imonth]:.3f}')
        sumCO2flx = sumCO2flx[0] / 10**12 * 86400 * 365.25 * 12.01 / 44.0095 * -1 
        #sumCO2flx = my_functions.monthly_to_annual(sumCO2flx,yr) / 10**12 * 86400 * 12.01 / 44.0095 *-1# PgC/yr
        #####
        sumCO2flx_cpl = nc.Dataset(file_name[0]).variables['sumCO2flx_cpl'][:,0,0] #unit: kgCO2/sec
        #for imonth in range(len(sumCO2flx_cpl)):
        #    print(f'cpl[{imonth}]: {sumCO2flx_cpl[imonth]:.3f}')
        #sumCO2flx_cpl = my_functions.monthly_to_annual(sumCO2flx_cpl,yr) / 10**12 * 86400 * 12.01 / 44.0095 * -1# PgC/yr
        sumCO2flx_cpl = sumCO2flx_cpl[0] / 10**12 * 86400 * 365.25 * 12.01 / 44.0095 *-1 
        print(f'{expname[iexp]} {cOcean:.3f} Pg, diff= {cOcean-last_year[iexp]:.3f} Pg, {sumCO2flx:.3f} PgC/yr, {sumCO2flx_cpl:.3f} PgC/yr')
        #print(f'{expname[iexp]} sumCO2flx: {sumCO2flx:.3f}, sumCO2flx_cpl: {sumCO2flx_cpl:.3f}, diff: {sumCO2flx-sumCO2flx_cpl:.3f} (PgC/yr)')
        last_year[iexp] = cOcean
        var[iexp].append(sumCO2flx_cpl)
var = np.array(var)
print(var.shape)
###################
# save to csv
if save_to_csv:
    df=[]
    for iexp in range(len(out_dir)):
        df.append(0)
        #df[iexp] = pd.read_csv(expname[iexp] + '.csv',index_col='year')
        #df[iexp]['fgco2'] = var[iexp]
        #df[iexp] = pd.DataFrame(data=var[iexp],index=range(2070,2100),columns=['fgco2'])
        df[iexp] = pd.DataFrame(data=var[iexp],index=range(2015,2100),columns=['fgco2'])
        df[iexp].index.name = 'year'
        df[iexp].to_csv('temp_csv/' + expname[iexp] + '_fgco2.csv')
    print('save sumCO2flx_cpl to csv')

