	program carter
c	Courtesy Dr. Meirion Jones
c	MIAS Institute of Oceanographic Sciences
c	Bidston Observatory, Birkenhead Merseyside, Liverpool, UK
        INTEGER D,DCOR
        print*, 'ECHO-SOUNDING CORRECTIONS......'
      print*, 'Depths corrected from echo-travel depths to true meters'
      print*,'according to Carter''s 3rd Edition of Matthews'' Tables'
      print*,'Enter depth <= zero to terminate program'
      OPEN(4,file='matt_tab.ddd',form='unformatted',access='direct',
     1	recl=4)
      OPEN(3,file='matt_tab.aaa',form='unformatted',access='direct',
     1	recl=4)
  1     write(*,'(a)') ' Latitude (deg,min) ? '
        read(*,*,err=999) lad,lam
        if (iabs(lad) .gt. 90) go to 999
        write(*,'(a)') ' Longitude (deg,min)? '
        read(*,*,err=999) lod,lom
        if (iabs(lod) .gt. 360) go to 999
        CALL FINAR(LAD,LAM,LOD,LOM,IAREA)
        print*, ' Data fall in area ',IAREA
  2     write(*,'(a)') ' Depth in uncorr. meters? '
        read(*,*,err=2) D
        IF(D .LT. 0) GO TO 999
        IF (D .EQ. 0) GO TO 1
        CALL DCORR(D,IAREA,DCOR)
        print*, ' Corrected depth is ',DCOR
        write(*,'(a)')
     1        ' Enter 1 for another depth, or 0 to get new location...'
        read(*,*,err=999) id
        IF(ID.EQ.1) GO TO 2
        GO TO 1
 999    STOP
        END
C            ************  SUBROUTINE FINAR  ************                      
C                                                                              
C                                                                              
C  THIS FILE CONTAINS A  LISTING,  IN  STANDARD  FORTRAN  FORMAT,  OF  A       
C  SUBROUTINE  (SUBROUTINE  FINAR) WHICH WILL RETURN THE CORRECTION AREA       
C  NUMBER APPLICABLE TO SUPPLIED VALUES OF LATITUDE AND  LONGITUDE  WHEN       
C  USED  IN  CONJUNCTION WITH A DIRECT ACCESS FILE CREATED FROM THE DATA       
C  SUPPLIED IN FILE 3 OF THIS TAPE.                                            
C                                                                              
C  WITH THE EXCEPTION OF DIRECT ACCESS 'READ' STATEMENTS, THE SUBROUTINE       
C  HAS  BEEN  WRITTEN  IN ANSI STANDARD FORTRAN, AND SHOULD THEREFORE BE       
C  COMPATIBLE WITH ALL MACHINES ON WHICH THE USE OF DIRECT ACCESS  FILES       
C  IS  SUPPORTED.  THE SYNTAX OF THE DIRECT ACCESS 'READ' STATEMENTS MAY       
C  VARY ON DIFFERENT INSTALLATIONS, HOWEVER, AND THE USER IS ADVISED  TO       
C  ENSURE THAT THESE LINES OF THE SUBROUTINE (4 IN TOTAL) CONFORM TO THE       
C  STANDARD USED ON THE SPECIFIC MACHINE ON WHICH THE FACILITY IS USED.        
C                                                                              
C  IF THE USE OF DIRECT ACCESS FILES IS  NOT  SUPPORTED  ON  THE  USER'S       
C  INSTALLATION,  THEN  A  SUITABLE  ALTERNATIVE  (USING  AN  ARRAY OR A       
C  SEQUENTIAL FILE, POSSIBLY CONTAINING ONLY A  GEOGRAPHICAL  SUBSET  OF       
C  THE AVAILABLE DATA) MUST BE IMPLEMENTED.                                    
C                                                                              
C  THE SUBROUTINE CHECKS THAT LATITUDE AND LONGITUDE VALUES SUPPLIED ARE       
C  WITHIN  THE  PERMISSIBLE  RANGE  (90  DEGREES  SOUTH  TO LESS THAN 90       
C  DEGREES NORTH; 540 DEGREES WEST TO 540 DEGREES EAST  [PERMITTING  ONE       
C  COMPLETE  360 DEGREE 'WRAPAROUND' IN LONGITUDE IN EITHER DIRECTION]),       
C  AND THAT THE ARITHMETIC SIGNS OF DEGREES AND SUBDIVISIONS OF  DEGREES       
C  ARE  COMPATIBLE; AN ERROR CODE IS RETURNED IF ANY OF THESE CONDITIONS       
C  IS NOT MET. LONGITUDE IS ADJUSTED IF NECESSARY TO BRING I   
C  +180 DEGREES (WEST NEGATIVE, EAST POSITIVE).       
C  THE ONE DEGREE LATITUDE BAND IS THEN COMPUTED AND USED AS A KEY  INTO       
C  THE  INDEX  SECTION  OF THE DIRECT ACCESS FILE. THE APPROPRIATE INDEX       
C  POINTER INDICATES THE START RECORD OF THE RELEVANT  DATA  SECTION  OF       
C  THE  FILE. THE NEXT INDEX POINTER IN SEQUENCE INDICATES THE BEGINNING       
C  OF THE FOLLOWING DATA SECTION, AND IS THEREFORE USED TO DETERMINE THE       
C  WORKING  DATA AREA (SINCE IT POINTS TO THE END RECORD OF THE RELEVANT       
C  DATA SECTION +  1).  THE  SUBROUTINE  THEN  READS  THROUGH  THE  DATA       
C  EXAMINING  LONGITUDE VALUES UNTIL IT LOCATES THE LONGITUDE BAND WHICH       
C  IS APPROPRIATE  TO  THE  SUPPLIED  LONGITUDE  VALUE  (IN  THE  MANNER       
C  DESCRIBED  FOR FILE 1 OF THIS TAPE),  AND RETURNS THE CORRECTION AREA       
C  NUMBER ASSOCIATED WITH THAT BAND.                                           
C                                                                              
C  COMMENT RECORDS WITHIN THE SUBROUTINE  CODING  IDENTIFY  THE  VARIOUS       
C  STAGES OF THIS PROCESS.                                                     
C                                                                              
C  A SERIES OF SETS OF INPUT VALUES, TOGETHER WITH THE CORRECT  RETURNED       
C  VALUE  FROM THE SUBROUTINE, IS SUPPLIED ON COMMENT RECORDS AT THE END       
C  OF THE SUBROUTINE CODING. THE USER IS ADVISED TO TEST THE  SUBROUTINE       
C  USING  THESE  VALUES  TO ENSURE THAT THE SOFTWARE IS FULLY COMPATIBLE       
C  WITH HIS MACHINE.  THE VALUES HAVE BEEN CHOSEN TO  TEST  THE  VARIOUS       
C  PATHWAYS THROUGH THE SUBROUTINE.                                            
C                                                                              
C                                                                              
      SUBROUTINE FINAR(LATD,LATM,LOND,LONM,IAREA)                              
C                                                                              
C TO USE:  CALL FINAR(LATD,LATM,LOND,LONM,IAREA)                               
C                                                                              
C WHERE:   LATD  =  SIGNED LATITUDE IN INTEGER DEGREES.                        
C          LATM  =  SIGNED INTEGER SUBDIVISION OF DEGREES OF LATITUDE,         
C                   E.G. MINUTES, MINUTES*10, MINUTES*100, HUNDREDTHS          
C                   OF A DEGREE ETC.. SIGN MUST BE THE SAME AS FOR             
C                   LATD, EXCEPT WHEN ZERO.                                    
C          LOND  =  SIGNED LONGITUDE IN INTEGER DEGREES.                       
C          LONM  =  SIGNED INTEGER SUBDIVISION OF DEGREES OF LONGITUDE,        
C                   E.G. MINUTES, MINUTES*10, MINUTES*100, HUNDREDTHS          
C                   OF A DEGREE ETC.. SIGN MUST BE THE SAME AS FOR             
C                   LOND, EXCEPT WHEN ZERO.                                    
C                                                         
C     THE FIELDS 'LATM' AND 'LONM' ARE USED SOLELY TO TEST WHETHER THE         
C     GIVEN POSITION LIES EXACTLY ON THE BOUNDARY OF A ONE DEGREE              
C     SQUARE, AND THEIR FORMATTING IS NOT CRITICAL PROVIDING THAT THEY         
C     HAVE THE SAME SIGN AS FOR 'LATD' AND 'LOND' RESPECTIVELY, EXCEPT         
C     WHEN ZERO.                                                               
C                                                                              
C     THE ABOVE INPUT PARAMETERS ARE UNALTERED ON RETURN                       
C                                                                              
C          IAREA =  RETURNED WITH AREA NUMBER (IN RANGE 1 TO 85), OR           
C                   SET TO -999 IF AN ERROR IS DETECTED IN THE INPUT           
C                   PARAMETER VALUES.                                          
C                                                                              
C                                                                              
C RESTRICTIONS:  LATITUDE LIMITS    -90 TO +90  DEGREES                        
C                LONGITUDE LIMITS  -540 TO +540 DEGREES                        
C                                                                              
C                                                                              
C SIGN CONVENTION:  NORTH AND EAST ARE POSITIVE                                
C                   SOUTH AND WEST ARE NEGATIVE                                
C                                                                              
C                                                                              
C ECHO-SOUNDING CORRECTION AREA DATA ARE STORED IN THE DIRECT ACCESS           
C FILE REFERENCED AS LOGICAL UNIT NUMBER 3                                     
C                                                                              
C                                                                              
      INTEGER   LATD , LATM , LOND , LONM , IAREA                              
      INTEGER   ILAT , ILON , IREC1, IREC2, IREC , KLON , IAREB                
C                                                                              
C CHECK THAT INPUT PARAMETERS HAVE COMPATIBLE SIGNS                            
C                                                                              
      IAREA=(-999)                                                             
      IF(((LATD*LATM).LT.0).OR.((LOND*LONM).LT.0))RETURN                       
C                                                                              
C CHECK FOR LATITUDE OF 90 DEGREES NORTH                                       
C                                                                              
      IF((LATD.EQ.90).AND.(LATM.EQ.0))GOTO 110                                 
C                                                                              
C CHECK THAT INPUT PARAMETERS ARE WITHIN PERMITTED RANGES                      
C                                                                              
      IF(LATM.GE.0)ILAT=1                                                      
      IF(LATM.LT.0)ILAT=(-1)                                                   
      ILAT=LATD+ILAT                                                           
      IF((ILAT.LT.(-90)).OR.(ILAT.GT.90))RETURN                                
      ILON=0                                                                   
      IF(LONM.GT.0)ILON=1                                                      
      IF(LONM.LT.0)ILON=(-1)                                                   
      ILON=LOND+ILON                                                           
      IF((ILON.LT.(-540)).OR.(ILON.GT.540))RETURN                              
C                                                                              
C DETERMINE S-W CORNER OF 1 DEGREE SQUARE CONTAINING POSITION                  
C                                                                              
C ILAT RANGES FROM 1 AT 89 DEGREES NORTH TO 180 AT 90 DEGREES SOUTH            
C                                                                              
      IF(LATD.LT.0.OR.LATM.LT.0)GOTO 10                                        
      ILAT=90-LATD                                                             
      GOTO 20                                                                  
   10 ILAT=90-ILAT                                                             
C                                                                              
C BRING LONGITUDE BETWEEN GREATER THAN OR EQUAL TO -180 DEGREES AND            
C LESS THAN 180 DEGREES                                                        
C                                                                              
   20 ILON=LOND                                                                
      IF((LOND.GT.(-180)).OR.((LOND.EQ.(-180)).AND.(LONM.EQ.0)))GOTO 30        
      ILON=ILON+360                                     
   30 IF((ILON.LT.180).OR.((ILON.EQ.180).AND.(LONM.LT.0))) GOTO 40
      ILON = ILON-360
C
C     ILON RANGES FROM -180 AT 180 DEGREES WEST TO 179 AT 179 DEGREES EAST
C
   40 IF(LONM.GE.0)GOTO 50                                                     
      ILON=ILON-1                                                              
C                                                                              
C FIND RECORD START AND END FROM INDEX SECTION OF FILE                         
C                                                                              
   50 READ(3,REC=ILAT)IREC1
      READ(3,REC=(ILAT+1))IREC2
C                                                                              
C SEARCH RECORD FOR CORRECT LONGITUDE AND SET VALUE OF IAREA                   
C                                                                              
      IREC=IREC1+1                                                             
   60 READ(3,REC=IREC)KLON
      READ(3,REC=(IREC+1))IAREB
      IF(KLON-ILON)70,90,100                                                   
   70 IF(IREC+2-IREC2)80,90,90                                                 
   80 IREC=IREC+2                                                              
      IAREA=IAREB                                                              
      GOTO 60                                                                  
   90 IAREA=IAREB                                                              
  100 RETURN                                                                   
C                                                                              
C  LATITUDE IS 90 DEGREES NORTH, SET AREA NUMBER TO 1                          
C                                                                              
  110 IAREA=1                                                                  
      RETURN                                                                   
      END                                                                      
C                                                                              
C                                                                              
C                                                                              
C                                                                              
C TEST DATA:                                                                   
C                                                                              
C THE FOLLOWING RECORDS SHOW THE CORRECT VALUE OF 'IAREA' WHICH SHOULD         
C BE RETURNED FOR THE GIVEN VALUES OF 'LATD', 'LATM', 'LOND' AND 'LONM'.       
C THESE VALUES HAVE BEEN CHOSEN TO TEST THE VARIOUS PATHWAYS THROUGH THE       
C SUBROUTINE.                                                                  
C                                                                              
C                                                                              
C     CALL FINAR  (  LATD  ,  LATM  ,  LOND  ,  LONM  ,  IAREA  )              
C                                                                              
C                     45      -30        0        0      -999                  
C                      0        0      -45       30      -999                  
C                     90        0        0        0         1                  
C                     63        0        0        0         4                  
C                    -33        0        0        0        24                  
C                    -33       -1        0        0        29                  
C                     91        0        0        0      -999                  
C                     90       -1        0        0      -999                  
C                      0        0      128        0        85                  
C                      0        0     -152        0        57                  
C                      0        0     -152       -1        53                  
C                      0        0     -540        0        53                  
C                      0        0     -540       -1      -999                  
C                      0        0      540        0        53                  
C                      0        0      540        1      -999                  

      SUBROUTINE DCORR(IOBS,IAREA,ICORR)                                      
C                                                                              
C  THIS FILE CONTAINS A  LISTING,  IN  STANDARD  FORTRAN  FORMAT,  OF  A       
C  SUBROUTINE   (SUBROUTINE  DCORR)  WHICH  WILL  RETURN  THE  CORRECTED       
C  SOUNDING  VALUE  WHEN  SUPPLIED  WITH  AN  OBSERVED  SOUNDING   VALUE       
C  (CORRECTED,  IF  NECESSARY,  TO  A  SOUND VELOCITY OF 1500 METRES PER       
C  SECOND) AND CORRECTION AREA NUMBER WHEN USED IN  CONJUNCTION  WITH  A       
C  DIRECT  ACCESS  FILE CREATED FROM THE DATA SUPPLIED IN FILE 4 OF THIS       
C  TAPE.                                                                       
C                                                                              
C  WITH THE EXCEPTION OF DIRECT ACCESS 'READ' STATEMENTS, THE SUBROUTINE       
C  HAS  BEEN  WRITTEN  IN ANSI STANDARD FORTRAN, AND SHOULD THEREFORE BE       
C  COMPATIBLE WITH ALL MACHINES ON WHICH THE USE OF DIRECT ACCESS  FILES       
C  IS  SUPPORTED.  THE SYNTAX OF THE DIRECT ACCESS 'READ' STATEMENTS MAY       
C  VARY ON DIFFERENT INSTALLATIONS, HOWEVER, AND THE USER IS ADVISED  TO       
C  ENSURE THAT THESE LINES OF THE SUBROUTINE (5 IN TOTAL) CONFORM TO THE       
C  STANDARD USED ON THE SPECIFIC MACHINE ON WHICH THE FACILITY IS USED.        
C                                                                              
C  IF THE USE OF DIRECT ACCESS FILES IS  NOT  SUPPORTED  ON  THE  USER'S       
C  INSTALLATION,  THEN  A  SUITABLE  ALTERNATIVE  (USING  AN  ARRAY OR A       
C  SEQUENTIAL FILE, POSSIBLY CONTAINING ONLY A SUBSET OF  THE  AVAILABLE       
C  DATA) MUST BE IMPLEMENTED.                                                  
C                                                                              
C  THE SUBROUTINE CHECKS THAT THE CORRECTION AREA NUMBER LIES WITHIN THE       
C  PERMISSIBLE RANGE (FROM 1 TO 85), RETURNING WITH AN ERROR CODE IF THE       
C  NUMBER IS OUTSIDE THAT RANGE. IF THE OBSERVED DEPTH IS LESS THAN  200       
C  METRES (THE MINIMUM DEPTH INCLUDED IN THE TABLES), THEN NO CORRECTION       
C  IS APPLIED AND THE CORRECTED  DEPTH  IS  RETURNED  IDENTICAL  TO  THE       
C  OBSERVED  DEPTH. THE CORRECTION AREA NUMBER IS USED AS A KEY INTO THE       
C  INDEX SECTION OF THE  DIRECT  ACCESS  FILE.   THE  APPROPRIATE  INDEX       
C  POINTER  INDICATES  THE  START RECORD OF THE RELEVANT DATA SECTION OF       
C  THE FILE. THE NEXT INDEX POINTER IN SEQUENCE INDICATES THE  BEGINNING       
C  OF THE FOLLOWING DATA SECTION, AND IS THEREFORE USED TO DETERMINE THE       
C  WORKING DATA AREA (SINCE IT POINTS TO THE END RECORD OF THE  RELEVANT       
C  DATA  SECTION  + 1).  THE SUBROUTINE THEN COMPUTES THE RECORD NUMBERS       
C  OF THE PAIR OF CORRECTED SOUNDING VALUES WHICH BRACKET  THE  OBSERVED       
C  SOUNDING.  IF  THESE RECORD NUMBERS LIE OUTSIDE THE WORKING DATA AREA       
C  (I.E. THE OBSERVED SOUNDING  VALUE  IS  GREATER  THAN  ANY  CORRECTED       
C  SOUNDING  VALUE  AVAILABLE FOR THAT CORRECTION AREA) THEN THE ROUTINE       
C  RETURNS AN ERROR CODE. IF THE OBSERVED  VALUE  DOES  LIE  WITHIN  THE       
C  RANGE  OF THE TABLE, THEN THE CORRECTED SOUNDING VALUE IS COMPUTED TO       
C  THE NEAREST METRE BY  LINEAR  INTERPOLATION  BETWEEN  THE  100  METRE       
C  SOUNDING  POINTS  IMMEDIATELY ABOVE AND BELOW THE OBSERVED VALUE, AND       
C  THE CORRECTED VALUE IS RETURNED BY THE SUBROUTINE.                          
C                                                                              
C  COMMENT RECORDS WITHIN THE SUBROUTINE  CODING  IDENTIFY  THE  VARIOUS       
C  STAGES OF THIS PROCESS.                                                     
C                                                                              
C  A SERIES OF SETS OF INPUT VALUES, TOGETHER WITH THE CORRECT  RETURNED       
C  VALUE  FROM THE SUBROUTINE, IS SUPPLIED ON COMMENT RECORDS AT THE END       
C  OF THE SUBROUTINE CODING. THE USER IS ADVISED TO TEST THE  SUBROUTINE       
C  USING  THESE  VALUES  TO ENSURE THAT THE SOFTWARE IS FULLY COMPATIBLE       
C  WITH HIS MACHINE.  THE VALUES HAVE BEEN CHOSEN TO  TEST  THE  VARIOUS       
C  PATHWAYS THROUGH THE SUBROUTINE.                                            
C                                                                              
C                                                                              
C     SUBROUTINE DCORR(IOBS,IAREA,ICORR)                                       
C                                                                              
C TO USE:  CALL DCORR(IOBS,IAREA,ICORR)                                        
C                                                                              
C WHERE:   IOBS  =  INTEGER OBSERVED DEPTH IN METRES FOR AN ASSUMED            
C                   SOUNDING VELOCITY OF 1500 METRES/SECOND.                   
C          IAREA =  ECHO-SOUNDING CORRECTION AREA IN THE                       
C                   RANGE 1 TO 85.                                             
C                                                                              
C     THE ABOVE INPUT PARAMETERS ARE UNALTERED ON RETURN                       
C                                                                              
C          ICORR =  RETURNED WITH THE INTEGER VALUE OF THE CORRECTED           
C                   SOUNDING IN METRES. RETURNED AS -999 IF AN ERROR           
C                   IS DETECTED IN THE INPUT PARAMETERS, OR IF THE             
C                   OBSERVED DEPTH IS GREATER THAN THE MAXIMUM VALUE           
C                   IN THE TABLE.                                              
C                   IF THE OBSERVED DEPTH IS LESS THAN 200 METRES, NO          
C                   CORRECTION IS APPLIED (I.E.  ICORR = IOBS).                
C                                                                              
C                                                                              
C RESTRICTIONS:  SOUNDING AREA LIMITS    1 TO 85                               
C                                                                              
C                                                                              
C CORRECTED DEPTH DATA ARE STORED IN THE DIRECT ACCESS FILE REFERENCED         
C AS LOGICAL UNIT NUMBER 4                                                     
C                                                                              
C                                                                              
      INTEGER   IOBS , IAREA, ICORR                                            
      INTEGER   IREC1, IREC2, KOBS , JREC1, ILOW , IHIGH                       
C                                                                              
C CHECK THAT CORRECTION AREA SPECIFIED IS WITHIN RANGE                         
C                                                                              
      ICORR=(-999)                                                             
      IF((IAREA.LT.1).OR.(IAREA.GT.85))RETURN                                  
C                                                                              
C CHECK THAT OBSERVED DEPTH IS NOT LESS THAN 200 METRES                        
C                                                                              
      IF(IOBS-200)10,20,20                                                     
   10 ICORR=IOBS                                                               
      RETURN                                                                   
C                                                                              
C FIND RECORD START AND END FROM INDEX SECTION OF FILE                         
C                                                                              
   20 READ(4,REC=IAREA)IREC1
      READ(4,REC=(IAREA+1))IREC2
  101   FORMAT(I6,1X)
C                                                                              
C COMPUTE RECORD NUMBERS OF RELEVANT CORRECTION FACTORS                        
C                                                                              
      KOBS=IOBS/100                                                            
      JREC1=KOBS+IREC1-1                                                       
      IF(JREC1+1-IREC2)50,40,30                                                
C                                                                              
C OBSERVED DEPTH GREATER THAN TABLE MAXIMUM - CANNOT COMPUTE CORRECTION        
C                                                                              
   30 RETURN                                                                   
C                                                                              
C OBSERVED DEPTH GREATER THAN OR EQUAL TO TABLE MAXIMUM - COMPUTE              
C CORRECTION IF OBSERVED DEPTH EQUAL TO TABLE MAXIMUM                          
C                                                                              
   40 IF(IOBS.NE.(IOBS/100*100))RETURN                                         
      READ(4,REC=JREC1)ICORR
      RETURN                                                                   
C                                                                              
C OBSERVED DEPTH IS WITHIN RANGE OF TABLE - COMPUTE CORRECTION                 
C                                                                              
   50 READ(4,REC=JREC1)ILOW
      READ(4,REC=(JREC1+1))IHIGH
      ICORR=((IHIGH-ILOW)*(IOBS-KOBS*100)+50)/100+ILOW                         
      RETURN                                                                   
      END                                                                      
C                                                                              
C                                                                              
C TEST DATA:                                                                   
C                                                                              
C THE FOLLOWING RECORDS SHOW THE CORRECT VALUE OF 'ICORR' WHICH SHOULD         
C BE RETURNED FOR THE GIVEN VALUES OF 'IOBS' AND 'IAREA'. THESE VALUES         
C HAVE BEEN CHOSEN TO TEST THE VARIOUS PATHWAYS THROUGH THE SUBROUTINE.        
C                                                                              
C                                                                              
C          CALL DCORR  (  IOBS  ,  IAREA  ,  ICORR  )                          
C                                                                              
C                          200        0       -999                             
C                          200        1        192                             
C                          200       85        205                             
C                          200       86       -999                             
C                          199       42        199                             
C                          200       42        205                             
C                          237       42        242                             
C                        11000       64      11421                             
C                        11001       64       -999                             
C                                                                                                                             
