예제 #1
0
def read_and_trim_diff(file_1, file_2, var_name):

    time_1 = netcdf_time(file_1, monthly=False)
    time_2 = netcdf_time(file_2, monthly=False)
    data_1 = read_netcdf(file_1, var_name)
    data_2 = read_netcdf(file_2, var_name)
    time, data_diff = trim_and_diff(time_1, time_2, data_1, data_2)
    return time, data_diff
예제 #2
0
def read_plot_timeseries_multi(var_names,
                               file_path,
                               diff=False,
                               precomputed=False,
                               grid=None,
                               lon0=None,
                               lat0=None,
                               fig_name=None,
                               monthly=True,
                               legend_in_centre=False,
                               dpi=None,
                               colours=None):

    if diff:
        if not isinstance(file_path, list) or len(file_path) != 2:
            print 'Error (read_plot_timeseries_multi): must pass a list of 2 file paths when diff=True.'
            sys.exit()
        file_path_1 = file_path[0]
        file_path_2 = file_path[1]
        time_1 = netcdf_time(file_path_1,
                             monthly=(monthly and not precomputed))
        time_2 = netcdf_time(file_path_2,
                             monthly=(monthly and not precomputed))
        time = trim_and_diff(time_1, time_2, time_1, time_2)[0]
    else:
        time = netcdf_time(file_path, monthly=(monthly and not precomputed))

    data = []
    labels = []
    units = None
    if colours is None:
        colours = [
            'blue', 'red', 'black', 'green', 'cyan', 'magenta', 'yellow'
        ]
        if len(var_names) > len(colours):
            print 'Error (read_plot_timeseries_multi): need to specify colours if there are more than 7 variables.'
            sys.exit()
        colours = colours[:len(var_names)]
    for var in var_names:
        if var.endswith('mass_balance'):
            print 'Error (read_plot_timeseries_multi): ' + var + ' is already a multi-plot by itself.'
            sys.exit()
        title, units_tmp = set_parameters(var)[2:4]
        labels.append(title)
        if units is None:
            units = units_tmp
        elif units != units_tmp:
            print 'Error (read_plot_timeseries_multi): units do not match for all timeseries variables'
            sys.exit()
        if precomputed:
            if diff:
                data_1 = read_netcdf(file_path_1, var)
                data_2 = read_netcdf(file_path_2, var)
                data.append(trim_and_diff(time_1, time_2, data_1, data_2)[1])
            else:
                data.append(read_netcdf(file_path, var))
        else:
            if diff:
                data.append(
                    calc_special_timeseries_diff(var,
                                                 file_path_1,
                                                 file_path_2,
                                                 grid=grid,
                                                 lon0=lon0,
                                                 lat0=lat0,
                                                 monthly=monthly)[1])
            else:
                data.append(
                    calc_special_timeseries(var,
                                            file_path,
                                            grid=grid,
                                            lon0=lon0,
                                            lat0=lat0,
                                            monthly=monthly)[1])
    title, labels = trim_titles(labels)
    if diff:
        title = 'Change in ' + title
    timeseries_multi_plot(time,
                          data,
                          labels,
                          colours,
                          title=title,
                          units=units,
                          monthly=monthly,
                          fig_name=fig_name,
                          dpi=dpi,
                          legend_in_centre=legend_in_centre)
예제 #3
0
 def read_and_trim(var_name):
     data_1 = read_netcdf(file_path_1, var_name)
     data_2 = read_netcdf(file_path_2, var_name)
     time, data_diff = trim_and_diff(time_1, time_2, data_1, data_2)
     return time, data_diff
예제 #4
0
def read_plot_timeseries_multi(var_names,
                               file_path,
                               diff=False,
                               precomputed=False,
                               grid=None,
                               lon0=None,
                               lat0=None,
                               fig_name=None,
                               monthly=True,
                               legend_in_centre=False,
                               dpi=None,
                               colours=None,
                               smooth=0,
                               annual_average=False):

    if diff and (not isinstance(file_path, list) or len(file_path) != 2):
        print 'Error (read_plot_timeseries_multi): must pass a list of 2 file paths when diff=True.'
        sys.exit()

    if precomputed:
        # Read time arrays
        if diff:
            time_1 = netcdf_time(file_path[0], monthly=False)
            time_2 = netcdf_time(file_path[1], monthly=False)
            time = trim_and_diff(time_1, time_2, time_1, time_2)[0]
        else:
            time = netcdf_time(file_path, monthly=False)

    # Set up the colours
    if colours is None:
        colours = default_colours(len(var_names))

    data = []
    labels = []
    units = None
    if annual_average:
        time_orig = np.copy(time)
    # Loop over variables
    for var in var_names:
        if var.endswith('mass_balance'):
            print 'Error (read_plot_timeseries_multi): ' + var + ' is already a multi-plot by itself.'
            sys.exit()
        title, units_tmp = set_parameters(var)[2:4]
        labels.append(title)
        if units is None:
            units = units_tmp
        elif units != units_tmp:
            print 'Error (read_plot_timeseries_multi): units do not match for all timeseries variables'
            sys.exit()
        if precomputed:
            if diff:
                data_1 = read_netcdf(file_path[0], var)
                data_2 = read_netcdf(file_path[1], var)
                data_tmp = trim_and_diff(time_1, time_2, data_1, data_2)[1]
            else:
                data_tmp = read_netcdf(file_path, var)
        else:
            if diff:
                time, data_tmp = calc_special_timeseries_diff(var,
                                                              file_path[0],
                                                              file_path[1],
                                                              grid=grid,
                                                              lon0=lon0,
                                                              lat0=lat0,
                                                              monthly=monthly)
            else:
                time, data_tmp = calc_special_timeseries(var,
                                                         file_path,
                                                         grid=grid,
                                                         lon0=lon0,
                                                         lat0=lat0,
                                                         monthly=monthly)
        if annual_average:
            time, data_tmp = calc_annual_averages(time_orig, data_tmp)
        data.append(data_tmp)
    for n in range(len(data)):
        data_tmp, time_tmp = moving_average(data[n], smooth, time=time)
        data[n] = data_tmp
    time = time_tmp
    title, labels = trim_titles(labels)
    if diff:
        title = 'Change in ' + title[0].lower() + title[1:]
    timeseries_multi_plot(time,
                          data,
                          labels,
                          colours,
                          title=title,
                          units=units,
                          monthly=monthly,
                          fig_name=fig_name,
                          dpi=dpi,
                          legend_in_centre=legend_in_centre)
예제 #5
0
 def read_and_trim(var_name):
     data_1 = read_netcdf(file_path[0], var_name)
     data_2 = read_netcdf(file_path[1], var_name)
     data_diff = trim_and_diff(time_1, time_2, data_1, data_2)[1]
     return data_diff
예제 #6
0
def read_plot_timeseries(var,
                         file_path,
                         diff=False,
                         precomputed=False,
                         grid=None,
                         lon0=None,
                         lat0=None,
                         fig_name=None,
                         monthly=True,
                         legend_in_centre=False,
                         dpi=None,
                         annual_average=False,
                         smooth=0):

    if diff and (not isinstance(file_path, list) or len(file_path) != 2):
        print 'Error (read_plot_timeseries): must pass a list of 2 file paths when diff=True.'
        sys.exit()

    if precomputed:
        # Read time arrays
        if diff:
            time_1 = netcdf_time(file_path[0],
                                 monthly=(monthly and not precomputed))
            time_2 = netcdf_time(file_path[1],
                                 monthly=(monthly and not precomputed))
            calendar = netcdf_time(file_path[0], return_units=True)[2]
            time = trim_and_diff(time_1, time_2, time_1, time_2)[0]
        else:
            time = netcdf_time(file_path,
                               monthly=(monthly and not precomputed))
            calendar = netcdf_time(file_path, return_units=True)[2]

    # Set parameters (only care about title and units)
    title, units = set_parameters(var)[2:4]
    if diff:
        title = 'Change in ' + title[0].lower() + title[1:]

    # Inner function to read a timeseries from both files and calculate the differences, trimming if needed. Only useful if precomputed=True.
    def read_and_trim(var_name):
        data_1 = read_netcdf(file_path[0], var_name)
        data_2 = read_netcdf(file_path[1], var_name)
        data_diff = trim_and_diff(time_1, time_2, data_1, data_2)[1]
        return data_diff

    if var.endswith('mass_balance'):
        if precomputed:
            # Read the fields from the timeseries file
            shelf = var[:var.index('_mass_balance')]
            if diff:
                melt = read_and_trim(shelf + '_total_melt')
                freeze = read_and_trim(shelf + '_total_freeze')
            else:
                melt = read_netcdf(file_path, shelf + '_total_melt')
                freeze = read_netcdf(file_path, shelf + '_total_freeze')
        else:
            # Calculate the timeseries from the MITgcm file(s)
            if diff:
                time, melt, freeze = calc_special_timeseries_diff(
                    var,
                    file_path[0],
                    file_path[1],
                    grid=grid,
                    monthly=monthly)
            else:
                time, melt, freeze = calc_special_timeseries(var,
                                                             file_path,
                                                             grid=grid,
                                                             monthly=monthly)
        if annual_average:
            time, [melt, freeze] = calc_annual_averages(time, [melt, freeze])
        melt = moving_average(melt, smooth, time=time)[0]
        freeze, time = moving_average(freeze, smooth, time=time)
        timeseries_multi_plot(time, [melt, freeze, melt + freeze],
                              ['Melting', 'Freezing', 'Net'],
                              ['red', 'blue', 'black'],
                              title=title,
                              units=units,
                              monthly=monthly,
                              fig_name=fig_name,
                              dpi=dpi,
                              legend_in_centre=legend_in_centre)
    else:
        if precomputed:
            if diff:
                data = read_and_trim(var)
            else:
                data = read_netcdf(file_path, var)
        else:
            if diff:
                time, data = calc_special_timeseries_diff(var,
                                                          file_path[0],
                                                          file_path[1],
                                                          grid=grid,
                                                          monthly=monthly)
            else:
                time, data = calc_special_timeseries(var,
                                                     file_path,
                                                     grid=grid,
                                                     lon0=lon0,
                                                     lat0=lat0,
                                                     monthly=monthly)
        if annual_average:
            time, data = calc_annual_averages(time, data)
        data, time = moving_average(data, smooth, time=time)
        make_timeseries_plot(time,
                             data,
                             title=title,
                             units=units,
                             monthly=monthly,
                             fig_name=fig_name,
                             dpi=dpi)