#!/bin/csh -f
#
# dsp2gif script
# 7/7/95 RCG
#
# 5/30/98 KWS
# Adds lat-lon grid and 200 meter bathymmetry on the fly, then
# it converts DSP formatted image file into gif image file as before
#
# February 17, 1999 rcg
# Migrate script to UMass/Dartmouth.  Change file locations to new
# places.
# March 3, 1999 rcg Change REMOTE_HOST to REMOTE_ADDR.  Define
#	pre_dir definition to define preface data directory location
#	needed by this routine (but don't know why.)  rcg
# March 19, 1999 rcg Add additional diagnostic messages and do not
#	delete temporary file if debugfile exists. Also use the
#	existence of this file to determine whether diagnostic messages
#	are printed out.
# April 26, 1999.  Chanage specification of imagepath.  Change logic
#	for debugfile.  Specifiy specific location for the file.  rcg 
# 
# Date: 5/19/99 Modified to insert DSP environmental variables.
# Rcg & GAS
#
# Assumes QUERY_STRING is the file to be converted
# mia2gifpath is set to the directory of where the mia2gif program resides
# tmppath is set to the directory where temporary gif files will be stored
# palfile is set to the full path/file name of the pallette data file
#
# #########################################################################


set version="June 15, 1999"



##echo "Content-type: text/plain"
##echo ""
##echo "# from dsp2gif QUERY_STRING = $QUERY_STRING"



# ##############################################################
# 			DSP environment setup

setenv DSPROOT /usr/dsp
setenv DSPSRC $DSPROOT/src      #  DSP sources live here
setenv MAKELIBDIR $DSPROOT/etc  #  Used by pmake to locate default rules
setenv LD_LIBRARY_PATH $DSPROOT/shlib
setenv TERM vt100
# SET THE PATH WHERE CSH SEARCHES FOR PROGRAMS
set path = ($DSPROOT/exe $DSPROOT/bin $DSPROOT/etc $DSPROOT/shlib $DSPROOT/lib $path)

##################################################################





# #################################################################
#  			Set directory variables

set pre_dir=/hosts/adriatic
set dspin=$pre_dir$QUERY_STRING
set mia2gifpath=/home/user/rgroman/support
set tmppath=/home/user/globec/port8080/htdocs/temp-dir
#Use a different way of specifying the imagepath since the
#	/usr/people/.. path is not visible from other accounts. rcg 4/26/1999
set imagepath=/home/user/globec/port8080/htdocs/images
set palfile=/home/user/rgroman/support/petes.pal
set debugfile=/usr/people/globec/debugfile
set dspbinpath=/hosts/adriatic/data1/dsp/bin







# ###########################################
#  Set DEbugging

@ debug = 1
#Set debug flag to 0 if you want debug output
if ( -e $debugfile ) @ debug = 1
if ($debug == 0) echo "Content-type: text/plain"
if ($debug == 0) echo ""
if ($debug == 0) echo "# dsp2gif version of $version QUERY_STRING = $QUERY_STRING"


set tmp=`date '+%H%M%S'`
if ($REMOTE_ADDR == "") then
	setenv REMOTE_ADDR "tmp.host"

endif

set gifout=$tmppath/$tmp.$REMOTE_ADDR.gif
#echo " gifout = $gifout"






# ###################################################
#   Set to use compressed files

set ext=$dspin:e

set zcatout = "no"
if ($ext == "Z") then
	set zcatout=$tmppath/$tmp.$REMOTE_ADDR.zcat
	zcat $dspin > $zcatout
	chmod o+w $tmppath/*.$REMOTE_ADDR.zcat
	set dspin=$zcatout
endif





########################################################################
#  		more preliminaries

set add_name = add
set diff_name = diff
set standard_out = "$tmppath/$tmp.$REMOTE_ADDR."



#########################################################################
#		"diff" section for grid

set specified_in_1 = "$imagepath/glob_std.tpl"
set standard_in_1 = "$dspin"
set standard_out_1 = "${standard_out}temp_1"
set tdiff_1 = "$dspbinpath/$diff_name $specified_in_1,0,245,0.0,2.0,0.,1.,0.0,1.0" 
set fdiff_1="$tdiff_1 in=$standard_in_1 out=$standard_out_1"


##########################
if ($debug == 0) echo "fdiff_1 command is:"
if ($debug == 0) echo "  $fdiff_1"

$fdiff_1                    # this is the entire command string
################################################################################
##################################################################################


set tempin = "$standard_out_1"	# grid in black, with bathymetry

if ($debug == 0) echo "Before mia2gif program "
if ($debug == 0) echo "   tempin=$tempin"
if ($debug == 0) echo "   gifout=$gifout"
if ($debug == 0) echo "   palfile=$palfile"







# ##############################################################
# Put filename on the image

rm -f /usr/people/globec/port8080/htdocs/temp-dir/fnam
rm -f /usr/people/globec/port8080/htdocs/temp-dir/fnam2
echo $dspin > /usr/people/globec/port8080/htdocs/temp-dir/fnam2
set infil = /usr/people/globec/port8080/htdocs/temp-dir/fnam2
set glntmp = `cut -f9 -d/ $infil`
echo $glntmp > /usr/people/globec/port8080/htdocs/temp-dir/fnam
echo $tempin >> /usr/people/globec/port8080/htdocs/temp-dir/fnam
/home/user/rgroman/globec/htmlbin/ad_nam.pl
chmod g+rwx /usr/people/globec/port8080/htdocs/temp-dir/ad_nam.csh
chmod o+rwx /usr/people/globec/port8080/htdocs/temp-dir/ad_nam.csh

# dsp < /usr/people/globec/port8080/htdocs/temp-dir/tmp.dsp
#/usr/people/globec/port8080/htdocs/temp-dir/ad_nam.csh



#######################
# Create the .GIF image

 $mia2gifpath/mia2gif in=$tempin out=$gifout pal=\($palfile,24\)



if ($debug == 0) echo "After mia2gif program"

chmod o+w $tmppath/*.$REMOTE_ADDR.gif


if (-e $zcatout && $debug == 1) rm $zcatout

if (-e $gifout) then

#	echo "Content-type: text/html"
#	ECHO ""
#	echo "Here is the satellite image requested"
#	echo "\<p\>\<img src=\"$gifout\"\>\</p\>"
#	echo " gifout file = $gifout created" ; ls -l $gifout

	echo "Content-type: image/gif"
	echo ""
	cat $gifout
 	if ( $debug  == 1) rm $gifout

else

	if ($debug == 1) echo " Content-type: text/plain"
	if ($debug == 1) echo ""
	echo " Could not convert the satellite image from the file "
	echo "  $dspin " 
	echo " Please contact the U.S. GLOBEC Georges Bank"
	echo " Data Mangement Office."
	echo " "
	echo " The following information may be useful for debugging the problem."
	echo " QUERY_STRING = $QUERY_STRING"
	echo " Debug flag file = $debugfile"
	echo " Debug flag = $debug"

	echo " pre_dir = $pre_dir"
	echo " mia2gifpath = $mia2gifpath"
	echo " tmppath = $tmppath"
	echo " palfile = $palfile"
	echo " dspin = $dspin"
	echo " ext = $ext"
	echo " zcatout = $zcatout"
	echo " gifout = $gifout"
	
	echo " specified_in_1 = $specified_in_1"
	echo " standard_in_1 = $standard_in_1"
	echo " standard_out_1 = $standard_out_1"
	echo " tdiff_1 = $tdiff_1"
	echo " fdiff_1 = $fdiff_1"

	echo " tempin = $tempin"
endif


