def trend_pic(MIP, VAR, ModelList, order, year_min, year_max, conv_pic_hist, gap, rmv_disc, verbose=False): '''Compute zos or zostoga trend over the pre-industrial control model simulations. Use rmv_disc=True to remove discontinuities in time. Only works for time series (zostoga) not for 3D data (zos)''' EXP = 'piControl' tot_year = year_max - year_min + 1 if MIP == 'cmip5': files = loc.select_cmip5_files('piControl', VAR, ModelList) elif MIP == 'cmip6': files = loc.select_cmip6_files('piControl', VAR, ModelList) if verbose: print("#### Using following files: ####") [print(str(x)) for x in files] ds = xr.open_mfdataset(files, combine='by_coords', use_cftime=True) y_ds = loc.yearly_mean(ds) if ModelList.Model == 'BCC-CSM2-MR' and VAR == 'zos': y_ds = y_ds.rename({'lat': 'rlat', 'lon': 'rlon'}) new_year = np.array(y_ds.time) + conv_pic_hist overlap_years = len( np.where((new_year >= year_min) & (new_year <= year_max))[0]) print(f'Number of overlapping years : {overlap_years}') # Require that at least 90% of the years are available if overlap_years >= tot_year * 0.9: print('Using branching time') branching_method = 'unsing_branching_time' y_ds = y_ds.assign_coords(time=(y_ds.time + conv_pic_hist)) else: print('Not using branching time for piControl') branching_method = 'not_unsing_branching_time' # Assumes piControl simulation starts in 1850 y_ds = y_ds.assign_coords(time=(y_ds.time - y_ds.time[0] + 1850.5)) VAR1 = y_ds[VAR].squeeze() VAR1 = VAR1.sel(time=slice(year_min, year_max)) if rmv_disc: VAR1 = loc.remove_discontinuities(VAR1, gap) VAR1_coeff = VAR1.polyfit(dim='time', deg=order) return VAR1_coeff.polyfit_coefficients, branching_method
try: print('#### Using the following files: ####') [print(str(x)) for x in hist_files] except: sys.exit('ERROR: No file available at that location') hist_ds = open_files(hist_files) if EXP=='historical': all_ds = hist_ds else: sce_ds = open_files(sce_files) all_ds = xr.concat([hist_ds,sce_ds],'time') VAR1 = all_ds[VAR].squeeze() VAR1a = loc.yearly_mean(VAR1) # Remove discontinuites in some time series try: VAR1a = loc.remove_discontinuities(VAR1a, gap) except ValueError as err: print(err.args) continue # Compute the trend from the piControl simulations and save trend if verbose: pic.info_branching(hist_ds.attrs) try: # Convert the year from piControl to historical run if MIP == 'cmip5':
sce_ds = xr.open_mfdataset(sce_files, combine='by_coords', use_cftime=True) except: print(f'!!!!!!!!! Could not open data from {Model[i]}!!!!!!!!!!!!!!!') print( 'Try the function open_mfdataset with the option combine="nested" ' ) continue if EXP != 'historical': ds = xr.concat([hist_ds, sce_ds], 'time') else: ds = hist_ds y_ds = loc.yearly_mean(ds) if Model[i] == 'BCC-CSM2-MR': y_ds = y_ds.rename({'lat': 'rlat', 'lon': 'rlon'}) if 'latitude' and 'longitude' in y_ds.coords: y_ds = y_ds.rename({'latitude': 'lat', 'longitude': 'lon'}) elif 'nav_lat' and 'nav_lon' in y_ds.coords: y_ds = y_ds.rename({'nav_lat': 'lat', 'nav_lon': 'lon'}) # Build regridder with xESMF try: reg_method = 'bilinear' regridder = xe.Regridder( y_ds, ds_out,