Пример #1
0
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
print(f'Number of years: {dimt}')

ds = xr.Dataset()
name_da = {0: VAR+'_corrected', 1: 'trend_piControl'}

da = xr.DataArray(np.zeros([dimMod, dimt]), coords=[ModelList.Model, time_all], 
                  dims=['model', 'time'])
trend_da = xr.DataArray(np.zeros([dimMod, dimt]), coords=[ModelList.Model, time_all], 
                  dims=['model', 'time'])

for i in range(dimMod):
    print(f'####### Working on model {i}, {ModelList.Model[i]}  ############')

    # Read paths and file names
    if MIP == 'cmip5':
        sce_files = loc.select_cmip5_files(EXP, VAR, ModelList.loc[i])
        hist_files = loc.select_cmip5_files('historical', VAR, ModelList.loc[i])
        
    elif MIP == 'cmip6':
        if EXP!='historical':
            if ModelList.Model[i] == 'MPI-ESM1-2-HR':
                # For this model the scenarios are done at DKRZ while piControl 
                # and historical are done at MPI-M
                ModelList.Center[i] = 'DKRZ'

            sce_files = loc.select_cmip6_files(EXP, VAR, ModelList.iloc[i])
            
            try:
                print('#### Using the following files: ####')
                [print(str(x)) for x in sce_files]
            except: