15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
39 use gtdata_generic, only: open, inquire, close, create, copy_attr
41 use dc_trace, only: beginsub, endsub
43 implicit none
44 intrinsic trim
45 type(GT_VARIABLE), intent(out) :: var
46 character(len = *), intent(in) :: url
47 type(GT_VARIABLE), intent(inout) :: copyfrom
48 logical, intent(in), optional :: copyvalue
49 logical, intent(in), optional :: overwrite
50 logical, intent(out), optional :: err
51 type(GT_VARIABLE), allocatable :: vDimSource(:)
52 type(GT_VARIABLE), allocatable :: vDimDest(:)
53 integer :: i, nd, stat
54 logical :: myerr
55 character(STRING) :: vpart, upart, desturl
56 character(TOKEN) :: xtype
57 character(len = *), parameter:: version = &
58 & '$Name: $' // &
59 & '$Id: gtvarcreatecopy.f90,v 1.1 2009-03-20 09:09:51 morikawa Exp $'
60continue
61 call beginsub('gtvarcreatecopy', 'url=%c copyfrom=%d', &
62 & c1=trim(url), i=(/copyfrom%mapid/), version=version)
63 stat = 0
64 myerr = .false.
65
66
67
68
69 call inquire(copyfrom, alldims=nd)
70 allocate(vdimsource(nd), vdimdest(nd), stat=stat)
71 if (stat /= 0) goto 999
72 desturl = url
73
74
75
76 do, i = 1, nd
77 call open(vdimsource(i), copyfrom, dimord=i, &
78 & count_compact=.true., err=myerr)
80 & target=desturl)
81 end do
82
83
84
85
87 if (vpart == "") then
88 call inquire(copyfrom, url=upart)
90 desturl = trim(desturl) //
gt_atmark // trim(vpart)
91 end if
92
93 call inquire(copyfrom, xtype=xtype)
94 call create(var, trim(desturl), dims=vdimdest, xtype=xtype, &
95 & overwrite=overwrite, err=myerr)
96 if (myerr) goto 990
97 call copy_attr(to=var, from=copyfrom, err=myerr)
98 if (myerr) goto 990
99 if (present(copyvalue)) then
100 if (copyvalue) then
101 call gtvarcopyvalue(to=var, from=copyfrom)
102 endif
103 endif
104 do, i = 1, nd
105 call close(vdimsource(i))
106 call close(vdimdest(i))
107 end do
108990 continue
109 deallocate(vdimsource, vdimdest, stat=stat)
110999 continue
111 if (stat /= 0) then
113 else if (present(err)) then
114 err = myerr
115 else if (myerr) then
117 end if
118 call endsub('gtvarcreatecopy', 'result=%d', i=(/var%mapid/))
119contains
120
121
122
123
124
125
130 use gtdata_generic, only: open, inquire, create, copy_attr
131 type(GT_VARIABLE), intent(out):: to
132 type(GT_VARIABLE), intent(inout):: from
133 character(len = *), intent(in):: target
134 character(len = string):: url, file, dimname
135 character(len = token):: xtype
136 logical:: growable, myerr
137 integer:: length
138 continue
139 call beginsub('gtvarcopydim', 'from=%d target=<%c>', &
140 & i=(/from%mapid/), c1=trim(target))
141
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')
146 return
147 endif
148
149
151 if (lookupequivalent(to, from, file)) then
152
153 call endsub('gtvarcopydim', 'equivalent-exists')
154 return
155 else
156
157
158 call inquire(var=from, growable=growable, allcount=length)
159 if (growable) length = 0
160 call inquire(var=from, xtype=xtype, name=dimname)
161
163 call create(to, trim(url), length, xtype, err=myerr)
164 if (myerr) then
165
166 call create(to, trim(file), length, xtype)
167 endif
168 call copy_attr(to, from, myerr)
169 call gtvarcopyvalue(to, from)
170 call endsub('gtvarcopydim', 'created')
171 return
172 endif
174
175
176
177
178
179
180
181
182
183
184
185
186
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
196 integer:: len1, len2
197 character(len = *), parameter:: subnam = "lookupequivalent"
198 call beginsub(subnam, 'from=%d file=<%c>', &
199 & i=(/from%mapid/), c1=trim(file))
200 result = .false.
201
202 call inquire(from, allcount=len1, growable=growable1)
203 call get_attr(from, 'units', units1, default='')
204
205
206 call gtvarsearch(file)
207 do
208 call gtvarsearch(url, end)
209 if (end) exit
210 call open(to, url, writable=.true., err=end)
211 if (end) exit
212
213
214
215 call inquire(to, allcount=len2, growable=growable2)
216
217
218 if (.not. growable1 .or. .not. growable2) then
219
220
221 if (len1 /= len2) then
222 call close(to)
223 cycle
224 endif
225 call get_attr(to, 'units', units2, default='')
226
227 if (units1 /= units2) then
228 call close(to)
229 cycle
230 else
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.'
235 endif
236 else
237 reason = 'from is UNLIMITED dimension, and file has it'
238 endif
239 result = .true.
240 call endsub(subnam, 'found (%c)', c1=trim(reason))
241 return
242 enddo
243 call endsub(subnam, 'not found')
244 end function lookupequivalent
245
246
247
248 subroutine gtvarcopyvalue(to, from)
252 use dc_string
253 type(GT_VARIABLE), intent(inout):: to
254 type(GT_VARIABLE), intent(inout):: from
255 real, allocatable:: rbuffer(:)
256 logical:: err
257 integer:: siz, stat
258
259 call beginsub('gtvarcopyvalue')
260
261 call slice(from)
262 call slice(to, compatible=from)
263 call inquire(from, size=siz)
264 allocate (rbuffer(siz))
265 do
270 call slice_next(from, stat=stat)
271 if (stat /= 0) exit
272 call slice_next(to, stat=stat)
273 enddo
274 deallocate (rbuffer)
275 call endsub('gtvarcopyvalue')
276 end subroutine gtvarcopyvalue
277
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
character, parameter, public gt_atmark