#!/bin/sh

#--------------------------------------------------------------------------
#
# Name:     goescopy.sh
#
# Purpose:  Bourne shell script used to run a sequence of McIDAS commands
#           outside of a McIDAS-X session.
#
# Syntax:   goescopy.sh dset desc band
#
# Examples: goescopy.sh EAST NH    1     <- GOES-East, Northern Hemisphere visible
#           goescopy.sh EAST SH    1     <- GOES-East, Southern Hemisphere visible
#           goescopy.sh EAST CONUS 1     <- GOES-East, Contiental U.S. visible
#           goescopy.sh EAST FD    1     <- GOES-East, full disk U.S. visible
#           ...
#
# Notes:    Environment variables that need to be set for McIDAS commands
#           to run outside of a McIDAS-X session:
#
#             MCHOME - the home directory for the user running mcrun.sh
#
#             MCDATA - the user's McIDAS-X working directory.  NOTE: this
#                      directory should also be the first directory
#                      specified in MCPATH.
#
#             MCPATH - colon separated list of directories that contain
#                      McIDAS data files, ancillary data files, and help
#                      files
#
#             PATH -   a colon separated list of directories that
#                      Unix will search when looking for executables.
#                      NOTE: the ~mcidas/bin directory should be first in
#                      the list of directories that will be searched.
#
#             LD_LIBRARY_PATH - the search path for sharable libraries;
#                      this should be the same search path as the one
#                      used by the McIDAS session
#
#             MCBATCHFILE.BAT - the name of the McIDAS BATCH file that
#                      you want to run.
#
#           This is a Bourne shell script.  All statements must conform to
#           Bourne shell scripting syntax.
#
#           The example PATH and LD_LIBRARY_PATH definitions are set
#           for Sun Solaris.  AIX and HP-UX users need to change
#           LD_LIBRARY_PATH everywhere in this file to what is appropriate
#           on their systems
#
#             AIX:  LD_LIBRARY_PATH -> LIBPATH
#             HPUX: LD_LIBRARY_PATH -> SHLIB_PATH
#
#
# History:  20000607 - Written for Unidata McIDAS-X, -XCD 7.70
#           20040720 - Changed example display to run MAP and BAR separately
#           20091012 - Created example script for Kings College London for
#                        accessing GOES-East/West data to a local AREA dataset
#
#--------------------------------------------------------------------------

# Define macros needed for McIDAS-7.X environment
#
# MCHOME - set this to the HOME directory of the user McIDAS was installed as
# 
# MCDATA - this will be set according to who is running the script:
# 
#          user       MCDATA
#          ----------+---------------------
#          mcidas     $HOME/workdata
#          other      $HOME/mcidas/data
#
# MCPATH - this is a colon-separated list of directories McIDAS will search
#          when looking for anciallary data (e.g., map databases, enhancements,
#          stretch tables, etc.) and data (e.g., AREA, MDXX, GRID, and TEXT
#          files.
#
#          The $MCDATA directory should _always_ be the first MCPATH directory!
#
# MCLOG  - the name of the file to log output to; defaulted to
#          $MCDATA/mcrun.log
#
# MCTABLE_READ - a quoted, semi-colon-separated list of directories to search
#          for McIDAS-X DATALOCation information.  This is typically
#          MCTABLE_READ="$MCDATA/MCTABLE.TXT;$MCHOME/data/ADDESITE.TXT"
#          but can be expanded to suit the user's needs.
#
# PATH   - Unix shell search PATH with $MCHOME/bin being the first directory.
#
# LD_LIBRARY_PATH - most likely not needed, but put in for future use
#

SHELL=sh
export SHELL

#
# First, define MCHOME as the HOME directory for the user under which
# McIDAS-X is installed.
#

MCHOME=/home/mcidas

#
# Find out who is trying to run the script.  Don't allow 'root'!
#

case `id` in
'uid=0('*)
	echo "ERROR: cannot execute as user 'root'"
	exit 1
	;;
uid=?'(mcidas)'*	|\
uid=??'(mcidas)'*	|\
uid=???'(mcidas)'*	|\
uid=????'(mcidas)'*	|\
uid=?????'(mcidas)'*	|\
uid=??????'(mcidas)'*	|\
uid=???????'(mcidas)'*	)
	MCDATA=$HOME/workdata
	;;
*)
	MCDATA=$HOME/mcidas/data
	;;
esac

#
# Set MCPATH using MCDATA and MCHOME
#

MCPATH=${MCDATA}:$MCHOME/data:$MCHOME/help

#
# Define MCLOG to be $MCDATA/mcrun.log
#

MCLOG=$MCDATA/goescopy.log

#
# Set MCTABLE_READ based on the existence of $MCDATA/MCTABLE.TXT.  Users
# should modify this section to match their setup if it does not follow
# the recommendations in the Unidata McIDAS-X online documentation
# for configuring user accounts.
#

if [ -f "${MCDATA}/MCTABLE.TXT" ]; then
  MCTABLE_READ="${MCDATA}/MCTABLE.TXT;${MCHOME}/data/ADDESITE.TXT"
else
  MCTABLE_READ="${MCHOME}/data/ADDESITE.TXT"
fi

#
# Setup PATH so that the McIDAS-X executables can be found
#

PATH=$MCHOME/linux64/bin:$PATH

#
# Set LD_LIBRARY_PATH to include all directories (other than those searched
# by default) that are needed to be searched to find shared libraries.
#

LD_LIBRARY_PATH=$MCHOME/lib:/usr/local/lib:/lib:/usr/lib:/usr/ucblib

#
# Send all textual output to the log file
#

exec 2>$MCLOG 1>&2

#
# Export the environment variables specified above
#

export MCHOME MCDATA MCLOG MCPATH MCTABLE_READ PATH LD_LIBRARY_PATH

#
# Check the command line parameters
#

case $# in
0)	echo "usage: $0 dset desc band..."
	exit 1
	;;
1)	echo "usage: $0 dset desc band..."
	exit 0
	;;
2)	echo "usage: $0 dset desc band..."
	exit 0
	;;
esac

dset=$1
desc=$2
band=$3

#
# Validate input values
#

case $dset in
EAST)	cyl=`expr "$band" + 3000`
	case $desc in
		CONUS)	;;
		NH)	;;
		SH)	;;
		FD)	;;
		*)	echo $0 Error: dataset descriptor must be CONUS, NH, SH or FD
			echo $0 exiting...
			exit 1
			;;
	esac
	case $band in
		1)	;;
		2)	;;
		3)	;;
		4)	;;
		6)	;;
		*)	echo $0 Error: band must be 1 \(0.64 um\), 2 \(3.9 um\), 3 \(6.5 um\), 4 \(10.7 um\) or 6 \(13.3 um\)
			echo $0 exiting...
			exit 1
			;;
	esac
	;;
WEST)	cyl=`expr "$band" + 3010`
	case $desc in
		CONUS)	;;
		NH)	;;
		SH)	;;
		FD)	;;
		*)	echo $0 Error: dataset descriptor must be CONUS, NH, SH or FD
			echo $0 exiting...
			exit 1
			;;
	esac
	case $band in
		1)	;;
		2)	;;
		3)	;;
		4)	;;
		5)	;;
		*)	echo $0 Error: band must be 1 \(0.65 um\), 2 \(3.9 um\), 3 \(6.8 um\), 4 \(10.7 um\) or 5 \(12.0 um\)
			echo $0 exiting...
			exit 1
			;;
	esac
	;;
SOUTH)	cyl=`expr "$band" + 3020`
	case $desc in
		SH)	;;
		FD)	;;
		*)	echo $0 Error: dataset descriptor must be SH or FD
			echo $0 exiting...
			exit 1
			;;
	esac
	case $band in
		1)	;;
		2)	;;
		3)	;;
		4)	;;
		5)	;;
		*)	echo $0 Error: band must be 1 \(0.65 um\), 2 \(3.9 um\), 3 \(6.8 um\), 4 \(10.7 um\) or 5 \(12.0 um\)
			echo $0 exiting...
			exit 1
			;;
	esac
	;;
*)	echo $0 Error: dataset must be EAST, WEST or SOUTH
	echo $0 exiting...
	exit 1
	;;
esac

#
# Now run 'mcenv' to create a McIDAS-X environment in which the McIDAS
# binary commands will run.
#
# It is the user's responsibility to put the commands they want to run
# in the following.
#

cd $MCDATA
mcenv << EOF

# put McIDAS-X commands you want to run here, one command per line.

# First, make sure that we have a local dataset into which to write
# GOES-East/West images.  We will use the MYDATA/IMAGES dataset:
dsserve.k ADD MYDATA/IMAGES AREA 1 9999 \"All images in AREA format

# Define where to get the realtime GOES images
dataloc.k ADD EAST  goeseast.unidata.ucar.edu
dataloc.k ADD WEST  goeswest.unidata.ucar.edu
dataloc.k ADD SOUTH goessouth.unidata.ucar.edu

# We will set aside certain AREA file numbers based on
# the satellite/band we want to copy from the server:
#
# EAST/<descriptor> BAND=1 - AREA3001 -- 0.65 um visible
#                   BAND=2 - AREA3002 --  3.9 um short IR
#                   BAND=3 - AREA3003 --  6.5 um water vapor
#                   BAND=4 - AREA3004 -- 10.7 um thermal IR
#                   BAND=6 - AREA3006 -- 13.3 um co2
#
# WEST/<descriptor> BAND=1 - AREA3011 -- 0.65 um visible
#                   BAND=2 - AREA3012 --  3.9 um short IR
#                   BAND=3 - AREA3013 --  6.8 um water vapor
#                   BAND=4 - AREA3014 -- 10.7 um thermal IR
#                   BAND=5 - AREA3015 -- 12.0 um long IR
#
# SOUTH/SH          BAND=1 - AREA3021 -- 0.65 um visible
#                   BAND=2 - AREA3022 --  3.9 um short IR
#                   BAND=3 - AREA3023 --  6.8 um water vapor
#                   BAND=4 - AREA3024 -- 10.7 um thermal IR
#                   BAND=5 - AREA3025 -- 12.0 um long IR

# Do the copy
imgcopy.k $dset/$desc MYDATA/IMAGES.$cyl BAND=$band SIZE=SAME

# Done
  exit

EOF

# Done
exit 0

