|
| 1 | +import typer, nt2, os |
| 2 | +from typing_extensions import Annotated |
| 3 | +import matplotlib.pyplot as plt |
| 4 | + |
| 5 | +app = typer.Typer() |
| 6 | + |
| 7 | + |
| 8 | +@app.command(help="Print the data info") |
| 9 | +def version(): |
| 10 | + print(nt2.__version__) |
| 11 | + |
| 12 | + |
| 13 | +def check_path(path: str) -> str: |
| 14 | + if not os.path.exists(path) or not ( |
| 15 | + os.path.exists(os.path.join(path, "fields")) |
| 16 | + or os.path.exists(os.path.join(path, "particles")) |
| 17 | + or os.path.exists(os.path.join(path, "spectra")) |
| 18 | + ): |
| 19 | + raise typer.BadParameter( |
| 20 | + f"Path {path} does not exist or is not a valid nt2 data directory." |
| 21 | + ) |
| 22 | + return path |
| 23 | + |
| 24 | + |
| 25 | +def check_sel(sel: str) -> dict[str, int | float | slice]: |
| 26 | + if sel == "": |
| 27 | + return {} |
| 28 | + sel_list = sel.strip().split(";") |
| 29 | + sel_dict: dict[str, int | float | slice] = {} |
| 30 | + for _, s in enumerate(sel_list): |
| 31 | + coord, arg = s.strip().split("=", 1) |
| 32 | + coord = coord.strip() |
| 33 | + arg_exec = eval(arg.strip()) |
| 34 | + assert isinstance( |
| 35 | + arg_exec, (int, float, slice) |
| 36 | + ), f"Invalid selection argument for '{coord}': {arg_exec}. Must be int, float, or slice." |
| 37 | + sel_dict[coord] = arg_exec |
| 38 | + return sel_dict |
| 39 | + |
| 40 | + |
| 41 | +def check_species(species: int) -> int: |
| 42 | + if species < 0: |
| 43 | + raise typer.BadParameter( |
| 44 | + f"Species index must be a non-negative integer, got {species}." |
| 45 | + ) |
| 46 | + return species |
| 47 | + |
| 48 | + |
| 49 | +def check_what(what: str) -> str: |
| 50 | + valid_options = ["fields", "particles", "spectra"] |
| 51 | + if what not in valid_options: |
| 52 | + raise typer.BadParameter( |
| 53 | + f"Invalid option '{what}'. Valid options are: {', '.join(valid_options)}." |
| 54 | + ) |
| 55 | + return what |
| 56 | + |
| 57 | + |
| 58 | +@app.command(help="Print the data info") |
| 59 | +def show( |
| 60 | + path: Annotated[ |
| 61 | + str, |
| 62 | + typer.Argument( |
| 63 | + callback=check_path, |
| 64 | + help="Path to the data", |
| 65 | + ), |
| 66 | + ] = "", |
| 67 | +): |
| 68 | + data = nt2.Data(path) |
| 69 | + print(data.to_str()) |
| 70 | + |
| 71 | + |
| 72 | +@app.command(help="Plot the data") |
| 73 | +def plot( |
| 74 | + path: Annotated[ |
| 75 | + str, |
| 76 | + typer.Argument( |
| 77 | + callback=check_path, |
| 78 | + help="Path to the data", |
| 79 | + ), |
| 80 | + ] = "", |
| 81 | + what: Annotated[ |
| 82 | + Annotated[ |
| 83 | + str, |
| 84 | + typer.Option( |
| 85 | + callback=check_what, |
| 86 | + help="Which data to plot [fields, particles, spectra]", |
| 87 | + ), |
| 88 | + ], |
| 89 | + str, |
| 90 | + ] = "fields", |
| 91 | + fields: Annotated[ |
| 92 | + str, |
| 93 | + typer.Option( |
| 94 | + help="Which fields to plot (only when `what` is `fields`). Separate multiple fields with ';'. May contain regex. Empty = all fields. Example: `--fields \"E.*;B.*\"`", |
| 95 | + ), |
| 96 | + ] = "", |
| 97 | + # species: Annotated[ |
| 98 | + # Annotated[ |
| 99 | + # int, |
| 100 | + # typer.Option( |
| 101 | + # callback=check_species, |
| 102 | + # help="Which species to take (only when `what` is `particles`). 0 = all species", |
| 103 | + # ), |
| 104 | + # ], |
| 105 | + # str, |
| 106 | + # ] = 0, |
| 107 | + sel: Annotated[ |
| 108 | + str, |
| 109 | + typer.Option( |
| 110 | + callback=check_sel, |
| 111 | + help="Select a subset of the data with xarray.sel. Separate multiple selections with ';'. Example: `--sel \"t=23;z=slice(0, None)\"`", |
| 112 | + ), |
| 113 | + ] = "", |
| 114 | + isel: Annotated[ |
| 115 | + str, |
| 116 | + typer.Option( |
| 117 | + callback=check_sel, |
| 118 | + help="Select a subset of the data with xarray.isel. Separate multiple selections with ';'. Example: `--isel \"t=slice(None, 5);z=5\"`", |
| 119 | + ), |
| 120 | + ] = "", |
| 121 | +): |
| 122 | + fname = os.path.basename(path.strip("/")) |
| 123 | + data = nt2.Data(path) |
| 124 | + assert isinstance( |
| 125 | + sel, dict |
| 126 | + ), f"Invalid selection format: {sel}. Must be a dictionary." |
| 127 | + assert isinstance(isel, dict), f"Invalid isel format: {isel}. Must be a dictionary." |
| 128 | + if what == "fields": |
| 129 | + d = data.fields |
| 130 | + if sel != {}: |
| 131 | + slices = {} |
| 132 | + sels = {} |
| 133 | + slices: dict[str, slice | float | int] = { |
| 134 | + k: v for k, v in sel.items() if isinstance(v, slice) |
| 135 | + } |
| 136 | + sels: dict[str, slice | float | int] = { |
| 137 | + k: v for k, v in sel.items() if not isinstance(v, slice) |
| 138 | + } |
| 139 | + d = d.sel(**sels, method="nearest") |
| 140 | + d = d.sel(**slices) |
| 141 | + if isel != {}: |
| 142 | + d = d.isel(**isel) |
| 143 | + if fields != "": |
| 144 | + ret = d.inspect.plot( |
| 145 | + name=fname, only_fields=fields.split(";"), fig_kwargs={"dpi": 200} |
| 146 | + ) |
| 147 | + else: |
| 148 | + ret = d.inspect.plot(name=fname, fig_kwargs={"dpi": 200}) |
| 149 | + if not isinstance(ret, bool): |
| 150 | + plt.savefig(fname=f"{fname}.png") |
| 151 | + |
| 152 | + elif what == "particles": |
| 153 | + raise NotImplementedError("Particles plotting is not implemented yet.") |
| 154 | + elif what == "spectra": |
| 155 | + raise NotImplementedError("Spectra plotting is not implemented yet.") |
| 156 | + else: |
| 157 | + raise typer.BadParameter( |
| 158 | + f"Invalid option '{what}'. Valid options are: fields, particles, spectra." |
| 159 | + ) |
0 commit comments