C+
C NAME:
C	READ_HOSF8
C PURPOSE:
C	Read Helios data for synoptic map
C CALLING SEQUENCE:
	subroutine READ_HOSF8(cWildCard,nCar,JDCar,XCbeg,ICnr, iYr,
     &		Radius,Speed,Eref,
     &		XElow,XEhigh,iEorW,XRlim,
     &		iSc,iHOS,cHOS,iSectBeg,iSectEnd,LT,iDay,nDev,dSig,
     &		NLMAX,NS,NL,SRCG,DIST,XC,XL,ZZ,XE,XLS,XCE,XPE,XLE,IYRS,DOYS8)
C INPUTS:
C	cWildCard
C	nCar
C	JDCar
C	XCbeg
C	ICnr,
C	Radius
C	Speed
C	Eref
C	XElow
C	XEhigh
C	iEorW
C	XRlim
C	iSc
C	iHOS
C	iSectBeg
C	iSectEnd
C	iDay
C	nDev
C	dSig,
C	NS			! starting N value
C OUTPUTS:
C	iHOS
C	cHOS
C	LT
C	iDay
C	nDev
C	dSig,
C	NLMAX
C	NS			! starting N value
C	NL
C	SRCG
C	DIST
C	XCE
C	ZZ
C	XL
C	XR
C	XC
C	IYRS
C	DOYS8
C CALLS:
C	CWRITE, ECLIPTIC_HELIOGRAPHIC, FLAG_GAL_CNTR, JULIAN, READ_1530,
C	SC_ECLIP, THOM_PNT_SRCE, T_FILTER, XMAP_SC_POS, XMAP_OBS_POS
C EXTERNAL:
	external	HELIOS_1
	external	HELIOS_2
C INCLUDE:
	include		'sun.h'
	include		'hos_e9.h'
	include		'hosucsd.h'
C PROCEDURE:
C MODIFICATION HISTORY:
C	FEB-1996, Paul Hick (UCSD); extracted from SANHEL.FOR
C	~2000,	  B. Jackson (UCSD): added SRCG file in order to pass the unique photometer identifier 
C		  through the calling sequence.
C	2/26/02   B. Jackson (UCSD): realized that the galactic center data was eliminated from the 
C		  output and I disabled this subroutine (GAL_CNTR) for tests by commenting it out.
C-
	double precision JDCar(nCar)
	character	cWildCard*(*),
     &			cSc(2)		/'A','B'/,
     &			cHOS(iHOS)*(*),
     &			SRCG(*)*9,SRCGS*9
	real		XC(*),			! Modified Carrington variable Point P
     &			XL(*),			! Heliographic latitude Point P (deg)
     &			ZZ(*),			! Intensity (S10)
     &			XE(*)			! Topocentric elongation Point P (deg)
						! (+=East;-=West)
	real		XLS(*),			! Topocentric ecliptic longitude Sun
     &			XCE(*),			! Modified Carrington variable S/C
     &			XPE(*),			! Topocentric ecliptic lng(P)-lng(Sun)
     &			XLE(*),			! Topocentric ecliptic lat(P)
     &			DIST(*)			! Helios Distance from the Sun
	integer		IYRS(*)
	double precision DOYS8(*)
C------
C *HOS arrays are use to store data read from the Helios data files by READ_1530
	integer		PHOS(MXC),		! Photometer ID
     &			NN(MXC)
	real		THOS(MXC),		! Times (doy of year)
     &			RHOS(MXC),		! Distance s/c-Sun (AU)
     &			LHOS(MXC),		! Ecliptic longitude s/c (deg)
     &			ZHOS(NSECT*MXC)		! Intensities (S10)
	real		CC(MXC),FF(MXC)
	real		SX (NSECT*NBUF),	! Scratch arrays for T_FILTER
     &			SNX(NSECT*NBUF),
     &			SXX(NSECT*NBUF)
C-------
C Filled by SC_ECLIP: relative heliocentric (H* arrays) and topocentric
C (T* arrays) ecliptic coordinates of point P for each line of sight
	real		HLNG(NBSECT:NESECT,2),		! Longitude (deg)
     &			HLAT(NBSECT:NESECT,2),		! Latitude (deg)
     &			HR  (NBSECT:NESECT,2),		! Distance (s/c-Sun dist)
     &			HELO(NBSECT:NESECT,2),		! Distance (s/c-Sun dist)
     &			TLNG(NBSECT:NESECT,2),		! Longitude (deg)
     &			TLAT(NBSECT:NESECT,2),		! Latitude (deg)
     &			TR  (NBSECT:NESECT,2),		! Distance (s/c-Sun dist)
     &			TELO(NBSECT:NESECT,2)		! Topocentric elongation
	double precision JEPOCH
	character	cName*80
	logical		bDataAvailable	/.FALSE./,
     &			bMore
	save		RadiusOld, SpeedOld, ErefOld, XElowOld	 , XEhighOld,
     &			iEorWOld , XRlimOld, iScOld , iSectBegOld, iSectEndOld,
     &			iDayOld  , nDevOld , dSigOld
C-------
C Check whether new data have to be read.
	if (bDataAvailable			.and.
     &		XCbegOld	.eq. XCbeg	.and.
     &		iCnrOld		.eq. iCnr	.and.
     &		RadiusOld	.eq. Radius	.and.
     &		SpeedOld	.eq. Speed	.and.
     &		ErefOld		.eq. Eref	.and.
     &		XElowOld	.eq. XElow	.and.
     &		XEhighOld	.eq. XEhigh	.and.
     &		iEorWOld	.eq. iEorW	.and.
     &		XRlimOld	.eq. XRlim	.and.
     &		iScOld		.eq. iSc	.and.
     &		iSectBegOld	.eq. iSectBeg	.and.
     &		iSectEndOld	.eq. iSectEnd	.and.
     &		iDayOld		.eq. iDay	.and.
     &		nDevOld		.eq. nDev	.and.
     &		dSigOld		.eq. dSig) return
C-------
C Save all parameters involved int the data selection.
C
	XCbegOld	= XCbeg
	iCnrOld		= iCnr
	RadiusOld	= Radius
	SpeedOld	= Speed
	ErefOld		= Eref
	XElowOld	= XElow
	XEhighOld	= XEhigh
	iEorWOld	= iEorW
	XRlimOld	= XRlim
	iScOld		= iSc
	iSectBegOld	= iSectBeg
	iSectEndOld	= iSectEnd
	iDayOld		= iDay
	nDevOld		= nDev
	dSigOld		= dSig
C-------
C Get relative heliocentric and topocentric ecliptic coordinates of point P
C for all lines of sight (photometer-sector combinations)
	call SC_ECLIP( iSc,NBSECT,NESECT,HLNG,HLAT,HR,HELO)	! Heliocentric
	call SC_ECLIP(-iSc,NBSECT,NESECT,TLNG,TLAT,TR,TELO)	! Topocentric
C	print *, TELO
	EScale	= 1.
	if (Eref  .ne. 0) EScale = 0.
	if (Speed .ne. 0) Spiral = Sun__Spiral/Speed
	iHOS	= 0
	NL	= NS		! Count data points from starting N
	NLTOOM  = NS
	bMore	= .TRUE.
	IC	= XCbeg
	XClast	= XCbeg+ICnr-1
	do while (IC .le. XClast .and. bMore)
C	    if(iSc.eq.1) cName = 'X__HOS1:'//cWildCard
C	    if(iSc.eq.2) cName = 'X__HOS2:'//cWildCard
C	    cName(7+ 1:7+ 1) = cSc(iSc)
C	    write (cName(7+11:7+14),'(I4.4)') IC
	    cName = cWildCard
	    if (iSearch(1,cName,cName) .eq. 1) then
		call JULIAN(1,IAYR,RADOY,JDCar(IC  ),JEPOCH)
	        call JULIAN(1,IBYR,RBDOY,JDCar(IC+1),JEPOCH)
		iHOS = iHOS+1
		cHOS(iHOS) = cName
		write (*,'(3X,2A,I4.4,A,2(I3.3,A))')
     &			cName(:itrim(cName)),'  (',IAYR,'  ',int(RADOY),'-',int(RBDOY),')'
		nS = NSECT		! Total # sectors
		if (LT.eq.1) iDC = HOS__C_1			! color of light
		if (LT.eq.2) iDC = HOS__C_2
		if (LT.eq.3) iDC = HOS__C_3
C	Read 15 and 30 deg data
		iD = iDC + HOS__P_1 + HOS__P_2			! photometers
		iD = iD  + HOS__F_4 + HOS__SWAP_SECT		! Filter 4 - clear
		call HOSRead(iD,cName,MXC,iCnt,NN,THOS,PHOS,CC,FF,RHOS,LHOS,nS,ZHOS)
C		call READ_1530(MXC,I,cName,iSc,iYr,LT,iFilt,iCnt,THOS,RHOS,LHOS,PHOS,ZHOS)
C-----
C EScale is set only if ERef .ne. 0, and only after reading the first file.
C Don't move this statement. LT is needed.
C		if (EScale .eq. 0) EScale = THOM_PNT_SRCE(Radius/sind(Eref),Eref,LT)
		if (EScale .eq. 0) then
		    call ThomsonUBVConst(LT,U,APM)
		    EScale = ThomsonlosS10Far(0.,-1.,sngl(Radius/Sun__RAu/sind(Eref)),Eref,APM,P)
		end if
C		call GAL_CNTR(iSc,iCnt,NBSECT,NESECT,LHOS,PHOS,ZHOS)   ! Out 2/26/02
		call T_FILTER(iCnt,NSECT,THOS,ZHOS,PHOS,iDay,nDev,dSig,NBUF,SNX,SX,SXX)
		ICoff = IC-XCbeg		! Map all points to rotation XCbeg
		I = 1
		do while (I .le. iCnt .and. bMore)	! Loop over all MC's
							! Carrington variable for S/C
		    if (iSc .eq. 1) XCHOS = XMAP_SC_POS(HELIOS_1,iYr,THOS(I),nCar,JDCar)
		    if (iSc .eq. 2) XCHOS = XMAP_SC_POS(HELIOS_2,iYr,THOS(I),nCar,JDCar)
		    XCHOS = XCHOS-ICoff
		    J = iSectBeg
		    do while (J .le. iSectEnd .and. bMore)! Loop over sectors
			if (iEorW .eq. 0) then		! All XE (East+West)
			    rElo = abs(TELO(J,PHOS(I)))
			else
			    rElo = iEorW*TELO(J,PHOS(I))! Only positive XE (East) or
			end if				! only negative XE (West)
			rLng = LHOS(I)+TLNG(J,PHOS(I))	! Topocentric ecliptic lng(P)
			rLat = TLAT(J,PHOS(I))		! Topocentric ecliptic lat(P)
			call ECLIPTIC_EQUATOR(0,iYr,THOS(I),rLng,rLat) ! rLng = RA of P
			dRA  = rLng			! Right Ascension RA(P)
			rLng = LHOS(I)			! Topocentric ecliptic lng(Sun)
			rLat = 0.			! Topocentric ecliptic lat(Sun)
			call ECLIPTIC_EQUATOR(0,iYr,THOS(I),rLng,rLat)
							! rLng = RA(Sun)
			dRA = dRA-rLng			! RA(P)-RA(Sun)
			if (abs(dRA) .gt. 180.) dRA = dRA-sign(1.,dRA)*360.
			JHOS = (I-1)*NSECT+J-NBSECT+1
			if (ZHOS(JHOS) .ne. BadR4() .and.			! Skip flagged data
     &				XElow .le. rElo .and. rElo .le. XEhigh .and.
     &				abs(dRA) .le. XRlim) then		! Check elongation and RA range
			    bMore = NL .lt. NLMAX*2
			    if (bMore) then
				NLTOOM = NLTOOM + 1
			        if(NL .ge. NLMAX) go to 123
				NL = NL+1
				XE(NL) = TELO(J,PHOS(I))		! Topocentric elongation P (deg)
				rLng = LHOS(I)+180.+HLNG(J,PHOS(I))	! Heliocentric ecliptic lng(P)
				rLat = HLAT(J,PHOS(I))			! Heliocentric ecliptic lat(P)
				call ECLIPTIC_HELIOGRAPHIC(0,iYr,THOS(I),rLng,rLat)
									! rLng: Heliographic lng(P)
									! rLat: Heliographic lat(P)
				XC(NL) = XMAP_OBS_POS(XCHOS,rLng)	! Carrington variable point P
				XR = HR(J,PHOS(I))*RHOS(I)		! Heliocentric dist(P) (AU)
				if (Speed .ne. 0) XC(NL) = XC(NL)+Sun__Spiral*(Radius-XR)/Speed
				XL(NL) = rLat				! Heliographic lat(P) (deg)
				ZZ(NL) = ZHOS(JHOS)			! Intensity (S10)
C				if (ERef .ne. 0) ZZ(NL) = ZZ(NL)*EScale/THOM_PNT_SRCE(XR/sind(rElo),rElo,LT)
				if(ZZ(NL) .ne. BadR4()) then
C				    if (ERef .ne. 0) ZZ(NL) = ZZ(NL)*EScale/
C     &				    ThomsonlosS10Far(0.,-1.,sngl(Radius/Sun__RAu/sind(rElo)),rElo,APM,P)
				end if
	    			write(SRCGS(1:2),'(I2)') iSc
	    			write(SRCGS(3:5),'(I3)') PHOS(I)
	    			write(SRCGS(6:9),'(I4)') J
				SRCG(NL)  = SRCGS
				IYRS(NL) = iYr
				DOYS8(NL)= dble(THOS(I))
				DIST(NL) = RHOS(I)			! Sun S/C distance
				XLS (NL) = LHOS(I)			! Topocentric ecliptic lng(Sun)
				XCE (NL) = XCHOS			! Carrington variable S/C
				XPE (NL) = TLNG(J,PHOS(I))		! Topocentric ecliptic lng(P)-lng(Sun)
				XLE (NL) = TLAT(J,PHOS(I))		! Topocentric ecliptic lat(P)
			    end if
 123       if(NLTOOM.eq.(NLMAX+1)) print *, 'Not all data read; NL gt NLMAX'
c	print *,nl, xc(nl),xl(nl),xe(nl)
			end if
			J = J+1			! Next sector
		    end do
		    I = I+1			! Next MC
		end do
C	Read 90 degree data
		iD = iDC + HOS__P_3 + HOS__F_4
		call HOSRead(iD,cName,MXC,iCnt,NN,THOS,PHOS,CC,FF,RHOS,LHOS,2,ZHOS)
C		call READ_90(MXC,1,2,cName,iSc,iYr,LT,iCnt,THOS,RHOS,LHOS,ZHOS)
		call ArrI4Constant(iCnt,3,PHOS)
		call T_FILTER(iCnt,2,THOS,ZHOS,PHOS,iDay,nDev,dSig,NBUF,SNX,SX,SXX)
		call ArrI4Constant(iCnt,2,PHOS)
		I = 1
		do while (I .le. iCnt .and. bMore)	! Loop over all MC's
							! Carrington variable for S/C
		    if (iSc .eq. 1) XCHOS = XMAP_SC_POS(HELIOS_1,iYr,THOS(I),nCar,JDCar)
		    if (iSc .eq. 2) XCHOS = XMAP_SC_POS(HELIOS_2,iYr,THOS(I),nCar,JDCar)
		    XCHOS = XCHOS-ICoff
		    J = 1
		    do while (J .le. 1 .and. bMore)	! Loop over sectors
			rElo = 90.
			rLng = LHOS(I)+TLNG(J,PHOS(I))	! Topocentric ecliptic lng(P)
			rLat = TLAT(J,PHOS(I))		! Topocentric ecliptic lat(P)
			call ECLIPTIC_EQUATOR(0,iYr,THOS(I),rLng,rLat) ! rLng = RA of P
			dRA  = rLng			! Right Ascension RA(P)
			rLng = LHOS(I)			! Topocentric ecliptic lng(Sun)
			rLat = 0.			! Topocentric ecliptic lat(Sun)
			call ECLIPTIC_EQUATOR(0,iYr,THOS(I),rLng,rLat)
							! rLng = RA(Sun)
			dRA = dRA-rLng			! RA(P)-RA(Sun)
			if (abs(dRA) .gt. 180.) dRA = dRA-sign(1.,dRA)*360.
			JHOS = (I-1)*2+J
			if (ZHOS(JHOS) .ne. BadR4() .and.			! Skip flagged data
     &				XElow .le. rElo .and. rElo .le. XEhigh .and.
     &				abs(dRA) .le. XRlim) then		! Check elongation and RA range
			    bMore = NL .lt. NLMAX*2
			    if (bMore) then

				NLTOOM = NLTOOM + 1
			        if(NL .ge. NLMAX) go to 124
		
				NL = NL+1
				XE(NL) = TELO(J,PHOS(I))		! Topocentric elongation P (deg)
				rLng = LHOS(I)+180.+HLNG(J,PHOS(I))	! Heliocentric ecliptic lng(P)
				rLat = HLAT(J,PHOS(I))			! Heliocentric ecliptic lat(P)
				call ECLIPTIC_HELIOGRAPHIC(0,iYr,THOS(I),rLng,rLat)
									! rLng: Heliographic lng(P)
									! rLat: Heliographic lat(P)
				XC(NL) = XMAP_OBS_POS(XCHOS,rLng)	! Carrington variable point P
				XR = HR(J,PHOS(I))*RHOS(I)		! Heliocentric dist(P) (AU)
				if (Speed .ne. 0) XC(NL) = XC(NL)+Sun__Spiral*(Radius-XR)/Speed
				XL(NL) = rLat				! Heliographic lat(P) (deg)
				ZZ(NL) = ZHOS(JHOS)			! Intensity (S10)

C				if (ERef .ne. 0) ZZ(NL) = ZZ(NL)*EScale/THOM_PNT_SRCE(XR/sind(rElo),rElo,LT)
				if(ZZ(NL) .ne. BadR4()) then
C				    if (ERef .ne. 0) ZZ(NL) = ZZ(NL)*EScale/
C     &				    ThomsonlosS10Far(0.,-1.,sngl(Radius/Sun__RAu/sind(rElo)),rElo,APM,P)
				end if
				IPHOT = 3
	    			write(SRCGS(1:2),'(I2)') iSc
	    			write(SRCGS(3:5),'(I3)') IPHOT
	    			if(iSc.eq.1) write(SRCGS(6:9),'(A)') ' -90'
				if(iSc.eq.2) write(SRCGS(6:9),'(A)') ' +90'
				SRCG(NL)  = SRCGS				
				IYRS(NL) = iYr
				DOYS8(NL)= dble(THOS(I))
				DIST(NL) = RHOS(I)			! Sun S/C distance
				XLS (NL) = LHOS(I)			! Topocentric ecliptic lng(Sun)
				XCE (NL) = XCHOS			! Carrington variable S/C
				XPE (NL) = 0.		 !TLNG(J,PHOS(I))		! Topocentric ecliptic lng(P)-lng(Sun)
				XLE (NL) = (2*iSc-3)*90. !TLAT(J,PHOS(I))		! Topocentric ecliptic lat(P)
			    end if
 124       if(NLTOOM.eq.(NLMAX+1)) print *, 'Not all data read; NL gt NLMAX'
c	print *,nl, xc(nl),xl(nl),xe(nl)
			end if
			J = J+1			! Next sector
		    end do
		    I = I+1			! Next MC
		end do
	    end if
	    IC = IC+1				! Next file
	end do
	bDataAvailable	= .TRUE.
	if(NLTOOM.gt.NLMAX) write (*,'(10X,A,I6,A,//)') 
     &	' There were',NLTOOM,' Helios density values' 
	return
	end
