#!/bin/csh
date
if ( ! $?SC ) then
	setenv	SC	2
endif
if ( ! $?DIR ) then
	setenv DIR $vgrDATA/v${SC}
endif
if ( ! $?ITER ) then
	setenv ITER	4
endif
if ( $?DELETE_HA ) then
	if ( x${DELETE_HA} == x ) then
		@ delete_ha=$ITER - 1
		setenv DELETE_HA $delete_ha
	endif
endif
if ( ! $?TYPE ) then
	setenv	TYPE vgr
	echo setting type to default vgr
endif
set type=${TYPE}
echo TYPE ${TYPE} type ${type}
unsetenv ECHO
setenv KEY
setenv HA
setenv COMPRESS 9
@ y1=77
if ( ${SC} == 1 ) then
	@ y2=80
else
	if ( ${type} == 'pls' || ${type} == 'sum' ) then
		@ y2=90
	else
		@ y2=95
	endif
endif
if ( x$1 != x ) then
	@ y1=$1
	if ( x$2 != x ) then
		@ y2=$2
	endif
endif

@ y=$y1
@ i89=0
while ( $y <= $y2 )
	setenv TIM ${y}
	unsetenv SUFF
	unsetenv ft01
	unsetenv ft02
	unsetenv ft03
	unsetenv ft04
	unsetenv ft08
	unsetenv ft09
	unsetenv ft13
	unsetenv ft16
	unsetenv ft17
	unsetenv ft18
	unsetenv ft32
	#unsetenv ft33
	if ( ${y} == 89 ) then
		@ i89++
		if ( ${i89} == 1 ) then
			set ee="s/1970/1989/g;s/365/240/"
			setenv	TIM	${TIM}.a
			@ y--
		else
			set ee="s/1970/1989/g;s/  1/241/"
			setenv	TIM	${TIM}.b
		endif
	else
		@	ly=${y}
		@	ly %= 4
		if ( ${ly} == 0 ) then
			set	ee="s/1970/19${y}/g;s/365/366/"
		else
			set	ee="s/1970/19${y}/g"
		endif
	endif
	sed -e "${ee}" time.PRD.sample > time.PRD
	echo time.PRD
	if ( $?TIME_PRD ) then
		set tttt=`cat time.PRD`
		set yy1=${tttt[1]}
		set dd1=${tttt[2]}
		set h1=${tttt[3]}
		set m1=${tttt[4]}
		set yy2=${tttt[5]}
		set dd2=${tttt[6]}
		set h2=${tttt[7]}
		set m2=${tttt[8]}
		set dd1=`expr 000${dd1} : '.*\(...\)'`
		set dd2=`expr 000${dd2} : '.*\(...\)'`
		set h1=`expr 000${h1} :  '.*\(..\)'`
		set h2=`expr 000${h2} :  '.*\(..\)'`
		set m1=`expr 000${m1} :  '.*\(..\)'`
		set m2=`expr 000${m2} :  '.*\(..\)'`
		echo ' '${yy1}' '${dd1}' '${h1}' '${m1}' '${yy1}' '${dd1}' '${h1}' '${m1}	> time.PRD
		cat ${TIME_PRD} >> time.PRD
		echo ' '${yy2}' '${dd2}' '${h2}' '${m2}' '${yy2}' '${dd2}' '${h2}' '${m2} >> time.PRD
	endif
	ht  time.PRD
	echo RUN PRD ${type} time.PRD SC $SC  y $y $y1 $y2
	RUN PRD ${type} time.PRD
	echo POST PROCESSING $TIM
	if ( ${i89} == 2 ) then
		@ i89++
		if ( $?RSH ) then
			rsh ${RSH} "gzcat -9 ${DIR}/ha/HA_PRD_v${sc}.89.${ITER}.a.gz  ${DIR}/ha/HA_PRD_v${sc}.89.${ITER}.b.gz >  ${DIR}/ha/HA_PRD_v${sc}.89.${ITER}.gz"
			rsh ${RSH} "gzcat -9 ${DIR}/key/KEY_PRD_v${sc}.89.${ITER}.a.gz  ${DIR}/key/KEY_PRD_v${sc}.89.${ITER}.b.gz >  ${DIR}/key/KEY_PRD_v${sc}.89.${ITER}.gz"
		else
			gzcat -9 ${DIR}/ha/HA_PRD_v${sc}.89.${ITER}.a.gz  ${DIR}/ha/HA_PRD_v${sc}.89.${ITER}.b.gz >  ${DIR}/ha/HA_PRD_v${sc}.89.${ITER}.gz
			gzcat -9  ${DIR}/key/KEY_PRD_v${sc}.89.${ITER}.a.gz  ${DIR}/key/KEY_PRD_v${sc}.89.${ITER}.b.gz >  ${DIR}/key/KEY_PRD_v${sc}.89.${ITER}.gz
		endif
	endif
	@ y++
end