#!/bin/csh

# -----------------------------------------------------------------------------
# Handle parameters
# -----------------------------------------------------------------------------

# Set Lagranto 
set LAGRANTO = ${LAGRANTOBASE}.${MODEL}/

# Write usage information
if ( ${#argv} == 0) then
  echo 
  ${LAGRANTO}/bin/lagrantohelp wrfmap short
  echo  
  exit 0
endif

# Name of the input file
set mode = $1

if ( "${mode}" == "-create" ) then
   set inpfile = $2
   if ( $#argv == 3 ) then
      set anglemode = $3
   else
      set anglemode = 'dateline'
   endif
endif

if ( "${mode}" == "-ll2xy" ) then
   set flag = `${LAGRANTO}/bin/isnumber $2`
   if ( "${flag}" == "1" ) then
   	 set mode   = "-ll2xy.single"
   	 set lonpos = $2
     set latpos = $3
   else
     set inpfile   = $2
     set outfile   = $3
   endif
endif

if ( "${mode}" == "-xy2ll" ) then
   set flag = `${LAGRANTO}/bin/isnumber $2`
   if ( "${flag}" == "1" ) then
   	 set mode   = "-xy2ll.single"
   	 set xpos   = $2
     set ypos   = $3
   else
     set inpfile   = $2
     set outfile   = $3
   endif
endif

if ( "${mode}" == "-p2z" ) then
   set flag = `${LAGRANTO}/bin/isnumber $2`
   if ( "${flag}" == "1" ) then
   	 set mode   = "-p2z.single"
   	 set xpos   = $2
   	 set ypos   = $3
   	 set ppos   = $4
   else
     set inpfile   = $2
     set outfile   = $3
   endif
endif

if ( "${mode}" == "-z2p" ) then
   set flag = `${LAGRANTO}/bin/isnumber $2`
   if ( "${flag}" == "1" ) then
   	 set mode   = "-z2p.single"
   	 set xpos   = $2
   	 set ypos   = $3
   	 set zpos   = $4
   else
     set inpfile   = $2
     set outfile   = $3
   endif
endif

if ( "${mode}" == "-mapscale" ) then
   if ( $#argv == 2 ) then
      set nfilter = $2
   else
      set nfilter = 0
   endif  
endif

# -----------------------------------------------------------------------------
# Create mapping file
# -----------------------------------------------------------------------------

if ( "${mode}" == "-create" ) then

# Test whether input file is present
if ( ! -f ${inpfile} ) then
   echo " ERROR: input file ${inpfile} is missing... Stop"
   exit 1
endif

# Check whether grid description is available
set ok = `${LAGRANTO}/bin/getvars ${inpfile} | grep XLAT`
if ( "{ok}" == "" ) then
   echo "Mapping XLAT is missing on ${inpfile}... Stop"
   exit 1
endif
set ok = `${LAGRANTO}/bin/getvars ${inpfile} | grep XLONG`
if ( "{ok}" == "" ) then
   echo "Mapping XLONG is missing on ${inpfile}... Stop"
   exit 1
endif

# Calculate the forward transformation
\rm -f wrfmap.param
echo \"${mode}\"      >! wrfmap.param
echo \"${inpfile}\"   >> wrfmap.param
echo \"${anglemode}\" >> wrfmap.param

${LAGRANTO}/goodies/wrfmap

# Make clean
\rm -f wrfmap.param

endif

# -----------------------------------------------------------------------------
# Transform lon/lat -> x/y
# -----------------------------------------------------------------------------

if ( "${mode}" == "-ll2xy" ) then

# Test whether input file is present
if ( ! -f ${inpfile} ) then
   echo " ERROR: input file ${inpfile} is missing... Stop"
   exit 1
endif

# Decide whether startfile has an explicit format specifier
set format = "0"
foreach app ( 1 2 3 4 5 6 7 8 9 )
  set flag = `echo ${inpfile} | grep ".${app}"`
  if ( "${flag}" != "" ) set format = "${app}"
end

# If format is 0, it might nevertheless be a hidden format 1
if ( "${format}" == "0" ) then
    set ncol = `awk "{print NF}" ${inpfile} | tail -1` 
    if ( "${ncol}" != "3" ) then
        set format = "1"
        echo " WARNING: ${inpfile} is a hidden trajectory file of format 1"
        echo "          it will be renamed: ${inpfile} -> ${inpfile}.1"
        echo
        ln -sf ${inpfile} ${inpfile}.1
        set inpfile = "${inpfile}.1"
    endif
endif

# Get the number of trajectories
if ( "${format}" == "0" ) then
   set ntra = `wc -l ${inpfile} | awk '{print $1}' `
   set ncol = 3
   set ntim = 1
else
   set ntra = `${LAGRANTO}/goodies/trainfo.sh ${inpfile} ntra`
   set ncol = `${LAGRANTO}/goodies/trainfo.sh ${inpfile} ncol`
   set ntim = `${LAGRANTO}/goodies/trainfo.sh ${inpfile} ntim`
endif

# Prepare parameter file and run Fortran code
\rm -f wrfmap.param 
echo \"${mode}\"             >! wrfmap.param
echo \"${inpfile}\"          >> wrfmap.param
echo \"${outfile}\"          >> wrfmap.param
echo ${ntra} ${ntim} ${ncol} >> wrfmap.param

cat wrfmap.param
${LAGRANTO}/goodies/wrfmap

# Make clean
\rm -f wrfmap.param

endif 

# -----------------------------------------------------------------------------
# Transform x/y -> lon/lat
# -----------------------------------------------------------------------------

if ( "${mode}" == "-xy2ll" ) then

# Test whether input file is present
if ( ! -f ${inpfile} ) then
   echo " ERROR: input file ${inpfile} is missing... Stop"
   exit 1
endif

# Decide whether startfile has an explicit format specifier
set format = "0"
foreach app ( 1 2 3 4 5 6 7 8 9 )
  set flag = `echo ${inpfile} | grep ".${app}"`
  if ( "${flag}" != "" ) set format = "${app}"
end

# If format is 0, it might nevertheless be a hidden format 1
if ( "${format}" == "0" ) then
    set ncol = `awk "{print NF}" ${inpfile} | tail -1` 
    if ( "${ncol}" != "3" ) then
        set format = "1"
        echo " WARNING: ${inpfile} is a hidden trajectory file of format 1"
        echo "          it will be renamed: ${inpfile} -> ${inpfile}.1"
        echo
        ln -sf ${inpfile} ${inpfile}.1
        set inpfile = "${inpfile}.1"
    endif
endif

# Get the number of trajectories
if ( "${format}" == "0" ) then
   set ntra = `wc -l ${inpfile} | awk '{print $1}' `
   set ncol = 3
   set ntim = 1
else
   set ntra = `${LAGRANTO}/goodies/trainfo.sh ${inpfile} ntra`
   set ncol = `${LAGRANTO}/goodies/trainfo.sh ${inpfile} ncol`
   set ntim = `${LAGRANTO}/goodies/trainfo.sh ${inpfile} ntim`
endif

# Prepare parameter file and run Fortran code
\rm -f wrfmap.param 
echo \"${mode}\"             >! wrfmap.param
echo \"${inpfile}\"          >> wrfmap.param
echo \"${outfile}\"          >> wrfmap.param
echo ${ntra} ${ntim} ${ncol} >> wrfmap.param

${LAGRANTO}/goodies/wrfmap

# Make clean
\rm -f wrfmap.param

endif

# -----------------------------------------------------------------------------
# Transform single lon/lat -> x/y
# -----------------------------------------------------------------------------

if ( "${mode}" == "-ll2xy.single" ) then

# Prepare parameter file and run Fortran code
\rm -f wrfmap.param 
echo \"${mode}\"             >! wrfmap.param
echo ${lonpos}, ${latpos}    >> wrfmap.param

${LAGRANTO}/goodies/wrfmap

# Make clean
\rm -f wrfmap.param

endif

# -----------------------------------------------------------------------------
# Transform single x/y -> lon/lat
# -----------------------------------------------------------------------------

if ( "${mode}" == "-xy2ll.single" ) then

# Prepare parameter file and run Fortran code
\rm -f wrfmap.param 
echo \"${mode}\"             >! wrfmap.param
echo ${xpos}, ${ypos}    >> wrfmap.param

${LAGRANTO}/goodies/wrfmap

# Make clean
\rm -f wrfmap.param

endif

# -----------------------------------------------------------------------------
# Calculate map scale factors
# -----------------------------------------------------------------------------

if ( "${mode}" == "-mapscale" ) then
	
# Prepare parameter file and run Fortran code
\rm -f wrfmap.param 
echo \"${mode}\"      >! wrfmap.param
echo ${nfilter}       >> wrfmap.param

${LAGRANTO}/goodies/wrfmap

# Make clean
\rm -f wrfmap.param
	
endif

# -----------------------------------------------------------------------------
# Transform pressure to height : x,y,p -> x,y,z
# -----------------------------------------------------------------------------

if ( "${mode}" == "-p2z" ) then

# Test whether input file is present
if ( ! -f ${inpfile} ) then
   echo " ERROR: input file ${inpfile} is missing... Stop"
   exit 1
endif

# Decide whether startfile has an explicit format specifier
set format = "0"
foreach app ( 1 2 3 4 5 6 7 8 9 )
  set flag = `echo ${inpfile} | grep ".${app}"`
  if ( "${flag}" != "" ) set format = "${app}"
end

# If format is 0, it might nevertheless be a hidden format 1
if ( "${format}" == "0" ) then
    set ncol = `awk "{print NF}" ${inpfile} | tail -1` 
    if ( "${ncol}" != "3" ) then
        set format = "1"
        echo " WARNING: ${inpfile} is a hidden trajectory file of format 1"
        echo "          it will be renamed: ${inpfile} -> ${inpfile}.1"
        echo
        ln -sf ${inpfile} ${inpfile}.1
        set inpfile = "${inpfile}.1"
    endif
endif

# Get the number of trajectories
if ( "${format}" == "0" ) then
   set ntra = `wc -l ${inpfile} | awk '{print $1}' `
   set ncol = 3
   set ntim = 1
else
   set ntra = `${LAGRANTO}/goodies/trainfo.sh ${inpfile} ntra`
   set ncol = `${LAGRANTO}/goodies/trainfo.sh ${inpfile} ncol`
   set ntim = `${LAGRANTO}/goodies/trainfo.sh ${inpfile} ntim`
endif

# Prepare parameter file and run Fortran code
\rm -f wrfmap.param 
echo \"${mode}\"             >! wrfmap.param
echo \"${inpfile}\"          >> wrfmap.param
echo \"${outfile}\"          >> wrfmap.param
echo ${ntra} ${ntim} ${ncol} >> wrfmap.param

cat wrfmap.param
${LAGRANTO}/goodies/wrfmap

# Make clean
\rm -f wrfmap.param

endif 

# -----------------------------------------------------------------------------
# Transform single pressure to height : x,y,p -> x,y,z
# -----------------------------------------------------------------------------

if ( "${mode}" == "-p2z.single" ) then

# Prepare parameter file and run Fortran code
\rm -f wrfmap.param 
echo \"${mode}\"                 >! wrfmap.param
echo ${xpos}, ${ypos},${ppos}    >> wrfmap.param

${LAGRANTO}/goodies/wrfmap

# Make clean
\rm -f wrfmap.param

endif

# -----------------------------------------------------------------------------
# Transform height to pressure : x,y,z -> x,y,p
# -----------------------------------------------------------------------------

if ( "${mode}" == "-z2p" ) then

# Test whether input file is present
if ( ! -f ${inpfile} ) then
   echo " ERROR: input file ${inpfile} is missing... Stop"
   exit 1
endif

# Decide whether startfile has an explicit format specifier
set format = "0"
foreach app ( 1 2 3 4 5 6 7 8 9 )
  set flag = `echo ${inpfile} | grep ".${app}"`
  if ( "${flag}" != "" ) set format = "${app}"
end

# If format is 0, it might nevertheless be a hidden format 1
if ( "${format}" == "0" ) then
    set ncol = `awk "{print NF}" ${inpfile} | tail -1` 
    if ( "${ncol}" != "3" ) then
        set format = "1"
        echo " WARNING: ${inpfile} is a hidden trajectory file of format 1"
        echo "          it will be renamed: ${inpfile} -> ${inpfile}.1"
        echo
        ln -sf ${inpfile} ${inpfile}.1
        set inpfile = "${inpfile}.1"
    endif
endif

# Get the number of trajectories
if ( "${format}" == "0" ) then
   set ntra = `wc -l ${inpfile} | awk '{print $1}' `
   set ncol = 3
   set ntim = 1
else
   set ntra = `${LAGRANTO}/goodies/trainfo.sh ${inpfile} ntra`
   set ncol = `${LAGRANTO}/goodies/trainfo.sh ${inpfile} ncol`
   set ntim = `${LAGRANTO}/goodies/trainfo.sh ${inpfile} ntim`
endif

# Prepare parameter file and run Fortran code
\rm -f wrfmap.param 
echo \"${mode}\"             >! wrfmap.param
echo \"${inpfile}\"          >> wrfmap.param
echo \"${outfile}\"          >> wrfmap.param
echo ${ntra} ${ntim} ${ncol} >> wrfmap.param

cat wrfmap.param
${LAGRANTO}/goodies/wrfmap

# Make clean
\rm -f wrfmap.param

endif 

# -----------------------------------------------------------------------------
# Transform single height to pressure : x,y,z -> x,y,p
# -----------------------------------------------------------------------------

if ( "${mode}" == "-z2p.single" ) then

# Prepare parameter file and run Fortran code
\rm -f wrfmap.param 
echo \"${mode}\"                 >! wrfmap.param
echo ${xpos}, ${ypos},${zpos}    >> wrfmap.param

${LAGRANTO}/goodies/wrfmap

# Make clean
\rm -f wrfmap.param

endif
