Notes:
- I have used a cutdown 'include' file because:
  - some compilers have the nice behaviour of telling you what variables
    are not used (so redundancy in declarations causes a flood of complier
    messages)
  - it is more flexible and modular to return data through arguments than
    through common, and it is wasteful to include commons (or variables)
    which are not going to be used.
 
- mixed-case names: I can see the value in this - by making things more
  readable. My concern is though that most Fortran doesn't recognise case,
  (MyVaR is myvar to the compiler - and programmers might fail to notice
  conflicting names because they look so different).

- could generalise further by passing in MXOB as an argument
 
 
 
 

c READGTSPP   routine to read the header and temperature profile from a
c             single-station unformatted GTSPP file
c
c USAGE:   call readgtspp(lun,path,stn,ndep,dep,dQC,t,tQC)
c
c INPUTS:
c      lun  - logical unit number to use for input file
c      path - string containing path to head of data storage area (leave off
c             trailing '/',   acceptable minimum is '.')
c      stn  - character string station number
c
c OUTPUTS:
c      ndep - number of depths in T profile.   TEST: -1 = read error!
c      dep  - vector of depths             [length MXOB]
c      dQC  - vector of depth QC flags     [length MXOB]
c      t    - vector of temperatures       [length MXOB]
c      tQC  - vector of t QC flags         [length MXOB]
c
c PARAMETER:   MXOB = 10000
c
c RELATED ROUTINES:
c      Test program:  drive_read.f
c      ... stuff to find out station numbers ...
c
c COMPILATION:
c      f77 readgtspp.f
c      Requires include file  "GTSPP_dec.inc"
c
c FURTHER WORK***
c   - access to .ed & .raw files??
c   - read other data?
c
c MODIFICATIONS:
c
c AUTHOR:  Jeff Dunn 9/4/99      Copyright   CSIRO Marine Research
c
c $Id: $
c-----------------------------------------------------------------------

      subroutine readgtspp(lun,path,stn,ndep,dep,dQC,t,tQC)
      implicit none
      integer MXOB
      parameter (MXOB=10000)
      integer lun, ndep, ios
      character path*(*), stn*(*)
      character*1 dQC(MXOB), tQC(MXOB)
      real dep(MXOB), t(MXOB)
      integer i, ii, j, ist, k, nos_seg

      include 'GTSPP_dec.inc'

 
      call openfl(path,stn,lun,ios)
      if(ios.ne.0)goto 999

      read(lun,err=999) key, Mky, One_Deg_Sq,
     &  Cruise_ID, Obs_Year, Obs_Month, Obs_Day, Obs_Time, Data_Type,
     &  Iumsgno , Stream_Source, Uflag, MEDS_Sta,
     &  Latitude, Longitude, Q_Pos, Q_Date_Time, Q_Record,
     &  Up_date, Bul_Time, Bul_Header, Source_ID, Stream_Ident,
     &  QC_Version, Data_Avail, No_Prof, NParms, Nsurfc, Num_Hists

      read(lun,err=999) (no_seg(ii), Prof_Type(ii), Dup_Flag(ii),
     &  Digit_Code(ii), Standard(ii), Deep_Depth(ii), ii=1,No_Prof)
 
c     Return if this is a duplicate record!  No read error, but do not use,
c     so set ndep=0 to indicate empty profile

      if(dup_flag(1).eq.'D')then
         ndep = 0
         return
      endif

c     If certain records are present, read them to get them out of the way

      if( NParms.gt.0 )then
         read(lun,err=999)
      endif
      if( Nsurfc.gt.0 )then
         read(lun,err=999)
      endif
      if( Num_Hists.gt.0 )then
         read(lun,err=999)
      endif

c     Add up number of segments

      nos_seg = 0
      do k=1,No_Prof
         nos_seg = nos_seg+no_seg(k)
      enddo

c     Read through all segments, collecting just the temperature profiles

      ist = 1
      do j=1,nos_seg
         read(lun,err=999) Mky, One_Deg_Sq, Cruise_ID, Obs_Year,
     &   Obs_Month, Obs_Day, Obs_Time, Data_Type, Iumsgno,
     &   Profile_Type(1), Profile_Seg(1), No_Depths(1), D_P_Code(1)

         if( profile_type(1).eq.'TEMP' )then
            ndep = ist + No_Depths(1) - 1
            read(lun,err=999) (dep(i),dQC(i),t(i),tQC(i),i=ist,ndep)
            ist = ndep+1
         endif
      enddo

      return

c     Error return; set ndep=-1 as an error flag
999   continue
      ndep = -1
      return
      end

c-----------------------------------------------------------------------
c OPENFL  routine to construct name of a single-station GTSPP file,
c          and attempt to open that file.

      subroutine openfl(path,stn,lun,ios)
      implicit none
      integer lun, ios, ll, mm, ii
      character path*(*), stn*(*), fname*120

c     Construct file name by adding stn string to end of path, inserting
c     '/' between each pair of non-blank digits in stn

      ll = 0
      mm=index(path,' ')-1       ! Set counter to last character in path
      fname(1:mm) = path(1:mm)

      do ii=1,len(stn)
         if( stn(ii:ii).ne.' ' )then   ! NOT a blank character
            ll = ll+1
            if( ll.gt.0 .and. ll-2*(ll/2).eq.1 )then    ! ll=1,3,5,7,...
               mm=mm+1
               fname(mm:mm) = '/'
            endif
            mm=mm+1
            fname(mm:mm) = stn(ii:ii)
         endif
      enddo

      open(unit=lun,file=fname(1:mm)//'.ed',form='unformatted',
     &     status='old',iostat=ios)
      if( ios.ne.0 )then
         type *,'Cannot open file',fname(1:mm)
      endif

      return
      end

c---------------------------------------------------------------------------