PROGRAM Main
 IMPLICIT NONE
 TYPE GridDef
    CHARACTER(LEN=20) :: GRIDDEF  
    INTEGER           :: ncols  
    INTEGER           :: nlins  
    REAL              :: X1  
    REAL              :: Y2  
    REAL              :: resX  
    REAL              :: resY  
    REAL              :: Lati  
    REAL              :: Loni  
    REAL              :: nodatavalue
 END TYPE GridDef  
 TYPE(GridDef), ALLOCATABLE :: GridBamg(:)
 REAL         , ALLOCATABLE :: MapVegBamIbis(:,:)
 REAL         , ALLOCATABLE :: MapVegBamIbisNew(:,:)
 REAL         , ALLOCATABLE :: CoorBamGblLat(:)
 REAL         , ALLOCATABLE :: CoorBamGblLon(:)
 REAL                 :: nlat
 REAL                 :: nlon
 INTEGER              :: lrec
 INTEGER              :: i,j,ii,jj
 CHARACTER(LEN=528)   :: FileName2='./ibismsk_old.form'
 CHARACTER(LEN=528)   :: FileName3='./ibismsk.form'

 ALLOCATE(GridBamg(1))
  GridBamg(1)%ncols=720
  GridBamg(1)%nlins=360
  GridBamg(1)%X1=-179.75
  GridBamg(1)%Y2=89.500
  GridBamg(1)%Loni=GridBamg(1)%X1
  GridBamg(1)%Lati=-89.7500
  GridBamg(1)%resX=0.5000000000
  GridBamg(1)%resY=0.5000000000
  GridBamg(1)%nodatavalue=-999.0
  PRINT*,GridBamg(1)%ncols
  PRINT*,GridBamg(1)%nlins
  PRINT*,GridBamg(1)%X1 
  PRINT*,GridBamg(1)%Y2 
  PRINT*,GridBamg(1)%Loni
  PRINT*,GridBamg(1)%Lati
  PRINT*,GridBamg(1)%nodatavalue

  ALLOCATE(CoorBamGblLat(GridBamg(1)%nlins))
  ALLOCATE(CoorBamGblLon(GridBamg(1)%ncols))
  ALLOCATE(MapVegBamIbis(GridBamg(1)%ncols,GridBamg(1)%nlins))
  ALLOCATE(MapVegBamIbisNew(GridBamg(1)%ncols,GridBamg(1)%nlins))
  INQUIRE(IOLENGTH=lrec)MapVegBamIbis
  OPEN(1,FILE=TRIM(FileName2), ACCESS='DIRECT',FORM='UNFORMATTED',RECL=lrec,STATUS='OLD',ACTION='READ') 
  READ(1,rec=1)MapVegBamIbis
  CLOSE(1,STATUS='KEEP')
  MapVegBamIbisNew=MapVegBamIbis
   CoorBamGblLat(1) = GridBamg(1)%Lati
   DO j=2,GridBamg(1)%nlins
      CoorBamGblLat(j) = CoorBamGblLat(j-1) + GridBamg(1)%resY
   END DO

   CoorBamGblLon(1) = GridBamg(1)%Loni
   DO i=2,GridBamg(1)%ncols
      CoorBamGblLon(i) = CoorBamGblLon(i-1) + GridBamg(1)%resX
   END DO
   PRINT*,MAXVAL(CoorBamGblLat),MINVAL(CoorBamGblLat)
   PRINT*,MAXVAL(CoorBamGblLon),MINVAL(CoorBamGblLon)
   DO jj=1,GridBamg(1)%nlins
       nlat=CoorBamGblLat(jj)
      DO ii=1,GridBamg(1)%ncols
          nlon= CoorBamGblLon(ii)
          IF( nlat >= 84.0 .and. nlat <= 90.0 )THEN
             MapVegBamIbisNew(ii,jj)=15.0
          ELSE IF((nlat>= 55.0 .and. nlat <= 85.0) .and. (nlon>= -60.0 .and. nlon <= -10.0))THEN
             MapVegBamIbisNew(ii,jj)=15.0
          ELSE IF((nlat>= 62.0 .and. nlat <= 68.0) .and. (nlon>= -25.0 .and. nlon <= -10.0))THEN
             MapVegBamIbisNew(ii,jj)=MapVegBamIbis(ii,jj)
          ELSE
             MapVegBamIbisNew(ii,jj)=MapVegBamIbis(ii,jj)
          END IF
      END DO
   END DO

  INQUIRE(IOLENGTH=lrec)MapVegBamIbisNew
  OPEN(1,FILE=TRIM(FileName3),  ACCESS='DIRECT',FORM='UNFORMATTED',RECL=lrec,STATUS='UNKNOWN',ACTION='WRITE') 
  WRITE(1,rec=1)MapVegBamIbisNew
  CLOSE(1,STATUS='KEEP')


END PROGRAM Main
