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

implicit none

include 'netcdf.inc'

integer,parameter::r8 = selected_real_kind(12)

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

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,"top_bottom",nl,lid))
call check(nf_def_dim(ncid,"south_north",ny,yid))
call check(nf_def_dim(ncid,"west_east",nx,xid))

nd = (/xid,yid,tid,lid/)

call check(nf_def_var(ncid,var_nm,nf_float,4,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_double(ncid,vrid,"FillValue",5,1,fill_value))
call check(nf_enddef(ncid)) ! End define
call check(nf_put_var_double(ncid,vrid,var)) ! Write data
call check(nf_close(ncid)) ! Close file

end subroutine writenc0
