# -*- coding: utf-8 -*-
"""
Created on Tue Apr 20 13:32:20 2021

    #--- ag csv results to single file based on dashboard_dbs
    #--- <NAME> (<EMAIL>)
    #--- Jul, 2021.
    #--- Dev-log in: https://github.com/Murilodsv/py-jules

@author: muril
"""

# DEBUG import os; os.chdir('C:/Murilo/py-jules')

#------------------------------#
#--- generate qsub-clusters ---#
#------------------------------#

dash_nm     = 'dashboard_db.csv'        # Filename of Dashboard CSV
wd_out      = 'ginore/csv_res'
sim_id      = 'future-crop'
dap_f       = range(1,361)
var_nm      = 'fsmc'
var_ag      = 'pft'
f_sufix     = '.day.time_pft_y_x.csv'

#--- Get scripts arguments
if __name__ == "__main__":
    import sys
    if len(sys.argv) > 1:
        #--- use arguments
        dash_nm    = str(sys.argv[1]) # debug dash_nm = 'dashboard_db_future.csv'
        wd_out     = str(sys.argv[2])
        sim_id     = str(sys.argv[3])
        dap_f      = str(sys.argv[4])
        var_nm     = str(sys.argv[5])
        var_ag     = str(sys.argv[6])
        f_sufix    = str(sys.argv[7])
        
        import numpy as np
        dap_f      = '1,360'
        dap_f      = np.array(dap_f.split(',')) # arg_run_id = np.array(str('SC3572').split(','))
        if len(dap_f) == 1:
            dap_f = range(0,int(dap_f))
        else:
            dap_f = range(int(dap_f[0]), int(dap_f[1])+1)
        
#----------------------#
#--- Load libraries ---#
#----------------------#
import os
import os.path
import util         as u
from time import time

#--- Track progress
run_start = time()

#----------------------#
#--- Read dashboard ---#
#----------------------#

#--- get run wd
wd   = os.getcwd().replace('\\','/')

#--- Open CSVs
dash = u.df_csv(wd+'/'+dash_nm)

#--- list of clusters
l_ids = dash['run_id'].unique()

ini_df = True
for i in l_ids:

    #--- filename
    fn = wd+'/jules_run/'+i+'/namelists/output/'+i+f_sufix

    if os.path.exists(fn):

        print('Reading '+i)

        #--- read file
        df_i = u.df_csv(fn)

        #--- filter dap
        df_i = df_i[:][df_i['dap'].isin(range(1,360))]
        
        #--- aggregate the var_nm by mean and var_ag
        df_i = df_i.groupby(var_ag, as_index=False)[var_nm].mean()
        
        #--- flag with run_id
        df_i['run_id'] = i

        #--- bind to single df
        if ini_df:
            df_res = df_i
            ini_df = False
        else:
            df_res = df_res.append(df_i)

#--- write results
df_res.to_csv(wd+'/'+wd_out+'/'+sim_id+f_sufix, index = None, header=True)

#--- track time
print("\nElapsed time of copying: --- %.3f seconds ---" % (time() - run_start))




# -*- coding: utf-8 -*-
"""
Created on Tue Apr 20 13:32:20 2021

    #--- ag csv results to single file based on dashboard_dbs
    #--- <NAME> (<EMAIL>)
    #--- Jul, 2021.
    #--- Dev-log in: https://github.com/Murilodsv/py-jules

@author: muril
"""

# DEBUG import os; os.chdir('C:/Murilo/py-jules')

#------------------------------#
#--- generate qsub-clusters ---#
#------------------------------#

dash_nm     = 'dashboard_db.csv'        # Filename of Dashboard CSV
wd_out      = 'ginore/csv_res'
sim_id      = 'future-crop'
dap_f       = range(1,361)
var_nm      = 'fsmc'
var_ag      = 'pft'
f_sufix     = '.day.time_pft_y_x.csv'

#--- Get scripts arguments
if __name__ == "__main__":
    import sys
    if len(sys.argv) > 1:
        #--- use arguments
        dash_nm    = str(sys.argv[1]) # debug dash_nm = 'dashboard_db_future.csv'
        wd_out     = str(sys.argv[2])
        sim_id     = str(sys.argv[3])
        dap_f      = str(sys.argv[4])
        var_nm     = str(sys.argv[5])
        var_ag     = str(sys.argv[6])
        f_sufix    = str(sys.argv[7])
        
        import numpy as np
        dap_f      = '1,360'
        dap_f      = np.array(dap_f.split(',')) # arg_run_id = np.array(str('SC3572').split(','))
        if len(dap_f) == 1:
            dap_f = range(0,int(dap_f))
        else:
            dap_f = range(int(dap_f[0]), int(dap_f[1])+1)
        
#----------------------#
#--- Load libraries ---#
#----------------------#
import os
import os.path
import util         as u
from time import time

#--- Track progress
run_start = time()

#----------------------#
#--- Read dashboard ---#
#----------------------#

#--- get run wd
wd   = os.getcwd().replace('\\','/')

#--- Open CSVs
dash = u.df_csv(wd+'/'+dash_nm)

#--- list of clusters
l_ids = dash['run_id'].unique()

ini_df = True
for i in l_ids:

    #--- filename
    fn = wd+'/jules_run/'+i+'/namelists/output/'+i+f_sufix

    if os.path.exists(fn):

        print('Reading '+i)

        #--- read file
        df_i = u.df_csv(fn)

        #--- filter dap
        df_i = df_i[:][df_i['dap'].isin(range(1,360))]
        
        #--- aggregate the var_nm by mean and var_ag
        df_i = df_i.groupby(var_ag, as_index=False)[var_nm].mean()
        
        #--- flag with run_id
        df_i['run_id'] = i

        #--- bind to single df
        if ini_df:
            df_res = df_i
            ini_df = False
        else:
            df_res = df_res.append(df_i)

#--- write results
df_res.to_csv(wd+'/'+wd_out+'/'+sim_id+f_sufix, index = None, header=True)

#--- track time
print("\nElapsed time of copying: --- %.3f seconds ---" % (time() - run_start))




# -*- coding: utf-8 -*- Created on Tue Apr 20 13:32:20 2021      #--- ag csv results to single file based on dashboard_dbs     #--- <NAME> (<EMAIL>)     #--- Jul, 2021.     #--- Dev-log in: https://github.com/Murilodsv/py-jules  @author: muril # DEBUG import os; os.chdir('C:/Murilo/py-jules') #------------------------------# #--- generate qsub-clusters ---# #------------------------------# # Filename of Dashboard CSV #--- Get scripts arguments #--- use arguments # debug dash_nm = 'dashboard_db_future.csv' # arg_run_id = np.array(str('SC3572').split(',')) #----------------------# #--- Load libraries ---# #----------------------# #--- Track progress #----------------------# #--- Read dashboard ---# #----------------------# #--- get run wd #--- Open CSVs #--- list of clusters #--- filename #--- read file #--- filter dap #--- aggregate the var_nm by mean and var_ag #--- flag with run_id #--- bind to single df #--- write results #--- track time