Index: trunk/tools/model_graphics.bash
===================================================================
--- trunk/tools/model_graphics.bash	(revision 867)
+++ trunk/tools/model_graphics.bash	(revision 868)
@@ -3,6 +3,4 @@
 
 main='model_graphics.bash'
-errormsg='ERROR -- error -- ERROR -- error'
-warnmsg='ERROR -- error -- ERROR -- error'
 
 function uploadvars() {
@@ -48,14 +46,140 @@
 }
 
+function ferrmsg() {
+# Function to exit and write an error message
+#  comdexit: code number exit from the last execution
+#  ref: script/reference from which error occurs
+#  msg: message to write ('!' for spaces, '#' for end of line)
+  comdexit=$1
+  ref=$2
+  msg=$3
+
+  if test ${comdexit} -ne 0; then
+    echo "ERROR -- error -- ERROR -- error"
+    echo "  "${ref}": "$(echo ${msg} | tr '!' ' ' | tr '#' '\n')" !!"
+    exit
+  fi
+}
+
+function compute_variable() {
+# Function to compute a variable
+#   idir: directory with the input files
+#   usefiles: ',' list of files as [headerf]@[file1]|...|[fileN]
+#   odir: directory to write the output files
+#   cvar: [CFvarn]|[varkind]|[headerf]|[varmod]|[vardiag]
+#     [CFvarn]: CF name of the variable
+#     [vark]: kind of variable: 
+#        acc: temporal accumulated values
+#        diff: differences between models
+#        direct: no statistics
+#        Lsec: latitudinal section (latitudinal value must be given, [var]@[lat]) 
+#        lsec: longitudinal section (longitudinal value must be given, [var]@[lat])
+#        lmean: longitudinal mean values
+#        tmean: temporal mean values
+#        zsum: vertical aggregated values
+#     [headerf]: header of the files to use
+#     [modvar]: variable to use from the file
+#     [diagvar]: variable computed from the diagnostics (diagnostic.py)
+#   scratch: whether is has to be done from the scratch or not
+  fname='compute_variable'
+  idir=$1
+  usefiles=$2
+  odir=$3
+  cvar=$4
+  scratch=$5
+
+  CFvarn=`echo ${cvar} | tr '|' ' ' | awk '{print $1}'`
+  vark=`echo ${cvar} | tr '|' ' ' | awk '{print $2}'`
+  headerf=`echo ${cvar} | tr '|' ' ' | awk '{print $3}'`
+  modvar=`echo ${cvar} | tr '|' ' ' | awk '{print $4}'`
+  diagvar=`echo ${cvar} | tr '|' ' ' | awk '{print $5}'`
+
+  cfiles=`echo ${usefiles} | tr ',' '\n' | grep ${headerf} | tr '|' ' ' | \
+    awk '{print $2}' | tr '@' ' '`
+
+  cd ${odir}
+
+  # Computing separately and then joinging for all files
+  Ntotfiles=`echo ${cfiles} | wc -w | awk '{print $1}'`
+  ifile=1
+  for cf in ${cfiles}; do
+    ifS=`printf "%0${Ntotfiles}d" ${ifile}`
+    # Computing variable
+    filen=${odir}/${CFvarn}_${headerf}_${ifile}-${Ntotfiles}.nc
+    if ${scratch}; then rm ${filen}; fi
+
+    if test ! -f ${filen}; then
+      if test ! ${modvar} = 'None'; then
+        # model variable
+        values=${modvar}',0,-1,-1'
+        vs=${modvar},${vdnx},${vdny},${vdnz},${vdnt}
+        pyout=`${pyHOME}/nc_var.py -f ${cf} -o DataSetSection_multivars -v ${vs}     \
+          -S ${values} | grep succesfull | awk '{print $6}' | tr '"' ' '`
+        ferrmsg $? ${fname} "python!'DataSetSection_multivars'!failed"
+        mv ${pyout} ${filen}
+        pyout=`${pyHOME}/nc_var.py -f ${filen} -o chvarname -v ${vs} -S ${CFvarn}`
+        ferrmsg $? ${fname} "python!'chvarname'!failed"        
+
+        # adding CF lon,lat,time in WRF files
+        if test ${headerf:0:3} = 'wrf'; then
+          ${pyHOME}/nc_var.py -o WRF_CFtime_creation -S 19491201000000,minutes       \
+            -f ${filen} -v time
+          ${pyHOME}/nc_var.py -o WRF_CFlonlat_creation -v ${vdnx},${vdny}            \
+            -f ${filen} -S lon,lat
+        fi
+      else
+        # diagnostic variable
+        dims=${dnt}@${vdnt},${dnz}@${vdnz},${dny}@${vdny},${dnx}@${vdnx}
+        diagn=`echo ${diagvar} | tr ':' '\n' | head -n 1`
+        Ndiagvars=`echo ${diagvar} | tr ':' ' ' | wc -w | awk '{print $1}'`
+        idiagv=2
+        diagc=''
+        while test ${idiagv} -le ${Ndiagvars}; do
+          diag1=`echo ${diagvar} | tr ':' '\n' | head -n ${idiagv} | tail -n 1`
+          if test ${idiagv} -eq 2; then
+            diagc=${diag1}
+          else
+            diagc=${diagc}'@'${diag1}
+          fi
+          idiagv=`expr ${idiagv} + 1`
+        done
+
+        echo "python ${pyHOME}/diagnostics.py -f ${cf} -d ${dims} -v ${diagn}'|'${diagc}"
+        pyout=`python ${pyHOME}/diagnostics.py -f ${cf} -d ${dims} -v ${diagn}'|'${diagc}`
+        ferrmsg $? ${fname} "python!'diagnostics'!failed"
+        mv diagnostics.nc ${filen}
+
+        # adding CF lon,lat,time in WRF files
+        if test ${headerf:0:3} = 'wrf'; then
+          ${pyHOME}/nc_var.py -o WRF_CFtime_creation -S 19491201000000,minutes       \
+            -f ${filen} -v time
+          ${pyHOME}/nc_var.py -o WRF_CFlonlat_creation -v ${vdnx},${vdny}            \
+            -f ${filen} -S lon,lat
+        fi
+      fi
+    fi
+
+    ifile=`expr ${ifile} + 1`
+  done
+
+# LLUIS
+#  exit
+}
+
 #######    #######
 ## MAIN
     #######
+rootsh=`pwd`
 
 # Uploading environment
 uploadvars model_graphics.dat
 
-export 
-exit
-
+if test scratch = 'true'; then
+  scratch=true
+  echo ${warnmsg}
+  echo "  "${main}": starting from the SCRATCH !!"
+else
+  scratch=false
+fi
 
 timeval='tstep'
@@ -72,9 +196,242 @@
 combosfile=${HOMEpy}/diagnostics.inf
 
-errmsg='ERROR -- error -- ERROR -- error'
 ####### ###### ##### #### ### ## #
 
-exps=`echo ${experiments} | tr ':' ' '`
+mods=`echo ${models} | tr ':' ' '`
 varks=`echo ${varkinds} | tr ':' ' '`
+
+# Models loop
+##
+for mod in ${mods}; do
+  case ${mod} in
+    'WRF')
+      exps=`echo ${WRFexps} | tr ':' ' '`
+      fheaders=`echo ${WRFheaders} | tr ':' ' '`
+    ;;
+    'LMDZ')
+      exps=`echo ${LMDZexps} | tr ':' ' '`
+      fheaders=`echo ${LMDZheaders} | tr ':' ' '`
+    ;;
+    'WRF_LMDZ')
+      exps=`echo ${WRF_LMDZexps} | tr ':' ' '`
+      fheaders=`echo ${WRF_LMDZheaders} | tr ':' ' '`
+    ;;
+    '*')
+      echo ${errmsg}
+      echo "  "${main}": model '"${mod}"' not ready!!"
+      exit
+    ;;
+  esac  
+
+  modinf=`$pyHOME/nc_var.py -o model_characteristics -f None -S ${mod} | \
+    grep singleline | awk '{print $2}'`
+  ferrmsg $? $main "python!'model_characteristics'!failed"
+  dnx=`echo ${modinf} | tr ';' '\n' | grep dimxn | tr '=' ' ' | awk '{print $2}'`
+  dny=`echo ${modinf} | tr ';' '\n' | grep dimyn | tr '=' ' ' | awk '{print $2}'`
+  dnz=`echo ${modinf} | tr ';' '\n' | grep dimzn | tr '=' ' ' | awk '{print $2}'`
+  dnt=`echo ${modinf} | tr ';' '\n' | grep dimtn | tr '=' ' ' | awk '{print $2}'`
+  vdnx=`echo ${modinf} | tr ';' '\n' | grep vardxn | tr '=' ' ' | awk '{print $2}'`
+  vdny=`echo ${modinf} | tr ';' '\n' | grep vardyn | tr '=' ' ' | awk '{print $2}'`
+  vdnz=`echo ${modinf} | tr ';' '\n' | grep vardzn | tr '=' ' ' | awk '{print $2}'`
+  vdnt=`echo ${modinf} | tr ';' '\n' | grep vardtn | tr '=' ' ' | awk '{print $2}'`
+  echo ${mod}
+  echo "  dims: "${dnx}", "${dny}", "${dnz}", "${dnt}
+  echo "  var dims: "${vdnx}", "${vdny}", "${vdnz}", "${vdnt}
+
+# Experiments loop
+##
+  for exp in ${exps}; do
+    echo "  "${exp}"..."
+    iwdir=${ifold}/${mod}/${exp}
+
+    # Does input folder exist?
+    if test ! -d ${iwdir}; then
+      echo ${errmsg}
+      echo "  "${main}": folder '"${iwdir}"' does not exist !!"
+      exit
+    fi
+
+    owdir=${ofold}/${mod}/${exp}
+    mkdir -p ${owdir}
+
+    # Need to pass to analyze all the data?
+    if ${scratch}; then rm ${owdir}/varcompute.inf >& /dev/null; fi
+    if test ! -f ${owdir}/varcompute.inf; then
+
+      # Does input folder has header files?
+      cd ${iwdir}
+      files=''
+      testfiles=''
+      ih=1
+      for fh in ${fheaders}; do
+        files1h=`ls -1 ${fh}* | tr '\n' '@'`
+        Lfiles1h=`expr length ${files1h}'0'`
+        if test ${Lfiles1h} -lt 2; then
+          ferrmsg 1 $main "folder!:!"${iwdir}"!does!not!contain!files!"${fh}"*"
+        fi
+        testfiles1h=`ls -1 ${fh}* | head -n 1`
+        if test ${ih} -eq 1; then
+          files=${fh}'|'${files1h}
+          testfiles=${fh}'|'${testfiles1h}
+        else
+          files=${files}','${fh}'|'${files1h}
+          testfiles=${testfiles}'@'${fh}'|'${testfiles1h}
+        fi
+        ih=`expr ${ih} + 1`
+      done
+      testfs=`echo ${testfiles} | tr '@' ' '`
+      cd ${rootsh}
+
+# Kind variables loop
+##
+      ik=1
+      itotv=1
+      for vark in ${varks}; do
+        echo "    "${vark}" ..."
+        case ${vark} in
+          'varacc')
+            varvks=${varacc}
+          ;;
+          'vardiff')
+            varvks=${vardiff}
+          ;;
+          'direct')
+            varvks=${vardirect}
+          ;;
+          'varLsec')
+            varvks=${varLsec}
+          ;;
+          'varlsec')
+            varvks=${varlsec}
+          ;;
+          'varlmean')
+            varvks=${varlmean}
+          ;;
+          'vartmean')
+            varvks=${vartmean}
+          ;;
+          'varzsum')
+            varvks=${varzsum}
+          ;;
+          '*')
+            echo ${errmsg}
+            echo "  "${main}": variable kind '"${vark}"' not ready!!"
+            exit
+          ;;
+        esac
+
+# Do we have variables for this kind?
+        Lvarvks=`expr length ${varvks}'0'`
+        if test ${Lvarvks} -lt 2; then
+          ferrmsg 1 ${main} "variable!kind!"${vark}"!without!variables"
+        fi
+        if test ${ik} -eq 1; then
+          allvars=${varvks}
+        else
+          allvars=${allvars}':'${varvks}
+        fi
+        ik=`expr ${ik} + 1`
+        vars=`echo ${varvks} | tr ':' ' '`
+
+# Variables loop
+##
+        iv=1
+        for var in ${vars}; do
+          echo "      "${var}
+          # How to copmute it?
+          cancompute=true
+          for ftest in ${testfs}; do
+            headerf=`echo ${ftest} | tr '|' ' ' | awk '{print $1}'`
+            filen=`echo ${ftest} | tr '|' ' ' | awk '{print $2}'`
+            compute=`${pyHOME}/nc_var.py -o computevar_model -f ${iwdir}/${filen}      \
+              -S ${var}`
+            ferrmsg $? ${main} "python!'computevar_model'!failed!#"$(echo ${compute} | \
+               tr ' ' '!')
+            varmod=`echo ${compute} | tr ' ' '#' | tr '|' ' ' | awk '{print $2}' |   \
+               tr '@' ' ' | awk '{print $2}' | tr '=' ' ' | awk '{print $2}'`
+            vardiag=`echo ${compute} | tr ' ' '#' | tr '|' ' ' | awk '{print $2}' |  \
+               tr '@' ' ' | awk '{print $3}' | tr '=' ' ' | awk '{print $2}'`
+            echo "  "${var}" mod:"${varmod}" diag: "${vardiag}
+            if test ${varmod} = 'None' && test ${vardiag} = 'None'; then 
+              cancompute=false
+            else
+              cancompute=true
+              # Should be considered that variable can also be computed by both ways?
+              break
+            fi
+          done
+          if ! ${cancompute}; then
+            msg="there!is!no!way!to!compute!'"${var}"'!for!model!"${mod}
+# Too extrict!
+#            ferrmsg 1 ${main} ${msg}
+            echo ${warnmsg}
+            echo $(echo $msg | tr '!' ' ')
+          fi
+
+          # A ';' list 'varcompute' it is created for each variable giving:
+          #   [var]|[vark]|[headerf][varmod]|[vardiag]
+          # This list will be used to compute a new file for each variable
+          varcomp=${var}'|'${vark}'|'${headerf}'|'${varmod}'|'${vardiag}
+          if test ${itotv} -eq 1; then
+            varcompute=${varcomp}
+          else
+            varcompute=${varcompute}';'${varcomp}
+          fi
+
+          iv=`expr ${iv} + 1`
+          itotv=`expr ${itotv} + 1`
+
+# end loop of variables
+        done
+# end of kind of variables
+      done
+
+      # Outwritting the varcompute to avoid next time (if it is not scratch!)
+      cat << EOF > ${owdir}/varcompute.inf
+files: ${files}
+varcompute: ${varcompute}
+itotv: ${itotv}
+EOF
+    else
+      echo $warnmsg
+      echo "  "${main}": getting already data information from the experiment!"
+      files=`cat ${owdir}/varcompute.inf | grep files | awk '{print $2}'`
+      varcompute=`cat ${owdir}/varcompute.inf | grep varcompute | awk '{print $2}'`
+      itotv=`cat ${owdir}/varcompute.inf | grep itotv | awk '{print $2}'`
+# End of avoiding to repeat all the experiment search
+    fi
+
+    echo "  For experiment '"${exp}"' is required to compute: "${itotv}" variables"
+# Computing files for each variable
+## 
+    echo "      Computing all variables ..."
+    cd $owdir
+    ic=1
+    cvars=`echo ${varcompute} | tr ';' ' '`
+    for cvar in ${cvars}; do
+      CFv=`echo ${cvar} | tr '|' ' ' | awk '{print $1}'`
+      modv=`echo ${cvar} | tr '|' ' ' | awk '{print $4}'`
+      diagv=`echo ${cvar} | tr '|' ' ' | awk '{print $5}'`
+      echo "        "${CFv}"; "${modv}" "${diagv}
+
+      if test ! ${modv} = 'None' || test ! ${diagv} = 'None'; then
+        compute_variable ${iwdir} ${files} ${owdir} ${cvar} ${scratch}
+        ic=`expr ${ic} + 1`
+      else
+        echo "        not computing '"${CFv}"' for model '"${mod}"' !!"
+      fi
+
+      # exit
+# end of computing vars
+    done
+    echo "  "${main}": has been "${ic}" variables computed"
+    cd ${rootsh}
+    exit
+# end of experiments
+  done
+# end of models
+done
+
+exit
 
 istep=0
Index: trunk/tools/model_graphics_template.dat
===================================================================
--- trunk/tools/model_graphics_template.dat	(revision 867)
+++ trunk/tools/model_graphics_template.dat	(revision 868)
@@ -1,18 +1,31 @@
-### ASCII file to manage `model_graphics.bash'
+### ASCII file to manage `model_graphics.bash' from different models and experiments 
+#  from each one
+#
+#  1: Variables choice using its CF name
+#  2: Kind of statisitcs to apply to the variable
+#  3: Kind of plot for the variable
+#  4: Kind of difference among models/experiments
+#
 # Different assumptions are made:
-#   model outputs are organized as ${ifold}/${model}/${sim}
-#   script outputs will be organized as ${ofold}/${model}/${sim}
-#   when diagnostic might be required a new file will be created
-#   WRF outputs will have added 'WRFtime' variable
+#  - model outputs are organized as ${ifold}/${model}/${exp}
+#  - script outputs will be organized as ${ofold}/${model}/${exp}
+#  - when diagnostic might be required a new file will be created
+#  - WRF outputs will have added 'WRFtime' variable
+#
+# NOTE: in this file all the values without '
 
 # python HOME
 HOMEpy = ${HOME}/etudes/WRF_LMDZ/svn/LMDZ_WRF/tools
 
+# Srcatch
+srcatch=false
+
 # Folder with the files
-
-ifold = /bdd/PCER/workspace/lfita/etudes/WRF_LMDZ/WaquaL/WRF_LMDZ
+##ifold = /bdd/PCER/workspace/lfita/etudes/WRF_LMDZ/WaquaL/WRF_LMDZ
+ifold = /home/lluis/etudes/WRF_LMDZ/WaquaL_highres/tests/model_graphics
 
 # Output folder
-ofold = /bdd/PCER/workspace/lfita/etudes/WRF_LMDZ/WaquaL/WRF_LMDZ
+##ofold = /bdd/PCER/workspace/lfita/etudes/WRF_LMDZ/WaquaL/WRF_LMDZ
+ofold = /home/lluis/etudes/WRF_LMDZ/WaquaL_highres/tests/model_graphics
 
 # Experiments (model runs)
@@ -20,10 +33,16 @@
 #   LMDZ: LMDZ model
 #   WRF_LMDZ: LMDZ physics coupled to WRF
-models=WRF:WRF_LMDZ:LMDZ
+#models=WRF:WRF_LMDZ:LMDZ
+models=WRF
 
-# Simulations (labels of the simualtions from each experiment)
-WRFsims = control:pbl1
-LMDZsims = AR40:NPv31
-WRF_LMDZsims = AR40:NPv31
+# Experiments (labels of each experiment)
+WRFexps = control:pbl1
+LMDZexps = AR40:NPv31
+WRF_LMDZexps = AR40:NPv31
+
+# Headers of the files to use
+WRFheaders = wrfout
+WRF_LMDZheaders = wrfout
+LMDZheaders = histins
 
 # Kind of calculations (adding the euiqvalent `surname' to each variable)
@@ -36,5 +55,4 @@
 #   tmean: temporal mean values
 #   zsum: vertical aggregated values
-
 varkinds = direct:tmean:lmean:diff
 
@@ -42,8 +60,8 @@
 #   in foudre values from: ${HOME}/UNSW-CCRC-WRF/tools/postprocess/GMS-UC/WRF4G/util/postprocess/wrfncxnj/wrfncxnj.table
 #   must exist on the table $pyHOME/variables_values.dat
-direct = tas:uas:vas:ps:pr:pracc:rsds:prw:rhs:evspsbl:hfss:hfls:hurs:huss:zmla:hufs:wakes:lwakeh:stherm:zmaxth:clt:cllmh:prc:prls:bils
-tmean = prc:prls:zmla:hfss:hfls:evspsbl:bils:uas:vas:tas:ps:clt:cllmh:prw:huss:pr:wakes:wakeh:stherm:zmaxth
-lmean = ta:ua:va:hur
-diff = ualmean:valmean:talmean:huslmean:uas:vas:ps:pr
+vardirect = tas:uas:vas:ps:pr:pracc:rsds:prw:evspsbl:hfss:hfls:hurs:huss:zmla:hufs:wakes:lwakeh:stherm:zmaxth:clt:cll:clm:clh:prc:prls:bils
+vartmean = prc:prls:zmla:hfss:hfls:evspsbl:bils:uas:vas:tas:ps:clt:cll:clm:clh:prw:huss:pr:wakes:wakeh:stherm:zmaxth
+varlmean = ta:ua:va:hur
+vardiff = ualmean:valmean:talmean:huslmean:uas:vas:ps:pr
 
 # Kind of plots
@@ -64,2 +82,7 @@
 # Figures output 
 kindfig = pdf
+
+# Generic
+errmsg = ERROR--error--ERROR--error
+warnmsg = WARNING--warning--WARNING--warning
+
