#!/bin/ksh
# palmplot - script for running the NCL scripts
# $Id: palmplot 513 2010-03-16 13:37:48Z raasch $

    # Using the available NCL scripts via the shell script palmplot

    # Last changes
    # 16/03/10 - Rieke - initial revision


 # SHORT INTRODUCTION FOR USING THIS SCRIPT
 if [[ "$1" = "?"  || "$1" = "--help" || "$1" = "-h" ]]
  then
    (printf "\n   palmplot is designed to run the NCL scripts of PALM \n"
     printf "\n   palmplot can be called as follows:"
     printf "\n   palmplot script_identifier file_1=OUTPUT/test.nc file_out=test format_out=pdf var=,pt,u, no_rows=2 ...\n"
     printf "\n   script_identifier has to be one of the following:"
     printf "\n    xy, xz, yz, pr, ts, sp"
     printf "\n   in dependence of the data that wants to be plotted\n"
     printf "\n   the further arguments control the plot"
     printf "\n   they can also be set in the configuration file .ncl.config"
     printf "\n   a documentation is available at $PALM_BIN/\NCL\/UserGuide_for_NCLscripts.pdf\n\n")
     exit
 fi


 # DEFINITION OF VARIABLES
 a=""
 arg=""
 key=""
 script=""
 value=""
 CMDLINE=""
 STRINGPARAMS=""
 STRINGPARAMS_CO=""
 STRINGPARAMS_CS=""
 STRINGPARAMS_PR="" 
 STRINGPARAMS_SP=""
 STRINGPARAMS_TS=""


 # LIST OF THE PARAMETERS OF THE NCL SCRIPTS WHICH NCL EXPECTED TO BE STRINGS
 # CO: PARAMETERS WHICH ARE COMMON FOR ALL NCL SCRIPTS
 # CS: cross_sections.ncl PR: profiles.ncl SP: spectra.ncl TS: timeseries.ncl
 STRINGPARAMS_CO="file_1 format_out file_out var "
 STRINGPARAMS_CS="sort mode fill_mode unit_x unit_y unit_z vec1 vec2 plotvec"
 STRINGPARAMS_PR="file_2 file_3 file_4 file_5 file_6 name_legend_1 name_legend_2 name_legend_3 name_legend_4 name_legend_5 name_legend_6 c_var"
 STRINGPARAMS_SP="sort unit_x unit_y"
 STRINGPARAMS_TS="unit_t"


 # CHOICE OF THE PARTICULAR NCL SCRIPT
 if [[ "$1" == "xy" ]]
  then 
    script="cross_sections.ncl"
    STRINGPARAMS=$STRINGPARAMS_CO$STRINGPARAMS_CS
    CMDLINE="xyc=1 "
  elif [[ "$1" == "xz" ]]
    then 
    script="cross_sections.ncl"
    STRINGPARAMS=$STRINGPARAMS_CO$STRINGPARAMS_CS
    CMDLINE="xzc=1 "
  elif [[ "$1" == "yz" ]]
    then 
    script="cross_sections.ncl"
    STRINGPARAMS=$STRINGPARAMS_CO$STRINGPARAMS_CS
    CMDLINE="yzc=1 "
  elif [[ "$1" == "pr" ]]
    then 
    script="profiles.ncl"
    STRINGPARAMS=$STRINGPARAMS_CO$STRINGPARAMS_PR
  elif [[ "$1" == "ts" ]]
    then 
    script="timeseries.ncl"
    STRINGPARAMS=$STRINGPARAMS_CO$STRINGPARAMS_TS
  elif [[ "$1" == "sp" ]]
    then 
    script="spectra.ncl"
    STRINGPARAMS=$STRINGPARAMS_CO$STRINGPARAMS_SP
   else
     (printf "\n  first argument has to be one of the following:"
      printf "\n   xy, xz, yz, pr, ts or sp \n" 
      printf "\n  type \"palmplot ?\" for information about the usage of this script\n")
     exit
 fi

 # SHIFTING THE FIRST COMMAND LINE ARGUMENT
 shift 

 # PROCESSING THE COMMAND LINE ARGUMENTS
 while [ $# -gt 0 ] 
 do
    arg=$1
    key=${arg%%=*}
    value=${arg#*=}
    
    # ALLOWING RELATIVE AND ABSOLUTE FILE PATHS 
    if [[ "$key" ==  @(file_*) && "$value" != @(~/*|/*) ]]
    then
       value=$PWD/$value
    fi
     
    # SETTING STRINGS IN QUOTATION MARKS - REQUIRED BY NCL
    for a in $STRINGPARAMS
    do
      if [ "$key" = "$a" ]
      then
         value=\"$value\"
         break
      fi
    done

    CMDLINE+="$key=$value "
    shift
 done

 # EXECUTING THE PARTICULAR NCL SCRIPT
 cd $PALM_BIN/NCL
 ncl $script $CMDLINE






