PROGRAM Main
 IMPLICIT NONE

  ! Selecting Kinds
  INTEGER        , PARAMETER   :: r4 = SELECTED_REAL_KIND(6)  ! Kind for 32-bits Real Numbers
  INTEGER        , PARAMETER   :: i4 = SELECTED_INT_KIND(9)   ! Kind for 32-bits Integer Numbers
  INTEGER        , PARAMETER   :: r8 = SELECTED_REAL_KIND(15) ! Kind for 64-bits Real Numbers
  INTEGER        , PARAMETER   :: i8 = SELECTED_INT_KIND(14)  ! Kind for 64-bits Integer Numbers
  INTEGER        , PARAMETER   :: r16 = SELECTED_REAL_KIND(15)! Kind for 128-bits Real Numbers
 !INTEGER         , PARAMETER   :: r4  = kind(1.0)
 INTEGER         , PARAMETER   :: wp  = r8  !! Real precision
 INTEGER         , PARAMETER   :: iMAxIn=1440
 INTEGER         , PARAMETER   :: jMaxIn=600
 INTEGER         , PARAMETER   :: jMaxAux=600   
 REAL(wp)        , PARAMETER   :: UNDEF=-9.99e+08
 REAL(KIND=r4)   , ALLOCATABLE :: auxIn(:,:),auxIn2(:,:)
 REAL(KIND=wp)   , ALLOCATABLE :: sstI ( :,:) !  REAL(wp)  :: sstI      ( GLDAS_NLONS,GLDAS_NLATS)
 REAL(KIND=wp)   , ALLOCATABLE :: sstO ( :,:) !  REAL(wp)  :: sstI      ( GLDAS_NLONS,GLDAS_NLATS)
 REAL(KIND=r4)   , ALLOCATABLE :: soilOut(:,:,:)!ALLOCATE(Vars_GLDAS_TEMP(GLDAS_NLONS, GLDAS_NLATS))

 REAL(wp)  :: lons (iMAxIn)
 REAL(wp)  :: alonO (iMAxIn)

 REAL(wp)  :: lats (jMaxAux)
 REAL(wp)  :: alatO (jMaxIn)


 INTEGER         , PARAMETER   :: iMAx=192
 INTEGER         , PARAMETER   :: jMax=iMax/2
 REAL(KIND=wp)   , PARAMETER   :: dlon=360.0/iMAx
 REAL(KIND=r4)   , ALLOCATABLE :: soil(:,:,:)

 REAL(wp)  :: alon (iMAx)
 REAL(wp)  :: alat (jMax)

 REAL(wp)  :: alats (jMax) 
 CHARACTER(LEN=6 )             :: Grid=''
 CHARACTER(LEN=3 )             :: PERC='100'
 CHARACTER(LEN=8 )             :: label='20040101'
 CHARACTER(LEN=12)             :: cdate='12z01jul2004'
 CHARACTER(LEN=3)              :: cmon
 REAL(KIND=r4), ALLOCATABLE :: mask(:,:)
 INTEGER(KIND=i4), ALLOCATABLE :: lsmo(:,:)
 INTEGER(KIND=i4), ALLOCATABLE :: lsmk(:,:)
 INTEGER(KIND=i4), ALLOCATABLE :: vgmo(:,:)
 INTEGER(KIND=i4), ALLOCATABLE :: vegm(:,:)

 REAL(KIND=r4)                 :: pc
 INTEGER                       :: lrec
 INTEGER                       :: lrec2
 INTEGER                       :: i
 INTEGER                       :: j
 INTEGER                       :: k,ihour,iday,imon,iyear
 integer                       :: num_args, ix
 character(len=12), dimension(:), allocatable :: args
 num_args = command_argument_count()
 IF(num_args ==2)THEN
 allocate(args(num_args))  ! I've omitted checking the return status of the allocation 
 ELSE
   print*, 'SoilMoisture.exe requires 2 argument: labeli    porcentage_soilMoisture (0<->1)[0-100]%'
   print*, 'SoilMoisture.exe 12z01dec2014  1'
  STOP
 END IF 
 do ix = 1, num_args
    call get_command_argument(ix,args(ix))
         ! now parse the argument as you wish
 end do
 cdate=args(1)
 READ(args(2),*)pc
 IF(LEN_TRIM(cdate) ==12)THEN   
   PRINT*, 'execution'
   READ(cdate(1:2),'(I2.2)')ihour
   READ(cdate(4:5),'(I2.2)')iday
   READ(cdate(9:12),'(I4.4)')iyear
   READ(cdate(6:8),'(A3)')cmon
   IF(cmon=='jan')imon=1
   IF(cmon=='feb')imon=2
   IF(cmon=='mar')imon=3
   IF(cmon=='apr')imon=4
   IF(cmon=='may')imon=5
   IF(cmon=='jun')imon=6
   IF(cmon=='jul')imon=7
   IF(cmon=='aug')imon=8
   IF(cmon=='sep')imon=9
   IF(cmon=='oct')imon=10
   IF(cmon=='nov')imon=11
   IF(cmon=='dec')imon=12
   WRITE(label,'(I4.4,2I2.2)')iyear,imon,iday
 ELSE
   PRINT*, 'type HHzDDjulYYYY'
   STOP    'type 12z01jul2004'
 ENDIF
 OPEN(1,FILE='glas_lon.dat',STATUS='OLD',ACTION='READ')
 READ(1,*)lons
 DO i=1,iMAxIn
    IF(lons(i) < 0.0)THEN
       alonO(i) =360+lons(i)
    ELSE
       alonO(i) =lons(i)
    END IF
 END DO
 lons=CSHIFT(alonO,SHIFT=SIZE(alonO)/2,DIM=1)
 alonO=lons
 CLOSE(1,STATUS='KEEP')
 
 OPEN(1,FILE='glas_lat.dat',STATUS='OLD',ACTION='READ')
 READ(1,*)lats
 DO j=1,jMaxIn
    alatO(j)         = lats(j)    
 END DO
 CLOSE(1,STATUS='KEEP')



 Grid(1:1)='G'
 WRITE(Grid(2:6),'(I5.5)')jmax 

 alon=0.000
 DO i=2,iMax
    alon(i)=alon(i-1)+dlon
 END DO
 OPEN(1,FILE='bam_lat.'//TRIM(Grid),STATUS='OLD',ACTION='READ')
 READ(1,*)alats
 DO j=1,jMax
    alat(j)         = alats(j)    
 END DO

 CLOSE(1,STATUS='KEEP')

 ALLOCATE(auxIn (iMAxIn,jMaxIn)) 
 ALLOCATE(auxIn2 (iMAxIn,jMaxIn)) 
 ALLOCATE(sstI  (iMAxIn,jMaxIn)) 

 ALLOCATE(soilOut(iMax,jMax,4)) 
 ALLOCATE(sstO   (iMax,jMax)) 
 INQUIRE(IOLENGTH=lrec2)auxIn
 OPEN(1,FILE='soilmoisture_'//cdate//'.bin',ACCESS='DIRECT',FORM='UNFORMATTED',RECL=lrec2,STATUS='OLD',ACTION='READ')
 DO k=1,4 
   READ(1,rec=k)auxIn(:,:)
   auxIn2=CSHIFT(auxIn,SHIFT=SIZE(auxIn,Dim=1)/2,DIM=1)  
   sstI =auxIn2
   PRINT *,MAXVAL(sstI),MINVAL(sstI)
   CALL INTERP_LAND(iMAx ,jMax ,alat ,alon , iMaxIn,jMaxIn,alatO,alonO,UNDEF,sstI,sstO)
!   CALL INTERP_LAND(nLonO,nLatO,alatO,alonO, nLonI ,nLatI ,alat ,alon, UNDEF,sstI,sstO)

   PRINT *,MAXVAL(sstO),MINVAL(sstO)

   soilOut(1:iMAx,jMax:1:-1,k)= sstO(1:iMAx,1:jMax)

 END DO
 CLOSE(1,STATUS='KEEP')

 ALLOCATE(mask(iMax,jMax)) 
 ALLOCATE(lsmo(iMax,jMax))  
 ALLOCATE(lsmk(iMax,jMax))  
 ALLOCATE(vgmo(iMax,jMax))  
 ALLOCATE(vegm(iMax,jMax))  
 ALLOCATE(soil(iMax,jMax,8))  
 INQUIRE(IOLENGTH=lrec2)mask
 OPEN(31,FILE='VegetationMaskIBIS.'//TRIM(Grid),ACCESS='DIRECT',FORM='UNFORMATTED',RECL=lrec2,STATUS='OLD',ACTION='READ')
 READ(31,rec=1)mask
 lsmo=mask
 READ(31,rec=2)mask
 lsmk=mask
 READ(31,rec=3)mask
 vgmo=mask
 READ(31,rec=4)mask
 vegm=mask


 alats(jMax:1:-1)=alat(1:jMax)
 DO k=1,8 
    DO j=1,jMax
       DO i=1,Imax

          !   0.25*((wsoi0_200*0.001)/(0.5*1) + (wsoi0_100*0.001)/(0.5*0.6)+  (wsoi0_040*0.001)/(0.5*0.3) + (wsoi0_010*0.001)/(0.5*0.1))
         IF(k >=1 .and. k<=2)THEN
            soil(i,j,k)= MIN(( (soilOut(i,j,1)*0.001)/(0.5*0.1)),1.0)
         ELSE IF(k >2 .and. k<=4)THEN
            soil(i,j,k)= MIN(( (soilOut(i,j,2)*0.001)/(0.5*0.3)),1.0)
         ELSE IF(k >4 .and. k<=6)THEN
            soil(i,j,k)= MIN(( (soilOut(i,j,3)*0.001)/(0.5*0.6)),1.0)
         ELSE IF(k >6 .and. k<=8)THEN
            soil(i,j,k)= MIN(( (soilOut(i,j,4)*0.001)/(0.5*1.0)),1.0)
         ELSE
            soil(i,j,k)= MIN(0.25*((soilOut(i,j,4)*0.001)/(0.5*1.0) + (soilOut(i,j,3)*0.001)/(0.5*0.6) +   &
                                   (soilOut(i,j,2)*0.001)/(0.5*0.3) + (soilOut(i,j,1)*0.001)/(0.5*0.1)),1.0)
         END IF


         if(vegm(i,j) >= 1_i4 )THEN 
            soil(i,j,k)=MAX(soil(i,j,k),0.0000001)
            if(soil(i,j,k)==0.0) soil(i,j,k)=1.0000
            IF(alats(j) < -60.0)THEN
               soil(i,j,k) = 1.0000
            END IF
         ELSE
            soil(i,j,k)=UNDEF
         endif 

       END DO
    END DO   
 END DO

 DO k=1,8 
    DO j=1,jMax
       DO i=1,Imax
          IF(vegm(i,j) > 0_i4)THEN
              soil(i,j,k)=soil(i,j,k)
              IF(k>=1)THEN 
                 IF(alats(j) >= -30.0 .and. alats(j) <= 12.0)THEN
                    IF(alon(i) >= 270.0 .and. alon(i) <= 330.0)THEN
                       IF(vegm(i,j) == 1_i4)THEN
                          soil(i,j,k)= MIN(MAX(soil(i,j,k) * pc,0.1),0.99)
                       END IF
                    END IF
                 END IF
              END IF
          ELSE
             soil(i,j,k)=1.0000
          END IF
       END DO
    END DO   
 END DO   
 WRITE(PERC,'(I3.3)')INT(pc*100)
 Grid(1:1)='G'
 WRITE(Grid(2:6),'(I5.5)')jmax 
 INQUIRE(IOLENGTH=lrec)soil(:,:,1)
 PRINT*,lrec
 OPEN(2,FILE='New_'//TRIM(PERC)//'_SoilMoistureWeekly.'//label//'.'//TRIM(Grid),ACCESS='DIRECT',&
        FORM='UNFORMATTED',RECL=lrec,STATUS='UNKNOWN',ACTION='WRITE')
PRINT*,lrec
 PRINT *,MAXVAL(soil),MINVAL(soil)

 DO k=1,8 
   WRITE(2,rec=k)soil(1:Imax,1:jMax,k)
 END DO

CONTAINS

!----------------------------------------------------
      SUBROUTINE INTERP_LAND(nLonO,nLatO,alatO,alonO,nLonI,nLatI,alat,alon,UNDEF,sstI,sstO)
      IMPLICIT NONE
!  read daily IEEE SST file and print selected values.

!  sst   - sea surface temperature array (deg C)
!  anom  - sea surface temperature anomalies array (deg C)
!  err   - standard deviation error of sea surface temperature array (deg C)
!  ice   - sea ice concentration (percentage)
!  iyr   - year of analysis
!  imo   - month of analysis
!  ida   - day of analysis
!  alon  - longitude of center of grid square
!  alat  - latitude of center of grid square
!  dall  - grid size (0.25)
!  ax1   - first longitude (0.125 E)
!  ay1   - first latitude (-89.875 or 89.875S)
!  nLonI - number of longitudes
!  nLatI - number of latitudes
      INTEGER, INTENT(IN)   :: nLonO
      INTEGER, INTENT(IN)   :: nLatO
      REAL(wp)   , INTENT(IN)   :: alatO(nLatO)
      REAL(wp)   , INTENT(IN)   :: alonO(nLonO)

      INTEGER, INTENT(IN)   :: nLonI
      INTEGER, INTENT(IN)   :: nLatI
      REAL(wp)   , INTENT(IN)   :: alon(nLonI)
      REAL(wp)   , INTENT(IN)   :: alat(nLatI)

      REAL(wp)   , INTENT(IN)   :: UNDEF
      REAL(wp)   , INTENT(IN)   :: sstI (nLonI,nLatI)
      REAL(wp)   , INTENT(OUT)  :: sstO (nLonO,nLatO)


      INTEGER(KIND=2) :: anom (nLonI,nLatI)
      INTEGER(KIND=2) :: err  (nLonI,nLatI)
      INTEGER(KIND=2) :: ice  (nLonI,nLatI)   

      REAL   (KIND=wp) :: dalat(nLatI)
      REAL   (KIND=wp) :: dalon(nLonI)

      INTEGER(KIND=4) :: ipt(1)
      INTEGER(KIND=4) :: jpt(1)

      INTEGER(KIND=4) :: iptPrev(1)
      INTEGER(KIND=4) :: jptPrev(1)

      INTEGER(KIND=4) :: iptNext(1)
      INTEGER(KIND=4) :: jptNext(1)
      INTEGER(KIND=4) :: iyr
      INTEGER(KIND=4) :: imo
      INTEGER(KIND=4) :: ida
      REAL   (KIND=wp) :: dall 
      REAL   (KIND=wp) :: title

      INTEGER         :: i,ii
      INTEGER         :: j,jj
 
!

      DO jj=1,nLatO
         DO ii=1,nLonO
            DO j=1,nLatI
               dalat(j)=ABS(alat(j)-alatO(jj))
            END DO   
            DO i=1,nLonI
               dalon(i)=ABS(alon(i)-alonO(ii))
            END DO 
            ipt=MINLOC(dalon)
            jpt=MINLOC(dalat)
            iptPrev = ipt
            jptPrev = jpt
            iptNext = ipt
            jptNext = jpt

            DO i=ipt(1),nLonI,1
               IF(sstI(i,jpt(1)) == UNDEF)THEN
                  IF(i/=nLonI)iptNext(1) = iptNext(1) + 1
               ELSE  
                  IF(i/=nLonI)iptNext(1) = iptNext(1) + 1
                  EXIT
               END IF
            END DO

            DO i=ipt(1),1,-1
               IF(sstI(i,jpt(1)) == UNDEF) THEN
                  IF(i/=1)iptPrev(1) = iptPrev(1) - 1
               ELSE
                  IF(i/=1)iptPrev(1) = iptPrev(1) - 1
                  EXIT               
               END IF          
            END DO

            DO j=jpt(1),nLatI,1
               IF(sstI(ipt(1),j) == UNDEF)THEN
                  IF(j/=nLatI)jptNext(1) = jptNext(1) + 1
               ELSE
                  IF(j/=nLatI)jptNext(1) = jptNext(1) + 1
                  EXIT
               END IF        
            END DO

            DO j=jpt(1),1,-1
               IF(sstI(ipt(1),j) == UNDEF) THEN
                  IF(j/=1)jptPrev(1) = jptPrev(1) - 1
               ELSE
                  IF(j/=1)jptPrev(1) = jptPrev(1) - 1
                  EXIT
               END IF
            END DO
            !sstO(ii,jj) =sstI(ipt(1),jpt(1))
            IF(sstI(iptNext(1),jpt(1)) /= UNDEF .and. sstI(iptPrev(1),jpt(1)) /= UNDEF .and. &
               sstI(ipt(1),jptNext(1)) /= UNDEF .and. sstI(ipt(1),jptPrev(1)) /= UNDEF) THEN   
               sstO(ii,jj) =( REAL(iptPrev(1))/REAL(iptNext(1)+iptPrev(1))*sstI(iptNext(1),jpt(1)) + &
                              REAL(iptNext(1))/REAL(iptNext(1)+iptPrev(1))*sstI(iptPrev(1),jpt(1)) + &
                              REAL(jptPrev(1))/REAL(jptNext(1)+jptPrev(1))*sstI(ipt(1),jptNext(1)) + &
                              REAL(jptNext(1))/REAL(jptNext(1)+jptPrev(1))*sstI(ipt(1),jptPrev(1)) )/2.0
            
           ELSE IF(sstI(iptNext(1),jpt(1)) == UNDEF .and. sstI(iptPrev(1),jpt(1)) /= UNDEF .and. &
                   sstI(ipt(1),jptNext(1)) /= UNDEF .and. sstI(ipt(1),jptPrev(1)) /= UNDEF) THEN   
              sstO(ii,jj) = (sstI(iptPrev(1),jpt(1)) + &
                            REAL(jptPrev(1))/REAL(jptNext(1)+jptPrev(1))*sstI(ipt(1),jptNext(1)) +  &
                            REAL(jptNext(1))/REAL(jptNext(1)+jptPrev(1))*sstI(ipt(1),jptPrev(1)))/2.0 
           
           ELSE IF(sstI(iptNext(1),jpt(1)) /= UNDEF .and. sstI(iptPrev(1),jpt(1)) == UNDEF .and. &
                   sstI(ipt(1),jptNext(1)) /= UNDEF .and. sstI(ipt(1),jptPrev(1)) /= UNDEF) THEN   
              sstO(ii,jj) = (sstI(iptNext(1),jpt(1)) +   &
                            REAL(jptPrev(1))/REAL(jptNext(1)+jptPrev(1))*sstI(ipt(1),jptNext(1)) + &
                            REAL(jptNext(1))/REAL(jptNext(1)+jptPrev(1))*sstI(ipt(1),jptPrev(1)))/2.0 
           
           ELSE IF (sstI(iptNext(1),jpt(1)) /= UNDEF .and. sstI(iptPrev(1),jpt(1)) /= UNDEF .and. &           
                    sstI(ipt(1),jptNext(1)) == UNDEF .and. sstI(ipt(1),jptPrev(1)) /= UNDEF) THEN   
              sstO(ii,jj) = (REAL(iptPrev(1))/REAL(iptNext(1)+iptPrev(1))*sstI(iptNext(1),jpt(1))+  &
                             REAL(iptNext(1))/REAL(iptNext(1)+iptPrev(1))*sstI(iptPrev(1),jpt(1)) + &
                            sstI(ipt(1),jptPrev(1)))/2.0 
           
           ELSE IF(sstI(iptNext(1),jpt(1)) /= UNDEF .and. sstI(iptPrev(1),jpt(1)) /= UNDEF .and. &
                   sstI(ipt(1),jptNext(1)) /= UNDEF .and. sstI(ipt(1),jptPrev(1)) == UNDEF) THEN   
              sstO(ii,jj) = (REAL(iptPrev(1))/REAL(iptNext(1)+iptPrev(1))*sstI(iptNext(1),jpt(1)) +  &
                             REAL(iptNext(1))/REAL(iptNext(1)+iptPrev(1))*sstI(iptPrev(1),jpt(1)) + &
                            sstI(ipt(1),jptNext(1)))/2.0  
           
           ELSE IF(sstI(iptNext(1),jpt(1)) == UNDEF .and. sstI(iptPrev(1),jpt(1)) == UNDEF .and. &
                   sstI(ipt(1),jptNext(1)) /= UNDEF .and. sstI(ipt(1),jptPrev(1)) /= UNDEF) THEN   
                   
              sstO(ii,jj) = REAL(jptPrev(1))/REAL(jptNext(1)+jptPrev(1))*sstI(ipt(1),jptNext(1)) +  &
                            REAL(jptNext(1))/REAL(jptNext(1)+jptPrev(1))*sstI(ipt(1),jptPrev(1)) 
           
           ELSE IF(sstI(iptNext(1),jpt(1)) /= UNDEF .and. sstI(iptPrev(1),jpt(1)) /= UNDEF .and. &           
                   sstI(ipt(1),jptNext(1)) == UNDEF .and. sstI(ipt(1),jptPrev(1)) == UNDEF) THEN   
              sstO(ii,jj) = REAL(iptPrev(1))/REAL(iptNext(1)+iptPrev(1))*sstI(iptNext(1),jpt(1)) + &
                            REAL(iptNext(1))/REAL(iptNext(1)+iptPrev(1))*sstI(iptPrev(1),jpt(1))  

           ELSE IF(sstI(iptNext(1),jpt(1)) == UNDEF .and. sstI(iptPrev(1),jpt(1)) /= UNDEF .and. &
                   sstI(ipt(1),jptNext(1)) /= UNDEF .and. sstI(ipt(1),jptPrev(1)) == UNDEF) THEN   
              sstO(ii,jj) = (sstI(iptPrev(1),jpt(1))  + sstI(ipt(1),jptNext(1)))/2.0!

           ELSE IF(sstI(iptNext(1),jpt(1)) /= UNDEF .and. sstI(iptPrev(1),jpt(1)) == UNDEF .and. &
                   sstI(ipt(1),jptNext(1)) == UNDEF .and. sstI(ipt(1),jptPrev(1)) /= UNDEF) THEN   
              sstO(ii,jj) =  (sstI(iptNext(1),jpt(1))  + REAL(jptNext(1))&
                     /REAL(jptNext(1)+jptPrev(1))*sstI(ipt(1),jptPrev(1)) )/2.0

           ELSE IF(sstI(iptNext(1),jpt(1)) == UNDEF .and. sstI(iptPrev(1),jpt(1)) /= UNDEF .and. &
                   sstI(ipt(1),jptNext(1)) == UNDEF .and. sstI(ipt(1),jptPrev(1)) /= UNDEF) THEN   
              sstO(ii,jj) =   (sstI(iptPrev(1),jpt(1))  + sstI(ipt(1),jptPrev(1)) )/2.0

           ELSE IF(sstI(iptNext(1),jpt(1)) /= UNDEF .and. sstI(iptPrev(1),jpt(1)) == UNDEF .and. &
                   sstI(ipt(1),jptNext(1)) /= UNDEF .and. sstI(ipt(1),jptPrev(1)) == UNDEF) THEN   
              sstO(ii,jj) = (sstI(iptNext(1),jpt(1)) + sstI(ipt(1),jptNext(1)) )/2.0

           ELSE IF(sstI(iptNext(1),jpt(1)) /= UNDEF .and. sstI(iptPrev(1),jpt(1)) == UNDEF .and. &
                   sstI(ipt(1),jptNext(1)) == UNDEF .and. sstI(ipt(1),jptPrev(1)) == UNDEF) THEN   
              sstO(ii,jj) =  sstI(iptNext(1),jpt(1)) 

           ELSE IF(sstI(iptNext(1),jpt(1)) == UNDEF .and. sstI(iptPrev(1),jpt(1)) /= UNDEF .and. &
                   sstI(ipt(1),jptNext(1)) == UNDEF .and. sstI(ipt(1),jptPrev(1)) == UNDEF) THEN   
              sstO(ii,jj) =  sstI(iptPrev(1),jpt(1)) 

           ELSE IF(sstI(iptNext(1),jpt(1)) == UNDEF .and. sstI(iptPrev(1),jpt(1)) == UNDEF .and. &
                   sstI(ipt(1),jptNext(1)) /= UNDEF .and. sstI(ipt(1),jptPrev(1)) == UNDEF) THEN   
              sstO(ii,jj) =  sstI(ipt(1),jptNext(1))

           ELSE IF(sstI(iptNext(1),jpt(1)) == UNDEF .and. sstI(iptPrev(1),jpt(1)) == UNDEF .and. &
                   sstI(ipt(1),jptNext(1)) == UNDEF .and. sstI(ipt(1),jptPrev(1)) /= UNDEF) THEN   
              sstO(ii,jj) =  sstI(ipt(1),jptPrev(1)) 
            END IF            
         END DO
      END DO
      PRINT*,'pk',MAXVAL(sstO),MINVAL(sstO)
       RETURN  

      !print *,' '
      !WRITE(nameo(1:28),'(A7,I2.2,A9,I4.4,i2.2,i2.2,i2.2)')'gdas1.T',ihr,'Z.sstgrd.',iyr,imo,ida,ihr
      !print*, nameo

      !OPEN(10,FILE=TRIM(nameo(1:28)),FORM='UNFORMATTED',&
      !       ACCESS='SEQUENTIAL',STATUS='UNKNOWN',ACTION='WRITE',IOSTAT=error)
      !WRITE(10)273+REAL(sstI/100.0,kind=4)
      !STOP
 
      print*, title
      print*, 'SST, SST anomalies and error are in deg-C x 100' 
      print*,' SST date: year =',iyr,'  month =',imo,' day =',ida       
       
  
!  .... loop thru several longitudes and latitudes and print out SSTs 

      do i = 1,nLonI,400
        do j = 1,nLatI,200  
          print *, 'lon = ',alon(i),'  lat = ',alat(j),&
            '  sst = ',sstI(i,j),'  anom = ',anom(i,j), &
            '  err = ',err(i,j),'   ice = ',ice(i,j)   
        enddo
        print *,' '
      enddo


      print *,'Longitudes are in degrees E (positive)'
      print *,'  and increase from i=1, ',alon(1),&
       'to i=nLonI,',alon(nLonI)
      print *,'Latitudes are in degrees S (negative) and N (positive)'
      print *,'  and increase from j=1,',alat(1),'to j=nLatI,',alat(nLatI)
      print *,'The grid interval =',dall 
          
      print *,' All arrays is (nLonI,nLatI)'
      print *,' the first index is longitude',' the second is latitude'
       stop 1
            
       WRITE(6,302)
302    FORMAT (' WARNING! - PREMATURE END OF INPUT DATA: stop 100')
       RETURN  
       END SUBROUTINE INTERP_LAND

!SUBROUTINESUBROUTINESUBROUTINESUBROUTINESUBROUTINESUBROUTINESUBROUTINESUBROUTINESUBROUTINESUBROUTINE

  INTEGER FUNCTION getpoint(coord,locate)
  IMPLICIT NONE
  REAL(KIND=wp), INTENT(IN   ) :: coord(:)
  REAL(KIND=wp), INTENT(IN   ) :: locate
  REAL(KIND=wp)                :: GeomDist(SIZE(coord)) 
  INTEGER             :: npoint(1) 
  GeomDist =ABS(coord-locate)  
  npoint   =MINLOC(GeomDist)
  getpoint =npoint(1)
  !PRINT*,'locate=',locate,'getpoint=',getpoint, 'coord',coord
 END FUNCTION getpoint

!SUBROUTINESUBROUTINESUBROUTINESUBROUTINESUBROUTINESUBROUTINESUBROUTINESUBROUTINESUBROUTINESUBROUTINE
END PROGRAM Main
