!
! File: sift_save_max.f
! Author: Christopher Moore
!
! Saves max amp and max speed for specified grids
! to netCDF output file for Desktop SIFT
!
      subroutine siftsavemax(ncid,mxid,mnid,spid,
     & LON_len,LAT_len,nx,ny,
     & maxh,minh,maxsp,latimg)

      implicit none
      include 'netcdf.inc'

      integer*4 i,j,k,ii,jj

c.....netCDF id and error status return
      integer*4  ncid, iret

c.....variable ids
      integer*4  mxid,mnid,spid

c.....netCDF dimension sizes for dimensions used with record variables
      integer*4  LON_len, LAT_len, nx, ny, npics

c.....data variables
      real*4 maxh(nx,ny), minh(nx,ny),maxsp(nx,ny)
      real*4 maxhsub(LON_len,LAT_len)
      real*4 minhsub(LON_len,LAT_len)
      real*4 maxspsub(LON_len,LAT_len)
c..... switch when finding image-ordered bathymetry files
      logical latimg

c..... Sub-sample maxVar for output
      if (latimg) then
        do j=1,LAT_len
          do i=1,LON_len
              maxhsub(i,LAT_len-j+1)=maxh(i,j)
              minhsub(i,LAT_len-j+1)=minh(i,j)
              maxspsub(i,LAT_len-j+1)=maxsp(i,j)
          end do
        end do
      else
        do j=1,LAT_len
          do i=1,LON_len
              maxhsub(i,j)=maxh(i,j)
              minhsub(i,j)=minh(i,j)
              maxspsub(i,j)=maxsp(i,j)
          end do
        end do
      endif

      iret = nf_put_var_real(ncid,mxid,maxhsub)
      call check_err(iret)
      iret = nf_put_var_real(ncid,mnid,minhsub)
      call check_err(iret)
      iret = nf_put_var_real(ncid,spid,maxspsub)
      call check_err(iret)


      end


