Index: trunk/tools/model_graphics.bash
===================================================================
--- trunk/tools/model_graphics.bash	(revision 885)
+++ trunk/tools/model_graphics.bash	(revision 886)
@@ -32,5 +32,5 @@
 }
 
-function isin_list() {
+function isInlist() {
 # Function to check whether a value is in a list
   list=$1
@@ -60,4 +60,64 @@
     exit
   fi
+}
+
+function ferrmsgF() {
+# Function to exit and write an error message and remove a file
+#  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)
+#  FileName: name of the file to remove in case of error
+  comdexit=$1
+  ref=$2
+  msg=$3
+  FileName=$4
+
+  if test ${comdexit} -ne 0; then
+    echo "ERROR -- error -- ERROR -- error"
+    echo "  "${ref}": "$(echo ${msg} | tr '!' ' ' | tr '#' '\n')" !!"
+    rm ${FileName} >& /dev/null
+    exit
+  fi
+}
+
+function WRF_toCF() {
+# Function to pass a WRF original file to CF-conventions
+#   wrff= WRF file
+#   wrfvl= WRF longitude var name (it might come from WPS!)
+#   wrfvL= WRF latitude var name (it might come from WPS!)
+
+  wrff=$1
+  wrfvl=$2
+  wrfvL=$3
+
+  fdims=`${pyHOME}/nc_var.py -o idims -f ${wrff} | grep alldims | tr '=' ' ' |       \
+    awk '{print $3}'`
+
+  CFdims='west_east@lon:south_north@lat:Time@time'
+  CFds=`echo ${CFdims} | tr ':' ' '`
+  
+  pyout=`${pyHOME}/nc_var.py -o WRF_CFtime_creation -S 19491201000000,minutes        \
+    -f ${wrff} -v time`
+  pyn=$?
+  Spyout=`echo ${pyout} | tr '\n' '#' | tr ' ' '!'`
+  ferrmsgF ${pyn} ${fname} "python!'WRF_CFtime_creation'!failed"${Spyout} ${wrff}
+  
+  pyout=`${pyHOME}/nc_var.py -o WRF_CFlonlat_creation -v ${wrfvl},${wrfvL}           \
+    -f ${wrff} -S lon,lat`
+  pyn=$?
+  Spyout=`echo ${pyout} | tr '\n' '#' | tr ' ' '!'`
+  ferrmsgF ${pyn} ${fname} "python!'WRF_CFlonlat_creation'!failed"${Spyout} ${wrff}
+
+  for CFd in ${CFds}; do
+    cd=`echo ${CFd} | tr '@' ' ' | awk '{print $2}'`
+    wd=`echo ${CFd} | tr '@' ' ' | awk '{print $1}'`
+    if $(isInlist ${fdims} ${wd}); then
+      pyout=`${pyHOME}/nc_var.py -o chdimname -f ${wrff} -S ${wd}':'${cd}`
+      pyn=$?
+      Spyout=`echo ${pyout} | tr '\n' '#' | tr ' ' '!'`
+      ferrmsgF ${pyn} ${fname} "python!'chdimname'!'"${wd}"'!failed#"${Spyout} ${wrff}
+    fi
+  done
+
 }
 
@@ -81,4 +141,6 @@
 #     [modvar]: variable to use from the file
 #     [diagvar]: variable computed from the diagnostics (diagnostic.py)
+#   mdim: name of the dimensions in the model
+#   mvdim: name of the vaariable-dimensions in the model
 #   scratch: whether is has to be done from the scratch or not
   fname='compute_variable'
@@ -87,5 +149,7 @@
   odir=$3
   cvar=$4
-  scratch=$5
+  mdim=$5
+  mvdim=$6
+  scratch=$7
 
   CFvarn=`echo ${cvar} | tr '|' ' ' | awk '{print $1}'`
@@ -98,4 +162,11 @@
     awk '{print $2}' | tr '@' ' '`
 
+# dimensions
+  dnx=`echo ${mdim} | tr ',' ' ' | awk '{print $1}'`
+  dny=`echo ${mdim} | tr ',' ' ' | awk '{print $2}'`
+# var-dimensions
+  vdnx=`echo ${mvdim} | tr ',' ' ' | awk '{print $1}'`
+  vdny=`echo ${mvdim} | tr ',' ' ' | awk '{print $2}'`
+
   cd ${odir}
 
@@ -103,11 +174,16 @@
   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 ${scratch}; then 
+      rm ${filen}
+      rm ${odir}/${CFvarn}_${headerf}.nc
+    fi
+
+    if test ! -f ${filen} && test ! -f ${odir}/${CFvarn}_${headerf}.nc; then
       if test ! ${modvar} = 'None'; then
         # model variable
@@ -116,16 +192,13 @@
         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"
+        pyn=$?
+        Spyout=`echo ${pyout} | tr '\n' '#' | tr ' ' '!'`
+        ferrmsg ${pyn} ${fname} "python!'DataSetSection_multivars'!failed"${Spyout}
         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
+
+        pyout=`${pyHOME}/nc_var.py -f ${filen} -o chvarname -v ${modvar} -S ${CFvarn}`
+        pyn=$?
+        Spyout=`echo ${pyout} | tr '\n' '#' | tr ' ' '!'`
+        ferrmsgF ${pyn} ${fname} "python!'chvarname'!failed"${Spyout} ${filen}
       else
         # diagnostic variable
@@ -145,24 +218,186 @@
         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"
+        pyn=$?
+        Spyout=`echo ${pyout} | tr '\n' '#' | tr ' ' '!'`
+        ferrmsg ${pyn} ${fname} "python!'diagnostics'!failed#"${Spyout}
         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
+
+      # adding CF lon,lat,time in WRF files
+      if test ${headerf:0:3} = 'wrf'; then
+        WRF_toCF ${filen} ${vdnx} ${vdny}
+      fi
+
+LLUIS TO ADD ALL CF attributes!!!!!!!
+
     fi
 
     ifile=`expr ${ifile} + 1`
+# End of files
   done
 
+  # Joining variable files
+  filen=${odir}/${CFvarn}_${headerf}.nc
+  if ${scratch}; then rm ${filen}; fi
+
+  if test ! -f ${filen}; then
+    pyout=`python ${pyHOME}/nc_var.py -f ${CFvarn}_${headerf}_                       \
+      -o netcdf_fold_concatenation -S ./,time -v all`
+    pyn=$?
+    Spyout=`echo ${pyout} | tr '\n' '#' | tr ' ' '!'`
+    ferrmsg ${pyn} ${fname} "python!'netcdf_fold_concatenation'!failed#"${Spyout}
+
+    mv netcdf_fold_concatenated.nc ${filen}
+    if test -f ${filen}; then
+      rm ${CFvarn}_${headerf}_*
+    fi
+  fi
+}
+
+function compute_statistics(){
+# Function to compute different statistics
+#   idir: directory with the input files
+#   usefiles: ',' list of files to use [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
+#        Lmean: latitudinal mean values
+#        Lsec: latitudinal section (latitudinal value must be given, [var]@[lat]) 
+#        lmean: longitudinal mean values
+#        lsec: longitudinal section (longitudinal value must be given, [var]@[lat])
+#        tmean: temporal mean values
+#        xmean: x-axis mean values
+#        ymean: y-axis 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)
+#   mdim: name of the dimensions in the model
+#   mvdim: name of the vaariable-dimensions in the model
+#   scratch: whether is has to be done from the scratch or not
+  fname='compute_statistics'
+
+  idir=$1
+  usefiles=$2
+  odir=$3
+  cvar=$4
+  mdim=$5
+  mvdim=$6
+  scratch=$7
+
+  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 ',' ' '`
+
+# dimensions
+  dnx=`echo ${mdim} | tr ',' ' ' | awk '{print $1}'`
+  dny=`echo ${mdim} | tr ',' ' ' | awk '{print $2}'`
+# var-dimensions
+  vdnx=`echo ${mvdim} | tr ',' ' ' | awk '{print $1}'`
+  vdny=`echo ${mvdim} | tr ',' ' ' | awk '{print $2}'`
+
+  cd ${odir}
+
+  filen=${CFvarn}_${headerf}_${vark}.nc
+  if ${scratch}; then rm ${filen} >& /dev/null; fi
+
+  if test ! -f ${filen}; then
+    # Computing stats
+    case ${vark} in
+       # temporal accumulated values
+      'acc') 
+        echo "  "${fname}": kind '"${vark}"' not ready !!"
+        exit
+      ;;
+      # differences between models
+      'diff')
+        echo "  "${fname}": kind '"${vark}"' not ready !!"
+        exit
+      ;;
+      # no statistics
+      'direct')
+        cp ${cfiles} ${filen}
+      ;;
+      # latitudinal mean values
+      'Lmean')
+        echo "  "${fname}": kind '"${vark}"' not ready !!"
+        exit
+      ;;
+      # latitudinal section (latitudinal value must be given, [var]@[lat]) 
+      'Lsec')
+        echo "  "${fname}": kind '"${vark}"' not ready !!"
+        exit
+      ;;
+      # longitudinal section (longitudinal value must be given, [var]@[lat])
+      'lsec')
+        echo "  "${fname}": kind '"${vark}"' not ready !!"
+        exit
+      ;;
+      # longitudinal mean values
+      'lmean')
+        echo "  "${fname}": kind '"${vark}"' not ready !!"
+        exit
+      ;;
+      # temporal mean values
+      'tmean')
+        vals='time:-1,time,mean,lon:lat:'${vdnz}':time'
+        dims='time@time,'${dnz}'@'${vdnz}',lat@lat,lon@lon'
+
+        pyout=`python ${pyHOME}/nc_var.py -o file_oper_alongdims -S ${vals}          \
+          -f ${cfiles} -v ${CFvarn}`
+        pyn=$?
+        Spyout=`echo ${pyout} | tr '\n' '#' | tr ' ' '!'`
+        ferrmsg ${pyn} ${fname} "python!'file_oper_alongdims'!'tmean'!failed#"${Spyout}
+        mv file_oper_alongdims_mean.nc ${filen}
+      ;;
+      # x-axis mean values
+      'xmean')
+        vals='lon:-1,lon,mean,lon:lat:'${vdnz}':time'
+        dims='time@time,'${dnz}'@'${vdnz}',lat@lat,lon@lon'
+        echo "python ${pyHOME}/nc_var.py -o file_oper_alongdims -S ${vals} -f ${cfiles} -v ${CFvarn}"
+
+        pyout=`python ${pyHOME}/nc_var.py -o file_oper_alongdims -S ${vals}         \
+          -f ${cfiles} -v ${CFvarn}`
+        pyn=$?
+        Spyout=`echo ${pyout} | tr '\n' '#' | tr ' ' '!'`
+        ferrmsg ${pyn} ${fname} "python!'file_oper_alongdims'!'tmean'!failed#"${Spyout}
+        mv file_oper_alongdims_mean.nc ${filen}
+      ;;
+      # y-axis mean values
+      'ymean')
+        vals='lat:-1,lat,mean,lon:lat:'${vdnz}':time'
+        dims='time@time,'${dnz}'@'${vdnz}',lat@lat,lon@lon'
+        echo "python ${pyHOME}/nc_var.py -o file_oper_alongdims -S ${vals} -f ${cfiles} -v ${CFvarn}"
+
+        pyout=`python ${pyHOME}/nc_var.py -o file_oper_alongdims -S ${vals}         \
+          -f ${cfiles} -v ${CFvarn}`
+        pyn=$?
+        Spyout=`echo ${pyout} | tr '\n' '#' | tr ' ' '!'`
+        ferrmsg ${pyn} ${fname} "python!'file_oper_alongdims'!'tmean'!failed#"${Spyout}
+        mv file_oper_alongdims_mean.nc ${filen}
+      ;;
+      # vertical aggregated values
+      'zsum')
+        echo "  "${fname}": kind '"${vark}"' not ready !!"
+        exit
+      ;;
+      *)
+        echo ${errmsg}
+        echo "  "${fname}": kind '"${vark}"' not ready !!"
+        exit
+      ;;
+    esac
+  fi
+#  exit
 # LLUIS
-#  exit
 }
 
@@ -238,4 +473,6 @@
   echo "  dims: "${dnx}", "${dny}", "${dnz}", "${dnt}
   echo "  var dims: "${vdnx}", "${vdny}", "${vdnz}", "${vdnt}
+  moddims=${dnx}','${dny}','${dnz}','${dnt}
+  modvdims=${vdnx}','${vdny}','${vdnz}','${vdnt}
 
 # Experiments loop
@@ -290,8 +527,8 @@
         echo "    "${vark}" ..."
         case ${vark} in
-          'varacc')
+          'acc')
             varvks=${varacc}
           ;;
-          'vardiff')
+          'diff')
             varvks=${vardiff}
           ;;
@@ -299,17 +536,26 @@
             varvks=${vardirect}
           ;;
-          'varLsec')
+          'Lmean')
+            varvks=${varLmean}
+          ;;
+          'Lsec')
             varvks=${varLsec}
           ;;
-          'varlsec')
+          'lmean')
+            varvks=${varlmean}
+          ;;
+          'lsec')
             varvks=${varlsec}
           ;;
-          'varlmean')
-            varvks=${varlmean}
-          ;;
-          'vartmean')
+          'tmean')
             varvks=${vartmean}
           ;;
-          'varzsum')
+          'xmean')
+            varvks=${varxmean}
+          ;;
+          'ymean')
+            varvks=${varymean}
+          ;;
+          'zsum')
             varvks=${varzsum}
           ;;
@@ -408,7 +654,10 @@
     cd $owdir
     ic=1
+    isc=1
     cvars=`echo ${varcompute} | tr ';' ' '`
     for cvar in ${cvars}; do
       CFv=`echo ${cvar} | tr '|' ' ' | awk '{print $1}'`
+      vark=`echo ${cvar} | tr '|' ' ' | awk '{print $2}'`
+      fileh=`echo ${cvar} | tr '|' ' ' | awk '{print $3}'`
       modv=`echo ${cvar} | tr '|' ' ' | awk '{print $4}'`
       diagv=`echo ${cvar} | tr '|' ' ' | awk '{print $5}'`
@@ -416,6 +665,16 @@
 
       if test ! ${modv} = 'None' || test ! ${diagv} = 'None'; then
-        compute_variable ${iwdir} ${files} ${owdir} ${cvar} ${scratch}
+        compute_variable ${iwdir} ${files} ${owdir} ${cvar} ${moddims} ${modvdims}   \
+          ${scratch}
         ic=`expr ${ic} + 1`
+
+        if test ! ${vark} = 'diff'; then
+          compute_statistics ${iwdir} ${CFv}_${fileh}.nc ${owdir} ${cvar}            \
+            ${moddims} ${modvdims} ${scratch}
+          isc=`expr ${isc} + 1`
+        else
+          echo "  "${main}": differences will be calculated when all the " \
+            "model/experiments will be done !"
+        fi
       else
         echo "        not computing '"${CFv}"' for model '"${mod}"' !!"
@@ -425,5 +684,7 @@
 # end of computing vars
     done
-    echo "  "${main}": has been "${ic}" variables computed"
+    echo "  "${main}": "${ic}" variables has been computed"
+    echo "  "${main}": "${isc}" statistics has been computed"
+
     cd ${rootsh}
     exit
Index: trunk/tools/model_graphics_template.dat
===================================================================
--- trunk/tools/model_graphics_template.dat	(revision 885)
+++ trunk/tools/model_graphics_template.dat	(revision 886)
@@ -13,5 +13,5 @@
 #  - WRF outputs will have added 'WRFtime' variable
 #
-# NOTE: in this file all the values without '
+# NOTE: in this file all the values are ':' separated lists without '
 
 # python HOME
@@ -50,10 +50,13 @@
 #   diff: differences between models
 #   direct: no statistics
+#   Lmean: latitudinal mean values
 #   Lsec: latitudinal section (latitudinal value must be given, [var]@[lat]) 
+#   lmean: longitudinal mean values
 #   lsec: longitudinal section (longitudinal value must be given, [var]@[lat])
-#   lmean: longitudinal mean values
 #   tmean: temporal mean values
+#   xmean: x-axis mean values
+#   ymean: y-axis mean values
 #   zsum: vertical aggregated values
-varkinds = direct:tmean:lmean:diff
+varkinds = direct:tmean:xmean:diff
 
 # variables (providing CF-name, script must provide equivalent variable for each model)
@@ -62,5 +65,5 @@
 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
+varxmean = ta:ua:va:hur
 vardiff = ualmean:valmean:talmean:huslmean:uas:vas:ps:pr
 
