@ -26,8 +26,6 @@ os.chdir('C:/Users/z5025317/OneDrive - UNSW/WRL_Postdoc_Manual_Backup/WRL_Postdo
import climdata_fcts as fct
import climdata_fcts as fct
import silo as sil
import silo as sil
#==========================================================#
#==========================================================#
# Set working direcotry (where postprocessed NARClIM data is located)
# Set working direcotry (where postprocessed NARClIM data is located)
os . chdir ( ' C:/Users/z5025317/OneDrive - UNSW/WRL_Postdoc_Manual_Backup/WRL_Postdoc/Projects/Paper#1/ ' )
os . chdir ( ' C:/Users/z5025317/OneDrive - UNSW/WRL_Postdoc_Manual_Backup/WRL_Postdoc/Projects/Paper#1/ ' )
@ -36,275 +34,295 @@ os.chdir('C:/Users/z5025317/OneDrive - UNSW/WRL_Postdoc_Manual_Backup/WRL_Postdo
#==========================================================#
#==========================================================#
#set input parameters
#set input parameters
Base_period_start = ' 1990-01-01 '
Case_Study_Name = ' CASESTUDY2 '
Case_Study_Name = ' CASESTUDY2 '
Base_period_end = ' 2080-01-01 ' #use last day that's not included in period as < is used for subsetting
Estuary = ' HUNTER ' # 'Belongil'
Clim_var_type = " sstmean " # '*' will create pdf for all variables in folder "pracc*|tasmax*"
plot_pdf = ' yes '
delta_csv = ' yes '
Stats = ' maxdaily '
Version = ' V4 '
#==========================================================#
#==========================================================#
#set directory path for output files
output_directory = ' Output/ ' + Case_Study_Name + ' / ' + Estuary
#output_directory = 'J:/Project wrl2016032/NARCLIM_Raw_Data/Extracted'
if not os . path . exists ( output_directory ) :
os . makedirs ( output_directory )
print ( ' ------------------------------------------- ' )
print ( " output directory folder didn ' t exist and was generated " )
print ( ' ------------------------------------------- ' )
#==========================================================#
#==========================================================#
Estuary_Folder = glob . glob ( ' ./Data/NARCLIM_Site_CSVs/ ' + Case_Study_Name + ' / ' + Estuary + ' * ' )
Clim_Var_CSVs = glob . glob ( Estuary_Folder [ 0 ] + ' / ' + Clim_var_type + ' * ' )
#==========================================================#
#==========================================================#
#read CSV files and start analysis
#==========================================================#
#for clim_var_csv_path in Clim_Var_CSVs:
clim_var_csv_path = Clim_Var_CSVs [ 0 ]
Filename = os . path . basename ( os . path . normpath ( clim_var_csv_path ) )
Clim_var_type = Filename . split ( ' _ ' , 1 ) [ 0 ]
print ( clim_var_csv_path )
Full_df = pd . read_csv ( clim_var_csv_path , index_col = 0 , parse_dates = True )
#pandas datestamp index to period (we don't need the 12 pm info in the index (daily periods are enough))
Full_df . index = Full_df . index . to_period ( ' D ' )
Full_df = Full_df . drop ( columns = [ ' period ' ] )
Ncols_df = len ( Full_df )
#check data types of columns
#Full_df.dtypes
#==========================================================#
#substract a constant from all values to convert from kelvin to celcius (temp)
if Clim_var_type == ' tasmean ' or Clim_var_type == ' tasmax ' or Clim_var_type == ' sstmean ' :
Full_df = Full_df . iloc [ : , 0 : ( Ncols_df - 1 ) ] - 273.15
if Clim_var_type == ' evspsblmean ' or Clim_var_type == ' potevpmean ' :
Full_df = Full_df . iloc [ : , 0 : ( Ncols_df - 1 ) ] * 60 * 60 * 24
Fdf_1900_2080 = Full_df
#==========================================================#
#Subset the data to the minimum base period and above (used to set the lenght of the present day climate period)
#Fdf_1900_2080 = Full_df.loc[(Full_df.index >= Base_period_start) & (Full_df.index < Base_period_end)] # not necessary if not using reanalysis models for base period
#==========================================================#
#Aggregate daily df to annual time series
if ( Clim_var_type == ' pracc ' or Clim_var_type == ' evspsblmean ' or Clim_var_type == ' potevpmean '
or Clim_var_type == ' pr1Hmaxtstep ' or Clim_var_type == ' wss1Hmaxtstep ' ) :
if ( Stats == ' maxdaily ' ) :
Fdf_1900_2080_annual = Fdf_1900_2080 . resample ( ' A ' ) . max ( )
Fdf_1900_2080_annual = Fdf_1900_2080_annual . replace ( 0 , np . nan )
Fdf_Seas_means = Fdf_1900_2080 . resample ( ' Q-NOV ' ) . max ( ) #seasonal means
Fdf_Seas_means = Fdf_Seas_means . replace ( 0 , np . nan )
else :
Fdf_1900_2080_annual = Fdf_1900_2080 . resample ( ' A ' ) . sum ( )
Fdf_1900_2080_annual = Fdf_1900_2080_annual . replace ( 0 , np . nan )
Fdf_Seas_means = Fdf_1900_2080 . resample ( ' Q-NOV ' ) . sum ( ) #seasonal means
Fdf_Seas_means = Fdf_Seas_means . replace ( 0 , np . nan )
else :
if ( Stats == ' maxdaily ' ) :
Fdf_1900_2080_annual = Fdf_1900_2080 . resample ( ' A ' ) . max ( )
Fdf_1900_2080_annual = Fdf_1900_2080_annual . replace ( 0 , np . nan )
Fdf_Seas_means = Fdf_1900_2080 . resample ( ' Q-NOV ' ) . max ( ) #seasonal means
Fdf_Seas_means = Fdf_Seas_means . replace ( 0 , np . nan )
else :
Fdf_1900_2080_annual = Fdf_1900_2080 . resample ( ' A ' ) . mean ( )
Fdf_Seas_means = Fdf_1900_2080 . resample ( ' Q-NOV ' ) . mean ( ) #seasonal means
Fdf_1900_2080_means = Fdf_1900_2080 . mean ( )
#==========================================================#
Estuaries = [ ' HUNTER ' , ' RICHMOND ' , ' NADGEE ' , ' SHOALHAVEN ' , ' GEORGES ' , ' CATHIE ' ]
Estuaries = [ ' HUNTER ' ]
for Est in Estuaries :
#==========================================================#
#Estuary = 'HUNTER' # 'Belongil'
#Select the 3 most representative models (min med and max difference betwen far future and present)
Estuary = Est # 'Belongil'
dfall , dfmin , dfmax , dfmed , Min_dif_mod_name , Med_dif_mod_name , Max_dif_mod_name = fct . select_min_med_max_dif_model ( Fdf_1900_2080 )
print Estuary
#==========================================================#
#Clim_var_type = 'potevpmean' # '*' will create pdf for all variables in folder "pracc*|tasmax*"
Clim_var_types = [ ' tasmax ' ]
#==========================================================#
for climvar in Clim_var_types :
#create a dataframe that has 1 column for each of the three representative models
Clim_var_type = climvar
dfa = Fdf_1900_2080_annual . iloc [ : , [ 0 ] ]
plot_pdf = ' no '
dfa1 = Fdf_1900_2080_annual . iloc [ : , [ 0 , 3 , 6 ] ] . loc [ ( Fdf_1900_2080_annual . index > = ' 1990 ' ) & ( Fdf_1900_2080_annual . index < = ' 2009 ' ) ]
delta_csv = ' yes '
dfa1 . columns = [ Min_dif_mod_name [ : - 5 ] , Med_dif_mod_name [ : - 5 ] , Max_dif_mod_name [ : - 5 ] ]
Stats = ' days_h_35 ' # 'maxdaily', 'mean'
dfa2 = Fdf_1900_2080_annual . iloc [ : , [ 1 , 4 , 7 ] ] . loc [ ( Fdf_1900_2080_annual . index > = ' 2020 ' ) & ( Fdf_1900_2080_annual . index < = ' 2039 ' ) ]
Version = ' V1 '
dfa2 . columns = [ Min_dif_mod_name [ : - 5 ] , Med_dif_mod_name [ : - 5 ] , Max_dif_mod_name [ : - 5 ] ]
#==========================================================#
dfa3 = Fdf_1900_2080_annual . iloc [ : , [ 2 , 5 , 8 ] ] . loc [ ( Fdf_1900_2080_annual . index > = ' 2060 ' ) & ( Fdf_1900_2080_annual . index < = ' 2079 ' ) ]
dfa3 . columns = [ Min_dif_mod_name [ : - 5 ] , Med_dif_mod_name [ : - 5 ] , Max_dif_mod_name [ : - 5 ] ]
dfall_annual = dfa1 . append ( dfa2 ) . append ( dfa3 )
#==========================================================#
#==========================================================#
#set directory path for output files
output_directory = ' Output/ ' + Case_Study_Name + ' / ' + Estuary
#==========================================================#
#output_directory = 'J:/Project wrl2016032/NARCLIM_Raw_Data/Extracted'
#Create Deltas of average change for annual and seasonal basis
if not os . path . exists ( output_directory ) :
#==========================================================#
os . makedirs ( output_directory )
delta_all_df = fct . calculate_deltas_NF_FF2 ( Fdf_1900_2080_annual , Fdf_Seas_means )
print ( ' ------------------------------------------- ' )
print ( " output directory folder didn ' t exist and was generated " )
print ( ' ------------------------------------------- ' )
#==========================================================#
#==========================================================#
if delta_csv == ' yes ' :
out_file_name = Estuary + ' _ ' + Clim_var_type + ' _ ' + Stats + ' _NARCliM_ensemble_changes.csv '
out_path = output_directory + ' / ' + out_file_name
#==========================================================#
delta_all_df . to_csv ( out_path )
Estuary_Folder = glob . glob ( ' ./Data/NARCLIM_Site_CSVs/ ' + Case_Study_Name + ' / ' + Estuary + ' * ' )
#==========================================================#
Clim_Var_CSVs = glob . glob ( Estuary_Folder [ 0 ] + ' / ' + Clim_var_type + ' * ' )
#==========================================================#
#==========================================================#
#create a dataframe that has a single column for present day, near and far future for the (3 selected models)
Full_current_df = Fdf_1900_2080 . iloc [ : , range ( 0 , 3 ) ]
#==========================================================#
Full_current_df = Full_current_df . stack ( )
#read CSV files and start analysis
#nearfuture
#==========================================================#
Full_nearfuture_df = Fdf_1900_2080 . iloc [ : , range ( 3 , 6 ) ]
#for clim_var_csv_path in Clim_Var_CSVs:
Full_nearfuture_df = Full_nearfuture_df . stack ( )
clim_var_csv_path = Clim_Var_CSVs [ 0 ]
#farfuture
Filename = os . path . basename ( os . path . normpath ( clim_var_csv_path ) )
Full_farfuture_df = Fdf_1900_2080 . iloc [ : , range ( 6 , len ( Fdf_1900_2080 . columns ) ) ]
Clim_var_type = Filename . split ( ' _ ' , 1 ) [ 0 ]
Full_farfuture_df = Full_farfuture_df . stack ( )
print ( clim_var_csv_path )
Summarized_df = pd . concat ( [ Full_current_df , Full_nearfuture_df ] , axis = 1 , ignore_index = True )
Full_df = pd . read_csv ( clim_var_csv_path , index_col = 0 , parse_dates = True )
Summarized_df = pd . concat ( [ Summarized_df , Full_farfuture_df ] , axis = 1 , ignore_index = True )
#pandas datestamp index to period (we don't need the 12 pm info in the index (daily periods are enough))
Summarized_df . columns = [ ' present ' , ' near ' , ' far ' ]
Full_df . index = Full_df . index . to_period ( ' D ' )
#==========================================================#
Full_df = Full_df . drop ( columns = [ ' period ' ] )
Ncols_df = len ( Full_df )
#check data types of columns
#Full_df.dtypes
#==========================================================#
#==========================================================#
#output some summary plot into pdf
#==========================================================#
if plot_pdf == ' yes ' :
#==========================================================#
plotcolours36 = [ ' darkolivegreen ' , ' turquoise ' , ' lightgreen ' , ' darkgreen ' , ' lightpink ' , ' slateblue ' , ' slategray ' , ' orange ' , ' tomato ' , ' peru ' , ' navy ' , ' teal ' ,
#substract a constant from all values to convert from kelvin to celcius (temp)
' darkolivegreen ' , ' turquoise ' , ' lightgreen ' , ' darkgreen ' , ' lightpink ' , ' slateblue ' , ' slategray ' , ' orange ' , ' tomato ' , ' peru ' , ' navy ' , ' teal ' ,
if Clim_var_type in [ ' tasmean ' , ' tasmax ' , ' sstmean ' ] :
' darkolivegreen ' , ' turquoise ' , ' lightgreen ' , ' darkgreen ' , ' lightpink ' , ' slateblue ' , ' slategray ' , ' orange ' , ' tomato ' , ' peru ' , ' navy ' , ' teal ' ]
Full_df = Full_df . iloc [ : , 0 : ( Ncols_df - 1 ) ] - 273.15
plotcolours36b = [ ' tomato ' , ' royalblue ' , ' mediumpurple ' , ' tomato ' , ' royalblue ' , ' mediumpurple ' , ' tomato ' , ' royalblue ' , ' mediumpurple ' , ' tomato ' , ' royalblue ' , ' mediumpurple ' ,
if Clim_var_type == ' evspsblmean ' or Clim_var_type == ' potevpmean ' :
' tomato ' , ' royalblue ' , ' mediumpurple ' , ' tomato ' , ' royalblue ' , ' mediumpurple ' , ' tomato ' , ' royalblue ' , ' mediumpurple ' , ' tomato ' , ' royalblue ' , ' mediumpurple ' ,
Full_df = Full_df . iloc [ : , 0 : ( Ncols_df - 1 ) ] * 60 * 60 * 24
' tomato ' , ' royalblue ' , ' mediumpurple ' , ' tomato ' , ' royalblue ' , ' mediumpurple ' , ' tomato ' , ' royalblue ' , ' mediumpurple ' , ' tomato ' , ' royalblue ' , ' mediumpurple ' ]
Fdf_1900_2080 = Full_df
plotcolours12 = [ ' darkolivegreen ' , ' turquoise ' , ' lightgreen ' , ' darkgreen ' , ' lightpink ' , ' slateblue ' , ' slategray ' , ' orange ' , ' tomato ' , ' peru ' , ' navy ' , ' teal ' ]
if Clim_var_type in [ ' rsdsmean ' , ' rldsmean ' ] :
plotcolours15 = [ ' darkolivegreen ' , ' turquoise ' , ' lightgreen ' , ' darkgreen ' , ' lightpink ' , ' slateblue ' , ' slategray ' , ' orange ' , ' tomato ' , ' peru ' , ' navy ' , ' teal ' , ' lightgreen ' , ' lightpink ' , ' slateblue ' ]
Full_df = Full_df . iloc [ : , 0 : ( Ncols_df - 1 ) ] * 60 * 60 * 24 / 1000000
#plt.cm.Paired(np.arange(len(Fdf_1900_2080_means)))
#==========================================================#
#write the key plots to a single pdf document
pdf_out_file_name = Clim_var_type + ' _ ' + Stats + ' _start_ ' + Base_period_start + ' _NARCliM_summary_ ' + Version + ' .pdf '
pdf_out_path = output_directory + ' / ' + pdf_out_file_name
#==========================================================#
#open pdf and add the plots
#Aggregate daily df to annual time series
with PdfPages ( pdf_out_path ) as pdf :
if Clim_var_type in [ ' pracc ' , ' evspsblmean ' , ' potevpmean ' , ' pr1Hmaxtstep ' ,
#barplot of model means
' wss1Hmaxtstep ' , ' rsdsmean ' , ' rldsmean ' ] :
plt . title ( Clim_var_type + ' - model means - full period ' )
if ( Stats == ' maxdaily ' ) :
ymin = min ( Fdf_1900_2080_means )
Fdf_1900_2080_annual = Fdf_1900_2080 . resample ( ' A ' ) . max ( )
ymax = max ( Fdf_1900_2080_means ) + 0.008 * min ( Fdf_1900_2080_means )
Fdf_1900_2080_annual = Fdf_1900_2080_annual . replace ( 0 , np . nan )
Fdf_1900_2080_means . plot ( kind = ' bar ' , ylim = ( ymin , ymax ) , color = plotcolours36 )
Fdf_Seas_means = Fdf_1900_2080 . resample ( ' Q-NOV ' ) . max ( ) #seasonal means
pdf . savefig ( bbox_inches = ' tight ' , pad_inches = 0.4 )
Fdf_Seas_means = Fdf_Seas_means . replace ( 0 , np . nan )
plt . close ( )
else :
#
Fdf_1900_2080_annual = Fdf_1900_2080 . resample ( ' A ' ) . sum ( )
neardeltadf = delta_all_df [ ' near ' ]
Fdf_1900_2080_annual = Fdf_1900_2080_annual . replace ( 0 , np . nan )
ymin = min ( neardeltadf ) + 0.1 * min ( neardeltadf )
Fdf_Seas_means = Fdf_1900_2080 . resample ( ' Q-NOV ' ) . sum ( ) #seasonal means
ymax = max ( neardeltadf ) + 0.1 * max ( neardeltadf )
Fdf_Seas_means = Fdf_Seas_means . replace ( 0 , np . nan )
neardeltadf = delta_all_df [ ' far ' ]
else :
ymin2 = min ( neardeltadf ) + 0.1 * min ( neardeltadf )
if ( Stats == ' maxdaily ' ) :
ymax2 = max ( neardeltadf ) + 0.1 * max ( neardeltadf )
Fdf_1900_2080_annual = Fdf_1900_2080 . resample ( ' A ' ) . max ( )
ymin = min ( ymin , ymin2 )
Fdf_1900_2080_annual = Fdf_1900_2080_annual . replace ( 0 , np . nan )
if ( Clim_var_type == ' tasmax ' or Clim_var_type == ' tasmean ' ) :
Fdf_Seas_means = Fdf_1900_2080 . resample ( ' Q-NOV ' ) . max ( ) #seasonal means
ymin = 0
Fdf_Seas_means = Fdf_Seas_means . replace ( 0 , np . nan )
ymax = max ( ymax , ymax2 )
if ( Stats [ : 4 ] == ' days ' ) :
#
Threshold = int ( Stats [ - 2 : ] )
# delta barplot for report 1#################################
#agg = ('abobe_27_count', lambda x: x.gt(27).sum()), ('average', 'mean')
ax = plt . subplot ( 2 , 1 , 1 )
agg = ( ' > ' + str ( Threshold ) + ' _count ' , lambda x : x . gt ( Threshold ) . sum ( ) ) ,
plt . title ( Clim_var_type + ' - model deltas - near-present ' )
Fdf_1900_2080_annual = Fdf_1900_2080 . resample ( ' A ' ) . agg ( agg )
neardeltadf = delta_all_df [ ' near ' ]
#Fdf_1900_2080_annual = Fdf_1900_2080_annual.replace(0, np.nan)
neardeltadf . plot ( kind = ' bar ' , color = plotcolours15 , ylim = ( ymin , ymax ) , ax = ax )
Fdf_Seas_means = Fdf_1900_2080 . resample ( ' Q-NOV ' ) . agg ( agg ) #seasonal means
plt . xticks ( [ ] )
#Fdf_Seas_means = Fdf_Seas_means.replace(0, np.nan)
#ax.xaxis.set_ticklabels([])
else :
#pdf.savefig(bbox_inches='tight', ylim=(ymin,ymax), pad_inches=0.4)
Fdf_1900_2080_annual = Fdf_1900_2080 . resample ( ' A ' ) . mean ( )
#plt.close()
Fdf_Seas_means = Fdf_1900_2080 . resample ( ' Q-NOV ' ) . mean ( ) #seasonal means
#
Fdf_1900_2080_means = Fdf_1900_2080 . mean ( )
ax = plt . subplot ( 2 , 1 , 2 )
#==========================================================#
plt . title ( Clim_var_type + ' - model deltas - far-present ' )
neardeltadf = delta_all_df [ ' far ' ]
neardeltadf . plot ( kind = ' bar ' , color = plotcolours15 , ylim = ( ymin , ymax ) , ax = ax )
#==========================================================#
ax . xaxis . grid ( False )
#Select the 3 most representative models (min med and max difference betwen far future and present)
#fig.patch.set_alpha(0)
dfall , dfmin , dfmax , dfmed , Min_dif_mod_name , Med_dif_mod_name , Max_dif_mod_name = fct . select_min_med_max_dif_model ( Fdf_1900_2080 )
#plt.show()
#==========================================================#
pdf . savefig ( bbox_inches = ' tight ' , ylim = ( ymin , ymax ) , pad_inches = 0.4 )
plt . close ( )
#==========================================================#
# end delta barplot for report 1#################################
#create a dataframe that has 1 column for each of the three representative models
#
dfa = Fdf_1900_2080_annual . iloc [ : , [ 0 ] ]
#full period density comparison
dfa1 = Fdf_1900_2080_annual . iloc [ : , [ 0 , 3 , 6 ] ] . loc [ ( Fdf_1900_2080_annual . index > = ' 1990 ' ) & ( Fdf_1900_2080_annual . index < = ' 2009 ' ) ]
plt . title ( Clim_var_type + ' - density comparison - full period - all models ' )
dfa1 . columns = [ Min_dif_mod_name [ : - 5 ] , Med_dif_mod_name [ : - 5 ] , Max_dif_mod_name [ : - 5 ] ]
Summarized_df . plot . kde ( )
dfa2 = Fdf_1900_2080_annual . iloc [ : , [ 1 , 4 , 7 ] ] . loc [ ( Fdf_1900_2080_annual . index > = ' 2020 ' ) & ( Fdf_1900_2080_annual . index < = ' 2039 ' ) ]
pdf . savefig ( bbox_inches = ' tight ' , pad_inches = 0.4 )
dfa2 . columns = [ Min_dif_mod_name [ : - 5 ] , Med_dif_mod_name [ : - 5 ] , Max_dif_mod_name [ : - 5 ] ]
plt . close ( )
dfa3 = Fdf_1900_2080_annual . iloc [ : , [ 2 , 5 , 8 ] ] . loc [ ( Fdf_1900_2080_annual . index > = ' 2060 ' ) & ( Fdf_1900_2080_annual . index < = ' 2079 ' ) ]
#full period density comparison
dfa3 . columns = [ Min_dif_mod_name [ : - 5 ] , Med_dif_mod_name [ : - 5 ] , Max_dif_mod_name [ : - 5 ] ]
plt . title ( Clim_var_type + ' - density comparison - full period - max delta model ' )
dfall_annual = dfa1 . append ( dfa2 ) . append ( dfa3 )
xmin = float ( max ( np . nanpercentile ( Fdf_1900_2080 . filter ( regex = Max_dif_mod_name [ : - 5 ] ) , 50 ) - 4 * np . std ( Fdf_1900_2080 . filter ( regex = Max_dif_mod_name [ : - 5 ] ) ) ) )
#==========================================================#
xmax = float ( max ( np . nanpercentile ( Fdf_1900_2080 . filter ( regex = Max_dif_mod_name [ : - 5 ] ) , 50 ) + 4 * np . std ( Fdf_1900_2080 . filter ( regex = Max_dif_mod_name [ : - 5 ] ) ) ) )
Fdf_1900_2080 . filter ( regex = Max_dif_mod_name [ : - 5 ] ) . plot . kde ( xlim = ( xmin , xmax ) )
#==========================================================#
pdf . savefig ( bbox_inches = ' tight ' , pad_inches = 0.4 )
#Create Deltas of average change for annual and seasonal basis
plt . close ( )
#==========================================================#
#annual box
delta_all_df = fct . calculate_deltas_NF_FF2 ( Fdf_1900_2080_annual , Fdf_Seas_means , Stats )
plt . title ( Clim_var_type + ' - Annual means/sums for max diff model ' )
Fdf_1900_2080_annual . boxplot ( rot = 90 )
pdf . savefig ( bbox_inches = ' tight ' , pad_inches = 0.4 )
#==========================================================#
plt . close ( )
if delta_csv == ' yes ' :
#
out_file_name = Estuary + ' _ ' + Clim_var_type + ' _ ' + Stats + ' _NARCliM_ensemble_changes.csv '
#daily box
out_path = output_directory + ' / ' + out_file_name
plt . title ( Clim_var_type + ' - Daily means/sums ' )
delta_all_df . to_csv ( out_path )
Fdf_1900_2080 . boxplot ( rot = 90 )
#==========================================================#
pdf . savefig ( bbox_inches = ' tight ' , pad_inches = 0.4 )
plt . close ( )
#==========================================================#
# time series plot annual ALL models
#create a dataframe that has a single column for present day, near and far future for the (3 selected models)
plt . title ( Clim_var_type + ' - Time series - all models ' )
Full_current_df = Fdf_1900_2080 . iloc [ : , range ( 0 , 3 ) ]
Mod_order = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 19 , 20 , 21 , 16 , 17 , 18 , 22 , 23 , 24 , 31 , 32 , 33 , 25 , 26 , 27 , 28 , 29 , 30 , 34 , 35 , 36 ]
Full_current_df = Full_current_df . stack ( )
test = Fdf_1900_2080_annual
#nearfuture
Mod_Names = test . columns
Full_nearfuture_df = Fdf_1900_2080 . iloc [ : , range ( 3 , 6 ) ]
New_Mod_Name = [ ]
Full_nearfuture_df = Full_nearfuture_df . stack ( )
for i in range ( 0 , len ( Mod_Names ) ) :
#farfuture
New_Mod_Name . append ( str ( Mod_order [ i ] + 10 ) + ' _ ' + Mod_Names [ i ] )
Full_farfuture_df = Fdf_1900_2080 . iloc [ : , range ( 6 , len ( Fdf_1900_2080 . columns ) ) ]
test . columns = New_Mod_Name
Full_farfuture_df = Full_farfuture_df . stack ( )
test_sorted = test . reindex_axis ( sorted ( test . columns ) , axis = 1 )
Summarized_df = pd . concat ( [ Full_current_df , Full_nearfuture_df ] , axis = 1 , ignore_index = True )
colnamest = test . columns
Summarized_df = pd . concat ( [ Summarized_df , Full_farfuture_df ] , axis = 1 , ignore_index = True )
test_sorted . columns = [ w [ 3 : - 5 ] for w in colnamest ]
Summarized_df . columns = [ ' present ' , ' near ' , ' far ' ]
test_sorted . plot ( legend = False , color = plotcolours36 )
#==========================================================#
pdf . savefig ( bbox_inches = ' tight ' , pad_inches = 0.4 )
plt . close ( )
# time series plot annual ALL models
plt . title ( Clim_var_type + ' - Time series - representative models ' )
#==========================================================#
dfall_annual . plot ( legend = False )
#output some summary plot into pdf
pdf . savefig ( bbox_inches = ' tight ' , pad_inches = 0.4 )
#==========================================================#
plt . close ( )
if plot_pdf == ' yes ' :
# seasonal mean boxplots
plotcolours36 = [ ' darkolivegreen ' , ' turquoise ' , ' lightgreen ' , ' darkgreen ' , ' lightpink ' , ' slateblue ' , ' slategray ' , ' orange ' , ' tomato ' , ' peru ' , ' navy ' , ' teal ' ,
ymin = min ( Fdf_Seas_means [ Fdf_Seas_means . index . quarter == 1 ] . mean ( ) )
' darkolivegreen ' , ' turquoise ' , ' lightgreen ' , ' darkgreen ' , ' lightpink ' , ' slateblue ' , ' slategray ' , ' orange ' , ' tomato ' , ' peru ' , ' navy ' , ' teal ' ,
ymax = max ( Fdf_Seas_means [ Fdf_Seas_means . index . quarter == 1 ] . mean ( ) )
' darkolivegreen ' , ' turquoise ' , ' lightgreen ' , ' darkgreen ' , ' lightpink ' , ' slateblue ' , ' slategray ' , ' orange ' , ' tomato ' , ' peru ' , ' navy ' , ' teal ' ]
plt . title ( Clim_var_type + ' - DJF Summer means/sums ' )
plotcolours36b = [ ' tomato ' , ' royalblue ' , ' mediumpurple ' , ' tomato ' , ' royalblue ' , ' mediumpurple ' , ' tomato ' , ' royalblue ' , ' mediumpurple ' , ' tomato ' , ' royalblue ' , ' mediumpurple ' ,
pd . DataFrame ( Fdf_Seas_means [ Fdf_Seas_means . index . quarter == 1 ] . mean ( ) ) . plot ( kind = ' bar ' , ylim = ( ymin , ymax ) )
' tomato ' , ' royalblue ' , ' mediumpurple ' , ' tomato ' , ' royalblue ' , ' mediumpurple ' , ' tomato ' , ' royalblue ' , ' mediumpurple ' , ' tomato ' , ' royalblue ' , ' mediumpurple ' ,
pdf . savefig ( bbox_inches = ' tight ' , pad_inches = 0.4 )
' tomato ' , ' royalblue ' , ' mediumpurple ' , ' tomato ' , ' royalblue ' , ' mediumpurple ' , ' tomato ' , ' royalblue ' , ' mediumpurple ' , ' tomato ' , ' royalblue ' , ' mediumpurple ' ]
plt . close ( )
plotcolours12 = [ ' darkolivegreen ' , ' turquoise ' , ' lightgreen ' , ' darkgreen ' , ' lightpink ' , ' slateblue ' , ' slategray ' , ' orange ' , ' tomato ' , ' peru ' , ' navy ' , ' teal ' ]
plt . title ( Clim_var_type + ' - DJF Summer means/sums ' )
plotcolours15 = [ ' darkolivegreen ' , ' turquoise ' , ' lightgreen ' , ' darkgreen ' , ' lightpink ' , ' slateblue ' , ' slategray ' , ' orange ' , ' tomato ' , ' peru ' , ' navy ' , ' teal ' , ' lightgreen ' , ' lightpink ' , ' slateblue ' ]
Fdf_Seas_means [ Fdf_Seas_means . index . quarter == 1 ] . boxplot ( rot = 90 )
#plt.cm.Paired(np.arange(len(Fdf_1900_2080_means)))
pdf . savefig ( bbox_inches = ' tight ' , pad_inches = 0.4 )
#write the key plots to a single pdf document
plt . close ( )
pdf_out_file_name = Clim_var_type + ' _ ' + Stats + ' _start_ ' + Base_period_start + ' _NARCliM_summary_ ' + Version + ' .pdf '
ymin = min ( Fdf_Seas_means [ Fdf_Seas_means . index . quarter == 2 ] . mean ( ) )
pdf_out_path = output_directory + ' / ' + pdf_out_file_name
ymax = max ( Fdf_Seas_means [ Fdf_Seas_means . index . quarter == 2 ] . mean ( ) )
#open pdf and add the plots
plt . title ( Clim_var_type + ' - MAM Autumn means/sums ' )
with PdfPages ( pdf_out_path ) as pdf :
pd . DataFrame ( Fdf_Seas_means [ Fdf_Seas_means . index . quarter == 2 ] . mean ( ) ) . plot ( kind = ' bar ' , ylim = ( ymin , ymax ) )
#barplot of model means
pdf . savefig ( bbox_inches = ' tight ' , pad_inches = 0.4 )
plt . title ( Clim_var_type + ' - model means - full period ' )
plt . close ( )
ymin = min ( Fdf_1900_2080_means )
plt . title ( Clim_var_type + ' - MAM Autumn means/sums ' )
ymax = max ( Fdf_1900_2080_means ) + 0.008 * min ( Fdf_1900_2080_means )
Fdf_Seas_means [ Fdf_Seas_means . index . quarter == 2 ] . boxplot ( rot = 90 )
Fdf_1900_2080_means . plot ( kind = ' bar ' , ylim = ( ymin , ymax ) , color = plotcolours36 )
pdf . savefig ( bbox_inches = ' tight ' , pad_inches = 0.4 )
pdf . savefig ( bbox_inches = ' tight ' , pad_inches = 0.4 )
plt . close ( )
plt . close ( )
ymin = min ( Fdf_Seas_means [ Fdf_Seas_means . index . quarter == 3 ] . mean ( ) )
#
ymax = max ( Fdf_Seas_means [ Fdf_Seas_means . index . quarter == 3 ] . mean ( ) )
neardeltadf = delta_all_df [ ' near ' ]
plt . title ( Clim_var_type + ' - JJA Winter means/sums ' )
ymin = min ( neardeltadf ) + 0.1 * min ( neardeltadf )
pd . DataFrame ( Fdf_Seas_means [ Fdf_Seas_means . index . quarter == 3 ] . mean ( ) ) . plot ( kind = ' bar ' , ylim = ( ymin , ymax ) )
ymax = max ( neardeltadf ) + 0.1 * max ( neardeltadf )
pdf . savefig ( bbox_inches = ' tight ' , pad_inches = 0.4 )
neardeltadf = delta_all_df [ ' far ' ]
plt . close ( )
ymin2 = min ( neardeltadf ) + 0.1 * min ( neardeltadf )
plt . title ( Clim_var_type + ' - JJA Winter means/sums ' )
ymax2 = max ( neardeltadf ) + 0.1 * max ( neardeltadf )
Fdf_Seas_means [ Fdf_Seas_means . index . quarter == 3 ] . boxplot ( rot = 90 )
ymin = min ( ymin , ymin2 )
pdf . savefig ( bbox_inches = ' tight ' , pad_inches = 0.4 )
if ( Clim_var_type == ' tasmax ' or Clim_var_type == ' tasmean ' ) :
plt . close ( )
ymin = 0
ymin = min ( Fdf_Seas_means [ Fdf_Seas_means . index . quarter == 4 ] . mean ( ) )
ymax = max ( ymax , ymax2 )
ymax = max ( Fdf_Seas_means [ Fdf_Seas_means . index . quarter == 4 ] . mean ( ) )
#
plt . title ( Clim_var_type + ' - SON Spring means/sums ' )
# delta barplot for report 1#################################
pd . DataFrame ( Fdf_Seas_means [ Fdf_Seas_means . index . quarter == 4 ] . mean ( ) ) . plot ( kind = ' bar ' , ylim = ( ymin , ymax ) )
ax = plt . subplot ( 2 , 1 , 1 )
pdf . savefig ( bbox_inches = ' tight ' , pad_inches = 0.4 )
plt . title ( Clim_var_type + ' - model deltas - near-present ' )
plt . close ( )
neardeltadf = delta_all_df [ ' near ' ]
plt . title ( Clim_var_type + ' - SON Spring means/sums ' )
neardeltadf . plot ( kind = ' bar ' , color = plotcolours15 , ylim = ( ymin , ymax ) , ax = ax )
Fdf_Seas_means [ Fdf_Seas_means . index . quarter == 4 ] . boxplot ( rot = 90 )
plt . xticks ( [ ] )
pdf . savefig ( bbox_inches = ' tight ' , pad_inches = 0.4 )
#ax.xaxis.set_ticklabels([])
plt . close ( )
#pdf.savefig(bbox_inches='tight', ylim=(ymin,ymax), pad_inches=0.4)
#plt.close()
#
ax = plt . subplot ( 2 , 1 , 2 )
plt . title ( Clim_var_type + ' - model deltas - far-present ' )
neardeltadf = delta_all_df [ ' far ' ]
neardeltadf . plot ( kind = ' bar ' , color = plotcolours15 , ylim = ( ymin , ymax ) , ax = ax )
ax . xaxis . grid ( False )
#fig.patch.set_alpha(0)
#plt.show()
pdf . savefig ( bbox_inches = ' tight ' , ylim = ( ymin , ymax ) , pad_inches = 0.4 )
plt . close ( )
# end delta barplot for report 1#################################
#
#full period density comparison
plt . title ( Clim_var_type + ' - density comparison - full period - all models ' )
Summarized_df . plot . kde ( )
pdf . savefig ( bbox_inches = ' tight ' , pad_inches = 0.4 )
plt . close ( )
#full period density comparison
plt . title ( Clim_var_type + ' - density comparison - full period - max delta model ' )
xmin = float ( max ( np . nanpercentile ( Fdf_1900_2080 . filter ( regex = Max_dif_mod_name [ : - 5 ] ) , 50 ) - 4 * np . std ( Fdf_1900_2080 . filter ( regex = Max_dif_mod_name [ : - 5 ] ) ) ) )
xmax = float ( max ( np . nanpercentile ( Fdf_1900_2080 . filter ( regex = Max_dif_mod_name [ : - 5 ] ) , 50 ) + 4 * np . std ( Fdf_1900_2080 . filter ( regex = Max_dif_mod_name [ : - 5 ] ) ) ) )
Fdf_1900_2080 . filter ( regex = Max_dif_mod_name [ : - 5 ] ) . plot . kde ( xlim = ( xmin , xmax ) )
pdf . savefig ( bbox_inches = ' tight ' , pad_inches = 0.4 )
plt . close ( )
#annual box
plt . title ( Clim_var_type + ' - Annual means/sums for max diff model ' )
Fdf_1900_2080_annual . boxplot ( rot = 90 )
pdf . savefig ( bbox_inches = ' tight ' , pad_inches = 0.4 )
plt . close ( )
#
#daily box
plt . title ( Clim_var_type + ' - Daily means/sums ' )
Fdf_1900_2080 . boxplot ( rot = 90 )
pdf . savefig ( bbox_inches = ' tight ' , pad_inches = 0.4 )
plt . close ( )
# time series plot annual ALL models
plt . title ( Clim_var_type + ' - Time series - all models ' )
Mod_order = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 19 , 20 , 21 , 16 , 17 , 18 , 22 , 23 , 24 , 31 , 32 , 33 , 25 , 26 , 27 , 28 , 29 , 30 , 34 , 35 , 36 ]
test = Fdf_1900_2080_annual
Mod_Names = test . columns
New_Mod_Name = [ ]
for i in range ( 0 , len ( Mod_Names ) ) :
New_Mod_Name . append ( str ( Mod_order [ i ] + 10 ) + ' _ ' + Mod_Names [ i ] )
test . columns = New_Mod_Name
test_sorted = test . reindex_axis ( sorted ( test . columns ) , axis = 1 )
colnamest = test . columns
test_sorted . columns = [ w [ 3 : - 5 ] for w in colnamest ]
test_sorted . plot ( legend = False , color = plotcolours36 )
pdf . savefig ( bbox_inches = ' tight ' , pad_inches = 0.4 )
plt . close ( )
# time series plot annual ALL models
plt . title ( Clim_var_type + ' - Time series - representative models ' )
dfall_annual . plot ( legend = False )
pdf . savefig ( bbox_inches = ' tight ' , pad_inches = 0.4 )
plt . close ( )
# seasonal mean boxplots
ymin = min ( Fdf_Seas_means [ Fdf_Seas_means . index . quarter == 1 ] . mean ( ) )
ymax = max ( Fdf_Seas_means [ Fdf_Seas_means . index . quarter == 1 ] . mean ( ) )
plt . title ( Clim_var_type + ' - DJF Summer means/sums ' )
pd . DataFrame ( Fdf_Seas_means [ Fdf_Seas_means . index . quarter == 1 ] . mean ( ) ) . plot ( kind = ' bar ' , ylim = ( ymin , ymax ) )
pdf . savefig ( bbox_inches = ' tight ' , pad_inches = 0.4 )
plt . close ( )
plt . title ( Clim_var_type + ' - DJF Summer means/sums ' )
Fdf_Seas_means [ Fdf_Seas_means . index . quarter == 1 ] . boxplot ( rot = 90 )
pdf . savefig ( bbox_inches = ' tight ' , pad_inches = 0.4 )
plt . close ( )
ymin = min ( Fdf_Seas_means [ Fdf_Seas_means . index . quarter == 2 ] . mean ( ) )
ymax = max ( Fdf_Seas_means [ Fdf_Seas_means . index . quarter == 2 ] . mean ( ) )
plt . title ( Clim_var_type + ' - MAM Autumn means/sums ' )
pd . DataFrame ( Fdf_Seas_means [ Fdf_Seas_means . index . quarter == 2 ] . mean ( ) ) . plot ( kind = ' bar ' , ylim = ( ymin , ymax ) )
pdf . savefig ( bbox_inches = ' tight ' , pad_inches = 0.4 )
plt . close ( )
plt . title ( Clim_var_type + ' - MAM Autumn means/sums ' )
Fdf_Seas_means [ Fdf_Seas_means . index . quarter == 2 ] . boxplot ( rot = 90 )
pdf . savefig ( bbox_inches = ' tight ' , pad_inches = 0.4 )
plt . close ( )
ymin = min ( Fdf_Seas_means [ Fdf_Seas_means . index . quarter == 3 ] . mean ( ) )
ymax = max ( Fdf_Seas_means [ Fdf_Seas_means . index . quarter == 3 ] . mean ( ) )
plt . title ( Clim_var_type + ' - JJA Winter means/sums ' )
pd . DataFrame ( Fdf_Seas_means [ Fdf_Seas_means . index . quarter == 3 ] . mean ( ) ) . plot ( kind = ' bar ' , ylim = ( ymin , ymax ) )
pdf . savefig ( bbox_inches = ' tight ' , pad_inches = 0.4 )
plt . close ( )
plt . title ( Clim_var_type + ' - JJA Winter means/sums ' )
Fdf_Seas_means [ Fdf_Seas_means . index . quarter == 3 ] . boxplot ( rot = 90 )
pdf . savefig ( bbox_inches = ' tight ' , pad_inches = 0.4 )
plt . close ( )
ymin = min ( Fdf_Seas_means [ Fdf_Seas_means . index . quarter == 4 ] . mean ( ) )
ymax = max ( Fdf_Seas_means [ Fdf_Seas_means . index . quarter == 4 ] . mean ( ) )
plt . title ( Clim_var_type + ' - SON Spring means/sums ' )
pd . DataFrame ( Fdf_Seas_means [ Fdf_Seas_means . index . quarter == 4 ] . mean ( ) ) . plot ( kind = ' bar ' , ylim = ( ymin , ymax ) )
pdf . savefig ( bbox_inches = ' tight ' , pad_inches = 0.4 )
plt . close ( )
plt . title ( Clim_var_type + ' - SON Spring means/sums ' )
Fdf_Seas_means [ Fdf_Seas_means . index . quarter == 4 ] . boxplot ( rot = 90 )
pdf . savefig ( bbox_inches = ' tight ' , pad_inches = 0.4 )
plt . close ( )