22 #include <fms_platform.h> 31 #include<file_version.h> 37 character(len=MAX_STR_LEN), _allocatable :: position_names(:) _null
38 character(len=MAX_STR_LEN), _allocatable :: position_units(:) _null
39 character(len=MAX_STR_LEN), _allocatable :: field_names(:) _null
40 character(len=MAX_STR_LEN), _allocatable :: field_units(:) _null
41 character(len=MAX_STR_LEN), _allocatable :: velocity_names(:) _null
42 real , _allocatable :: positions(:,:) _null
43 integer , _allocatable :: ids(:) _null
44 character(len=MAX_STR_LEN) :: time_units
45 character(len=MAX_STR_LEN) :: title
46 character(len=MAX_STR_LEN) :: version
49 interface assignment(=)
60 character(len=*),
intent(in) :: filename
61 character(len=*),
intent(out):: ermesg
64 integer :: ier, ncid, nd, nf, np, ipos, j, id, i, isz
65 character(len=MAX_STR_LEN) :: attribute
70 ier = nf_open(filename, nf_nowrite, ncid)
71 if(ier/=nf_noerr)
then 72 ermesg =
'drifters_input: ERROR could not open netcdf file '//filename
77 ier = nf_put_att_text(ncid, nf_global,
'version', len(version), version)
79 ier = nf_inq_dimid(ncid,
'nd', id)
80 if(ier/=nf_noerr)
then 81 ermesg =
'drifters_input: ERROR could not find "nd" (number of dimensions)' 85 ier = nf_inq_dimlen(ncid, id, nd)
89 ier = nf_get_att_text(ncid, nf_global,
'field_names', attribute)
90 isz =
min(len(attribute), len(trim(attribute))+1)
91 attribute(isz:isz) =
' ' 100 ier = nf_inq_dimid(ncid,
'np', id)
101 if(ier/=nf_noerr)
then 102 ermesg =
'drifters_input: ERROR could not find "np" (number of particles)' 106 ier = nf_inq_dimlen(ncid, id, np)
108 allocate(self%position_names(nd))
109 allocate(self%position_units(nd))
110 allocate(self%field_names(nf))
111 allocate(self%field_units(nf))
112 allocate(self%velocity_names(nd))
113 allocate(self%ids(np))
114 allocate(self%positions(nd, np))
116 ier = nf_inq_varid(ncid,
'ids', id)
117 if(ier/=nf_noerr)
then 118 ermesg =
'drifters_input: ERROR could not find "ids"' 122 ier = nf_get_var_int(ncid, id, self%ids)
124 ier = nf_inq_varid(ncid,
'positions', id)
125 if(ier/=nf_noerr)
then 126 ermesg =
'drifters_input: ERROR could not find "positions"' 130 ier = nf_get_var_double(ncid, id, self%positions)
133 ier = nf_get_att_text(ncid, nf_global,
'version', attribute)
134 self%version = trim(attribute)
137 ier = nf_get_att_text(ncid, nf_global,
'time_units', attribute)
138 self%time_units = trim(attribute)
141 ier = nf_get_att_text(ncid, nf_global,
'title', attribute)
142 self%title = trim(attribute)
145 ier = nf_get_att_text(ncid, id,
'names', attribute)
146 isz =
min(len(attribute), len(trim(attribute))+1)
147 attribute(isz:isz) =
' ' 152 self%position_names(j) = trim(adjustl(attribute(ipos:i-1)))
160 ier = nf_get_att_text(ncid, id,
'units', attribute)
161 isz =
min(len(attribute), len(trim(attribute))+1)
162 attribute(isz:isz) =
' ' 167 self%position_units(j) = trim(adjustl(attribute(ipos:i-1)))
175 ier = nf_get_att_text(ncid, nf_global,
'field_names', attribute)
176 isz =
min(len(attribute), len(trim(attribute))+1)
177 attribute(isz:isz) =
' ' 182 self%field_names(j) = trim(adjustl(attribute(ipos:i-1)))
190 ier = nf_get_att_text(ncid, nf_global,
'field_units', attribute)
191 isz =
min(len(attribute), len(trim(attribute))+1)
192 attribute(isz:isz) =
' ' 197 self%field_units(j) = trim(adjustl(attribute(ipos:i-1)))
205 ier = nf_get_att_text(ncid, nf_global,
'velocity_names', attribute)
206 isz =
min(len(attribute), len(trim(attribute))+1)
207 attribute(isz:isz) =
' ' 212 self%velocity_names(j) = trim(adjustl(attribute(ipos:i-1)))
224 character(len=*),
intent(out):: ermesg
230 deallocate(self%position_names, stat=iflag)
231 deallocate(self%position_units, stat=iflag)
232 deallocate(self%field_names, stat=iflag)
233 deallocate(self%field_units, stat=iflag)
234 deallocate(self%velocity_names, stat=iflag)
235 deallocate(self%ids, stat=iflag)
236 deallocate(self%positions, stat=iflag)
243 type(drifters_input_type),
intent(inout) :: new_instance
244 type(drifters_input_type),
intent(in) :: old_instance
246 allocate(new_instance%position_names(
size(old_instance%position_names) ))
247 allocate(new_instance%position_units(
size(old_instance%position_units) ))
248 allocate(new_instance%field_names(
size(old_instance%field_names) ))
249 allocate(new_instance%field_units(
size(old_instance%field_units) ))
250 allocate(new_instance%velocity_names(
size(old_instance%velocity_names) ))
251 new_instance%position_names = old_instance%position_names
252 new_instance%position_units = old_instance%position_units
253 new_instance%field_names = old_instance%field_names
254 new_instance%field_units = old_instance%field_units
255 new_instance%velocity_names = old_instance%velocity_names
256 new_instance%time_units = old_instance%time_units
257 new_instance%title = old_instance%title
258 new_instance%version = old_instance%version
259 allocate(new_instance%positions(
size(old_instance%positions,1),
size(old_instance%positions,2) ))
260 new_instance%positions = old_instance%positions
261 allocate(new_instance%ids(
size(old_instance%ids)))
262 new_instance%ids = old_instance%ids
270 character(len=*),
intent(in ):: filename
271 real,
intent(in),
optional :: geolon(:), geolat(:)
272 character(len=*),
intent(out):: ermesg
274 integer ncid, nc_nd, nc_np, ier, nd, np, nf, nc_pos, nc_ids, i, j, n
275 integer nc_lon, nc_lat
276 character(len=MAX_STR_LEN) :: att
282 ier = nf_create(filename, nf_clobber, ncid)
283 if(ier/=nf_noerr)
then 284 ermesg =
'drifters_input: ERROR cannot create '//filename
288 nd =
size(self%positions, 1)
289 np =
size(self%positions, 2)
290 nf =
size(self%field_names)
293 ier = nf_def_dim(ncid,
'nd', nd, nc_nd)
294 if(ier/=nf_noerr) ermesg =
'drifters_input_save: ERROR creating dim "nd" '//nf_strerror(ier)
296 ier = nf_def_dim(ncid,
'np', np, nc_np)
297 if(ier/=nf_noerr) ermesg =
'drifters_input_save: ERROR creating dim "np" '//nf_strerror(ier)
300 ier = nf_put_att_text(ncid, nf_global,
'title', len_trim(self%title), self%title)
301 if(ier/=nf_noerr) ermesg =
'drifters_input_save: ERROR setting global att "title" ' &
304 ier = nf_put_att_text(ncid, nf_global,
'time_units', len_trim(self%time_units), self%time_units)
305 if(ier/=nf_noerr) ermesg =
'drifters_input_save: ERROR setting global att "time_units" ' &
311 n = len_trim(self%field_units(i))
312 att(j:j+n+1) = trim(self%field_units(i)) //
' ' 315 ier = nf_put_att_text(ncid, nf_global,
'field_units', len_trim(att), &
317 if(ier/=nf_noerr) ermesg =
'drifters_input_save: ERROR setting global att "field_units" ' &
323 n = len_trim(self%field_names(i))
324 att(j:j+n+1) = trim(self%field_names(i)) //
' ' 327 ier = nf_put_att_text(ncid, nf_global,
'field_names', len_trim(att), &
329 if(ier/=nf_noerr) ermesg =
'drifters_input_save: ERROR setting global att "field_names" ' &
335 n = len_trim(self%velocity_names(i))
336 att(j:j+n+1) = trim(self%velocity_names(i)) //
' ' 339 ier = nf_put_att_text(ncid, nf_global,
'velocity_names', len_trim(att), &
341 if(ier/=nf_noerr) ermesg =
'drifters_input_save: ERROR setting global att "velocity_names" ' &
345 ier = nf_def_var(ncid,
'positions', nf_double, 2, (/nc_nd, nc_np/), nc_pos)
346 if(ier/=nf_noerr) ermesg =
'drifters_input_save: ERROR creating var "positions" '//nf_strerror(ier)
348 ier = nf_def_var(ncid,
'ids', nf_int, 1, (/nc_np/), nc_ids)
349 if(ier/=nf_noerr) ermesg =
'drifters_input_save: ERROR creating var "ids" '//nf_strerror(ier)
352 if(
present(geolon))
then 353 ier = nf_def_var(ncid,
'longitude', nf_double, 1, (/nc_np/), nc_lon)
354 if(ier/=nf_noerr) ermesg =
'drifters_input_save: ERROR creating var "longitude" ' &
357 ier = nf_put_att_text(ncid, nc_lon,
'units', len(trim(att)), trim(att))
358 if(ier/=nf_noerr) ermesg =
'drifters_input_save: ERROR setting att "units" to "longitude" ' &
361 if(
present(geolat))
then 362 ier = nf_def_var(ncid,
'latitude', nf_double, 1, (/nc_np/), nc_lat)
363 if(ier/=nf_noerr) ermesg =
'drifters_input_save: ERROR creating var "latitude" ' &
365 att =
'degrees_north' 366 ier = nf_put_att_text(ncid, nc_lat,
'units', len(trim(att)), trim(att))
367 if(ier/=nf_noerr) ermesg =
'drifters_input_save: ERROR setting att "units" to "latitude" ' &
376 n = len_trim(self%position_units(i))
377 att(j:j+n+1) = trim(self%position_units(i)) //
' ' 380 ier = nf_put_att_text(ncid, nc_pos,
'units', len_trim(att), &
382 if(ier/=nf_noerr) ermesg =
'drifters_input_save: ERROR setting att "units" to "positions" ' &
388 n = len_trim(self%position_names(i))
389 att(j:j+n+1) = trim(self%position_names(i)) //
' ' 392 ier = nf_put_att_text(ncid, nc_pos,
'names', len_trim(att), &
394 if(ier/=nf_noerr) ermesg =
'drifters_input_save: ERROR setting att "names" to "positions" ' &
398 ier = nf_enddef(ncid)
399 if(ier/=nf_noerr) ermesg =
'drifters_input_save: ERROR could not end define mode ' &
403 ier = nf_put_var_double(ncid, nc_pos, self%positions)
404 if(ier/=nf_noerr) ermesg =
'drifters_input_save: ERROR could not write "positions" ' &
407 ier = nf_put_var_int(ncid, nc_ids, self%ids)
408 if(ier/=nf_noerr) ermesg =
'drifters_input_save: ERROR could not write "ids" ' &
411 if(
present(geolon))
then 412 ier = nf_put_var_double(ncid, nc_lon, geolon)
413 if(ier/=nf_noerr) ermesg =
'drifters_input_save: ERROR could not write "geolon" ' &
416 if(
present(geolat))
then 417 ier = nf_put_var_double(ncid, nc_lat, geolat)
418 if(ier/=nf_noerr) ermesg =
'drifters_input_save: ERROR could not write "geolat" ' &
424 if(ier/=nf_noerr) ermesg =
'drifters_input_save: ERROR could not close file ' &
433 #ifdef _TEST_DRIFTERS_INPUT 437 character(len=128) :: ermesg
443 if(ermesg/=
'') print *,
'ERROR: ', ermesg
445 print *,
'field_names:' 446 do i = 1,
size(obj%field_names)
447 print *,trim(obj%field_names(i))
450 print *,
'velocity_names:' 451 do i = 1,
size(obj%velocity_names)
452 print *,trim(obj%velocity_names(i))
455 print *,
'ids = ', obj%ids
457 print *,
'positions: ' 458 do i = 1,
size(obj%positions, 2)
459 print *,obj%positions(:,i)