write_output.f90 4.91 KB

!____________________________________________________________________
!
!DESCRIPTION: This routine create the NETCDF output
!             file. You must have the FORTRAN NETCDF libraries
!             installed on your computer  to use this routine.
!
!____________________________________________________________________

!INTERFACE:
subroutine write_output

!MODULE USES:
   use netcdf
   use parameters                 

!LOCAL PARAMETERS:

   implicit none

!INCLUDES:
   include 'netcdf.inc'

   integer :: ncid
   integer :: omID
   integer :: xID
   integer :: tID
   integer :: EID
   integer :: om_varID
   integer :: x_varID
   integer :: t_varID
   integer :: stat
   integer :: Dmax_varID
   integer :: Dave_varID
   integer :: c_varID
   integer :: h_varID
   integer :: floeID
   integer :: fsdID
   integer :: floe_varID
   integer :: SrcID
   integer :: HsID
   integer :: hID
   integer :: hcat_varID
   integer :: idtID

   stat=nf90_create(namefile,cmode=or(nf90_clobber,nf90_64bit_offset),ncid=ncid)
   call handle_err(stat)

   ! dimensions
   stat=nf90_def_dim(ncid,"omega",nf,omID)
   call handle_err(stat)

   stat=nf90_def_dim(ncid,"x"   ,nx,xID)
   call handle_err(stat)

   stat=nf90_def_dim(ncid,"time",nt,tID)
   call handle_err(stat)

   stat=nf90_def_dim(ncid,"dcat",nr,floeID)
   call handle_err(stat)

   stat=nf90_def_dim(ncid,"hcat",nh,hID)
   call handle_err(stat)

   ! coordinates
   stat=nf90_def_var(ncid,"omega",nf90_double,omID,om_varID)
   call handle_err(stat)

   stat=nf90_def_var(ncid,"x",nf90_double,xID,x_varID)
   call handle_err(stat)

   stat=nf90_def_var(ncid,"time",nf90_double,tID,t_varID)
   call handle_err(stat)

   stat=nf90_def_var(ncid,"hcat",nf90_double,hID,hcat_varID)
   call handle_err(stat)

   stat=nf90_def_var(ncid,"dcat",nf90_double,floeID,floe_varID)
   call handle_err(stat)

   ! variables
   ! (x, f, t)
   stat=nf90_def_var(ncid,"E",nf90_double,(/xID,omID,tID/),EID)
   call handle_err(stat)

   stat=nf90_def_var(ncid,"Sice",nf90_double,(/xID,omID,tID/),SrcID)
   call handle_err(stat)

   ! (x)
   stat=nf90_def_var(ncid,"dave",nf90_double,xID,Dave_varID)
   call handle_err(stat)

   stat=nf90_def_var(ncid,"dmax",nf90_double,xID,Dmax_varID)
   call handle_err(stat)

   stat=nf90_def_var(ncid,"icec",nf90_double,xID,c_varID)
   call handle_err(stat)

   stat=nf90_def_var(ncid,"ithk",nf90_double,xID,h_varID)
   call handle_err(stat)

   ! (t, x, d, h)
   stat=nf90_def_var(ncid,"fsd",nf90_double,(/tID,xID,floeID,hID/),fsdID)
   call handle_err(stat)

   ! (t, x)
   stat=nf90_def_var(ncid,"hs",nf90_double,(/tID,xID/),HsID)
   call handle_err(stat)

   ! (x, h)
   stat=nf90_def_var(ncid,"itd",nf90_double,(/xID,hID/),idtID)
   call handle_err(stat)

   ! set attributes
   stat=nf_put_att_text(ncid,xID,'units',10,'km')
   stat=nf_put_att_text(ncid,xID,'axis',1,'X')
   stat=nf_put_att_text(ncid,xID,'long_name',7,'Distance')

   stat=nf_put_att_text(ncid,omID,'units',5,'Hz')
   stat=nf_put_att_text(ncid,omID,'long_name',7,'Frequency')

   stat=nf_put_att_text(ncid,hID,'units',5,'m')
   stat=nf_put_att_text(ncid,hID,'axis',1,'Z')
   stat=nf_put_att_text(ncid,hID,'long_name',7,'Ice thickness')

   stat=nf_put_att_text(ncid,tID,'units',7,'hours')
   stat=nf_put_att_text(ncid,tID,'axis',1,'T')
   stat=nf_put_att_text(ncid,tID,'long_name',4,'Time')

   stat=nf_put_att_text(ncid,HsID,'units',1,'m')
   stat=nf_put_att_text(ncid,HsID,'long_name',23,               &
        'Significant Wave Heigth')

   stat=nf_put_att_text(ncid,EID,'units',10,'m2 s rad-1')
   stat=nf_put_att_text(ncid,EID,'long_name',50,                &
        'Sea surface wave variance spectral density')

   ! end file definition
   stat=nf90_enddef(ncid)
   call handle_err(stat)

   ! populate variables
   stat=nf90_put_var(ncid,om_varID,omega)
   call handle_err(stat)

   stat=nf90_put_var(ncid,EID,E)
   call handle_err(stat)

   stat=nf90_put_var(ncid,SrcID,S_ice)
   call handle_err(stat)

   stat=nf90_put_var(ncid,Dmax_varID,Dmax)
   call handle_err(stat)

   stat=nf90_put_var(ncid,Dave_varID,Dave)
   call handle_err(stat)

   stat=nf90_put_var(ncid,t_varID,time)
   call handle_err(stat)

   stat=nf90_put_var(ncid,x_varID,x_axis)
   call handle_err(stat)

   stat=nf90_put_var(ncid,c_varID,C_ice)
   call handle_err(stat)

   stat=nf90_put_var(ncid,h_varID,h)
   call handle_err(stat)

   stat=nf90_put_var(ncid,fsdID,FSD)
   call handle_err(stat)

   stat=nf90_put_var(ncid,floe_varID,middle_floe_cat)
   call handle_err(stat)

   stat=nf90_put_var(ncid,hcat_varID,middle_h_cat)
   call handle_err(stat)

   stat=nf90_put_var(ncid,HsID,h_sign)
   call handle_err(stat)

   stat=nf90_put_var(ncid,idtID,itd)
   call handle_err(stat)

   stat=nf90_close(ncid)
   call handle_err(stat)
        
contains

subroutine handle_err(stat)

   integer, intent(in) :: stat

   if ( stat /= nf90_noerr ) then
      write(*,*)trim(nf90_strerror(stat))
      stop 'unable to write the netcdf output' 
   end if

end subroutine handle_err


end subroutine write_output