#! /bin/csh
#---------------------------------------
# A minor change by YHJ: moving the images with S ID (images for supernova
# searching) to /pip2 is no longer needed, for they should be moved to the
# dedicate disk for minor planet data.
# Feb. 4'97
#---------------------------------------
# The sizes of standards and field images were considered seperately when
#  calculate disk space.
# 
# Change by Zhou Xu
#                      1995.11.2
#---------------------------------------
# All the lines contain raw2 are commented since it is used to write CD
#
# ZZY     1996.12.10
############################################################################################
echo ""
echo "+---------------------------------------------------------------------------+"
echo "|                           POSTERIOR BATPIP V3.4                           |"
echo "|                                                                           |"
echo "|                          This is a shell program                          |"
echo "|                       for post-batpip in batch mode                       |"
echo "|                  =======================================                  |"
echo "|                                    Z.-Y. Zheng, 04/01/94                  |"
echo "+---------------------------------------------------------------------------+"
echo "                60228 "
#####################################################################################
# some small changes are made from V1.0 to V2.0 by Jin Zhu to permit the seperation
# of minor planet data from others. zj950930.
############################################################################################
# check user

set user = `whoami`
if ($user != "ccdev") then
	echo ""
	echo "	You must be ccdev for executing this program\!"
	echo ""
	exit(1)
endif

############################################################################################
# check directory and determine JD number

set dir = `pwd | cut -c1-14`
set jd = `pwd | cut -c15-18`
if ($dir != "/pip1/data/pip") then
	echo ""
	echo "       You must be in /pip1/data/pipxxxx for executing this program\!"
	echo ""
	exit(2)
endif

###########################################################################################
# clear temporary file

\rm -f space.list flat.list pole.list total.list totalc.list bias.list

###########################################################################################
# check available disk space

df | grep -v Filesystem > space.list
set raw1space = (`grep raw1 space.list`)
set raw2space = (`grep raw2 space.list`)
set pip2space = (`grep pip2 space.list`)
set polespace = (`df /raw2 | grep raw2`)
@ raw1available = $raw1space[4] / 8400
@ raw2available = $raw2space[4] / 8400
@ pip2available = $pip2space[4] / 8400
@ poleavailable = $polespace[4] / 8400

(ls F$jd?.fit > flat.list) >& /dev/null
set fframewc = (`wc flat.list`)
@ fframe = $fframewc[1] * 2

(ls p???${jd}Polei???.fit > pole.list) >& /dev/null
set pframewc = (`wc pole.list`)
@ pframe = $pframewc[1] * 2

(ls p???${jd}[NTU]*.fit > total.list) >& /dev/null
set tframewc = (`wc total.list`)
(ls p???${jd}C*.fit > totalc.list) >& /dev/null
set cframewc = (`wc totalc.list`)
###########################################################
# The numbers of bias and flat images are added to tframe
#			ZZY. 06/12/96
###########################################################
(ls Z*.fit > bias.list) >& /dev/null
set zframewc = (`wc bias.list`)
@ zframe = $zframewc[1] * 2

@ tframe = ( $tframewc[1] + $fframewc[1] + $zframewc[1] + ( $cframewc[1] / 45 ) ) * 2
echo $tframe

\rm -f space.list flat.list pole.list total.list totalc.list bias.list

if ($pframe >= $poleavailable) then
	echo ""
	echo "	No enough available space in /polar for North Pole frames"
	echo "	You must su superuser to move them manually and execute this program again"
	echo ""
	exit(5)
endif

if ($fframe >= $raw1available || $fframe >= $raw2available || $tframe >= $pip2available) then
	echo  ""
	echo "	No enough disk space\! You may need to write data into CD first"
	echo ""
#	exit(6)
endif
############################################################################################
# display start job time

echo ""
echo ""
echo -n "START job time : "
date

if (! -d /pip2/data/pip$jd) then
	echo "mkdir /pip2/data/pip$jd"
	mkdir /pip2/data/pip$jd
endif

########################################################################################
# write document

/usr/local/sbin/writedoc $jd
#########################################################################################
# mv the North Pole frame into partitial directory

if ($pframe != 0) then

	echo "\mv -f p???${jd}Polei???.fit /polar/pip"
	\mv -f p???${jd}Polei???.fit /polar/pip

endif

########################################################################################
# copy flat-field frames
# select only the survey data to copy---modified by ChR on 12/29/97

foreach color (`\ls p???${jd}[NTCU]*.fit | cut -c 13-13 | sort | uniq`)
###select the colors

	if ( -e F${jd}${color}.fit ) then

		set flat_frame="F${jd}${color}.fit"

		echo "\\cp  $flat_frame /raw1/data/raw$jd"
		\cp  $flat_frame /raw1/data/raw$jd

		echo "\\cp  $flat_frame /raw2/data/raw$jd"
		\cp  $flat_frame /raw2/data/raw$jd

		echo "\\mv -f $flat_frame /pip2/data/pip$jd"
		\mv -f $flat_frame /pip2/data/pip$jd

	endif

end ##### end of foreach
###### The above "foreach" loop is added by ChR on 12/29/97 to prevent 
####### copying useless unrelevant flat_frames
		

#if ($fframe != 0) then

#	echo "\\cp  F$jd?.fit /raw1/data/raw$jd"
#	\cp  F$jd?.fit /raw1/data/raw$jd

#	echo "\\cp  F$jd?.fit /raw2/data/raw$jd"
#	\cp  F$jd?.fit /raw2/data/raw$jd

#	echo "\mv -f F$jd?.fit /pip2/data/pip$jd"
#	\mv -f F$jd?.fit /pip2/data/pip$jd

#endif

##########################################################################################
# copy post-pipline data to /pip2

if ($tframe != 0) then

	echo "\mv -f p???${jd}[NTCU]*.fit /pip2/data/pip$jd"
	\mv -f p???${jd}[NTCU]*.fit /pip2/data/pip$jd >& /dev/null
endif

# changed by ZHOU Xu for move bias frames

if ($zframe != 0) then

	echo "\mv -f Zp??${jd}.fit /pip2/data/pip$jd"
	\mv -f Zp??${jd}.fit /pip2/data/pip$jd >& /dev/null

endif


#########################################################################################
# mv log files into /pip2

echo "\mv -f task$jd.log file into /pip2"
\mv -f task$jd.log /pip2/data/pip$jd

#########################################################################################
# calculate used disk space and modify document

echo "modify raw$jd.doc"
set used = (`du /raw1/data/raw$jd`)
@ space = ($used[1] + 512) / 1024
sed s+" ^^^"+" $space"+g raw$jd.doc > raw$jd.doc.true
\mv -f raw$jd.doc.true raw$jd.doc
echo "\\cp raw$jd.doc /raw1/data/raw$jd"
\cp raw$jd.doc /raw1/data/raw$jd
echo "\mv -f raw$jd.doc /raw2/data/raw$jd"
\mv -f raw$jd.doc /raw2/data/raw$jd

echo "modify pip$jd.doc"
set used = (`du /pip2/data/pip$jd`)
@ space = ($used[1] + 512) / 1024
sed s+" ^^^"+" $space"+g pip$jd.doc > pip$jd.doc.true
\mv -f pip$jd.doc.true pip$jd.doc
echo "\mv -f pip$jd.doc /pip2/data/pip$jd"
\mv -f pip$jd.doc /pip2/data/pip$jd

########################################################################################
# make dir file

echo "make raw$jd.dir"
cd /raw1/data/raw$jd
/usr/local/sbin/dirccd > raw$jd.dir
echo "\\cp raw$jd.dir /raw2/data/raw$jd"
\cp raw$jd.dir /raw2/data/raw$jd

echo "make pip$jd.dir"
cd /pip2/data/pip$jd
/usr/local/sbin/dirccd > pip$jd.dir

###########################################################################
# add these lines for Licai to do SuperMogo  --- Zheng Zhongyuan, Feb.9,96
echo "add pip$jd.dir to /pip2/document/alltarget.dir"
echo "::::::::::::::" >> /pip2/document/alltarget.dir
echo "pip$jd/pip$jd.dir" >> /pip2/document/alltarget.dir
echo "::::::::::::::" >> /pip2/document/alltarget.dir
cat pip$jd.dir >> /pip2/document/alltarget.dir
###########################################################################

########################################################################################
# check if there are rest file

cd /pip1/data
set rest = (`ls pip$jd | wc`)












if ($rest[1] != 0) then
	echo ""
	echo "	Error \! There are rest files in /pip1/data/pip$jd"
	echo ""
#	exit(7)
endif

########################################################################################
# display end message

echo " "
echo " "
echo -n "END job time : "
date

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













































































































































































































































