4646from shutil import copyfile
4747
4848import matplotlib
49- matplotlib .use ('Agg' )
50- #import matplotlib.pyplot as plt
51- from matplotlib import pyplot as plt
49+
50+ matplotlib .use ("Agg" )
5251import numpy as np
5352import pandas as pd
5453import xarray as xr
5554import xcdat as xc
55+ # import matplotlib.pyplot as plt
56+ from matplotlib import pyplot as plt
5657
5758import pcmdi_metrics
5859from pcmdi_metrics import resources
59- from pcmdi_metrics .io import load_regions_specs , region_subset
60+ from pcmdi_metrics .io import load_regions_specs , region_subset , xcdat_open
6061from pcmdi_metrics .mean_climate .lib import pmp_parser
6162from pcmdi_metrics .monsoon_sperber .lib import (
6263 AddParserArgument ,
6768 sperber_metrics ,
6869)
6970from pcmdi_metrics .utils import create_land_sea_mask , fill_template
70- from pcmdi_metrics .io import xcdat_open
7171
7272
7373def tree ():
@@ -92,7 +92,7 @@ def pick_year_last_day(ds):
9292# =================================================
9393# Hard coded options... will be moved out later
9494# -------------------------------------------------
95- #list_monsoon_regions = ["AIR", "AUS", "Sahel", "GoG", "NAmo", "SAmo"]
95+ # list_monsoon_regions = ["AIR", "AUS", "Sahel", "GoG", "NAmo", "SAmo"]
9696list_monsoon_regions = ["AIR" , "Sahel" ]
9797# list_monsoon_regions = ["all"]
9898
@@ -293,7 +293,16 @@ def pick_year_last_day(ds):
293293 modpath (model = model , exp = exp , realization = realization , variable = var )
294294 )
295295 if debug :
296- print ("model: " , model , " exp: " , exp , " realization: " , realization , " variable: " , var )
296+ print (
297+ "model: " ,
298+ model ,
299+ " exp: " ,
300+ exp ,
301+ " realization: " ,
302+ realization ,
303+ " variable: " ,
304+ var ,
305+ )
297306 print ("debug: model_path_list: " , model_path_list )
298307 # land fraction
299308 model_lf_path = modpath_lf (model = model )
@@ -314,18 +323,18 @@ def pick_year_last_day(ds):
314323 ds_lf = xcdat_open (model_lf_path )
315324 except Exception :
316325 ds_lf = None
317-
318- if not ds_lf :
326+
327+ if not ds_lf :
319328 lf_array = create_land_sea_mask (ds_lf , method = "pcmdi" )
320329 ds_lf = lf_array .to_dataset ().compute ()
321330 ds_lf = ds_lf .rename_vars ({"lsmask" : "sftlf" })
322331
323- # use pcmdi mask
324- # lf_array = create_land_sea_mask(ds_lf, method="pcmdi")
325- # ds_lf = lf_array.to_dataset().compute()
326- # ds_lf = ds_lf.rename_vars({"lsmask": "sftlf"})
332+ # use pcmdi mask
333+ # lf_array = create_land_sea_mask(ds_lf, method="pcmdi")
334+ # ds_lf = lf_array.to_dataset().compute()
335+ # ds_lf = ds_lf.rename_vars({"lsmask": "sftlf"})
327336
328- if model in [ "EC-EARTH" ]: # , "BNU-ESM" ]:
337+ if model in ["EC-EARTH" ]: # , "BNU-ESM" ]:
329338 ds_lf = ds_lf .isel (lat = slice (None , None , - 1 ))
330339 lf = ds_lf .sftlf .sel (lat = slice (- 90 , 90 )) # land frac file must be global
331340
@@ -350,10 +359,9 @@ def pick_year_last_day(ds):
350359 # Get time coordinate information
351360 print ("model_path = " , model_path )
352361
353-
354362 dc = xcdat_open (model_path , decode_times = True )
355- dc [' time' ].attrs [' axis' ] = 'T'
356- dc [' time' ].attrs [' standard_name' ] = ' time'
363+ dc [" time" ].attrs [" axis" ] = "T"
364+ dc [" time" ].attrs [" standard_name" ] = " time"
357365 dc = xr .decode_cf (dc , decode_times = True )
358366 dc = dc .bounds .add_missing_bounds ("X" )
359367 dc = dc .bounds .add_missing_bounds ("Y" )
@@ -363,7 +371,6 @@ def pick_year_last_day(ds):
363371 c = xc .center_times (dc )
364372 eday = pick_year_last_day (dc )
365373
366-
367374 # Get starting and ending year and month
368375 startYear = c .time .values [0 ].year
369376 startMonth = c .time .values [0 ].month
@@ -493,7 +500,6 @@ def pick_year_last_day(ds):
493500 d .values = d .values * 86400.0
494501 d ["units" ] = units
495502
496-
497503 # variable for over land only
498504 d_land = model_land_only (model , d , lf , debug = debug )
499505
@@ -523,7 +529,6 @@ def pick_year_last_day(ds):
523529 d_sub_pr .values = d_sub_pr .values * 86400.0
524530 d_sub_pr ["units" ] = units
525531
526-
527532 else :
528533 # land-only rainfall
529534
@@ -544,11 +549,9 @@ def pick_year_last_day(ds):
544549 model , d_sub_pr , lf_sub , debug = debug
545550 )
546551
547-
548552 d_sub_pr .values = d_sub_pr .values * 86400.0
549553 d_sub_pr ["units" ] = units
550554
551-
552555 # Area average
553556
554557 ds_sub_pr = d_sub_pr .to_dataset ().compute ()
@@ -557,7 +560,6 @@ def pick_year_last_day(ds):
557560 ds_sub_pr = ds_sub_pr .bounds .add_missing_bounds ("Y" )
558561 ds_sub_pr = ds_sub_pr .bounds .add_missing_bounds ("T" )
559562
560-
561563 if "lat_bnds" not in ds_sub_pr .variables :
562564 lat_bnds = dc ["lat_bnds" ].sel (lat = ds_sub_pr ["lat" ])
563565 ds_sub_pr ["lat_bnds" ] = lat_bnds
@@ -567,8 +569,6 @@ def pick_year_last_day(ds):
567569 ).compute ()
568570 d_sub_aave = ds_sub_aave .pr
569571
570-
571-
572572 if debug :
573573 print ("debug: region:" , region )
574574
@@ -603,7 +603,6 @@ def pick_year_last_day(ds):
603603
604604 d_sub_aave = xr .concat ([part1 , part2 ], dim = "time" )
605605
606-
607606 if debug :
608607 print (
609608 "debug: " ,
@@ -638,7 +637,6 @@ def pick_year_last_day(ds):
638637 coords = {"time" : time_coords },
639638 )
640639
641-
642640 if debug :
643641 print (
644642 "debug: pentad_time_series length: " ,
@@ -648,14 +646,14 @@ def pick_year_last_day(ds):
648646 # Keep pentad time series length in consistent
649647 ref_length = int (365 / n )
650648 if len (pentad_time_series ) < ref_length :
651-
652649 pentad_time_series = pentad_time_series .interp (
653- time = pd .date_range (time_coords [0 ], time_coords [- 1 ], periods = ref_length )
650+ time = pd .date_range (
651+ time_coords [0 ], time_coords [- 1 ], periods = ref_length
652+ )
654653 )
655654
656655 time_coords = pentad_time_series .coords ["time" ]
657656
658-
659657 pentad_time_series_cumsum = np .cumsum (pentad_time_series )
660658 pentad_time_series = xr .DataArray (
661659 pentad_time_series ,
@@ -672,7 +670,6 @@ def pick_year_last_day(ds):
672670 pentad_time_series_cumsum .attrs ["units" ] = str (d .units .values )
673671 pentad_time_series_cumsum .coords ["time" ] = time_coords
674672
675-
676673 if nc_out :
677674 # Archive individual year time series in netCDF file
678675 pentad_time_series .to_netcdf (file_path , mode = "a" )
@@ -819,9 +816,7 @@ def pick_year_last_day(ds):
819816
820817 # obs
821818 ax [region ].plot (
822- np .array (
823- dict_obs_composite [reference_data_name ][region ]
824- ),
819+ np .array (dict_obs_composite [reference_data_name ][region ]),
825820 c = "blue" ,
826821 label = reference_data_name ,
827822 )
@@ -836,9 +831,9 @@ def pick_year_last_day(ds):
836831 ax [region ].axvline (
837832 x = idx ,
838833 ymin = 0 ,
839- ymax = dict_obs_composite [reference_data_name ][
840- region
841- ][ idx ] .item (),
834+ ymax = dict_obs_composite [reference_data_name ][region ][
835+ idx
836+ ].item (),
842837 c = "blue" ,
843838 ls = "--" ,
844839 )
0 commit comments