      program ceobadcp
      parameter (nrecs=20000,NDATA=NRECS,NumOfBins=18)
!     Last change:  CS    3 May 97   10:36 am
      INTEGER  NumOfBins
      CHARACTER*8 StartDate,EndDate
      CHARACTER*4 StartTime,EndTime
      CHARACTER*6 StationLat
      CHARACTER*7 StationLon
      CHARACTER LatFlag,LonFlag,LatFlag1,LonFlag1
      CHARACTER*42 HEADER1
      CHARACTER*42 HEADER2
      CHARACTER*42 HEADER3
      CHARACTER*42 HEADER4
      CHARACTER*36 HEADER5
      CHARACTER*37 HEADER51
      CHARACTER*41 HEADER8,HEADER9,HEADER10
      CHARACTER*41 HEADER81,HEADER91,HEADER101
      CHARACTER*29 HEADER7
      CHARACTER*30 HEADER71
      CHARACTER*42 HEADER6
      INTEGER Lat,Lon
      INTEGER DEPTH,TDepth,PDepth
      REAL    jday,temp,Echo,Pres,VVel
      INTEGER  yy,mm,dd,hh,mn,ss
      CHARACTER*36 InputFileName
      CHARACTER*12 HVelFileName,VVelFileName,EchoFileName
      CHARACTER*12 PresFileName,TempFileName

      CHARACTER*11 FileListing
      CHARACTER*12 HVelInvFile,VVelInvFile
      CHARACTER*12 TempInvFile,EchoInvFile,PresInvFile
      dimension ic(6)
      dimension data(6,nrecs),d(6,nrecs)
      DIMENSION ihead(34)
      REAL MLLW
      COMMON /HEADERS/StartDate,StartTime,EndDate,EndTime,
     1  StationLat,LatFlag,StationLon,LonFlag,DEPTH
      data ic/ 9, 8, 3, 6 ,7,5/
      data mllw/12.75/
      character d_no
      character*3 ProjectCode
      character*4 DeployNo
      ProjectCode='chd'
      write(*,*)'Enter the deployment number'
      read(5,'(a)') d_no
      
      FileListing='chump'//D_NO//'.lis'
      DeployNo=ProjectCode//D_NO
      HVelInvFile=DeployNo//'hvel.inv'
      TempInvFile=DeployNo//'temp.inv'
      EchoInvFile=DeployNo//'echo.inv'
      PresInvFile=DeployNo//'pres.inv'
      VvelInvFile=DeployNo//'vvel.inv'
      OPEN(31,File=HvelInvFile,form='formatted') 
      OPEN(32,File=VvelInvFile,form='formatted') 
      OPEN(33,File=PresInvFile,form='formatted') 
      OPEN(34,File=TempInvFile,form='formatted') 
      OPEN(35,File=EchoInvFile,form='formatted') 
      HEADER5='YYYY MM DD HH MN SS H_VSpeed  H_VDir'
      HEADER51='                     (cm/s)  (Deg. T)'
      HEADER7='YYYY MM DD HH MN SS    Temp'
      HEADER71='                     (Deg. C)'
      HEADER8 ='YYYY MM DD HH MN SS   Pres               '
      HEADER81='                      (dB)               '
      HEADER9='YYYY MM DD HH MN SS Echo Ints.           '
      HEADER91='                                         '
      HEADER10 ='YYYY MM DD HH MN SS   V. VEL             '
      HEADER101='                      (cm/s)             '

      open(7,file=FIleLIsting,form='formatted',status='old')
      ierr=0
      iunit=8 
      nf=0
100   continue
      read(7,'(a)',end=1000) InputFileName
      do 900 kk=1,6
      ichan=ic(kk)
      open(unit=iunit,file=InputFileName,status='old',
     1 form='unformatted')
C
      call cdfin(iunit,ichan,data,nrecs,ihead,nr)
C The ADCP acoustic transducers were situated approximately one meter
C above the bottom, the bottom of the first ADCP bin is two meters off 
C of the bottom

      DEPTH=DEPTH+1
C
      do 850 ll=1,nr
       d(kk,ll)=data(5,ll)
 850  continue
 900  continue

      write(header1,'(a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a)')
     1 'Station Location: ',StationLat(1:2),' ',
     1 StationLat(3:4),"'",StationLat(5:6),'"',LatFlag,',  ',
     2 StationLon(1:3),' ',StationLon(4:5),"'",StationLon(6:7),'"',
     3 LonFlag

      WRITE(header2,'(a,a,a,a,a,a,a,a,a,a,a)')
     1 'Start Date=',StartDate(1:4),'/',StartDate(5:6),'/',
     1 StartDate(7:8),' Start Time= ',StartTime(1:2),':',StartTime(3:4),
     2'UTC'

      WRITE(header3,'(a,a,a,a,a,a,a,a,a,a,a)')
     1 '  End Date=',EndDate(1:4),'/',EndDate(5:6),'/',
     1 EndDate(7:8),'   End Time= ',EndTime(1:2),':',EndTime(3:4),'UTC'

      WRITE(HEADER4,'(a,F6.1,a)')'Obs. Depth=',real(Depth),
     1 ' M from the sea bottom'
      IF (NF .EQ. 0) THEN
      TDEPTH=DEPTH
      PDEPTH=DEPTH
      ENDIF
      NF=NF+1
      WRITE(HVelFileName,'(A4,A2,I2.2,A4)')DeployNo,'hv',nf,'.txt'
      WRITE(31,102)
     1 HvelFileName,' ',StationLat,LatFlag,' ',
     1 StationLon,LonFlag,' ',StartDate,StartTime,' ',EndDate,EndTime,
     1 ' ',Depth,'M',MLLW,'M'
102   FORMAT(A12,A1,A6,A1,A1,A7,A1,A1,A8,A4,A1,A8,A4,A1,I3.3,A1,F7.2,A1)
      OPEN(21,File=HVelFileName,Form='formatted')
      WRITE(21,'(A42)')header1
      WRITE(21,'(A42)')header2
      WRITE(21,'(A42)')header3
      WRITE(21,'(A41)')header4
      WRITE(21,'(A)')header5
      WRITE(21,'(A)')header51
      
      WRITE(VVelFileName,'(A4,A2,I2.2,A4)')DeployNo,'vv',nf,'.txt'
      WRITE(32,102)
     1 VvelFileName,' ',StationLat,LatFlag,' ',
     1 StationLon,LonFlag,' ',StartDate,StartTime,' ',EndDate,EndTime,
     1 ' ',Depth,'M',MLLW,'M'
      OPEN(22,File=VVelFileName,Form='formatted')
      WRITE(22,'(A42)')header1
      WRITE(22,'(A42)')header2
      WRITE(22,'(A42)')header3
      WRITE(22,'(A41)')header4
      WRITE(22,'(A41)')header10
      WRITE(22,'(A41)')header101

      WRITE(PresFileName,'(A4,A4,A4)')DeployNo,'pres','.txt'
      WRITE(33,102)
     1 PresFileName,' ',StationLat,LatFlag,' ',
     1 StationLon,LonFlag,' ',StartDate,StartTime,' ',EndDate,EndTime,
     1 ' ',Depth,'M',MLLW,'M'
      OPEN(23,File=PresFileName,Form='formatted')
      WRITE(23,'(A42)')header1
      WRITE(23,'(A42)')header2
      WRITE(23,'(A42)')header3
      WRITE(HEADER4,'(a,F6.1,a)')'Obs. Depth=',real(PDepth),
     1 ' M from the sea bottom'
      WRITE(23,'(A41)')header4
      WRITE(23,'(A41)')header8
      WRITE(23,'(A41)')header81

      WRITE(TempFileName,'(A4,A4,A4)')DeployNo,'temp','.txt'
      WRITE(34,102)
     1 TempFileName,' ',StationLat,LatFlag,' ',
     1 StationLon,LonFlag,' ',StartDate,StartTime,' ',EndDate,EndTime,
     1 ' ',Depth,'M',MLLW,'M'
      OPEN(24,File=TempFileName,Form='formatted')
      WRITE(24,'(A42)')header1
      WRITE(24,'(A42)')header2
      WRITE(24,'(A42)')header3
      WRITE(HEADER4,'(a,F6.1,a)')'Obs. Depth=',real(TDepth),
     1 ' M from the sea bottom'
      WRITE(24,'(A41)')header4
      WRITE(24,'(A)')header7
      WRITE(24,'(A)')header71

      WRITE(EchoFileName,'(A4,A2,I2.2,A4)')DeployNo,'ec',nf,'.txt'
      WRITE(35,102)
     1 EchoFileName,' ',StationLat,LatFlag,' ',
     1 StationLon,LonFlag,' ',StartDate,StartTime,' ',EndDate,EndTime,
     1 ' ',Depth,'M',MLLW,'M'
      OPEN(25,File=EchoFileName,Form='formatted')
      WRITE(25,'(A42)')header1
      WRITE(25,'(A42)')header2
      WRITE(25,'(A42)')header3
      WRITE(HEADER4,'(a,F6.1,a)')'Obs. Depth=',real(Depth),
     1 ' M from the sea bottom'
      WRITE(25,'(A41)')header4
      WRITE(25,'(A41)')header9
      WRITE(25,'(A41)')header91


         kk=0
         do 700 k=1,nr
           kk=kk+1
           do 600 i=1,6
             data(i,kk)=data(i,k)
             d(i,kk)=d(i,k)
600        continue
700      continue
      radpdeg=acos(-1.0)/180.
        do 800 kk=1,nr
!         ALPHA=D(2,kk)*RADPDEG
!         AU=D(1,kk)*SIN(ALPHA)
!         AV=D(1,kk)*COS(ALPHA)
          Pres=D(4,kk)
          Temp=D(3,kk)
          VVel=D(5,kk)
          Echo=D(6,kk)
          IYR=IFIX(DATA(1,KK))
          IJD=IFIX(DATA(2,KK))
          IHR=IFIX(DATA(3,KK))
          IMN=IFIX(DATA(4,KK))
        CALL CONJTC (IJD,ICM,ICD,IYR) 
c       write(9,200) 1900+IYR,ICM,ICD,IHR,IMN,ISEC,(d(j,kk),j=1,2) 
!    1                au,av,aw,(d(j,kk),j=4,3,-1),AE
        write(21,200) 1900+IYR,ICM,ICD,IHR,IMN,ISEC,(d(j,kk),j=1,2) 
        write(22,201) 1900+IYR,ICM,ICD,IHR,IMN,ISEC,VVel
        write(23,201) 1900+IYR,ICM,ICD,IHR,IMN,ISEC,Pres
        write(24,201) 1900+IYR,ICM,ICD,IHR,IMN,ISEC,Temp
        write(25,201) 1900+IYR,ICM,ICD,IHR,IMN,ISEC,Echo
 200    format(I4,5I3,2(f9.4) )
 201    format(I4,5I3,f9.4 )
800     continue
      CLOSE (21)
      CLOSE (22)
      CLOSE (23)
      CLOSE (24)
      CLOSE (25)
      goto 100
1000  continue
      stop
      end
      SUBROUTINE CONJTC (IJD,ICM,ICD,IYR) 
C 
C**** THIS SUBROUTINE WILL CONVERT JULIAN DAY(IJD) TO THE 
C**** CORRESPONDING CALENDAR MONTH(ICM) AND CALENDAR DAY(ICD) 
C**** IF IT IS A LEAP YEAR .... USE ILTBLE
C 
      DIMENSION   IDTBLE(12,2),ILTBLE(12,2) 
C 
      DATA ((IDTBLE(I,J),J=1,2),I=1,12)/1,31,32,59,60,90,91,120,121,151,
     1                                  152,181,182,212,213,243,244,273,
     2                                  274,304,305,334,335,365/
      DATA ((ILTBLE(I,J),J=1,2),I=1,12)/1,31,32,60,61,91,92,121,122,152,
     1                                  153,182,183,213,214,244,245,274,
     2                                  275,305,306,335,336,366/
C 
C**** TEST FOR LEAP YEAR
C 
      ISW = 1 
      IF (MOD(IYR,4).EQ.0)   ISW = 2
C 
      L = IJD/30
      IF (L.EQ.0)   GO TO 4 
      GO TO (6,7)   ISW 
    6 IF (IJD.GT.IDTBLE(L,2))   GO TO 4 
      GO TO 8 
    7 IF (IJD.GT.ILTBLE(L,2))   GO TO 4 
    8 ICM = L 
      GO TO 5 
    4 ICM = L + 1 
    5 GO TO (9,10)   ISW
    9 ICD = IJD - IDTBLE(ICM,1) + 1 
      RETURN
   10 ICD = IJD - ILTBLE(ICM,1) + 1 
      RETURN
      END 

      subroutine cdfin(iunit,ichan,data,nrecs,ihead,nr)
C     Purpose:
C      To provide a general purpose subroutine to read a
C      Circulation Data Format data file and retrieve
C      1 column of data plus the date and time for all
C      records in the file.
C      The header record containing 34 words of station
C      information is also returned.
C=====================================================
C==     iunit = Data set reference # for CDF file. (INPUT)
C==     ichan = Channel desired. (INPUT)
C==     data  = Two dimensional data array returned. (OUTPUT)
C               data(1, ) = Time (year)
C                   (2, ) = Time (julian day)
C                   (3, ) = Time (hour)
C                   (4, ) = Time (minute)
C                   (5, ) = Data value
C                   (6, ) = Line number
C==     nrecs = Dimension of data array. (INPUT)
C== ihead(34) = Header record from CDF file. (OUTPUT)
C==        nr = Number of records read. (OUTPUT)
C=======================================================
C==>
      dimension data(6,nrecs),ihead(34)
      dimension xdata(14,50)        
C         Read header record and print it
      call phead(iunit,ihead)
C         Read a data record
      nr=0
 100  read(iunit,end=999) xdata
      do 120 k=1,50
      if(xdata(1,k).eq.-10.) then
      go to 999
      endif
      nr=nr+1
      if(nr.gt.nrecs) then
      print*,' !!! Dimension of data exceeded !!!'
      print*,'     in subroutine cdfin.  Subroutine'
      print*,'     HALTS.'
      print*,' Current dimension is ',nrecs
      stop
      endif
      data(1,nr) = xdata(14,k)
      data(2,nr) = xdata(13,k)
      data(3,nr) = xdata(12,k)
      data(4,nr) = xdata(11,k)
      data(5,nr) = xdata(ichan,k)
      data(6,nr) = xdata(1,k)
 120  continue
      go to 100
 999  close(iunit)
      return
      end
      subroutine phead(iunit,ihead)
C      IHEAD(1) =1h1  
C      IHEAD(2) =1h1  
C      IHEAD(3) =1h1  
C      IHEAD(4) = Station name (left justified)
C      IHEAD(5) =  "
C      IHEAD(6) =  "
C      IHEAD(7) = Meter Serial number
C      IHEAD(8) = Meter reference number
C      IHEAD(9) = Latitude (direction)
C      IHEAD(10) = Latitude (degrees)
C      IHEAD(11) = Latitude (minutes)
C      IHEAD(12) = Latitude (seconds)
C      IHEAD(13) = Longitude (direction)
C      IHEAD(14) = Longitude (degrees)
C      IHEAD(15) = Longitude (minutes)
C      IHEAD(16) = Longitude (seconds)
C      IHEAD(17) = Time of the first data point MINUTE
C      IHEAD(18) = Time of the first data point HOUR    
C      IHEAD(19) = Time of the first data point DAY 
C      IHEAD(20) = Time of the first data point YEAR
C      IHEAD(21) = Time of the last data point MINUTE
C      IHEAD(22) = Time of the last data point HOUR
C      IHEAD(23) = Time of the last data point DAY
C      IHEAD(24) = Time of the last data point YEAR
C      IHEAD(25..32) = 0
C      IHEAD(33) = Depth of the instrument
C      IHEAD(34) = 0
C
      CHARACTER*8 StartDate,EndDate
      CHARACTER*4 StartTime,EndTime
      CHARACTER*6 StationLat
      CHARACTER*7 StationLon
      CHARACTER LatFlag,LonFlag,LatFlag1,LonFlag1
      INTEGER DEPTH
      COMMON /HEADERS/StartDate,StartTime,EndDate,EndTime,
     1  StationLat,LatFlag,StationLon,LonFlag,DEPTH
      save icall
      dimension ihead(34)
      data icall/0/
C==> Read header record of cdf file.
      read(iunit)ihead
      if (icall .eq. 0) then
      write(6,21)(IHEAD(NZ),NZ=1,33)
      icall = icall + 1
      end if
      write(StationLat,'(I2.2,I2.2,I2.2)') IHEAD(10),IHEAD(11),IHEAD(12)
      write(StationLon,'(I3.3,I2.2,I2.2)') IHEAD(14),IHEAD(15),IHEAD(16)
      write(LatFlag,'(A1)') IHEAD(9)
      write(LonFlag,'(A1)') IHEAD(13)
          IYR=IHEAD(20)
          IJD=IHEAD(19)
          IHR=IHEAD(18)
          IMN=IHEAD(17)
        CALL CONJTC (IJD,ICM,ICD,IYR) 
      WRITE(StartDate,'(I4,2I2.2)')IYR+1900,ICM,ICD
      WRITE(StartTime,'(2I2.2)')IHR,IMN
          IYR=IHEAD(24)
          IJD=IHEAD(23)
          IHR=IHEAD(22)
          IMN=IHEAD(21)
        CALL CONJTC (IJD,ICM,ICD,IYR) 
      WRITE(  EndDate,'(I4,2I2.2)')IYR+1900,ICM,ICD
      WRITE(  EndTime,'(2I2.2)')IHR,IMN
      DEPTH=IHEAD(33)
   21 FORMAT( ' SUMMARY OF HEADING INFORMATION FOR THIS FILE',//,              
     1' DATA GENERATION DATE ',A4,A4,A2,/,' S T A T I O N --     '            41
     *  ,A4,A4,A2,//,
     2' METER SERIAL NUMBER ',I7,/,' METER REFERENCE NUMBER ',I4,//,          41
     3' LATITUDE,  DIRECTION = ',A1,', DEGREES = ',I3,', MINUTES = ',I2,      41
     4', SECONDS = ',I2,/,' LONGITUDE, DIRECTION = ',A1,', DEGREES = ',       41
     5I3,', MINUTES = ',I2,', SECONDS = ',I2,//,' START TIME, ',              41
     6'MINUTES = ',I2,', HOURS = ',I2,', DAYS = ',I3,', YEAR  = ',            41
     7I2,/,' STOP  TIME, MINUTES = ',I2,', HOURS = ',I2,', DAYS = ',          41
     8I3,', YEAR  = ',I2,//,                                                  41
     9' TIME ANCHOR DROPPED, MINUTES =',I3,', HOURS =',I3,', DAYS =',I4,      41
     1', YEARS =',I3,/,' TIME ANCHOR RELEASE, MINUTES =',I3,', HOURS =',      41
     2I3,', DAYS =',I4,', YEARS =',I3,//,' DEPTH OF METER =',I5,' FEET,       41
     3IF NEGATIVE, FEET FROM THE SURFACE',/,29X,                              41
     4'IF POSITIVE, FEET FROM THE BOTTOM'//)                                  41
C                                                                             41
      RETURN                                                                 41
      END                                                                     41
