C+
C NAME:
C	star_transit_gauss
C MODIFICATION HISTORY:
C	July, 2003 A. Smith (UCSD/CASS)
C-
	program star_transit_gauss
	implicit none

	character	infile*50,outfile*16,dum*20
	integer*4	star(200,200), gauss(200)
	integer*4       i,j,starloop,dateloop,orbitloop,date,orbitnum,minr,maxr,i0,ihalfmax,halfmax,dif
	real*8		radius,temp,thetax

	print*, 'Fortran in...'

	do i=1,200
	 do j=1,200
	   star(i,j)=0
	 enddo
	enddo
	
	do dateloop=1,1
	 if(dateloop.eq.1)  date=0227
	 if(dateloop.eq.2)  date=0228
	 if(dateloop.eq.3)  date=0301
	do orbitloop=1,1
	 orbitnum=orbitloop
	do starloop=1,7
	
	do i=1,200
	 do j=1,200
	   star(i,j)=0
	 enddo
	enddo

	if(starloop.eq.1)then
	  write (infile,'(A35,I4.4,A1,I2.2,A8)') 'Star_Transits/brx_transits/Aldebar_',date,'_',orbitnum,'_brx.grd'
	  thetax=1.23
	elseif(starloop.eq.2)then
	  write (infile,'(A35,I4.4,A1,I2.2,A8)') 'Star_Transits/brx_transits/Betel___',date,'_',orbitnum,'_brx.grd'
	  thetax=19.37
	elseif(starloop.eq.3)then
	  write (infile,'(A35,I4.4,A1,I2.2,A8)') 'Star_Transits/brx_transits/Sirius__',date,'_',orbitnum,'_brx.grd'
	  thetax=26.19
	elseif(starloop.eq.4)then
	  write (infile,'(A35,I4.4,A1,I2.2,A8)') 'Star_Transits/brx_transits/Vega____',date,'_',orbitnum,'_brx.grd'
	  thetax=-21.89
	elseif(starloop.eq.5)then
	  write (infile,'(A35,I4.4,A1,I2.2,A8)') 'Star_Transits/brx_transits/Alcaid__',date,'_',orbitnum,'_brx.grd'
	  thetax=28.74
	elseif(starloop.eq.6)then
	  write (infile,'(A35,I4.4,A1,I2.2,A8)') 'Star_Transits/brx_transits/EtaDrac_',date,'_',orbitnum,'_brx.grd'
	  thetax=4.77
	elseif(starloop.eq.7)then
	  write (infile,'(A35,I4.4,A1,I2.2,A8)') 'Star_Transits/brx_transits/Almach__',date,'_',orbitnum,'_brx.grd'
	  thetax=-22.47
	endif


	!! read in headers, then image array
	open(12,file=infile,form='formatted',readonly)
	read(12,120)dum
	read(12,120)dum
	read(12,120)dum
	read(12,120)dum
	read(12,120)dum
120	format(a20)
	read(12,12)((star(i,j),i=1,200),j=1,200)
12	format(20i6)
	close(12)
	
	print*, '	'
	do j=120,140
C	do j=1,200
	 maxr=0
	 dif=65000
	 do i=80,120
	   if(maxr.lt.star(i,j))then
	     maxr=star(i,j)
	     i0=i
	   endif
	 enddo
	 halfmax=nint(float(maxr)/2.0)
	 do i=1,100
	   if(dif.gt.abs(halfmax-star(i,j))) then
	     ihalfmax=i
	     dif=abs(halfmax-star(i,j))
	   endif
	 enddo
C	 if(maxr.gt.60000)
	 print*, j, maxr, i0, ihalfmax, i0-ihalfmax
	enddo

	write (outfile,'(A16)') 'avg_star_str.grd'
	!!Write rotated image 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)')(star(i,j),i=1,200)
	enddo
	close(10)
	
	enddo !!end starloop
	enddo !!end orbitloop
	enddo !!end dateloop

	print*, 'Fortran out!!!'

	end
