Пример #1
0
def main(inargs):
    """Run the program."""

    df = pd.read_csv(inargs.infile)

    fig, axes =plt.subplots(1,2)
    sns.set(style="whitegrid")
    
    sns.boxplot(x="project", y="regression coefficient", hue="comparison",
                data=df[df['realm'] == 'energy'], ax=axes[0], palette='hot')
    axes[0].set_title('(a) energy budget')
    axes[0].set_ylim(-0.25, 1.75)
    axes[0].axhline(y=1.0, color='0.5', linewidth=0.2)
    axes[0].xaxis.label.set_visible(False)
    set_legend(axes[0])
    
    sns.boxplot(x="project", y="regression coefficient", hue="comparison",
                data=df[df['realm'] == 'mass'], ax=axes[1], palette='GnBu_r')
    axes[1].set_title('(b) mass budget')
    axes[1].set_ylim(-0.25, 1.75)
    axes[1].axhline(y=1.0, color='0.5', linewidth=0.2)
    axes[1].xaxis.label.set_visible(False)
    set_legend(axes[1])
    
    for ax in axes.flat:
        ax.label_outer()
    
    plt.savefig(inargs.outfile, bbox_inches='tight', dpi=400)
    log_file = re.sub('.png', '.met', inargs.outfile)
    log_text = cmdprov.new_log(git_repo=repo_dir)
    cmdprov.write_log(log_file, log_text)
Пример #2
0
def main(inargs):
    """Run the program."""

    df = pandas.read_csv(inargs.infile)

    colors = {'historical': 'black', 'GHG-only': 'red', 'AA-only': 'blue'}

    fig, ax = plt.subplots(figsize=(16, 10))

    offset = lambda p: transforms.ScaledTranslation(p / 72., 0,
                                                    plt.gcf().dpi_scale_trans)
    trans = plt.gca().transData

    for experiment in ['historical', 'GHG-only', 'AA-only']:
        toa_vals = numpy.array(
            df.loc[(df['variable'] == 'netTOA')
                   & (df['experiment'] == experiment)]['accumulated_heat'])
        ohu_vals = numpy.array(
            df.loc[(df['variable'] == 'OHU')
                   & (df['experiment'] == experiment)]['accumulated_heat'])
        ohc_vals = numpy.array(
            df.loc[(df['variable'] == 'OHC')
                   & (df['experiment'] == experiment)]['accumulated_heat'])
        xvals = numpy.arange(toa_vals.shape[0])

        plt.scatter(xvals,
                    toa_vals,
                    c=colors[experiment],
                    marker='s',
                    transform=trans + offset(-5))
        plt.scatter(xvals, ohu_vals, c=colors[experiment])
        plt.scatter(xvals,
                    ohc_vals,
                    c=colors[experiment],
                    marker='D',
                    transform=trans + offset(5))

    plt.ticklabel_format(style='sci',
                         axis='y',
                         scilimits=(0, 0),
                         useMathText=True)
    ax.yaxis.major.formatter._useMathText = True

    plt.xticks(xvals, list(df.model.unique()), rotation=40, ha='right')
    ax.axhline(y=0, color='0.5', linestyle='--', linewidth=0.5)
    add_legends()
    plt.ylabel('J')

    plt.savefig(inargs.outfile, bbox_inches='tight')

    log_text = cmdprov.new_log(git_repo=repo_dir)
    log_file = re.sub('.png', '.met', inargs.outfile)
    cmdprov.write_log(log_file, log_text)
def main(inargs):
    """Run the program."""

    metadata_dict = {}
    time_constraint = gio.get_time_constraint(
        [inargs.start_date, inargs.end_date])

    fig = plt.figure(figsize=[11, 10])

    if inargs.rndt_files:
        rndt_nh, rndt_sh = read_hemisphere_data(inargs.rndt_files, 'rndt',
                                                time_constraint, inargs.ensagg)
        iplt.plot(rndt_nh, label='netTOA, NH', color='red', linestyle='solid')
        iplt.plot(rndt_sh, label='netTOA, SH', color='red', linestyle='dashed')

    if inargs.hfds_files:
        hfds_nh, hfds_sh = read_hemisphere_data(inargs.hfds_files, 'hfds',
                                                time_constraint, inargs.ensagg)
        iplt.plot(hfds_nh, label='OHU, NH', color='orange', linestyle='solid')
        iplt.plot(hfds_sh, label='OHU, SH', color='orange', linestyle='dashed')

    if inargs.ohc_files:
        ohc_nh, ohc_sh = read_hemisphere_data(inargs.ohc_files, 'ohc',
                                              time_constraint, inargs.ensagg)
        iplt.plot(ohc_nh, label='OHC, NH', color='blue', linestyle='solid')
        iplt.plot(ohc_sh, label='OHC, SH', color='blue', linestyle='dashed')

    if inargs.ohc_guide_files:
        guide_nh, guide_sh = read_guide_data(inargs.ohc_guide_files, 'ohc',
                                             time_constraint, inargs.ensagg)
        iplt.plot(guide_nh,
                  label='OHC guide, NH',
                  color='0.5',
                  linestyle='solid')
        iplt.plot(guide_sh,
                  label='OHC guide, SH',
                  color='0.5',
                  linestyle='dashed')

    plt.legend()
    if inargs.ybounds:
        ymin, ymax = inargs.ybounds
        plt.ylim([ymin, ymax])

    dpi = inargs.dpi if inargs.dpi else plt.savefig.__globals__['rcParams'][
        'figure.dpi']
    print('dpi =', dpi)
    plt.savefig(inargs.outfile, bbox_inches='tight', dpi=dpi)

    log_text = cmdprov.new_log(
        git_repo=repo_dir)  # infile_history={nh_file: history}
    log_file = re.sub('.png', '.met', inargs.outfile)
    cmdprov.write_log(log_file, log_text)
Пример #4
0
def main(inargs):
    """Run the program."""

    time_constraint = gio.get_time_constraint(inargs.time_bounds)
    basin_names = ['atlantic', 'indian', 'pacific', 'land']
    anomaly_data = {}
    start_data = {}
    data = []
    model_list = []
    for filenum, infile in enumerate(inargs.infiles):
        cube, anomaly_data, start = get_data(infile, inargs.var,
                                             time_constraint)
        units = cube.units
        cum_change = anomaly_data[-1, :]
        model = cube.attributes[
            'source_id'] if 'source_id' in cube.attributes else cube.attributes[
                'model_id']
        ntimes = anomaly_data.shape[0]
        pct_change = ((cum_change / ntimes) / np.absolute(start)) * 100
        total_cum_change = cum_change.sum()
        total_start = start.sum()
        total_pct_change = ((total_cum_change / ntimes) / total_start) * 100
        pct_change_anomaly = pct_change - total_pct_change

        model_list.append(model)
        for basin in range(4):
            data.append([
                model, basin_names[basin], start[basin], cum_change[basin],
                pct_change[basin], pct_change_anomaly[basin]
            ])

    df = pd.DataFrame(data,
                      columns=[
                          'model', 'basin', 'start', 'cumulative_change',
                          'percentage_change', 'percentage_change_anomaly'
                      ])

    model_list.sort()
    experiment = cube.attributes['experiment_id']
    plot_ensemble_lines(df, inargs.var, model_list, experiment, str(units),
                        inargs.ymax)

    plt.savefig(inargs.outfile, bbox_inches='tight')

    log_file = re.sub('.png', '.met', inargs.outfile)
    log_text = cmdprov.new_log(
        infile_history={inargs.infiles[-1]: cube.attributes['history']},
        git_repo=repo_dir)
    cmdprov.write_log(log_file, log_text)

    csv_file = re.sub('.png', '.csv', inargs.outfile)
    df.to_csv(csv_file)
Пример #5
0
def main(inargs):
    """Run the program."""

    metadata_dict = {}
    nplots = len(inargs.contourf_files)
    if inargs.contour_files:
        assert len(inargs.contour_files) == nplots

    fig, cbar_ax, gs = grid_config(nplots)

    for pnum in range(nplots):
        contourf_cube = iris.load_cube(inargs.contourf_files[pnum],
                                       inargs.variable)
        metadata_dict[
            inargs.contourf_files[pnum]] = contourf_cube.attributes['history']

        if inargs.rescale:
            depth_values = numpy.arange(0.5, 5500, 1)
            lat_values = numpy.arange(-89.5, 90.5, 1)
            ref_cube = make_grid(depth_values, lat_values)
            contourf_cube = regrid(contourf_cube, ref_cube)

        if inargs.contour_files:
            contour_cube = iris.load_cube(inargs.contour_files[pnum],
                                          inargs.variable)
            metadata_dict[inargs.contour_files[
                pnum]] = contour_cube.attributes['history']
        else:
            contour_cube = None

        title = inargs.titles[pnum] if inargs.titles else None
        create_plot(gs[pnum],
                    cbar_ax,
                    contourf_cube,
                    contour_cube,
                    inargs.scale_factor,
                    inargs.nyrs,
                    title,
                    ticks=inargs.ticks,
                    rescale=inargs.rescale)

    # Save output
    dpi = inargs.dpi if inargs.dpi else plt.savefig.__globals__['rcParams'][
        'figure.dpi']
    print('dpi =', dpi)
    plt.savefig(inargs.outfile, bbox_inches='tight', dpi=dpi)

    log_text = cmdprov.new_log(infile_history=metadata_dict, git_repo=repo_dir)
    log_file = re.sub('.png', '.met', inargs.outfile)
    cmdprov.write_log(log_file, log_text)
Пример #6
0
def write_rec_file(root: str, main_extension=None, infiles=[]):
    '''
    Write log file for input extension.
    root: input file root
    main_extension: input file extension
    infiles: list of all input files that may have logs attached
    '''

    inlogs = {}
    for file in infiles:
        get_inlog(file, inlogs)
    outlog = cmdprov.new_log(infile_history=inlogs)
    outlog_file = get_rec_file_root(root, main_extension)
    print('writing', outlog_file)
    cmdprov.write_log(outlog_file, outlog)
Пример #7
0
def main(inargs):
    """Run the program."""

    df = pd.read_csv(inargs.infile)
    df.set_index(df['model'] + ' (' + df['run'] + ')', drop=True, inplace=True)

    if inargs.domain == 'energy':
        plot_energy_conservation(df, inargs.cmip_line)
    elif inargs.domain == 'mass':
        plot_mass_conservation(df, inargs.cmip_line)

    plt.savefig(inargs.outfile, bbox_inches='tight', dpi=200)
    log_file = re.sub('.png', '.met', inargs.outfile)
    log_text = cmdprov.new_log(git_repo=repo_dir)
    cmdprov.write_log(log_file, log_text)
Пример #8
0
def main(inargs):
    """Run the program."""

    assert inargs.var in [
        'precipitation_minus_evaporation_flux', 'water_flux_into_sea_water',
        'water_evapotranspiration_flux', 'precipitation_flux'
    ]
    cmap = 'BrBG'
    basins_to_plot = [
        'Atlantic', 'Indian', 'Pacific', 'Land', 'Ocean', 'Globe'
    ]
    if inargs.var == 'precipitation_minus_evaporation_flux':
        var_abbrev = 'P-E'
    elif inargs.var == 'water_evapotranspiration_flux':
        var_abbrev = 'evaporation'
        cmap = 'BrBG_r'
    elif inargs.var == 'precipitation_flux':
        var_abbrev = 'precipitation'
    elif inargs.var == 'water_flux_into_sea_water':
        var_abbrev = 'net mositure import/export (i.e. P-E+R)'
        basins_to_plot = ['Atlantic', 'Indian', 'Pacific', 'Arctic', 'Globe']

    time_constraint = gio.get_time_constraint(inargs.time_bounds)
    input_files = [
        inargs.control_files, inargs.ghg_files, inargs.aa_files,
        inargs.hist_files
    ]
    experiments = ['piControl', 'GHG-only', 'AA-only', 'historical']

    metadata_dict = {}
    fig, axes = plt.subplots(2, 2, figsize=(24, 12))
    axes = axes.flatten()
    for plotnum, exp_files in enumerate(input_files):
        if exp_files:
            experiment = experiments[plotnum]
            print(f"Number of {experiment} models = {len(exp_files)}")
            time_selector = None if experiment == 'piControl' else time_constraint
            file_history = plot_data(axes[plotnum], exp_files, inargs,
                                     experiment, var_abbrev, time_selector,
                                     inargs.scale_factor[plotnum],
                                     basins_to_plot, cmap)
            metadata_dict[exp_files[0]] = file_history[0]

    plt.savefig(inargs.outfile, bbox_inches='tight')
    log_text = cmdprov.new_log(infile_history=metadata_dict, git_repo=repo_dir)
    log_file = re.sub('.png', '.met', inargs.outfile)
    cmdprov.write_log(log_file, log_text)
Пример #9
0
def main(inargs):
    """Run the program."""

    time_constraints = {'historical-rcp85': gio.get_time_constraint(inargs.rcp_time),
                        'historical': gio.get_time_constraint(inargs.historical_time),
                        'GHG-only': gio.get_time_constraint(inargs.historical_time),
                        'AA-only': gio.get_time_constraint(inargs.historical_time),
                        '1pctCO2': gio.get_time_constraint(inargs.pctCO2_time)}

    fig = plt.figure(figsize=[20, 7])
    ax1 = fig.add_subplot(1, 2, 1)
    ax2 = fig.add_subplot(1, 2, 2)

    for experiment_num, experiment in enumerate(inargs.experiment_list):
        time_constraint = time_constraints[experiment]
        ita_cube_list = iris.cube.CubeList([])
        eei_cube_list = iris.cube.CubeList([])
        for model_num in range(0, len(inargs.toa_files[experiment_num])):
            toa_file = inargs.toa_files[experiment_num][model_num]
            thetao_sh_file = inargs.thetao_sh_files[experiment_num][model_num]
            thetao_nh_file = inargs.thetao_nh_files[experiment_num][model_num]

            ita_cube, ita_history = calc_ita(thetao_sh_file, thetao_nh_file, time_constraint, model_num)
            ita_cube_list.append(ita_cube)

            eei_cube, eei_history = calc_eei(toa_file, time_constraint, model_num)
            eei_cube_list.append(eei_cube)

        ita_ensemble_agg = ensemble_aggregation(ita_cube_list, inargs.ensagg)
        ita_ensemble_spread = ensemble_aggregation(ita_cube_list, 'percentile')    
        eei_ensemble_agg = ensemble_aggregation(eei_cube_list, inargs.ensagg)
    
        temporal_plot(ax1, ita_ensemble_agg, ita_ensemble_spread, experiment)
        eei_plot(ax2, eei_ensemble_agg, ita_ensemble_agg, experiment)   

    ylim = temporal_plot_features(ax1)
    eei_plot_features(ax2, ylim)
    
    dpi = inargs.dpi if inargs.dpi else plt.savefig.__globals__['rcParams']['figure.dpi']
    print('dpi =', dpi)
    plt.savefig(inargs.outfile, bbox_inches='tight', dpi=dpi)

    infile_metadata = {thetao_nh_file: ita_history, toa_file: eei_history}
    log_text = cmdprov.new_log(infile_history=infile_metadata, git_repo=repo_dir)
    log_file = re.sub('.png', '.met', inargs.outfile)
    cmdprov.write_log(log_file, log_text)
Пример #10
0
def main(inargs):
    """Run the program."""

    df = pd.read_csv(inargs.infile)
    df.set_index(df['model'], drop=True, inplace=True)
    #df.set_index(df['model'] + ' (' + df['run'] + ')', drop=True, inplace=True)
    x = np.arange(df.shape[0])
    ncmip5 = df['project'].value_counts()['cmip5']

    df_ohc = df[[
        'OHC (J yr-1)', 'thermal OHC (J yr-1)', 'barystatic OHC (J yr-1)'
    ]]

    sec_in_year = 365.25 * 24 * 60 * 60
    earth_surface_area = 5.1e14
    df_ohc = (df_ohc / sec_in_year) / earth_surface_area
    df_ohc = df_ohc.rename(
        columns={
            "OHC (J yr-1)":
            "change in OHC ($dH/dt$)",
            "thermal OHC (J yr-1)":
            "change in OHC temperature component ($dH_T/dt$)",
            "barystatic OHC (J yr-1)":
            "change in OHC barystatic component ($dH_m/dt$)"
        })

    df_ohc.plot.bar(figsize=(18, 6),
                    color=['#272727', 'tab:red', 'tab:blue'],
                    width=0.9,
                    zorder=2)
    plt.axhspan(0.4, 1.0, color='0.95', zorder=1)
    plt.axvline(x=ncmip5 - 0.5, color='0.5', linewidth=2.0)
    units = 'equivalent planetary energy imbalance (W m$^{-2}$)'
    plt.ylabel(units)
    plt.axvline(x=x[0] - 0.5, color='0.5', linewidth=0.1)
    for val in x:
        plt.axvline(x=val + 0.5, color='0.5', linewidth=0.1)

    quartiles = get_quartiles(df_ohc, "change in OHC ($dH/dt$)", df['project'],
                              units)

    plt.savefig(inargs.outfile, bbox_inches='tight', dpi=400)
    log_file = re.sub('.png', '.met', inargs.outfile)
    log_text = cmdprov.new_log(git_repo=repo_dir, extra_notes=quartiles)
    cmdprov.write_log(log_file, log_text)
def main(inargs):
    """Run the program."""
    warnings.filterwarnings('ignore')
    assert inargs.mask[1] in ['ocean', 'land'], "domain must be ocean or land"
    cube = read_data(inargs.infile, inargs.month)
    cube = apply_mask(cube, inargs.mask[0], inargs.mask[1])
    #pdb.set_trace()
    cube = convert_pr_units(cube)
    clim = cube.collapsed('time', iris.analysis.MEAN)
    plot_data(clim,
              inargs.month,
              gridlines=inargs.gridlines,
              levels=inargs.cbar_levs)
    plt.savefig(inargs.outfile)
    new_log = cmdprov.new_log(
        infile_history={inargs.infile: cube.attributes['history']})
    fname, extension = inargs.outfile.split('.')
    cmdprov.write_log(fname + '.txt', new_log)
Пример #12
0
def main(inargs):
    """Run the program."""

    inlogs = {}
    my_log = cmdprov.new_log()
    cube = read_data(inargs.infile, inargs.month)
    inlogs[inargs.infile] = cube.attributes['history']  #add data history
    cube = convert_pr_units(cube)
    if type(inargs.mask) is list:
        assert inargs.mask[1] == 'land' or inargs.mask[
            1] == 'ocean', 'mask should specify land or ocean'
        cube = mask_data(cube, inargs.mask[0], inargs.mask[1])
        inlogs[inargs.mask[0]] = cube.attributes['history']  #add mask history
    clim = cube.collapsed('time', iris.analysis.MEAN)
    plot_data(clim, inargs.month, inargs.gridlines, inargs.cbar_levels)
    plt.savefig(inargs.outfile + '.png')

    my_log = cmdprov.new_log(infile_history=inlogs, git_repo='.')
    cmdprov.write_log(inargs.outfile + '.log', my_log)
def main(inargs):
    """Run the program."""

    dset = xr.open_dataset(inargs.pr_file)
    
    clim = dset['pr'].groupby('time.season').mean('time', keep_attrs=True)
    clim = convert_pr_units(clim)

    if inargs.mask:
        sftlf_file, realm = inargs.mask
        clim = apply_mask(clim, sftlf_file, realm)

    create_plot(clim, dset.attrs['model_id'], inargs.season,
                gridlines=inargs.gridlines, levels=inargs.cbar_levels)
    plt.savefig(inargs.output_file, dpi=200)

    new_log = cmdprov.new_log(infile_history={inargs.pr_file: dset.attrs['history']})
    fname, extension = inargs.output_file.split('.')
    cmdprov.write_log(fname+'.txt', new_log)
Пример #14
0
def main(inargs):
    """Run the program."""

    cube = read_data(inargs.infile, inargs.month)   
    cube = convert_pr_units(cube)
    clim = cube.collapsed('time', iris.analysis.MEAN)

    if inargs.mask:
        sftlf_file, realm = inargs.mask
        sftlf_cube = iris.load_cube(sftlf_file, 'land_area_fraction')
        clim = apply_mask(clim, sftlf_cube, realm)

    plot_data(clim, inargs.month, gridlines=inargs.gridlines,
              levels=inargs.cbar_levels)
    plt.savefig(inargs.outfile)
	
    new_log = cmdprov.new_log(infile_history={inargs.infile: cube.attributes['history']})
    fname, extension = inargs.outfile.split('.')
    cmdprov.write_log(fname+'.txt', new_log)
Пример #15
0
def main(args):
    """Run the program."""

    experiments = ['GHG-only', 'AA-only', 'historical']
    letters = ['(a) ', '(b) ', '(c) ']
    file_dict, nfiles = sort_files(args)

    start_year = args.time_bounds[0][0:4]
    end_year = args.time_bounds[1][0:4]

    var_names = {
        'precipitation_flux': 'precipitation',
        'water_evapotranspiration_flux': 'evaporation',
        'precipitation_minus_evaporation_flux': 'P-E'
    }
    var_name = var_names[args.var]
    ylabel = f"time integrated {var_name} anomaly, {start_year}-{end_year} (kg)"

    fig, axes = plt.subplots(1, 3, figsize=(18, 6))
    time_constraint = gio.get_time_constraint(args.time_bounds)
    for plotnum, exp in enumerate(experiments):
        data = []
        for modelnum, infile in enumerate(file_dict[exp]):
            cube, history = get_data(infile, args.var, time_constraint)
            try:
                model = cube.attributes['model_id']
            except KeyError:
                model = cube.attributes['source_id']
            data.append([model, 'Atlantic', cube.data[0]])
            data.append([model, 'Indian', cube.data[2]])
            data.append([model, 'Pacific', cube.data[1]])
            data.append([model, 'Land', cube.data[5]])
        df = pd.DataFrame(data, columns=['model', 'basin', ylabel])
        title = letters[plotnum] + exp
        plot_data(axes[plotnum], df, title, ylabel, model_dots=args.dots)

    plt.savefig(args.outfile, bbox_inches='tight')

    metadata_dict = {file_dict['historical'][-1]: history}
    log_text = cmdprov.new_log(infile_history=metadata_dict, git_repo=repo_dir)
    log_file = re.sub('.png', '.met', args.outfile)
    cmdprov.write_log(log_file, log_text)
Пример #16
0
def main(inargs):
    """Run the program."""

    metadata_dict = {}

    # Read data
    control_cube, control_history = read_data(inargs.control_files, inargs.variable,
                                              inargs.grid_point, convert_to_annual=inargs.annual)
    metadata_dict[inargs.control_files[0]] = control_history
    coord_names = [coord.name() for coord in control_cube.dim_coords]
    time_var = coord_names[0]
    assert time_var in ['time', 'year']

    experiment_cube, experiment_history = read_data(inargs.experiment_files, inargs.variable,
                                                    inargs.grid_point, convert_to_annual=inargs.annual)
    metadata_dict[inargs.experiment_files[0]] = experiment_history

    if inargs.dedrifted_files:
        dedrifted_cube, dedrifted_history = read_data(inargs.dedrifted_files, inargs.variable,
                                                      inargs.grid_point, convert_to_annual=inargs.annual)
        metadata_dict[inargs.dedrifted_files[0]] = dedrifted_history   

    if inargs.coefficient_file:
        cubic_data, a_cube = cubic_fit(inargs.coefficient_file, inargs.grid_point,
                                       control_cube.coord(time_var).points)
        #TODO: coeff metadata    

    # Time axis adjustment
    if time_var == 'time':
        first_data_cube = iris.load_cube(inargs.experiment_files[0], gio.check_iris_var(inargs.variable))
        if inargs.grid_point:
            first_data_cube = select_point(first_data_cube, inargs.grid_point, timeseries=True)
        if inargs.annual:
            first_data_cube = timeseries.convert_to_annual(first_data_cube)
        time_diff, branch_time, new_time_unit = remove_drift.time_adjustment(first_data_cube, control_cube, 'annual',
                                                                             branch_time=inargs.branch_time)
        print(f'branch time: {branch_time - 182.5}')
        time_coord = experiment_cube.coord('time')
        time_coord.convert_units(new_time_unit)
        experiment_time_values = time_coord.points.astype(numpy.float32) - time_diff
    elif time_var == 'year':
        if not inargs.branch_year == None:
            branch_year = inargs.branch_year
        else:
            if not inargs.control_time_units:
                control_time_units = gio.fix_time_descriptor(experiment_cube.attributes['parent_time_units'])
            else:
                control_time_units = inargs.control_time_units.replace("_", " ")
            branch_time = experiment_cube.attributes['branch_time_in_parent']
            branch_datetime = cf_units.num2date(branch_time, control_time_units, cf_units.CALENDAR_STANDARD)
            branch_year = branch_datetime.year
        print(f'branch year: {branch_year}')
        experiment_time_values = numpy.arange(branch_year, branch_year + experiment_cube.shape[0])

    # Plot
    fig = plt.figure(figsize=[14, 7])
    plt.plot(control_cube.coord(time_var).points, control_cube.data, label='control')
    plt.plot(experiment_time_values, experiment_cube.data, label='experiment')
    if inargs.dedrifted_files:
        plt.plot(experiment_time_values, dedrifted_cube.data, label='dedrifted')
    if inargs.coefficient_file:
        plt.plot(control_cube.coord(time_var).points, cubic_data, label='cubic fit')
    if inargs.outlier_threshold:
        data, outlier_idx = timeseries.outlier_removal(control_cube.data, inargs.outlier_threshold)
        plt.plot(control_cube.coord(time_var).points[outlier_idx], control_cube.data[outlier_idx],
                 marker='o', linestyle='none', color='r', alpha=0.3)
    if inargs.ylim:
        ymin, ymax = inargs.ylim
        plt.ylim(ymin, ymax)
    plt.ylabel(f"{gio.check_iris_var(inargs.variable)} ({control_cube.units})")
    if time_var == 'time':
        plt.xlabel(str(new_time_unit))
    else:
        plt.xlabel('control run year')
    plt.legend()
    if inargs.grid_point:
        title = get_title(inargs.control_files[0], inargs.variable, inargs.grid_point)
        plt.title(title)

    # Save output
    plt.savefig(inargs.outfile, bbox_inches='tight')
    
    log_text = cmdprov.new_log(infile_history=metadata_dict, git_repo=repo_dir)
    log_file = re.sub('.png', '.met', inargs.outfile)
    cmdprov.write_log(log_file, log_text)
def main(args):
    """Run the program."""

    infiles = sorted(args.infiles)

    var_names = {
        'precipitation_flux': 'precipitation',
        'water_evapotranspiration_flux': 'evaporation',
        'precipitation_minus_evaporation_flux': 'P-E'
    }
    var_name = var_names[args.var]
    ylabel = f"annual mean {var_name} (kg)"

    fig, axes = plt.subplots(1, 2, figsize=(12, 6))

    region_data = []
    basin_data = []
    nfiles = len(infiles)
    print(f"Number models: {nfiles}")
    for modelnum in range(nfiles):
        cube, history = gio.combine_files(infiles[modelnum], args.var)
        cube = cube.collapsed('time', iris.analysis.MEAN)
        cube.remove_coord('time')
        try:
            model = cube.attributes['model_id']
        except KeyError:
            model = cube.attributes['source_id']

        # cube(time, pereg, basin)
        #  pereg: SH_precip SH_evap tropical_precip NH_evap NH_precip globe
        #  basin: atlantic pacific indian arctic marginal_seas land ocean globe

        region_data.append([model, 'SH-P', cube.data[0, -1]])
        region_data.append([model, 'SH-E', cube.data[1, -1]])
        region_data.append([model, 'T-P', cube.data[2, -1]])
        region_data.append([model, 'NH-E', cube.data[3, -1]])
        region_data.append([model, 'NH-P', cube.data[4, -1]])

        basin_data.append([model, 'Atlantic', cube.data[-1, 0]])
        basin_data.append([model, 'Indian', cube.data[-1, 2]])
        basin_data.append([model, 'Pacific', cube.data[-1, 1]])
        basin_data.append([model, 'Land', cube.data[-1, 5]])

    region_df = pd.DataFrame(region_data,
                             columns=['model', 'P-E region', ylabel])
    basin_df = pd.DataFrame(basin_data, columns=['model', 'basin', ylabel])
    plot_data(axes[0],
              region_df,
              'P-E region',
              ylabel,
              '(a) meridional climatology',
              model_dots=args.dots)
    plot_data(axes[1],
              basin_df,
              'basin',
              ylabel,
              '(b) zonal climatology',
              model_dots=args.dots)

    plt.savefig(args.outfile, bbox_inches='tight')

    metadata_dict = {infiles[-1]: history[0]}
    log_text = cmdprov.new_log(infile_history=metadata_dict, git_repo=repo_dir)
    log_file = re.sub('.png', '.met', args.outfile)
    cmdprov.write_log(log_file, log_text)
Пример #18
0
def main(inargs):
    """Run the program."""

    volo_models = [
        'CNRM-CM6-1', 'CNRM-ESM2-1', 'E3SM-1-0', 'E3SM-1-1', 'EC-Earth3',
        'EC-Earth3-Veg', 'IPSL-CM6A-LR'
    ]
    cpocean_dict = {'HadGEM3-GC31-LL': 3991.867957, 'UKESM1-0-LL': 3991.867957}
    rhozero_dict = {'HadGEM3-GC31-LL': 1026, 'UKESM1-0-LL': 1026}
    if inargs.colors:
        colors = iter(inargs.colors)
    else:
        colors = iter(plt.cm.rainbow(numpy.linspace(0, 1, len(inargs.models))))

    if inargs.linestyles:
        styles = iter(inargs.linestyles)
    else:
        styles = iter([
            '-', '--', ':', '-', '--', ':', '-', '--', ':', '-', '--', ':',
            '-', '--', ':'
        ])

    if inargs.manual_files:
        with open(inargs.manual_files, 'r') as reader:
            manual_files = yaml.load(reader)
    else:
        manual_files = {}

    fig = plt.figure(figsize=[14, 8])
    nrows = 2
    ncols = 2
    ax_ohc = fig.add_subplot(nrows, ncols, 1)
    ax_ohcd = fig.add_subplot(nrows, ncols, 2)
    ax_masso = fig.add_subplot(nrows, ncols, 3)
    ax_massod = fig.add_subplot(nrows, ncols, 4)
    #ax_massa = fig.add_subplot(nrows, ncols, 5)
    #ax_massad = fig.add_subplot(nrows, ncols, 6)

    max_time = 0
    count = 0
    for model, run in zip(inargs.models, inargs.runs):
        print(model, run)
        extra_log.append(['# ' + model + ', ' + run])

        # OHC and masso
        if model in volo_models:
            volo = read_global_variable(model, 'volo', run, manual_files)
            rhozero = 1026
            masso = volo * rhozero
        else:
            masso = read_global_variable(model, 'masso', run, manual_files)

        thetaoga = read_global_variable(model, 'thetaoga', run, manual_files)
        thetaoga = gio.temperature_unit_check(thetaoga, 'K')

        masso, thetaoga = common_time_period(masso, thetaoga)

        cp = cpocean_dict[model] if model in cpocean_dict.keys() else 4000
        ohc = masso.data * thetaoga.data * cp
        if inargs.ohc_outlier:
            ohc, outlier_idx = timeseries.outlier_removal(
                ohc, inargs.ohc_outlier)
            if outlier_idx:
                print('%s (%s) outliers:' % (model, run), outlier_idx)

        if inargs.runmean_window:
            ohc = timeseries.runmean(ohc, inargs.runmean_window)
            masso = timeseries.runmean(masso.data, inargs.runmean_window)

        ohc_anomaly = ohc - ohc[0]
        masso_anomaly = masso - masso[0]

        ohc_dedrifted = dedrift_data(ohc_anomaly)
        masso_dedrifted = dedrift_data(masso_anomaly)
        ohc_dedrifted_anomaly = ohc_dedrifted - ohc_dedrifted[0]
        masso_dedrifted_anomaly = masso_dedrifted - masso_dedrifted[0]

        label = model  #'%s (%s)' %(model, run)
        color = next(colors)
        style = next(styles)

        ax_ohc.plot(ohc_anomaly, color=color, label=label, linestyle=style)
        record_trend(ohc_anomaly, 'OHC linear trend', 'C/yr')
        ax_ohcd.plot(ohc_dedrifted_anomaly,
                     color=color,
                     label=label,
                     linestyle=style)

        ax_masso.plot(masso_anomaly, color=color, label=label, linestyle=style)
        record_trend(masso_anomaly, 'Mass linear trend', 'kg/yr')
        ax_massod.plot(masso_dedrifted_anomaly,
                       color=color,
                       label=label,
                       linestyle=style)

        if len(ohc) > max_time:
            max_time = len(ohc)

        # massa
        #massa = read_global_variable(model, 'massa', run, manual_files)
        #if inargs.runmean_window:
        #    massa = timeseries.runmean(massa.data, inargs.runmean_window)
        #massa_anomaly = massa - massa[0]
        #massa_dedrifted = dedrift_data(massa_anomaly)
        #massa_dedrifted_anomaly = massa_dedrifted - massa_dedrifted[0]
        #ax_massa.plot(massa_anomaly, color=color, label=label, linestyle=style)
        #record_trend(massa_anomaly, 'Atmospheric water mass linear trend', 'kg/yr')
        #ax_massad.plot(massa_dedrifted_anomaly, color=color, label=label, linestyle=style)

    ax_ohc.plot()

    ax_ohc.set_title('(a) OHC')
    ax_ohc.set_ylabel('OHC anomaly (J)')
    ax_ohc.grid(linestyle=':')
    ax_ohc.ticklabel_format(useOffset=False)
    ax_ohc.yaxis.major.formatter._useMathText = True
    plot_reference_eei(ax_ohc, 0.4, max_time)
    plot_reference_eei(ax_ohc, 0.2, max_time)
    plot_reference_eei(ax_ohc, 0.1, max_time)
    plot_reference_eei(ax_ohc, -0.4, max_time)
    plot_reference_eei(ax_ohc, -0.2, max_time)
    plot_reference_eei(ax_ohc, -0.1, max_time)

    ax_ohcd.set_title('(b) OHC (linear trend removed)')
    ax_ohcd.set_ylabel('OHC anomaly (J)')
    ax_ohcd.grid(linestyle=':')
    ax_ohcd.ticklabel_format(useOffset=False)
    ax_ohcd.yaxis.major.formatter._useMathText = True

    ax_masso.set_title('(c) ocean mass')
    ax_masso.set_xlabel('year')
    ax_masso.set_ylabel('ocean mass anomaly (kg)')
    ax_masso.grid(linestyle=':')
    ax_masso.ticklabel_format(useOffset=False)
    ax_masso.yaxis.major.formatter._useMathText = True
    plot_reference_mass(ax_masso, 0.0018, max_time)
    plot_reference_mass(ax_masso, 0.0009, max_time)
    plot_reference_mass(ax_masso, 0.00045, max_time)
    plot_reference_mass(ax_masso, -0.0018, max_time)
    plot_reference_mass(ax_masso, -0.0009, max_time)
    plot_reference_mass(ax_masso, -0.00045, max_time)

    ax_massod.set_title('(d) ocean mass (linear trend removed)')
    ax_massod.set_xlabel('year')
    ax_massod.set_ylabel('ocean mass anomaly (kg)')
    ax_massod.grid(linestyle=':')
    ax_massod.ticklabel_format(useOffset=False)
    ax_massod.yaxis.major.formatter._useMathText = True
    ax_massod.legend(loc='center left', bbox_to_anchor=(1, 1))

    #ax_massa.set_title('(e) atmos water mass')
    #ax_massa.set_xlabel('year')
    #ax_massa.set_ylabel('kg')
    #ax_massa.grid(linestyle=':')
    #ax_massa.ticklabel_format(useOffset=False)
    #ax_massa.yaxis.major.formatter._useMathText = True

    #ax_massad.set_title('(f) atmos water mass (linear trend removed)')
    #ax_massad.set_xlabel('year')
    #ax_massad.set_ylabel('kg')
    #ax_massad.grid(linestyle=':')
    #ax_massad.ticklabel_format(useOffset=False)
    #ax_massad.yaxis.major.formatter._useMathText = True

    dpi = inargs.dpi if inargs.dpi else plt.savefig.__globals__['rcParams'][
        'figure.dpi']
    print('dpi =', dpi)
    plt.savefig(inargs.outfile, bbox_inches='tight', dpi=dpi)
    log_text = get_log_text(extra_log)
    log_file = re.sub('.png', '.met', inargs.outfile)
    cmdprov.write_log(log_file, log_text)
Пример #19
0
def main(inargs):
    """Run the program."""

    # Process data
    bmin, bmax = inargs.bin_bounds
    bin_edges = numpy.arange(bmin, bmax + inargs.bin_width, inargs.bin_width)
    xvals = (bin_edges[1:] + bin_edges[:-1]) / 2

    label_counts = initialise_label_counts(inargs.labels)
    ref_datasets = ['EN4']
    hist_dict = collections.OrderedDict()
    metadata_dict = {}
    for groupnum, infiles in enumerate(inargs.data_files):
        label = inargs.labels[groupnum]
        print(label)
        vfile = inargs.volume_files[groupnum]
        bfile = inargs.basin_files[groupnum]
        ref = (label in ref_datasets) and len(inargs.labels) > 1
        vcube, bcube, time_constraint = read_supporting_inputs(vfile,
                                                               bfile,
                                                               inargs,
                                                               ref=ref)
        dcube = combine_infiles(infiles, inargs, time_constraint)
        metadata_dict = update_metadata(metadata_dict, vfile, vcube, bfile,
                                        bcube, infiles[0], dcube)
        voldist_timeseries = numpy.array([])
        V_timeseries = numpy.array([])
        for time_slice in dcube.slices_over('time'):
            df = create_df(time_slice,
                           inargs.variable,
                           vcube,
                           bcube,
                           basin=inargs.basin,
                           abort=operator.not_(inargs.no_abort))
            voldist, edges, binnum = scipy.stats.binned_statistic(
                df[inargs.variable].values,
                df['volume'].values,
                statistic='sum',
                bins=bin_edges)
            V = voldist.cumsum()
            voldist_timeseries = numpy.vstack([
                voldist_timeseries, voldist
            ]) if voldist_timeseries.size else voldist
            V_timeseries = numpy.vstack([V_timeseries, V
                                         ]) if V_timeseries.size else V
        for metric in inargs.metrics:
            hist_dict[(label, metric, label_counts[label])] = calc_metric(
                voldist_timeseries, V_timeseries, metric)
        label_counts[label] = label_counts[label] + 1

    # Plot individual model/run results
    nrows, ncols = inargs.subplot_config
    fig, axes = plt.subplots(nrows, ncols, figsize=(9 * ncols, 6 * nrows))
    for plotnum, metric in enumerate(inargs.metrics):
        ax = axes.flatten()[plotnum] if type(axes) == numpy.ndarray else axes

        labelnum = 0
        labelled = []
        for key, yvals in hist_dict.items():
            label, dict_metric, count = key
            if dict_metric == metric:
                if not ((label in ref_datasets) and
                        (metric not in ['dV/dT', 'dV/dS'])):
                    plot_label = label if not label in labelled else None
                    ax.plot(xvals,
                            yvals,
                            'o-',
                            color=inargs.colors[labelnum],
                            label=plot_label)
                    labelled.append(label)
                labelnum = labelnum + 1

        ax.set_title(metric_names[metric])
        xlabel, ylabel = get_labels(time_slice, inargs.variable, metric)
        ax.set_xlabel(xlabel)
        ax.set_ylabel(ylabel)
        ax.grid(True)
        set_axis_limits(ax, plotnum, inargs.xlim, inargs.ylim)
        if metric in ['dVdt/dVdT', 'dVdt/dVdS']:
            ax.legend(loc=2)
        else:
            ax.legend(loc=1)
        plt.ticklabel_format(style='sci',
                             axis='y',
                             scilimits=(0, 0),
                             useMathText=True)
        ax.yaxis.major.formatter._useMathText = True

    title = get_title(dcube, inargs.basin)
    plt.suptitle(title)

    # Save output
    dpi = inargs.dpi if inargs.dpi else plt.savefig.__globals__['rcParams'][
        'figure.dpi']
    print('dpi =', dpi)
    plt.savefig(inargs.outfile, bbox_inches='tight', dpi=dpi)

    # Metadata
    log_text = cmdprov.new_log(infile_history=metadata_dict, git_repo=repo_dir)
    log_file = re.sub('.png', '.met', inargs.outfile)
    cmdprov.write_log(log_file, log_text)
Пример #20
0
def main(inargs):
    """Run the program."""

    metadata_dict = {}
    ensemble_dict = {
        'historical': iris.cube.CubeList([]),
        'historicalGHG': iris.cube.CubeList([]),
        'historicalMisc': iris.cube.CubeList([])
    }
    depth_constraint = gio.iris_vertical_constraint(inargs.min_depth,
                                                    inargs.max_depth)
    new_grid = make_grid(
        numpy.arange(inargs.min_depth + 0.5, inargs.max_depth, 1))
    experiment_list = []

    for infile in inargs.infiles:
        cube = iris.load_cube(
            infile,
            gio.check_iris_var(inargs.var) & depth_constraint)
        depth_cube = collapse_dims(cube, inargs.dimagg)

        experiment = cube.attributes['experiment_id']
        experiment_list.append(experiment)

        ensemble_number = experiment_list.count(experiment)
        new_aux_coord = iris.coords.AuxCoord(ensemble_number,
                                             long_name='ensemble_member',
                                             units='no_unit')
        depth_cube.add_aux_coord(new_aux_coord)

        if inargs.regrid or inargs.ensagg:
            new_depth_cube = regrid(depth_cube, new_grid)
        else:
            new_depth_cube = depth_cube
        ensemble_dict[experiment].append(new_depth_cube)

    fig = plt.figure(figsize=[10, 30])
    enswidth = 2.0
    ilinewidth = enswidth * 0.25 if inargs.ensagg else enswidth
    for experiment in ['historical', 'historicalGHG', 'historicalMisc']:
        for num, cube in enumerate(ensemble_dict[experiment]):
            label = experiment if (num == 1) and not inargs.ensagg else False
            plot_data(cube, experiment, label=label, linewidth=ilinewidth)
        if inargs.ensagg:
            ensagg_cube = ensemble_aggregate(ensemble_dict[experiment],
                                             inargs.ensagg)
            plot_data(ensagg_cube, experiment, label=experiment, linewidth=2.0)

    plt.gca().invert_yaxis()
    plt.ylim([inargs.max_depth, inargs.min_depth])
    plt.legend()
    if inargs.xbounds:
        xmin, xmax = inargs.xbounds
        plt.xlim([xmin, xmax])
    plt.grid(True)
    plt.xlabel(str(cube.units))
    plt.ylabel('Depth (m)')
    plt.title('Excess heat storage, 1861-2005')

    # Save output
    dpi = inargs.dpi if inargs.dpi else plt.savefig.__globals__['rcParams'][
        'figure.dpi']
    print('dpi =', dpi)
    plt.savefig(inargs.outfile, bbox_inches='tight', dpi=dpi)

    log_text = cmdprov.new_log(infile_history=metadata_dict, git_repo=repo_dir)
    log_file = re.sub('.png', '.met', inargs.outfile)
    cmdprov.write_log(log_file, log_text)
def main(inargs):
    """Run the program."""

    fig, ax = plt.subplots(figsize=[14, 7])
    nexperiments = len(inargs.hist_files) + len(inargs.rcp_files) + len(
        inargs.control_files)

    # Plot historical data
    for infiles in inargs.hist_files:
        data_dict, experiment, ylabel, metadata_dict = plot_file(
            infiles, inargs.hist_time, inargs, nexperiments)

    # Plot control data
    if inargs.control_files:
        assert inargs.hist_files, 'Control plot requires branch time information from historical files'
        ref_cube = data_dict.popitem()[1]
        if inargs.full_control:
            time_bounds = None
        else:
            plot_start_time = inargs.hist_time[0]
            plot_end_time = inargs.rcp_time[
                -1] if inargs.rcp_files else inargs.hist_time[-1]
            time_bounds = [plot_start_time, plot_end_time]
        for infiles in inargs.control_files:
            data_dict, experiment, ylabel, metadata_dict = plot_file(
                infiles,
                time_bounds,
                inargs,
                nexperiments,
                ref_cube=ref_cube,
                branch_index=inargs.branch_index,
                branch_time=inargs.branch_time)

    # Plot rcp data
    for infiles in inargs.rcp_files:
        data_dict, experiment, ylabel, metadata_dict = plot_file(
            infiles, inargs.rcp_time, inargs, nexperiments)

    if inargs.title:
        plt.title(inargs.title)
    else:
        title = get_title(inargs.var, experiment, nexperiments)
        plt.title(title)

    if inargs.ylim:
        ymin, ymax = inargs.ylim
        plt.ylim(ymin, ymax)

    #plt.ylim(-4e+23, 8e+23)

    if inargs.scientific:
        plt.ticklabel_format(style='sci',
                             axis='y',
                             scilimits=(0, 0),
                             useMathText=True,
                             useOffset=False)
        ax.yaxis.major.formatter._useMathText = True
    ax.set_ylabel(ylabel)
    ax.yaxis.major.formatter._useOffset = False

    if inargs.zero_line:
        plt.axhline(y=0, color='0.5', linestyle='--')

    if inargs.legloc:
        ax.legend(loc=inargs.legloc)
    else:
        box = ax.get_position()
        ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])
        ax.legend(loc='center left', bbox_to_anchor=(1.0, 0.5))

    ymin, ymax = plt.ylim()
    print('ymin:', ymin)
    print('ymax:', ymax)

    dpi = inargs.dpi if inargs.dpi else plt.savefig.__globals__['rcParams'][
        'figure.dpi']
    print('dpi =', dpi)
    plt.savefig(inargs.outfile, bbox_inches='tight', dpi=dpi)

    fname, extension = inargs.outfile.split('.')
    new_log = cmdprov.new_log(infile_history=metadata_dict, git_repo=repo_dir)
    cmdprov.write_log(fname + '.met', new_log)
def main(inargs):
    """Run the program."""

    time_constraints = {
        'historical': gio.get_time_constraint(inargs.hist_time),
        'historical-rcp85': gio.get_time_constraint(inargs.hist_time),
        'historicalGHG': gio.get_time_constraint(inargs.hist_time),
        'historicalMisc': gio.get_time_constraint(inargs.hist_time),
        '1pctCO2': gio.get_time_constraint(inargs.pctCO2_time)
    }

    file_variables = ['rndt', 'hfds', 'ohc']
    column_list = []
    ylabel = get_ylabel(inargs.toa_files, inargs.ohu_files, inargs.ohc_files)
    nregions = len(inargs.regions)
    figtype = '.' + inargs.outfile.split('.')[-1]

    for var_index, var_files in enumerate(
        [inargs.toa_files, inargs.ohu_files, inargs.ohc_files]):
        var = file_variables[var_index]
        for model_files in var_files:
            for exp_index, file_group in enumerate([
                    model_files[i:i + nregions]
                    for i in range(0, len(model_files), nregions)
            ]):
                experiment = inargs.experiments[exp_index]
                time_constraint = time_constraints[experiment]
                values, model, history = calc_hemispheric_heat(
                    file_group, inargs.regions, var, experiment,
                    time_constraint)
                column_list = generate_heat_row_dicts(column_list, values,
                                                      inargs.regions, model,
                                                      experiment, var, ylabel)
    heat_df = pandas.DataFrame(column_list)

    write_values(heat_df, ylabel, file_variables, inargs.experiments,
                 inargs.regions, inargs.outfile, figtype)

    color_list = get_colors(inargs.experiments)
    seaborn.set_style("whitegrid")

    fig, ax = plt.subplots(figsize=(16, 10))
    g = seaborn.catplot(x="hemisphere",
                        y=ylabel,
                        hue="experiment",
                        col="variable",
                        data=heat_df,
                        kind="bar",
                        palette=color_list)
    g.set_titles("{col_name}")

    plt.ticklabel_format(style='sci',
                         axis='y',
                         scilimits=(0, 0),
                         useMathText=True)
    ax.yaxis.major.formatter._useMathText = True

    dpi = inargs.dpi if inargs.dpi else plt.savefig.__globals__['rcParams'][
        'figure.dpi']
    print('dpi =', dpi)
    plt.savefig(inargs.outfile, bbox_inches='tight', dpi=dpi)

    log_text = cmdprov.new_log(infile_history={file_group[-1]: history},
                               git_repo=repo_dir)
    log_file = re.sub(figtype, '.met', inargs.outfile)
    cmdprov.write_log(log_file, log_text)
Пример #23
0
def main(inargs):
    """Run the program."""

    df = pd.read_csv(inargs.infile)
    #df.set_index(df['model'] + ' (' + df['run'] + ')', drop=True, inplace=True)
    df.set_index(df['model'], drop=True, inplace=True)
    if inargs.cmip_line:
        cmip_line = inargs.cmip_line
    else:
        ncmip5 = df['project'].value_counts()['cmip5']
        cmip_line = ncmip5 - 0.5
        print('x-value for CMIP dividing line:', cmip_line)

    df['atmos energy leakage (J yr-1)'] = df['netTOA (J yr-1)'] - df[
        'hfdsgeou (J yr-1)']
    df['ocean energy leakage (J yr-1)'] = df['hfdsgeou (J yr-1)'] - df[
        'thermal OHC (J yr-1)']
    df['total energy leakage (J yr-1)'] = df['netTOA (J yr-1)'] - df[
        'thermal OHC (J yr-1)']

    df_leakage = df[[
        'total energy leakage (J yr-1)', 'atmos energy leakage (J yr-1)',
        'ocean energy leakage (J yr-1)'
    ]]
    df_leakage = df_leakage.dropna(axis=0, how='all')
    x = np.arange(df_leakage.shape[0])

    sec_in_year = 365.25 * 24 * 60 * 60
    earth_surface_area = 5.1e14
    df_leakage = (df_leakage / sec_in_year) / earth_surface_area
    df_leakage = df_leakage.rename(
        columns={
            "total energy leakage (J yr-1)":
            "total leakage ($dQ_r/dt - dH_T/dt$)",
            "ocean energy leakage (J yr-1)":
            "ocean leakage ($dQ_h/dt - dH_T/dt$)",
            "atmos energy leakage (J yr-1)":
            "non-ocean leakage ($dQ_r/dt - dQ_h/dt$)"
        })

    if inargs.split_axes:
        fig, (ax1, ax2,
              ax3) = plt.subplots(3,
                                  1,
                                  sharex=True,
                                  figsize=(18, 8),
                                  gridspec_kw={'height_ratios': [1, 5, 1]})
        ax1.spines['bottom'].set_visible(False)
        ax2.spines['bottom'].set_visible(False)
        ax1.tick_params(axis='x', which='both', bottom=False)
        ax2.tick_params(axis='x', which='both', bottom=False)
        ax2.spines['top'].set_visible(False)
        ax3.spines['top'].set_visible(False)
        ax1.set_ylim(30, 45)
        ax2.set_ylim(-1.5, 2.5)
        ax3.set_ylim(-45, -30)

        df_leakage.plot(ax=ax3,
                        kind='bar',
                        color=['tab:olive', 'tab:green', 'tab:blue'],
                        width=0.9,
                        legend=False)
        df_leakage.plot(ax=ax2,
                        kind='bar',
                        color=['tab:olive', 'tab:green', 'tab:blue'],
                        width=0.9,
                        legend=False)
        df_leakage.plot(ax=ax1,
                        kind='bar',
                        color=['tab:olive', 'tab:green', 'tab:blue'],
                        width=0.9)
        for tick in ax3.get_xticklabels():
            tick.set_rotation(90)

        plt.subplots_adjust(hspace=0.15)

        ax1.axvline(x=cmip_line, color='0.5', linewidth=2.0)
        ax2.axvline(x=cmip_line, color='0.5', linewidth=2.0)
        ax3.axvline(x=cmip_line, color='0.5', linewidth=2.0)
        ax2.axhline(y=-0.5, color='0.5', linewidth=0.5, linestyle='--')
        ax2.axhline(y=0.5, color='0.5', linewidth=0.5, linestyle='--')
        ax2.set_ylabel('$W \; m^{-2}$')

        ax1.axvline(x=x[0] - 0.5, color='0.5', linewidth=0.1)
        ax2.axvline(x=x[0] - 0.5, color='0.5', linewidth=0.1)
        ax3.axvline(x=x[0] - 0.5, color='0.5', linewidth=0.1)
        for val in x:
            ax1.axvline(x=val + 0.5, color='0.5', linewidth=0.1)
            ax2.axvline(x=val + 0.5, color='0.5', linewidth=0.1)
            ax3.axvline(x=val + 0.5, color='0.5', linewidth=0.1)

    else:

        df_leakage.plot.bar(figsize=(18, 6),
                            color=['tab:olive', 'tab:green', 'tab:blue'],
                            width=0.9,
                            zorder=2)
        plt.axvline(x=cmip_line, color='0.5', linewidth=2.0)
        plt.axhspan(0.4, 1.0, color='0.95', zorder=1)
        plt.axhspan(-0.4, -1.0, color='0.95', zorder=1)
        units = 'equivalent planetary energy imbalance (W m$^{-2}$)'
        plt.ylabel(units)
        plt.axvline(x=x[0] - 0.5, color='0.5', linewidth=0.1)
        for val in x:
            plt.axvline(x=val + 0.5, color='0.5', linewidth=0.1)
        plt.ylim(-1.5, 2.5)

    column_names = [
        "total leakage ($dQ_r/dt - dH_T/dt$)",
        "ocean leakage ($dQ_h/dt - dH_T/dt$)",
        "non-ocean leakage ($dQ_r/dt - dQ_h/dt$)"
    ]
    quartiles = get_quartiles(df_leakage, column_names, df['project'], units)

    plt.savefig(inargs.outfile, bbox_inches='tight', dpi=400)
    log_file = re.sub('.png', '.met', inargs.outfile)
    log_text = cmdprov.new_log(git_repo=repo_dir, extra_notes=quartiles)
    cmdprov.write_log(log_file, log_text)
def main(inargs):
    """Run the program."""

    plot_vars, plot_files = get_plot_vars(inargs)
    nvars = len(plot_vars)
    nrows, ncols = grid_configs[nvars]

    fig = plt.figure(figsize=[11 * ncols, 7 * nrows])
    gs = gridspec.GridSpec(nrows, ncols, wspace=0.27, hspace=0.25)
    axes = []
    for index in range(nvars):
        axes.append(plt.subplot(gs[index]))

    time_constraints = {
        'historical-rcp85': gio.get_time_constraint(inargs.rcp_time),
        'historical': gio.get_time_constraint(inargs.historical_time),
        'GHG-only': gio.get_time_constraint(inargs.historical_time),
        'AA-only': gio.get_time_constraint(inargs.historical_time),
        '1pctCO2': gio.get_time_constraint(inargs.pctCO2_time)
    }

    for experiment_num, experiment in enumerate(inargs.experiment_list):
        time_constraint = time_constraints[experiment]
        ensemble_agg_dict = {}
        ensemble_spread_dict = {}
        for var_index, var_files in enumerate(plot_files):
            var = plot_vars[var_index]
            cube_list = iris.cube.CubeList([])
            for model_num, model_files in enumerate(var_files):
                start = experiment_num * 2
                sh_file, nh_file = model_files[start:start + 2]
                value, history = calc_hemispheric_value(
                    sh_file, nh_file, inargs.metric, var, time_constraint,
                    model_num)
                if model_num == 0:
                    first_model = value.attributes['model_id']
                cube_list.append(value)
                if inargs.individual:
                    plt.sca(axes[var_index])
                    iplt.plot(value,
                              color=exp_colors[experiment],
                              linewidth=0.3)

            ensemble_agg_dict[var], ensemble_spread_dict[
                var] = ensemble_aggregation(cube_list, inargs.ensagg)

            plt.sca(axes[var_index])
            iplt.plot(ensemble_agg_dict[var],
                      label=experiment,
                      color=exp_colors[experiment])
            if inargs.spread and ensemble_spread_dict[var]:
                time_values = ensemble_spread_dict[var][0, ::].coord(
                    'time').points + scale_factors[first_model]
                upper_bound = ensemble_spread_dict[var][0, ::].data
                lower_bound = ensemble_spread_dict[var][-1, ::].data
                iplt.plt.fill_between(time_values,
                                      upper_bound,
                                      lower_bound,
                                      facecolor=exp_colors[experiment],
                                      alpha=0.15)

    if inargs.title:
        plt.suptitle('interhemispheric difference in accumulated heat')
    for index, var in enumerate(plot_vars):
        plt.sca(axes[index])
        if nvars > 1:
            plt.title(titles[var])
        set_plot_features(inargs, axes[index], index, var, nvars)

    dpi = inargs.dpi if inargs.dpi else plt.savefig.__globals__['rcParams'][
        'figure.dpi']
    print('dpi =', dpi)
    plt.savefig(inargs.outfile, bbox_inches='tight', dpi=dpi)

    log_text = cmdprov.new_log(infile_history={nh_file: history},
                               git_repo=repo_dir)
    log_file = re.sub('.png', '.met', inargs.outfile)
    cmdprov.write_log(log_file, log_text)
Пример #25
0
def main(args):
    """Run the program."""

    clim_cube_list, clim_history = get_cube_list(args.clim_files,
                                                 'clim',
                                                 quick=args.quick)
    clim_ensemble_mean, clim_ensemble_agreement = ensemble_stats(
        clim_cube_list)
    clim_ensemble_mean.data = clim_ensemble_mean.data * 86400

    ghg_cube_list, ghg_history = get_cube_list(args.ghg_files,
                                               'anom',
                                               time_bounds=args.time_bounds)
    ghg_ensemble_mean, ghg_ensemble_agreement = ensemble_stats(ghg_cube_list)

    aa_cube_list, aa_history = get_cube_list(args.aa_files,
                                             'anom',
                                             time_bounds=args.time_bounds)
    aa_ensemble_mean, aa_ensemble_agreement = ensemble_stats(aa_cube_list)

    hist_cube_list, hist_history = get_cube_list(args.hist_files,
                                                 'anom',
                                                 time_bounds=args.time_bounds)
    hist_ensemble_mean, hist_ensemble_agreement = ensemble_stats(
        hist_cube_list)

    width = 25
    height = 10
    fig = plt.figure(figsize=[width, height])
    outproj = ccrs.PlateCarree(central_longitude=180.0)

    nrows = 2
    ncols = 2
    ax1 = plt.subplot(nrows, ncols, 1, projection=outproj)
    plot_data(ax1,
              clim_ensemble_mean,
              clim_ensemble_agreement,
              'clim',
              '(a) piControl',
              agreement_bounds=[0.33, 0.66])

    ax2 = plt.subplot(nrows, ncols, 2, projection=outproj)
    plot_data(ax2,
              ghg_ensemble_mean,
              ghg_ensemble_agreement,
              'anom',
              '(b) GHG-only',
              agreement_bounds=[0.33, 0.66],
              clim=clim_ensemble_mean)

    ax3 = plt.subplot(nrows, ncols, 3, projection=outproj)
    plot_data(ax3,
              aa_ensemble_mean,
              aa_ensemble_agreement,
              'anom',
              '(c) AA-only',
              agreement_bounds=[0.33, 0.66],
              clim=clim_ensemble_mean)

    ax4 = plt.subplot(nrows, ncols, 4, projection=outproj)
    plot_data(ax4,
              hist_ensemble_mean,
              hist_ensemble_agreement,
              'anom',
              '(d) historical',
              agreement_bounds=[0.33, 0.66],
              clim=clim_ensemble_mean)

    fig.tight_layout()
    fig.subplots_adjust(wspace=-0.15, hspace=0.2)
    plt.savefig(args.outfile, bbox_inches='tight', dpi=300)
    metadata_dict = {
        args.ghg_files[-1]: ghg_history[-1],
        args.clim_files[-1]: clim_history[-1]
    }
    log_text = cmdprov.new_log(infile_history=metadata_dict, git_repo=repo_dir)
    log_file = re.sub('.png', '.met', args.outfile)
    cmdprov.write_log(log_file, log_text)
def main(inargs):
    """Run the program."""

    df = pd.read_csv(inargs.infile)
    #df.set_index(df['model'] + ' (' + df['run'] + ')', drop=True, inplace=True)
    df.set_index(df['model'], drop=True, inplace=True)
    fig = plt.figure(figsize=[18.5, 21])  # width, height
    gs = GridSpec(3, 2)

    # EEI conservation
    eei_ax = fig.add_subplot(gs[0, 0])
    plot_broken_comparison(eei_ax,
                           df,
                           '(a) planetary energy imbalance',
                           'netTOA (J yr-1)',
                           'thermal OHC (J yr-1)',
                           'W m-2',
                           legend=True)
    handles, labels = get_legend_info(
        eei_ax, df[['netTOA (J yr-1)', 'thermal OHC (J yr-1)']])

    # Ocean energy conservation
    xlims = [(-41.05, -40.82), (-0.55, 0.71)]
    ylims = [(-0.55, 0.66)]
    wspace = hspace = 0.08
    ocean_energy_ax = brokenaxes(xlims=xlims,
                                 ylims=ylims,
                                 hspace=hspace,
                                 wspace=wspace,
                                 subplot_spec=gs[0, 1],
                                 d=0.0)
    #ocean_energy_ax = fig.add_subplot(gs[0, 1])
    plot_broken_comparison(ocean_energy_ax,
                           df,
                           '(b) ocean energy conservation',
                           'hfdsgeou (J yr-1)',
                           'thermal OHC (J yr-1)',
                           'W m-2',
                           xpad=25,
                           ypad=45,
                           broken=True)
    handles, labels = update_legend_info(
        ocean_energy_ax, df[['hfdsgeou (J yr-1)', 'thermal OHC (J yr-1)']],
        handles, labels)

    # Ocean mass conservation
    xlims = [(-7, 4), (472, 474), (492, 495)]
    ylims = [(-0.7, 0.25)]
    hspace = 0.1
    ocean_mass_ax = brokenaxes(xlims=xlims,
                               ylims=ylims,
                               hspace=hspace,
                               subplot_spec=gs[1, 0],
                               d=0.0)
    #ocean_mass_ax = fig.add_subplot(gs[1, 0])
    plot_broken_comparison(ocean_mass_ax,
                           df,
                           '(c) ocean mass conservation',
                           'wfo (kg yr-1)',
                           'masso (kg yr-1)',
                           'kg yr-1',
                           scale_factor=-15,
                           broken=True,
                           xpad=30,
                           ypad=50)
    handles, labels = update_legend_info(
        ocean_mass_ax, df[['wfo (kg yr-1)', 'masso (kg yr-1)']], handles,
        labels)

    # Salt conservation
    xlims = [(-0.73, 0.35), (3.55, 3.7)]
    ylims = [(-0.8, 3.1)]
    hspace = wspace = 0.1
    salt_ax = brokenaxes(xlims=xlims,
                         ylims=ylims,
                         hspace=hspace,
                         wspace=wspace,
                         subplot_spec=gs[1, 1],
                         d=0.0)
    #salt_ax = fig.add_subplot(gs[1, 1])
    plot_broken_comparison(salt_ax,
                           df,
                           '(d) salt conservation',
                           'masso (kg yr-1)',
                           'soga (kg yr-1)',
                           'kg yr-1',
                           scale_factor=-15,
                           xpad=30,
                           ypad=40,
                           broken=True)
    handles, labels = update_legend_info(
        salt_ax, df[['masso (kg yr-1)', 'soga (kg yr-1)']], handles, labels)

    # Atmosphere mass conservation
    atmos_mass_ax = fig.add_subplot(gs[2, :])
    plot_broken_comparison(atmos_mass_ax,
                           df,
                           '(e) atmospheric mass conservation',
                           'massa (kg yr-1)',
                           'wfa (kg yr-1)',
                           'kg yr-1',
                           scale_factor=-12,
                           ypad=20)
    handles, labels = update_legend_info(atmos_mass_ax, df[['wfa (kg yr-1)']],
                                         handles, labels)

    fig.legend(handles, labels, loc='center left', bbox_to_anchor=(0.815, 0.5))
    plt.tight_layout(rect=(0, 0, 0.8, 1))

    for variable, data in cmip6_data_points.items():
        record_quartiles(variable, data, 'cmip6')
    for variable, data in cmip5_data_points.items():
        record_quartiles(variable, data, 'cmip5')

    plt.savefig(inargs.outfile, dpi=400)
    log_file = re.sub('.png', '.met', inargs.outfile)
    log_text = cmdprov.new_log(git_repo=repo_dir, extra_notes=quartiles)
    cmdprov.write_log(log_file, log_text)
def main(args):
    """Run the program."""

    experiments = ['GHG-only', 'AA-only', 'historical']
    letters = ['(a) ', '(b) ', '(c) ']
    area_bar_files, flux_bar_files, file_dict, nfiles = sort_files(args)

    start_year = args.time_bounds[0][0:4]
    end_year = args.time_bounds[1][0:4]

    var_names = {'precipitation_flux': 'precipitation',
                 'water_evapotranspiration_flux': 'evaporation',
                 'precipitation_minus_evaporation_flux': 'P-E'}
    var_name = var_names[args.var]
    ylabel = f"time integrated {var_name} anomaly, {start_year}-{end_year} (kg)"

    time_constraint = gio.get_time_constraint(args.time_bounds)        
    fig, axes = plt.subplots(1, 3, figsize=(18, 6))
    for plotnum, exp in enumerate(experiments): 
        total_files, flux_dashed_files, area_dashed_files = file_dict[exp]
        data = []
        for modelnum in range(nfiles):
            total_cube, history = get_data(total_files[modelnum], args.var, time_constraint, 'anomaly', ref_model=None)
            try:
                model = total_cube.attributes['model_id']
            except KeyError:
                model = total_cube.attributes['source_id']
            flux_bar_cube, history = get_data(flux_bar_files[modelnum], args.var, None, 'mean', ref_model=model)
            flux_dashed_integral_cube, history = get_data(flux_dashed_files[modelnum], args.var, time_constraint, 'anomaly', ref_model=model)
            area_bar_cube, area_bar_history = get_data(area_bar_files[modelnum], 'cell_area', None, 'mean', ref_model=model)
            area_dashed_integral_cube, area_dashed_integral_history = get_data(area_dashed_files[modelnum], 'cell_area',
                                                                               time_constraint, 'anomaly', ref_model=model)
            
            area_component = flux_bar_cube.data * area_dashed_integral_cube.data
            intensity_component = area_bar_cube.data * flux_dashed_integral_cube.data

            data.append([model, 'total', 'SH-P', total_cube.data[0]])
            data.append([model, 'total', 'SH-E', total_cube.data[1]])
            data.append([model, 'total', 'T-P', total_cube.data[2]])
            data.append([model, 'total', 'NH-E', total_cube.data[3]])
            data.append([model, 'total', 'NH-P', total_cube.data[4]])
            data.append([model, 'intensity', 'SH-P', intensity_component[0]])
            data.append([model, 'intensity', 'SH-E', intensity_component[1]])
            data.append([model, 'intensity', 'T-P', intensity_component[2]])
            data.append([model, 'intensity', 'NH-E', intensity_component[3]])
            data.append([model, 'intensity', 'NH-P', intensity_component[4]])
            data.append([model, 'area', 'SH-P', area_component[0]])
            data.append([model, 'area', 'SH-E', area_component[1]])
            data.append([model, 'area', 'T-P', area_component[2]])
            data.append([model, 'area', 'NH-E', area_component[3]])
            data.append([model, 'area', 'NH-P', area_component[4]])
           
        df = pd.DataFrame(data, columns=['model', 'component', 'P-E region', ylabel])
        title = letters[plotnum] + exp
        plot_data(axes[plotnum], df, title, ylabel, args.ymax, model_dots=args.dots)

    plt.savefig(args.outfile, bbox_inches='tight')

    metadata_dict = {area_bar_files[0]: area_bar_history[0],
                     area_dashed_files[0]: area_dashed_integral_history[0]}
    log_text = cmdprov.new_log(infile_history=metadata_dict, git_repo=repo_dir)
    log_file = re.sub('.png', '.met', args.outfile)
    cmdprov.write_log(log_file, log_text)