src/slice.c: extend to allow multiple dims and pos
Similar to resize, slice now accepts an arbitrary number of dims and
positions.
#include "misc/mmio.h"
#include "misc/misc.h"
+#include "misc/opts.h"
#endif
-static const char usage_str[] = "dimension position <input> <output>";
-static const char help_str[] = "Extracts a slice from {position} along {dimension}.\n";
+static const char usage_str[] = "dim1 pos1 ... dimn posn <input> <output>";
+static const char help_str[] = "Extracts a slice from positions along dimensions.\n";
int main_slice(int argc, char* argv[])
{
- mini_cmdline(&argc, argv, 4, usage_str, help_str);
+ const struct opt_s opts[] = {};
+
+ cmdline(&argc, argv, 4, 1000, usage_str, help_str, ARRAY_SIZE(opts), opts);
num_init();
+ int count = argc - 3;
+ assert((count > 0) && (count % 2 == 0));
+
long in_dims[DIMS];
long out_dims[DIMS];
- complex float* in_data = load_cfl(argv[3], DIMS, in_dims);
+ complex float* in_data = load_cfl(argv[argc - 2], DIMS, in_dims);
+ md_copy_dims(DIMS, out_dims, in_dims);
+
+ long pos2[DIMS] = { [0 ... DIMS - 1] = 0 };
+ unsigned long flags = 0L;
+
+ for (int i = 0; i < count; i += 2) {
- int dim = atoi(argv[1]);
- int pos = atoi(argv[2]);
+ unsigned int dim = atoi(argv[i + 1]);
+ unsigned int pos = atoi(argv[i + 2]);
assert(dim < DIMS);
- assert(pos >= 0);
assert(pos < in_dims[dim]);
- for (int i = 0; i < DIMS; i++)
- out_dims[i] = in_dims[i];
-
out_dims[dim] = 1;
+ flags = MD_SET(flags, dim);
+ pos2[dim] = pos;
+ }
- complex float* out_data = create_cfl(argv[4], DIMS, out_dims);
- long pos2[DIMS] = { [0 ... DIMS - 1] = 0 };
- pos2[dim] = pos;
- md_slice(DIMS, MD_BIT(dim), pos2, in_dims, out_data, in_data, CFL_SIZE);
+ complex float* out_data = create_cfl(argv[argc - 1], DIMS, out_dims);
+
+ md_slice(DIMS, flags, pos2, in_dims, out_data, in_data, CFL_SIZE);
unmap_cfl(DIMS, out_dims, out_data);
unmap_cfl(DIMS, in_dims, in_data);
