NOAA-OWP / t-route

Tree based hydrologic and hydraulic routing
Other
42 stars 47 forks source link

Turn Kernel Test compile into an input #399

Open jameshalgren opened 2 years ago

jameshalgren commented 2 years ago

_Originally posted by @jameshalgren in https://github.com/NOAA-OWP/t-route/pull/393#discussion_r700475696_

Consider a solution something like the following. This works for the bespoke test but doesn't properly load the libraries for the pytest script...

diff --git a/src/fortran_routing/mc_pylink_v00/MC_singleSeg_singleTS/mc_sseg_stime_NOLOOP_demo.py b/src/fortran_routing/mc_pylink_v00/MC_singleSeg_singleTS/mc_sseg_stime_NOLOOP_demo.py
index 44a2890..b077e78 100644
--- a/src/fortran_routing/mc_pylink_v00/MC_singleSeg_singleTS/mc_sseg_stime_NOLOOP_demo.py
+++ b/src/fortran_routing/mc_pylink_v00/MC_singleSeg_singleTS/mc_sseg_stime_NOLOOP_demo.py
@@ -1,81 +1,102 @@
 import traceback
+import argparse
 from troute.routing.fast_reach import reach
 from test_suite_parameters import generate_conus_MC_parameters
+import sys

-debuglevel = 0
-COMPILE = True
-if COMPILE:
-    try:
-        import subprocess
-
-        fortran_compile_call = []
-        fortran_compile_call.append(r"f2py3")
-        fortran_compile_call.append(r"-c")
-        fortran_compile_call.append(r"varPrecision.f90")
-        fortran_compile_call.append(r"MUSKINGCUNGE.f90")
-        fortran_compile_call.append(r"-m")
-        fortran_compile_call.append(r"mc_wrf_hydro")
-
-        if debuglevel <= -1:
-            print(fortran_compile_call)
-        if debuglevel <= -2:
-            subprocess.run(fortran_compile_call)
-        else:
-            subprocess.run(
-                fortran_compile_call,
-                stdout=subprocess.DEVNULL,
-                stderr=subprocess.DEVNULL,
-            )
-        from mc_wrf_hydro import submuskingcunge_wrf_module
-
-    except Exception as e:
-        print(e)
-        if debuglevel <= -1:
-            traceback.print_exc()
-else:
-    from mc_wrf_hydro import submuskingcunge_wrf_module

-debuglevel = 0
-if COMPILE:
-    try:
-        import subprocess
-
-        fortran_compile_call = []
-        fortran_compile_call.append(r"f2py3")
-        fortran_compile_call.append(r"-c")
-        fortran_compile_call.append(r"varPrecision.f90")
-        fortran_compile_call.append(r"MCsingleSegStime_f2py_NOLOOP.f90")
-        fortran_compile_call.append(r"-m")
-        fortran_compile_call.append(r"mc_sseg_stime")
-        # fortran_compile_call.append(r"--opt='-fdefault-real-8'")
-
-        if debuglevel <= -1:
-            print(fortran_compile_call)
-        if debuglevel <= -2:
-            subprocess.run(fortran_compile_call)
-        else:
-            subprocess.run(
-                fortran_compile_call,
-                stdout=subprocess.DEVNULL,
-                stderr=subprocess.DEVNULL,
-            )
-        from mc_sseg_stime import muskingcunge_module
-
-        # import mc_sseg_stime as muskingcunge_module
-    except Exception as e:
-        print(e)
-        if debuglevel <= -1:
-            traceback.print_exc()
-else:
-    from mc_sseg_stime import muskingcunge_module
+def _handle_args(argv):
+    parser = argparse.ArgumentParser(
+        formatter_class=argparse.ArgumentDefaultsHelpFormatter
+    )
+    parser.add_argument(
+        "-ct",
+        "--compile-Troute",
+        dest="compile_troute",
+        help="set this flag to trigger compilation of the f2py troute model",
+        action="store_true",
+    )
+    parser.add_argument(
+        "-cw",
+        "--compile-WRF",
+        dest="compile_wrf",
+        help="set this flag to trigger compilation of the f2py wrf model",
+        action="store_true",
+    )
+    parser.add_argument(
+        "--debuglevel",
+        help="Set the debuglevel",
+        dest="debuglevel",
+        choices=[0, 1, 2, 3],
+        default=0,
+        type=int,
+    )
+    return parser.parse_args()
+
+
+def _prep_f2py_wrapped_kernels(COMPILE_WRF = True, COMPILE_TROUTE = True, debuglevel = 0):
+    debuglevel = -debuglevel
+    if COMPILE_WRF:
+        try:
+            import subprocess
+
+            fortran_compile_call = []
+            fortran_compile_call.append(r"f2py3")
+            fortran_compile_call.append(r"-c")
+            fortran_compile_call.append(r"varPrecision.f90")
+            fortran_compile_call.append(r"MUSKINGCUNGE.f90")
+            fortran_compile_call.append(r"-m")
+            fortran_compile_call.append(r"mc_wrf_hydro")
+
+            if debuglevel <= -1:
+                print(fortran_compile_call)
+            if debuglevel <= -2:
+                subprocess.run(fortran_compile_call)
+            else:
+                subprocess.run(
+                    fortran_compile_call,
+                    stdout=subprocess.DEVNULL,
+                    stderr=subprocess.DEVNULL,
+                )
+
+        except Exception as e:
+            print(e)
+            if debuglevel <= -1:
+                traceback.print_exc()
+
+    if COMPILE_TROUTE:
+        try:
+            import subprocess
+
+            fortran_compile_call = []
+            fortran_compile_call.append(r"f2py3")
+            fortran_compile_call.append(r"-c")
+            fortran_compile_call.append(r"varPrecision.f90")
+            fortran_compile_call.append(r"MCsingleSegStime_f2py_NOLOOP.f90")
+            fortran_compile_call.append(r"-m")
+            fortran_compile_call.append(r"mc_sseg_stime")
+            # fortran_compile_call.append(r"--opt='-fdefault-real-8'")
+
+            if debuglevel <= -1:
+                print(fortran_compile_call)
+            if debuglevel <= -2:
+                subprocess.run(fortran_compile_call)
+            else:
+                subprocess.run(
+                    fortran_compile_call,
+                    stdout=subprocess.DEVNULL,
+                    stderr=subprocess.DEVNULL,
+                )
+
+        except Exception as e:
+            print(e)
+            if debuglevel <= -1:
+                traceback.print_exc()

-    # import mc_sseg_stime as muskingcunge_module

 # # Method 1
 # Python: time loop; segment loop; constant channel variables are passed to Fortran
 # Fortran: Take constant variable values and then run MC for a single segment
-
-
 def compute_mc_up2down_ReachbySegment():
     """HANDLE LOOPING,
         Then call single segment routine for each segment"""
@@ -337,7 +358,17 @@ def activate_compound_channel():
     )

-def main():
+def main(argv):
+
+    args = _handle_args(argv)
+    if args.compile_troute or args.compile_wrf:
+        _prep_f2py_wrapped_kernels(args.compile_wrf, args.compile_troute, args.debuglevel)
+
+    global submuskingcunge_wrf_module
+    global muskingcunge_module
+    from mc_wrf_hydro import submuskingcunge_wrf_module
+    from mc_sseg_stime import muskingcunge_module
+
     single_vs_double()
     activate_compound_channel()

@@ -465,4 +496,4 @@ def compare_methods(

 if __name__ == "__main__":
-    main()
+    main(sys.argv)
jameshalgren commented 2 years ago

@groutr, you asked:

Should this be in the docstring for demo.compare_methods? Tied to these lines in new test kernel

src/fortran_routing/mc_pylink_v00/MC_singleSeg_singleTS/test_MC_kernel.py
def random_mc_input_tuple():
return generate_conus_MC_parameters(1,16)

If we pursue this issue, perhaps we could update the docstrings at that time...