diff --git a/cli/tests/cli/commands/decode.rs b/cli/tests/cli/commands/decode.rs index 0da2a8c..2adb6c1 100644 --- a/cli/tests/cli/commands/decode.rs +++ b/cli/tests/cli/commands/decode.rs @@ -73,7 +73,7 @@ macro_rules! test_operation_with_data_without_nan_values_and_byte_order_options .stdout(predicate::str::is_empty()) .stderr(predicate::str::is_empty()); - let actual = utils::cat_as_bytes(&out_path)?; + let actual = utils::get_uncompressed(&out_path)?; assert_eq!(actual, $expected); Ok(()) @@ -154,7 +154,7 @@ fn decoding_run_length_packing_as_big_endian() -> Result<(), Box b.to_vec(), }) .collect(); - let actual = utils::cat_as_bytes(&out_path)?; + let actual = utils::get_uncompressed(&out_path)?; assert_eq!(actual, expected); Ok(()) @@ -189,7 +189,7 @@ macro_rules! test_operation_with_data_with_nan_values_as_little_endian { b => b.to_vec(), }) .collect(); - let actual = utils::cat_as_bytes(&out_path)?; + let actual = utils::get_uncompressed(&out_path)?; assert_eq!(actual, expected); Ok(()) @@ -279,7 +279,7 @@ macro_rules! test_operation_with_data_without_nan_values_compared_using_simple_p let dig: i16 = $dig; let expected = $expected; let expected = utils::encode_le_bytes_using_simple_packing(expected, ref_val, exp, dig); - let actual = utils::cat_as_bytes(&out_path)?; + let actual = utils::get_uncompressed(&out_path)?; let actual = utils::encode_le_bytes_using_simple_packing(actual, ref_val, exp, dig); assert_eq!(actual, expected); diff --git a/cli/tests/cli/utils.rs b/cli/tests/cli/utils.rs index d3a32e2..552207d 100644 --- a/cli/tests/cli/utils.rs +++ b/cli/tests/cli/utils.rs @@ -5,77 +5,40 @@ use std::{ }; use tempfile::NamedTempFile; -use xz2::bufread::XzDecoder; pub(crate) mod testdata; -fn cat_to_tempfile

(file_path: P) -> Result +fn write_uncompressed_to_tempfile

(file_path: P) -> Result where P: AsRef, { - let mut buf = Vec::new(); let mut out = NamedTempFile::new()?; - - let f = File::open(file_path)?; - let mut f = BufReader::new(f); - f.read_to_end(&mut buf)?; + let buf = get_uncompressed(file_path)?; out.write_all(&buf)?; - Ok(out) } -fn gzcat_to_tempfile

(file_path: P) -> Result +pub(crate) fn get_uncompressed

(file_path: P) -> Result, io::Error> where P: AsRef, { let mut buf = Vec::new(); - let mut out = NamedTempFile::new()?; - let f = File::open(file_path)?; - let f = BufReader::new(f); - let mut f = flate2::read::GzDecoder::new(f); - f.read_to_end(&mut buf)?; - out.write_all(&buf)?; - - Ok(out) -} - -fn xzcat_to_tempfile

(file_path: P) -> Result -where - P: AsRef, -{ - let mut buf = Vec::new(); - let mut out = NamedTempFile::new()?; - - let f = File::open(file_path)?; - let f = BufReader::new(f); - let mut f = XzDecoder::new(f); - f.read_to_end(&mut buf)?; - out.write_all(&buf)?; - - Ok(out) -} - -fn unxz_as_bytes

(file_path: P) -> Result, io::Error> -where - P: AsRef, -{ - let mut buf = Vec::new(); - - let f = File::open(file_path)?; - let f = BufReader::new(f); - let mut f = XzDecoder::new(f); - f.read_to_end(&mut buf)?; - - Ok(buf) -} - -pub(crate) fn cat_as_bytes(file_name: &str) -> Result, io::Error> { - let mut buf = Vec::new(); - - let f = File::open(file_name)?; + let f = File::open(&file_path)?; let mut f = BufReader::new(f); - f.read_to_end(&mut buf)?; + match file_path.as_ref().extension().map(|s| s.as_encoded_bytes()) { + Some(b"gz") => { + let mut f = flate2::read::GzDecoder::new(f); + f.read_to_end(&mut buf)?; + } + Some(b"xz") => { + let mut f = xz2::bufread::XzDecoder::new(f); + f.read_to_end(&mut buf)?; + } + _ => { + f.read_to_end(&mut buf)?; + } + }; Ok(buf) } diff --git a/cli/tests/cli/utils/testdata.rs b/cli/tests/cli/utils/testdata.rs index 3dc4ea4..a532ad5 100644 --- a/cli/tests/cli/utils/testdata.rs +++ b/cli/tests/cli/utils/testdata.rs @@ -6,7 +6,7 @@ use std::{ use tempfile::NamedTempFile; -use crate::utils::{cat_to_tempfile, gzcat_to_tempfile, unxz_as_bytes, xzcat_to_tempfile}; +use crate::utils::{get_uncompressed, write_uncompressed_to_tempfile}; fn testdata_dir() -> PathBuf { Path::new(concat!(env!("CARGO_MANIFEST_DIR"), "/..")).join("testdata") @@ -35,7 +35,7 @@ pub(crate) mod grib2 { use super::*; pub(crate) fn cmc_glb() -> Result { - cat_to_tempfile(cmc_glb_file()) + write_uncompressed_to_tempfile(cmc_glb_file()) } fn cmc_glb_file() -> PathBuf { @@ -46,62 +46,32 @@ pub(crate) mod grib2 { testdata_dir().join("icon_global_icosahedral_single-level_2021112018_000_TOT_PREC.grib2") } - pub(crate) fn jma_kousa() -> Result { - xzcat_to_tempfile( - testdata_dir() - .join("Z__C_RJTD_20170221120000_MSG_GPV_Gll0p5deg_Pys_B20170221120000_F2017022115-2017022212_grib2.bin.xz"), - ) - } - - pub(crate) fn jma_meps() -> Result { - xzcat_to_tempfile( - testdata_dir() - .join("Z__C_RJTD_20190605000000_MEPS_GPV_Rjp_L-pall_FH00-15_grib2.bin.0-20.xz"), - ) - } - - pub(crate) fn jma_msmguid() -> Result { - xzcat_to_tempfile( - testdata_dir() - .join("Z__C_RJTD_20190304000000_MSM_GUID_Rjp_P-all_FH03-39_Toorg_grib2.bin.xz"), - ) - } - - pub(crate) fn jma_tornado_nowcast() -> Result { - cat_to_tempfile( - testdata_dir() - .join("Z__C_RJTD_20160822020000_NOWC_GPV_Ggis10km_Pphw10_FH0000-0100_grib2.bin"), - ) - } - - pub(crate) fn ncmrwf_wind_solar() -> Result { - xzcat_to_tempfile(testdata_dir().join("wind_solar_ind_0.125_20240521_12Z.grib2.0.xz")) - } - - pub(crate) fn noaa_gdas_0_10() -> Result { - xzcat_to_tempfile(testdata_dir().join("gdas.t12z.pgrb2.0p25.f000.0-10.xz")) - } - - pub(crate) fn noaa_gdas_12() -> Result { - xzcat_to_tempfile(testdata_dir().join("gdas.t12z.pgrb2.0p25.f000.12.xz")) - } - - pub(crate) fn noaa_gdas_46() -> Result { - xzcat_to_tempfile(testdata_dir().join("gdas.t12z.pgrb2.0p25.f000.46.xz")) - } - - pub(crate) fn noaa_mrms() -> Result { - gzcat_to_tempfile( - testdata_dir().join("MRMS_ReflectivityAtLowestAltitude_00.50_20230406-120039.grib2.gz"), - ) - } - - pub(crate) fn noaa_ndfd_critfireo() -> Result { - xzcat_to_tempfile(testdata_dir().join("ds.critfireo.bin.xz")) - } - - pub(crate) fn noaa_ndfd_minrh() -> Result { - xzcat_to_tempfile(testdata_dir().join("ds.minrh.bin.xz")) + macro_rules! definitions_of_grib2_test_data { + ($(($name:ident, $file_name:expr),)*) => ($( + pub(crate) fn $name() -> Result { + write_uncompressed_to_tempfile(testdata_dir().join($file_name)) + } + )*); + } + + definitions_of_grib2_test_data! { + ( + jma_kousa, + "Z__C_RJTD_20170221120000_MSG_GPV_Gll0p5deg_Pys_B20170221120000_F2017022115-2017022212_grib2.bin.xz" + ), + (jma_meps, "Z__C_RJTD_20190605000000_MEPS_GPV_Rjp_L-pall_FH00-15_grib2.bin.0-20.xz"), + (jma_msmguid, "Z__C_RJTD_20190304000000_MSM_GUID_Rjp_P-all_FH03-39_Toorg_grib2.bin.xz"), + ( + jma_tornado_nowcast, + "Z__C_RJTD_20160822020000_NOWC_GPV_Ggis10km_Pphw10_FH0000-0100_grib2.bin" + ), + (ncmrwf_wind_solar, "wind_solar_ind_0.125_20240521_12Z.grib2.0.xz"), + (noaa_gdas_0_10, "gdas.t12z.pgrb2.0p25.f000.0-10.xz"), + (noaa_gdas_12, "gdas.t12z.pgrb2.0p25.f000.12.xz"), + (noaa_gdas_46, "gdas.t12z.pgrb2.0p25.f000.46.xz"), + (noaa_mrms, "MRMS_ReflectivityAtLowestAltitude_00.50_20230406-120039.grib2.gz"), + (noaa_ndfd_critfireo, "ds.critfireo.bin.xz"), + (noaa_ndfd_minrh, "ds.minrh.bin.xz"), } pub(crate) fn multi_message_data(n: usize) -> Result { @@ -122,75 +92,31 @@ pub(crate) mod grib2 { pub(crate) mod flat_binary { use super::*; - pub(crate) fn cmc_glb_le() -> Result, io::Error> { - unxz_as_bytes(testdata_dir().join("gen").join("cmc-glb-wgrib2-le.bin.xz")) - } - - pub(crate) fn jma_kousa_be() -> Result, io::Error> { - unxz_as_bytes(testdata_dir().join("gen").join("kousa-wgrib2-be.bin.xz")) - } - - pub(crate) fn jma_kousa_le() -> Result, io::Error> { - unxz_as_bytes(testdata_dir().join("gen").join("kousa-wgrib2-le.bin.xz")) - } - - pub(crate) fn jma_meps_le() -> Result, io::Error> { - unxz_as_bytes(testdata_dir().join("gen").join("meps-wgrib2-le.bin.xz")) - } - - pub(crate) fn jma_msmguid_le() -> Result, io::Error> { - unxz_as_bytes(testdata_dir().join("gen").join("msmguid-wgrib2-le.bin.xz")) - } - - pub(crate) fn jma_tornado_nowcast_be() -> Result, io::Error> { - unxz_as_bytes(testdata_dir().join("gen").join("tornado-wgrib2-be.bin.xz")) - } - - pub(crate) fn jma_tornado_nowcast_le() -> Result, io::Error> { - unxz_as_bytes(testdata_dir().join("gen").join("tornado-wgrib2-le.bin.xz")) - } - - pub(crate) fn ncmrwf_wind_solar_le() -> Result, io::Error> { - unxz_as_bytes( - testdata_dir() - .join("gen") - .join("wind_solar_ind_0.125_20240521_12Z.wgrib2-le.bin.xz"), - ) - } - - pub(crate) fn noaa_gdas_0_le() -> Result, io::Error> { - unxz_as_bytes(testdata_dir().join("gen").join("gdas-0-wgrib2-le.bin.xz")) - } - - pub(crate) fn noaa_gdas_1_le() -> Result, io::Error> { - unxz_as_bytes(testdata_dir().join("gen").join("gdas-1-wgrib2-le.bin.xz")) - } - - pub(crate) fn noaa_gdas_2_le() -> Result, io::Error> { - unxz_as_bytes(testdata_dir().join("gen").join("gdas-2-wgrib2-le.bin.xz")) - } - - pub(crate) fn noaa_gdas_12_le() -> Result, io::Error> { - unxz_as_bytes(testdata_dir().join("gen").join("gdas-12-wgrib2-le.bin.xz")) - } - - pub(crate) fn noaa_gdas_46_le() -> Result, io::Error> { - unxz_as_bytes(testdata_dir().join("gen").join("gdas-46-wgrib2-le.bin.xz")) - } - - pub(crate) fn noaa_mrms_le() -> Result, io::Error> { - unxz_as_bytes(testdata_dir().join("gen").join("mrms-wgrib2-le.bin.xz")) - } - - pub(crate) fn noaa_ndfd_critfireo_0_le() -> Result, io::Error> { - unxz_as_bytes(testdata_dir().join("gen").join("ds.critfireo.bin.0.xz")) - } - - pub(crate) fn noaa_ndfd_critfireo_1_le() -> Result, io::Error> { - unxz_as_bytes(testdata_dir().join("gen").join("ds.critfireo.bin.1.xz")) - } - - pub(crate) fn noaa_ndfd_minrh_0_le() -> Result, io::Error> { - unxz_as_bytes(testdata_dir().join("gen").join("ds.minrh.bin.0.xz")) + macro_rules! definitions_of_flat_binary_test_data { + ($(($name:ident, $file_name:expr),)*) => ($( + pub(crate) fn $name() -> Result, io::Error> { + get_uncompressed(testdata_dir().join("gen").join($file_name)) + } + )*); + } + + definitions_of_flat_binary_test_data! { + (cmc_glb_le, "cmc-glb-wgrib2-le.bin.xz"), + (jma_kousa_be, "kousa-wgrib2-be.bin.xz"), + (jma_kousa_le, "kousa-wgrib2-le.bin.xz"), + (jma_meps_le, "meps-wgrib2-le.bin.xz"), + (jma_msmguid_le, "msmguid-wgrib2-le.bin.xz"), + (jma_tornado_nowcast_be, "tornado-wgrib2-be.bin.xz"), + (jma_tornado_nowcast_le, "tornado-wgrib2-le.bin.xz"), + (ncmrwf_wind_solar_le, "wind_solar_ind_0.125_20240521_12Z.wgrib2-le.bin.xz"), + (noaa_gdas_0_le, "gdas-0-wgrib2-le.bin.xz"), + (noaa_gdas_1_le, "gdas-1-wgrib2-le.bin.xz"), + (noaa_gdas_2_le, "gdas-2-wgrib2-le.bin.xz"), + (noaa_gdas_12_le, "gdas-12-wgrib2-le.bin.xz"), + (noaa_gdas_46_le, "gdas-46-wgrib2-le.bin.xz"), + (noaa_mrms_le, "mrms-wgrib2-le.bin.xz"), + (noaa_ndfd_critfireo_0_le, "ds.critfireo.bin.0.xz"), + (noaa_ndfd_critfireo_1_le, "ds.critfireo.bin.1.xz"), + (noaa_ndfd_minrh_0_le, "ds.minrh.bin.0.xz"), } } diff --git a/src/grid/earth.rs b/src/grid/earth.rs index da802ec..ed54f53 100644 --- a/src/grid/earth.rs +++ b/src/grid/earth.rs @@ -75,47 +75,34 @@ mod tests { use super::*; - fn ungz_as_bytes

(file_path: P) -> Result, std::io::Error> + fn get_uncompressed

(file_path: P) -> Result, std::io::Error> where P: AsRef, { let mut buf = Vec::new(); - let f = File::open(file_path)?; - let f = BufReader::new(f); - let mut f = flate2::read::GzDecoder::new(f); - f.read_to_end(&mut buf)?; - - Ok(buf) - } - - fn unxz_as_bytes

(file_path: P) -> Result, std::io::Error> - where - P: AsRef, - { - let mut buf = Vec::new(); - - let f = File::open(file_path)?; - let f = BufReader::new(f); - let mut f = xz2::bufread::XzDecoder::new(f); - f.read_to_end(&mut buf)?; - - Ok(buf) - } - - fn cat_as_bytes(file_name: &str) -> Result, std::io::Error> { - let mut buf = Vec::new(); - - let f = File::open(file_name)?; + let f = File::open(&file_path)?; let mut f = BufReader::new(f); - f.read_to_end(&mut buf)?; + match file_path.as_ref().extension().map(|s| s.as_encoded_bytes()) { + Some(b"gz") => { + let mut f = flate2::read::GzDecoder::new(f); + f.read_to_end(&mut buf)?; + } + Some(b"xz") => { + let mut f = xz2::bufread::XzDecoder::new(f); + f.read_to_end(&mut buf)?; + } + _ => { + f.read_to_end(&mut buf)?; + } + }; Ok(buf) } #[test] fn radii_for_shape_1() -> Result<(), Box> { - let buf = unxz_as_bytes("testdata/ds.critfireo.bin.xz")?; + let buf = get_uncompressed("testdata/ds.critfireo.bin.xz")?; let earth_actual = EarthShapeDefinition::from_buf(&buf[0x83..]); let earth_expected = EarthShapeDefinition { shape_of_the_earth: 1, @@ -134,7 +121,7 @@ mod tests { #[test] fn radii_for_shape_2() -> Result<(), Box> { - let buf = ungz_as_bytes( + let buf = get_uncompressed( "testdata/MRMS_ReflectivityAtLowestAltitude_00.50_20230406-120039.grib2.gz", )?; let earth_actual = EarthShapeDefinition::from_buf(&buf[0x33..]); @@ -155,7 +142,7 @@ mod tests { #[test] fn radii_for_shape_4() -> Result<(), Box> { - let buf = cat_as_bytes( + let buf = get_uncompressed( "testdata/Z__C_RJTD_20160822020000_NOWC_GPV_Ggis10km_Pphw10_FH0000-0100_grib2.bin", )?; let earth_actual = EarthShapeDefinition::from_buf(&buf[0x33..]); @@ -176,7 +163,7 @@ mod tests { #[test] fn radii_for_shape_6() -> Result<(), Box> { - let buf = unxz_as_bytes("testdata/gdas.t12z.pgrb2.0p25.f000.0-10.xz")?; + let buf = get_uncompressed("testdata/gdas.t12z.pgrb2.0p25.f000.0-10.xz")?; let earth_actual = EarthShapeDefinition::from_buf(&buf[0x33..]); let earth_expected = EarthShapeDefinition { shape_of_the_earth: 6,