gdncvarinquire.f90
Go to the documentation of this file.
1 !
2 != 変数または属性に関する問い合わせ
3 !
4 ! Authors:: Eizi TOYODA, Yasuhiro MORIKAWA
5 ! Version:: $Id: gdncvarinquire.f90,v 1.2 2009-05-25 09:51:59 morikawa Exp $
6 ! Tag Name:: $Name: $
7 ! Copyright:: Copyright (C) GFD Dennou Club, 2000-2005. All rights reserved.
8 ! License:: See COPYRIGHT[link:../../COPYRIGHT]
9 !
10 ! 以下のサブルーチン、関数は gtdata_netcdf_generic から gtdata_netcdf_generic#Inquire
11 ! として提供されます。
12 !
13 
14 !--
15 ! 問い合わせは型ごとに手続をわけた。
16 !++
17 
18 subroutine gdncvarinquire(var, ndims, dimlen, growable, name, url, xtype)
23  use dc_trace, only: beginsub, endsub, dbgmessage
24  use netcdf, only: nf90_noerr, nf90_max_name, &
25  & nf90_inquire_variable, nf90_inquire_dimension, nf90_inquire
26  implicit none
27  type(gd_nc_variable), intent(in):: var
28  integer, intent(out), optional:: ndims
29  ! 変数の次元数
30  integer, intent(out), optional:: dimlen
31  ! 変数が1次元である場合、次元長
32  logical, intent(out), optional:: growable
33  ! 変数が成長可能次元を持つか
34  character(*), intent(out), optional:: name
35  ! 文字型引数が短いと値の切り詰めが起こりうる。'?' のあとの変数名
36  character(*), intent(out), optional:: url
37  ! 変数名、少なくともファイル名を含む、なるべく長い名前
38  character(*), intent(out), optional:: xtype
39  ! 変数の型名
40 
41  ! 内部変数
42  type(gd_nc_variable_entry):: ent
43  integer:: stat, length, i, i_xtype, idim_growable
44  character(len = *), parameter:: subname = 'GDNcVarInquire'
45  character(len = NF90_MAX_NAME):: buffer
46  character(len = NF90_MAX_NAME):: fbuffer
47 continue
48  call beginsub(subname, 'var.id=%d', i=(/var%id/))
49 
50  ! フェイルセーフ用にエラー値をまず入れる
51  if (present(ndims)) ndims = -1
52  if (present(dimlen)) dimlen = -1
53 
54  ! 変数実体の探索
55  stat = vtable_lookup(var, ent)
56  if (stat /= nf90_noerr) then
57  call endsub(subname, 'var not found')
58  return
59  endif
60 
61  ! 各引数が与えられている場合について値を取得する動作を
62 
63  if (present(ndims)) then
64  if (associated(ent%dimids)) then
65  ndims = size(ent%dimids)
66  else
67  ndims = 0
68  endif
69  endif
70 
71  if (present(dimlen)) then
72  dimlen = 1
73  if (ent%dimid > 0) then
74  ! 実体に次元としての問い合わせが可能な場合
75  stat = nf90_inquire_dimension(ent%fileid, ent%dimid, len = dimlen)
76  if (stat /= nf90_noerr) then
77  dimlen = -1
78  call endsub(subname, 'dimlen err')
79  return
80  endif
81  else
82  ! 実体が変数として問い合わせるしかない場合
83  if (associated(ent%dimids)) then
84  do, i = 1, size(ent%dimids)
85  stat = nf90_inquire_dimension(ent%fileid, ent%dimids(i), len = length)
86  if (stat /= nf90_noerr) then
87  dimlen = -1
88  exit
89  endif
90  dimlen = dimlen * length
91  enddo
92  endif
93  endif
94  endif
95 
96  if (present(xtype)) then
97  stat = nf90_inquire_variable(ent%fileid, ent%varid, xtype=i_xtype)
98  if (stat /= nf90_noerr) i_xtype = 0
99  call gdncxtypename(i_xtype, xtype)
100  endif
101 
102  if (present(name)) then
103  call local_getname(ent, buffer)
104  name = buffer
105  endif
106 
107  if (present(url)) then
108  call local_getname(ent, buffer)
109  call dbgmessage('ent%%fileid=%d', i=(/ent%fileid/))
110  call gdncfileinquire(ent%fileid, name=fbuffer)
111  url = trim(fbuffer) // '?' // buffer
112  endif
113 
114  if (present(growable)) then
115  growable = .false.
116  stat = vtable_lookup(var, ent)
117  if (stat /= nf90_noerr) return
118  stat = nf90_inquire(ent%fileid, unlimiteddimid = idim_growable)
119  if (stat /= nf90_noerr) return
120 
121  if (ent%varid > 0) then
122  if (.not. associated(ent%dimids)) return
123  do, i = 1, size(ent%dimids)
124  if (ent%dimids(i) == idim_growable) growable = .true.
125  enddo
126  else
127  growable = (ent%dimid == idim_growable)
128  endif
129  endif
130 
131  ! 安全に終った
132  call endsub(subname, 'ok')
133  return
134 
135 contains
136 
137  subroutine local_getname(ent, varname)
138  use netcdf, only: &
139  & nf90_inquire_dimension, nf90_inquire_variable, nf90_noerr
140  type(gd_nc_variable_entry), intent(in):: ent
141  character(len = *), intent(out):: varname
142  if (ent%dimid > 0) then
143  stat = nf90_inquire_dimension(ent%fileid, ent%dimid, name = varname)
144  else
145  stat = nf90_inquire_variable(ent%fileid, ent%varid, name = varname)
146  endif
147  if (stat /= nf90_noerr) varname = ""
148  end subroutine local_getname
149 
150 end subroutine gdncvarinquire
integer function, public vtable_lookup(var, entry)
subroutine gdncxtypename(ixtype, xtype)
subroutine, public dbgmessage(fmt, i, r, d, L, n, c1, c2, c3, ca)
Definition: dc_trace.f90:509
subroutine, public beginsub(name, fmt, i, r, d, L, n, c1, c2, c3, ca, version)
Definition: dc_trace.f90:351
subroutine gdncvarinquire(var, ndims, dimlen, growable, name, url, xtype)
subroutine local_getname(ent, varname)
subroutine, public endsub(name, fmt, i, r, d, L, n, c1, c2, c3, ca)
Definition: dc_trace.f90:446