      SUBROUTINE GTM(RLATG,RLONG,RLATMP,RLONMP,RLATM,RLONM,DIP,DEC,W,N) 
      SAVE
C     ****     TRANSFORMS GEOGRAPHIC COORDINATES TO GEOMAGNETIC,        
C     ****     CALCULATES DIP AND DECLINATION.                          
C     ****                                                              
C     ****     RLATG(N), RLONG(N) --  N-DIMENSIONAL ARRAYS OF GEOGRAPHIC
C     ****     LATITUDES AND CORRESPONDING LONGITUDES. (RADIANS)        
C     ****                                                              
C     ****     RLATMP, RLONMP --  GEOGRAPHIC COORDINATES OF GEOMAGNETIC 
C     ****     NORTH POLE. (RADIANS)                                    
C     ****                                                              
C     ****     RLATM(N), RLONM(N) --  OUTPUT ARRAYS OF GEOMAGNETIC      
C     ****     COORDINATES. (RADIANS)                                   
C     ****                                                              
C     ****     DIP(N), DEC(N) -- OUTPUT ARRAYS OF DIP AND DECLINATION.  
C     ****                                                              
C     ****     W(N,4) --  WORKING ARRAY.                                
C     ****                                                              
C     ****     N --  NUMBER OF (LAT,LON) POINTS TO BE TRANSFORMED.      
C     ****                                                              
      DIMENSION RLATG(N),RLONG(N),RLATM(N),RLONM(N),DIP(N),DEC(N),W(N,4)
      DATA E/1.E-10/                                                    
      SINLAM=SIN(RLATMP)                                                
      COSLAM=COS(RLATMP)                                                
      DO 1 I=1,N                                                        
      W(I,1)=SIN(RLATG(I))                                              
      W(I,2)=COS(RLATG(I))                                              
      W(I,3)=SIN(RLONG(I)+E-RLONMP)                                     
      W(I,4)=COS(RLONG(I)+E-RLONMP)                                     
      W(I,4)=SINLAM*W(I,1)+COSLAM*W(I,2)*W(I,4)                         
      RLONM(I)=ATAN2(W(I,3)*W(I,2),(W(I,4)*SINLAM-W(I,1))/COSLAM)       
      RLATM(I)=ASIN(W(I,4))                                             
C     DEC(I)=ATAN2(COSLAM,(SINLAM-W(I,1)*W(I,4))/(W(I,2)*W(I,3)))       
      DEC(I)=ATAN2(COSLAM*W(I,2)*W(I,3),SINLAM-W(I,1)*W(I,4))           
      DEC(I)=-DEC(I)                                                    
      DIP(I)=ATAN(2.*TAN(RLATM(I)))                                     
    1 CONTINUE                                                          
      RETURN                                                            
      ENTRY MTG(RLATG,RLONG,RLATMP,RLONMP,RLATM,RLONM,W,N)              
C     ****     INVERSE TRANSFORMATION                                   
C     ****                                                              
C     ****     PARAMETERS AS ABOVE EXCEPT INPUT --  RLATM,RLONM         
C     ****                                OUTPUT --  RLATG,RLONG        
C     ****                                                              
      SINLAM=SIN(RLATMP)                                                
      COSLAM=COS(RLATMP)                                                
      SINLOM=SIN(RLONMP)                                                
      COSLOM=COS(RLONMP)                                                
      DO 2 I=1,N                                                        
      W(I,1)=SIN(RLATM(I))                                              
      W(I,2)=COS(RLATM(I))                                              
      W(I,3)=SIN(RLONM(I))                                              
      W(I,4)=COS(RLONM(I))                                              
      RLATG(I)=SINLAM*W(I,1)-COSLAM*W(I,2)*W(I,4)                       
      W(I,3)=W(I,3)*W(I,2)                                              
      W(I,4)=-(RLATG(I)*SINLAM-W(I,1))/COSLAM                           
      RLATG(I)=ASIN(RLATG(I))                                           
      RLONG(I)=ATAN2(SINLOM*W(I,4)+COSLOM*W(I,3),COSLOM*W(I,4)-SINLOM   
     A*W(I,3))                                                          
    2 CONTINUE                                                          
      RETURN                                                            
      END                                                               
