MODULE Class_SSTtoMONAN
  IMPLICIT NONE
  PRIVATE
  
  PUBLIC :: SSTtoMONAN
  !PK character (len=*),dimension(10),parameter :: dname = (/"i1","j1","i2","j2","i3","j3","i4","j4","i5","j5"/)
  !PK character (len=*),parameter :: DATEV    = "date"
  !PK character (len=*),parameter :: FCSTV    = "forecast"
  !PK character (len=*),parameter :: SOURCEV  = "map_source"
  !PK character (len=*),parameter :: LEVELV   = "level"
  !PK character (len=*),parameter :: FIELDV   = "field"
  !PK character (len=*),parameter :: UNITSV   = "units"
  !PK character (len=*),parameter :: DESCV    = "description"
  !PK character (len=*),parameter :: NX       = "nx"
  !PK character (len=*),parameter :: NY       = "ny"
  !PK character (len=*),parameter :: IPROJ    = "projection"
  !PK character (len=*),parameter :: STARTI   = "starti"
  !PK character (len=*),parameter :: STARTJ   = "startj"
  !PK character (len=*),parameter :: STARTLAT = "startlat"
  !PK character (len=*),parameter :: STARTLON = "startlon"
  !PK character (len=*),parameter :: DELTALAT = "deltalat"
  !PK character (len=*),parameter :: DELTALON = "deltalon"
  !PK character (len=*),parameter :: NLATS    = "nlats"
  !PK character (len=*),parameter :: DX       = "dx"
  !PK character (len=*),parameter :: DY       = "dy"
  !PK character (len=*),parameter :: XLONC    = "xlonc"
  !PK character (len=*),parameter :: TRUELAT1 = "truelat1"
  !PK character (len=*),parameter :: TRUELAT2 = "truelat2"
  !PK character (len=*),parameter :: EARTH_RADIUS = "earth_radius"
  !PK character (len=*),parameter :: IS_WIND_GRID_REL = "is_wind_grid_rel"
  !PK character (len=*),parameter :: FILLVALUE = "_FillValue"

CONTAINS

SUBROUTINE SSTtoMONAN(nx,ny,hdate,latitudeNorth,longitudeWest,&
                       IncrementNorth_South,IncrementWest_East,filename,&
		       sst,icec)
 use met_data_module
 USE write_met_module, only :  write_met_init,write_next_met_field,write_met_close
 USE read_met_module , only :  read_met_init,read_next_met_field,read_met_close
 use module_debug
 implicit none
   integer         , intent(in   ) :: nx
   integer         , intent(in   ) :: ny 
   character(len=19), intent(in   ) :: hdate! '2024-04-02_00:00:00
   real            , intent(in   ) :: latitudeNorth
   real            , intent(in   ) :: longitudeWest
   real            , intent(in   ) :: IncrementNorth_South
   real            , intent(in   ) :: IncrementWest_East
   real            , intent(in   ) :: sst(nx,ny)
   real            , intent(in   ) :: icec(nx,ny)
   character (len=132), intent(in   ) ::  filename

   integer, parameter :: nvar = 2
   integer :: ivar
   integer :: istatus 

   type (met_data)             :: fg_data


   !filename='SST:2023-04-02_00'
   !  Get the input file name from the command line.
   !call getarg (1,flnm)

   if (filename(1:1) == ' ') then
      print *,'USAGE: sst.exe <filename>'
      print *,'       where <filename> is the name of an intermediate-format file'
      stop
   end if

   call set_debug_level(WARN)

   call write_met_init(trim(filename), .true., '0000-00-00_00', istatus)

   print*, 'OPENING FILE: ',trim(adjustl(filename))

   if(istatus==0) then

     do ivar=1,2!nvar 
       fg_data%version      = 5          ! WPS
       fg_data%nx           = nx
       fg_data%ny           = ny
       fg_data%iproj        = 0          ! Cylindrical equidistant
       fg_data%xfcst        = 0.0
       fg_data%xlvl         = 200100.000 ! surface 
       fg_data%startlat     = latitudeNorth
       fg_data%startlon     = longitudeWest
       fg_data%starti       = 1
       fg_data%startj       = 1
       fg_data%deltalat     = -IncrementNorth_South 
       fg_data%deltalon     =  IncrementWest_East
       fg_data%dx           = 2.80259693E-42
       fg_data%dy           = 0.0
       fg_data%xlonc        = 8.40779079E-45
       fg_data%centerlat    = 0.0
       fg_data%centerlon    = 0.0
       fg_data%pole_lat     = 0.0
       fg_data%pole_lon     = -6.11213127E-30
       fg_data%truelat1     = -3.39207606E-35
       fg_data%truelat2     = 2.80259693E-45
       fg_data%earth_radius = 6371.22949
       fg_data%is_wind_grid_rel =.false. 
       fg_data%hdate      = hdate  !fg_data%hdate

       if(ivar == 1)THEN
          fg_data%field      = 'SST'
          fg_data%units      = 'K'
          fg_data%map_source = 'NCEP GFS Analysis' 
          fg_data%desc       = 'Sea Surface Temperature'
       else
          fg_data%field      = 'SEAICE'
          fg_data%units      = '0/1 Flag'
          fg_data%map_source = 'NCEP GFS Analysis' 
          fg_data%desc       = 'Ice flag'
       endif
        if(istatus ==0)then 
          print*,'next='
          print*,'version          =', fg_data%version
          print*,'nx               =', fg_data%nx
          print*,'ny               =', fg_data%ny
          print*,'iproj            =', fg_data%iproj
          print*,'xfcst            =', fg_data%xfcst
          print*,'xlvl             =', fg_data%xlvl
          print*,'startlat         =', fg_data%startlat
          print*,'startlon         =', fg_data%startlon
          print*,'starti           =', fg_data%starti
          print*,'startj           =', fg_data%startj
          print*,'deltalat         =', fg_data%deltalat
          print*,'deltalon         =', fg_data%deltalon
          print*,'dx               =', fg_data%dx
          print*,'dy               =', fg_data%dy
          print*,'xlonc            =', fg_data%xlonc
          print*,'centerlat        =', fg_data%centerlat
          print*,'centerlon        =', fg_data%centerlon
          print*,'pole_lat         =', fg_data%pole_lat
          print*,'pole_lon         =', fg_data%pole_lon
          print*,'truelat1         =', fg_data%truelat1
          print*,'truelat2         =', fg_data%truelat2
          print*,'earth_radius     =', fg_data%earth_radius
          print*,'is_wind_grid_rel =', fg_data%is_wind_grid_rel 
          print*,'field            =', TRIM(fg_data%field) 
          print*,'hdate            =', TRIM(fg_data%hdate) 
          print*,'units            =', TRIM(fg_data%units) 
          print*,'map_source       =', TRIM(fg_data%map_source) 
          print*,'desc             =', TRIM(fg_data%desc) 
       endif
       allocate(fg_data % slab(fg_data % nx, fg_data % ny))

       if(ivar == 1)THEN
          fg_data % slab = sst
       else
          fg_data % slab = icec
       endif


       
       
       call write_next_met_field(fg_data,istatus)
       deallocate(fg_data % slab)
 
     end do
   else
     print *, 'File = ',trim(filename)
     print *, 'Problem with input file, I can''t open it'
     stop
   end if
   call write_met_close()

 END SUBROUTINE SSTtoMONAN
 END MODULE Class_SSTtoMONAN
