From 0867ad3c77208f4a901ac4789a733100992ee520 Mon Sep 17 00:00:00 2001
From: Jeff Candy <candy@fusion.gat.com>
Date: Fri, 23 Feb 2024 22:28:55 -0800
Subject: [PATCH] This version now checks for J < 0 and will not print
 input.gacode

---
 f2py/expro/expro.f90             |  1 +
 f2py/expro/expro_util.f90        |  5 ++---
 profiles_gen/bin/profiles_gen    |  8 +++++++-
 profiles_gen/src/prgen.f90       |  5 -----
 profiles_gen/src/prgen_write.f90 | 20 +++++++++++++++-----
 5 files changed, 25 insertions(+), 14 deletions(-)

diff --git a/f2py/expro/expro.f90 b/f2py/expro/expro.f90
index fa7409d75..522abcba2 100644
--- a/f2py/expro/expro.f90
+++ b/f2py/expro/expro.f90
@@ -180,6 +180,7 @@ module expro
   integer :: expro_ctrl_quasineutral_flag 
   integer :: expro_ctrl_numeq_flag
   integer :: expro_error=0
+  integer :: expro_jerr=0
 
   ! Header information
   character(len=70) :: expro_head_original =  '#  *original : null'
diff --git a/f2py/expro/expro_util.f90 b/f2py/expro/expro_util.f90
index 5fbcf924f..8b0210f3b 100644
--- a/f2py/expro/expro_util.f90
+++ b/f2py/expro/expro_util.f90
@@ -8,7 +8,7 @@ subroutine expro_compute_derived
   integer :: n
   integer :: i
   integer :: is
-  integer :: nx,err
+  integer :: nx
 
   double precision, parameter :: k  = 1.6022d-12 ! erg/eV
   double precision, parameter :: e  = 4.8032d-10 ! statcoul
@@ -209,7 +209,6 @@ subroutine expro_compute_derived
   ! r_min = a [m]
   r_min = expro_rmin(expro_n_exp)
   
-100 err=0
   do i=2,expro_n_exp
 
      ! Parameters to be passed to geo library   
@@ -267,7 +266,7 @@ subroutine expro_compute_derived
      call geo_interp(1,theta,.true.)
      if (minval(geov_jac_r) <= 0d0) then
         print '(a,i3,a,f5.2)','WARNING: (expro_util) J < 0 for i=',i,' in input.gacode: ',minval(geov_jac_r)
-        err = 1
+        expro_jerr = 1
      endif
 
      ! V, dV/dr and S (note that S=dV/dr only in a circle)
diff --git a/profiles_gen/bin/profiles_gen b/profiles_gen/bin/profiles_gen
index 66b7168f4..b6e8a2d4b 100755
--- a/profiles_gen/bin/profiles_gen
+++ b/profiles_gen/bin/profiles_gen
@@ -687,7 +687,13 @@ fi
 $EXEC
 
 if [ -f "success" ] ; then
-   echo "INFO: (profiles_gen) Done."
+   if [ `cat success` -eq "1" ] ; then
+      echo "INFO: (profiles_gen) Done."
+   else
+      m=0.001
+      r=$(echo "($PSINORM-$m)"| bc -l)
+      echo "ERROR: (profiles_gen) Rerun with -psinorm $r"
+   fi
 else
    echo "ERROR: (profiles_gen) *Failed*. Try running with -V option"
 fi
diff --git a/profiles_gen/src/prgen.f90 b/profiles_gen/src/prgen.f90
index e9b16654b..499b2b356 100644
--- a/profiles_gen/src/prgen.f90
+++ b/profiles_gen/src/prgen.f90
@@ -166,9 +166,4 @@ program prgen
   call prgen_swap
   call prgen_write
 
-  ! Successful completion
-  open(unit=1,file='success',status='replace')
-  write(1,*) 1
-  close(1)
-
 end program prgen
diff --git a/profiles_gen/src/prgen_write.f90 b/profiles_gen/src/prgen_write.f90
index 78fe4b849..1517273cf 100644
--- a/profiles_gen/src/prgen_write.f90
+++ b/profiles_gen/src/prgen_write.f90
@@ -48,7 +48,7 @@ subroutine prgen_write
      expro_shape_cos6 = shape_cos(6,:)
 
      expro_z_eff = zeff
-     
+
      ! EFIT passthrough functions
      expro_ptot = p_tot
      expro_fpol = fpol
@@ -63,10 +63,20 @@ subroutine prgen_write
   expro_head_gfile     = '#     *gfile : '//trim(file_g)
   expro_head_cerfile   = '#   *cerfile : '//trim(file_cer)
 
+  ! Run check for Jacobian
   call expro_compute_derived
-  call expro_write('input.gacode')
-  print '(a)','INFO: (prgen_write) Wrote input.gacode.'
-  !-------------------------------------------------------------------------------------
-   
+
+  ! Use unit=2 since expro use unit=1
+  open(unit=2,file='success',status='replace')
+  if (expro_jerr == 0) then
+     call expro_write('input.gacode')
+     print '(a)','INFO: (prgen_write) Wrote input.gacode.'
+     ! Successful completion
+     write(2,*) 1
+  else
+     write(2,*) 2
+  endif
+  close(2)
+
 end subroutine prgen_write