#!/bin/tcsh

if ($# == 0) then
        echo "USAGE: new_redoss"
	echo "\t(-v) .......... verbose"
	echo "\t(-force) ..... force the creation of imagery, skips creation if imagery already created and .merged file is older than imagery files"
	echo "\t(-viewss) .... for real-time viewing of the output imagery from each line"
	echo "\t(-pixel 0.5) ... force the pixel size of the output imagery"
	echo "\t(-DN_Shift 40) ... apply an intensity shift to all data"
	echo "\t(-rolling 500) ... number of pings used in rolling average BP calcs"
	echo "\t(-skip_plain) .... skip making plain ss, this over-rides -force"
	echo "\t(-skip_bp) .... skip making ss with beam pattern correction, this over-rides -force"
	echo "\t(-skip_rolling) .... skip making ss with rolling beam pattern correction, this over-rides -force"
	echo "\tmerged/JDXXX/file(s).merged"
	echo "\nNote: following subdirs are auto-generated:"
	echo "\t\t\t\tss/subdir"
	echo "\n\tExample file path: merged/JD220/0071_20050808_001404.merged\n"
        exit(0)
endif

# Various options
set VERBOSE = 0
set FORCE = 0
set REAL_TIME_VIEW_SS = 0
set SS_PIXEL_SIZE = 0
set DN_SHIFT = 0
set ROLLING = 500

set SKIP_PLAIN_SS = 0
set SKIP_BP_SS = 0
set SKIP_ROLLING_SS = 0

set NUM_TO_DO = 0
set FILES_TO_DO = ""

while ($#argv)

	switch ($argv[1])
		case -v:
			set VERBOSE = 1
		breaksw

		case -viewss:
			set REAL_TIME_VIEW_SS = 1
		breaksw

		case -force:
			set FORCE = 1
		breaksw

		case -pixel:
			shift argv
			set SS_PIXEL_SIZE = $argv[1]
		breaksw

		case -DN_Shift:
			shift argv
			set DN_SHIFT = $argv[1]
		breaksw

		case -rolling:
			shift argv
			set ROLLING = $argv[1]
		breaksw

		case -skip_plain:
			set SKIP_PLAIN_SS = 1
		breaksw

		case -skip_bp:
			set SKIP_BP_SS = 1
		breaksw

		case -skip_rolling:
			set SKIP_ROLLING_SS = 1
		breaksw

	default:
		# Any other argument must be an input file, so 
		# make sure it exists
		if (! -e $argv[1]) then
			echo "Hey" $argv[1] "does not exist (or mispelled command line arg?)"
			exit(1)
		endif

		set FILES_TO_DO = `echo $FILES_TO_DO $argv[1]`
		@ NUM_TO_DO = $NUM_TO_DO + 1
		breaksw
	endsw

	shift argv
end

set BAD_PATH = 0
foreach COMMAND (getBeamPattern compAngStrip makess glfill jview )
	which $COMMAND > /dev/null
	if ($?) then 
		echo "Can't find" $COMMAND "in your path!"
		set BAD_PATH = 1
	endif
end

if ($BAD_PATH) then
	echo "One or more OMG binaries are not visible in your path!"
	exit(1)
endif

if (! -w . ) then
        echo "You don't have write permissions for this directory!"
        exit(1)
endif

set FILE_COUNT = 0
set SKIP_COUNT_PLAIN = 0
set SKIP_COUNT_BP = 0
set SKIP_COUNT_ROLLING = 0
set ERROR_COUNT = 0
set ERROR_COUNT_PLAIN = 0
set ERROR_COUNT_BP = 0
set ERROR_COUNT_ROLLING = 0

foreach FILE ($FILES_TO_DO)

        # Retooling this to handle the case of no subdirectory or many subdirectory levels
        # The raw directory is the first field in the path (separated by /)
        set MERGEDDIR = `echo $FILE | awk -F/ '{print $1}' `
        
        # The line name is the last field in the path (separated by /)
        set LINE_NUMBER = `echo $FILE | awk -F/ '{print $NF}' | sed -e 's/\.merged//' `
        
        # The subdirectorys are the remaining fields in the path once you've removed the raw directory and line name
        set SUBDIRS = `echo $FILE | sed -e 's/'$MERGEDDIR'\///'  -e 's/'$LINE_NUMBER'//' -e 's/\.merged//' `

        if ($VERBOSE) then
                echo "From" $FILE", I parsed the following:"
                echo "Merged directory:" $MERGEDDIR
                echo "Sub directories:" $SUBDIRS
                echo "Line number:" $LINE_NUMBER
        endif

	if (! (-e merged/$SUBDIRS/$LINE_NUMBER.merged) ) then
		echo "No merged file found by that name"
		echo "data must be in path: merged/subdir/"
		echo `date` $SUBDIRS $LINE_NUMBER "...No merged file found by that name!" >> error.log
		@ ERROR_COUNT = $ERROR_COUNT + 1
		continue
	endif

	# No harm in making this if it already exists
	mkdir -p ss/$SUBDIRS

	# Decide whether or not to bother with plain ss
	if ($SKIP_PLAIN_SS) then
		set DO_PLAIN_SS = 0
	else
		set DO_PLAIN_SS = 1
	endif

	# Check the timestamp of pre-existing .ss files
	if ($DO_PLAIN_SS && -e ss/$SUBDIRS/$LINE_NUMBER.ss) then
		set NEWER = `find merged/$SUBDIRS/$LINE_NUMBER.merged -newer ss/$SUBDIRS/$LINE_NUMBER.ss|wc -l`

		if (! $NEWER && ! $FORCE) then
			echo "Skipping merged/"$SUBDIRS"/"$LINE_NUMBER".merged"
			@ SKIP_COUNT_PLAIN = $SKIP_COUNT_PLAIN + 1
			set DO_PLAIN_SS = 0
		endif
	endif

	if ($DO_PLAIN_SS) then
		makess \
			-DN_Shift $DN_SHIFT \
			-pixel $SS_PIXEL_SIZE \
			merged/$SUBDIRS/$LINE_NUMBER.merged \
			ss/$SUBDIRS/$LINE_NUMBER.temp

		if ($?) then
			echo `date` $SUBDIRS $LINE_NUMBER "...puked on makess (plain)..." >> error.log
			rm ss/$SUBDIRS/$LINE_NUMBER.temp
			@ ERROR_COUNT_PLAIN = $ERROR_COUNT_PLAIN + 1
		else
			glfill \
				ss/$SUBDIRS/$LINE_NUMBER.temp \
				ss/$SUBDIRS/$LINE_NUMBER.ss

			/bin/rm ss/$SUBDIRS/$LINE_NUMBER.temp
		endif
	endif

	# Decide whether or not to bother with bp ss
	if ($SKIP_BP_SS) then
		set DO_BP_SS = 0
	else
		set DO_BP_SS = 1
	endif

	# Check the timestamp of pre-existing .ss_bp files
	if ($DO_BP_SS && -e ss/$SUBDIRS/$LINE_NUMBER.ss_bp) then
		set NEWER = `find merged/$SUBDIRS/$LINE_NUMBER.merged -newer ss/$SUBDIRS/$LINE_NUMBER.ss_bp|wc -l`

		if (! $NEWER && ! $FORCE) then
			echo "Skipping merged/"$SUBDIRS"/"$LINE_NUMBER".merged"
			@ SKIP_COUNT_BP = $SKIP_COUNT_BP + 1
			set DO_BP_SS = 0
		endif
	endif

	if ($DO_BP_SS) then
		# Running with -by_mode will create .bp files by mode.  No harm in running it this way
		# if there's only one mode
		getBeamPattern \
			-DN_Shift $DN_SHIFT \
			-by_mode \
			-out merged/$SUBDIRS/$LINE_NUMBER.bp \
			merged/$SUBDIRS/$LINE_NUMBER

		if ($?) then
			echo `date` $SUBDIRS $LINE_NUMBER "...puked on getBeamPattern..." >> error.log
			@ ERROR_COUNT_BP = $ERROR_COUNT_BP + 1
		else
			# Running with -multimode_bp does no harm if there's only one mode
			makess \
				-DN_Shift $DN_SHIFT \
				-multimode_bp merged/$SUBDIRS/$LINE_NUMBER.bp \
				-pixel $SS_PIXEL_SIZE \
				merged/$SUBDIRS/$LINE_NUMBER.merged \
				ss/$SUBDIRS/$LINE_NUMBER.temp

			if ($?) then
				echo `date` $SUBDIRS $LINE_NUMBER "...puked on makess (with beam pattern)..." >> error.log
				rm ss/$SUBDIRS/$LINE_NUMBER.temp
				@ ERROR_COUNT_BP = $ERROR_COUNT_BP + 1
			else
				glfill \
					ss/$SUBDIRS/$LINE_NUMBER.temp \
					ss/$SUBDIRS/$LINE_NUMBER.ss_bp

				/bin/rm ss/$SUBDIRS/$LINE_NUMBER.temp
			endif
		endif
	endif

	# Decide whether or not to bother with rolling ss
	if ($SKIP_ROLLING_SS) then
		set DO_ROLLING_SS = 0
	else
		set DO_ROLLING_SS = 1
	endif

	# Check the timestamp of pre-existing .ss_$ROLLING files
	if ($DO_ROLLING_SS && -e ss/$SUBDIRS/$LINE_NUMBER.ss_$ROLLING) then
		set NEWER = `find merged/$SUBDIRS/$LINE_NUMBER.merged -newer ss/$SUBDIRS/$LINE_NUMBER.ss_$ROLLING|wc -l`

		if (! $NEWER && ! $FORCE) then
			echo "Skipping merged/"$SUBDIRS"/"$LINE_NUMBER".merged"
			@ SKIP_COUNT_ROLLING = $SKIP_COUNT_ROLLING + 1
			set DO_ROLLING_SS = 0
		endif
	endif

	if ($DO_ROLLING_SS) then
		compAngStrip \
        		-DN_Shift $DN_SHIFT \
        		-ss \
        		-stack $ROLLING \
        		-out_prefix merged/$SUBDIRS/ \
        		-prefix merged/$SUBDIRS/ \
        		-suffix .merged \
        		$LINE_NUMBER

		if ($?) then
			echo `date` $SUBDIRS $LINE_NUMBER "...puked on compAngStrip..." >> error.log
			rm merged/$SUBDIRS/$LINE_NUMBER\_Rolling_Averages_$ROLLING
			@ ERROR_COUNT_ROLLING = $ERROR_COUNT_ROLLING + 1
		else
			makess \
				-pixel $SS_PIXEL_SIZE \
				-DN_Shift $DN_SHIFT \
				-rolling \
				-beam_patt merged/$SUBDIRS/$LINE_NUMBER\_Rolling_Averages_$ROLLING \
				merged/$SUBDIRS/$LINE_NUMBER.merged ss/$SUBDIRS/$LINE_NUMBER.temp

			if ($?) then
				echo `date` $SUBDIRS $LINE_NUMBER "...puked on makess (with rolling beam pattern)..." >> error.log
				rm ss/$SUBDIRS/$LINE_NUMBER.temp
				@ ERROR_COUNT_ROLLING = $ERROR_COUNT_ROLLING + 1
			else
				glfill \
					ss/$SUBDIRS/$LINE_NUMBER.temp \
					ss/$SUBDIRS/$LINE_NUMBER.ss_$ROLLING

				/bin/rm ss/$SUBDIRS/$LINE_NUMBER.temp
			endif
		endif
	endif


	# Phew...done making imagery 
	if ($REAL_TIME_VIEW_SS) then
		jview -reclen 1160 -initstr 150 245 ss/$SUBDIRS/$LINE_NUMBER.ss* &
	endif

	if ($VERBOSE) then
		echo "Done" $SUBDIRS $LINE_NUMBER "!\n"
	endif

	@ FILE_COUNT = $FILE_COUNT + 1
end

echo 

if ($FILE_COUNT) then
	echo "Did" $FILE_COUNT "of" $NUM_TO_DO "files...\n"
endif

if ($SKIP_COUNT_PLAIN) then
	echo "Skipped" $SKIP_COUNT_PLAIN "of" $NUM_TO_DO ".ss files... (use -force to force creation)"
endif

if ($ERROR_COUNT_PLAIN) then
	echo "Errors for" $ERROR_COUNT_PLAIN "of" $NUM_TO_DO ".ss files... (see error.log for details)"
endif

if ($SKIP_COUNT_BP) then
	echo "Skipped" $SKIP_COUNT_BP "of" $NUM_TO_DO ".ss_bp files... (use -force to force creation)"
endif

if ($ERROR_COUNT_BP) then
	echo "Errors for" $ERROR_COUNT_BP "of" $NUM_TO_DO ".ss_bp files... (see error.log for details)"
endif

if ($SKIP_COUNT_ROLLING) then
	echo "Skipped" $SKIP_COUNT_ROLLING "of" $NUM_TO_DO ".ss_$ROLLING files... (use -force to force creation)"
endif

if ($ERROR_COUNT_ROLLING) then
	echo "Errors for" $ERROR_COUNT_ROLLING "of" $NUM_TO_DO ".ss_$ROLLING files... (see error.log for details)"
endif

if ($ERROR_COUNT) then
	echo "General errors for" $ERROR_COUNT "of" $NUM_TO_DO "files... (see error.log for details)"
endif

exit(0)

