変数 copyfrom と同じ次元、属性を持った変数を url に作成します。 必要ならば次元変数も複製されます。 copyvalue を .true. に指定すると値も複製されます。 作成された変数の ID は var に返されます。
既存変数があるとき失敗しますが、overwrite == .true. であれば 上書きして続行します。(まだ overwrite の動作は保障されていません)。
作成の際にエラーが生じた場合、メッセージを出力してプログラムは 強制終了します。err を与えてある場合にはこの引数に .true. が返り、プログラムは終了しません。
81 implicit none
82 intrinsic trim
83 type(GT_VARIABLE), intent(out) :: var
84 character(len = *), intent(in) :: url
85 type(GT_VARIABLE), intent(inout) :: copyfrom
86 logical, intent(in), optional :: copyvalue
87 logical, intent(in), optional :: overwrite
88 logical, intent(out), optional :: err
89 type(GT_VARIABLE), allocatable :: vDimSource(:)
90 type(GT_VARIABLE), allocatable :: vDimDest(:)
91 integer :: i, nd, stat
92 logical :: myerr
93 character(STRING) :: vpart, upart, desturl
94 character(TOKEN) :: xtype
95 character(len = *), parameter:: version = &
96 & '$Name: $' // &
97 & '$Id: gtvarcreatecopy.f90,v 1.1 2009-03-20 09:09:51 morikawa Exp $'
98continue
99 call beginsub(
'gtvarcreatecopy',
'url=%c copyfrom=%d', &
100 & c1=trim(url), i=(/copyfrom%mapid/), version=version)
101 stat = 0
102 myerr = .false.
103
104
105
106
107 call inquire(copyfrom, alldims=nd)
108 allocate(vdimsource(nd), vdimdest(nd), stat=stat)
109 if (stat /= 0) goto 999
110 desturl = url
111
112
113
114 do, i = 1, nd
115 call open(vdimsource(i), copyfrom, dimord=i, &
116 & count_compact=.true., err=myerr)
118 & target=desturl)
119 end do
120
121
122
123
125 if (vpart == "") then
126 call inquire(copyfrom, url=upart)
128 desturl = trim(desturl) //
gt_atmark // trim(vpart)
129 end if
130
131 call inquire(copyfrom, xtype=xtype)
132 call create(var, trim(desturl), dims=vdimdest, xtype=xtype, &
133 & overwrite=overwrite, err=myerr)
134 if (myerr) goto 990
135 call copy_attr(to=var, from=copyfrom, err=myerr)
136 if (myerr) goto 990
137 if (present(copyvalue)) then
138 if (copyvalue) then
139 call gtvarcopyvalue(to=var, from=copyfrom)
140 endif
141 endif
142 do, i = 1, nd
143 call close(vdimsource(i))
144 call close(vdimdest(i))
145 end do
146990 continue
147 deallocate(vdimsource, vdimdest, stat=stat)
148999 continue
149 if (stat /= 0) then
151 else if (present(err)) then
152 err = myerr
153 else if (myerr) then
155 end if
156 call endsub(
'gtvarcreatecopy',
'result=%d', i=(/var%mapid/))
157contains
158
159
160
161
162
163
169 type(GT_VARIABLE), intent(out):: to
170 type(GT_VARIABLE), intent(inout):: from
171 character(len = *), intent(in):: target
172 character(len = string):: url, file, dimname
173 character(len = token):: xtype
174 logical:: growable, myerr
175 integer:: length
176 continue
177 call beginsub(
'gtvarcopydim',
'from=%d target=<%c>', &
178 & i=(/from%mapid/), c1=trim(target))
179
180 call inquire(var=from, url=url)
181 if (trim(url) .onthesamefile. trim(target)) then
182 call open(to, from, dimord=0)
183 call endsub(
'gtvarcopydim',
'dup-handle')
184 return
185 endif
186
187
189 if (lookupequivalent(to, from, file)) then
190
191 call endsub(
'gtvarcopydim',
'equivalent-exists')
192 return
193 else
194
195
196 call inquire(var=from, growable=growable, allcount=length)
197 if (growable) length = 0
198 call inquire(var=from, xtype=xtype, name=dimname)
199
201 call create(to, trim(url), length, xtype, err=myerr)
202 if (myerr) then
203
204 call create(to, trim(file), length, xtype)
205 endif
207 call gtvarcopyvalue(to, from)
208 call endsub(
'gtvarcopydim',
'created')
209 return
210 endif
212
213
214
215
216
217
218
219
220
221
222
223
224
225 logical function lookupequivalent(to, from, file) result(result)
229 type(GT_VARIABLE), intent(out):: to
230 type(GT_VARIABLE), intent(in):: from
231 character(len = *), intent(in):: file
232 character(len = string):: url, units1, units2, reason
233 logical:: end, growable1, growable2
234 integer:: len1, len2
235 character(len = *), parameter:: subnam = "lookupequivalent"
236 call beginsub(subnam,
'from=%d file=<%c>', &
237 & i=(/from%mapid/), c1=trim(file))
238 result = .false.
239
240 call inquire(from, allcount=len1, growable=growable1)
241 call get_attr(from,
'units', units1, default=
'')
242
243
245 do
247 if (end) exit
248 call open(to, url, writable=.true., err=end)
249 if (end) exit
250
251
252
253 call inquire(to, allcount=len2, growable=growable2)
254
255
256 if (.not. growable1 .or. .not. growable2) then
257
258
259 if (len1 /= len2) then
260 call close(to)
261 cycle
262 endif
263 call get_attr(to, 'units', units2, default='')
264
265 if (units1 /= units2) then
266 call close(to)
267 cycle
268 else
269 reason = 'length of from is ' // trim(tochar(len1)) // &
270 & '. units of from is ' // "[" // &
271 & trim(units1) // "]" // &
272 & '. And file has same length and units.'
273 endif
274 else
275 reason = 'from is UNLIMITED dimension, and file has it'
276 endif
277 result = .true.
278 call endsub(subnam,
'found (%c)', c1=trim(reason))
279 return
280 enddo
281 call endsub(subnam,
'not found')
282 end function lookupequivalent
283
284
285
286 subroutine gtvarcopyvalue(to, from)
291 type(GT_VARIABLE), intent(inout):: to
292 type(GT_VARIABLE), intent(inout):: from
293 real, allocatable:: rbuffer(:)
294 logical:: err
295 integer:: siz, stat
296
298
300 call slice(to, compatible=from)
302 allocate (rbuffer(siz))
303 do
309 if (stat /= 0) exit
311 enddo
312 deallocate (rbuffer)
313 call endsub(
'gtvarcopyvalue')
314 end subroutine gtvarcopyvalue
315
subroutine gtvarcopydim(to, from, target)
subroutine gtvargetreal(var, value, nvalue, err)
subroutine gtvarputreal(var, value, nvalue, err)
手続引用仕様. いずれ差し替えられるように外部関数にしておく。
subroutine, public storeerror(number, where, err, cause_c, cause_i)
integer, parameter, public gt_enomem
subroutine, public beginsub(name, fmt, i, r, d, l, n, c1, c2, c3, ca, version)
subroutine, public endsub(name, fmt, i, r, d, l, n, c1, c2, c3, ca)
character, parameter, public gt_atmark