Index: palm/trunk/UTIL/chemistry/gasphase_preproc/kpp4palm/bin/kpp4palm.ksh
===================================================================
--- palm/trunk/UTIL/chemistry/gasphase_preproc/kpp4palm/bin/kpp4palm.ksh	(revision 2718)
+++ palm/trunk/UTIL/chemistry/gasphase_preproc/kpp4palm/bin/kpp4palm.ksh	(revision 3298)
@@ -21,4 +21,15 @@
 # Copyright 2017-2018  Leibniz Universitaet Hannover
 #------------------------------------------------------------------------------#
+# Nov. 2016: Initial Version of KPP chemistry convertor adapted for PALM 
+# by Klaus Ketelsen
+#
+# This code is a modified version of KP4 (JÃ¶ckel, P., Kerkweg, A., Pozzer, A., 
+# Sander, R., Tost, H., Riede, H., Baumgaertner, A., Gromov, S., and Kern, B.,
+# 2010: Development cycle 2 of the Modular Earth Submodel System (MESSy2), 
+# Geosci. Model Dev., 3, 717-752, https://doi.org/10.5194/gmd-3-717-2010).
+# KP4 is part of the Modular Earth Submodel System (MESSy), which is is
+# available under the  GNU General Public License (GPL).
+#
+#------------------------------------------------------------------------------#
 #
 # Current revisions:
@@ -29,7 +40,28 @@
 # -----------------
 # $Id$ 
+# forkel   25. September 2018: Added cat for $MECH to pass mechanism name to kpp4palm
+# ketelsen 18. September 2018: Added cat for '#INLINE F90_GLOBAL' 
+# (moved here from mechanisms/def_MECH/chem_gasphase.kpp
+#
+# forkel: 14. September  2018: WCOPY removed
+# ketelsen: July 2018: Adaptations for vektor mode
+# forkel June 2018: re-established original case of subroutine names
+# forkel May 2018: additional copying of chem_gasphase_mod.f90 into $DEFDIR
+# forkel 20.04.2018: removed  wlamch and wlamch_add from $KPP_SUBROUTINE_LIST
+#                    (epsilon(one) is used now)
+# forkel March 2017
+# Re-introduced relative path for KPP_HOME
+# Subroutine list adapted to lowercase subroutine names
+# Added arr2, removed update_sun and k_3rd from subroutine list
+# Renamed output file to chem_gasphase_mod
+# Renamed this file from kp4/ksh to kpp4kpp.ksh
+# changed location of def_mechanism directories to gasphase_preproc/mechanisms
+# 
+# 
+# 2718 2018-01-02 08:49:38Z maronga
 # Initial revision
 # 
 # 
+##########################################################################
 #
 #
@@ -63,4 +95,5 @@
 # Default
 
+MECH=smog
 OUTDIR=`pwd`/../../../SOURCE
 OUTFILE=chem_gasphase_mod
@@ -70,6 +103,7 @@
 VLEN=1
 KEEP="NO"
-DE_INDEX="NO"
-DE_INDEX_FAST="NO"
+UPDT="NO"
+DE_INDEX=0
+DE_INDEX_FAST="YES"
 
 export KPP_SOLVER=Rosenbrock
@@ -77,10 +111,9 @@
 # get Command line option
 
-echo xxxxxxxxxx
-while  getopts :d:ifkp:o:s:v:w:  c     # get options
+while  getopts :m:i:fkup:o:s:vl:w:  c     # get options
 do case $c in
-      d)   DEFDIR=$OPTARG;;          # directory of definition files
-
-      i)   DE_INDEX="YES";;          # if set, deindexing
+      m)   MECH=$OPTARG;;            # mechanism
+
+      i)   DE_INDEX=$OPTARG;;        # if set, deindexing
 
       f)   DE_INDEX_FAST="YES";;     # if set, fast deindexing
@@ -92,13 +125,16 @@
       p)   PREFIX=$OPTARG;;          # Name Prefix
 
-      s)   KPP_SOLVER=$OPTARG;;      # Name Prefix
-
-      v)   MODE="vector"
-           VLEN=$OPTARG;;            # Set to vector Mode
+      s)   KPP_SOLVER=$OPTARG;;      # Update sample f90 code in the def_MECH directory
+
+      u)   UPDT="YES";;              # keep Working directory
+
+      v)   MODE="vector";;           # Set to vector Mode
+
+      l)   VLEN=$OPTARG;;            # Set vector length
 
       w)   WORK=$OPTARG;;            # Working directory
 
       \?)  print ${0##*/} "unknown option:" $OPTARG
-           print "USAGE: ${0##*/} [ -d dir -e -k -o dir -p name -s solver -v length -w dir ] "
+           print "USAGE: ${0##*/} [ -m dir -e -k -u -o dir -p name -s solver -v -l length -w dir ] "
            exit 1;;
    esac
@@ -106,7 +142,14 @@
 shift OPTIND-1
 
-echo $DEFDIR
+echo MECHANISM = $MECH
+echo DE_INDEX = $DE_INDEX
+echo KEEP = $KEEP
+echo UPDT = $UPDT
+echo MODE = $MODE
+echo VLEN = $VLEN
 
 DEF_PREFIX=${PREFIX}.kpp
+DEFDIR=`pwd`/mechanisms/def_$MECH
+echo DEFDIR = $DEFDIR
 
 # Create or clean working directory
@@ -122,9 +165,9 @@
 
 
-KPP_SUBROUTINE_LIST="initialize"
-KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST integrate fun"
-KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST kppsolve kppdecomp wlamch wlamch_add"
-KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST jac_sp k_arr "
-KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST update_rconst arr2"
+KPP_SUBROUTINE_LIST="Initialize"
+KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST INTEGRATE Fun"
+KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST KppSolve KppDecomp"
+KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST Jac_SP k_arr "
+KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST Update_RCONST ARR2"
 KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST initialize_kpp_ctrl error_output"
 
@@ -149,5 +192,9 @@
 
 # Interface ignore list
-KPP_INTERFACE_IGNORE="waxpy wcopy"
+KPP_INTERFACE_IGNORE=" "
+
+echo " "
+echo KPP_SOLVER $KPP_SOLVER
+echo " "
 
 case $KPP_SOLVER in
@@ -155,9 +202,12 @@
 
     Rosenbrock)   
-      KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST wcopy wscal waxpy"
-    if [[ $MODE != "vector" ]]
-    then
-      KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST rosenbrock  funtemplate jactemplate"
-    fi;;
+      KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST WSCAL WAXPY"
+      if [[ $MODE != "vector" ]]
+      then
+         KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST Rosenbrock  FunTemplate JacTemplate"
+        KPP_INTERFACE_IGNORE="WAXPY"
+      else 
+        KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST FunTemplate JacTemplate"
+      fi;;
 
     rosenbrock_mz)
@@ -167,5 +217,5 @@
     rosenbrock)
       KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST WCOPY WSCAL WAXPY"
-      KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST rosenbrock  funtemplate jactemplate";;
+      KPP_SUBROUTINE_LIST="$KPP_SUBROUTINE_LIST Rosenbrock  FunTemplate JacTemplate";;
 
     kpp_lsode)
@@ -203,4 +253,23 @@
 cp $DEFDIR/${PREFIX}.kpp     .
 
+# Global variable are defined here 
+# This has the advantage that it is not necessary to include these variables in all .kpp definition files
+
+cat  >> ${PREFIX}.kpp  <<  EOF
+#INLINE F90_GLOBAL
+! QVAP - Water vapor
+  REAL(kind=dp) :: QVAP
+! FAKT - Conversion factor
+  REAL(kind=dp) :: FAKT
+
+#ENDINLINE
+EOF
+
+# Store mechanism name in file mech_list
+cat  >> mech_list  <<  EOF
+!   Mechanism: $MECH
+!
+EOF
+
 # Run kpp
 
@@ -240,6 +309,14 @@
 done
 
+echo start kp4.exe with arguments
+echo $PREFIX $MODE $VLEN $DE_INDEX $DE_INDEX_FAST
+
 $BASE/bin/kpp4palm.exe $PREFIX $MODE $VLEN $DE_INDEX $DE_INDEX_FAST
 
+#Prelimanary, substitution has to be moved into kpp4palm.exe
+if [[ $MODE = "vector" ]]
+then
+  sed -i -e 's/phot(nphot/phot(vl_dim,nphot/g' kk_kpp.f90
+fi
 
 if [[ -e $OUTDIR/${OUTFILE}.f90 ]] 
@@ -248,8 +325,13 @@
 fi
 cp -p kk_kpp.f90    $OUTDIR/${OUTFILE}.f90
-#cp -p kk_kpp.f90    $MY_PWD/../SOURCE/${OUTFILE}.f90
-
 echo " "
 echo "Write kpp module -- > " $OUTDIR/${OUTFILE}.f90
+
+if [[ $UPDT = "YES" ]]
+then
+cp -p kk_kpp.f90    $DEFDIR/${OUTFILE}.f90
+echo " "
+echo "Write kpp module -- > " $DEFDIR/${OUTFILE}.f90
+fi
 
 if [[ $KEEP = "NO" ]]
