示例#1
0
def test_completeOMIReproduction_coarsegrid(tmp_path):

    errors = []

    # Calculate EOFs
    raw_olr = olr.load_noaa_interpolated_olr(olr_data_filename)
    shorter_olr = olr.restrict_time_coverage(raw_olr,
                                             np.datetime64('1979-01-01'),
                                             np.datetime64('2012-12-31'))
    coarse_lat = np.arange(-20., 20.1, 8.0)
    coarse_long = np.arange(0., 359.9, 20.0)
    interpolated_olr = olr.interpolate_spatial_grid(shorter_olr, coarse_lat,
                                                    coarse_long)
    eofs = omi.calc_eofs_from_olr(interpolated_olr,
                                  sign_doy1reference=True,
                                  interpolate_eofs=True,
                                  strict_leap_year_treatment=True)
    eofs.save_all_eofs_to_npzfile(
        tmp_path / "test_completeOMIReproduction_coarsegrid_EOFs.npz")

    # Validate EOFs against mjoindices own reference (results should be equal)
    mjoindices_reference_eofs = eof.restore_all_eofs_from_npzfile(
        mjoindices_reference_eofs_filename_coarsegrid)
    for idx, target_eof in enumerate(eofs.eof_list):
        if not mjoindices_reference_eofs.eof_list[idx].close(target_eof):
            errors.append(
                "mjoindices-reference-validation: EOF data at index %i is incorrect"
                % idx)

    # Calculate PCs
    raw_olr = olr.load_noaa_interpolated_olr(olr_data_filename)
    pcs = omi.calculate_pcs_from_olr(raw_olr,
                                     eofs,
                                     np.datetime64("1979-01-01"),
                                     np.datetime64("2018-08-28"),
                                     use_quick_temporal_filter=False)
    pc_temp_file = tmp_path / "test_completeOMIReproduction_coarsegrid_PCs.txt"
    pcs.save_pcs_to_txt_file(pc_temp_file)

    # Validate PCs against mjoindices own reference (results should be equal)
    # Reload pcs instead of using the calculated ones, because the saving routine has truncated some decimals of the
    # reference values. So do the same with the testing target pcs.
    pcs = pc.load_pcs_from_txt_file(pc_temp_file)
    mjoindices_reference_pcs = pc.load_pcs_from_txt_file(
        mjoindices_reference_pcs_filename_coarsegrid)
    if not np.all(mjoindices_reference_pcs.time == pcs.time):
        errors.append(
            "mjoindices-reference-validation: Dates of PCs do not match.")
    if not np.allclose(mjoindices_reference_pcs.pc1, pcs.pc1):
        errors.append(
            "mjoindices-reference-validation: PC1 values do not match.")
    if not np.allclose(mjoindices_reference_pcs.pc2, pcs.pc2):
        errors.append(
            "mjoindices-reference-validation: PC2 values do not match.")

    assert not errors, "errors occurred:\n{}".format("\n".join(errors))
示例#2
0
def calculate_pcs_from_olr(olrdata: olr.OLRData,
                           eofdata: eof.EOFDataForAllDOYs,
                           period_start: np.datetime64,
                           period_end: np.datetime64,
                           use_quick_temporal_filter=False) -> pc.PCData:
    """
    This major function computes PCs according to the OMI algorithm based on given OLR data and previously calculated
    EOFs.

    :param olrdata: The OLR dataset. The spatial grid must fit to that of the EOFs
    :param eofdata: The previously calculated DOY-dependent EOFs.
    :param period_start: the beginning of the period, for which the PCs should be calculated.
    :param period_end: the ending of the period, for which the PCs should be calculated.
    :param use_quick_temporal_filter: There are two implementations of the temporal filtering: First, the original
        Wheeler-Kiladis-Filter, which is closer to the original implementation while being slower (because it is based
        on a 2-dim FFT) or a 1-dim FFT Filter. Setting this parameter to True uses the quicker 1-dim implementation. The
        results are quite similar.

    :return: The PC time series.
    """
    resticted_olr_data = olr.restrict_time_coverage(olrdata, period_start,
                                                    period_end)
    resampled_olr_data = olr.interpolate_spatial_grid(resticted_olr_data,
                                                      eofdata.lat,
                                                      eofdata.long)
    if use_quick_temporal_filter:
        filtered_olr_data = qfilter.filter_olr_for_mjo_pc_calculation_1d_spectral_smoothing(
            resampled_olr_data)
    else:
        filtered_olr_data = wkfilter.filter_olr_for_mjo_pc_calculation(
            resampled_olr_data)
    raw_pcs = regress_3dim_data_onto_eofs(filtered_olr_data, eofdata)
    normalization_factor = 1 / np.std(raw_pcs.pc1)
    pc1 = np.multiply(raw_pcs.pc1, normalization_factor)
    pc2 = np.multiply(raw_pcs.pc2, normalization_factor)
    return pc.PCData(raw_pcs.time, pc1, pc2)
示例#3
0
pctxtfile = Path(os.path.abspath('')) / "example_data" / "PCs_coarsegrid.txt"

# Directory in which the figures are saved.
fig_dir = Path(os.path.abspath(
    '')) / "example_data" / "omi_recalc_example_plots_coarsegrid"

# ############## Calculation of the EOFs ###################

if not fig_dir.exists():
    fig_dir.mkdir(parents=True, exist_ok=False)

# Load the OLR data.
# This is the first place to insert your own OLR data, if you want to compute OMI for a different dataset.
raw_olr = olr.load_noaa_interpolated_olr(olr_data_filename)
# Restrict dataset to the original length for the EOF calculation (Kiladis, 2014).
shorter_olr = olr.restrict_time_coverage(raw_olr, np.datetime64('1979-01-01'),
                                         np.datetime64('2012-12-31'))

# This is the line, where the spatial grid is changed.
interpolated_olr = olr.interpolate_spatial_grid(shorter_olr, coarse_lat,
                                                coarse_long)

# Diagnosis plot of the loaded OLR data.
fig = olr.plot_olr_map_for_date(interpolated_olr, np.datetime64("2010-01-01"))
fig.show()
fig.savefig(fig_dir / "OLR_map.png")

# Calculate the eofs. In the postprocessing, the signs of the EOFs are adjusted and the EOF in a period
# around DOY 300 are replaced by an interpolation see Kiladis, 2014).
# The switch strict_leap_year_treatment has major implications only for the EOFs calculated for DOY 366 and causes only
# minor differences for the other DOYs. While the results for setting strict_leap_year_treatment=False are closer to the
# original values, the calculation strict_leap_year_treatment=True is somewhat more stringently implemented using
示例#4
0
def test_completeOMIReproduction(tmp_path):

    errors = []

    # Calculate EOFs
    raw_olr = olr.load_noaa_interpolated_olr(olr_data_filename)
    shorter_olr = olr.restrict_time_coverage(raw_olr,
                                             np.datetime64('1979-01-01'),
                                             np.datetime64('2012-12-31'))
    interpolated_olr = olr.interpolate_spatial_grid_to_original(shorter_olr)
    eofs = omi.calc_eofs_from_olr(interpolated_olr,
                                  sign_doy1reference=True,
                                  interpolate_eofs=True,
                                  strict_leap_year_treatment=False)
    eofs.save_all_eofs_to_npzfile(tmp_path /
                                  "test_completeOMIReproduction_EOFs.npz")

    # Validate EOFs against original (results are inexact but close)
    orig_eofs = eof.load_all_original_eofs_from_directory(
        originalOMIDataDirname)

    corr_1, diff_mean_1, diff_std_1, diff_abs_percent68_1, diff_abs_percent95_1, diff_abs_percent99_1 = mjoindices.evaluation_tools.calc_comparison_stats_for_eofs_all_doys(
        orig_eofs,
        eofs,
        1,
        exclude_doy366=False,
        percentage=False,
        do_print=False)
    if not np.all(corr_1 > 0.994):
        errors.append(
            "original-validation: Correlation for EOF1 at least for one DOY too low!"
        )
    if not np.all(diff_abs_percent99_1 < 0.0084):
        errors.append(
            "original-validation: 99% percentile for EOF1 at least for one DOY too high!"
        )
    if not np.all(diff_abs_percent68_1 < 0.0018):
        errors.append(
            "original-validation: 68% percentile for EOF1 at least for one DOY too high!"
        )

    corr_2, diff_mean_2, diff_std_2, diff_abs_percent68_2, diff_abs_percent95_2, diff_abs_percent99_2 = mjoindices.evaluation_tools.calc_comparison_stats_for_eofs_all_doys(
        orig_eofs,
        eofs,
        2,
        exclude_doy366=False,
        percentage=False,
        do_print=False)
    if not np.all(corr_2 > 0.993):
        errors.append(
            "original-validation: Correlation for EOF2 at least for one DOY too low!"
        )
    if not np.all(diff_abs_percent99_2 < 0.0065):
        errors.append(
            "original-validation: 99% percentile for EOF2 at least for one DOY too high!"
        )
    if not np.all(diff_abs_percent68_2 < 0.0018):
        errors.append(
            "original-validation: 68% percentile for EOF2 at least for one DOY too high!"
        )

    # Validate explained variance against original (results are inexact but close)
    orig_explained_variance_1, orig_explained_variance_2 = mjoindices.evaluation_tools.load_omi_explained_variance(
        original_omi_explained_variance_file)

    corr_var1, diff_mean_var1, diff_std_var1, diff_vec_var1, diff_abs_percent68_var1, diff_abs_percent95_var1, diff_abs_percent99_var1 = mjoindices.evaluation_tools.calc_comparison_stats_for_explained_variance(
        orig_explained_variance_1,
        eofs.explained_variance1_for_all_doys(),
        do_print=False,
        exclude_doy366=False)
    if not diff_std_var1 < 0.0008:
        errors.append(
            "original-validation: Std.Dev. of the difference of both explained variances for EOF1 is to too high!"
        )
    if not diff_abs_percent99_var1 < 0.0017:
        errors.append(
            "original-validation: 99% percentile of the difference of both explained variances for EOF1 is to too high!"
        )
    if not diff_abs_percent68_var1 < 0.0009:
        errors.append(
            "original-validation: 68% percentile of the difference of both explained variances for EOF1 is to too high!"
        )

    corr_var2, diff_mean_var2, diff_std_var2, diff_vec_var2, diff_abs_percent68_var2, diff_abs_percent95_var2, diff_abs_percent99_var2 = mjoindices.evaluation_tools.calc_comparison_stats_for_explained_variance(
        orig_explained_variance_2,
        eofs.explained_variance2_for_all_doys(),
        do_print=False,
        exclude_doy366=False)
    if not diff_std_var2 < 0.0008:
        errors.append(
            "original-validation: Std.Dev. of the difference of both explained variances for EOF2 is to too high!"
        )
    if not diff_abs_percent99_var2 < 0.0018:
        errors.append(
            "original-validation: 99% percentile of the difference of both explained variances for EOF2 is to too high!"
        )
    if not diff_abs_percent68_var2 < 0.001:
        errors.append(
            "original-validation: 68% percentile of the difference of both explained variances for EOF2 is to too high!"
        )

    # Validate EOFs against mjoindices own reference (results should be equal)
    mjoindices_reference_eofs = eof.restore_all_eofs_from_npzfile(
        mjoindices_reference_eofs_filename)
    for idx, target_eof in enumerate(eofs.eof_list):
        if not mjoindices_reference_eofs.eof_list[idx].close(target_eof):
            errors.append(
                "mjoindices-reference-validation: EOF data at index %i is incorrect"
                % idx)

    # Calculate PCs
    raw_olr = olr.load_noaa_interpolated_olr(olr_data_filename)
    pcs = omi.calculate_pcs_from_olr(raw_olr,
                                     eofs,
                                     np.datetime64("1979-01-01"),
                                     np.datetime64("2018-08-28"),
                                     use_quick_temporal_filter=False)
    pc_temp_file = tmp_path / "test_completeOMIReproduction_PCs.txt"
    pcs.save_pcs_to_txt_file(pc_temp_file)

    # Validate PCs  against original (results are inexact but close)
    orig_pcs = pc.load_original_pcs_from_txt_file(origOMIPCsFilename)
    # Reload pcs instead of using the calculated ones, because the saving routine has truncated some decimals of the
    # reference values. So do the same with the testing target pcs.
    pcs = pc.load_pcs_from_txt_file(pc_temp_file)

    tempa, tempb, corr_pc1, diff_mean_pc1, diff_std_pc1, diff_ts_abs_pc1, diff_abs_percent68_pc1, diff_abs_percent95_pc1, diff_abs_percent99_pc1 = mjoindices.evaluation_tools.calc_timeseries_agreement(
        orig_pcs.pc1,
        orig_pcs.time,
        pcs.pc1,
        pcs.time,
        exclude_doy366=False,
        do_print=False)
    if not corr_pc1 > 0.998:
        errors.append(
            "original-validation: Correlation for PC1 timeseries is to too low!"
        )
    if not diff_std_pc1 < 0.0458:
        errors.append(
            "original-validation: Std.Dev. of the difference of both PC1 timeseries is to too high!"
        )
    if not diff_abs_percent99_pc1 < 0.157:
        errors.append(
            "original-validation: 99% percentile of the difference of both PC1 timeseries is to too high!"
        )
    if not diff_abs_percent68_pc1 < 0.0327:
        errors.append(
            "original-validation: 68% percentile of the difference of both PC1 timeseries is to too high!"
        )

    tempa, tempb, corr_pc2, diff_mean_pc2, diff_std_pc2, diff_ts_abs_pc2, diff_abs_percent68_pc2, diff_abs_percent95_pc2, diff_abs_percent99_pc2 = mjoindices.evaluation_tools.calc_timeseries_agreement(
        orig_pcs.pc2,
        orig_pcs.time,
        pcs.pc2,
        pcs.time,
        exclude_doy366=False,
        do_print=False)
    if not corr_pc2 > 0.998:
        errors.append(
            "original-validation: Correlation for PC2 timeseries is to too low!"
        )
    if not diff_std_pc2 < 0.0488:
        errors.append(
            "original-validation: Std.Dev. of the difference of both PC2 timeseries is to too high!"
        )
    if not diff_abs_percent99_pc2 < 0.1704:
        errors.append(
            "original-validation: 99% percentile of the difference of both PC2 timeseries is to too high!"
        )
    if not diff_abs_percent68_pc2 < 0.0353:
        errors.append(
            "original-validation: 68% percentile of the difference of both PC2 timeseries is to too high!"
        )

    strength = np.sqrt(np.square(pcs.pc1) + np.square(pcs.pc2))
    orig_strength = np.sqrt(np.square(orig_pcs.pc1) + np.square(orig_pcs.pc2))

    tempa, tempb, corr_strength, diff_mean_strength, diff_std_strength, diff_ts_abs_strength, diff_abs_percent68_strength, diff_abs_percent95_strength, diff_abs_percent99_strength = mjoindices.evaluation_tools.calc_timeseries_agreement(
        orig_strength,
        orig_pcs.time,
        strength,
        pcs.time,
        exclude_doy366=False,
        do_print=False)
    if not corr_strength > 0.9998:
        errors.append(
            "original-validation: Correlation for strength timeseries is to too low!"
        )
    if not diff_std_strength < 0.0103:
        errors.append(
            "original-validation: Std.Dev. of the difference of both strength timeseries is to too high!"
        )
    if not diff_abs_percent99_strength < 0.0341:
        errors.append(
            "original-validation: 99% percentile of the difference of both strength timeseries is to too high!"
        )
    if not diff_abs_percent68_strength < 0.0079:
        errors.append(
            "original-validation: 68% percentile of the difference of both strength timeseries is to too high!"
        )

    # Validate PCs against mjoindices own reference (results should be equal)
    mjoindices_reference_pcs = pc.load_pcs_from_txt_file(
        mjoindices_reference_pcs_filename)
    if not np.all(mjoindices_reference_pcs.time == pcs.time):
        errors.append(
            "mjoindices-reference-validation: Dates of PCs do not match.")
    if not np.allclose(mjoindices_reference_pcs.pc1, pcs.pc1):
        errors.append(
            "mjoindices-reference-validation: PC1 values do not match.")
    if not np.allclose(mjoindices_reference_pcs.pc2, pcs.pc2):
        errors.append(
            "mjoindices-reference-validation: PC2 values do not match.")

    assert not errors, "errors occurred:\n{}".format("\n".join(errors))