32 #include<file_version.h> 42 integer :: nc_positions, nc_fields, nc_ids, nc_time, nc_index_time
51 character(len=*),
intent(in) :: filename
52 integer,
intent(in) :: nd
53 integer,
intent(in) :: nf
54 character(len=*),
intent(out) :: ermesg
56 integer ier, nc_it_id, nc_nd, nc_nf
57 integer :: size1(1), size2(2)
63 ier = nf_create(filename, nf_clobber, self%ncid)
64 if(ier/=nf_noerr) ermesg =
'drifters_io_new::nf_create ('//filename//
') '//nf_strerror(ier)
67 ier = nf_put_att_text(self%ncid, nf_global,
'version', len_trim(version), trim(version))
71 ier = nf_def_dim(self%ncid,
'it_id', nf_unlimited, nc_it_id)
72 if(ier/=nf_noerr) ermesg =
'drifters_io_new::nf_def_dim (it_id) '//nf_strerror(ier)
74 ier = nf_def_dim(self%ncid,
'nf', nf, nc_nf)
75 if(ier/=nf_noerr) ermesg =
'drifters_io_new::nf_def_dim (nf) '//nf_strerror(ier)
77 ier = nf_def_dim(self%ncid,
'nd', nd, nc_nd)
78 if(ier/=nf_noerr) ermesg =
'drifters_io_new::nf_def_dim (nd) '//nf_strerror(ier)
82 ier = nf_def_var(self%ncid,
'index_time', nf_int, 1, size1, self%nc_index_time)
83 if(ier/=nf_noerr) ermesg =
'drifters_io_new::nf_def_var (index_time)'//nf_strerror(ier)
85 ier = nf_def_var(self%ncid,
'time', nf_double, 1, size1, self%nc_time)
86 if(ier/=nf_noerr) ermesg =
'drifters_io_new::nf_def_var (time)'//nf_strerror(ier)
88 ier = nf_def_var(self%ncid,
'ids', nf_int, 1, size1, self%nc_ids)
89 if(ier/=nf_noerr) ermesg =
'drifters_io_new::nf_def_var (ids)'//nf_strerror(ier)
91 size2 = (/nc_nd, nc_it_id/)
92 ier = nf_def_var(self%ncid,
'positions', nf_double, 2, size2, self%nc_positions)
93 if(ier/=nf_noerr) ermesg =
'drifters_io_new::nf_def_var (positions)'//nf_strerror(ier)
95 size2 = (/nc_nf, nc_it_id/)
96 ier = nf_def_var(self%ncid,
'fields', nf_double, 2, size2, self%nc_fields)
97 if(ier/=nf_noerr) ermesg =
'drifters_io_new::nf_def_var (fields)'//nf_strerror(ier)
108 character(len=*),
intent(out) :: ermesg
115 ier = nf_close(self%ncid)
116 if(ier/=nf_noerr) ermesg =
'drifters_io_del::nf_close '//nf_strerror(ier)
123 character(len=*),
intent(in) :: name
124 character(len=*),
intent(out) :: ermesg
130 ier = nf_put_att_text(self%ncid, nf_global, &
131 &
'time_units', len_trim(name), trim(name))
133 & ermesg =
'drifters_io_set_time_units::failed to add time_units attribute ' &
141 character(len=*),
intent(in) :: names(:)
142 character(len=*),
intent(out) :: ermesg
145 character(len=128) :: attname
152 write(attname,
'(i6)' ) i
153 attname =
'name_'//adjustl(attname)
154 ier = nf_put_att_text(self%ncid, self%nc_positions, &
155 & trim(attname), len_trim(names(i)), trim(names(i)))
157 & ermesg =
'drifters_io_set_position_names::failed to add name attribute to positions '//nf_strerror(ier)
165 character(len=*),
intent(in) :: names(:)
166 character(len=*),
intent(out) :: ermesg
169 character(len=128) :: attname
176 write(attname,
'(i6)' ) i
177 attname =
'unit_'//adjustl(attname)
178 ier = nf_put_att_text(self%ncid, self%nc_positions, &
179 & trim(attname), len_trim(names(i)), trim(names(i)))
181 & ermesg =
'drifters_io_set_position_names::failed to add unit attribute to positions '//nf_strerror(ier)
189 character(len=*),
intent(in) :: names(:)
190 character(len=*),
intent(out) :: ermesg
193 character(len=128) :: attname
200 write(attname,
'(i6)' ) i
201 attname =
'name_'//adjustl(attname)
202 ier = nf_put_att_text(self%ncid, self%nc_fields, &
203 & trim(attname), len_trim(names(i)), trim(names(i)))
205 & ermesg =
'drifters_io_set_field_names::failed to add name attribute to fields '//nf_strerror(ier)
213 character(len=*),
intent(in) :: names(:)
214 character(len=*),
intent(out) :: ermesg
217 character(len=128) :: attname
224 write(attname,
'(i6)' ) i
225 attname =
'unit_'//adjustl(attname)
226 ier = nf_put_att_text(self%ncid, self%nc_fields, &
227 & trim(attname), len_trim(names(i)), trim(names(i)))
229 & ermesg =
'drifters_io_set_field_units::failed to add unit attribute to fields '//nf_strerror(ier)
235 subroutine drifters_io_write(self, time, np, nd, nf, ids, positions, fields, ermesg)
237 real,
intent(in) :: time
238 integer,
intent(in) :: np
239 integer,
intent(in) :: nd
240 integer,
intent(in) :: nf
241 integer,
intent(in) :: ids(np)
242 real,
intent(in) :: positions(nd,np)
243 real,
intent(in) :: fields(nf,np)
244 character(len=*),
intent(out) :: ermesg
247 integer :: start1(1), len1(1), start2(2), len2(2)
248 integer :: it_indices(np)
249 real :: time_array(np)
254 if(.not. self%enddef)
then 255 ier = nf_enddef(self%ncid)
256 if(ier/=nf_noerr)
then 257 ermesg =
'drifters_io_write::nf_enddef failure. No data will be written. '//nf_strerror(ier)
264 self%it = self%it + 1
268 start1(1) = self%it_id
271 it_indices = (/(self%it,i=1,np)/)
272 ier = nf_put_vara_int( self%ncid, self%nc_index_time, start1, len1, it_indices )
274 & ermesg =
'drifters_io_write::failed to write index_time: ' //nf_strerror(ier)
276 time_array = (/(time,i=1,np)/)
277 ier = nf_put_vara_double( self%ncid, self%nc_time, start1, len1, time_array )
279 & ermesg =
'drifters_io_write::failed to write time: ' //nf_strerror(ier)
281 ier = nf_put_vara_int(self%ncid, self%nc_ids, start1, len1, ids)
283 & ermesg =
'drifters_io_write::failed to write ids: '//nf_strerror(ier)
286 start2(2) = self%it_id
291 ier = nf_put_vara_double(self%ncid, self%nc_positions, start2, len2, positions)
293 & ermesg =
'drifters_io_write::failed to write positions: '//nf_strerror(ier)
298 ier = nf_put_vara_double(self%ncid, self%nc_fields, start2, len2, fields)
300 & ermesg =
'drifters_io_write::failed to write fields: '//nf_strerror(ier)
302 self%it_id = self%it_id + np
309 #ifdef _TEST_DRIFTERS_IO 319 character(len=128) :: ermesg
320 character(len=31) :: filename
321 integer :: np, nd, nf, nt, i, j, k, npmax
322 real :: dt, time, xmin, xmax, ymin, ymax, u, v, dr, x, y
323 integer,
allocatable :: ids(:)
324 real,
allocatable :: positions(:,:), fields(:,:)
344 u = (xmax-xmin)*sqrt(2.)
345 v = (ymax-ymin)*sqrt(2.)
352 if(ermesg/=
'') print *,
'ERROR after drifters_io_new: ', ermesg
357 if(ermesg/=
'') print *,
'ERROR after drifters_io_position_names: ', ermesg
363 if(ermesg/=
'') print *,
'ERROR after drifters_io_field_names: ', ermesg
366 if(ermesg/=
'') print *,
'ERROR after drifters_io_position_units: ', ermesg
369 if(ermesg/=
'') print *,
'ERROR after drifters_io_field_units: ', ermesg
371 allocate(positions(nd, npmax), ids(npmax), fields(nf, npmax))
372 dr = sqrt( (xmax-xmin)**2 + (ymax-ymin)**2 )/
real(npmax)
376 positions(1, :) = +(/ (i*dr,i=0,npmax-1) /)/sqrt(2.)
378 positions(2, :) = -(/ (i*dr,i=0,npmax-1) /)/sqrt(2.)
383 ids = (/ (i, i=1, npmax) /)
386 fields(1, :) = sqrt( (positions(1,:)-xmin)**2 + (positions(2,:)-ymin)**2 )
387 fields(2, :) = positions(1,:)-u*time + positions(2,:)-v*time
393 if(x>=xmin .and. x<=xmax .and. y>=ymin .and. y<=ymax)
then 395 & ids=ids(i), positions=positions(:,i), fields=fields(:,i), ermesg=ermesg)
396 if(ermesg/=
'') print *,
'ERROR after drifters_io_write: ', ermesg
404 positions(1, :) = positions(1, :) + u*dt
405 positions(2, :) = positions(2, :) + v*dt
406 fields(1, :) = sqrt( (positions(1,:)-xmin)**2 + (positions(2,:)-ymin)**2 )
407 fields(2, :) = positions(1,:)-u*time + positions(2,:)-v*time
412 if(x>=xmin .and. x<=xmax .and. y>=ymin .and. y<=ymax)
then 414 & ids=ids(i), positions=positions(:,i), fields=fields(:,i), ermesg=ermesg)
415 if(ermesg/=
'') print *,
'ERROR after drifters_io_write: ', ermesg
421 deallocate(positions, ids, fields)
424 if(ermesg/=
'') print *,
'ERROR after drifters_io_del: ', ermesg
subroutine, public drifters_io_del(self, ermesg)
subroutine, public drifters_io_new(self, filename, nd, nf, ermesg)
subroutine, public drifters_io_set_field_units(self, names, ermesg)
subroutine, public drifters_io_set_field_names(self, names, ermesg)
integer, parameter, private max_str_len
subroutine, public drifters_io_set_time_units(self, name, ermesg)
subroutine, public drifters_io_set_position_units(self, names, ermesg)
subroutine, public drifters_io_write(self, time, np, nd, nf, ids, positions, fields, ermesg)
subroutine, public drifters_io_set_position_names(self, names, ermesg)