示例#1
0
def test_io_export_netcdf_one_member_one_time_step():
    """Test the export netcdf."""
    # open a netcdf file
    root_path = pysteps.rcparams.data_sources["bom"]["root_path"]
    rel_path = os.path.join("prcp-cscn", "2", "2018", "06", "16")
    filename = os.path.join(root_path, rel_path,
                            "2_20180616_100000.prcp-cscn.nc")
    precip, _, metadata = pysteps.io.import_bom_rf3(filename)

    # save it back to disk
    with tempfile.TemporaryDirectory() as outpath:
        outfnprefix = 'test_netcdf_out'
        file_path = os.path.join(outpath, outfnprefix + '.nc')
        startdate = datetime.strptime("2018-06-16 10:00:00",
                                      "%Y-%m-%d %H:%M:%S")
        timestep = metadata['accutime']
        n_timesteps = 1
        shape = precip.shape
        exporter = initialize_forecast_exporter_netcdf(
            outpath,
            outfnprefix,
            startdate,
            timestep,
            n_timesteps,
            shape,
            metadata,
            n_ens_members=1,
        )
        export_forecast_dataset(precip[np.newaxis, :], exporter)
        close_forecast_files(exporter)
        # assert if netcdf file was saved and file size is not zero
        assert (os.path.exists(file_path) and os.path.getsize(file_path) > 0)
示例#2
0
def test_io_export_netcdf_one_member_one_time_step():
    """
    Test the export netcdf.
    Also, test that the exported file can be read by the importer.
    """

    pytest.importorskip("pyproj")

    precip, metadata = get_precipitation_fields(return_raw=True,
                                                metadata=True,
                                                source="fmi")
    precip = precip.squeeze()

    invalid_mask = get_invalid_mask(precip)

    # save it back to disk
    with tempfile.TemporaryDirectory() as outpath:
        outfnprefix = "test_netcdf_out"
        file_path = os.path.join(outpath, outfnprefix + ".nc")
        startdate = metadata["timestamps"][0]
        timestep = metadata["accutime"]
        n_timesteps = 1
        shape = precip.shape
        exporter = initialize_forecast_exporter_netcdf(
            outpath,
            outfnprefix,
            startdate,
            timestep,
            n_timesteps,
            shape,
            metadata,
            n_ens_members=1,
        )
        export_forecast_dataset(precip[np.newaxis, :], exporter)
        close_forecast_files(exporter)

        # assert if netcdf file was saved and file size is not zero
        assert os.path.exists(file_path) and os.path.getsize(file_path) > 0

        # Test that the file can be read by the nowcast_importer
        output_file_path = os.path.join(outpath, f"{outfnprefix}.nc")

        precip_new, _ = import_netcdf_pysteps(output_file_path)

        assert_array_almost_equal(precip, precip_new.data)
        assert precip_new.dtype == "single"

        precip_new, _ = import_netcdf_pysteps(output_file_path, dtype="double")
        assert_array_almost_equal(precip, precip_new.data)
        assert precip_new.dtype == "double"

        precip_new, _ = import_netcdf_pysteps(output_file_path, fillna=-1000)
        new_invalid_mask = precip_new == -1000
        assert (new_invalid_mask == invalid_mask).all()