#!/bin/csh 
#this script generates mission tapes for SC 2 by default
echo $0 $*
prtenv
if ( ! $?SC ) setenv	SC	2
if ( ! ${?FILL_SUM} ) setenv	FILL_SUM	/plas7/d2/vgr/data/sum.gsfc.fillin/v${SC}/sum
if ( ! ${?FILL_EDR} ) setenv	FILL_EDR	"/plas7/d2/vgr/edr.gsfc.fill/v${SC}/edr /plasma/d2/vgr/edr.fill"
prtenv
@ yr=$1
@ sub_yr=$2
setenv	DIR00	${DIR}
if ( ! ${?SC} ) then
	setenv	SC	2
endif
if ( ! $?DIR ) then
	setenv	DIR	${vgrDATA}/v${SC}/vgr
endif
		echo " "
		echo START yr ${yr} sub_yr ${sub_yr}
		set	xx=( )
		setenv	DIR	${DIR00}
		setenv	TIM	${yr}
		setenv	SUFF ${TIM}.${sub_yr}
		setenv	DIRO	${DIR}
		set	ee="s/yyyy/19${yr}/g"
		switch ( $sub_yr )
			case 1:
				set dd1=001
				@ dd2=120
				breaksw
			case 2:
				@ dd1=121
				@ dd2=240
				breaksw
			case 3:
				@	t=${yr}
				@	t%=4
				if ( ${yr} == 77 ) then
					@ dd1=230
					@ dd2=365
				else if ( ${t} == 0 ) then
					@ dd1=240
					@ dd2=366
				else
					@ dd1=240
					@ dd2=365
				endif
				breaksw
			default:
				echo sub_yr ${sub_yr} an error
				breaksw
		endsw
		set ee="${ee};s/dd1/${dd1}/;s/dd2/${dd2}/"
		echo ee ${ee}
		rm -f time.run.* >& /dev/null
		sed -e "${ee}" TIME.vgr.sample.fine > time.run
		rm -fr	${DIRO}/{q,f,v,e,s,S,l,m,e1,e2,F,E}
		setenv	VGR
		#	first EDRs quick look & finials if after 89, else use old VGRs
		echo 	first EDRs quick look \& finials if after 89, else use old VGRs	"   " ${yr}
		if ( ${yr} < 90 ) then
			foreach mode ( e1 e2 l m )
				setenv	DIR	${DIRO}/${mode}
				mkdir	-p	${DIR}
				#cp		-p	${DATA}/vgr.gsfc/${mode}${SC}/v${SC}_vgr_19${yr}.* ${DIR}
				foreach file (   ${DATA}/vgr.gsfc/${mode}${SC}/v${SC}_vgr_19${yr}.* )
					set d1=`expr ${file} : '.*v._.*_....\.\(...\)\..._....\....\...'`
					set d2=`expr ${file} : '.*v._.*_.*_....\.\(...\)'`
					echo d1 $d1 d2 $d2 dd1 $dd1 dd2 $dd2 file $file
					set echo
					if ( ${d1} <= ${dd2} && ${d2} >= ${dd1} ) then
						cp		-p	${file}	${DIR}
					endif
					unset echo
				end
				echo yr ${yr} sub_yr ${sub_yr} mode ${mode}
				ls -l  ${DIR}/v${SC}_vgr*
				set xx=( ${xx} ${DIR}/v${SC}_vgr* )
			end
		else
			#mkdir	-p	${DIRO}/v
			foreach	qf	( q f )
				set echo
				setenv	QF	${qf}
				setenv	DIR	${DIRO}/${qf}
				mkdir -p	${DIR}/vgr/l  ${DIR}/vgr/m  ${DIR}/vgr/e1  ${DIR}/vgr/e2
				unset echo
				echo vgrDATA $vgrDATA DIR $DIR
				RUN	run	edr
			end
			unsetenv QF
			set echo
			echo yr ${yr} sub_yr ${sub_yr}
			echo  ls -l  ${DIRO}/ qf /vgr/v${SC}_vgr 
			#echo  ls -l  ${DIRO}/[qf]/vgr/*/v${SC}_vgr*
			#ls -l  ${DIRO}/[qf]/vgr/*/v${SC}_vgr*
			set xxx=(`ls ${DIRO}/[qf]/vgr/*/v${SC}_vgr*` )
			echo ${#xxx} xxx ${xxx}
			unset echo
			if ( ${#xxx} > 0 ) then
				set xx=( ${xx} ${xxx} )
			else
				echo NO DATA yr ${yr} sub_yr ${sub_yr} first
			endif
			#set xx=( ${xx} ${DIRO}/[qf]/vgr/v${SC}_vgr* )
		endif
		if ( ${SC} == 1 ) then
			echo for now no fill in for VOYAGER ${SC}
			goto TRY6
		else if ( ${yr} > 90 ) then
			echo for now no fill after 1990
			goto TRY6
		endif
		#	second	SUMMARYs from GSFC pre & post Neptune
		echo 	second	SUMMARYs from GSFC pre \& post Neptune	"   " ${yr}"   " ${sub_yr}
		setenv	DIR	${DIRO}/s
		mkdir -p	${DIR}/vgr
		switch ( ${yr} )
			case 89
				switch ( ${sub_yr} )
					case 2:
						setenv ft32 "cpnto1sum sum_pre_nep |"
						RUN run sum
						set echo
						echo DIR $DIR SC $SC
						set xx=( ${xx} ${DIR}/vgr/*/v${SC}_vgr* )
						echo ${xx}
						unset echo
						unsetenv	ft32
						breaksw
					case 3:
						setenv	DIR	${DIRO}/S
						mkdir -p	${DIR}/vgr
						setenv ft32 "cpnto1sum sum_post_nep_89 |"
						RUN run sum
						unsetenv	ft32
						set xx=( ${xx} ${DIR}/vgr/*/v${SC}_vgr* )
						breaksw
				endsw
				breaksw
			case 90
				setenv ft32 "cpnto1sum sum_post_nep_90 |"
				RUN run sum
				set echo
				echo yr ${yr} sub_yr ${sub_yr}
				set xxx=(` ls ${DIR}/vgr/*/v${SC}_vgr*` )
				echo ${#xxx} xxx $xxx
				unset echo
				if ( ${#xxx} > 0 ) then
					set xx=( ${xx} ${xxx} )
				else
					echo no sum_post_nep_90 yr ${yr} sub_yr ${sub_yr}
				endif
				#set xx=( ${xx} ${DIR}/vgr/v${SC}_vgr* )
				unsetenv	ft32
				breaksw
		endsw
		echo $#xx xx $xx
		#	third	EDRs from GSFC
		echo 	third	EDRs from GSFC	"   " ${yr}"   " ${sub_yr}
		setenv	DIR	${DIRO}/e
		mkdir -p	${DIR}
		switch ( ${yr} )
			case 90
				setenv ft32 "cpnedrgsfcto1  |"
				RUN run edr
				echo yr ${yr} sub_yr ${sub_yr} third
				set xxx=( `ls ${DIR}/vgr/v${SC}_vgr*` )
				echo ${#xxx} xxx ${xxx}
				if ( ${#xxx} > 0 ) then
					set xx=( ${xx} ${xxx} )
				else
					echo no cpnedrgsfcto1 yr ${yr} sub_yr ${sub_yr}
				endif
				#set xx=( ${xx} ${DIR}/vgr/v${SC}_vgr* )
				unsetenv	ft32
				breaksw
		endsw
		# fourth, SUM fillin from GSFC
		echo fourth, SUM fillin from GSFC yr ${yr} sub_yr ${sub_yr}
		set gg_fill=( `GETDATA ${yr}${dd1} ${yr}${dd2} ${SC} sum.print ${FILL_SUM} |& cat` )
		#set gg_fill=( `setenv vgrDATA /plas7/d2/vgr/data/sum.gsfc.fillin ;GETDATA ${yr}${dd1} ${yr}${dd2} ${SC} sum.print ${FILL_SUM} |& cat` )
		echo  ${#gg_fill} gg_fill ${gg_fill}
		unset echo
		if ( ${#gg_fill} > 1 ) then
			# we have some fill data, use it
			setenv	DIR	${DIR0}/F
			mkdir -p ${DIR}/vgr/l
			mkdir -p ${DIR}/vgr/m
			mkdir -p ${DIR}/vgr/e1
			mkdir -p ${DIR}/vgr/e2
			setenv ft25 ${DIR}/vgr/l/v${SC}_vgr_l_${yr}.${sub_yr}
			setenv ft26 ${DIR}/vgr/m/v${SC}_vgr_m_${yr}.${sub_yr}
			setenv ft27 ${DIR}/vgr/e1/v${SC}_vgr_e1_${yr}.${sub_yr}
			setenv ft28 ${DIR}/vgr/e2/v${SC}_vgr_e2_${yr}.${sub_yr}
			setenv ft32 "GETDATA ${yr}${dd1} ${yr}${dd2} ${SC} sum ${FILL_SUM} |"
			#setenv ft32 "vgrDATA=/plas7/d2/vgr/data/sum.gsfc.fillin;export vgrDATA ;GETDATA ${yr}${dd1} ${yr}${dd2} ${SC} sum ${FILL_SUM} |"
			RUN run sum
			rename $ft25 $ft26 $ft27 $ft28
			unsetenv ft25  ft26  ft27  ft28
			unsetenv ft32
			set	xxx=( `ls ${DIR}/vgr/*/v${SC}_vgr_* ` )
			if ( ${#xxx} > 0 ) then
				set xx=( ${xx} ${xxx} )
				echo fill data for SUM xx ${xx}
			else
				echo " "
				echo NO fill data generated for SUM xx ${xx}
				echo " "
			endif
		else
			echo NO try for fill data for SUM xx ${xx}
		endif
		# fifth, EDR  fillin from GSFC
		echo fifth, EDR  fillin from GSFC yr ${yr} sub_yr ${sub_yr}
		set gg_fill=( `GETDATA ${yr}${dd1} ${yr}${dd2} ${SC} edr.print ${FILL_EDR} |& cat` )
		#set gg_fill=( `setenv vgrDATA /plas7/d2/vgr/data/edr.gsfc.fillin ;GETDATA ${yr}${dd1} ${yr}${dd2} ${SC} edr.print ${FILL_EDR} |& cat` )
		echo  ${#gg_fill} gg_fill ${gg_fill}
		unset echo
		if ( ${#gg_fill} > 1 ) then
			# we have some fill data, use it
			setenv	DIR	${DIR0}/E
			mkdir -p ${DIR}/vgr/l
			mkdir -p ${DIR}/vgr/m
			mkdir -p ${DIR}/vgr/e1
			mkdir -p ${DIR}/vgr/e2
			setenv ft25 ${DIR}/vgr/l/v${SC}_vgr_l_${yr}.${sub_yr}
			setenv ft26 ${DIR}/vgr/m/v${SC}_vgr_m_${yr}.${sub_yr}
			setenv ft27 ${DIR}/vgr/e1/v${SC}_vgr_e1_${yr}.${sub_yr}
			setenv ft28 ${DIR}/vgr/e2/v${SC}_vgr_e2_${yr}.${sub_yr}
			setenv ft32 "GETDATA ${yr}${dd1} ${yr}${dd2} ${SC} edr ${FILL_EDR} |"
			#setenv ft32 "vgrDATA=/plas7/d2/vgr/edr.gsfc.fill/v${SC}/edr;export vgrDATA ;GETDATA ${yr}${dd1} ${yr}${dd2} ${SC} edr ${FILL_EDR} |"
			RUN run edr
			rename $ft25 $ft26 $ft27 $ft28
			unsetenv ft25  ft26  ft27  ft28
			unsetenv ft32
			set	xxx=( `ls ${DIR}/vgr/*/v${SC}_vgr_* ` )
			if ( ${#xxx} > 0 ) then
				set xx=( ${xx} ${xxx} )
				echo fill data for EDR xx ${xx}
			else
				echo " "
				echo NO fill data generated for EDR xx ${xx}
				echo " "
			endif
		else
			echo NO try for fill data for EDR xx ${xx}
		endif
		echo end TRY 5
	TRY6:	#	end of all trys, not only vgr for Voyager 1
		setenv	DIR	${DIRO}
		if ( ! -f ${DIR}/v${SC}/vgr ) then
			mkdir -p ${DIR}/v${SC}/vgr
		endif
		#foreach ff ( ${DIR}/{l,m,e1,e2}/v${SC}_vgr* ${DIR}/[fq]/v${SC}.VGR* ${DIR}/s/v${SC}.VGR*  ${DIR}/e/v${SC}.VGR* )
		echo do merge
		echo " "
		echo xx ${#xx}
		@ i=1
		foreach xxx ( ${xx} )
			echo ${i} ${xxx}
			@ i++
		end
		echo " "
		set echo
		@ i=10
		setenv	ft10	${DIR}/v${SC}/vgr/v${SC}_vgr_19${yr}.${sub_yr}
		foreach ff ( ${xx} )
			@ i++
			if ( `expr ${ff} : '.*\.gz$' ` > 0 ) then
				setenv	ft${i}	"gunzip      < ${ff} |"
			else if ( `expr ${ff} : '.*\.Z$'  ` > 0 ) then
				setenv	ft${i}	"uncompress  < ${ff} |"
			else
				setenv	ft${i}	${ff}
			endif
		end
		@ i++
		unsetenv	ft${i}
		@ i++
		unsetenv	ft${i}
		setenv	ft01	time.merge.vgr
		sed -e "${ee}" time.merge.sample.fine > ${ft01} 
		cat ${ft01}
		unset echo
		prtft
		echo $vgrLIB/mergexxx
		time  $vgrLIB/mergexxx
		unsetenv	ft01
		while ( ${i} > 10 ) 
			unsetenv	ft${i}
			@ i--
		end
		du ${DIR}
		set echo
		ls -lR ${DIR}
		if ( $?RSH ) then
			rsh -n $RSH " gzip -v -9 ${ft10} ;	 rename ${ft10}.gz " &
		else
			gzip -v -9 ${ft10} ;	 rename ${ft10}.gz
		endif
		unsetenv	ft10
		unset echo
	echo END yr ${yr}