Write a grid to a file
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | fn | Filename |
||
character(len=*), | intent(in), | dimension(:) | :: | vars | Variable names |
|
real(kind=dp), | intent(in), | optional | dimension(:) | :: | x | Grid data |
real(kind=dp), | intent(in), | optional | dimension(:) | :: | y | Grid data |
real(kind=dp), | intent(in), | optional | dimension(:) | :: | z | Grid data |
subroutine writeGrid(fn,vars,x,y,z)
!! Write a grid to a file
character(*),intent(in)::fn
!! Filename
character(*),dimension(:),intent(in)::vars
!! Variable names
real(dp),dimension(:),intent(in),optional::x
!! Grid data
real(dp),dimension(:),intent(in),optional::y
!! Grid data
real(dp),dimension(:),intent(in),optional::z
!! Grid data
character(100)::p
integer::c
integer,dimension(:),allocatable::dims
integer::f_id
integer::i_id,j_id,k_id,l_id
integer::x_id,y_id,z_id,t_id
integer::v_id
integer::ier
integer::k
ier = nf90_create(fn,NF90_CLOBBER,f_id)
ier = nf90_def_dim(f_id,'x',size(x),i_id)
if(present(y)) ier = nf90_def_dim(f_id,'y',size(y),j_id)
if(present(z)) ier = nf90_def_dim(f_id,'z',size(z),k_id)
ier = nf90_def_dim(f_id,'t',NF90_UNLIMITED,l_id)
ier = nf90_def_var(f_id,'x',NF90_DOUBLE,[i_id],x_id)
if(present(y)) ier = nf90_def_var(f_id,'y',NF90_DOUBLE,[j_id],y_id)
if(present(z)) ier = nf90_def_var(f_id,'z',NF90_DOUBLE,[k_id],z_id)
ier = nf90_def_var(f_id,'t',NF90_DOUBLE,[l_id],t_id)
ier = nf90_put_att(f_id,t_id,'units','seconds since start')
c = 1
if(present(y)) c = c+10
if(present(z)) c = c+100
select case(c)
case(001)
dims = [i_id,l_id]
case(011)
dims = [i_id,j_id,l_id]
p = 'yp, product; xp, product'
case(111)
dims = [i_id,j_id,k_id,l_id]
end select
do k=1,size(vars)
ier = nf90_def_var(f_id,vars(k),NF90_DOUBLE,dims,v_id)
ier = nf90_put_att(f_id,v_id,'field',vars(k))
end do
ier = nf90_enddef(f_id)
ier = nf90_put_var(f_id,x_id,x)
if(present(y)) ier = nf90_put_var(f_id,y_id,y)
if(present(z)) ier = nf90_put_var(f_id,z_id,z)
ier = nf90_close(f_id)
end subroutine writeGrid