From ae7a03031e303d31dd120605c1dbd75baa23b7b8 Mon Sep 17 00:00:00 2001 From: veenstrajelmer <60435591+veenstrajelmer@users.noreply.github.com> Date: Wed, 11 Oct 2023 11:37:26 +0200 Subject: [PATCH] prevent reading entire asc file upon finding header and skiprows (#573) --- dfm_tools/bathymetry.py | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/dfm_tools/bathymetry.py b/dfm_tools/bathymetry.py index cf7af068e..8b9bbe76d 100644 --- a/dfm_tools/bathymetry.py +++ b/dfm_tools/bathymetry.py @@ -57,29 +57,27 @@ def read_asc(file_asc:str) -> xr.Dataset: the lat/lon coordinates as separate coordinate variables. """ - with open(file_asc) as f: - lines = f.readlines() - # read header header_dict = {} - for il, line in enumerate(lines): - linesplit = line.split() - linestart = linesplit[0] - linestop = linesplit[-1] - - try: - # try to convert it to float, this will fail for headerlines - # it will succeed for numeric lines, but then the loop breaks - float(linestart) - skiprows = il - break - except ValueError: - # convert header values to int if possible or float if not + with open(file_asc) as f: + for linenum, line in enumerate(f, 1): + linesplit = line.split() + linestart = linesplit[0] + linestop = linesplit[-1] + try: - header_value = int(linestop) + # try to convert it to float, this will fail for headerlines + # it will succeed for numeric lines, but then the loop breaks + float(linestart) + break except ValueError: - header_value = float(linestop) - header_dict[linestart] = header_value + # convert header values to int if possible or float if not + try: + header_value = int(linestop) + except ValueError: + header_value = float(linestop) + header_dict[linestart] = header_value + skiprows = linenum # read data asc_np = np.loadtxt(file_asc, skiprows=skiprows, dtype=float)