#!/bin/csh

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

# -----------------------------------------------------------------------------
# Set some parameters
# -----------------------------------------------------------------------------

# Get arguments
if ( ${#argv} == 0 ) then
    set poleprint = 1
    set inpfile   = ""
    set outfile   = ""
else
    set poleprint = 0
    set inpfile   = $1
    set outfile   = $2
endif

# Get the pole position
if ( -f rot2geo.pole ) then
   set pole = `cat rot2geo.pole` 
else if ( -f geo2rot.pole ) then 
   set pole = `cat geo2rot.pole` 
else
   set pole = "0. 90."
endif

# Handle optional arguments
set polefile = 0

while ( $#argv > 0 )

  switch ( $argv[1] )

   case -lonlat
     set pole = "$argv[2] $argv[3]"
     shift; shift;
   breaksw

   case -file
     set polefile = 1
     set filename = $argv[2] 
     shift;
   breaksw

  endsw
 
  shift;

end

# Get the pole position from file  if requested
if ( "${polefile}" == "1" ) then
    
    # Check whether file is available
    if ( ! -f ${filename} ) then
       echo "file ${filename} not found... stop"
       exit 1
    endif

    # Get pole directly from cst or from P,S file
    set pollon = ""
    set ok     = `ncdump -h ${filename} | grep pollon`
    if ( "${ok}" != "" ) then
       set pollon = `ncdump ${filename} | grep 'pollon =' | awk '{print $3}'`
    else 
       set ok       = `ncdump -h ${filename} | grep constants_file_name`
       set filename = `ncdump -h ${filename} | grep 'constants_file_name' | awk '{print $3}'`
       set filename = `echo ${filename} | sed -e 's/\"//g'`
       set ok       = `ncdump -h ${filename} | grep pollon`
       set pollon   = `ncdump ${filename}    | grep 'pollon =' | awk '{print $3}'`
    endif

    set pollat = ""
    set ok     = `ncdump -h ${filename} | grep pollat`
    if ( "${ok}" != "" ) then
       set pollat = `ncdump ${filename} | grep 'pollat =' | awk '{print $3}'`
    else 
       set ok       = `ncdump -h ${filename} | grep constants_file_name`
       set filename = `ncdump -h ${filename} | grep 'constants_file_name' | awk '{print $3}'`
       set filename = `echo ${filename} | sed -e 's/\"//g'`
       set ok       = `ncdump -h ${filename} | grep pollat`
       set pollat   = `ncdump ${filename}    | grep 'pollat =' | awk '{print $3}'`
    endif

    if ( ( "${pollon}" == "" ) | ( "${pollat}" == "" ) ) then
       echo "cannot find pollon/pollat on ${filename}... Stop"
       exit 1
    endif

    set pole = "${pollon} ${pollat}"

endif

# Write the pole position to the pole file  
\rm -f rot2geo.pole
echo ${pole} > rot2geo.pole

# Decide whether to exit from the script
if ( "${inpfile}" == "-lonlat" ) set poleprint = 1
if ( "${inpfile}" == "-file"   ) set poleprint = 1

# On request, print the pole position and exit
if ( ${poleprint} == 1 ) then
    cat rot2geo.pole
    exit 0
endif

# Decide whether a position or a trajectory is to be rotated
set ok1 = `echo ${inpfile} | sed -e 's/[0123456789.-]//g' | wc -w`
set ok2 = `echo ${outfile} | sed -e 's/[0123456789.-]//g' | wc -w`

if ( ( ${ok1} == 0 ) && ( ${ok2} == 0 ) ) then
    set mode = 'position'
    set lon  = ${inpfile}
    set lat  = ${outfile}
else if ( ( ${ok1} != 0 ) && ( ${ok2} != 0 ) ) then
    set mode = 'trajectory'
    if ( ! -f ${inpfile} ) then
       echo "${inpfile} is missing... Stop"
       exit 1
    endif
else
    set mode = 'nil'
endif

# Decide whether the Fortran programe needs to be launched
if ( "${mode}" == "nil" ) then
    exit 0
endif
    
# Prepare the parameter file and run Fortran program
\rm -f rot2geo.param
echo \"${mode}\"                             >! rot2geo.param
if ( "${mode}" == "position" ) then
   echo ${lon}                               >> rot2geo.param
   echo ${lat}                               >> rot2geo.param
else if ( "${mode}" == "trajectory" ) then
   echo \"${inpfile}\"                       >> rot2geo.param
   echo \"${outfile}\"                       >> rot2geo.param
   ${LAGRANTO}/bin/trainfo.sh ${inpfile} dim >> rot2geo.param
endif
echo ${pole}                                 >> rot2geo.param

${LAGRANTO}/goodies/rot2geo

# Make clean
#\rm -f rot2geo.param

exit 0

