C+
C NAME:
C	A_mainf_centroid
C PURPOSE:
C	average over transits of a star to get an average of that star
C CATEGORY:
C	SMEI Data processing
C INPUTS:
C
C OUTPUTS:
C
C CALLS:
C       none
C PROCEDURE:
C
C MODIFICATION HISTORY:
C	April, 2003 A. Smith (UCSD/CASS)
C-
	program A_mainf_centroid
	implicit none

	! Dimensions for node, nodeid, hits above must be at least 2*(4**(level+1))
	! For LEVEL = 12, KMAX = 134217728
	! For LEVEL =  8, KMAX =    524288

	integer*2	  TOP_LEVEL
	parameter	( TOP_LEVEL = 12 )

	integer*8	  TOP_KMAX
	parameter	( TOP_KMAX = 2*(4**(TOP_LEVEL+1)) )

	real*4		node  ( TOP_KMAX )	!/TOP_KMAX*0.0/
	integer*8	nodeid( TOP_KMAX )	!/TOP_KMAX*0  /
	integer*2	hits  ( TOP_KMAX )	!/TOP_KMAX*0  /

	integer*2	level			/TOP_LEVEL/
	integer*8	kmax			/TOP_KMAX/

	real*4		frame      (1272,256)
	real*4		framessff  (1272,256)
	real*4		framelsff  (1272,256)
	real*4		framelsffu (1272,256)
	real*4  	ped			/0./
	real*4		dark			/0./
	real*4		avg        (1272,256)    /0./
	real*4		hroom			/49400./		!'headroom'=new onboard value of 65k ADUs
	real*4		adu2e
	real*8		affine(6)		/6*0.0D0/		! Affine parameters zero for now
	real*8		cra
	real*8		cdec
	real*8		alfa
	real*8		beta
	real*8		gamma
	real*8		euler(3)
	real*8		camlat
	real*8		ra0
	real*8		arg
	real*8		arg1
	real*8		dusk

	integer*4	mode			/1/			!onboard binning mode
	integer*4	ic			/2/			!Camera #
	integer*4	ifm			/0/			!ROI mask (0=no,1=yes)
	integer*4	id			/1272/			!1280 for TMO
	integer*4	jd			/256/			!600 for TMO
	integer*4	icrcnt			/0/
	integer*4	neighbors		/0/
	integer*4	iframe
	integer*4	iframemask (1272,256)	/325632*1/
	integer*4	ihit(1272,256)
	integer*4	ihist(250)
	integer*4	ifmin                   /193/
	integer*4	ifmax                   /469/
	integer*4	i
	integer*4	j
	integer*4	night			/99/			!night=99 for SMEI data
	integer*4	image(200,200)		/40000*0/
	integer*4	image100(100,100)	/10000*0/

	character	outfile*17,listfile*23,framename*28,infile*47
c
c	AARON'S DECLARATIONS
c
	integer*4 idarkcr, ii, jj, icra, meascnt, kstart
	integer*8 k, kk, frametest, ss, xmax, xmin
	real*8 rq, dr, thetax, thetaxsq, thetay, cx, cy, cz, crsq
	real*8 rccd, x0, y0, r0, tempx
	real*8 response, arg, arg1, phi, rlat
	real*8 x(4), y(4), ra(4), dec(4), tan1, tan2
	real*8 f0, f, xf, yf, zf, rfsq, rf, raf, decf
	real*8 qcam(3,4), qc(4), qsc(4), qandy(4), qdum(4), qfinal(4)

	  write(listfile,'(A23)') 'DATA_SMEI/list_0227.txt'
c	  write(listfile,'(A23)') 'DATA_SMEI/list_0228.txt'
C	  write(listfile,'(A23)') 'DATA_SMEI/list_0301.txt'
C	  write(listfile,'(A23)') 'DATA_SMEI/list_0034.txt'
C	  write(listfile,'(A23)') 'DATA_SMEI/list_0035.txt'

	!!quaternion for cameras from SMEIPoint v1.0
	qcam(1,1) = -0.24387742D0
	qcam(1,2) = 0.46807739D0
	qcam(1,3) = 0.84666176D0
	qcam(1,4) = 0.067758569D0
c	qcam(2,1) = -0.27454405D0
c	qcam(2,2) = 0.017651734D0
c	qcam(2,3) = 0.96032546D0
c	qcam(2,4) = 0.045705368D0
	qcam(3,1) = -0.21298450D0
	qcam(3,2) = -0.43870382D0
	qcam(3,3) = 0.86674816D0
	qcam(3,4) = 0.10451884D0
	!! (3) updated test quaternions (e1=-86.023, e2=59.35, e3=-6.00)
c	qcam(2,1) = -0.265524D0
c	qcam(2,2) = 0.0195955D0
c	qcam(2,3) = 0.9630200
c	qcam(2,4) = 0.0412865D0
	!! updated test quaternions (e1=-86.10, e2=59.36, e3=-6.75)
c	qcam(2,1) = -0.265531D0
c	qcam(2,2) = 0.0172160D0
c	qcam(2,3) = 0.962764D0
c	qcam(2,4) = 0.0477712D0
	!! updated test quaternions (e1=-86.10, e2=59.00, e3=-6.75)
c	qcam(2,1) = -0.268551D0
c	qcam(2,2) = 0.0170094D0
c	qcam(2,3) = 0.961937D0
c	qcam(2,4) = 0.0476192D0
	!! updated test quaternions (e1=-86.10, e2=59.05, e3=-6.80)
c	qcam(2,1) = -0.268139D0
c	qcam(2,2) = 0.0169211D0
c	qcam(2,3) = 0.962032D0
c	qcam(2,4) = 0.0480602D0
	!! 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

	!!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
	data ihist /250*0/
	data ihit /81408*0/

	print*, 'Fortran IN ...'
	adu2e=4.5*65536./hroom
c	call set_flatfields(ic,framessff,framelsff,framelsffu)	!note dimensions should be checked before ff is done...
	call set_cr_backgnd(mode,ic,ifm,id,jd,avg)

	do ss=2,2
	 
	 do k=1,kmax
	   node(k)=0.0
	   nodeid(k)=0
	   hits(k)=0
	 enddo

	 if(ss.eq.1) then
	   xmin=22
	   xmax=1260
	   tan1=0.008727D0  !!tan(0.5)
	   tan2=-0.008727D0  !!tan(-0.5)
	   cra=70.2662D0
	   cdec=0.07366D0
	   kstart=314
	   write (outfile,'(A17)') 'rect_00_trans.grd'
	 elseif(ss.eq.2) then
	   xmin=800
	   xmax=1260
	   tan1=0.383864D0  !!tan(21)
	   tan2=0.36397D0  !!tan(20)
	   cra=90.7083D0
	   cdec=1.4879D0
	   kstart=314
	   write (outfile,'(A17)') 'rect_00_tran2.grd'
	 elseif(ss.eq.3)then
	   xmin=800
	   xmax=1260
	   tan1=0.383864D0  !!tan(21)
	   tan2=0.36397D0  !!tan(20)
	   cra=90.4381D0
	   cdec=38.60867D0
	   kstart=423
	   write (outfile,'(A17)') 'rect_40_trans.grd'
	 endif

	 open(13,file=listfile,status='old')
	 frametest=14
	 do k=1,1000

 	  read(13,13)framename
13	  format(a28)

	  if( k.eq.kstart) then


	      write(infile,'(A19,A28)') 'DATA_SMEI/20030227/',framename
C	      write(infile,'(A19,A28)') 'DATA_SMEI/20030228/',framename
C	      write(infile,'(A19,A28)') 'DATA_SMEI/old_0301/',framename
C	      write(infile,'(A19,A28)') 'DATA_SMEI/day_0034/',framename
		  print*, infile

		call read_framecrx(infile,mode,ic,id,jd,frame,qdum,meascnt)
		
		if(meascnt.lt.5000)then

		  !! 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)
		  
c		  print*, qdum

		  !! multiply camera quaternion into spacecraft(frame) quaternion and take the inverse
		  !! of camera quaternion because it goes from sky to ccd
		  qc(1)=qcam(ic,1)
		  qc(2)=-qcam(ic,2)
		  qc(3)=-qcam(ic,3)
		  qc(4)=-qcam(ic,4)

		  call qmultiply(qsc,qc,qdum)
		  call qmultiply(qdum,qandy,qfinal)

		  !!take inverse of final quaternion: going from ccd to sky, not the other way around
C		  qfinal(1)=qfinal(1)
		  qfinal(2)=-qfinal(2)
		  qfinal(3)=-qfinal(3)
		  qfinal(4)=-qfinal(4)

c		  print*, qfinal

C		  find ra/dec of center of FOV for a frame
c		      !!find ra of center of the first frame; use to calculate ra bounds in final image
c		      !!car/cdec calculated from thetax=thetay=0
c		      cra=0.D0
c		      cdec=90.D0
c	              call rotateq(qfinal,cra,cdec) !!rotate to standard system
c		      print*, k,' RA/DEC: ',cra,cdec

		  print*, 'Indexing frame...'
		  call indexframe(mode,ic,id,jd,level,TOP_KMAX,node,nodeid,hits,frame,affine,qfinal,cra,cdec,xmin,xmax,tan1,tan2)

	   endif
	 endif
	 enddo

	 print*, "Calling average night"
	 call averagenight(node,nodeid,hits,TOP_KMAX,level,cra,cdec,image,image100)
	
	!!Write imagecnt(sky atlas) to file
	open (10,file = outfile, form = 'formatted')
	write(10,10)
10	format('DSAA'/'200 200'/'0 200'/'0 200'/'0 10000')
	do j=1,200
	  write(10,'(20i6)')(image(i,j),i=1,200)
	enddo
	close(10)

	close(13)
	enddo

99	print*, 'Fortran out!'

	end
