Skip to content

Commit

Permalink
Add --hide-progress to zarrs_ncvar2zarr
Browse files Browse the repository at this point in the history
  • Loading branch information
LDeakin committed Apr 26, 2024
1 parent 633c91c commit 0442a20
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added
- Add `--hide-progress` to `zarrs_ncvar2zarr`

### Changed
- Replace `--no_overwrite` with `--exists` in `zarrs_filter` and `zarrs_ome`
- Both support `erase` and `exit` options
Expand Down
24 changes: 21 additions & 3 deletions src/bin/zarrs_ncvar2zarr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ struct Cli {
#[arg(long, default_value_t = false)]
memory_test: bool,

/// Set to hide the progress bar.
#[arg(long)]
hide_progress: bool,

/// The path to a netCDF file or a directory of netcdf files.
path: PathBuf,

Expand All @@ -63,6 +67,14 @@ struct Cli {
out: PathBuf,
}

fn create_progress_bar(len: Option<u64>) -> ProgressBar {
if let Some(len) = len {
ProgressBar::new(len)
} else {
ProgressBar::hidden()
}
}

#[allow(clippy::too_many_arguments)]
fn ncfiles_to_array<TStore: ReadableWritableStorageTraits + ?Sized + 'static>(
nc_paths: &[PathBuf],
Expand All @@ -72,6 +84,7 @@ fn ncfiles_to_array<TStore: ReadableWritableStorageTraits + ?Sized + 'static>(
array: &Array<TStore>,
num_concurrent_blocks: Option<usize>,
validate: bool,
show_progress: bool,
) -> usize {
let style_all =
ProgressStyle::with_template("[{bar}] ({pos}/{len} blocks, {elapsed_precise}, ETA {eta})")
Expand Down Expand Up @@ -157,11 +170,15 @@ fn ncfiles_to_array<TStore: ReadableWritableStorageTraits + ?Sized + 'static>(
.par_chunks((nc_paths.len() + concurrent_blocks - 1) / concurrent_blocks);

let m = MultiProgress::new();
let pb_all = m.add(ProgressBar::new(enumerated_paths.len() as u64));
let pb_all = m.add(create_progress_bar(
show_progress.then_some(enumerated_paths.len() as u64),
));
pb_all.set_style(style_all.clone());
pb_all.set_position(0);
chunks.for_each(|blocks| {
let pb = m.add(ProgressBar::new(blocks.len() as u64));
let pb = m.add(create_progress_bar(
show_progress.then_some(blocks.len() as u64),
));
pb.set_style(style.clone());
pb.set_position(0);
for (idx, nc_path) in blocks {
Expand All @@ -173,7 +190,7 @@ fn ncfiles_to_array<TStore: ReadableWritableStorageTraits + ?Sized + 'static>(
});
pb_all.finish();
} else {
let pb = ProgressBar::new(nc_paths.len() as u64);
let pb = create_progress_bar(show_progress.then_some(nc_paths.len() as u64));
pb.set_style(style_all);
pb.set_position(0);
for (idx, nc_path) in nc_paths.iter().enumerate() {
Expand Down Expand Up @@ -323,6 +340,7 @@ fn main() {
&array,
cli.concurrent_blocks,
cli.validate,
!cli.hide_progress,
);
let duration_s = start.elapsed().as_secs_f32();

Expand Down

0 comments on commit 0442a20

Please sign in to comment.