Read a grid from a file
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | fn | Filename |
||
character(len=*), | intent(inout), | dimension(:), allocatable | :: | vars | List of variable names |
|
real(kind=dp), | intent(inout), | dimension(:), allocatable | :: | x | Grid data from file; allocated if present |
|
real(kind=dp), | intent(inout), | dimension(:), allocatable | :: | y | Grid data from file; allocated if present |
|
real(kind=dp), | intent(inout), | dimension(:), allocatable | :: | z | Grid data from file; allocated if present |
|
real(kind=dp), | intent(inout), | dimension(:), allocatable | :: | t | Grid data from file; allocated if present |
subroutine readGrid(fn,vars,x,y,z,t)
!! Read a grid from a file
character(*),intent(in)::fn
!! Filename
character(*),dimension(:),allocatable,intent(inout)::vars
!! List of variable names
real(dp),dimension(:),allocatable,intent(inout)::x
!! Grid data from file; allocated if present
real(dp),dimension(:),allocatable,intent(inout)::y
!! Grid data from file; allocated if present
real(dp),dimension(:),allocatable,intent(inout)::z
!! Grid data from file; allocated if present
real(dp),dimension(:),allocatable,intent(inout)::t
!! Grid data from file; allocated if present
integer::f_id
integer::Nd,Nv,Na,ud_id,fmtn,d_len
character(NF90_MAX_NAME)::d_name,v_name
integer::ier
integer::k,kv
ier = nf90_open(fn,NF90_NOWRITE,f_id)
ier = nf90_inquire(f_id,Nd,Nv,Na,ud_id,fmtn)
allocate(vars(Nv-Nd))
kv = 0
do k=1,Nd
ier = nf90_inquire_dimension(f_id, k,d_name,d_len)
select case(trim(d_name))
case('x')
allocate(x(d_len))
case('y')
allocate(y(d_len))
case('z')
allocate(z(d_len))
case('t')
allocate(t(d_len))
end select
end do
do k=1,Nv
ier = nf90_inquire_variable(f_id,k,v_name)
select case(trim(v_name))
case('x')
ier = nf90_get_var(f_id,k,x)
case('y')
ier = nf90_get_var(f_id,k,y)
case('z')
ier = nf90_get_var(f_id,k,z)
case('t')
ier = nf90_get_var(f_id,k,t)
case default
kv = kv+1
vars(kv) = trim(v_name)
end select
end do
ier = nf90_close(f_id)
end subroutine readGrid