	PROGRAM READ_AMES_SUN_ARCHIVE_v02
C
C		10 March 2000
C
C		This FORTRAN program reads and decodes a Gaines and Hipskind
C		(1994) formatted file containing data derived from measurements
C		taken during TARFOX (summer 1996) with the NASA Ames 6-channel
C		airborne sunphotometer (AATS-6) from aboard the University of 
C		Washington C-131A.  Note that this program reads only Version 2.0 
C		archived files that were submitted to the NASA Langley DAAC in 
C		March, 2000.  This program prompts the user for the name of the data
C		file to be read.  Values for the following parameters are stored
C		in arrays:
C
C		  TIMEUTCHR			time [decimal hours UTC]
C		  RLATITUDE			latitude [deg]
C		  RLONGITUDE		longitude [deg]
C		  PRESSALTKM		pressure altitude [km]
C		  GEOMALTKM			geometric altitude [km]
C		  PRESSMB			static pressure [mb]
C		  AIRMASS			relative airmass
C		  WVL				filter center wavelength [nm]
C		  DETECTOR_VOLTS		detector voltage
C		  DETECTOR_VOLTS_940	detector voltage at about 941-nm
C		  WATVAPCOL			water vapor column content in [cm]
C		  UNCWATVAPCOL		uncertainty in water vapor col content in [cm]
C		  TAUTOT			total optical depth
C		  TAURAY			Rayleigh optical depth
C		  TAUPART			particulate optical depth
C		  UNCTAUP			absolute unc in part opt depth
C	
C		The program uses a paramter IPRINT to output decoded data.
C		For IPRINT=1, it will output to the screen only the
C		first 20 records of the data file.  For IPRINT=2, it will
C		write a new condensed data file (REWRSUNARC.WRT) in a format
C		that can be imported easily into a spreadsheet program.
C		This format contains on each line the number of the observation,
C		decimal time in UTC, latitude in deg, longitude in deg,
C		aircraft pressure in mb, pressure altitude in km, geometric altitude
C		in km, airmass, water vapor column content in cm, and particulate
C		optical depths at NWL.
C
C		It is left to the user to modify this routine as necessary
C		for integration into his/her own computer analysis routines.
C
C		USAGE NOTES
C		-----------
C		Missing data values are set equal to 99999 in the original file.
C		After decoding, these values have decimal values of all 9s.
C
C		
        DIMENSION WVLNM(14),VSCAL(14),ASCAL(14),
     1    DETECTOR_VOLTS(10000,14),TAUTOT(10000,14),TAURAY(10000,14),
     2    TAUPART(10000,14),UNCTAUP(10000,14),TIMEUTCHR(10000),
     3    RLATITUDE(10000),RLONGITUDE(10000),PRESSALTKM(10000),
     4    PRESSMB(10000),AIRMASS(10000),DETECTOR_VOLTS_940(10000),
     5    WATVAPCOL(10000),UNCWATVAPCOL(10000),GEOMALTKM(10000),DX(2)
	INTEGER*4 VMISS(14),AMISS(14),NVOLT(14),NTAUTOT(14),NTAURAY(14),
     1    NTAUPART(14),NUNCTAUP(14)
	BYTE FILESUNREAD(36)
	CHARACTER*80 LINE,VNAME(14),ANAME(14),INVESTIGATOR_NAME,ORG_NAME,
     1    INSTRUMENT_NAME,XNAME(2)
	CHARACTER*6 MISSION_NAME
	PARAMETER (IPRINT=2)
C
	TYPE *, 'INPUT NAME OF ARCHIVED SUNPHOTOMETER DATA FILE TO READ'
	ACCEPT 6, NCHARREAD,(FILESUNREAD(I),I=1,NCHARREAD)
C
	OPEN(UNIT=8,FILE=FILESUNREAD,ACCESS='SEQUENTIAL',STATUS='OLD',
     1    FORM='FORMATTED',READONLY)
C
C		Read number of lines in the header (NLHEAD) and the 
C		File Format Index (IFFI).
C
	READ(8,*) NLHEAD,IFFI
C
C		Read investigator names, organization name, instrument source
C		name, mission name
C
	READ(8,9) INVESTIGATOR_NAME
	READ(8,9) ORG_NAME
	READ(8,9) INSTRUMENT_NAME
	READ(8,9) MISSION_NAME
C
C		Read date of data acquisition into IYR,IMO,IDA, and date that
C		the archive file was created into JYR_ARC, JMO_ARC, JDA_ARC
C
	READ(8,*) IYEAR,IMO,IDA,JYEAR,JMO_ARC,JDA_ARC
	IYR = IYEAR-(IYEAR/100)*100
	JYR_ARC = JYEAR-(JYEAR/100)*100
C
C		Read interval between values of independent variables
C		(=0.0 for nonuniform interval).
C
	READ(8,*) DX(1),DX(2)
C
C		Skip a line.
C
	READ(8,*) NX	
C
C		Read number of sunphotometer channels (NWL), bandpass center
C		wavelengths in nanometers (WVLNM)
C
	READ(8,*) NWL
	READ(8,*) (WVLNM(I),I=1,NWL)
C
C		Read names of two independent variables
C
	READ(8,9) XNAME(1)
	READ(8,9) XNAME(2)
C
C		Read number of primary variables (NV)
C
	READ(8,*) NV
C
C	       For primary variables:
C		Read scale factors (VSCAL)
C		Read missing or erroneous data values (VMISS)
C
	READ(8,*) (VSCAL(I),I=1,NV)	
	READ(8,*) (VMISS(I),I=1,NV)
C
C		Read primary variable names (VNAME)
C
	DO J = 1,NV
	 READ(8,9) VNAME(J)
	END DO
C	
C		Read number of auxiliary variables (NAUXV)
C
	READ(8,*) NAUXV
C
C	       For auxiliary variables:
C		Read scale factors (ASCAL)
C		Read missing or erroneous data values (AMISS)
C
	READ(8,*) (ASCAL(I),I=1,NAUXV)	
	READ(8,*) (AMISS(I),I=1,NAUXV)
C
C		Read auxiliary variable names (ANAME)
C
	DO J = 1,NAUXV
	 READ(8,9) ANAME(J)
	END DO
C
C		Read number of special comment lines (NSCOML)
C
	READ(8,*) NSCOML
C
	IF(NSCOML.GE.1) THEN
	 DO J = 1,NSCOML
	  READ(8,9) LINE
	 END DO
	END IF
C
C		Read number of normal comment lines (NNCOML)
C
	READ(8,*) NNCOML
C
C		These lines contain values used for ozone and NO2 column content; 
C		earth-sun distance; relative uncertainties in airmass,
C		Rayleigh optical depth, ozone optical depth, and nitrogen
C		dioxide optical depth; absolute uncertainty in instantaneous
C		measured voltages; values for ozone and nitrogen dioxide
C		optical depths, zero-airmass intercept voltages and associated
C		uncertainties, level of cloud screening used, special notes pertaining
C		to the data in each particular file.
C
	IF(NNCOML.GE.1) THEN
	 DO J = 1,NNCOML
	  READ(8,9) LINE
	 END DO
	END IF
C
C		Read the data
C
	DO 1000 JOBS = 1,10000
C
	READ(8,*,END=1050) NUTCSECS,NLAT,NLON,NPRESSMB,NPRESSALTM,
     1    NGEOMALTM,NAIRMASS,NVOLT940,NWATVAPCOLCM,NUNCWATVAPCOLCM
C
C		Apply scale factors as necessary to values above
C
C
	TIMEUTCHR(JOBS) = FLOAT(NUTCSECS)/86400. * 24.
	RLATITUDE(JOBS) = ASCAL(1)*FLOAT(NLAT)
	RLONGITUDE(JOBS) = ASCAL(2)*FLOAT(NLON)

	IF (MISSION_NAME.EQ.'TARFOX') THEN
	  PRESSMB(JOBS) = ASCAL(3)*FLOAT(NPRESSMB)
	  PRESSALTKM(JOBS) = ASCAL(4)*0.001*FLOAT(NPRESSALTM)
	  GEOMALTKM(JOBS) = ASCAL(5)*0.001*FLOAT(NGEOMALTM)
	END IF

	AIRMASS(JOBS) = ASCAL(6)*FLOAT(NAIRMASS)
	DETECTOR_VOLTS_940(JOBS) = ASCAL(7)*FLOAT(NVOLT940)
	WATVAPCOL(JOBS) = ASCAL(8)*FLOAT(NWATVAPCOLCM)
	UNCWATVAPCOL(JOBS) = ASCAL(9)*FLOAT(NUNCWATVAPCOLCM)
C
	READ(8,*) (NVOLT(L),L=1,NWL)	
	READ(8,*) (NTAUTOT(L),L=1,NWL)	
	READ(8,*) (NTAURAY(L),L=1,NWL)	
	READ(8,*) (NTAUPART(L),L=1,NWL)	
	READ(8,*) (NUNCTAUP(L),L=1,NWL)	
C
C		Apply scale factors to values above
C
	  DO L = 1,NWL
		DETECTOR_VOLTS(JOBS,L) = VSCAL(1)*NVOLT(L)
		TAUTOT(JOBS,L) = VSCAL(2)*NTAUTOT(L)
		TAURAY(JOBS,L) = VSCAL(3)*NTAURAY(L)
		TAUPART(JOBS,L) = VSCAL(4)*NTAUPART(L)
		UNCTAUP(JOBS,L) = VSCAL(5)*NUNCTAUP(L)
	  END DO
C
1000	CONTINUE
C
1050	CONTINUE
	NOBS = JOBS - 1
	TYPE *,' END OF FILE REACHED AFTER READING ',NOBS,' OBSERVATIONS'
C
		IF(IPRINT.EQ.1) THEN
	DO IOBS = 1,MIN0(NOBS,20)
C
	 IF (MISSION_NAME.EQ.'TARFOX') THEN
	  TYPE 22, IOBS,TIMEUTCHR(IOBS),RLATITUDE(IOBS),
     1      RLONGITUDE(IOBS),PRESSMB(IOBS),PRESSALTKM(IOBS),
     2      GEOMALTKM(IOBS),AIRMASS(IOBS),DETECTOR_VOLTS_940(IOBS),
     3      WATVAPCOL(IOBS),UNCWATVAPCOL(IOBS)
	 END IF
C
	 DO L = 1,NWL
	   TYPE 23, WVLNM(L),DETECTOR_VOLTS(IOBS,L),TAUTOT(IOBS,L),
     1       TAURAY(IOBS,L),TAUPART(IOBS,L),UNCTAUP(IOBS,L)
	 END DO
	END DO
C
		ELSE IF(IPRINT.EQ.2) THEN
C
	OPEN(UNIT=44,FILE='REWRSUNARC.WRT',TYPE='NEW',ACCESS='SEQUENTIAL',
	1 FORM='FORMATTED')
C
	DO IOBS = 1,NOBS
	 IF (MISSION_NAME.EQ.'TARFOX') THEN
	  WRITE(44,44) IOBS,TIMEUTCHR(IOBS),RLATITUDE(IOBS),
     1      RLONGITUDE(IOBS),PRESSMB(IOBS),PRESSALTKM(IOBS),
     2      GEOMALTKM(IOBS),AIRMASS(IOBS),WATVAPCOL(IOBS),
     3      (TAUPART(IOBS,L),L=1,NWL)
	 END IF
	END DO
		END IF
C
	STOP
C
6	FORMAT(Q,80A1)
9	FORMAT(A)
22      FORMAT(1X,I5,2X,F8.5,2X,F8.3,2X,F8.3,2X,F6.1,2(2X,F6.3),F6.3,2X,
     1     F6.3,2X,F6.3,2X,F6.2)
23	FORMAT(1X,F6.1,3X,F6.3,4(3X,F7.3))
24	FORMAT(1X,I5,2X,F8.5,2X,F8.3,2X,F8.3,2X,F6.1,2X,F6.3,2X,F6.3,
     1     2X,F6.2)
44	FORMAT(1X,I5,1X,F8.5,3(1X,F8.3),2(1X,F5.0),1X,F8.5,1X,F8.2,
     1     5(1X,F6.3))
	END
