CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
C
C
C  NAME : READUMP_L3AL_DATA.F
C
C  USAGE : THIS PROGRAM READS UARS LEVEL 3AL DATA FILES DISTRIBUTED 
C          FROM THE GSFC DAAC.  TO RUN THIS PROGRAM, TYPE IN THE PROGRAM 
C          NAME. THE PROGRAM WILL PROMPT YOU FOR THE METADATA FILE NAME
C          (*MERA EXTENSION).
C          EXAMPLE: READUMP_L3AL_DATA 
C                   PLEASE ENTER MATA DATA FILE NAME BELLOW :
C                   MLS_L3AL_STEMP_D0006.V0001_C03_META
C
C  DESCRIPTION : THIS PROGRAM FIRST OPENS THE METADATA FILE (*.*META) 
C                TO GET THE DATA FILE NAME (*.*PROD), AND ITS PHYSICAL 
C                RECORD SIZE.  THE DATA FILE IS THEN OPENED, AND ITS
C                CONTENTS ARE READ AND DUMPED TO THE SCREEN.
C
C  AUTHOR : KE - JUN SUN   HUGHES_STX
C
C  DATE : FEBRUARY 2, 1994
C
C  LIMITATIONS : THIS PROGRAM AND THE DATA IT READS ARE DESIGNED FOR 
C              MACHINES RUNNING A UNIX OPERATING SYSTEM, AND HAVING  
C              32-BIT ARCHITECTURE. IEEE FLOATING POINT AND/OR INTEGER  
C              VALUES WILL NOT TRANSLATE CORRECTLY ON OTHER MACHINES!
C
C  NOTES : SINCE THIS PROGRAM USES THE METADATA FILE (*.*META) TO GET 
C          THE DATA FILE NAME (*.*PROD) AND RECORD SIZE, BOTH THE DATA 
C          FILE AND ITS CORRESPONDING METADATA FILE MUST BE IN THE SAME 
C          DIRECTORY.
C          NAGWare (nag_decs and nag_pfort) AND ftnchek WERE USED
C          ON THIS PROGRAM TO ENHANCE NETWORK PORTABILITY.
C
C  ALGORITHM INFORMATION : NO CALCULATION IS PERFORMED IN THIS PROGRAM.
C
C  VERSION : 0
C
C  TO COMPILE THIS PROGAM :
C         f77 -o READUMP_L3AL_DATA READUMP_L3AL_DATA.F
C       or for SUN machines
C         f77 -DSUNF -o READUMP_L3AL_DATA READUMP_L3AL_DATA.F
C     EXECUTABLE CODE WILL BE NAMED READUMP_L3AL_DATA
C
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC

C     OPEN META DATA FILE FIRST

C     .. PARAMETERS ..
      INTEGER MAXLEN
      PARAMETER (MAXLEN=796)
      INTEGER MAXDAT
      PARAMETER (MAXDAT=178)
C     ..
C     .. LOCAL SCALARS ..
      CHARACTER SBTYPE*12,INSTMT*12,HDR*48,TEMP*4
      CHARACTER DATAFN*170,METAFN*170,LINE*255,NREC*8
      INTEGER ACTUAL,I,IOSTT,IREC,IROW,K,RECSIZ,RLEN,START,STINDX,TOTAL
      INTEGER PHYREC, NUMREC
C     ..
C     .. LOCAL ARRAYS ..
      CHARACTER BUFFER(MAXLEN)
      INTEGER TIME(2)
      REAL DATA(MAXDAT),LTLNSL(4)
C     ..

      DO 666 I = 1,170
       DATAFN(I:I) = ' '
 666  CONTINUE

C
C     ** REMOVE THE C-COMMENT BELOW IF USING PROGRAM OUT OF SCRIPT
C     PRINT *,' PLEASE ENTER META DATA FILE NAME BELOW: '
      READ (5,FMT='(A)') METAFN

      OPEN (UNIT=20,FILE=METAFN,STATUS='OLD',ERR=991)

C   SKIP 9 LINES IN META FILE

      DO 111 IROW = 1,9
          READ (20,FMT=50) LINE
  111 CONTINUE

C   GET DATA FILE NAME FROM 10TH LINE OR FROM META DATA FILE NAME

      READ (20,FMT=50) LINE
C      DO 112 I = 1,70
C          DATAFN(I:I) = LINE(14+I:14+I)
C  112 CONTINUE

      I = 1
      DO 777 WHILE (METAFN(I:I).NE.' ')
       I = I + 1
 777  CONTINUE
      I = I - 4
      DO 888 K = 1,I
        DATAFN(K:K) = METAFN(K:K)
 888  CONTINUE
      DATAFN(I:I+4) = 'PROD'

C   SKIP ONE LINE

      READ (20,FMT=50) LINE

C   GET DATA FILE RECORD SIZE

      READ (20,FMT=50) LINE
   50 FORMAT (A80)

      READ (LINE(17:19),FMT=333) RECSIZ
  333 FORMAT (I3)

C   CLOSE META FILE

      CLOSE (20)

C----------------------------------------------------------
C  NOTE: SGI AND HP READ 4 BYTE WORDS, SUN READS 1 BYTE.
C        ADDED BY JAMES JOHNSON 09/27/94.                  
C----------------------------------------------------------
#ifndef SUNF
      RLEN=RECSIZ/4
#endif
#ifdef SUNF
      RLEN=RECSIZ
#endif

      OPEN (UNIT=10,FILE=DATAFN,STATUS='OLD',ERR=992,FORM='UNFORMATTED',
     +     ACCESS='DIRECT',RECL=RLEN)

      PRINT *,'********************************************'
      PRINT *,' READING   ',DATAFN
      PRINT *,'********************************************'
      READ (10,REC=1,IOSTAT=IOSTT,ERR=993) (BUFFER(K),K=1,RECSIZ)
      WRITE (6,FMT=*) ' RECORD  1     DESCRIPTOR'
      WRITE (6,FMT=*) '-------------------------------'
      WRITE (6,FMT=*) ' SFDU T(Z) AND L(Z) '
      WRITE (6,FMT=*) '-------------------------------'
      WRITE (6,FMT=*) ' RECORD KEY : ', (BUFFER(K),K=1,20)
      WRITE (6,FMT=*) ' CONTROL ID : ', (BUFFER(K),K=21,24)
      WRITE (6,FMT=*) ' VERSION ID : ',BUFFER(25)
      WRITE (6,FMT=*) ' CLASS ID   : ',BUFFER(26)
      WRITE (6,FMT=*) ' DATA DESCRIPTIVE RECORD ID : ',
     +  (BUFFER(K),K=29,32)
      WRITE (6,FMT=*) ' LENGTH     : ', (BUFFER(K),K=33,40)
      WRITE (6,FMT=*) '-------------------------------'
      WRITE (6,FMT=*) ' SFDU T(I) AND L(I) '
      WRITE (6,FMT=*) '-------------------------------'
      WRITE (6,FMT=*) ' CONTROL ID : ', (BUFFER(K),K=41,44)
      WRITE (6,FMT=*) ' VERSION ID : ',BUFFER(45)
      WRITE (6,FMT=*) ' CLASS ID   : ',BUFFER(46)
      WRITE (6,FMT=*) ' DATA DESCRIPTIVE RECORD ID : ',
     +  (BUFFER(K),K=49,52)
      WRITE (6,FMT=*) ' LENGTH     : ', (BUFFER(K),K=53,60)
      READ (10,REC=2,IOSTAT=IOSTT,ERR=994) (BUFFER(K),K=1,RECSIZ)
      WRITE (6,FMT=*) '-------------------------------'
      WRITE (6,FMT=*) '         LABEL RECORD'
      WRITE (6,FMT=*) '-------------------------------'
      WRITE (6,FMT=*) ' RECORD KEY    : ', (BUFFER(K),K=1,20)
      WRITE (6,FMT=*) ' SATELLITE ID  : ', (BUFFER(K),K=21,24)
      WRITE (6,FMT=*) ' RECORD TYPE   : ', (BUFFER(K),K=25,26)
      DO 345 I = 1,12
          INSTMT(I:I) = BUFFER(I+16)
          SBTYPE(I:I) = BUFFER(I+38)
  345 CONTINUE
      WRITE (6,FMT=*) ' INSTRUMENT ID : ',INSTMT
      WRITE (6,FMT=*) ' DATA SUBTYPE OR SPECIES : ',SBTYPE
      WRITE (6,FMT=*) ' FORMAT VERSION NUMBER : ', (BUFFER(K),K=51,54)
      WRITE (6,FMT=*) ' PHYSICAL RECORD COUNT : ', (BUFFER(K),K=55,62)
      WRITE (6,FMT=*)
     +  ' NUMBER OF CONTINUATION RECORDS FOR FILE LABEL : ',
     +  (BUFFER(K),K=63,66)
      DO 112 I = 1,8
        NREC(I:I) = BUFFER(I+66)
 112  CONTINUE
      READ(NREC(1:8),FMT=321)NUMREC
 321  FORMAT(I8)
      WRITE (6,FMT=*) ' NUMBER OF PHYSICAL RECORDS IN FILE : ', NUMREC
      WRITE (6,FMT=*) ' FILE CREATION TIME : ', (BUFFER(K),K=75,97)
      WRITE (6,FMT=*) ' YEAR FOR 1ST DATA RECORD        : ',
     +  (BUFFER(K),K=98,100)
      WRITE (6,FMT=*) ' DAY OF YEAR OF 1ST DATA RECORD  : ',
     +  (BUFFER(K),K=101,103)
      WRITE (6,FMT=*) ' MS OF DAY FOR 1ST DATA RECORD   : ',
     +  (BUFFER(K),K=104,111)
      WRITE (6,FMT=*) ' YEAR FOR LAST DATA RECORD       : ',
     +  (BUFFER(K),K=112,114)
      WRITE (6,FMT=*) ' DAY OF YEAR OF LAST DATA RECORD : ',
     +  (BUFFER(K),K=115,117)
      WRITE (6,FMT=*) ' MS OF DAY FOR LAST DATA RECORD  : ',
     +  (BUFFER(K),K=118,125)
      WRITE (6,FMT=*) ' DATA LEVEL : ', (BUFFER(K),K=126,128)
      WRITE (6,FMT=*) ' UARS DAY NUMBER : ', (BUFFER(K),K=129,132)
      WRITE (6,FMT=*) ' NUMBER OF DATA POINTS PER RECORD : ',
     +  (BUFFER(K),K=133,136)
      DO 346 I = 1,4
          TEMP(I:I) = BUFFER(I+136)
  346 CONTINUE

      READ (TEMP,FMT=414)STINDX 
  414 FORMAT (I4)

      WRITE (6,FMT=*) ' BASE INDEX OF DATA POINT VALUE   : ',
     +  STINDX 
      WRITE (6,FMT=*) ' RECORD LENGTH IN BYTES   : ',
     +  (BUFFER(K),K=141,145)
      WRITE (6,FMT=*) ' MIN. LATITUDE FOR RECORD IN FILE : ',
     +  (BUFFER(K),K=146,148)
      WRITE (6,FMT=*) ' MAX. LATITUDE FOR RECORD IN FILE : ',
     +  (BUFFER(K),K=149,151)
      WRITE (6,FMT=*) ' CCB VERSION NUMBER       : ',
     +  (BUFFER(K),K=152,160)
      WRITE (6,FMT=*) ' FILE CYCLE NUMBER        : ',
     +  (BUFFER(K),K=161,165)
      WRITE (6,FMT=*) ' VITUAL FILE FLAG         : ',BUFFER(166)
      WRITE (6,FMT=*) ' TOTAL NUMBER OF TIME/VERSION ENTRIES IN FILE : '
     +  , (BUFFER(K),K=167,170)
      WRITE (6,FMT=*) ' NUMBER OF TIME/VERSION ENTRIES IN RECORD : ',
     +  (BUFFER(K),K=171,174)

      IREC = 2
      DO 445  PHYREC = 1, (NUMREC - 1)

C----------------------------------------------------------
C  NOTE: SGI AND HP USE ERR OPTION, SUN USES END INSTEAD
C        ADDED BY JAMES JOHNSON 09/27/94.                  
C----------------------------------------------------------
#ifdef SUNF
      READ (10,REC=IREC+1,IOSTAT=IOSTT,END=900) HDR,TOTAL,ACTUAL,START,
     +  TIME,LTLNSL, (DATA(K),K=1,2*TOTAL)
#endif
#ifndef SUNF
      READ (10,REC=IREC+1,IOSTAT=IOSTT,ERR=900) HDR,TOTAL,ACTUAL,START,
     +  TIME,LTLNSL, (DATA(K),K=1,2*TOTAL)
#endif

      WRITE (6,FMT=*) '---------------------------------------------'
      WRITE (6,FMT=*) ' RECORD ',IREC,'   DATA RECORD'
      WRITE (6,FMT=*) '---------------------------------------------'
      WRITE (6,FMT=*) ' RECORD KEY    : ',HDR(1:20)
      WRITE (6,FMT=*) ' SATELLITE ID  : ',HDR(21:24)
      WRITE (6,FMT=*) ' RECOORD TYPE  : ',HDR(25:26)
      WRITE (6,FMT=*) ' INSTRUMENT ID : ',HDR(27:38)
      WRITE (6,FMT=*) ' PHYSICAL RECORD COUNT : ',HDR(39:46)
      WRITE (6,FMT=*) ' TOTAL NUMBER POINTS IN RECORD : ',TOTAL
      WRITE (6,FMT=*) ' NUMBER OF ACTUAL POINTS : ',ACTUAL
      WRITE (6,FMT=*) ' STARTING INDEX OF 1ST ACTUAL POINT : ',START
      WRITE (6,FMT=222) TIME(1),TIME(2)
  222 FORMAT ('  RECORD TIME IN UDTF FORMAT: ',I10,I10,/,30X,
     +       '(YEAR DAY, MILLISECONDS)')
      WRITE (6,FMT=*) ' LATITUDE  : ',LTLNSL(1),'(DEGREES)'
      WRITE (6,FMT=*) ' LONGITUDE : ',LTLNSL(2),'(DEGREES)'
      WRITE (6,FMT=*) ' LOCAL SOLAR TIME   : ',LTLNSL(3),'(HOURS)'
      WRITE (6,FMT=*) ' SOLAR ZENITH ANGLE : ',LTLNSL(4),'(DEGREES)'
      WRITE (6,FMT=*)
      IF ((INSTMT.EQ.'HRDI        ').OR.
     +         (INSTMT.EQ.'WINDII      ')) THEN
          WRITE(6,FMT=*)
     + ' INDEX       DATA (M/S)                QUALITY (M/S)^2'
      ELSE IF (SBTYPE.EQ.'TEMP       ') THEN
          WRITE (6,FMT=*)
     + ' INDEX       DATA (DEGREES K)          QUALITY (DEGREES K)'
      ELSE IF ((SBTYPE(1:4) .EQ.'AEXT').OR.
     +          (SBTYPE(1:4) .EQ.'AERO')) THEN
          WRITE (6,FMT=*)
     + ' INDEX       DATA (1/KM)               QUALITY (1/KM)'
      ELSE
          WRITE (6,FMT=*)
     + ' INDEX       DATA (PPMV)               QUALITY (PPMV)'
      END IF

      DO 124 I = 1,TOTAL
          IF ((DATA(I).GT. (-3.402823*10.0**38.0)) .AND.
     +        (DATA(I).LT. (3.402823*10.0**38.0))) THEN
              WRITE (6,FMT=444) I-1+STINDX,DATA(I),DATA(I+TOTAL)
  444         FORMAT (I4,')        ',E,'         ',E)
          ELSE
              WRITE (6,FMT=555) I-1+STINDX
  555         FORMAT (I4,')                    ***  FILL VALUE  ***')
          END IF
  124 CONTINUE
      IREC = IREC + 1
  445 CONTINUE

  900 WRITE (6,FMT=*) ' END OF DATA'
      GO TO 999
  991 WRITE (6,FMT=*) ' CAN NOT OPEN META DATA FILE. '
      GO TO 999
  992 WRITE (6,FMT=*) ' CAN NOT OPEN DATA PRODUCT FILE. '
      GO TO 999
  993 WRITE (6,FMT=*) ' ERROR IN READING 1ST RECORD. ERROR CODE : ',
     +  IOSTT
      GO TO 999
  994 WRITE (6,FMT=*) ' ERROR IN READING 2ND RECORD. ERROR CODE : ',
     +  IOSTT
      GO TO 999
  999 CLOSE (10)
      CONTINUE
      STOP
      END
