C+
C NAME:
C	ReadVIPSn8_2
C	ReadVIPSLOSCheckn_2
C PURPOSE:
C	Read IPS velocity data into arrays
C CATEGORY:
C	I/O
C CALLING SEQUENCE:
C	call ReadVIPSn8g(iSys,iReadVIPSn_2,iProcessVIPSn_2,cWildVIPS,
C     &		nCar,JDCar,bAuto,XCtest1,XCtest2,NCoff,MJDref,
C     &		Radius,
C     &		XElow,XEhigh,iEorW,XRlim,
C     &		NLmax,NL,cbvfiles,SRCVG,SRC,SRCVGsav,SRCsav,
C     &		IYRF,IREC,IYRS,DOYS8,XDS,XLS,XLL,XDL,XCE,XE,XC,YL,VV,DVV,GG,DGG,
C     &		NSmax,IYRFsav,IRECsav,IYRSsav,DOYSsav8,XDSsav,XLSsav,XLLsav,
C     &		XDLsav,XCEsav,XEsav,XCsav,YLsav,VVsav,DVVsav,GGsav,DGGsav)
C
C	call ReadVIPSLOSCheckn_2(XCbeg,XCend,NLOS,NINSIDE,DLOS)
C INPUTS:
C   ReadVIPSn8_2:
C	iSys		integer		integer indicating which IPS system is used (modified 8/30/2013 BVJ)
C					1 = UCSD
C					2 = Cambridge
C					3 = Ooty
C					4 = Nagoya
C					5 = General
C	iReadVIPSn8_2	external integer function
C	iProcessVIPSn_2	external integer function
C					external functions control data file access
C	cWildVIPS	character*(*)	wildcard used to locate data files;
C					the wildcard must contain the substring 
C					'%%'
C	nCar		integer		dimension of JDCar
C	JDCar(nCar)	integer		start times (Julian days) of Carrington rotations
C	bAuto		logical
C	XCtest1		real		modified Carrington variable for start of search
C	XCtest2		real		modified Carrington variable for end of search
C	NCoff		integer		JDCar(I) is the start of rotation NCoff+I
C	MJDref		real*8		if not equal 0, only data prior to
C					modified Julian day MJDref are used
C	Radius		real		reference distance (AU)
C
C	XElow		real
C	XEhigh		real
C	iEorW		integer
C	XRlim		real
C
C	NLmax		integer		max. # points read into arrays 
C					XC,YL,VV,DVV,GG,DGG XCsav,YLsav,VVsav,DVVsav,GGsav,DGGsav
C	NSmax		integer
C
C    ReadVIPSLOSCheckn_2:
C	XCbeg		real		modified Carrington variable for start of map
C	XCend		real		modified Carrington variable for end of map
C	NLOS		integer
C	NINSIDE		integer
C	DLOS
C OUTPUTS:
C	NL		integer		# valid data points in arrays XC,YL,VV,DVV,GG,DGG
C	SRC(NLmax)	character*8	Character Source identifier
C	IYRF(NLmax)	integer		year of file from which observation was extracted (should be same as IYRS)
C	IREC(NLmax)	integer		record number on file IYRF
C	DOYS8(NLmax)	real*8		time of observation: day of year (incl. fraction for time of day)
C	XDS (NLmax)	real		Sun-Earth distance
C	XLS (NLmax)	real		geocentric ecliptic longitude Sun
C	XLL (NLmax)	real		geocentric ecliptic lng(P)-lng(Sun) (deg)
C	XDL (NLmax)	real		geocentric ecliptic lat(P) (deg)
C	XCE (NLmax)	real		modified Carrington variable of sub-Earth point on Sun
C	XE  (NLmax)	real		elongation (deg) (>0: East of Sun; <0: West of Sun)
C	XC  (NLmax)	real		modified Carrington variable point P after traceback to
C					  heliocentric distance Radius at speed VV
C	YL(NLmax)	real		heliographic latitude point P
C	VV(NLmax)	real		IPS velocity (km/s)
C	DVV(NLmax)	real		IPS velocity error (km/s)
C	GG(NLmax)	real		IPS disturbance factor
C	DGG(NLmax)	real		IPS disturmance factor error
C	NSmax		integer		# data points saved in XCsav,YLsav,VVsav
C	SRCsav(NSmax)	character	scratch arrays (used internally only),
C	IYRFsav(NSmax)	integer		
C	IRECsav(NSmax)	integer
C	IYRSsav(NSmax)	integer
C	DOYSsav(NSmax)	real*8
C	XDSsav (NSmax)	real
C	XLLsav (NSmax)	real
C	XDLsav (NSmax)	real
C	XCEsav (NSmax)	real
C	XEsav  (NSmax)	real
C	XCsav  (NSmax)	real
C	YLsav  (NSmax)	real
C	VVsav  (NSmax)	real
C	DVVsav (NSmax)	real
C	GGsav  (NSmax)	real
C	DGGsav  (NSmax)	real
C RESTRICTIONS:
C	Data files are read using a logical unit number assigned by iGetLun.
C	Since a data file may be left open on return, there is a potential
C	danger if the same unit is used somewhere. Assign unit numbers
C	with iGetLun to make sure this doesn't happen.
C FUNCTIONS/SUBROUTINES:
C	bOpenFile, itrim, SunNewcomb, iSetFileSpec, iGetFileSpec, Julian
C EXTERNAL:
C INCLUDE:
C	include		'filparts.h'
C	include		'sun.h'
C COMMON BLOCKS:
C SIDE EFFECTS:
C PROCEDURE:
C >	All points in the range [XCtest1,XCtest2] are extracted from the data files
C >!!!!	The modified Carrington variables are in units offset by NCoff from
C	the regular Carrington rotation number (i.e. in units which can be
C	used as index to JDCar)
C >	Two external user-defined are needed to read the data files.
C	External functions exist for reading the Nagoya and the UCSD velocity
C	IPS data: iReadNagoyan8_2, iProcessNagoyan8_2
C		  iReadUCSDn8_2  , iProcessUCSDn8_2
C		  iReadOotyn8_2  , iProcessOotyn8g_2
C		  iReadGen8g    , iProcessGen8_2
C		  iReadGen8_2   , iProcessGen8_2
C	These are programmed in the form:
C
C	function iReadNagoyan8_2(iSys,iU,iFirst)
C	... ( read a record from the data file ) ...
C	return
C	entry iProcessNagoyan8_2(iRec,nCar,JDCar,NCoff,Radius,
C		cSRCVG,cSrc,nYr,Doy8,XCsc,XCobs,xLat,xVV,xDVV,xGG,xDGG,dRA,
C		rLngSun,rLngP,rLatP,rEloP )
C	... ( process the record to produce the output values	...
C	...   nYr,Doy8,XCsc,XCobs,xLat,xVV,xDVV,xGG,xDGG,dRA,		...
C	...   rLngSun,rLngP,rLatP,rEloP )
C	return
C	end
C
C	Input values to iReadVIPSn_2
C	iU	integer		logical unit number assigned by bOpenFile
C	iFirst	integer		1 : initializes record counter
C					(used when reading 1st record of file)
C				0 : adds one to record counter
C 	Output values of iProcessVIPSn_2:
C	iRec	integer		record # on file
C	cSrc	character	Source name
C	nYr	integer		year and ..
C	Doy	real		.. day of year of observation
C	XCsc	real		modified Carington variable for sub-SC point
C				  (NCoff already subtracted)
C	XCobs	real		modified Carrington variable of point P after
C				  trace-back to distance Radius (NCoff already subtracted)
C	xLat	real		heliographic latitude point P (deg)
C	xVV	real		IPS velocity (km/s)
C	xDVV	real		IPS velocity error (km/s)
C	xGG	real		IPS disturbance factor
C	xDGG	real		IPS disturbance factor error
C	dRA	real		RA(point P)-RA(Sun) (deg)
C	rLngSun real		geocentric ecliptic longitude Sun (deg)
C	rLngP	real		geocentric ecliptic longitude of point P (deg)
C					(relative to Sun)
C	rLatP	real		geocentric ecliptic latitude  of point P (deg)
C	rEloP	real		geocentric source elongation (deg)
C				NOTE: the sign is used to indicate East/West of Sun
C					rEloP>0: East of Sun; rEloP<0: West of Sun
C MODIFICATION HISTORY:
C	JUN-1994, Paul Hick (UCSD)
C	OCT-1996, Paul Hick (UCSD); added Ooty routines
C-
	subroutine ReadVIPSn8_2(iSys,iReadVIPSn_2,iProcessVIPSn_2,cWildVIPS,
     &		nCar,JDCar,bAuto,XCtest1,XCtest2,NCoff,MJDref,
     &		Radius,XElow,XEhigh,iEorW,XRlim,
     &		NLmax,NL,cvgfiles,SRCVG,SRC,SRCVGsav,SRCsav,
     &		IYRF,IREC,IYRS,DOYS8,XDS,XLS,XLL,XDL,XCE,XE,XC,YL,VV,DVV,GG,DGG,
     &		NSmax,IYRFsav,IRECsav,IYRSsav,DOYSsav8,XDSsav,XLSsav,XLLsav,
     &		XDLsav,XCEsav,XEsav,XCsav,YLsav,VVsav,DVVsav,GGsav,DGGsav)

	real*8		JDCar(nCar),
     &			MJDref,
     &			xTT,JEpoch,
     &			Doy8,
     &			DOYS8(NLmax),DOYSsav8(NSmax)		! Time: doy of year

	integer		IYRF(NLmax), IYRFsav(NSmax),
     &			IREC(NLmax), IRECsav(NSmax),
     &			IYRS(NLmax), IYRSsav(NSmax)

	real		XDS (NLmax), XDSsav (NSmax),		! Sun-Earth distance
     &			XLS (NLmax), XLSsav (NSmax),		! Ecliptic longitude Sun
     &			XLL (NLmax), XLLsav (NSmax),
     &			XDL (NLmax), XDLsav (NSmax),
     &			XCE (NLmax), XCEsav (NSmax),
     &			XE  (NLmax), XEsav  (NSmax),		! Elongation point P
     &			XC  (NLmax), XCsav  (NSmax),		! Carrington variable of Point P
     &			YL  (NLmax), YLsav  (NSmax),		! Heliographic latitude of Point P
     &			VV  (NLmax), VVsav  (NSmax),		! Solar wind velocity (km/s)
     &			DVV (NLmax), DVVsav (NSmax),		! Solar wind velocity error (km/s)
     &			GG  (NLmax), GGsav  (NSmax),		! G-factor
     &			DGG (NLmax), DGGsav (NSmax)		! G-factor error

C	character	cWildVIPS*(*),
	character	cWildVIPS*100, cWildVIPSo*100,
     &			cvgfiles(*)*11,
     &			SRC(NLmax)*9,SRCsav(NSmax)*9,cSrc*9,SRCVG(NLmax)*115,SRCVGsav(NSmax)*115,cSRCVG*115,
     &			cSay*9			/'ReadVIPSn8'/,
     &			cFile*80,cFiles*11

	integer		iFrstYr			/0/,
     &			NS			/0/

	real		XCmargin		/1./

	logical		bAuto,
     &			bOpen			/.FALSE./,
     &			bGetRecord,
     &			bReading,
     &			bOpenFile,
     &			bMore,
     &			bReadErr,
     &			bSaveOverrun		/.FALSE./

	integer		nYrLen			/4/		! # digits used to identify year in file name
	integer		Str2Str, Flt2Str

	include		'dirspec.h'
	include		'filparts.h'
	include		'openfile.h'

	save		bOpen, bGetRecord, bSaveOverrun,
     &			iU, iYr, NS,
     &			iFrstYr, iFile

	logical		bLOSCheckLocn8_2
	integer		NLOS			/0/
	save		XCbeg,XCend,NLOS,NINSIDE,DLOS,cWildVIPSo
C-------
C On first call check whether data files are present


	if (iSys.gt.0) then
	   print *, ' This is an IPS velocity read' 
	   iGvalue = 0
        else
	   print *, ' This is an IPS g-value read'
	   iSys = -iSys
	   iGvalue = 1
	end if

	if(cWildVIPS.ne.cWildVIPSo) then
	  iFrstYr = 0	  
	  NS      = 0
	  cWildVIPSo = cWildVIPS
	end if
	if (iFrstYr .eq. 0) then
	    print *, 'in readvips 1', cWildVIPS
	    I = iSetFileSpec(cWildVIPS)
	    I = iGetFileSpec(FIL__NAME,FIL__TYPE,cFile)

	    iFile = index(cFile,cWildChar(:nYrLen))
	    if (iFile .eq. 0) call Say(cSay,'E','INVTMPLTE',
     &			'wildcard template must contain '//cWildChar(:nYrLen))

	    if (iSearch(1,cWildVIPS,cFile) .ne. 1)
     &		call Say(cSay,'E','FILNOTFOUND',
C     &			'no files matching wildcard found :'//cWildVIPS)
     &			'no files matching wildcard found :')

	    I = iSetFileSpec(cFile)
	    I = iGetFileSpec(FIL__NAME,FIL__TYPE,cFile)

	    I = 1

	      call Str2Flt_Int(.TRUE.)			! Save year of first file
	      call Str2Flt(cFile(iFile:iFile+nYrLen-1),I,iFrstYr)
	      iFile = index(cWildVIPS,cWildChar(:nYrLen)) ! Save position of wildcard %%%% in cWildVIPS

	end if
C------
C Check the save scratch arrays for valid data points.
C NOTE: NS can only be unequal to zero in AUTO mode. In AUTO mode only XCtest1
C	and XCtest2 change (both increase). All other data selection
C	criteria remain the same.

C	NL = 0				! Initialize data point counter Not needed to be set if set 0 outside (B. Jackson 10/04/2014)
	LOS = 0				! Counter for rejected lines of sight by bLOSCheckLocn8_2

	if (NS .ne. 0) then		! Only in AUTO mode
C-------
C Scan the `save' arrays. Only points with XC >= XCtest1 are needed for
C subsequent maps. Discard points with XC < XCtest1.

	    N = 0
	    do I=1,NS
		if (XCsav(I) .ge. XCtest1) then
		    N = N+1
		    SRCVGsav(N)= SRCVGsav(I)
		    SRCsav(N)  = SRCsav(I)
		    IYRFsav(N) = IYRFsav(I)
		    IRECsav(N) = IRECsav(I)

		    IYRSsav(N) = IYRSsav(I)
		    DOYSsav8(N)= DOYSsav8(I)
 		    XDSsav (N) = XDSsav (I)
		    XLSsav (N) = XLSsav (I)
		    XLLsav (N) = XLLsav (I)
		    XDLsav (N) = XDLsav (I)
		    XCEsav (N) = XCEsav (I)
		    XEsav  (N) = XEsav  (I)
		    XCsav  (N) = XCsav  (I)
		    YLsav  (N) = YLsav  (I)
		    VVsav  (N) = VVsav  (I)
		    DVVsav (N) = DVVsav (I)
		    GGsav  (N) = GGsav  (I)
		    DGGsav (N) = DGGSav (I)
		end if
	    end do
	    NS = N			! Valid # point in save arrays

C--------
C Pick up all points in the save arrays which are needed for the current
C map. Since all points with XC < XCtest1 have just been discarded, the
C points are only tested for XC <= XCtest2.

	    do I=1,NS
		if (XCsav(I) .le. XCtest2) then
		    NL = NL+1
	            IL1 = iSetFileSpec(cFile)
	            IL1 = iGetFileSpec(FIL__NAME,FIL__TYPE,cFiles)
	    	    cvgfiles(NL) = cFiles
		    SRCVG(NL)= SRCVGsav(I)
		    SRC(NL)  = SRCsav(I)
		    IYRF(NL) = IYRFsav(I)
		    IREC(NL) = IRECsav(I)

		    IYRS(NL) = IYRSsav(I)
		    DOYS8(NL)= DOYSsav8(I)
 		    XDS (NL) = XDSsav (I)
 		    XLS (NL) = XLSsav (I)
 		    XLL (NL) = XLLsav (I)
 		    XDL (NL) = XDLsav (I)
 		    XCE (NL) = XCEsav (I)
 		    XE  (NL) = XEsav  (I)

		    XC  (NL) = XCsav  (I)
		    YL  (NL) = YLsav  (I)
		    VV  (NL) = VVsav  (I)
		    DVV (NL) = DVVsav (I)
		    GG  (NL) = GGsav  (I)
		    DGG (NL) = DGGsav (I)
		endif
	    end do
	end if

C-------
C If no file open then check which file is to be read.
C (bOpen=.TRUE. only happens in AUTO mode. Then also bGetRecord=.FALSE.,
C indicating an unprocessed record is still in memory).
C The data files are assumed to be organized in yearly files: one file
C for each year of data. XCtest1-XCmargin is used to determine which yearly
C file must be opened first.

	if (.not. bOpen) then

	    XCt = XCtest1-XCmargin
	    I = XCt
	    if (I .lt. 1 .or. I .ge. nCar) call Say(cSay,'E','InvXCtest1',
     &		    'start value outside range covered by JDCar')

	    xTT = JDCar(I)+(XCt-I)*(JDCar(I+1)-JDCar(I))
	    call Julian8(1,iYr,Doy8,xTT,JEpoch)
	    iYr = max(iYr,iFrstYr)	! Year of file to be opened

	    iYr = iYr-1			! Will be incremented again in a second
	    bGetRecord = .TRUE.		! Make sure to read record

	end if
C-------
C  Read data until
C  -  end point XCtest2 is reached, or
C  -  EOF is reached, or
C  -  arrays are full
C
C bOpen and bGetRecord are used in subsequent calls.
C If a record has been read succesfully, but is rejected because it's out
C of range (iProc=2) or because the arrays are full (NL>=NLmax), then
C the file is kept open (bOpen=.TRUE.) and bGetRecord is set .FALSE.
C On the next call to ReadVIPSn8_2, the procedure will skip straight to the
C iProcessVIPSn8_2 call to try processing the last record read in the previous
C call before reading the next record from the file that is still open.

	bMore	 = .TRUE.
	bReading = .FALSE.		! Only used to control messages to screen

	do while (bMore)

	    if (.not. bOpen) then	! Open next year's file
		iYr = iYr+1
		iFirst = 1		! Force ReadVIPSn8_2 to initialize record counter
C-------
C Complete the data file name by changing the file extension to the
C proper year. Open data file. If bOpenFile returns 0 the file probably
C didn't exist (exit do while loop). bOpenFile assigns iU if successfully
C opening the file.

		cFile = cWildVIPS	! Replace wildcard by year
		I = Int2Str(iYr,cFile(iFile:iFile+nYrLen-1))
		
		bOpen = bOpenFile(OPN__REOPEN+OPN__TEXT+OPN__READONLY,iU,cFile,iRecl)
		bMore = bOpen		! Open error will exit while loop
	        IL1 = iSetFileSpec(cFile)
	        IL1 = iGetFileSpec(FIL__NAME,FIL__TYPE,cFiles)
	    end if

	    bReadErr = .FALSE.
	    if (bOpen .and. bGetRecord) then
		if (.not. bReading) then
		    call Say(cSay,'I','Reading',cFile)
		    bReading = .TRUE.
		end if
		bMore = iReadVIPSn_2(iSys,iU,iFirst) .eq. 0
		if (bMore) then
		    iFirst = 0		! Record counter has been initialized
		else			! Read error: EOF assumed
		    iU = iFreeLun(iU)
		    bOpen = .FALSE.
		    bReadErr = .TRUE.
		end if

	    end if

	    if (bReadErr) then		! After read error, open new file
		bMore = .TRUE.

	    else if (bMore) then
		iProc = iProcessVIPSn_2(iR,nCar,JDCar,NCoff,XCtest2,Radius,
     &			cSRCVG,cSrc,nYr,Doy8,XCsc,XCobs,xLat,xVV,xDVV,xGG,xDGG,dRA,
     &			rLngSun,rDisSun,rLngP,rLatP,rEloP)

		bMore = iProc .ne. 2		! Check for XCsc past XCtest2
		if (iProc .eq. 1) then		! Record contains valid data with XCsc <= XCtest2

		    call Julian8(10,nYr,Doy8,xTT,JEpoch)
						! All XE (East+West), only positive XE (East) or only negative XE (West)
		    Rdum = (1-abs(iEorW))*abs(rEloP)+iEorW*rEloP

     		    if ((XElow .le. Rdum .and. Rdum .le. XEhigh) .and.
     &			( abs(dRA) .le. XRlim ) .and.
     &			(MJDref .eq. 0 .or. xTT .le. MJDref)
     &		       ) then

			if (XCtest1 .le. XCobs) then

			    if (bAuto) then
			        bSaveOverrun = NS .ge. NSmax
				if (.not. bSaveOverrun) then
				    NS = NS+1
				    SRCVGsav(NS)= cSRCVG
				    SRCsav(NS)	= cSrc
				    IYRFsav(NS) = iYr
				    IRECsav(NS) = iR

				    IYRSsav(NS) = nYr
				    DOYSsav8(NS)= Doy8
				    XDSsav (NS) = rDisSun
				    XLSsav (NS) = rLngSun
				    XLLsav (NS) = rLngP
				    XDLsav (NS) = rLatP
				    XCEsav (NS) = XCsc
				    XEsav  (NS) = rEloP

				    XCsav  (NS) = XCobs
				    YLsav  (NS) = xLat
				    VVsav  (NS) = xVV
				    DVVsav (NS) = xDVV
				    GGsav  (NS) = xGG
				    DGGsav (NS) = xDGG
				end if

			    end if
C--------
C Pick up all points needed for the current map.Test only for XC <= XCtest2
			    if (XCobs .le. XCtest2) then
C--------
C Check what part of line of sight is inside the actual boundaries [XCbeg,XCend]

				if (bLOSCheckLocn8_2(XCbeg,XCend,NLOS,NINSIDE,DLOS,
     &						nYr,Doy8,rLngP,rLatP,XCsc,rLngSun,rDisSun)) then

				    if (NL .lt. NLmax) then

					NL = NL+1
	    	    			cvgfiles(NL) = cFiles
				        SRCVG(NL)= cSRCVG
					SRC(NL)	 = cSrc
					IYRF(NL) = iYr
					IREC(NL) = iR

					IYRS(NL) = nYr		! Time of observation
					DOYS8(NL)= Doy8
					XDS (NL) = rDisSun	! Sun-Earth distance (AU)
					XLS (NL) = rLngSun	! Geocentric ecliptic longitude of Sun (deg)
					XLL (NL) = rLngP	! Geocentric ecliptic lng(P)-lng(Sun)
					XDL (NL) = rLatP	! Geocentric ecliptic lat(P)
					XCE (NL) = XCsc		! Carrington variable sub-Earth point
					XE  (NL) = rEloP
					XC  (NL) = XCobs

					YL  (NL) = xLat
					VV  (NL) = xVV
					DVV (NL) = xDVV
					GG  (NL) = xGG
					DGG (NL) = xDGG

C	write (*,'(I4,I5,7F10.3)') NL,IYRS(NL),Doy8,XC(NL),YL(NL),VV(NL),DVV(NL),GG(NL),DGG(NL)
C	write (*,'(I4,F6.2,4F10.2)') ID,UT,XCtest1,XCobs,XCtest2

				    else
					call Say(cSay,'I','NLmax','Data arrays full: not all data processed')
					bMore = .FALSE.
				    end if
				else
				    LOS = LOS+1

				end if
			    end if
			end if
		    end if
		end if

C-------
C At this point bMore = .FALSE. if iProc=2 (XCsc past XCtest2) or the data arrays are full

		bGetRecord = bMore .or. .not. bGetRecord

	    end if

	end do

	if (.not. bAuto .and. bOpen) then	! If not in AUTO mode, make sure to close open file
	    iU = iFreeLun(iU)
	    bOpen = .FALSE.
	end if

	if (bSaveOverrun) then			! Can only happen in AUTO mode
	    call Say(cSay,'W','NSmax','Save arrays overrun: contents discarded')
	    NS = 0
	    bSaveOverrun = .FALSE.
	end if

	I = 0
	I = I+Int2Str(NL,cFile(I+1:))+1
	I = I+Str2Str('sources in',cFile(I+1:))+1
	I = I+Flt2Str(NCoff+XCtest1,2,cFile(I+1:))
	I = I+Str2Str('-',cFile(I+1:))
	I = I+Flt2Str(NCoff+XCtest2,2,cFile(I+1:))

	if (NLOS .ne. 0) then
	    I = I+2
	    I = I+Int2Str(LOS,cFile(I+1:))+1
	    I = I+Str2Str('; sources rejected',cFile(I+1:))
	end if
	call Say(cSay,'I','# obs',cFile)

	return

	entry ReadVipsLOSCheckn_2(XCbegIn,XCendIn,NLOSIn,NINSIDEIn,DLOSIn)

	XCbeg 	= XCbegIn
	XCend 	= XCendIn
	NLOS  	= NLOSIn
	NINSIDE = NINSIDEIn
	DLOS  	= DLOSIn

	return
	end

	logical function bLOSCheckLocn8_2(XCbeg,XCend,NLOS,NINSIDE,DLOS,iYr,Doy8,rLngLOS,rLatLOS,ObsXC,rLngSun,rDisSun)

	real*8		Doy8
	
	bLOSCheckLocn8_2 = NLOS .eq. 0
	if (bLOSCheckLocn8_2) return

	ObsLng = rLngSun-180.

	Inside = 0
	do J=1,NLOS			! Loop over all segments along LOS
	    P1 = rLngLOS		! Topocentric ecliptic longitude diff. with Earth-Sun line
	    P2 = rLatLOS		! Topocentric ecliptic latitude
	    P3 = (J-.5)*DLOS		! Distance along LOS (units of ObsR)
					! Convert to heliocentric coordinates
	    call POINT_ON_LOS(P1,P2,P3,ELO,ELOS,iEorW)
	    P3 = P3*rDisSun		! Convert to AU

	    P1 = ObsLng+P1		! Heliocentric longitude of P
	    call ECLIPTIC_HELIOGRAPHIC8(0,iYr,Doy8,P1,P2)
					! Convert to heliographic coordinates
	    P1 = XMAP_OBS_POS(ObsXC,P1)	! Modified Carrington variable of P

	    if (XCbeg .le. P1 .and. P1 .le. XCend) Inside = Inside+1
	end do

	bLOSCheckLocn8_2 = Inside .ge. NINSIDE

	return
	end
C+
C >	Structure of Nagoya files:
C	cSrce		string		descriptor for radio source
C	iY,iM,iD	integer 	year, month and day of observation
C	UT		real		universal time of day
C	DP		real		heliocentric distance point P (AU)
C	iTHR		integer		?? (only from 1990 - 1993)
C	iHLA		integer		heliocentric ecliptic latitude point P
C	iHLO		integer 	heliocentric longitude point P relative to
C					Sun-Earth direction (i.e. a difference of
C					ecliptic longitudes)
C	iGLA,iGLO	integer		heliographic location point P (degrees)
C	iROT		integer		Carrington rotation number
C	iVEL		integer		IPS velocity (km/s)
C	iDVEL		integer		error in velocity (km/s)
C	SCINDX/G-VALUE	real		scintillation index (arbitrary units);
C					present from 1990 to 1996) or G-value (after 1997)
C Record length of original Nagoya files:
C	1990-1993: 73 chars
C	1994-1996: 68 chars (no THR column)
C	1997-1998: 66 chars (no THR, g-value instead of source index
C In the processed files, iTHR has been omitted, and the source index has been replaced by
C	zero (i.e. results in a zero g-value)
C-
	function iReadNagoyan8_2(iSys,iU,iFirst)
	character	cMon*3,
     &			cSrce*9,cSrc*9,cSRCVGe*71,cSRCVG*71
	real*8		JDCar(nCar),
     &			dLngSun, dLatSun, dDisSun
        real*8		Doy8
	character	cFmt*41		/'(A9,3I2,F6.2,F5.2,I4,I5,I4,3I5,I4,F8.5)'/

	external	EARTH8
	include		'sun.h'

	save		cSrce,iY,iM,iD,UT,DP,iHLA,iHLO,iGLA,iGLO,iROT,iVEL,iDVEL,GVAL,iRec,cSRCVGe

	iU = iU					! Avoids compiler warning on VMS
	read (iU,cFmt,iostat=iReadNagoyan8_2) cSrce,iY,iM,iD,UT,DP,iHLA,iHLO,iGLA,iGLO,iROT,iVEL,iDVEL,GVAL

	write(cSRCVGe(1:71),cFmt)
     &		cSrce,iY,iM,iD,UT,DP,iHLA,iHLO,iGLA,iGLO,iROT,iVEL,iDVEL,GVAL

	if(iY.lt.50) iY = iY + 100
	iRec = (1-iFirst)*iRec+1		! iFirst=1 initializes record counter

	return

	entry iProcessNagoyan8_2(iR,nCar,JDCar,NCoff,XCend,Radius,
     &		cSRCVG,cSrc,nYr,Doy8,XCsc,XCobs,xLat,xVV,xDVV,xGG,xDGG,dRA,
     &		rLngSun,rDisSun,rLngP,rLatP,rEloP)

	if (iVEL .eq. 0) then			! No velocity available: reject
	    iProcessNagoyan8_2 = 0
	    return
	end if

	iR   = iRec

	cSRCVG = cSRCVGe
	cSrc = cSrce
	nYr  = 1900+iY
	cMon = '   '				! Make sure iM is used
	call DATE_DOY(0,nYr,cMon,iM,iD,iDoy)
	Doy8  = iDoy+dble(UT/24.)

 	XCsc = XMAP_SC_POS8(EARTH8,nYr,Doy8,-nCar,JDCar)

	if (XCsc .eq. BadR4()) then		! Time outside JDCar range
		iProcessNagoyan8_2 = 0
		return
	end if
						! Sub-Earth position
	if (XCsc .gt. XCend) then		! Sub-Earth past XCend
	    iProcessNagoyan8_2 = 2
	    return
	end if

	XCobs = iROT+(360.-iGLO)/360.		! Point-P at DP
	XCobs = XCobs+Sun__Spiral*(Radius-DP)/iVEL ! Point-P traced to Radius
	XCobs = XCobs-NCoff			! Subtract offset

	xLat = iGLA				! Heliographic lat(P)
	xVV  = iVEL				! IPS velocity
	xDVV = iDVEL				! IPS velocity error
	xGG  = GVAL				! Set to -999. if GVAL not available
	xDGG = -999.				! DGVAL not available, set to -999.  

	rLng = iHLO				! Heliocentric ecliptic lng(P)-lng(Earth)
	rLat = iHLA				! Heliocentric ecliptic lat(P)
	rP   = DP				! Distance Sun-P (AU)
	call POINT_ON_LOS(rLng,rLat,rP,rEloSun,rEloP,i)
c	call PointOnLos(rLng,rLat,rP,rEloSun,rEloP,i)

	rLngP = rLng				! Geocentric ecliptic lng(P)-lng(Sun)
	rLatP = rLat				! Geocentric ecliptic lat(P)

	rEloP = -i*rEloP			! rEloP>0 : East of Sun
						!      <0 : West of Sun
	call SunNewcomb8(0,nYr,Doy8,dLngSun,dLatSun,dDisSun)
	rDisSun = dDisSun
	rLngSun = dLngSun			! Geocentric ecliptic lng(Sun)
	rSun    = rLngSun

	rLng = rSun+rLng			! Geocentric ecliptic lng(P)
	Doy  = sngl(Doy8)
	call ECLIPTIC_EQUATOR(0,nYr,Doy,rLng,rLat) ! rLng = RA of P

	rLat = dLatSun				! Geocentric ecliptic lat(Sun)
	call ECLIPTIC_EQUATOR(0,nYr,Doy,rSun,rLat) ! rSun = RA of Sun

	dRA = rLng-rSun				! RA(P)-RA(Sun)
	if (abs(dRA) .gt. 180.) dRA = dRA-sign(1.,dRA)*360.

	iProcessNagoyan8_2 = 1

	return
	end
C+
C NAME:
C	iReadOotyn8_2
C PROCEDURE:
C >	Structure of Ooty files:
C	iY,iM,iD	integer 	year, month and day of observation
C	IST		integer		time of observation in IST - hr and min
C					(the longitude difference between
C					Greenwich and the IST longitude is 82.5
C					deg corresponding to 5.5 hrs time difference. 
C	UT		real		universal time (should be IST-5.5)
C	cSrce		string		descriptor for radio source
C	iRA50(3)	integer		1950 Right Ascension (h,m,s)
C	iDEC50(3)	integer		1950 declination (deg,m,s)
C	iRA(3)		integer		Right Ascension at equinox of date
C	iDEC(3)		integer		declination at equinox of date
C	ELO		real		source elongation (deg)
C	SIZE		real		angular size of the compact component
C					of the source (seconds of arc)
C	iVEL		integer		IPS velocity (km/s)
C	AR		real		axial ratio of the solar wind density
C					irregularities
C	SN		real		signal to noise ratio of the spectrum
C	SCINDX		real		scintillation index
C	GVAL		real		disturbance factor
C
C From an email by Janardhan Padmanabhan:
C
C I have selected only those sources in this list with elongations
C greater than or equal to 10 deg (the turnover in the m vs epsilon 
C curve at 327 MHz is around 10 deg) and signal to noise greater than or 
C equal to 10db. However, please note that source sizes derived for 
C sources with S/N less than 15 db will have larger errors.  Therefore 
C if source sizes are an important input in your program, then use 
C only data with S/N greater than or equal to 15 db.
C
C NOTE: 
C for 8 observations in the file I was unable to extract the scintillation 
C indicies. 7 of these are the august observations. I've marked these in the
C list with a "N/A" (not available) in the last column. 
C-
	function iReadOotyn8_2(iSys,iU,iFirst)
	character	cMon*3,
     &			cSrce*8,cSrc*8,cSRCVGe*111,cSRCVG*111
	real*8		JDCar(nCar),
     &			dLngSun, dLatSun, dDisSun
     	real*8		Doy8
	integer		iRA50 (3), iRA (3),
     &			iDEC50(3), iDEC(3)
	external	EARTH8
	save		iY,iMO,iD,iH,iMI,UT,cSrce,iRA50,iDEC50,iRA,
     &			iDEC,ELO,SIZE,iVEL,SN,SCINDX,GVAL,iRec,cSRCVGe
	include		'sun.h'

	iU = iU
	read (iU,'(2X,3I2,2X,2I2,F8.3,2X,A8,1X,2(1X,6I3),F7.2,F7.3,I6,F6.1,2F7.3)',
     &		iostat=iReadOotyn8_2) iY,iMO,iD,iH,iMI,UT,cSrce,iRA50,iDEC50,iRA,
     &		iDEC,ELO,SIZE,iVEL,SN,SCINDX,GVAL

	write(cSRCVGe(1:111),'(2X,3I2,2X,2I2,F8.3,2X,A8,1X,2(1X,6I3),F7.2,F7.3,I6,F6.1,2F7.3)')
     &		iY,iMO,iD,iH,iMI,UT,cSrce,iRA50,iDEC50,iRA,iDEC,ELO,SIZE,iVEL,SN,SCINDX,GVAL

	if (iY .lt. 50) iY = iY+100

	iRec = (1-iFirst)*iRec+1		! iFirst=1 initializes record counter

	return
C+
C NAME:
C	iProcessOotyn8_2
C CALLING SEQUENCE:
	entry iProcessOotyn8_2(iR,nCar,JDCar,NCoff,XCend,Radius,
     &		cSRCVG,cSrc,nYr,Doy8,XCsc,XCobs,xLat,xVV,xDVV,xGG,xDGG,dRA,
     &		rLngSun,rDisSun,rLngP,rLatP,rEloP)
C-
	if (iVEL .le. 0) then			! No velocity available: reject
	    iProcessOotyn8_2 = 0			! (never happens?)
	    return
	end if

	cSRCVG = cSRCVGe
	cSrc = cSrce
	nYr  = 1900+iY
	cMon = '   '				! Make sure iMO is used
	call DATE_DOY(0,nYr,cMon,iMO,iD,iDoy)
	Doy8 = iDoy+dble(UT/24.)
C	Doy8 = dble(float(iDoy)) + dble(float(iH)/24.0 + float(iMI)/1440. - 5.5/24.) ! Installed 08/04/2008 BVJ
	XCsc = XMAP_SC_POS8(EARTH8,nYr,Doy8,-nCar,JDCar)

	if (XCsc .eq. BadR4()) then		! Time outside JDCar range
		iProcessOotyn8_2 = 0
		return
	end if
						! Sub-Earth position
	if (XCsc .gt. XCend) then		! Sub-Earth past XCend
	    iProcessOotyn8_2 = 2
	    return
	end if

	iR   = iRec

	call SunNewcomb8(0,nYr,Doy8,dLngSun,dLatSun,dDisSun)
						! Geocentric eclip. coord(Sun)
	rDisSun = dDisSun
	rLngSun = dLngSun			! Geocentric eclip. lng(Sun)

	rLng = dLngSun
	rLat = dLatSun				! Geocentric ecliptic lat(Sun)
	call ECLIPTIC_EQUATOR8(0,nYr,Doy8,rLng,rLat)
	dRA = rLng				! RA (Sun)

	rLng = (float(iRA(1))+(float(iRA(2))+float(iRA(3))/60.)/60.)*15.	! RA (P) (deg)
	if(iDEC(1).lt.0.or.iDEC(2).lt.0.or.iDEC(3).lt.0) then
	  if(iDEC(3).lt.0) then
	    rLat = float(iDEC(3))/3600.0
	  else
	    if(iDEC(2).lt.0) then
	      rLat = float(iDEC(2))/60.0 - float(iDEC(3))/3600.0
	    else
	      rLat = float(iDEC(1)) - float(iDEC(2))/60.0 - float(iDEC(3))/3600.0
	    end if
	  end if
	else
	  rLat = float(iDEC(1)) + (float(iDEC(2)) + float(iDEC(3))/60.)/60.	! Decl (P) (deg)
	end if

C	rLng  = (iRA(1)+(iRA(2)+iRA(3)/60.)/60.)*15.	! RA (P) (deg)
C	rLat  = iDEC(1)+(iDEC(2)+iDEC(3)/60.)/60.	! Decl (P)

	dRA = rLng-dRA				! RA(P)-RA(Sun)
	if (abs(dRA) .gt. 180.) dRA = dRA-sign(1.,dRA)*360.

	call ECLIPTIC_EQUATOR8(1,nYr,Doy8,rLng,rLat)
						! Geocentric eclip. coord(P)
	rLng  = rLng-rLngSun
	rLngP = rLng				! Geocentric ecliptic lng(P)-lng(Sun)
	rLatP = rLat				! Geocentric ecliptic lat(P)

	rP = max(0.25, cosd(rLng)*cosd(rLat) )	! Geocentric distance P (at least 0.25 AU)
	call POINT_ON_LOS(rLng,rLat,rP,rEloP,EloSun,i)
						! Heliocentric coord(P) rel. Sun
c	print *, ELO,rEloP, ELO-rEloP

	rP    = rP*dDisSun			! Dist. Sun-P (AU)
	rEloP = i*rEloP				! rEloP>0 : East of Sun
						!      <0 : West of Sun
	rLng = rLngSun+180.+rLng		! Heliocentric eclip. lng(P)
	call ECLIPTIC_HELIOGRAPHIC8(0,nYr,Doy8,rLng,rLat)
						! Heliographic coord(P)
	xLat = rLat				! Heliographic lat(P)

	XCobs = XMAP_OBS_POS(XCsc,rLng)		! Point-P at rP
	XCobs = XCobs+(Radius-rP)/iVEL*SUN__SPIRAL ! Point-P traced to Radius

	xVV  = iVEL				! IPS velocity
	xDVV = -999.				! No velocity error. Set to -999.
	xGG  = GVAL
	xDGG = -999.				! No g-value error. Set to -999.

	iProcessOotyn8_2 = 1

	return
	end
C+
C NAME:
C	iReadGen8_2
C PROCEDURE:
C >	Structure of Gen files from Nagoya:
C	iY,iM,iD	integer*8 	year, month and day of observation in UT (yyyymmdd)
C	ih,im,is	integers	hour, minute, sec of observation start in UT (hh:mm:ss)
C	Site		string		STEL327
C	cSrce		string		descriptor for radio source
C	iRA(3)		integers	hour, minute, sec in J2000 coordinateC	  save		iY,iMO,iD,iH,iMI,UT,cSrce,iRA50,iDEC50,iRA,
C     &			iDEC,ELO,SIZE,iVEL,SN,SCINDX,GVAL,iRec,cSRCVGe
C	  save		DP,iHLA,iHLO,iGLA,iGLO,iROT,iDVEL

C              cFmt*40 '(A9,3I2,F6.2,F5.2,I4,I5,I4,3I5,I4,F8.5)'/s (hh mm ss)
C	iDEC(3)		integers	deg, minute, sec in J2000 coordinates (deg mm ss)
C	Limb		string		solar limb of source (E - east, W - west)
C	Dist		real		P-point distance (in solar radii)
C	Lat.		real		Latitude of radio source p-point (in degrees)
C	PA		real		Ecliptic Position angle  (in degrees)
C	Elong		real		elongation of the radio source (in degrees)
C	iVel1		integer		IPS single-site velocity (km/s)
C	idVel1		integer		IPS single-site velocity error (km/s)
C	gval1		real		IPS single-site g-value (at Toyokawa)
C	gdval1		real		IPS single-site g-value error (at Toyokawa)
C	Method1		string		descriptor indicating single or multi site
C	iVel3		integer		IPS three-site velo	read (iU,'(4I,2I2,1X,3(I2,1X)city (km/s)
C	idVel3		integer		IPS single-site velocity error (km/s)
C	gval3		real		IPS single-site g-value (at Toyokawa)
C	gdval3		real		IPS single-site g-value error (at Toyokawa)
C	Method3		string		descriptor indicating single or multi site
C
C These are set up currently for the new Nagoya files. When we have a standard format, hopefully Ooty, MEXART, and the KSWC will follow this. 
C-
	function iReadGen8_2(iSys,iU,iFirst)
	character	cMon*3,
     &			cSrce*8,cSrc*8,cSRCVGe*111,cSRCVG*111
	character	Aunfor*162, AunforP*162
	character	onespace*1/' '/
	character	name1*4
	character*3	cRA(3),cDEC(3)
	character*4	ciVel1						! This may be changed to an integer some day
	character*7	cSite,cdiVel1,cgdval1,cdgval3			! These may all be changed to integers some day
	real*8		JDCar(nCar),
     &			dLngSun, dLatSun, dDisSun
     	real*8		Doy8
	real*8		JD1,JEpoch1,JD2,JEpoch2,Doy18,Doy28
	integer		iRA (3), iRA2(3),
     &			iDEC(3),iDEC2(3)
	external	EARTH8
	save		iY,iMO,iD,iH,iMI,UT,cSrce,iRA50,iDEC50,iRA,
     &			iDEC,ELO,SIZE,iVEL,SN,SCINDX,GVAL,iRec,cSRCVGe
	save		DP,iHLA,iHLO,iGLA,iGLO,iROT,iDVEL
	include		'sun.h'

	idvel3 = -999

	read (iU,'(I4,2I2,1X,3(I2,1X),A7,1X,A8,2X,3I3,3I3,26X,
     &		20X,F7.3,17X,I7)',
C     &		F6.1,F2.0,A5,A7,F7.3,A7,10X,2I7,F7.3,A7)',
     &		iostat=iReadGen8_2) iY,iMO,iD,iH,iMI,iSI,csite,cSrce,iRA2(1),iRA2(2),iRA2(3),iDEC2(1),iDEC2(2),IDEC2(3),
     &		gval1,ivel3
C     &		ELO,SIZE,ciVEL1,cdiVel1,gval1,cgdval1,ivel3,idvel3,gval3,cdgval3

	print *, 'Now writing the general formatted data'
	write(*,'(I4,2I2,1X,3(I2,1X),A7,1X,A8,2X,3I3,3I3,26X,
     &		20X,F7.3,17X,2I7)')

C     &		F6.1,F2.0,A5,A7,F7.3,A7,10X,2I7,F7.3,A7)') 
     &		iY,iMO,iD,iH,iMI,iSI,csite,cSrce,iRA2(1),iRA2(2),iRA2(3),iDEC2(1),iDEC2(2),IDEC2(3),
     &		gval1,ivel3,idvel3
C     &		ELO,SIZE,ciVEL1,cdiVel1,gval1,cgdval1,ivel3,idvel3,gval3,cdgval3

	if(name1.ne.'name') then				! In the case this is KSWC data, or the file has a header
C
C	  Precess from IRA2000, DEC2000 to present, end up with the present-day IRA, IDEC
C
	  UT  = float(iH) + (float(IMI) + 2.0)/60.0 + float(iSI)/3600.0		! The + 2.0 min is to get to the midpoint of the observation
 	  iY2 = iY		
	  call Precession_Drive(3,2000,0.0D0,iY2,0.0D0,iMO,iD,UT,iRA2,iDEC2,iRA,iDEC,cRA,cDEC)
C
          if(iSys .eq. 4 .or. iSys .eq. 5 .or. iSys .eq. 6) then			! Nagoya, KSWC, or MEXART Data - standard format

	    iVEL  = ivel3								! These may change
	    iDVEL = idvel3
	    GVAL  = gval1

	    write(cSRCVGe(1:66),'(A8,1X,3I3,1X,3I3,1X,I4,2I2,F7.3,1X,F5.3,I5,I5,F6.3)')
     &		cSrce,iRA(1),iRA(2),iRA(3),iDEC(1),iDEC(2),iDEC(3),iY,iMO,iD,UT,SIZE,iVEL,iDVEL,GVAL

C	    write(*,            '(A8,1X,3I3,1X,3I3,1X,I4,2I2,F7.3,1X,F5.3,I5,I5,F6.3)')
C     &		cSrce,iRA(1),iRA(2),iRA(3),iDEC(1),iDEC(2),iDEC(3),iY,iMO,iD,UT,SIZE,iVEL,iDVEL,GVAL

	  end if

C          if(isys.eq.5) then										! KSWC data
C	    write(cSRCVGe(1:92),'(A8,1X,3I3,1X,F6.2,F5.2,19X,I4,1X,I4,5X,F7.5,I6,2I3,I4,2I3)')
C     &		cSrce, iY, iMO, iD, UT, ELO, ICROT, iVel, GVAL, iRA(1), iRA(2), iRA(3), iDEC(1), iDEC(2), iDEC(3)

C	    write(*,'(I4,1X,A8,1X,3I3,1X,F6.2,F5.2,1X,A1,1X,I4,1X,I4,5X,F7.5,I4,2I3,I4,2I3)')
C     &		iRec, cSrce, iY, iMO, iD, UT, ELO, '-', ICROT, iVel, GVAL, iRA50(1), iRA50(2), iRA50(3), iDEC50(1), iDEC50(2), iDEC50(3)
C	    write(*,'(59X,I4,2I3,I4,2I3)') iRA(1), iRA(2), iRA(3), iDEC(1), iDEC(2), iDEC(3)
C	  end if

	end if

	iRec = (1-iFirst)*iRec+1		! iFirst=1 initializes record counter

	return
C+
C NAME:
C	iProcessGen8_2
C CALLING SEQUENCE:
	entry iProcessGen8_2(iR,nCar,JDCar,NCoff,XCend,Radius,
     &		cSRCVG,cSrc,nYr,Doy8,XCsc,XCobs,xLat,xVV,xDVV,xGG,xDGG,dRA,
     &		rLngSun,rDisSun,rLngP,rLatP,rEloP)
C-
C	print *, 'iVEL =',iVEL
C	if (iVEL .eq. 0) then			! No velocity available: reject
C	    iProcessGen8_2 = 0		
C	    return				! We can't do this check if the same entry is used for density - BVJ 9/16/2014
C	end if

	cSRCVG = cSRCVGe
	cSrc = cSrce
	nYr  = iY
	cMon = '   '				! Make sure iMO is used
	call DATE_DOY(0,nYr,cMon,iMO,iD,iDoy)
	Doy8 = iDoy+dble(UT/24.)

	XCsc = XMAP_SC_POS8(EARTH8,nYr,Doy8,-nCar,JDCar)

	if (XCsc .eq. BadR4()) then		! Time outside JDCar range
		iProcessGen8_2 = 0
		return
	end if
						! Sub-Earth position
	if (XCsc .gt. XCend) then		! Sub-Earth past XCend
	    iProcessGen8_2 = 2
	    return
	end if

	iR   = iRec

	call SunNewcomb8(0,nYr,Doy8,dLngSun,dLatSun,dDisSun)
						! Geocentric eclip. coord(Sun)
	rDisSun = dDisSun
	rLngSun = dLngSun			! Geocentric eclip. lng(Sun)

	rLng = dLngSun
	rLat = dLatSun				! Geocentric ecliptic lat(Sun)
	call ECLIPTIC_EQUATOR8(0,nYr,Doy8,rLng,rLat)
	dRA = rLng				! RA (Sun)

	rLng = (float(iRA(1))+(float(iRA(2))+float(iRA(3))/60.)/60.)*15.	! RA (P) (deg)
	if(iDEC(1).lt.0.or.iDEC(2).lt.0.or.iDEC(3).lt.0) then
	  if(iDEC(3).lt.0) then
	    rLat = float(iDEC(3))/3600.0
	  else
	    if(iDEC(2).lt.0) then
	      rLat = float(iDEC(2))/60.0 - float(iDEC(3))/3600.0
	    else
	      rLat = float(iDEC(1)) - float(iDEC(2))/60.0 - float(iDEC(3))/3600.0
	    end if
	  end if
	else
	  rLat = float(iDEC(1)) + (float(iDEC(2)) + float(iDEC(3))/60.)/60.	! Decl (P) (deg)
	end if

C	rLng  = (iRA(1)+(iRA(2)+iRA(3)/60.)/60.)*15.	! RA (P) (deg)
C	rLat  = iDEC(1)+(iDEC(2)+iDEC(3)/60.)/60.	! Decl (P)

	dRA = rLng-dRA				! RA(P)-RA(Sun)
	if (abs(dRA) .gt. 180.) dRA = dRA-sign(1.,dRA)*360.

	call ECLIPTIC_EQUATOR8(1,nYr,Doy8,rLng,rLat)
						! Geocentric eclip. coord(P)
	rLng  = rLng-rLngSun
	rLngP = rLng				! Geocentric ecliptic lng(P)-lng(Sun)
	rLatP = rLat				! Geocentric ecliptic lat(P)

	rP = max(0.25, cosd(rLng)*cosd(rLat) )	! Geocentric distance P (at least 0.25 AU)
	call POINT_ON_LOS(rLng,rLat,rP,rEloP,EloSun,i)
						! Heliocentric coord(P) rel. Sun
C	print *, ELO,rEloP, ELO-rEloP

	rP    = rP*dDisSun			! Dist. Sun-P (AU)
	rEloP = i*rEloP				! rEloP>0 : East of Sun
						!      <0 : West of Sun
	rLng = rLngSun+180.+rLng		! Heliocentric eclip. lng(P)
	call ECLIPTIC_HELIOGRAPHIC8(0,nYr,Doy8,rLng,rLat)
						! Heliographic coord(P)
	xLat = rLat				! Heliographic lat(P)

	XCobs = XMAP_OBS_POS(XCsc,rLng)		! Point-P at rP
	XCobs = XCobs+(Radius-rP)/iVEL*SUN__SPIRAL ! Point-P traced to Radius

	xVV  = iVEL				! IPS velocity
	xDVV = iDVEL				! IPS velocity error
	xGG  = GVAL				! IPS g-value
	xDGG = -999.				! IPS g-value error, now zero

	iProcessGen8_2 = 1

	return
	end
C ******************************************************************************
C+
C+
C >	Structure of UCSD files:
C	iY		integer		year-1900
C	IDOY		integer		day of year
C	IH,IM		integer		time of day
C	cSrce		string		descriptor for radio source
C	IQUAL		integer		quality indicator
C	VEL		real		IPS velocity (km/s)
C	DVEL		real		error in velocity (km/s)
C	VELpeak		real		IPS peak velocity (km/s)
C	DVELpeak	real		error in peak velocity (km/s)
C	SCALE		real		spatial scale of IPS pattern (km)
C	ISITE		integer		site parameter
C	ELO		real		Sun-Source angle (elongation; deg)
C	ILONG		integer		Carrington longitude point P (mapped to Sun)
C	XC		real		modified Carrington variable point P (x100) (mapped to Sun)
C	HLAT		real		heliographic latitude point P (deg)
C	ILONGe		real		Carrington longitude sub-Earth point
C	XCe		real		modified Carrington variable sub-Earth point (x100)
C
C NOTES:
C	ILONG = (1-(XC-int(XC)))*360.
C	XCe = XMAP_SC_POS8(EARTH8,nYr,Doy8,nCar,JDCar)
C	Both appear to be true to within 2 degree in longitude, or so.
C
C       For large elongation the point P was probably taken at a fixed distance
C       from the Earth. In principle, the distance can be reconstructed from
C       ILONG and HLAT by reversing the traceback to the solar surface.
C       I tried to do this for a number of elongations above 90 degrees and
C       found point-P distances close to 0.3 AU:
C	Elongation    Dist (AU)			Elongation    Dist (AU)
C	91.00000      0.3017747			115.0000      0.3055834
C	92.00000      0.3120534			116.0000      0.2949757
C	93.00000      0.3077490			117.0000      0.2932528
C	94.00000      0.3076252			121.0000      0.2888033
C	95.00000      0.2509942			122.0000      0.2779170
C	96.00000      0.2847953			123.0000      0.2820099
C	97.00000      0.2842951			124.0000      0.2752993
C	98.00000      0.2923858			125.0000      0.2946435
C	99.00000      0.3047100			126.0000      0.2908247
C	100.0000      0.2982190			127.0000      0.2950612
C	101.0000      0.2989129			128.0000      0.2925285
C	102.0000      0.3032739			129.0000      0.2909675
C	103.0000      0.2805566			130.0000      0.2826101
C	104.0000      0.2944881			131.0000      0.2786521
C	105.0000      0.2971804			132.0000      0.3057403
C	106.0000      0.2883710			133.0000      0.2940556
C	107.0000      0.3060628			134.0000      0.2966698
C	108.0000      0.2852099			135.0000      0.2718044
C	110.0000      0.2984203			136.0000      0.272730
C	111.0000      0.2984303			137.0000      0.276677
C	112.0000      0.2980161			140.0000      0.2886397
C	113.0000      0.2884684
C	114.0000      0.2940932
C-
	function iReadUCSDn8_2(iSys,iU,iFirst)
	character	cSrce*9,cSrc*9,cSRCVGe*115,cSRCVG*115
	real*8		JDCar(nCar),
     &			dLngSun, dLatSun, dDisSun
     	real*8		Doy8
	external	EARTH8
	save		iY,iD,iH,iM,cSrce,iQUAL,VEL,DVEL,R1,R2,R3,I1,
     &			ELO,ILON,XCobs_in,HLAT,RLONsc,XCsc_in,iRec,cSRCVGe
	include		'sun.h'

	iU = iU
	read (iU,'(2I4,I3,I2,A,I3,5F9.1,I2,F7.1,I4,F9.1,2F7.1,F9.1)',iostat=iReadUCSDn8_2)
     &		iY,iD,iH,iM,cSrce,iQUAL,VEL,DVEL,R1,R2,R3,I1,
     &		ELO,ILON,XCobs_in,HLAT,RLONsc,XCsc_in

	write(cSRCVGe(1:115),'(2I4,I3,I2,A,I3,5F9.1,I2,F7.1,I4,F9.1,2F7.1,F9.1)')
     &		iY,iD,iH,iM,cSrce,iQUAL,VEL,DVEL,R1,R2,R3,I1,
     &		ELO,ILON,XCobs_in,HLAT,RLONsc,XCsc_in

	XCobs_in = XCobs_in/100.

	iRec = (1-iFirst)*iRec+1		! iFirst=1 initializes record counter

	return

	entry iProcessUCSDn8_2(iR,nCar,JDCar,NCoff,XCend,Radius,
     &		cSRCVG,cSrc,nYr,Doy8,XCsc,XCobs,xLat,xVV,xDVV,xGG,xDGG,dRA,
     &		rLngSun,rDisSun,rLngP,rLatP,rEloP)

	if (iQUAL .lt. 21) then			! Data not good enough: reject
	    iProcessUCSDn8_2 = 0
	    return
	end if

	iR   = iRec

	cSRCVG = cSRCVGe
	cSrc = cSrce
	nYr  = 1900+iY
	Doy8  = iD+dble((iH+iM/60.)/24.)
	XCsc = XMAP_SC_POS8(EARTH8,nYr,Doy8,-nCar,JDCar) ! Sub-Earth position

	if (XCsc .eq. BadR4()) then		! Time outside JDCar range
		iProcessUCSDn8_2 = 0
		return
	end if

	if (XCsc .gt. XCend) then		! Sub-Earth past XCend
	    iProcessUCSDn8_2 = 2
	    return
	end if

	XCobs = XCobs_in+(Radius-Sun__RAu)/VEL*Sun__Spiral-NCoff
						! Point-P at Radius
	xLat = HLAT				! Heliographic lat(P)
	xVV  = VEL				! IPS velocity (km/s)
	xDVV = DVEL				! IPS velocity error (km/s)
	xGG  = 0.				! No disturbance factor
	xDGG = 0.				! No disturbance factor error

	call SunNewcomb8(0,nYr,Doy8,dLngSun,dLatSun,dDisSun)
	rDisSun = dDisSun
	rLngSun = dLngSun			! Geocentric ecliptic lng(Sun)
	rSun    = rLngSun
C-------
C POINT_ON_LOS calculates the elongation based on the DP value assumed here.
C The cut-off at 0.30 appears to be the best choice and reproduces the
C elongations on the input file to within a few degrees for most sources.

	DP = max(0.30,cosd(ELO))		! Geocentric distance P (at least 0.3 AU)

	DP = 1.+DP*DP-2*DP*cosd(ELO)		! Cos-rule in Earth-Sun-P
	DP = sqrt(max(0.,DP))			! Distance Sun-P (AU)

	rLng = XCobs_in+(DP-Sun__RAu)/VEL*Sun__Spiral-NCoff
	rLng = rLng-int(rLng)			! Fractional Carrington rotation
	rLng = 360.*(1.-rLng)			! Heliographic lng(P)
	rLat = HLAT				! Heliographic lat(P)
	call ECLIPTIC_HELIOGRAPHIC8(1,nYr,Doy8,rLng,rLat)
						! rLng = Heliocentric ecliptic lng(P)
						! rLat = Heliocentric ecliptic lat(P)
	rLng = rLng-(180.+rSun)			! Heliocentric ecliptic lng(P)-lng(Earth)
	rP   = DP				! Distance Sun-P (AU)
	call POINT_ON_LOS(rLng,rLat,rP,rEloSun,rElo,i)

c	print *, ELO,rElo,ELO-rElo

	rLngP = rLng				! Geocentric ecliptic lng(P)-lng(Sun)
	rLatP = rLat				! Geocentric ecliptic lat(P)
	rEloP = -i*ELO				! rEloP>0 : East of Sun
						!      <0 : West of Sun
	rLng = rSun+rLng			! Geocentric ecliptic lng(P)

	Doy	= sngl(Doy8)
	call ECLIPTIC_EQUATOR(0,nYr,Doy,rLng,rLat) ! rLng = RA of P

	rLat = dLatSun				! Geocentric ecliptic lat(Sun)
	call ECLIPTIC_EQUATOR(0,nYr,Doy,rSun,rLat) ! rSun = RA of Sun

	dRA = rLng-rSun				! RA(P)-RA(Sun)
	if (abs(dRA) .gt. 180.) dRA = dRA-sign(1.,dRA)*360.

	iProcessUCSDn8_2 = 1

	return
	end
