#! /bin/sh
#---------------------------------------
############################################################################################
echo
echo "+---------------------------------------------------------------------------+"
echo "|                            BATCH PIPELINE V3.4                            |"
echo "|                                                                           |"
echo "|                        This is a shell program for                        |"
echo "|                      executing pipeline in batch mode                     |"
echo "|                                                                           |"
echo "|           Note :         This  version can check                          |"
echo "|                  illegal file name and available disk space               |"
echo "|                  ==========================================               |"
echo "|                                       Z.-Y. Zheng, 05/21/95               |"
echo "+---------------------------------------------------------------------------+"
echo "|                                                                           |"
echo "|     Add 515x512 image size                                                |"
echo "|                                                                           |"
echo "|                                       ZHOU Xu, 05/05/05                   |"
echo "+---------------------------------------------------------------------------+"
echo
echo "+---------------------------------------------------------------------------+"
echo "|                                                                           |"
echo "|     Add W as triple peak filter name                                                |"
echo "|                                                                           |"
echo "|                                       ZHOU Xu, 06/01/01                   |"
echo "+---------------------------------------------------------------------------+"
echo
############################################################################################
# new features of V26.4:
#
# Do nothing more if there is no T or M or S fields.
#
# zj Tue Nov 12 19:52:43 GMT+0800 1996
############################################################################################
# new features of V26.3:
#
# A bug of no-processing when there is only flat data is corrected.
#
# zj Wed Sep 18 08:55:24 CST 1996
############################################################################################
# Ported to 586
# 
# ZZY 961006
###########################################################################################
# add facility for supernova data from V26 to V27.
#
# ZJ 961010
###########################################################################################
# change some orders or working directories. See menual for detail.
#
# ZJ 961012  (from V2.7 to V3.0)
###########################################################################################
# change some disk names according to new convention.
#
# ZJ 970105  (from V3.2 to V3.3)
###########################################################################################
# check user

user=`whoami`
if test "$user" != "ccdev"
then
	echo ""
	echo "	You must be ccdev for executing this program\!"
	echo ""
	exit
fi
############################################################################################
# check dir

dir=`pwd | cut -c1-14`
if test "$dir" != "/pip1/data/pip"
then
	echo ""
	echo "       You must be in /pip1/data/pipxxxx for executing this program\!"
	echo ""
	exit
fi
##########################################################################################
set `ls d*T???????.fit 2> /dev/null | wc`
tframe=$1
set `ls d*C???????.fit 2> /dev/null | wc`
cframe=$1
set `ls d*N???????.fit 2> /dev/null | wc`
nframe=$1
set `ls d*Pole????.fit 2> /dev/null | wc`
pframe=$1
set `ls d*M???????.fit 2> /dev/null | wc`
mframe=$1
set `ls d*S???????.fit 2> /dev/null | wc`
sframe=$1
set `ls d*U???????.fit 2> /dev/null | wc`
uframe=$1
set `ls d*K???????.fit 2> /dev/null | wc`
kframe=$1
set `ls d*F???????.fit 2> /dev/null | wc`
fframe=$1
set `ls d*Z001????.fit 2> /dev/null | wc`
zpreframe=$1
set `ls d*Z002????.fit 2> /dev/null | wc`
zposframe=$1
###########################################################################################
frame=`expr $tframe + $cframe + $nframe + $pframe + $mframe + $sframe + $uframe + $kframe + $fframe + $zpreframe + $zposframe`
if test "$frame" = "0" 
then
	echo
	echo "+---------------------------------------+"
	echo "|	no frames to be processed	|"
	echo "+---------------------------------------+"
	echo

	exit
fi
###########################################################################################
# determine RUN and JD

ls d*.fit | 
gawk 	'BEGIN{
		"pwd" | getline
		dir=$1
		dirjd=substr(dir,length(dir)-3,4)
	}

	{filename = $1}

	filename ~ /^d[0-9][0-9][0-9][0-9][0-9][0-9][0-9][TCNPSMUKF]/{
		frun = substr(filename,2,3)
		fjd = substr(filename,5,4)
		runcount[frun]++
		jdcount[fjd]++
	}

	END{	
		bigruncount = 0
		bigjdcount = 0
		for(frun in runcount){
			if(runcount[frun] > bigruncount){
				run = frun
				bigruncount = runcount[frun]
			}
		}
		for(fjd in jdcount){
			if(jdcount[fjd] > bigjdcount){
				jd = fjd
				bigjdcount = jdcount[fjd]
			}
		}

		{
			if(dirjd != jd){
				printf "error" > "runjd.error"
			}else{
				printf "%3s %4s",run,jd > "runjd"
			}
		}
	}'
if test -f "runjd.error"
then
	/bin/rm -f runjd.error
        echo "ERROR! JD number between in directory name and in filename are different"
	exit 2
fi
##########################################################################################################
# check illegal file name

ls -l d*.fit | grep -v doc | grep -v dir | grep -v flat | grep -v total | grep -v runjd | cut -c26-34,48- |
gawk	'BEGIN{
		getline < "runjd"
		run = $1
		jd = $2
		errorsign = "n"
	}

	{
		filesize = $1
		filename = $2
		filerror = "n"
	}


	{
		if(length(filename) != 20){
			if(filerror != "n"){
				printf "%25s : %-20s\n"," ","illegal file name length"
			}else{
				errorsign = "y"
				filerror = "y"
				printf "%25s : %-20s\n",filename,"illegal file name length"
			}
		}
	}

	filename !~ /^d/{
		if(filerror != "n"){
			printf "%25s : %-20s\n"," ","1st character is not \"d\""
		}else{
			errorsign = "y"
			filerror = "y"
			printf "%25s : %-20s\n",filename,"1st character is not \"d\""
		}
	}

	{
		if(substr(filename,2,3) != run){
			if(filerror != "n"){
				printf "%25s : %-20s\n"," ","run number error"
			}else{
				errorsign = "y"
				filerror = "y"
				printf "%25s : %-20s\n",filename,"run number error"
			}
		}
	}

	{
		fjd = substr(filename,5,4)
		type = substr(filename,9,1)
		if(fjd != jd && (type == "T" || type == "C" || type == "N" || type == "P")){
			if(filerror != "n"){
				printf "%25s : %-20s\n"," ","JD number error or illegal type"
			}else{
				errorsign = "y"
				filerror = "y"
				printf "%25s : %-20s\n",filename,"JD number error or illegal type"
			}
		}
	}

	substr(filename,9,1) !~ /[ZFTCNPSMUK]/{
		if(filerror != "n"){
			printf "%25s : %-20s\n"," ","illegal type"
		}else{
			errorsign = "y"
			filerror = "y"
			printf "%25s : %-20s\n",filename,"illegal type"
		}
	}

	substr(filename,10,3) !~ /[0-9A-Za-z][0-9A-Za-z][0-9A-Za-z]|ole/{
		type = substr(filename,9,1)
		if(type != "F"){
			if(filerror != "n"){
				printf "%25s : %-20s\n"," ","illegal target field number"
			}else{
				errorsign = "y"
				filerror = "y"
				printf "%25s : %-20s\n",filename,"illegal target field number"
			}
		}
	}

	substr(filename,13,1) !~ /[a-z]|R|V|B|I|U|W|/{
		if(filerror != "n"){
			printf "%25s : %-20s\n"," ","illegal filter color"
		}else{
			errorsign = "y"
			filerror = "y"
			printf "%25s : %-20s\n",filename,"illegal filter color"
		}
	}

	substr(filename,14,3) !~ /[0-9][0-9][0-9]/{
		if(filerror != "n"){
			printf "%25s : %-20s\n"," ","illegal sequence number"
		}else{
			errorsign = "y"
			filerror = "y"
			printf "%25s : %-20s\n",filename,"illegal sequence number"
		}
	}

	filename !~ /\.fit$/{
		if(filerror != "n"){
			printf "%25s : %-20s\n"," ","illegal suffix"
		}else{
			errorsign = "y"
			filerror = "y"
			printf "%25s : %-20s\n",filename,"illegal suffix"
		}
	}

	END{
		if(errorsign == "y"){
			print "error" > "filename.error"
		}
	}'

if test -f "filename.error"
then
	/bin/rm -f filename.error
	exit 3
fi
#################################################################################
# check available disk space

df | grep -v Filesystem | 
gawk	'BEGIN{
		"pwd" | getline
		rootdir = substr($1,1,5)
	}

	{
		if(rootdir == $6){
			avail = $4
			aframe = int(avail/8400)

			if(aframe < 2){
				print "No enough disk space for processing"
				print "error" > "space.error"
				exit 1
			}

			"ls *Z*.fit 2> /dev/null | wc" | getline; zframe = $1
			system("ls *Z*.fit 2> /dev/null | cut -c10-12 | sort | uniq | wc > Z.wc"); getline < "Z.wc"; rzframe = $1
			system("\\rm -f Z.wc")
			"ls *F*.fit 2> /dev/null | wc" | getline; fframe = $1
			system("ls *F*.fit 2> /dev/null | cut -c13 | sort | uniq | wc > F.wc"); getline < "F.wc"; rfframe = $1
			system("\\rm -f F.wc")
			"ls *T*.fit *N*.fit *Pole*.fit *S*.fit *M*.fit *U*.fit 2> /dev/null | wc" | getline; tframe = $1
			"ls d*C*.fit 2> /dev/null | wc" | getline; cframe = $1

			least=(zframe-rzframe*2)+(fframe-rfframe*2)+aframe
# The following two lines were modified by J.Z. on Dec. 16, 1995
#                       tneed=tframe+(cframe/45)
#			if(least <= tneed*2){
                        tneed=tframe+(cframe/45)+4
			if(least <= tneed){
				print "error" > "space.error"
				exit 2
			}

			exit 0
		}
	}'

if test -f "space.error"
then
	/bin/rm -f space.error
        echo "No enough disk space for processing"
	exit 4
fi
#############################################################################################################
# produce task file

exec < runjd
read run jd
exec < /dev/tty
/bin/rm -f runjd $jd.task*

ls d*S???????.fit d*M???????.fit d*T???????.fit d*C???????.fit d*N???????.fit d*Pole????.fit d*U???????.fit d*F???????.fit 2> /dev/null | cut -c13 | sort | uniq > color.list
exec < color.list
colorline=""
while read color
do
	colorline="$colorline $color"
done
exec < /dev/tty
/bin/rm color.list
set $colorline
for color in $*
do
	set `ls d*F???$color???.fit 2> /dev/null | wc`
	fcnum=$1
	if test "$fcnum" != "0"
	then
		echo "**flat_$color: /dele" >> $jd.taskf
		echo "**flat_$color: /file=F$jd$color.fit" >> $jd.task
		ls d*F???$color???.fit >> $jd.taskf
	else
		echo
		echo "There are not filter_$color flat images taked for JD$jd"
		echo
		while true
		do
			echo -n "Input path name of filter_$color flat image used insteadly : "
			read ffname
			if test -f "$ffname"
			then
				echo "**flat_$color: /file=$ffname" >> $jd.task
				break
			else
				echo
				echo "$ffname doesn't exist!"
				echo
			fi
		done
	fi
done
###########################################################################################
echo "**data: /color=13" >> $jd.taskf
/usr/local/sbin/pip $jd.taskf ! >> task$jd.log 2>> task$jd.log
/bin/rm -f flat.tmp
##########################################################################################
set `ls flat_?.fit 2> /dev/null | wc`
fcnum=$1
if test "$fcnum" != "0"
then
	ls flat_?.fit | cut -c6 | 
	while read color
	do
		mv flat_$color.fit F$jd$color.fit
	done
fi
#########################################################################################
#echo ""
#echo "Flatfield combination finished. Now you can begin to do pipeline"
#echo "for supernova/asteroid field from class2:/minor/super/compress/pip$jd.s"
#echo "for minor planet field from class2:/minor/compress/pip$jd.m."
#echo ""

set `ls /minor/data/raw/raw$jd.m* 2> /dev/null | wc`
fcnum=$1
if test "$fcnum" != "0"
then

echo "cp F${jd}R.fit /minor/data/raw/raw$jd.m*/."
cp F${jd}R.fit /minor/data/raw/raw$jd.m*/.
echo ""

fi

set `ls /super/data/raw/raw$jd.s* 2> /dev/null | wc`
fcnum=$1
if test "$fcnum" != "0"
then

echo "cp F${jd}R.fit /super/data/raw/raw$jd.s*/."
cp F${jd}R.fit /super/data/raw/raw$jd.s*/.
echo ""

fi

echo "I will continue to do pipeline for those fields..."
echo ""
echo "**data: /color=13 /dele" >> $jd.task
ls d*S???????.fit d*M???????.fit d*T???????.fit d*C???????.fit d*N???????.fit d*P???????.fit d*U???????.fit d*K???????.fit >> $jd.task 2> /dev/null
###########################################################################################
if test "$zpreframe" != "0"
then
	ls d*Z001????.fit > zpre.bat
	/usr/local/sbin/batbias zpre.bat Zpre$jd.fit task$jd.log
	/bin/rm -f d*Z001????.fit zpre.bat
fi
##
if test "$zposframe" != "0"
then
	ls d*Z002????.fit > zpos.bat
	/usr/local/sbin/batbias zpos.bat Zpos$jd.fit task$jd.log
	/bin/rm -f d*Z002????.fit zpos.bat
fi
##########################################################################################
/usr/local/sbin/batpipcore $jd.task  task$jd.log 
/bin/rm -f flat.tmp

if test "$mframe" != "0"
then
mv p*M*.fit /minor/data/pip/pip$jd.m*/.
fi

if test "$sframe" != "0"
then
mv p*S*.fit /pip1/super.tmp/pip$jd.s*/.
fi

#mv $jd.task* task$jd.log /pip1/task.doc/.
##########################################################################################
cp $jd.task* task$jd.log /pip1/task.doc/.
pip $jd.taskf !
chflatname $jd
pip $jd.task !
rm *.tmp
rm *.log
