
	PROGRAM quatvstime
	implicit none

	real*4		frame      (318,64)
	real*8		cra
	real*8		cdec

	integer*4	mode			/4/
	integer*4	ic			/2/
	integer*4	ifm			/0/
	integer*4	id			/318/
	integer*4	jd			/64/
	integer*8	k
c
c	AARON'S DECLARATIONS
c
	character infile*47, listfile*23
	character outfile*26
	integer*8 time
	integer*4 date, hour, min, sec, maxk
	integer*4 i, j, meascnt
	real*4 ra, dec
	real*8 phi, rlat
	real*8 qcam(3,4), qc(4), qsc(4), qandy(4), qdum(4)

	!! updated test quaternions (e1=-86.10, e2=59.05, e3=-6.75)
	qcam(2,1) = -0.268131D0
	qcam(2,2) = 0.0170381D0
	qcam(2,3) = 0.962053D0
	qcam(2,4) = 0.0476403D0
	!! refined camera 1 quaternions [-28.5,70.4,-21.5]
	qcam(1,1) = -0.237689D0
	qcam(1,2) = 0.467706D0
	qcam(1,3) = 0.848236D0
	qcam(1,4) = 0.0724612D0
	!! test quaternion for camera 3 [-143.3, 63.43, -0.42]
	qcam(3,1) = 0.203781D0
	qcam(3,2) = 0.437286D0
	qcam(3,3) = -0.869462D0
	qcam(3,4) = -0.106259D0

	!!quaternion to rotate Andy's coordinates to Don's
	qandy(1)=0.5D0
	qandy(2)=0.5D0
	qandy(3)=0.5D0
	qandy(4)=0.5D0

	print*, 'Fortran In ...'

	write (outfile,'(A26)') 'quat_vs_time_0529_0531.txt'
	open (16,file = outfile, form = 'formatted')
	do i=1,3

	  if(i.eq.1)  date=0529
	  if(i.eq.2)  date=0530
	  if(i.eq.3)  date=0531
	  if(date.eq.0529)  maxk=14077
	  if(date.eq.0530)  maxk=17204
	  if(date.eq.0531)  maxk=11320
	  write(listfile,'(A15,I4.4,A4)') 'DATA_SMEI/cam2_',date,'.txt'

	  open(12,file=listfile,status='old')
	  do k=1,maxk

	    	  read(12,12)hour,min,sec
12	    	  format(15x,I2.2,I2.2,I2.2)

CC		  time=(hour*60*60)+(min*60)+sec

	      	  if(date.eq.0529)  write(infile,'(A34,I2.2,I2.2,I2.2,A7)') 'DATA_SMEI/cam20529/c2frm_2003_149_',hour,min,sec,'crx.bin'
		  if(date.eq.0530)  write(infile,'(A34,I2.2,I2.2,I2.2,A7)') 'DATA_SMEI/cam20530/c2frm_2003_150_',hour,min,sec,'crx.bin'
		  if(date.eq.0531)  write(infile,'(A34,I2.2,I2.2,I2.2,A7)') 'DATA_SMEI/cam20531/c2frm_2003_151_',hour,min,sec,'crx.bin'
	      	  if(mod(k,50).eq.0)  print 1001,k,infile
1001	      	  format(i7,': ',a47)

		  call read_framecrx_mode(infile,mode,ic,id,jd,frame,qdum,meascnt)

		  !! input quaternion is of form ai+bj+ck+d, not d+ai+bj+cK -> swap with new quaternion
		  qsc(1)=qdum(4)
		  qsc(2)=qdum(1)
		  qsc(3)=qdum(2)
		  qsc(4)=qdum(3)

CC		  find ra/dec of center of FOV for a frame
CC		  find ra of center of the first frame; use to calculate ra bounds in final image
CC		  car/cdec calculated from thetax=thetay=0
	              cra=0.D0
		      cdec=90.D0
	              call rotateq(qsc,cra,cdec) !!rotate to standard system

	  	  if(date.eq.0529) write(16,16) '"05/29/03',hour,':',min,':',sec,'"',cdec,cra,qsc(1),qsc(2),qsc(3),qsc(4)
		  if(date.eq.0530) write(16,16) '"05/30/03',hour,':',min,':',sec,'"',cdec,cra,qsc(1),qsc(2),qsc(3),qsc(4)
		  if(date.eq.0531) write(16,16) '"05/31/03',hour,':',min,':',sec,'"',cdec,cra,qsc(1),qsc(2),qsc(3),qsc(4)
16		  format(A9,1x,I2.2,A1,I2.2,A1,I2.2,A1,1x,F15.10,1x,F15.10,1x,F15.10,1x,F15.10,1x,F15.10,1x,F15.10)

	  enddo  !!end k frame loop
	  close(12)

	enddo  !!end i date loop

	close(16)

	print*, 'Finished!!!'
	END
