gtool5 Fortran 90/95 ライブラリ 1.0.0-rc5
English
Loading...
Searching...
No Matches
gdncvarcreate.f90
Go to the documentation of this file.
1!> @file gdncvarcreate.f90
2!>
3!> @author Eizi TOYODA, Yasuhiro MORIKAWA
4!> @copyright Copyright (C) GFD Dennou Club, 2000-2026. All rights reserved. <br/>
5!> License is BSD-2-Clause. See [COPYRIGHT](@ref COPYRIGHT) in detail
6!>
7!> @en
8!> @brief Create variable in netCDF file
9!>
10!> These subroutines and functions are provided as gtdata_netcdf_generic#Create
11!> through gtdata_netcdf_generic.
12!> @enden
13!>
14!> @ja
15!> @brief netCDF ファイルへ変数作成
16!>
17!> これらのサブルーチン、関数は gtdata_netcdf_generic から
18!> gtdata_netcdf_generic#Create として提供されます。
19!> @endja
20!>
21
22!>
23!> @en
24!> @brief Create a variable
25!>
26!> Creates a variable at the specified URL.
27!> The dimensions the variable depends on are given in dims.
28!> The returned var contains the variable ID and other information.
29!>
30!> Setting overwrite to .true. enables overwrite mode (default: no overwrite).
31!> If err is provided, errors return .false. instead of terminating.
32!> @enden
33!>
34!> @ja
35!> @brief 変数を作成
36!>
37!> 変数 URL url に変数を作成します。
38!> 変数が依存する次元を dims に与えます。
39!> 返される引数 var には変数 ID などの情報が格納されます。
40!>
41!> overwrite に .true. を設定すると上書き可能モード (デフォルト: 上書き不可)。
42!> err を与えた場合、エラー時はプログラム終了せず .false. が返されます。
43!> @endja
44!>
45!> @param[out] var @en Created variable handle @enden @ja 作成した変数ハンドル @endja
46!> @param[in] url @en Variable URL @enden @ja 変数 URL @endja
47!> @param[in] xtype @en Data type (float, double, int, char) @enden
48!> @ja データ型 (float, double, int, char) @endja
49!> @param[in] dims @en Array of dimension variables @enden @ja 次元変数の配列 @endja
50!> @param[in] overwrite @en Enable overwrite mode (optional) @enden
51!> @ja 上書きモードを有効化 (省略可能) @endja
52!> @param[out] err @en Error flag (optional) @enden @ja エラーフラグ (省略可能) @endja
53!>
54subroutine gdncvarcreate(var, url, xtype, dims, overwrite, err)
56 use dc_types, only: string
57 use dc_string, only: strieq
60 use dc_url, only: urlsplit
62 use gtdata_netcdf_generic, only: tostring ! for debug
63 use netcdf, only: &
64 & nf90_noerr, nf90_float, nf90_double, nf90_int, nf90_char, nf90_ebaddim, nf90_def_var
67 implicit none
68 type(gd_nc_variable), intent(out):: var
69 character(len = *), intent(in):: url
70 character(len = *), intent(in):: xtype
71 type(gd_nc_variable), intent(in):: dims(:)
72 logical, intent(in), optional:: overwrite
73 logical, intent(out), optional:: err
74 type(gd_nc_variable_search):: ent
75 type(gd_nc_variable_entry):: ent_dim
76 character(len = string):: filename, varname
77 integer, allocatable:: dimids(:)
78 integer:: stat, nvdims, i
79 integer:: nc_xtype
80 logical:: clobber
81 intrinsic trim
82 character(len = *), parameter:: subnam = "GDNcVarCreate"
83continue
84 clobber = .false.
85 if (present(overwrite)) clobber = overwrite
86 call beginsub(subnam)
87 call dbgmessage('url=%c', c1=trim(url))
88 call dbgmessage('xtype=%c', c1=trim(xtype))
89 call dbgmessage('dims=(/%*d/)', i=(/dims(:)%id/), n=(/size(dims)/))
90 call dbgmessage('ovwr=%y', l=(/clobber/))
91
92 ! もし必要ならファイル作成
93 call urlsplit(url, filename, varname)
94 call gdncfileopen(ent%fileid, filename, stat=stat, writable=.true., &
95 & overwrite=clobber)
96 if (stat /= nf90_noerr) goto 999
97
98 ! 次元にまつわる準備
99 nvdims = size(dims)
100 allocate(dimids(max(1, nvdims)), stat=stat)
101 if (stat /= 0) then
102 stat = gt_enomem
103 goto 999
104 end if
105 do, i = 1, nvdims
106 stat = vtable_lookup(dims(i), ent_dim)
107 if (stat /= nf90_noerr) then
108 stat = nf90_ebaddim
109 goto 999
110 endif
111 if (ent%fileid /= ent_dim%fileid) then
112 stat = gt_eotherfile
113 goto 999
114 endif
115 if (ent_dim%dimid <= 0) then
116 stat = gt_edimmultidim
117 goto 999
118 endif
119 dimids(i) = ent_dim%dimid
120 enddo
121 ent%dimid = 0
122
123 ! 変数の型の判定
124 nc_xtype = nf90_float
125 if (strieq(xtype, "double")) then
126 nc_xtype = nf90_double
127 else if (strieq(xtype, "DOUBLEPRECISION")) then
128 nc_xtype = nf90_double
129 end if
130 if (strieq(xtype, "int")) then
131 nc_xtype = nf90_int
132 else if (strieq(xtype, "INTEGER")) then
133 nc_xtype = nf90_int
134 end if
135 if (strieq(xtype, "char")) then
136 nc_xtype = nf90_char
137 else if (strieq(xtype, "CHARACTER")) then
138 nc_xtype = nf90_char
139 end if
140
141 ! 本当の変数作成操作
142 stat = gdncfiledefinemode(ent%fileid)
143 if (stat /= nf90_noerr) goto 999
144 if ( nvdims == 0 ) then
145 stat = nf90_def_var(ent%fileid, name = trim(varname), &
146 & xtype = nc_xtype, varid=ent%varid)
147 else
148 stat = nf90_def_var(ent%fileid, name = trim(varname), &
149 & xtype = nc_xtype, dimids = dimids, varid=ent%varid)
150 end if
151 if (stat /= nf90_noerr) goto 999
152
153 ! 登録
154 stat = vtable_add(var, ent)
155
156999 continue
157 if (allocated(dimids)) deallocate(dimids)
158 if (stat /= nf90_noerr) var % id = -1
159 call storeerror(stat, subnam, err, cause_c=url)
160 call endsub(subnam, 'stat=%d, var.id=%d', i=(/stat, var % id/))
161end subroutine
subroutine gdncvarcreate(var, url, xtype, dims, overwrite, err)
変数に関する問い合わせ
エラー処理用モジュール
Definition dc_error.f90:454
subroutine, public storeerror(number, where, err, cause_c, cause_i)
Definition dc_error.f90:891
integer, parameter, public gt_eotherfile
Definition dc_error.f90:514
integer, parameter, public gt_enomem
Definition dc_error.f90:513
integer, parameter, public gt_edimmultidim
Definition dc_error.f90:509
文字型変数の操作
Definition dc_string.f90:83
デバッグ時の追跡用モジュール
Definition dc_trace.f90:150
subroutine, public dbgmessage(fmt, i, r, d, l, n, c1, c2, c3, ca)
Definition dc_trace.f90:661
subroutine, public beginsub(name, fmt, i, r, d, l, n, c1, c2, c3, ca, version)
Definition dc_trace.f90:457
subroutine, public endsub(name, fmt, i, r, d, l, n, c1, c2, c3, ca)
Definition dc_trace.f90:580
種別型パラメタを提供します。
Definition dc_types.f90:55
integer, parameter, public string
文字列を保持する 文字型変数の種別型パラメタ
Definition dc_types.f90:137
変数 URL の文字列解析
Definition dc_url.f90:61
integer function, public vtable_add(var, entry)
integer function, public vtable_lookup(var, entry)