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/

C	real*4		frame      (1272,256)
C	real*4		framessff  (1272,256)
C	real*4		framelsff  (1272,256)
C	real*4		framelsffu (1272,256)
	real*4		frame      (636,128)
	real*4		framessff  (636,128)
	real*4		framelsff  (636,128)
	real*4		framelsffu (636,128)
c	real*4		frame      (318,64)
c	real*4		framessff  (318,64)
c	real*4		framelsff  (318,64)
c	real*4		framelsffu (318,64)
	real*4  	ped			/0./
	real*4		dark			/0./
C	real*4		avg        (1272,256)    /0./
	real*4		avg        (636,128)    /0./
C	real*4		avg        (318,64)    /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			/2/			!onboard binning mode
	integer*4	ic			/3/			!Camera #
	integer*4	ifm			/0/			!ROI mask (0=no,1=yes)
	integer*4	id			/636/			!1280 for TMO
	integer*4	jd			/128/			!600 for TMO
	integer*4	icrcnt			/0/
	integer*4	neighbors		/0/
	integer*4	iframe
C	integer*4	iframemask (1272,256)	/325632*1/
C	integer*4	ihit(1272,256)
	integer*4	iframemask (636,128)	/81408*1/
	integer*4	ihit(636,128)
C	integer*4	iframemask (318,64)	/20352*1/
C	integer*4	ihit(318,64)
	integer*4	ihist(250)
	integer*4	ifmin                   /193/
	integer*4	ifmax                   /469/
	integer*4	i
	integer*4	j
	integer*4	night			/99/
	integer*4	image(200,200)		/40000*0/

	character	outfile*19,listfile*23,framename*28,infile*47
c
c	AARON'S DECLARATIONS
c
	integer*4 idarkcr, ii, jj, icra, meascnt, fcnt, badangle, orbitnum, date, maxk
	integer*8 k, kk, frametest, framemax, starloop, dateloop, orbitloop
	real*8 rq, dr, thetax, thetaxsq, thetay, cx, cy, cz, crsq
	real*8 rccd, x0, y0, r0, tempx, avgthetarot, thetarot
	real*8 response, arg, arg1, phi, rlat, xra(2), ydec(2)
	real*8 x(4), y(4), ra(4), dec(4), avgthetax
	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)


	!! updated test quaternions [86.10,59.05,-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
	!! Quaternion from Andy's Euler angles for camera 3
c	qcam(3,1) = 0.215554D0
c	qcam(3,2) = 0.440505D0
c	qcam(3,3) = -0.864540D0
c	qcam(3,4) = -0.109830D0
	!! 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
	data ihist /250*0/
	data ihit /81408*0/
C	data ihit /20352*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 k=1,kmax
	   node(k)=0.0
	   nodeid(k)=0
	   hits(k)=0
	enddo

	do dateloop=1,1
	  if(dateloop.eq.1)then
	    date=0529
	    framemax=30
	  endif
	  write(listfile,'(A15,I4.4,A4)') 'DATA_SMEI/list_',date,'.txt'
	  if(date.eq.0529) write(listfile,'(A15,I4.4,A4)') 'DATA_SMEI/cam3_',date,'.txt'
	do orbitloop=1,1
	  orbitnum=orbitloop
	  if(orbitloop.eq.1) maxk=1517
	do starloop=7,11

	 open(13,file=listfile,status='old')
	 frametest=framemax+1
	 avgthetax=0.D0
	 avgthetarot=0.D0
	 thetarot=0.D0
	 fcnt=0

	 do k=1,maxk

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

	  frametest=frametest+1

	if(starloop.eq.1)then
C	  print*, 'Vega...'
	  cra=279.23475D0
	  cdec=38.7837D0
	  if(k.eq.1482) frametest=1
	  write (outfile,'(A8,I4.4,A1,I2.2,A4)') 'Vega____',date,'_',orbitnum,'.grd'
	elseif(starloop.eq.2)then
C	  print*, 'Altair...'
	  cra=297.696D0
	  cdec=8.868D0
	  if(k.eq.162) frametest=1
	  write (outfile,'(A8,I4.4,A1,I2.2,A4)') 'Altair__',date,'_',orbitnum,'.grd'
	elseif(starloop.eq.3)then
C	  print*, 'Antares...'
	  cra=247.352D0
	  cdec=-26.432D0
	  if(k.eq.538) frametest=1
	  write (outfile,'(A8,I4.4,A1,I2.2,A4)') 'Antares_',date,'_',orbitnum,'.grd'
	elseif(starloop.eq.4)then
C	  print*, 'Arcturus...'
	  cra=213.915D0
	  cdec=19.182D0
	  if(k.eq.1014) frametest=1
	  write (outfile,'(A8,I4.4,A1,I2.2,A4)') 'Arcturs_',date,'_',orbitnum,'.grd'
	elseif(starloop.eq.5)then
C	  print*, 'Spica...'
	  cra=201.298D0
	  cdec=-11.161D0
	  if(k.eq.842) frametest=1
	  write (outfile,'(A8,I4.4,A1,I2.2,A4)') 'Spica___',date,'_',orbitnum,'.grd'
	elseif(starloop.eq.6)then
C	  print*, 'Alfa Oph...'
	  cra=263.734D0
	  cdec=12.56D0
	  if(k.eq.44) frametest=1
	  framemax=60
	  write (outfile,'(A8,I4.4,A1,I2.2,A4)') 'AlfaOph_',date,'_',orbitnum,'.grd'
	
	elseif(starloop.eq.7)then
C	  print*, 'Sirius...'
	  cra=101.287D0
	  cdec=-16.716D0
	  if(k.eq.784) frametest=1
	  framemax=36
	  write (outfile,'(A8,I4.4,A1,I2.2,A4)') 'Sirius__',date,'_',orbitnum,'.grd'
	elseif(starloop.eq.8)then
C	  print*, 'Achernar...'
	  cra=24.429D0
	  cdec=-57.236D0
	  if(k.eq.463) frametest=1
	  framemax=23
	  write (outfile,'(A8,I4.4,A1,I2.2,A4)') 'Achernr_',date,'_',orbitnum,'.grd'
	elseif(starloop.eq.9)then
C	  print*, 'BEta Cetus...'
	  cra=10.897D0
	  cdec=-17.986D0
	  if(k.eq.245) frametest=1
	  framemax=23
	  write (outfile,'(A8,I4.4,A1,I2.2,A4)') 'BetaCet_',date,'_',orbitnum,'.grd'
	elseif(starloop.eq.10)then
C	  print*, 'Canopus...'
	  cra=95.766D0
	  cdec=-52.695D0
	  if(k.eq.643) frametest=1
	  framemax=24
	  write (outfile,'(A8,I4.4,A1,I2.2,A4)') 'Canopus_',date,'_',orbitnum,'.grd'
	elseif(starloop.eq.11)then
C	  print*, 'Hamal...'
	  cra=31.793D0
	  cdec=23.462D0
	  if(k.eq.1479) frametest=1
	  framemax=28
	  write (outfile,'(A8,I4.4,A1,I2.2,A4)') 'Hamal___',date,'_',orbitnum,'.grd'
	endif

	if(frametest.le.framemax)then

	      write(infile,'(A14,I4.4,A1,A28)') 'DATA_SMEI/2003',date,'/',framename
	      if(date.eq.0529) write(infile,'(A14,I4.4,A1,A28)') 'DATA_SMEI/cam3',date,'/',framename

	          if(frametest.eq.1) then
		    print*, ' '
		    print*, outfile
		  endif
c		  print*, k, infile

		meascnt=0
C		call read_framecrx(infile,mode,ic,id,jd,frame,qdum,meascnt)
		call read_framecrx_old_i2(infile,mode,ic,id,jd,frame,qdum)

		if(meascnt.lt.55000)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)

		  !! 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		  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		      phi=0.D0
c		      rlat=90.D0
c	              call rotateq(qfinal,phi,rlat) !!rotate to standard system
c		      print*, ' RA/DEC: ',phi,rlat

C		  print*, 'Indexing frame...'
		  call indexframe(mode,ic,id,jd,level,TOP_KMAX,node,nodeid,hits,frame,affine,qfinal,cra,cdec,avgthetarot,badangle,xra,ydec)
		  if(badangle.eq.0) fcnt=fcnt+1

	   endif
	 endif
	 enddo

	 print*, 'Fcnt: ',fcnt
	 
	 if(fcnt.le.3) goto 98  !!too many frames were saturated with cosmic rays

	 thetarot=avgthetarot/float(fcnt)
c	 print*, 'Thetarot: ',thetarot

	 print*, 'Calling average night...'
c	 call averagenight(node,nodeid,hits,TOP_KMAX,level,cra,cdec,image)
	 call averagenight_rotate(node,nodeid,hits,TOP_KMAX,level,cra,cdec,thetarot,image)

	!!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 65000')
	do j=1,200
	  write(10,'(20i6)')(image(i,j),i=1,200)
	enddo
	close(10)

98	close(13)
	enddo  !!end starloop
	enddo  !!end orbitloop
	enddo  !!end dateloop

99	print*, 'Fortran out!'

	end
