From 7202cf289099709914ba18fe45f214de224ef164 Mon Sep 17 00:00:00 2001
From: "Joachim Wuttke (h)" <j.wuttke@fz-juelich.de>
Date: Sat, 17 Jun 2023 16:47:44 +0200
Subject: [PATCH] correct figures; simplify do_show

---
 Tests/Examples/CMakeLists.txt | 21 +++++++++---------
 Wrap/Python/ba_plot.py        | 41 ++++++++++++++++++++++-------------
 2 files changed, 37 insertions(+), 25 deletions(-)

diff --git a/Tests/Examples/CMakeLists.txt b/Tests/Examples/CMakeLists.txt
index 85f2eccdcb9..f22ce5cc36c 100644
--- a/Tests/Examples/CMakeLists.txt
+++ b/Tests/Examples/CMakeLists.txt
@@ -27,8 +27,9 @@ add_custom_target(excopy
 ####################################################################################################
 
 macro(parse_example example)
-    cmake_path(SET EXAMPLE_SCRIPT NORMALIZE ${EXAMPLES_TEST_DIR}/${example}.py)
-    get_filename_component(EXAMPLE_NAME ${EXAMPLE_SCRIPT} NAME_WE)
+    cmake_path(SET MINI_SCRIPT NORMALIZE ${EXAMPLES_TEST_DIR}/${example}.py)
+    cmake_path(SET PUBL_SCRIPT NORMALIZE ${EXAMPLES_PUBL_DIR}/${example}.py)
+    get_filename_component(EXAMPLE_NAME ${MINI_SCRIPT} NAME_WE)
     get_filename_component(EXAMPLE_SUBDIR ${example} DIRECTORY)
 endmacro()
 
@@ -43,10 +44,10 @@ function(run_example example)
     parse_example(${example})
     string(REPLACE "/" "." TARGET_NAME Example.${example}.fig)
     add_test(NAME Example.run.${EXAMPLE_NAME}
-        ${launch_env} ${launch_py} ${EXAMPLE_SCRIPT} show=n)
+        ${launch_env} ${launch_py} ${MINI_SCRIPT} show=n)
     add_custom_target(${TARGET_NAME}
-        ${launch_env} ${launch_py} ${EXAMPLE_SCRIPT}
-           figfile=${FIG_DIR}/${EXAMPLE_SUBDIR}/${EXAMPLE_NAME}.png)
+        ${launch_env} ${launch_py} ${PUBL_SCRIPT}
+           show=n figfile=${FIG_DIR}/${EXAMPLE_SUBDIR}/${EXAMPLE_NAME}.png)
     add_dependencies(figures ${TARGET_NAME})
 endfunction()
 
@@ -55,7 +56,7 @@ function(run_plotless example)
     parse_example(${example})
     string(REPLACE "/" "." TARGET_NAME Example.${example}.run)
     add_test(NAME ${TARGET_NAME}
-        ${launch_env} ${launch_py} ${EXAMPLE_SCRIPT}
+        ${launch_env} ${launch_py} ${MINI_SCRIPT}
         WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
 endfunction()
 
@@ -65,7 +66,7 @@ function(run_manually example)
     string(REPLACE "/" "." TARGET_NAME Example.${example}.run)
     add_custom_target(${TARGET_NAME}
         COMMAND echo "\n### MANUAL TEST: EXAMPLE ${EXAMPLE_NAME}\n\n"
-        ${launch_env} ${launch_py} ${EXAMPLE_SCRIPT}
+        ${launch_env} ${launch_py} ${MINI_SCRIPT}
         WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
     add_dependencies(manualtest ${TARGET_NAME})
 endfunction()
@@ -77,12 +78,12 @@ function(test_equality example reference tolerance)
     cmake_path(SET outfile NORMALIZE ${TEST_OUTPUT_DIR_PY_PERSIST}/${example})
     cmake_path(SET reffile NORMALIZE ${REFERENCE_DIR_EXAMPLES_MINI}/${reference})
     add_test(NAME ${TARGET_NAME}
-        ${launch_env} ${launch_py} ${EXAMPLE_SCRIPT} show=n datfile=${outfile}
+        ${launch_env} ${launch_py} ${MINI_SCRIPT} show=n datfile=${outfile}
             run_tolerance=${tolerance} run_reference=${reffile}
         WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
     add_custom_target(${TARGET_NAME}
-        ${launch_env} ${launch_py} ${EXAMPLE_SCRIPT}
-          figfile=${FIG_DIR}/${EXAMPLE_SUBDIR}/${EXAMPLE_NAME}.png)
+        ${launch_env} ${launch_py} ${PUBL_SCRIPT}
+          show=n figfile=${FIG_DIR}/${EXAMPLE_SUBDIR}/${EXAMPLE_NAME}.png)
     add_dependencies(figures ${TARGET_NAME})
 endfunction()
 
diff --git a/Wrap/Python/ba_plot.py b/Wrap/Python/ba_plot.py
index b9546a3dc82..3cac75e2056 100644
--- a/Wrap/Python/ba_plot.py
+++ b/Wrap/Python/ba_plot.py
@@ -53,6 +53,10 @@ rc('text', usetex=usetex_default)
 plotargs = {}
 simargs = {}
 runargs = {}
+datfile = None
+figfile = None
+saveformat = None
+do_show = True
 
 #  **************************************************************************  #
 #  internal functions
@@ -408,7 +412,9 @@ def parse_args(**kwargs):
         if key[0:4] == 'sim_':
             simargs[key[4:]] = tmp[key]
             print(f"""
-Warning: obsolete argument \"{key}\".
+Warning from bp.parse_args,
+called from {sys.argv[0]}:
+Obsolete argument \"{key}\".
 All arguments starting with \"sim_\" are obsolete since BornAgain 21.
 Support for bp.simargs will be removed in future releases.
 Replace \"bp.simargs['{key[4:]}']\" by a hard-coded value
@@ -419,33 +425,38 @@ or by variable under your own control.
         else:
             plotargs[key] = tmp[key]
 
-    global datfile
-    datfile = plotargs.pop('datfile', None)
-    if datfile:
+    global datfile, figfile, do_show
+    a = plotargs.pop('datfile', None)
+    if a:
+        datfile = a
         if pathlib.Path(datfile).suffix != '':
             raise Exception(
-                "Parameter 'datfile' must contain no suffix (we will append .int.gz)"
-            )
+                "Parameter 'datfile' must contain no suffix (we will append .int.gz)")
 
-    global figfile
-    figfile = plotargs.pop('figfile', None)
-    if figfile:
-        global save_format
+    a = plotargs.pop('figfile', None)
+    if a:
+        figfile = a
         save_format = pathlib.Path(figfile).suffix[1:]
         if save_format == '':
             raise Exception(
                 "Parameter 'figfile' must contain extension (like .pdf)")
 
-    global do_show
-    if figfile or datfile:
-        do_show = plotargs.pop('show', 'n') in ['y']
-    else:
-        do_show = not plotargs.pop('show', 'y') in ['n']
+    a = plotargs.pop('show', None)
+    if a:
+        if a == 'y':
+            print("DEBUG Y")
+            do_show = True
+        elif a == 'n':
+            print("DEBUG N")
+            do_show = False
+        else:
+            raise Exception("Parameter 'show' must be 'y' or 'n'")
 
 
 def show_or_export():
     if figfile:
         plt.savefig(figfile, format=save_format, bbox_inches='tight')
+    print(f"DEBUG S={do_show}")
     if do_show:
         plt.show()
 
-- 
GitLab