plt.figure(figsize=(15, 10))
if plot_type == 'lead-lag':
    gs = gridspec.GridSpec(2, 2)

#model_name_list.remove('AWI-CM-1-1-MR')

for i, model_name in enumerate(model_name_list):

    try:
        # read in SSTs
        if model_name != 'ERA20C':
            data_path0 = '/network/aopp/hera/mad/patterson/CMIP6/data/tos_regridded/'
            full_path = data_path0 + ML.model_institute[
                model_name] + '/' + model_name + '/tos/'
            list_of_files = files_in_directory(
                full_path,
                concat_directory=True,
                include_files_with=ML.ensemble_id[model_name])
            tos_data, lats, lons, levs, times, calendar, t_units = read_in_variable(
                list_of_files[:], 'tos')
        else:
            file_name = [
                '/network/group/aopp/met_data/MET003_ERA20C/data/tos/mon/tos_mon_ERA20C_2.5x2.5_189002-201012.nc'
            ]
            tos_data, lats, lons, levs, times, calendar, t_units = read_in_variable(
                file_name, 'sst')
        SST_am, years_SST = calculate_annual_mean(tos_data,
                                                  times,
                                                  calendar,
                                                  t_units,
                                                  season=season)
                         axis=1)
    NINO34_SST = (NINO34_SST - np.mean(NINO34_SST)) / np.std(NINO34_SST)
    return NINO34_SST


# read in data
id = 'r102i1p1f1'  #'r101i1p1f1' #'r1i1p2f1' #'r2i1p1f1' #'r1i1p5f1' #'r1i1p3f1' #'r1i1p1f3' #'r1i2p1f1' # 'r1i1p1f2' #'r1i1p1f1' # ensemble member

for i, model_name in enumerate(model_name_list):
    try:
        if model_name is not 'ERA20C':
            data_path0 = '/network/aopp/hera/mad/patterson/CMIP6/data/tos_regridded/'
            full_path = data_path0 + model_institute[
                model_name] + '/' + model_name + '/tos/'
            list_of_files = files_in_directory(full_path,
                                               concat_directory=True,
                                               include_files_with=id)
            tos_data, lats, lons, levs, times, calendar, t_units = read_in_variable(
                list_of_files[:], 'tos')
        else:
            list_of_files = [
                '/network/group/aopp/met_data/MET003_ERA20C/data/tos/mon/tos_mon_ERA20C_2.5x2.5_189002-201012.nc'
            ]
            tos_data, lats, lons, levs, times, calendar, t_units = read_in_variable(
                list_of_files[:], 'sst')
        PDO = calculate_PDO(tos_data, lats, lons, times, t_units, calendar)
        IPO = calculate_IPO(tos_data, lats, lons, times, t_units, calendar)
        IOBM = calculate_IOBM(tos_data, lats, lons, times, t_units, calendar)
        IOBM2 = calculate_IOBM2(tos_data, lats, lons, times, t_units, calendar)
        IOD = calculate_IOD(tos_data, lats, lons, times, t_units, calendar)
        NINO34 = calculate_NINO34(tos_data, lats, lons, times, t_units,
lat_min_tropics, lat_max_tropics = -30, 30

# running mean
N = 10
halfN = int(N / 2)

for i, model_name in enumerate(model_name_list):

    try:
        data_path0 = '/network/group/aopp/predict/AWH007_BEFORT_CMIP6/piControl/'

        # variable 1
        data_path1 = '/piControl/Amon/' + variable1_name + '/gn/latest'
        full_path = data_path0 + model_institute[
            model_name] + '/' + model_name + data_path1
        list_of_files1 = files_in_directory(full_path, concat_directory=True)
        variable1_data, lats1, lons1, levs1, times1, calendar1, t_units1 = read_in_variable(
            list_of_files1[:], variable1_name, chosen_level=chosen_level1)

        # variable 2
        data_path2 = '/piControl/Amon/' + variable2_name + '/gn/latest'
        full_path = data_path0 + model_institute[
            model_name] + '/' + model_name + data_path2
        list_of_files2 = files_in_directory(full_path, concat_directory=True)
        variable2_data, lats2, lons2, levs2, times2, calendar2, t_units2 = read_in_variable(
            list_of_files2[:], variable2_name, chosen_level=chosen_level2)

        # truncate time series so that only common time periods are included
        if np.min(times1) != np.min(times2):
            if np.min(times1) > np.min(times2):
                earliest_common_time = np.min(times1)
    try:
        # read in SST file and times
        SST_index_file_name = loading_dir + '/SST_indices_' + model_name + '.nc'
        nc_SST = Dataset(SST_index_file_name, 'r')
        times_SST = nc_SST.variables['times'][:]
        calendar_SST = nc_SST.variables['times'].calendar
        t_units_SST = nc_SST.variables['times'].units

        # read in precipitation, zonal wind, slp
        data_path0 = '/network/group/aopp/predict/AWH007_BEFORT_CMIP6/piControl/'
        # precip
        data_path_pr = '/piControl/Amon/pr/gn/latest'
        full_path_pr = data_path0 + model_institute[
            model_name] + '/' + model_name + data_path_pr
        list_of_files_pr = files_in_directory(full_path_pr,
                                              concat_directory=True)
        pr_data, lats_pr, lons_pr, levs_pr, times_pr, calendar_pr, t_units_pr = read_in_variable(
            list_of_files_pr[:], 'pr')
        # slp
        data_path_psl = '/piControl/Amon/psl/gn/latest'
        full_path_psl = data_path0 + model_institute[
            model_name] + '/' + model_name + data_path_psl
        list_of_files_psl = files_in_directory(full_path_psl,
                                               concat_directory=True)
        psl_data, lats_psl, lons_psl, levs_psl, times_psl, calendar_psl, t_units_psl = read_in_variable(
            list_of_files_psl[:], 'psl')

        # truncate times so that only a common time period is used
        #earliest_common_time = np.min(times_SST)
        #if np.min(times_SST) != np.min(times_psl) & (np.min(times_psl) > np.min(times_SST)): earliest_common_time = np.min(times_psl)
        #latest_common_time = np.max(times_SST)
model_name_list.remove('CanESM5')
model_name_list.append('HadGEM3-GC31-LL')
model_name_list.append('CESM2')

plt.figure(figsize=(30, 35))
gs = gridspec.GridSpec(8, 2)

for i, model_name in enumerate(model_name_list):

    try:
        #data_path0 = '/network/aopp/hera/mad/patterson/iCMIP6/data/piControl/ts/'
        data_path0 = '/network/group/aopp/predict/AWH007_BEFORT_CMIP6/piControl/'  #AWI/AWI-CM-1-1-MR/piControl/Amon/tas/gn/latest
        full_path = data_path0 + ML.model_institute[
            model_name] + '/' + model_name + '/piControl/Amon/tas/gn/latest/'
        list_of_files = files_in_directory(full_path,
                                           concat_directory=True,
                                           exclude_files_with='ImonAnt')
        ts, lats_ts, lons_ts, levs_ts, times_ts, calendar_ts, t_units_ts = read_in_variable(
            list_of_files[:], 'tas')

        ts_am, years = calculate_annual_mean(ts,
                                             times_ts,
                                             calendar_ts,
                                             t_units_ts,
                                             season=season)

        ts_mean = global_mean(ts_am, lats_ts)

        ax = plt.subplot(gs[i])
        plt.title(model_name, fontsize=20)
        plt.plot(ts_mean, color='r')