Skip to content

Commit

Permalink
use tempfiles to not access file multiple times
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristianBeilschmidt committed Sep 2, 2022
1 parent cb0ba5d commit cad4727
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 43 deletions.
46 changes: 44 additions & 2 deletions src/programs/raster/mdimtranslate.rs
Expand Up @@ -231,15 +231,55 @@ mod tests {

use crate::{DatasetOptions, Driver, GdalOpenFlags};

/// Create a copy of the test file in a temporary directory
/// and returns a tuple of the temp dir (for clean-up) as well as the path to the file.
/// We can remove this when <https://github.com/OSGeo/gdal/issues/6253> is resolved.
struct TempDataset {
_temp_dir: tempfile::TempDir,
temp_path: PathBuf,
}

impl TempDataset {
pub fn fixture(name: &str) -> Self {
let path = std::path::Path::new(file!())
.parent()
.unwrap()
.parent()
.unwrap()
.parent()
.unwrap()
.parent()
.unwrap()
.join("fixtures")
.join(name);

let temp_dir = tempfile::tempdir().unwrap();
let temp_path = temp_dir.path().join(path.file_name().unwrap());

std::fs::copy(&path, &temp_path).unwrap();

Self {
_temp_dir: temp_dir,
temp_path,
}
}

pub fn path(&self) -> &Path {
&self.temp_path
}
}

#[test]
fn test_build_tiff_from_path() {
let fixture = TempDataset::fixture("cf_nasa_4326.nc");

let dataset_options = DatasetOptions {
open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER,
allowed_drivers: None,
open_options: None,
sibling_files: None,
};
let dataset = Dataset::open_ex("fixtures/cf_nasa_4326.nc", dataset_options).unwrap();
let dataset = Dataset::open_ex(fixture.path(), dataset_options).unwrap();

let mem_file_path = "/vsimem/2d3e9124-a7a0-413e-97b5-e79d46e50ff8";

Expand All @@ -263,13 +303,15 @@ mod tests {

#[test]
fn test_build_tiff_from_dataset() {
let fixture = TempDataset::fixture("cf_nasa_4326.nc");

let dataset_options = DatasetOptions {
open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER,
allowed_drivers: None,
open_options: None,
sibling_files: None,
};
let dataset = Dataset::open_ex("fixtures/cf_nasa_4326.nc", dataset_options).unwrap();
let dataset = Dataset::open_ex(fixture.path(), dataset_options).unwrap();

let driver = Driver::get_by_name("MEM").unwrap();
let output_dataset = driver.create("", 5, 7, 1).unwrap();
Expand Down
143 changes: 102 additions & 41 deletions src/raster/mdarray.rs
Expand Up @@ -782,33 +782,75 @@ impl Attribute {

#[cfg(test)]
mod tests {
use std::path::{Path, PathBuf};

use super::*;

use crate::{Dataset, DatasetOptions, GdalOpenFlags};

/// Create a copy of the test file in a temporary directory
/// and returns a tuple of the temp dir (for clean-up) as well as the path to the file.
/// We can remove this when <https://github.com/OSGeo/gdal/issues/6253> is resolved.
struct TempDataset {
_temp_dir: tempfile::TempDir,
temp_path: PathBuf,
}

impl TempDataset {
pub fn fixture(name: &str) -> Self {
let path = std::path::Path::new(file!())
.parent()
.unwrap()
.parent()
.unwrap()
.parent()
.unwrap()
.join("fixtures")
.join(name);

let temp_dir = tempfile::tempdir().unwrap();
let temp_path = temp_dir.path().join(path.file_name().unwrap());

std::fs::copy(&path, &temp_path).unwrap();

Self {
_temp_dir: temp_dir,
temp_path,
}
}

pub fn path(&self) -> &Path {
&self.temp_path
}
}

#[test]
fn test_root_group_name() {
let fixture = TempDataset::fixture("byte_no_cf.nc");

let options = DatasetOptions {
open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER,
allowed_drivers: None,
open_options: None,
sibling_files: None,
};
let dataset = Dataset::open_ex("fixtures/byte_no_cf.nc", options).unwrap();
let dataset = Dataset::open_ex(fixture.path(), options).unwrap();
let root_group = dataset.root_group().unwrap();
let root_group_name = root_group.name();
assert_eq!(root_group_name, "/");
}

#[test]
fn test_array_names() {
let fixture = TempDataset::fixture("byte_no_cf.nc");

let dataset_options = DatasetOptions {
open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER,
allowed_drivers: None,
open_options: None,
sibling_files: None,
};
let dataset = Dataset::open_ex("fixtures/byte_no_cf.nc", dataset_options).unwrap();
let dataset = Dataset::open_ex(fixture.path(), dataset_options).unwrap();
let root_group = dataset.root_group().unwrap();
let options = CslStringList::new(); //Driver specific options determining how groups should be retrieved. Pass nullptr for default behavior.
let array_names = root_group.array_names(options);
Expand All @@ -817,13 +859,15 @@ mod tests {

#[test]
fn test_n_dimension() {
let fixture = TempDataset::fixture("byte_no_cf.nc");

let dataset_options = DatasetOptions {
open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER,
allowed_drivers: None,
open_options: None,
sibling_files: None,
};
let dataset = Dataset::open_ex("fixtures/byte_no_cf.nc", dataset_options).unwrap();
let dataset = Dataset::open_ex(fixture.path(), dataset_options).unwrap();
let root_group = dataset.root_group().unwrap();
let array_name = "Band1".to_string();
let options = CslStringList::new(); //Driver specific options determining how the array should be opened. Pass nullptr for default behavior.
Expand All @@ -834,13 +878,15 @@ mod tests {

#[test]
fn test_n_elements() {
let fixture = TempDataset::fixture("byte_no_cf.nc");

let dataset_options = DatasetOptions {
open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER,
allowed_drivers: None,
open_options: None,
sibling_files: None,
};
let dataset = Dataset::open_ex("fixtures/byte_no_cf.nc", dataset_options).unwrap();
let dataset = Dataset::open_ex(fixture.path(), dataset_options).unwrap();
let root_group = dataset.root_group().unwrap();
let array_name = "Band1".to_string();
let options = CslStringList::new(); //Driver specific options determining how the array should be opened. Pass nullptr for default behavior.
Expand All @@ -851,13 +897,15 @@ mod tests {

#[test]
fn test_dimension_name() {
let fixture = TempDataset::fixture("byte_no_cf.nc");

let dataset_options = DatasetOptions {
open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER,
allowed_drivers: None,
open_options: None,
sibling_files: None,
};
let dataset = Dataset::open_ex("fixtures/byte_no_cf.nc", dataset_options).unwrap();
let dataset = Dataset::open_ex(fixture.path(), dataset_options).unwrap();
let root_group = dataset.root_group().unwrap();

// group dimensions
Expand All @@ -883,13 +931,15 @@ mod tests {

#[test]
fn test_dimension_size() {
let fixture = TempDataset::fixture("byte_no_cf.nc");

let dataset_options = DatasetOptions {
open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER,
allowed_drivers: None,
open_options: None,
sibling_files: None,
};
let dataset = Dataset::open_ex("fixtures/byte_no_cf.nc", dataset_options).unwrap();
let dataset = Dataset::open_ex(fixture.path(), dataset_options).unwrap();
let root_group = dataset.root_group().unwrap();
let array_name = "Band1".to_string();
let options = CslStringList::new(); //Driver specific options determining how the array should be opened. Pass nullptr for default behavior.
Expand All @@ -904,13 +954,15 @@ mod tests {

#[test]
fn test_read_data() {
let fixture = TempDataset::fixture("byte_no_cf.nc");

let dataset_options = DatasetOptions {
open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER,
allowed_drivers: None,
open_options: None,
sibling_files: None,
};
let dataset = Dataset::open_ex("fixtures/byte_no_cf.nc", dataset_options).unwrap();
let dataset = Dataset::open_ex(fixture.path(), dataset_options).unwrap();

let root_group = dataset.root_group().unwrap();
let md_array = root_group
Expand All @@ -924,13 +976,15 @@ mod tests {

#[test]
fn test_read_string_array() {
let fixture = TempDataset::fixture("alldatatypes.nc");

let dataset_options = DatasetOptions {
open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER,
allowed_drivers: None,
open_options: None,
sibling_files: None,
};
let dataset = Dataset::open_ex("fixtures/alldatatypes.nc", dataset_options).unwrap();
let dataset = Dataset::open_ex(fixture.path(), dataset_options).unwrap();

let root_group = dataset.root_group().unwrap();

Expand All @@ -950,13 +1004,15 @@ mod tests {

#[test]
fn test_datatype() {
let fixture = TempDataset::fixture("byte_no_cf.nc");

let dataset_options = DatasetOptions {
open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER,
allowed_drivers: None,
open_options: None,
sibling_files: None,
};
let dataset = Dataset::open_ex("fixtures/byte_no_cf.nc", dataset_options).unwrap();
let dataset = Dataset::open_ex(fixture.path(), dataset_options).unwrap();

let root_group = dataset.root_group().unwrap();

Expand All @@ -973,13 +1029,15 @@ mod tests {

#[test]
fn test_spatial_ref() {
let fixture = TempDataset::fixture("byte_no_cf.nc");

let dataset_options = DatasetOptions {
open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER,
allowed_drivers: None,
open_options: None,
sibling_files: None,
};
let dataset = Dataset::open_ex("fixtures/byte_no_cf.nc", dataset_options).unwrap();
let dataset = Dataset::open_ex(fixture.path(), dataset_options).unwrap();

let root_group = dataset.root_group().unwrap();
let md_array = root_group
Expand All @@ -995,13 +1053,15 @@ mod tests {

#[test]
fn test_no_data_value() {
let fixture = TempDataset::fixture("byte_no_cf.nc");

let dataset_options = DatasetOptions {
open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER,
allowed_drivers: None,
open_options: None,
sibling_files: None,
};
let dataset = Dataset::open_ex("fixtures/byte_no_cf.nc", dataset_options).unwrap();
let dataset = Dataset::open_ex(fixture.path(), dataset_options).unwrap();

let root_group = dataset.root_group().unwrap();
let md_array = root_group
Expand All @@ -1013,13 +1073,15 @@ mod tests {

#[test]
fn test_attributes() {
let fixture = TempDataset::fixture("cf_nasa_4326.nc");

let dataset_options = DatasetOptions {
open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER,
allowed_drivers: None,
open_options: None,
sibling_files: None,
};
let dataset = Dataset::open_ex("fixtures/cf_nasa_4326.nc", dataset_options).unwrap();
let dataset = Dataset::open_ex(fixture.path(), dataset_options).unwrap();

let root_group = dataset.root_group().unwrap();

Expand Down Expand Up @@ -1064,13 +1126,15 @@ mod tests {

#[test]
fn test_unit() {
let fixture = TempDataset::fixture("cf_nasa_4326.nc");

let dataset_options = DatasetOptions {
open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER,
allowed_drivers: None,
open_options: None,
sibling_files: None,
};
let dataset = Dataset::open_ex("fixtures/cf_nasa_4326.nc", dataset_options).unwrap();
let dataset = Dataset::open_ex(fixture.path(), dataset_options).unwrap();

let root_group = dataset.root_group().unwrap();

Expand Down Expand Up @@ -1101,34 +1165,31 @@ mod tests {

#[test]
fn test_stats() {
{
let dataset_options = DatasetOptions {
open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER,
allowed_drivers: None,
open_options: None,
sibling_files: None,
};
let dataset = Dataset::open_ex("fixtures/byte_no_cf.nc", dataset_options).unwrap();
let root_group = dataset.root_group().unwrap();
let array_name = "Band1".to_string();
let options = CslStringList::new(); //Driver specific options determining how the array should be opened. Pass nullptr for default behavior.
let md_array = root_group.open_md_array(&array_name, options).unwrap();

assert!(md_array.get_statistics(false, true).unwrap().is_none());

assert_eq!(
md_array.get_statistics(true, true).unwrap().unwrap(),
MdStatisticsAll {
min: 74.0,
max: 255.0,
mean: 126.76500000000001,
std_dev: 22.928470838675654,
valid_count: 400,
}
);
}
let fixture = TempDataset::fixture("byte_no_cf.nc");

// clean up aux file
std::fs::remove_file("fixtures/byte_no_cf.nc.aux.xml").unwrap();
let dataset_options = DatasetOptions {
open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER,
allowed_drivers: None,
open_options: None,
sibling_files: None,
};
let dataset = Dataset::open_ex(fixture.path(), dataset_options).unwrap();
let root_group = dataset.root_group().unwrap();
let array_name = "Band1".to_string();
let options = CslStringList::new(); //Driver specific options determining how the array should be opened. Pass nullptr for default behavior.
let md_array = root_group.open_md_array(&array_name, options).unwrap();

assert!(md_array.get_statistics(false, true).unwrap().is_none());

assert_eq!(
md_array.get_statistics(true, true).unwrap().unwrap(),
MdStatisticsAll {
min: 74.0,
max: 255.0,
mean: 126.76500000000001,
std_dev: 22.928470838675654,
valid_count: 400,
}
);
}
}

0 comments on commit cad4727

Please sign in to comment.