19 use odb_module,
only: &
26 use,
intrinsic :: iso_c_binding, only: &
33 character(len=1000) :: filename
34 character(len=1000) :: output_filename
37 character(len=7000) :: messages(4)
38 real,
allocatable :: latitudes(:)
39 real,
allocatable :: longitudes(:)
40 real,
allocatable :: times_netcdf(:)
41 real,
allocatable :: observations(:)
42 real,
allocatable :: pressures(:)
43 integer,
allocatable :: times(:)
44 integer,
allocatable :: dates(:)
45 integer(kind=c_int) :: odb_rc
49 integer :: dummy_num_rows
50 integer :: num_columns
51 real(kind=c_double),
allocatable :: desc(:,:)
52 real(kind=c_double),
allocatable :: hdr(:,:)
53 real(kind=c_double),
allocatable :: body(:,:)
55 integer :: pool_number
58 function setenv (envname, envval, overwrite) bind (c, name = "setenv")
59 use,
intrinsic :: iso_c_binding, only: &
62 character(kind=c_char) :: envname(*)
63 character(kind=c_char) :: envval(*)
64 integer(kind=c_int),
value :: overwrite
65 integer(kind=c_int) :: setenv
69 call get_command_argument (1, filename)
70 call get_command_argument (2, output_filename)
72 if (filename ==
"" .or. output_filename ==
"")
then 73 write (messages(1),
'(a,i0)')
"Please provide two arguments, input-filename output-filename" 74 call fail (messages(1:1))
77 rc = nf90_open(filename, nf90_nowrite, file_id)
78 if (rc /= nf90_noerr)
then 79 write (messages(1),
'(a,i0)')
"nf90_open failed with rc = ", rc
80 write (messages(2),
'(2a)')
"file name = ", trim(filename)
81 messages(3) = nf90_strerror(rc)
82 call fail (messages(1:3))
89 allocate (times(
size(times_netcdf)))
90 allocate (dates(
size(times_netcdf)))
92 rc = nf90_get_att(file_id, nf90_global,
"date_time", date_time)
93 if (rc /= nf90_noerr)
then 94 write (messages(1),
'(a,i0)')
"nf90_get_at failed for date_time with rc = ", rc
95 write (messages(2),
'(a,i0)')
"file id = ", file_id
96 messages(3) = nf90_strerror(rc)
97 call fail (messages(1:3))
100 dates = date_time / 100
101 times = (date_time - (date_time / 100) * 100) * 10000
102 times = times + 10000 * int(times_netcdf)
103 times_netcdf = times_netcdf - int(times_netcdf)
104 where (times_netcdf >= 0)
105 times = times + nint(60 * times_netcdf) * 100
107 times = times - 10000
108 times = times + nint(60 * (1 - abs(times_netcdf))) * 100
114 rc = nf90_close(file_id)
115 if (rc /= nf90_noerr)
then 116 write (messages(1),
'(a,i0)')
"nf90_close failed with rc = ", rc
117 write (messages(2),
'(a,i0)')
"file id = ", file_id
118 messages(3) = nf90_strerror(rc)
119 call fail (messages(1:3))
122 odb_rc = setenv(
"ODB_DATAPATH_OOPS" // c_null_char, &
123 trim(output_filename) // c_null_char, &
126 odb_rc = setenv(
"ODB_SRCPATH_OOPS" // c_null_char, &
127 trim(output_filename) // c_null_char, &
130 odb_rc = setenv(
"IOASSIGN" // c_null_char, &
131 trim(output_filename) //
"/IOASSIGN" // c_null_char, &
134 call execute_command_line (
"unset ODB_COMPILER_FLAGS && & 135 &create_ioassign -l OOPS -d " // trim(output_filename))
138 handle = odb_open(
"OOPS",
"NEW", num_pools)
140 odb_rc = odb_getsize(handle,
"@desc", dummy_num_rows, num_columns)
141 allocate (desc(1,0:num_columns))
143 odb_rc = odb_getsize(handle,
"@hdr", dummy_num_rows, num_columns)
144 allocate (hdr(
size(latitudes),0:num_columns))
146 odb_rc = odb_getsize(handle,
"@body", dummy_num_rows, num_columns)
147 allocate (body(
size(latitudes),0:num_columns))
153 desc(:,4) =
size(latitudes)
155 do iob = 1,
size(latitudes)
157 hdr(iob,2) = dates(iob)
158 hdr(iob,3) = times(iob)
159 hdr(iob,4) = latitudes(iob)
160 hdr(iob,5) = longitudes(iob)
165 do iob = 1,
size(latitudes)
167 body(iob,2) = observations(iob)
169 body(iob,4) = pressures(iob)
173 num_columns =
size(desc,dim=2)-1
174 odb_rc = odb_put(handle,
"@desc", desc, 1, poolno = pool_number)
175 num_columns =
size(hdr,dim=2)-1
176 odb_rc = odb_put(handle,
"@hdr", hdr,
size(latitudes), poolno = pool_number)
177 num_columns =
size(body,dim=2)-1
178 odb_rc = odb_put(handle,
"@body", body,
size(latitudes), poolno = pool_number)
180 odb_rc = odb_close(handle,
save = .true.)
185 subroutine fail (messages)
187 character(len=*),
intent(in) :: messages(:)
191 do i = 1,
size(messages)
192 write (0,
'(a)') trim(messages(i))
193 write (*,
'(a)') trim(messages(i))
204 nf90_inquire_dimension, &
205 nf90_inquire_variable, &
209 integer,
intent(in) :: file_id
210 character(len=*),
intent(in) :: var_name
211 real,
allocatable,
intent(out) :: output_array(:)
213 integer :: var_id, rc, ndims, length, dimids(1)
214 character(len=7000) :: messages(5)
216 rc = nf90_inq_varid(file_id, var_name, var_id)
217 if (rc /= nf90_noerr)
then 218 write (messages(1),
'(a,i0)')
"nf90_inq_varid failed for " // trim(var_name) //
" with rc = ", rc
219 write (messages(2),
'(a,i0)')
"file id = ", file_id
220 messages(3) = nf90_strerror(rc)
221 call fail (messages(1:3))
224 rc = nf90_inquire_variable(file_id, var_id, ndims = ndims, dimids = dimids)
225 if (rc /= nf90_noerr)
then 226 write (messages(1),
'(a,i0)')
"nf90_inquire_variable failed for " // trim(var_name) //
" with rc = ", rc
227 write (messages(2),
'(a,i0)')
"file id = ", file_id
228 write (messages(3),
'(a,i0)')
"var id = ", var_id
229 messages(4) = nf90_strerror(rc)
230 call fail (messages(1:4))
234 write (messages(1),
'(a,i0)')
"Should be a 1d arrays for " // trim(var_name)
235 write (messages(2),
'(a,i0,a)')
"Actually got ", ndims,
"d array" 236 write (messages(3),
'(a,i0)')
"file id = ", file_id
237 write (messages(4),
'(a,i0)')
"var id = ", var_id
238 call fail (messages(1:4))
241 rc = nf90_inquire_dimension(file_id, dimids(1), len = length)
242 if (rc /= nf90_noerr)
then 243 write (messages(1),
'(a,i0)')
"nf90_inquire_dimension failed for " // trim(var_name) //
" with rc = ", rc
244 write (messages(2),
'(a,i0)')
"file id = ", file_id
245 write (messages(3),
'(a,i0)')
"var id = ", var_id
246 write (messages(4),
'(a,i0)')
"dim id = ", dimids(1)
247 messages(5) = nf90_strerror(rc)
248 call fail (messages(1:5))
251 allocate (output_array(length))
253 rc = nf90_get_var(file_id, var_id, output_array)
254 if (rc /= nf90_noerr)
then 255 write (messages(1),
'(a,i0)')
"nf90_get_var failed for " // trim(var_name) //
" with rc = ", rc
256 write (messages(2),
'(a,i0)')
"file id = ", file_id
257 write (messages(2),
'(a,i0)')
"var id = ", var_id
258 messages(4) = nf90_strerror(rc)
259 call fail (messages(1:4))
subroutine get_netcdf_array(file_id, var_name, output_array)
subroutine fail(messages)
program netcdf_to_odb
Fortran program to convert the existing NetCDF file into an ODB-1.