20 use netcdf,
only: nf90_open, nf90_close, nf90_inquire, &
21 nf90_inquire_dimension, nf90_inquire_variable, nf90_get_var, &
22 nf90_put_var, nf90_inq_dimid, &
23 nf90_nowrite, nf90_byte, nf90_short, nf90_int, nf90_float, &
24 nf90_double, nf90_char, nf90_fill_char, nf90_max_name
30 integer(i_long) :: cur_dim_id, cur_dim_len
31 integer(i_long) :: cur_out_var_id, cur_out_var_ndims, cur_out_var_counter
32 integer(i_long) :: cur_out_dim_ind, cur_out_var_ind
33 integer(i_long) :: max_cur_pos
34 integer(i_long),
dimension(:),
allocatable :: cur_out_dim_ids, cur_dim_ids
35 integer(i_long),
dimension(:),
allocatable :: cur_out_dim_sizes
36 integer(i_long),
dimension(:),
allocatable :: cur_dim_sizes
38 integer(i_long) :: tmp_dim_index
39 integer(i_long) :: input_ndims
40 integer(i_long) :: input_nvars
41 integer(i_long) :: input_nattrs
43 character(len=NF90_MAX_NAME) :: tmp_var_name
44 integer(i_long) :: tmp_var_type, tmp_var_ndims
45 integer(i_long),
dimension(:),
allocatable :: tmp_var_dimids
46 character(len=NF90_MAX_NAME) ,
allocatable :: tmp_var_dim_names(:)
48 integer(i_long),
dimension(:),
allocatable :: tmp_input_varids
50 character(1) ,
dimension(:,:),
allocatable :: tmp_string_buffer
51 character(1),
dimension(:,:,:),
allocatable :: string_2d_buffer
53 integer(i_long) :: arg_index, var_index, i
55 character(len=NF90_MAX_NAME) ,
allocatable :: tmp_in_dim_names(:)
57 character(len=1000) :: err_string
59 character(:),
allocatable :: input_file_cut
66 call ncdc_info(
"Reading in data from all files...")
69 print *,
" !!! BEGINNING DATA PASS!!" 76 print *,
" !!! INPUT FILE STAGE" 78 call get_command_argument(2 + arg_index,
input_file)
82 if (len(input_file_cut) <= 0)
then 83 call ncdc_usage(
"Invalid input file name - likely blank!")
88 call ncdc_info(
" -> Skipping " // input_file_cut //
" since it is the output file...")
91 call ncdc_info(
" -> Opening " // input_file_cut //
" for reading...")
94 cache_size = 2147483647))
98 nvariables = input_nvars, nattributes = input_nattrs))
101 allocate(tmp_in_dim_names(input_ndims))
102 do tmp_dim_index = 1, input_ndims
104 tmp_in_dim_names(tmp_dim_index)))
109 write (*,
"(A, I0)")
"Number of variables: ", input_nvars
112 allocate(tmp_input_varids(input_nvars))
115 do var_index = 1, input_nvars
117 call ncdc_check(nf90_inquire_variable(
ncid_input, var_index, name = tmp_var_name, ndims = tmp_var_ndims))
120 print *,
"** PROCESSING VARIABLE: " // trim(tmp_var_name)
124 allocate(tmp_var_dimids(tmp_var_ndims))
125 allocate(tmp_var_dim_names(tmp_var_ndims))
126 allocate(cur_dim_ids(tmp_var_ndims))
127 allocate(cur_dim_sizes(tmp_var_ndims))
128 allocate(cur_out_dim_ids(tmp_var_ndims))
129 allocate(cur_out_dim_sizes(tmp_var_ndims))
132 print *,
"** (ALLOC DONE)" 137 xtype = tmp_var_type))
140 write (*,
"(A, I0, A, I0)")
" => Variable #", var_index,
": " // &
142 write (*,
"(A)", advance =
"NO")
" => Dimension IDs: " 144 do i = 1, tmp_var_ndims
145 if (i /= 1)
write (*,
"(A)", advance =
"NO")
", " 146 write (*,
"(I0)", advance =
"NO") tmp_var_dimids(i)
151 write (*,
"(A)", advance =
"NO")
" => Dimensions: " 154 do i = 1, tmp_var_ndims
156 if (i /= 1)
write (*,
"(A)", advance =
"NO")
", " 158 call ncdc_check(nf90_inquire_dimension(
ncid_input, tmp_var_dimids(i), tmp_var_dim_names(i), cur_dim_sizes(i)))
160 write (*,
"(A)", advance =
"NO") trim(tmp_var_dim_names(i))
164 cur_out_dim_sizes(i) =
dim_sizes(cur_out_dim_ind)
178 print *,
" (starting var write)" 183 if (((.NOT. any(cur_out_dim_sizes == -1)) .AND. (cur_out_var_counter == 0)) &
184 .OR. (any(cur_out_dim_sizes == -1)))
then 186 if ((cur_out_var_ndims == 1) .OR. &
187 ((cur_out_var_ndims == 2) .AND. (tmp_var_type == nf90_char)))
then 188 if (tmp_var_type == nf90_byte)
then 192 data_blobs(cur_out_var_ind)%cur_pos + cur_dim_sizes(1) - 1), &
194 count = (/ cur_dim_sizes(1) /) ))
195 else if (tmp_var_type == nf90_short)
then 199 data_blobs(cur_out_var_ind)%cur_pos + cur_dim_sizes(1) - 1), &
201 count = (/ cur_dim_sizes(1) /) ))
202 else if (tmp_var_type == nf90_int)
then 206 data_blobs(cur_out_var_ind)%cur_pos + cur_dim_sizes(1) - 1), &
208 count = (/ cur_dim_sizes(1) /) ))
209 else if (tmp_var_type == nf90_float)
then 213 data_blobs(cur_out_var_ind)%cur_pos + cur_dim_sizes(1) - 1), &
215 count = (/ cur_dim_sizes(1) /) ))
216 else if (tmp_var_type == nf90_double)
then 220 data_blobs(cur_out_var_ind)%cur_pos + cur_dim_sizes(1) - 1), &
222 count = (/ cur_dim_sizes(1) /) ))
223 else if (tmp_var_type == nf90_char)
then 234 allocate(tmp_string_buffer(cur_dim_sizes(1), cur_dim_sizes(2)))
235 tmp_string_buffer = nf90_fill_char
238 start = (/ 1, 1 /), &
239 count = (/ cur_dim_sizes(1), cur_dim_sizes(2) /) ))
242 (1 : cur_dim_sizes(1), &
244 data_blobs(cur_out_var_ind)%cur_pos + cur_dim_sizes(2) - 1) = &
247 deallocate(tmp_string_buffer)
249 write (err_string,
"(A, I0, A)") &
250 "Invalid type detected during write." // &
252 "(Variable '" // trim(tmp_var_name) //
"' has an type of ", &
253 tmp_var_type,
"," // &
258 else if ((cur_out_var_ndims == 2) .OR. &
259 ((cur_out_var_ndims == 3) .AND. (tmp_var_type == nf90_char)))
then 261 if (tmp_var_type == nf90_byte)
then 264 (1 : cur_dim_sizes(1), &
266 data_blobs(cur_out_var_ind)%cur_pos + cur_dim_sizes(2) - 1)))
267 else if (tmp_var_type == nf90_short)
then 270 (1 : cur_dim_sizes(1), &
272 data_blobs(cur_out_var_ind)%cur_pos + cur_dim_sizes(2) - 1)))
273 else if (tmp_var_type == nf90_int)
then 276 (1 : cur_dim_sizes(1), &
278 data_blobs(cur_out_var_ind)%cur_pos + cur_dim_sizes(2) - 1)))
282 else if (tmp_var_type == nf90_float)
then 284 data_blobs(cur_out_var_ind)%rsingle_2d_buffer &
285 (1 : cur_dim_sizes(1), &
287 data_blobs(cur_out_var_ind)%cur_pos + cur_dim_sizes(2) - 1), &
288 start = (/ 1, 1 /), &
289 count = (/ cur_dim_sizes(1), cur_dim_sizes(2) /) ))
290 else if (tmp_var_type == nf90_double)
then 292 data_blobs(cur_out_var_ind)%rdouble_2d_buffer &
293 (1 : cur_dim_sizes(1), &
295 data_blobs(cur_out_var_ind)%cur_pos + cur_dim_sizes(2) - 1), &
296 start = (/ 1, 1 /), &
297 count = (/ cur_dim_sizes(1), cur_dim_sizes(2) /) ))
298 else if (tmp_var_type == nf90_char)
then 300 allocate(string_2d_buffer(cur_dim_sizes(1), cur_dim_sizes(2), cur_dim_sizes(3)))
301 string_2d_buffer = nf90_fill_char
303 start = (/ 1, 1, 1 /), &
304 count = (/ cur_dim_sizes(1), cur_dim_sizes(2), cur_dim_sizes(3) /) ))
305 print *,
"CUR_POS COUNTER:",
data_blobs(cur_out_var_ind)%cur_pos
306 data_blobs(cur_out_var_ind)%string_2d_buffer &
307 (1 : cur_dim_sizes(1), 1 : cur_dim_sizes(2), &
309 data_blobs(cur_out_var_ind)%cur_pos + cur_dim_sizes(3) - 1) &
310 = string_2d_buffer(:,:,:)
311 deallocate(string_2d_buffer)
313 write (err_string,
"(A, I0, A)") &
314 "Invalid type detected during write." // &
316 "(Variable '" // trim(tmp_var_name) //
"' has an type of ", &
317 tmp_var_type,
"," // &
324 if (any(cur_out_dim_sizes == -1)) &
326 data_blobs(cur_out_var_ind)%cur_pos + cur_dim_sizes(cur_out_var_ndims)
333 print *,
" (end var write / start dealloc)" 337 deallocate(tmp_var_dimids)
338 deallocate(tmp_var_dim_names)
339 deallocate(cur_dim_ids)
340 deallocate(cur_dim_sizes)
341 deallocate(cur_out_dim_ids)
342 deallocate(cur_out_dim_sizes)
345 print *,
" (end dealloc)" 358 if (
data_blobs(var_index)%cur_pos > max_cur_pos) &
362 if (max_cur_pos > 0)
then 377 print *,
"Unlimited dimension name: ", trim(
dim_names(i))
394 deallocate(tmp_input_varids)
395 deallocate(tmp_in_dim_names)
401 integer(i_long) :: var_index
404 call ncdc_info(
"Doing final data commit...")
417 count = (/
data_blobs(var_index)%alloc_size(1) /) ))
418 if (
var_types(var_index) == nf90_short) &
422 count = (/
data_blobs(var_index)%alloc_size(1) /) ))
427 count = (/
data_blobs(var_index)%alloc_size(1) /) ))
428 if (
var_types(var_index) == nf90_float) &
432 count = (/
data_blobs(var_index)%alloc_size(1) /) ))
434 if (
var_types(var_index) == nf90_double) &
438 count = (/
data_blobs(var_index)%alloc_size(1) /) ))
442 start = (/ 1, 1 /), &
443 count = (/
data_blobs(var_index)%alloc_size(1), &
450 start = (/ 1, 1 /), &
451 count = (/
data_blobs(var_index)%alloc_size(1), &
453 if (
var_types(var_index) == nf90_short) &
456 start = (/ 1, 1 /), &
457 count = (/
data_blobs(var_index)%alloc_size(1), &
462 start = (/ 1, 1 /), &
463 count = (/
data_blobs(var_index)%alloc_size(1), &
465 if (
var_types(var_index) == nf90_float) &
468 start = (/ 1, 1 /), &
469 count = (/
data_blobs(var_index)%alloc_size(1), &
471 if (
var_types(var_index) == nf90_double) &
474 start = (/ 1, 1 /), &
475 count = (/
data_blobs(var_index)%alloc_size(1), &
480 start = (/ 1, 1, 1 /), &
481 count = (/
data_blobs(var_index)%alloc_size(1), &
integer(i_long) var_arr_total
integer(i_long), dimension(:), allocatable dim_output_ids
integer, parameter, public i_byte
character(len=10000000) output_file
integer(i_long), dimension(:), allocatable var_types
integer, parameter, public i_long
character(len=100), dimension(:), allocatable dim_names
integer(i_long) cli_arg_count
character(len=10000000) prgm_name
subroutine ncdc_usage(err)
integer(i_long) function nc_diag_cat_lookup_dim(dim_name)
character(len=100), dimension(:), allocatable var_names
integer(i_long) ncid_output
integer(i_long), dimension(:), allocatable dim_counters
subroutine ncdc_error(err)
integer(i_long) ncid_input
subroutine ncdc_info(ifo)
subroutine ncdc_warning(warn)
integer(i_long) input_count
subroutine ncdc_check(status)
integer(i_long), dimension(:), allocatable var_counters
integer(i_long) function nc_diag_cat_lookup_var(var_name)
integer, parameter, public i_short
type(nc_diag_cat_dim_names), dimension(:), allocatable var_dim_names
subroutine nc_diag_cat_data_commit
integer(i_long), dimension(:), allocatable var_output_ids
integer(i_long) dim_arr_total
integer, parameter, public r_double
type(data_blob), dimension(:), allocatable data_blobs
integer, parameter, public r_single
subroutine nc_diag_cat_data_pass
integer(i_long), dimension(:), allocatable dim_sizes
character(len=10000000) input_file