subroutine writenc2(fil_nm,var_nm,units,fill_value,nt,ny,nx,var)

implicit none

include 'netcdf.inc'

integer,parameter::r8 = selected_real_kind(12)

integer,intent(in)::nt,ny,nx
integer,intent(in)::var(nx,ny,nt),fill_value(1)
character(*),intent(in)::fil_nm,var_nm,units
integer::ncid,vrid,tid,yid,xid
integer::nd(3)

call check(nf_create(fil_nm,nf_clobber,ncid)) ! Create file
call check(nf_def_dim(ncid,"Time",nt,tid))    ! Define dimension
call check(nf_def_dim(ncid,"south_north",ny,yid))
call check(nf_def_dim(ncid,"west_east",nx,xid))

nd = (/xid,yid,tid/)

call check(nf_def_var(ncid,var_nm,nf_int,3,nd,vrid)) ! Define variable
call check(nf_put_att_text(ncid,vrid,"description",50,var_nm)) ! Add attribute
call check(nf_put_att_text(ncid,vrid,"units",50,units))
call check(nf_put_att_int(ncid,vrid,"FillValue",4,1,fill_value))
call check(nf_enddef(ncid)) ! End define
call check(nf_put_var_int(ncid,vrid,var)) ! Write data
call check(nf_close(ncid)) ! Close file

end subroutine writenc2
