44 """The ordering whose div B is smaller, with both values and the scale.
46 The order is None when the two are too close to choose between, so a
47 symmetric map is reported as undecided instead of arbitrarily transposed.
49 as_read, scale = fmt.div_b_rms(field, rng)
50 transposed, _ = fmt.div_b_rms(fmt.transpose_xy(field), rng)
51 lower, upper = sorted((as_read, transposed))
52 if upper <= lower * DIVERGENCE_MARGIN:
55 order =
"xslow" if as_read < transposed
else "yslow"
56 return order, as_read, transposed, scale
60 """Print what the file says about itself and what the field says."""
61 residuals = fmt.coord_residuals(path, rng)
62 print(f
" grid {rng.Nx} x {rng.Ny} x {rng.Nz} = {rng.N} nodes")
63 print(f
" extent mismatch {tuple(float(round(e, 4)) for e in rng.extent_mismatch())} bins")
65 print(
" coordinates absent")
68 f
" coordinates off by {tuple(float(round(r, 3)) for r in residuals)} bins "
69 f
"from canonical order -> {fmt.infer_data_order(path, rng) or 'neither'}"
73 f
" RMS(div B) xslow {as_read:.3e}, yslow {transposed:.3e} "
74 f
"(gradient scale {scale:.3e}) -> {order or 'too close to call'}"
76 on_axis = fmt.on_axis(field, rng)
77 peaks = [float(np.abs(on_axis[:, i]).max())
for i
in range(3)]
78 print(f
" peak on axis Bx {peaks[0]:.4f}, By {peaks[1]:.4f}, Bz {peaks[2]:.4f} T")
83 """Settle on the input ordering, or explain why it cannot be settled."""
85 from_coords = fmt.infer_data_order(path, rng)
87 if requested !=
"auto":
88 if from_field
is not None and requested != from_field:
90 f
"Warning: --data-order {requested} disagrees with the divergence "
91 f
"of the field, which points at {from_field}.",
96 if from_field
is not None and from_coords
is not None and from_coords != from_field:
98 f
"{path} is internally inconsistent: its coordinates are written in "
99 f
"{from_coords} order but its field is only divergence-free read as "
100 f
"{from_field}. Pass --data-order explicitly once you know which is "
101 "right; the coordinates are regenerated either way."
103 if from_coords
is not None:
105 if from_field
is not None:
106 print(f
"Using {from_field} from the divergence of the field; the map stores no usable coordinates.")
109 f
"{path} does not say what order it is in: its coordinates match neither "
110 "layout, and its divergence is the same read either way. Pass "
111 "--data-order explicitly."
116 parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
117 parser.add_argument(
"input", help=
"field map to read")
118 parser.add_argument(
"-o",
"--output", help=
"field map to write (omit to only report)")
121 choices=(
"auto", *fmt.ORDERS),
123 help=
"ordering of the input; auto requires the coordinates and the field to agree",
128 help=
"report on the input and exit non-zero unless it is already canonical",
130 args = parser.parse_args()
132 rng = fmt.read_range(args.input)
133 field = fmt.read_field(args.input, rng=rng)
135 print(f
"{args.input}:")
136 from_field =
describe(args.input, rng, field)
139 residuals = fmt.coord_residuals(args.input, rng)
141 canonical = from_field !=
"yslow" and residuals
is not None and max(residuals) < 0.5
142 print(
" verdict " + (
"canonical" if canonical
else "NEEDS REPACKING"))
143 return 0
if canonical
else 1
146 parser.error(
"give --output, or --check-only to just report")
148 order =
resolve_order(args.input, rng, field, args.data_order)
149 print(f
"Reading {args.input} as {order} and writing {args.output}")
150 fmt.canonicalise(args.input, args.output, order=order, rng=rng)
152 print(f
"{args.output}:")
153 written = fmt.read_field(args.output)
154 describe(args.output, fmt.read_range(args.output), written)
156 print(f
" field contents {before} -> {after} ({'unchanged' if before == after else 'reordered'})")