!
! File: sift_create_max.f
! Author: Christopher Moore
!
! Creates netCDF output file for Desktop SIFT.
! This routine includes max_amp and max_speed
! for each grid.
! Called from sift_create.f
!
      subroutine SiftCreateMax(ncid,lonname,latname,nx_n,ny_n,
     &            lonid,latid,
     &            maxhname,minhname,maxspname,maxhid,minhid,maxspid)
      implicit none
      include 'netcdf.inc'

      integer*4 iret, ncid,nx_n,ny_n,lonid,latid
      integer*4 maxhid, minhid, maxspid
      integer*4 LAT_dim, LON_dim, LON_dims(1), LAT_dims(1), maxdims(2)
      character*(*) lonname,latname,maxhname,minhname,maxspname
      real*4 realval(1)

!.....define dimensions
      iret = nf_def_dim(ncid, lonname, nx_n, LON_dim)
      call check_err(iret)
      iret = nf_def_dim(ncid, latname, ny_n, LAT_dim)
      call check_err(iret)

!.....define variables
      LON_dims(1) = LON_dim
      iret = nf_def_var(ncid, lonname, NF_DOUBLE, 1, LON_dims, lonid)
      call check_err(iret)
      iret = nf_put_att_text(ncid, lonid, 'units', 12, 'degrees_east')
      call check_err(iret)
      LAT_dims(1) = LAT_dim
      iret = nf_def_var(ncid, latname, NF_DOUBLE, 1, LAT_dims, latid)
      call check_err(iret)
      iret = nf_put_att_text(ncid, latid, 'units', 13, 'degrees_north')
      call check_err(iret)

      maxdims(2) = LAT_dim
      maxdims(1) = LON_dim
      realval(1) = -9.9999998e+33

!.....Create variable max_amp
      iret = nf_def_var(ncid, maxhname, NF_REAL, 2, maxdims, maxhid)
      call check_err(iret)
      iret = nf_put_att_text(ncid, maxhid, 'long_name', 22,
     &   'Maximum Wave Amplitude')
      call check_err(iret)
      iret = nf_put_att_text(ncid, maxhid, 'units', 11, 'CENTIMETERS')
      call check_err(iret)
      iret = nf_put_att_real(ncid, maxhid, 'missing_value', NF_REAL,1,
     &   realval)
      call check_err(iret)
      iret = nf_put_att_real(ncid, maxhid, '_FillValue', NF_REAL, 1,
     &   realval)
      call check_err(iret)

!.....Create variable min_amp
      realval(1) = 1.0e+34
      iret = nf_def_var(ncid, minhname, NF_REAL, 2, maxdims, minhid)
      call check_err(iret)
      iret = nf_put_att_text(ncid, minhid, 'long_name', 22,
     &   'Minimum Wave Amplitude')
      call check_err(iret)
      iret = nf_put_att_text(ncid, minhid, 'units', 11, 'CENTIMETERS')
      call check_err(iret)
      iret = nf_put_att_real(ncid, minhid, 'missing_value', NF_REAL,1,
     &   realval)
      call check_err(iret)
      iret = nf_put_att_real(ncid, minhid, '_FillValue', NF_REAL, 1,
     &   realval)
      call check_err(iret)


!.....Create variable max_speed
      realval(1) = -9.9999998e+33
      iret = nf_def_var(ncid,maxspname, NF_REAL, 2, maxdims, maxspid)
      call check_err(iret)
      iret = nf_put_att_text(ncid, maxspid, 'long_name', 21,
     &   'Maximum Current Speed')
      call check_err(iret)
      iret = nf_put_att_text(ncid, maxspid, 'units', 18,
     & 'CENTIMETERS/SECOND')
      call check_err(iret)
      iret = nf_put_att_real(ncid, maxspid, 'missing_value',
     & NF_REAL,1,realval)
      call check_err(iret)
      iret = nf_put_att_real(ncid, maxspid, '_FillValue', NF_REAL, 1,
     &   realval)
      call check_err(iret)


      end

      subroutine SiftWriteAxes(ncid,nx,ny,nnx,nny,
     &                         h1,h2,lonid,latid,latimg)

      implicit none
      include 'netcdf.inc'

      integer*4 i,j,ii,jj,iret
      integer*4 ncid,nx,ny,nnx,nny,lonid,latid
      real*8 h1(nx),h2(ny),lon(nx),lat(ny)
      logical latimg

!.....define axis
      do i=1,nx
         lon(i)=h1(i)
      end do
      if (latimg) then
        do j=1,ny
           lat(ny-j+1)=h2(j)
        end do
      else
        do j=1,ny
           lat(j)=h2(j)
        end do
      end if
      iret = nf_put_var_double(ncid, lonid, lon)
      call check_err(iret)
      iret = nf_put_var_double(ncid, latid, lat)
      call check_err(iret)

      end
