Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1pt] Fix inundation nation pathing errors #1026

Merged
merged 25 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
98281ee
Fixes for inundate_nation to create boolean rasters
RyanSpies-NOAA Oct 30, 2023
cef718f
Merge branch 'dev' into dev-fix-inundate-nation
RyanSpies-NOAA Nov 3, 2023
74f8e23
fixed bug with default huc lists
RobHanna-NOAA Nov 3, 2023
1dac8b3
took out debug line, added more prints
RobHanna-NOAA Nov 3, 2023
b285864
fix some output print errors
RobHanna-NOAA Nov 4, 2023
930c124
debugging
RobHanna-NOAA Nov 4, 2023
54de8ca
rolled most changes back to Ryan's orig fix
RobHanna-NOAA Nov 4, 2023
03e4772
minor adj to temp dir
RobHanna-NOAA Nov 4, 2023
bb18bf3
update notes in_nation
RobHanna-NOAA Nov 14, 2023
dc0cf9e
Update inundate_nation.py
RobHanna-NOAA Nov 14, 2023
dc5a5c2
Update CHANGELOG.md
RobHanna-NOAA Nov 14, 2023
d0a565d
Update params_template.env
RobHanna-NOAA Nov 27, 2023
2569ca5
Added vrt raster and final mosaic step; fixed logging
RyanSpies-NOAA Nov 27, 2023
fff562c
Merge branch 'dev-fix-inundate-nation' of https://github.com/NOAA-OWP…
RyanSpies-NOAA Nov 27, 2023
95b07e5
Merge branch 'dev' into dev-fix-inundate-nation
RobHanna-NOAA Nov 27, 2023
5852085
More fixes for logging info and adding log printing to screen
RyanSpies-NOAA Nov 27, 2023
a4b7b5e
Merge branch 'dev-fix-inundate-nation' of https://github.com/NOAA-OWP…
RyanSpies-NOAA Nov 27, 2023
4452be6
fix linting error
RobHanna-NOAA Nov 27, 2023
3445107
Added multi-threading to gdal.translate
RyanSpies-NOAA Nov 30, 2023
78fedab
Merge branch 'dev-fix-inundate-nation' of https://github.com/NOAA-OWP…
RyanSpies-NOAA Nov 30, 2023
15aad04
linting fix
RobHanna-NOAA Dec 1, 2023
e445a63
Revert "linting fix"
RobHanna-NOAA Dec 1, 2023
205525e
fixing linting errors
RobHanna-NOAA Dec 1, 2023
f71e0af
more linting fixes
RobHanna-NOAA Dec 1, 2023
d0806fe
Merge branch 'dev' into dev-fix-inundate-nation
CarsonPruitt-NOAA Dec 8, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/params_template.env
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export src_subdiv_toggle="True"
# text to append to output log and hydrotable file names (use for testing/debugging)
export vrough_suffix=""
# input file location with nwm feature_id and channel roughness and overbank roughness attributes
export vmann_input_file="$inputsDir/rating_curve/variable_roughness/mannings_global_06_12.csv"
export vmann_input_file="$inputsDir/rating_curve/variable_roughness/mannings_global_025_05.csv"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we want this changed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh sorry. Not sure why that change was made. Anyways. I fixed it and put it back. :)


#### SRC calibration variables
# input file location with nwm feature_id and recurrence flow values
Expand Down
15 changes: 15 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
All notable changes to this project will be documented in this file.
We follow the [Semantic Versioning 2.0.0](http://semver.org/) format.

## v4.4.x.x - 2023-11-14 - [PR#1026](https://github.com/NOAA-OWP/inundation-mapping/pull/1026)

A couple of directly related issues were fixed in this PR.
The initial problem came from Issue #[1025](https://github.com/NOAA-OWP/inundation-mapping/issues/1025) which was about a pathing issue for the outputs directory. In testing that fix, it exposed a few other pathing and file cleanup issues which are now fixed. We also added more console output to help view variables and pathing.

### Changes

- `config`/`params_template.env`: Updated for a newer mannings global file. Changed and tested by Ryan Spies.
- `tools`
- `inundate_mosiac_wrapper.py`: Took out a misleading and non-required print statement.
- `inundate_nation.py`: As mentioned above.

<br/><br/>


## v4.4.5.0 - 2023-10-26 - [PR#1018](https://github.com/NOAA-OWP/inundation-mapping/pull/1018)

During a recent BED attempt which added the new pre-clip system, it was erroring out on a number of hucs. It was issuing an error in the add_crosswalk.py script. While a minor bug does exist there, after a wide number of tests, the true culprit is the memory profile system embedded throughout FIM. This system has been around for at least a few years but not in use. It is not 100% clear why it became a problem with the addition of pre-clip, but that changes how records are loaded which likely affected memory at random times.
Expand Down
2 changes: 0 additions & 2 deletions tools/inundate_mosaic_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,6 @@ def produce_mosaicked_inundation(
"Please lower the num_workers.".format(num_workers, total_cpus_available)
)

fh.vprint("Running inundate for " + huc + "...", verbose)

# Call Inundate_gms
map_file = Inundate_gms(
hydrofabric_dir=hydrofabric_dir,
Expand Down
47 changes: 34 additions & 13 deletions tools/inundate_nation.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

import rasterio
from inundate_mosaic_wrapper import produce_mosaicked_inundation
from osgeo import gdal, ogr
from rasterio.merge import merge

from utils.shared_functions import FIM_Helpers as fh
Expand All @@ -26,6 +25,9 @@
# DEFAULT_OUTPUT_DIR = '/data/inundation_review/inundate_nation/mosaic_output/'


# TODO: Nov 2023, Logging system appears to be not working correctly.


def inundate_nation(fim_run_dir, output_dir, magnitude_key, flow_file, huc_list, inc_mosaic, job_number):
assert os.path.exists(flow_file), f"ERROR: could not find the flow file: {flow_file}"

Expand All @@ -37,10 +39,12 @@ def inundate_nation(fim_run_dir, output_dir, magnitude_key, flow_file, huc_list,
+ " max jobs will be used instead."
)

print()
print("Inundation Nation script starting...")

fim_version = os.path.basename(os.path.normpath(fim_run_dir))
logging.info(f"Using fim version: {fim_version}")
output_base_file_name = magnitude_key + "_" + fim_version
# print(output_base_file_name)

__setup_logger(output_dir, output_base_file_name)

Expand All @@ -65,7 +69,7 @@ def inundate_nation(fim_run_dir, output_dir, magnitude_key, flow_file, huc_list,
shutil.rmtree(magnitude_output_dir, ignore_errors=True)
os.mkdir(magnitude_output_dir)

if huc_list is None:
if huc_list == 'all' or len(huc_list) == 0:
huc_list = []
for huc in os.listdir(fim_run_dir):
# if (
Expand All @@ -78,22 +82,27 @@ def inundate_nation(fim_run_dir, output_dir, magnitude_key, flow_file, huc_list,
huc_list.append(huc)
else:
for huc in huc_list:
assert os.path.isdir(
fim_run_dir + os.sep + huc
), f'ERROR: could not find the input fim_dir location: {fim_run_dir + os.sep + huc}'
huc_path = os.path.join(fim_run_dir, huc)
assert os.path.isdir(huc_path), f'ERROR: could not find the input fim_dir location: {huc_path}'

print("Inundation raw mosaic outputs here: " + magnitude_output_dir)
huc_list.sort()

print("Inundation raw mosaic outputs will saved here: " + magnitude_output_dir)
print()

run_inundation([fim_run_dir, huc_list, magnitude_key, magnitude_output_dir, flow_file, job_number])

print()

# Perform mosaic operation
if inc_mosaic:
fh.print_current_date_time()
logging.info(datetime.now().strftime("%Y_%m_%d-%H_%M_%S"))
print("Performing bool mosaic process...")
logging.info("Performing bool mosaic process...")

output_bool_dir = os.path.join(output_dir, "bool_temp")
print(f"output_bool_dir is {output_bool_dir}")

if not os.path.exists(output_bool_dir):
os.mkdir(output_bool_dir)
else:
Expand All @@ -105,7 +114,7 @@ def inundate_nation(fim_run_dir, output_dir, magnitude_key, flow_file, huc_list,
for rasfile in os.listdir(magnitude_output_dir):
if rasfile.endswith(".tif") and "extent" in rasfile:
# p = magnitude_output_dir + rasfile
procs_list.append([magnitude_output_dir, rasfile, output_bool_dir])
procs_list.append([magnitude_output_dir, rasfile, output_bool_dir, fim_version])

# Multiprocess --> create boolean inundation rasters for all hucs
if len(procs_list) > 0:
Expand All @@ -116,11 +125,14 @@ def inundate_nation(fim_run_dir, output_dir, magnitude_key, flow_file, huc_list,
print(msg)
logging.info(msg)

# now cleanup the raw mosiac directories
# now cleanup the temp bool directory
shutil.rmtree(output_bool_dir, ignore_errors=True)

else:
print("Skipping mosiaking")

# now cleanup the raw mosiac directories
shutil.rmtree(magnitude_output_dir, ignore_errors=True)
# shutil.rmtree(magnitude_output_dir, ignore_errors=True)

fh.print_current_date_time()
logging.info(logging.info(datetime.now().strftime("%Y_%m_%d-%H_%M_%S")))
Expand Down Expand Up @@ -150,6 +162,12 @@ def run_inundation(args):
inundation_raster = os.path.join(magnitude_output_dir, magnitude + "_inund_extent.tif")

print("Running the NWM recurrence intervals for HUC inundation (extent) for magnitude: " + str(magnitude))
print(
"This will take a long time depending on the number of HUCs. No progress bar will be shown."
" Once is gets to mosiacing (if applicable), screen output will exist. To see if the script has frozen,"
" you should be able to watch the file system for some changes."
)
print()

produce_mosaicked_inundation(
fim_run_dir,
Expand All @@ -158,7 +176,7 @@ def run_inundation(args):
inundation_raster=inundation_raster,
num_workers=job_number,
remove_intermediate=True,
verbose=True,
verbose=False,
is_mosaic_for_branches=True,
)

Expand All @@ -167,6 +185,7 @@ def create_bool_rasters(args):
in_raster_dir = args[0]
rasfile = args[1]
output_bool_dir = args[2]
fim_version = args[3]

print("Calculating boolean inundate raster: " + rasfile)
p = in_raster_dir + os.sep + rasfile
Expand All @@ -189,7 +208,9 @@ def create_bool_rasters(args):
dtype="int8",
compress="lzw",
)
with rasterio.open(output_bool_dir + os.sep + "bool_" + rasfile, "w", **profile) as dst:
with rasterio.open(
output_bool_dir + os.sep + rasfile[:-4] + '_' + fim_version + '.tif', "w", **profile
) as dst:
dst.write(array.astype(rasterio.int8))


Expand Down