def do_calculations(data_path=None, lay_widths=None, start_year=None, end_year=None): cache = get_cache_file(**locals()) if os.path.isfile(cache): return pickle.load(open(cache, "rb")) alt_data_manager = CRCMDataManager(layer_widths=lay_widths) tsoil3d_field = None for lev in range(len(lay_widths)): field_dict = analysis.get_annual_maxima(path_to_hdf_file=data_path, var_name="I0", level=lev, start_year=start_year, end_year=end_year) field = np.max([fi for fi in field_dict.values()], axis=0) if tsoil3d_field is None: tsoil3d_field = np.zeros(field.shape + (len(lay_widths), )) print("lev, min, max = {}, {}, {}".format(lev, field.min(), field.max())) plt.figure() img = plt.contourf(field) plt.colorbar(img) plt.show() tsoil3d_field[:, :, lev] = field alt_field = alt_data_manager.get_alt(tsoil3d_field) with open(cache, "wb") as f: pickle.dump(alt_field, f) return alt_field
def main(): soiltemp_var_name = "TBAR" path1 = "/skynet1_rech3/huziy/class_offline_simulations_VG/dpth_var/CLASS_output_CLASSoff1_Arctic_0.5_ERA40_dpth_to_bdrck_var_1980-2009/TBAR.rpn" label1 = "Variable (real) depth to bedrock" path2 = "/skynet1_rech3/huziy/class_offline_simulations_VG/dpth_3.6m/CLASS_output_CLASSoff1_Arctic_0.5_ERA40_dpth_to_bdrck_constant_1980-2009/TBAR.rpn" label2 = "Fixed depth to bedrock (3.6 m)" path_to_dpth_to_bedrock = "/skynet1_rech3/huziy/CLASS_offline_VG/GEOPHYSICAL_FIELDS/test_analysis.rpn" # read depth to bedrock r = RPN(path_to_dpth_to_bedrock) dpth = r.get_first_record_for_name("DPTH") r.close() layer_widths = [ 0.1, 0.2, 0.3, 0.5, 0.9, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5 ] print(len(layer_widths)) crcm_data_manager = CRCMDataManager(layer_widths=layer_widths) # Read in the temperature profiles r1 = RPN(path1) soiltemp1 = r1.get_4d_field(name=soiltemp_var_name) lons2d, lats2d = r1.get_longitudes_and_latitudes_for_the_last_read_rec() rll = RotatedLatLon(**r1.get_proj_parameters_for_the_last_read_rec()) b = rll.get_basemap_object_for_lons_lats(lons2d=lons2d, lats2d=lats2d, resolution="c") r1.close() r2 = RPN(path2) soiltemp2 = r2.get_4d_field(name=soiltemp_var_name) r2.close() dates_sorted = list(sorted(soiltemp1.keys())) levels_sorted = list(sorted(list(soiltemp1.items())[0][1].keys())) # group dates for each year alt1_list = [] alt2_list = [] for year, dates_group in itertools.groupby(dates_sorted, lambda par: par.year): t1 = [] t2 = [] for d in dates_group: t1.append([soiltemp1[d][lev] for lev in levels_sorted]) t2.append([soiltemp2[d][lev] for lev in levels_sorted]) # Get maximum temperature profiles t1max = np.max(t1, axis=0).transpose(1, 2, 0) t2max = np.max(t2, axis=0).transpose(1, 2, 0) print(t1max.shape, t2max.shape) #calculate and save alts h1 = crcm_data_manager.get_alt(t1max) h2 = crcm_data_manager.get_alt(t2max) h1[h1 < 0] = np.nan h2[h2 < 0] = np.nan alt1_list.append(h1) alt2_list.append(h2) #take into account permafrost alt1_list_pf = [] alt2_list_pf = [] n_years_for_pf = 3 for i in range(len(alt1_list) - n_years_for_pf): alt1_list_pf.append(np.max(alt1_list[i:i + n_years_for_pf], axis=0)) alt2_list_pf.append(np.max(alt2_list[i:i + n_years_for_pf], axis=0)) # calculate climatological mean alt1 = np.mean(alt1_list_pf, axis=0) alt2 = np.mean(alt2_list_pf, axis=0) print(np.isnan(alt1).any(), np.isnan(alt2).any()) #mask nans alt1 = np.ma.masked_where(np.isnan(alt1), alt1) alt2 = np.ma.masked_where(np.isnan(alt2), alt2) #calculate change due to fixed depth to bedrock delta = alt2 - alt1 # for i in range(len(alt1_list)): alt1_list[i] = np.ma.masked_where(np.isnan(alt1_list[i]), alt1_list[i]) alt2_list[i] = np.ma.masked_where(np.isnan(alt2_list[i]), alt2_list[i]) #tval, pval = ttest_ind(alt1_list, alt2_list) #mask oceans lons2d[lons2d > 180] -= 360 dpth = maskoceans(lons2d, lats2d, dpth) delta = np.ma.masked_where(dpth.mask, delta) #calculate differences in SWE path_swe_1 = path1.replace("TBAR.rpn", "SNO.rpn") r1 = RPN(path_swe_1) swe1 = r1.get_all_time_records_for_name("SNO") r1.close() swe1_winter_clim = np.mean( [field for key, field in swe1.items() if key.month in [1, 2, 12]], axis=0) swe1_winter_clim = np.ma.masked_where( (swe1_winter_clim >= 999) | dpth.mask, swe1_winter_clim) path_swe_2 = path2.replace("TBAR.rpn", "SNO.rpn") r2 = RPN(path_swe_2) swe2 = r2.get_all_time_records_for_name("SNO") r2.close() swe2_winter_clim = np.mean( [field for key, field in swe2.items() if key.month in [1, 2, 12]], axis=0) swe2_winter_clim = np.ma.masked_where( (swe2_winter_clim >= 999) | dpth.mask, swe2_winter_clim) #plotting print("Start plotting ...") plot_differences(b, lons2d, lats2d, dpth, delta, label="ALT(DPTH=3.6m) - ALT(DPTH~)", pvalue=None, swe_diff=swe2_winter_clim - swe1_winter_clim)
def main(): soiltemp_var_name = "TBAR" path1 = "/skynet1_rech3/huziy/class_offline_simulations_VG/dpth_var/CLASS_output_CLASSoff1_Arctic_0.5_ERA40_dpth_to_bdrck_var_1980-2009/TBAR.rpn" label1 = "Variable (real) depth to bedrock" path2 = "/skynet1_rech3/huziy/class_offline_simulations_VG/dpth_3.6m/CLASS_output_CLASSoff1_Arctic_0.5_ERA40_dpth_to_bdrck_constant_1980-2009/TBAR.rpn" label2 = "Fixed depth to bedrock (3.6 m)" path_to_dpth_to_bedrock = "/skynet1_rech3/huziy/CLASS_offline_VG/GEOPHYSICAL_FIELDS/test_analysis.rpn" # read depth to bedrock r = RPN(path_to_dpth_to_bedrock) dpth = r.get_first_record_for_name("DPTH") r.close() layer_widths = [0.1, 0.2, 0.3, 0.5, 0.9, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5] print(len(layer_widths)) crcm_data_manager = CRCMDataManager(layer_widths=layer_widths) # Read in the temperature profiles r1 = RPN(path1) soiltemp1 = r1.get_4d_field(name=soiltemp_var_name) lons2d, lats2d = r1.get_longitudes_and_latitudes_for_the_last_read_rec() rll = RotatedLatLon(**r1.get_proj_parameters_for_the_last_read_rec()) b = rll.get_basemap_object_for_lons_lats(lons2d=lons2d, lats2d=lats2d, resolution="c") r1.close() r2 = RPN(path2) soiltemp2 = r2.get_4d_field(name=soiltemp_var_name) r2.close() dates_sorted = list(sorted(soiltemp1.keys())) levels_sorted = list(sorted(list(soiltemp1.items())[0][1].keys())) # group dates for each year alt1_list = [] alt2_list = [] for year, dates_group in itertools.groupby(dates_sorted, lambda par: par.year): t1 = [] t2 = [] for d in dates_group: t1.append([soiltemp1[d][lev] for lev in levels_sorted]) t2.append([soiltemp2[d][lev] for lev in levels_sorted]) # Get maximum temperature profiles t1max = np.max(t1, axis=0).transpose(1, 2, 0) t2max = np.max(t2, axis=0).transpose(1, 2, 0) print(t1max.shape, t2max.shape) #calculate and save alts h1 = crcm_data_manager.get_alt(t1max) h2 = crcm_data_manager.get_alt(t2max) h1[h1 < 0] = np.nan h2[h2 < 0] = np.nan alt1_list.append(h1) alt2_list.append(h2) #take into account permafrost alt1_list_pf = [] alt2_list_pf = [] n_years_for_pf = 3 for i in range(len(alt1_list) - n_years_for_pf): alt1_list_pf.append(np.max(alt1_list[i:i+n_years_for_pf], axis=0)) alt2_list_pf.append(np.max(alt2_list[i:i+n_years_for_pf], axis=0)) # calculate climatological mean alt1 = np.mean(alt1_list_pf, axis=0) alt2 = np.mean(alt2_list_pf, axis=0) print(np.isnan(alt1).any(), np.isnan(alt2).any()) #mask nans alt1 = np.ma.masked_where(np.isnan(alt1), alt1) alt2 = np.ma.masked_where(np.isnan(alt2), alt2) #calculate change due to fixed depth to bedrock delta = alt2 - alt1 # for i in range(len(alt1_list)): alt1_list[i] = np.ma.masked_where(np.isnan(alt1_list[i]), alt1_list[i]) alt2_list[i] = np.ma.masked_where(np.isnan(alt2_list[i]), alt2_list[i]) #tval, pval = ttest_ind(alt1_list, alt2_list) #mask oceans lons2d[lons2d > 180] -= 360 dpth = maskoceans(lons2d, lats2d, dpth) delta = np.ma.masked_where(dpth.mask, delta) #calculate differences in SWE path_swe_1 = path1.replace("TBAR.rpn", "SNO.rpn") r1 = RPN(path_swe_1) swe1 = r1.get_all_time_records_for_name("SNO") r1.close() swe1_winter_clim = np.mean( [field for key, field in swe1.items() if key.month in [1, 2, 12]], axis=0) swe1_winter_clim = np.ma.masked_where((swe1_winter_clim >= 999) | dpth.mask, swe1_winter_clim) path_swe_2 = path2.replace("TBAR.rpn", "SNO.rpn") r2 = RPN(path_swe_2) swe2 = r2.get_all_time_records_for_name("SNO") r2.close() swe2_winter_clim = np.mean( [field for key, field in swe2.items() if key.month in [1, 2, 12]], axis=0) swe2_winter_clim = np.ma.masked_where((swe2_winter_clim >= 999) | dpth.mask, swe2_winter_clim) #plotting print("Start plotting ...") plot_differences(b, lons2d, lats2d, dpth, delta, label="ALT(DPTH=3.6m) - ALT(DPTH~)", pvalue=None, swe_diff=swe2_winter_clim - swe1_winter_clim)