Skip to content

Commit e507e57

Browse files
EliEli
authored andcommitted
Fixed index bugs with merging station.in files in preprocessor
1 parent 4bc0383 commit e507e57

2 files changed

Lines changed: 73 additions & 39 deletions

File tree

schimpy/schism_hotstart.py

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747
import datetime
4848
import logging
4949

50-
from schimpy.prepare_schism import create_gr3_with_polygons, setup_logger
50+
from schimpy.prepare_schism import create_gr3_with_polygons
51+
from schimpy.logging_config import configure_logging, resolve_loglevel
5152
from schimpy.schism_setup import create_schism_setup
5253
from schimpy.schism_mesh import read_mesh, write_mesh, SchismMeshGr3Reader, compare_mesh
5354
from schimpy import geo_tools
@@ -107,25 +108,19 @@ def __init__(self, input=None, modules=None, crs=None, envvar=None):
107108
raise ValueError("envvar must be a dictionary if provided.")
108109
self.envvar = envvar
109110

110-
def read_yaml(self, use_logging=True):
111+
def read_yaml(self):
111112
"""
112113
read yaml and load mesh grid
113114
"""
114-
print(self.input)
115+
logger.info("Reading yaml: %s", self.input)
115116
info = yaml_from_file(self.input, envvar=self.envvar)
116117

117118
if "out_dir" in info.keys():
118119
self.out_dir = info["out_dir"]
119120
else:
120121
self.out_dir = "./"
121-
print("No out_dir specified, using current directory as output directory.")
122-
self.use_logging = use_logging
123-
if self.use_logging:
124-
setup_logger(self.out_dir)
125-
self.logger = logging.getLogger("SCHISM")
126-
else:
127-
self.logger = logging.getLogger("")
128-
self.logger.info("Start generating SCHISM hotstart...")
122+
logger.info("No out_dir specified, using current directory as output directory.")
123+
logger.info("Start generating SCHISM hotstart...")
129124

130125
hotstart_info = info["hotstart"]
131126
self.info = hotstart_info
@@ -147,8 +142,8 @@ def read_yaml(self, use_logging=True):
147142
"prepro_output_dir": self.out_dir,
148143
}
149144
inputs = yaml_from_dict(inputs, envvar=self.envvar)
150-
s = create_schism_setup(self.hgrid_fn, self.logger)
151-
create_gr3_with_polygons(s, inputs, self.logger)
145+
s = create_schism_setup(self.hgrid_fn, logger)
146+
create_gr3_with_polygons(s, inputs, logger)
152147
variables.remove("elev.ic")
153148

154149
if "restart_time" in hotstart_info.keys():
@@ -2017,13 +2012,34 @@ def project_mesh(mesh, new_crs):
20172012

20182013

20192014
import click
2015+
from pathlib import Path
2016+
2017+
logger = logging.getLogger(__name__)
20202018

20212019

20222020
@click.command()
2023-
@click.option('--input', type=str, required=True, help='input yaml file for hotstart')
2024-
def create_hotstart_cli(input):
2025-
"""Create hotstart for a schism run"""
2026-
h = hotstart(input)
2021+
@click.argument('input_file', required=False)
2022+
@click.option('--input', 'input_opt', type=str, default=None, help='input yaml file for hotstart')
2023+
@click.option('--logdir', type=click.Path(path_type=Path), default=None, help='directory for log file')
2024+
@click.option('--debug', is_flag=True, help='enable debug logging')
2025+
@click.option('--quiet', is_flag=True, help='suppress console logging')
2026+
@click.help_option('-h', '--help')
2027+
def create_hotstart_cli(input_file, input_opt, logdir, debug, quiet):
2028+
"""Create hotstart initial condition for a SCHISM run"""
2029+
level, console = resolve_loglevel(debug=debug, quiet=quiet)
2030+
configure_logging(
2031+
package_name='schimpy',
2032+
level=level,
2033+
console=console,
2034+
logdir=logdir,
2035+
logfile_prefix='hotstart',
2036+
)
2037+
2038+
input_path = input_opt if input_opt is not None else input_file
2039+
if input_path is None:
2040+
raise click.UsageError('Please provide an input file as an argument or with --input')
2041+
2042+
h = hotstart(input_path)
20272043
h.create_hotstart()
20282044
output_fn = h.output_fn
20292045
hnc = h.nc_dataset

schimpy/station.py

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -250,23 +250,32 @@ def read_station_in(fpath):
250250
Returns
251251
-------
252252
Result : DataFrame
253-
DataFrame with hierarchical index (id,subloc) and columns x,y,z,name
253+
DataFrame with hierarchical index (station_id,subloc) and columns x,y,z,name
254254
255255
"""
256256

257+
rows = []
257258
with open(fpath, "r") as f:
258-
request = f.readline()
259-
n_entry = f.readline()
260-
stations = pd.read_csv(
261-
f,
262-
sep=r"\s+",
263-
header=None,
264-
names=["index", "x", "y", "z", "excl", "id", "subloc", "name"],
265-
usecols=["x", "y", "z", "id", "subloc", "name"],
266-
index_col=["id", "subloc"],
267-
na_values="-",
268-
keep_default_na=True,
269-
)
259+
f.readline() # request line
260+
f.readline() # n_entry line
261+
for line in f:
262+
line = line.strip()
263+
if not line:
264+
continue
265+
if "!" not in line:
266+
continue
267+
left, right = line.split("!", 1)
268+
parts_left = left.split()
269+
# parts_left: index, x, y, z
270+
x, y, z = float(parts_left[1]), float(parts_left[2]), float(parts_left[3])
271+
# right: station_id subloc name
272+
parts_right = right.strip().split(None, 2)
273+
station_id = parts_right[0]
274+
subloc = parts_right[1] if len(parts_right) > 1 else "default"
275+
name = parts_right[2].strip("'\" ") if len(parts_right) > 2 else ""
276+
rows.append((station_id, subloc, x, y, z, name))
277+
stations = pd.DataFrame(rows, columns=["station_id", "subloc", "x", "y", "z", "name"])
278+
stations = stations.set_index(["station_id", "subloc"])
270279
return stations
271280

272281

@@ -303,7 +312,7 @@ def write_station_in(fpath, station_in, request=None):
303312
# Then the specific requests, here written to a string buffer
304313
buffer2 = dfmerged.to_csv(
305314
None,
306-
columns=["x", "y", "z", "excl", "id", "subloc", "name"],
315+
columns=["x", "y", "z", "excl", "station_id", "subloc", "name"],
307316
index_label="id",
308317
sep=" ",
309318
float_format="%.2f",
@@ -342,8 +351,8 @@ def read_station_subloc(fpath):
342351
DataFrame with hierarchical index (id,subloc) and data column z
343352
344353
"""
345-
346-
df = pd.read_csv(fpath, sep=",", header=0, index_col=["id", "subloc"], comment="#")
354+
355+
df = pd.read_csv(fpath, sep=",", header=0, index_col=["station_id", "subloc"], comment="#")
347356
df["z"] = df.z
348357
return df[["z"]]
349358

@@ -375,7 +384,7 @@ def read_station_dbase(fpath):
375384
db = pd.read_csv(
376385
fpath, sep=",", comment="#", header=0, index_col="station_id", dtype={"agency_id": str}
377386
)
378-
db.index.name = "id"
387+
db.index.name = "station_id"
379388

380389
db["agency_id"] = db["agency_id"].str.replace("'", "", regex=True)
381390

@@ -405,12 +414,13 @@ def merge_station_subloc(station_dbase, station_subloc, default_z):
405414
DataFrame that links the information.
406415
407416
"""
408-
417+
print(station_dbase.head())
418+
print(station_subloc.head())
409419
merged = station_dbase.reset_index().merge(
410-
station_subloc.reset_index(), left_on="id", right_on="id", how="left"
420+
station_subloc.reset_index(), left_on="station_id", right_on="station_id", how="left"
411421
)
412422
merged.fillna({"subloc": "default", "z": default_z}, inplace=True)
413-
merged.set_index(["id", "subloc"], inplace=True)
423+
merged = merged.set_index(["station_id", "subloc"])
414424

415425
return merged
416426

@@ -834,8 +844,11 @@ def convert_db_station_in(
834844
sublocdb=None,
835845
station_request="all",
836846
default=-0.5,
847+
mask_col=None,
837848
):
838849
stations_utm = read_station_dbase(stationdb)
850+
if mask_col is not None:
851+
stations_utm = stations_utm[stations_utm[mask_col] == 1]
839852
ssubloc = read_station_subloc(sublocdb)
840853
stations_in = merge_station_subloc(stations_utm, ssubloc, default_z=-0.5)
841854
write_station_in(outfile, stations_in, request=station_request)
@@ -866,7 +879,12 @@ def convert_db_station_in(
866879
help="z coordinate used when there is no listing for station id (z coordinate, not subloc from surface)",
867880
)
868881
@click.option("--out", default="station.in", help="station.in formatted file")
869-
def convert_station_cli(station_db, subloc_db, request, default_zcor, out):
882+
@click.option(
883+
"--mask-col",
884+
default=None,
885+
help="Integer column in station dbase used to select stations (1=include)",
886+
)
887+
def convert_station_cli(station_db, subloc_db, request, default_zcor, out, mask_col):
870888
"""Create station.in file from station database (stations_utm.csv) and station subloc listing station_subloc.csv"""
871889
stationdb = station_db
872890
sublocdb = subloc_db
@@ -877,7 +895,7 @@ def convert_station_cli(station_db, subloc_db, request, default_zcor, out):
877895
if sublocdb is None:
878896
sublocdb = config_file("sublocations")
879897

880-
convert_db_station_in(outfile, stationdb, sublocdb, request, default)
898+
convert_db_station_in(outfile, stationdb, sublocdb, request, default, mask_col=mask_col)
881899

882900

883901
if __name__ == "__main__":

0 commit comments

Comments
 (0)