39 use gtdata_generic,
only: open, inquire, close, create, copy_attr
41 use dc_trace,
only: beginsub, endsub
46 character(len = *),
intent(in) :: url
48 logical,
intent(in),
optional :: copyvalue
49 logical,
intent(in),
optional :: overwrite
50 logical,
intent(out),
optional :: err
53 integer :: i, nd, stat
55 character(STRING) :: vpart, upart, desturl
56 character(TOKEN) :: xtype
57 character(len = *),
parameter:: version = &
59 &
'$Id: gtvarcreatecopy.f90,v 1.1 2009-03-20 09:09:51 morikawa Exp $'
61 call beginsub(
'gtvarcreatecopy',
'url=%c copyfrom=%d', &
62 & c1=trim(url), i=(/copyfrom%mapid/), version=version)
69 call inquire(copyfrom, alldims=nd)
70 allocate(vdimsource(nd), vdimdest(nd), stat=stat)
71 if (stat /= 0)
goto 999
77 call open(vdimsource(i), copyfrom, dimord=i, &
78 & count_compact=.true., err=myerr)
88 call inquire(copyfrom, url=upart)
90 desturl = trim(desturl) //
gt_atmark // trim(vpart)
93 call inquire(copyfrom, xtype=xtype)
94 call create(var, trim(desturl), dims=vdimdest, xtype=xtype, &
95 & overwrite=overwrite, err=myerr)
97 call copy_attr(to=var, from=copyfrom, err=myerr)
99 if (
present(copyvalue))
then
101 call gtvarcopyvalue(to=var, from=copyfrom)
105 call close(vdimsource(i))
106 call close(vdimdest(i))
109 deallocate(vdimsource, vdimdest, stat=stat)
113 else if (
present(err))
then
118 call endsub(
'gtvarcreatecopy',
'result=%d', i=(/var%mapid/))
130 use gtdata_generic,
only: open, inquire, create, copy_attr
133 character(len = *),
intent(in):: target
134 character(len = string):: url, file, dimname
135 character(len = token):: xtype
136 logical:: growable, myerr
139 call beginsub(
'gtvarcopydim',
'from=%d target=<%c>', &
140 & i=(/from%mapid/), c1=trim(
target))
142 call inquire(var=from, url=url)
143 if (trim(url) .onthesamefile. trim(
target))
then
144 call open(to, from, dimord=0)
145 call endsub(
'gtvarcopydim',
'dup-handle')
151 if (lookupequivalent(to, from, file))
then
153 call endsub(
'gtvarcopydim',
'equivalent-exists')
158 call inquire(var=from, growable=growable, allcount=length)
159 if (growable) length = 0
160 call inquire(var=from, xtype=xtype, name=dimname)
163 call create(to, trim(url), length, xtype, err=myerr)
166 call create(to, trim(file), length, xtype)
168 call copy_attr(to, from, myerr)
169 call gtvarcopyvalue(to, from)
170 call endsub(
'gtvarcopydim',
'created')
187 logical function lookupequivalent(to, from, file)
result(result)
189 use dc_string,
only: tochar
190 use gtdata_generic,
only: inquire, gtvarsearch, open, get_attr
191 type(gt_variable),
intent(out):: to
192 type(gt_variable),
intent(in):: from
193 character(len = *),
intent(in):: file
194 character(len = string):: url, units1, units2, reason
195 logical:: end, growable1, growable2
197 character(len = *),
parameter:: subnam =
"lookupequivalent"
198 call beginsub(subnam,
'from=%d file=<%c>', &
199 & i=(/from%mapid/), c1=trim(file))
202 call inquire(from, allcount=len1, growable=growable1)
203 call get_attr(from,
'units', units1, default=
'')
206 call gtvarsearch(file)
208 call gtvarsearch(url,
end)
210 call open(to, url, writable=.true., err=
end)
215 call inquire(to, allcount=len2, growable=growable2)
218 if (.not. growable1 .or. .not. growable2)
then
221 if (len1 /= len2)
then
225 call get_attr(to,
'units', units2, default=
'')
227 if (units1 /= units2)
then
231 reason =
'length of from is ' // trim(tochar(len1)) // &
232 &
'. units of from is ' //
"[" // &
233 & trim(units1) //
"]" // &
234 &
'. And file has same length and units.'
237 reason =
'from is UNLIMITED dimension, and file has it'
240 call endsub(subnam,
'found (%c)', c1=trim(reason))
243 call endsub(subnam,
'not found')
244 end function lookupequivalent
248 subroutine gtvarcopyvalue(to, from)
255 real,
allocatable:: rbuffer(:)
259 call beginsub(
'gtvarcopyvalue')
262 call slice(to, compatible=from)
263 call inquire(from, size=siz)
264 allocate (rbuffer(siz))
270 call slice_next(from, stat=stat)
272 call slice_next(to, stat=stat)
275 call endsub(
'gtvarcopyvalue')
276 end subroutine gtvarcopyvalue