|
| 1 | +#!/usr/bin/env python |
| 2 | +import click |
| 3 | +import os |
| 4 | +import sys |
| 5 | +import pandas as pd |
| 6 | +import schimpy.station as station |
| 7 | +import glob |
| 8 | +import numpy as np |
| 9 | + |
| 10 | + |
| 11 | +def read_staout_lite(fname, station_infile): |
| 12 | + """Read a SCHISM staout_* file into a pandas DataFrame |
| 13 | + A "light" version of `read_staout` from schimpy. |
| 14 | + The original `read_staout` requires `reftime`, which requires a prior knowledge of the start time. |
| 15 | +
|
| 16 | + Parameters |
| 17 | + ---------- |
| 18 | + fname : file path |
| 19 | + Path to input staout file or a variable name in ["elev", "air pressure", "wind_x", "wind_y", "temp", "salt", "u", "v", "w"] whose |
| 20 | + 1-index will be mapped to a name like staout_1 for elev |
| 21 | +
|
| 22 | + station_infile : str or DataFrame |
| 23 | + Path to station.in file or DataFrame from read_station_in |
| 24 | + """ |
| 25 | + if isinstance(station_infile, str): |
| 26 | + station_in = station.read_station_in(station_infile) |
| 27 | + else: |
| 28 | + station_in = station_infile |
| 29 | + |
| 30 | + staout = pd.read_csv(fname, index_col=0, sep=r"\s+", header=None) |
| 31 | + station_index = station_in.index.copy() |
| 32 | + staout.columns = station_index |
| 33 | + staout.columns = [f"{loc}_{subloc}" for loc, subloc in staout.columns] |
| 34 | + |
| 35 | + return staout |
| 36 | + |
| 37 | + |
| 38 | +def write_staout(staout: pd.DataFrame, out_path): |
| 39 | + staout.index = staout.index.map("{:0.6E}".format) |
| 40 | + staout.to_csv( |
| 41 | + out_path, |
| 42 | + sep="\t", |
| 43 | + header=None, |
| 44 | + na_rep="-999", |
| 45 | + float_format="%.6e", |
| 46 | + ) |
| 47 | + |
| 48 | + |
| 49 | +def create_station_output( |
| 50 | + input_type, old_input, new_input, out_dir, overwrite_existing, target |
| 51 | +): |
| 52 | + file_list = glob.glob(target) |
| 53 | + |
| 54 | + # check for overwriting |
| 55 | + for file in file_list: |
| 56 | + out_path = os.path.join(out_dir, os.path.basename(file)) |
| 57 | + if os.path.exists(out_path): |
| 58 | + if overwrite_existing == True: |
| 59 | + print(f"Warning: {out_path} already exists and will be overwritten.") |
| 60 | + else: |
| 61 | + raise Exception( |
| 62 | + f"Error: {out_path} already exists. Rename it or set 'overwrite_existing=True' to overwrite." |
| 63 | + ) |
| 64 | + |
| 65 | + # station.in |
| 66 | + if input_type == "station": |
| 67 | + |
| 68 | + station_new = station.read_station_in(new_input) |
| 69 | + |
| 70 | + # Get the list of columns from station_new |
| 71 | + out_columns = [f"{loc}_{subloc}" for loc, subloc in station_new.index] |
| 72 | + |
| 73 | + for file in file_list: |
| 74 | + staout_old = read_staout_lite(file, old_input) |
| 75 | + |
| 76 | + # Identify columns in station_new that are missing from staout_old, fill with NaN |
| 77 | + missing_cols = [col for col in out_columns if col not in staout_old.columns] |
| 78 | + for col in missing_cols: |
| 79 | + staout_old[col] = np.nan |
| 80 | + |
| 81 | + # Reorder new staout to match the column order of B |
| 82 | + staout_new = staout_old[out_columns] |
| 83 | + |
| 84 | + write_staout(staout_new, os.path.join(out_dir, os.path.basename(file))) |
| 85 | + |
| 86 | + |
| 87 | +@click.command() |
| 88 | +@click.option( |
| 89 | + "--input_type", |
| 90 | + default=None, |
| 91 | + required=True, |
| 92 | + type=str, |
| 93 | + help="Type of input. Must be one of: `station`, `fluxflag`, `fluxlines`", |
| 94 | +) |
| 95 | +@click.option( |
| 96 | + "--old_input", |
| 97 | + default=None, |
| 98 | + required=True, |
| 99 | + type=click.Path(exists=True), |
| 100 | + help="Reference input file from a previous run", |
| 101 | +) |
| 102 | +@click.option( |
| 103 | + "--new_input", |
| 104 | + default=None, |
| 105 | + required=True, |
| 106 | + type=click.Path(exists=True), |
| 107 | + help="station.in file for the current simulation", |
| 108 | +) |
| 109 | +@click.option( |
| 110 | + "--out_dir", |
| 111 | + default=".", |
| 112 | + required=False, |
| 113 | + type=click.Path(exists=True), |
| 114 | + help="output location", |
| 115 | +) |
| 116 | +@click.option( |
| 117 | + "--overwrite_existing", |
| 118 | + default=None, |
| 119 | + required=False, |
| 120 | + type=bool, |
| 121 | + help="If true, existing output files will be overwritten. If false, warning given without generating file.", |
| 122 | +) |
| 123 | +@click.argument("target", metavar="target") |
| 124 | +@click.help_option("-h", "--help") |
| 125 | +def create_station_output_cli( |
| 126 | + input_type, old_input, new_input, out_dir, overwrite_existing, target |
| 127 | +): |
| 128 | + """ |
| 129 | + Prepares station-related SCHISM inputs |
| 130 | + target: SCHISM input associated with `input_type`. For example, use following input_type and target pair:\n |
| 131 | + station : staout* |
| 132 | + """ |
| 133 | + |
| 134 | + create_station_output( |
| 135 | + input_type, old_input, new_input, out_dir, overwrite_existing, target |
| 136 | + ) |
| 137 | + |
| 138 | + |
| 139 | +if __name__ == "__main__": |
| 140 | + create_station_output_cli() |
0 commit comments