150 #ifdef enable_action_msgs
151 nclayer_enable_action, nclayer_actionm, &
156 nclayer_error, nclayer_warning, nclayer_info, nclayer_check
158 use netcdf,
only: nf90_inquire, nf90_inquire_variable, &
159 nf90_inquire_dimension, nf90_def_dim, nf90_def_var, &
160 nf90_put_var, nf90_def_var_chunking, nf90_def_var_deflate, &
161 nf90_byte, nf90_short, nf90_int, nf90_float, nf90_double, &
162 nf90_char, nf90_max_name, nf90_chunked
175 integer(i_long),
intent(in) :: multiplier
192 integer(i_long) :: ndims, nvars, var_index, type_index
193 integer(i_long) :: rel_index, i, nobs_size
195 character(len=NF90_MAX_NAME) :: tmp_var_name
196 integer(i_long) :: tmp_var_type, tmp_var_ndims
198 integer(i_long),
dimension(:),
allocatable :: tmp_var_dimids, tmp_var_dim_sizes
199 character(len=NF90_MAX_NAME) ,
allocatable :: tmp_var_dim_names(:)
201 logical :: is_metadata_var
204 call nclayer_check(nf90_inquire(
ncid, ndimensions = ndims, &
209 do var_index = 1, nvars
211 call nclayer_check(nf90_inquire_variable(
ncid, var_index, name = tmp_var_name, ndims = tmp_var_ndims))
214 allocate(tmp_var_dimids(tmp_var_ndims))
215 allocate(tmp_var_dim_names(tmp_var_ndims))
216 allocate(tmp_var_dim_sizes(tmp_var_ndims))
220 call nclayer_check(nf90_inquire_variable(
ncid, var_index, dimids = tmp_var_dimids, &
221 xtype = tmp_var_type))
223 if ((tmp_var_ndims == 1) .OR. &
224 ((tmp_var_ndims == 2) .AND. (tmp_var_type == nf90_char)))
then 225 is_metadata_var = .false.
227 do i = 1, tmp_var_ndims
228 call nclayer_check(nf90_inquire_dimension(
ncid, tmp_var_dimids(i), tmp_var_dim_names(i), &
229 tmp_var_dim_sizes(i)))
231 if (tmp_var_dim_names(i) ==
"nobs")
then 232 nobs_size = tmp_var_dim_sizes(i)
233 if (tmp_var_type /= nf90_char)
then 234 is_metadata_var = .true.
235 else if (tmp_var_type == nf90_char)
then 236 if (index(tmp_var_dim_names(1),
"_maxstrlen") /= 0) &
237 is_metadata_var = .true.
242 if (is_metadata_var)
then 253 rel_index = nobs_size
255 if (tmp_var_type == nf90_byte)
then 259 else if (tmp_var_type == nf90_short)
then 263 else if (tmp_var_type == nf90_int)
then 267 else if (tmp_var_type == nf90_float)
then 271 else if (tmp_var_type == nf90_double)
then 275 else if (tmp_var_type == nf90_char)
then 281 call nclayer_error(
"NetCDF4 type invalid!")
300 deallocate(tmp_var_dimids)
301 deallocate(tmp_var_dim_names)
302 deallocate(tmp_var_dim_sizes)
309 logical,
intent(in),
optional :: internal
311 integer(i_byte) :: data_type
312 character(len=100) :: data_name
314 integer(i_llong) :: curdatindex, j
315 integer(i_long) :: nc_data_type
316 integer(i_long) :: tmp_dim_id
317 character(len=120) :: data_dim_name
319 character(len=:),
allocatable :: string_arr(:)
321 #ifdef ENABLE_ACTION_MSGS 322 character(len=1000) :: action_str
324 if (nclayer_enable_action)
then 325 if (
present(internal))
then 326 write(action_str,
"(A, L, A)")
"nc_diag_metadata_write_def(internal = ", internal,
")" 328 write(action_str,
"(A)")
"nc_diag_metadata_write_def(internal = (not specified))" 330 call nclayer_actionm(trim(action_str))
344 call nclayer_info(
"metadata: defining " // trim(data_name))
346 if (data_type ==
nlayer_byte) nc_data_type = nf90_byte
347 if (data_type ==
nlayer_short) nc_data_type = nf90_short
348 if (data_type ==
nlayer_long) nc_data_type = nf90_int
349 if (data_type ==
nlayer_float) nc_data_type = nf90_float
354 print *,
"metadata part 1" 358 write (data_dim_name,
"(A, A)") trim(data_name),
"_maxstrlen" 364 allocate(
character(10000) :: string_arr(diag_metadata_store%stor_i_arr(curdatindex)%icount))
365 do j = 1, diag_metadata_store%stor_i_arr(curdatindex)%icount
366 string_arr(j) = diag_metadata_store%m_string(diag_metadata_store%stor_i_arr(curdatindex)%index_arr(j))
371 diag_metadata_store%stor_i_arr(curdatindex)%icount)
373 deallocate(string_arr)
377 call nclayer_check(nf90_def_dim(
ncid, data_dim_name, &
378 diag_metadata_store%max_str_lens(curdatindex), tmp_dim_id))
381 print *,
"Defining char var type..." 385 call nclayer_check(nf90_def_var(
ncid, data_name, nc_data_type, &
387 diag_metadata_store%var_ids(curdatindex)))
390 print *,
"Done defining char var type..." 395 diag_metadata_store%var_ids(curdatindex)))
399 print *,
"metadata part 2" 403 diag_metadata_store%types(curdatindex), &
404 diag_metadata_store%var_ids(curdatindex))
409 print *,
"Defining compression 1 (chunking)..." 414 call nclayer_check(nf90_def_var_chunking(
ncid, diag_metadata_store%var_ids(curdatindex), &
415 nf90_chunked, (/ diag_metadata_store%max_str_lens(curdatindex),
nlayer_chunking /)))
417 call nclayer_check(nf90_def_var_chunking(
ncid, diag_metadata_store%var_ids(curdatindex), &
422 print *,
"Defining compression 2 (gzip)..." 424 call nclayer_check(nf90_def_var_deflate(
ncid, diag_metadata_store%var_ids(curdatindex), &
428 print *,
"Done defining compression..." 433 diag_metadata_store%def_lock = .true.
436 if(.NOT.
present(internal)) &
437 call nclayer_error(
"Can't write definitions - definitions have already been written and locked!")
446 logical,
intent(in),
optional :: flush_data_only
448 integer(i_byte) :: data_type
449 character(len=100) :: data_name
451 integer(i_long) :: curdatindex, j
453 integer(i_byte),
dimension(:),
allocatable :: byte_arr
454 integer(i_short),
dimension(:),
allocatable :: short_arr
455 integer(i_long),
dimension(:),
allocatable :: long_arr
456 real(r_single),
dimension(:),
allocatable :: rsingle_arr
457 real(r_double),
dimension(:),
allocatable :: rdouble_arr
458 character(len=:),
allocatable :: string_arr(:)
460 integer(i_llong) :: string_arr_maxlen
462 integer(i_llong) :: data_length_counter
463 character(len=100) :: counter_data_name
464 integer(i_llong) :: current_length_count
465 character(len=1000) :: data_uneven_msg
467 #ifdef ENABLE_ACTION_MSGS 468 character(len=1000) :: action_str
470 if (nclayer_enable_action)
then 471 if (
present(flush_data_only))
then 472 write(action_str,
"(A, L, A)")
"nc_diag_metadata_write_data(flush_data_only = ", flush_data_only,
")" 474 write(action_str,
"(A)")
"nc_diag_metadata_write_data(flush_data_only = (not specified))" 476 call nclayer_actionm(trim(action_str))
485 data_length_counter = -1
486 current_length_count = -1
497 call nclayer_info(
"metadata: writing " // trim(data_name))
500 if (.NOT. (
present(flush_data_only) .AND. flush_data_only))
then 504 if (data_length_counter == -1)
then 505 data_length_counter = current_length_count
506 counter_data_name = data_name
508 if (data_length_counter /= current_length_count)
then 511 write (data_uneven_msg,
"(A, I0, A, I0, A)")
"Amount of data written in " // &
512 trim(data_name) //
" (", &
513 current_length_count, &
515 " differs from variable " // trim(counter_data_name) // &
516 " (", data_length_counter,
")!" 519 call nclayer_error(trim(data_uneven_msg))
521 call nclayer_warning(trim(data_uneven_msg))
534 call nclayer_check(nf90_put_var(&
546 call nclayer_check(nf90_put_var(&
552 deallocate(short_arr)
559 call nclayer_check(nf90_put_var(&
572 call nclayer_check(nf90_put_var(&
578 deallocate(rsingle_arr)
585 call nclayer_check(nf90_put_var(&
590 deallocate(rdouble_arr)
594 allocate(
character(10000) :: string_arr(diag_metadata_store%stor_i_arr(curdatindex)%icount))
595 do j = 1, diag_metadata_store%stor_i_arr(curdatindex)%icount
596 string_arr(j) = diag_metadata_store%m_string(diag_metadata_store%stor_i_arr(curdatindex)%index_arr(j))
600 diag_metadata_store%stor_i_arr(curdatindex)%icount)
602 deallocate(string_arr)
604 string_arr_maxlen = diag_metadata_store%max_str_lens(curdatindex)
607 allocate(
character(string_arr_maxlen) :: string_arr(diag_metadata_store%stor_i_arr(curdatindex)%icount))
608 do j = 1, diag_metadata_store%stor_i_arr(curdatindex)%icount
609 string_arr(j) = diag_metadata_store%m_string(diag_metadata_store%stor_i_arr(curdatindex)%index_arr(j))
612 call nclayer_check(nf90_put_var(&
613 ncid, diag_metadata_store%var_ids(curdatindex), &
615 (/ 1, 1 + diag_metadata_store%rel_indexes(curdatindex) /) &
617 deallocate(string_arr)
622 if (
present(flush_data_only) .AND. flush_data_only)
then 623 diag_metadata_store%rel_indexes(curdatindex) = &
624 diag_metadata_store%rel_indexes(curdatindex) + &
625 diag_metadata_store%stor_i_arr(curdatindex)%icount
626 diag_metadata_store%stor_i_arr(curdatindex)%icount = 0
629 print *,
"diag_metadata_store%rel_indexes(curdatindex) is now:" 630 print *, diag_metadata_store%rel_indexes(curdatindex)
637 if (
present(flush_data_only) .AND. flush_data_only)
then 639 print *,
"In buffer flush mode!" 643 diag_metadata_store%acount = 0
646 diag_metadata_store%data_lock = .true.
648 print *,
"In data lock mode!" 652 call nclayer_error(
"Can't write data - data have already been written and locked!")
655 call nclayer_error(
"Can't write data - NetCDF4 layer not initialized yet!")
659 print *,
"All done writing metadata data" 664 logical,
intent(in) :: enable_strict
669 call nclayer_error(
"Can't set strictness level for metadata - NetCDF4 layer not initialized yet!")
675 integer(i_llong),
intent(in) :: num_of_addl_vars
676 #ifdef ENABLE_ACTION_MSGS 677 character(len=1000) :: action_str
679 if (nclayer_enable_action)
then 680 write(action_str,
"(A, I0, A)")
"nc_diag_metadata_prealloc_vars(num_of_addl_vars = ", num_of_addl_vars,
")" 681 call nclayer_actionm(trim(action_str))
747 call nclayer_error(
"NetCDF4 layer not initialized yet!")
753 integer(i_byte),
intent(in) :: nclayer_type
754 integer(i_llong),
intent(in) :: num_of_addl_slots
756 #ifdef ENABLE_ACTION_MSGS 757 character(len=1000) :: action_str
759 if (nclayer_enable_action)
then 760 write(action_str,
"(A, I0, A, I0, A)")
"nc_diag_metadata_prealloc_vars_storage(nclayer_type = ", nclayer_type,
", num_of_addl_slots = ", num_of_addl_slots,
")" 761 call nclayer_actionm(trim(action_str))
778 call nclayer_error(
"Invalid type specified for variable storage preallocation!")
784 integer(i_llong),
intent(in) :: num_of_addl_slots
787 #ifdef ENABLE_ACTION_MSGS 788 character(len=1000) :: action_str
790 if (nclayer_enable_action)
then 791 write(action_str,
"(A, I0, A)")
"nc_diag_metadata_prealloc_vars_storage_all(num_of_addl_slots = ", num_of_addl_slots,
")" 792 call nclayer_actionm(trim(action_str))
802 integer(i_llong) :: addl_fields
805 logical :: meta_realloc
807 meta_realloc = .false.
813 call nclayer_debug(
"INITIAL value of diag_metadata_store%alloc_s_multi:")
820 call nclayer_debug(
"Reallocating diag_metadata_store%names...")
826 call nclayer_debug(
"Reallocated diag_metadata_store%names. Size:")
829 meta_realloc = .true.
833 call nclayer_debug(
"Allocating diag_metadata_store%names for first time...")
840 call nclayer_debug(
"Allocated diag_metadata_store%names. Size:")
848 call nclayer_debug(
"Reallocating diag_metadata_store%types...")
853 meta_realloc = .true.
862 call nclayer_debug(
"Reallocating diag_metadata_store%stor_i_arr...")
868 meta_realloc = .true.
877 meta_realloc = .true.
887 meta_realloc = .true.
897 meta_realloc = .true.
907 meta_realloc = .true.
914 if (meta_realloc)
then 917 print *,
"Incrementing alloc_s_multi... new value:" 922 call nclayer_error(
"NetCDF4 layer not initialized yet!")
928 character(len=*),
intent(in) :: metadata_name
946 character(len=*),
intent(in) :: metadata_name
947 integer(i_byte),
intent(in) :: metadata_value
949 integer(i_long) :: var_index
951 #ifdef ENABLE_ACTION_MSGS 952 character(len=1000) :: action_str
954 if (nclayer_enable_action)
then 955 write(action_str,
"(A, I0, A)")
"nc_diag_metadata_byte(metadata_name = " // metadata_name //
", metadata_value = ", metadata_value,
")" 956 call nclayer_actionm(trim(action_str))
961 call nclayer_error(
"Can't add new data - data have already been written and locked!")
966 if (var_index == -1)
then 969 call nclayer_error(
"Can't add new variable - definitions have already been written and locked!")
995 character(len=*),
intent(in) :: metadata_name
996 integer(i_short),
intent(in) :: metadata_value
998 integer(i_long) :: var_index
1000 #ifdef ENABLE_ACTION_MSGS 1001 character(len=1000) :: action_str
1003 if (nclayer_enable_action)
then 1004 write(action_str,
"(A, I0, A)")
"nc_diag_metadata_short(metadata_name = " // metadata_name //
", metadata_value = ", metadata_value,
")" 1005 call nclayer_actionm(trim(action_str))
1010 call nclayer_error(
"Can't add new data - data have already been written and locked!")
1015 if (var_index == -1)
then 1018 call nclayer_error(
"Can't add new variable - definitions have already been written and locked!")
1044 character(len=*),
intent(in) :: metadata_name
1045 integer(i_long),
intent(in) :: metadata_value
1047 integer(i_long) :: var_index
1049 #ifdef ENABLE_ACTION_MSGS 1050 character(len=1000) :: action_str
1052 if (nclayer_enable_action)
then 1053 write(action_str,
"(A, I0, A)")
"nc_diag_metadata_long(metadata_name = " // metadata_name //
", metadata_value = ", metadata_value,
")" 1054 call nclayer_actionm(trim(action_str))
1059 call nclayer_error(
"Can't add new data - data have already been written and locked!")
1064 if (var_index == -1)
then 1067 call nclayer_error(
"Can't add new variable - definitions have already been written and locked!")
1081 call nclayer_debug(
"Current total:")
1098 character(len=*),
intent(in) :: metadata_name
1099 real(r_single),
intent(in) :: metadata_value
1101 integer(i_long) :: var_index
1103 #ifdef ENABLE_ACTION_MSGS 1104 character(len=1000) :: action_str
1106 if (nclayer_enable_action)
then 1107 write(action_str,
"(A, F0.5, A)")
"nc_diag_metadata_rsingle(metadata_name = " // metadata_name //
", metadata_value = ", metadata_value,
")" 1108 call nclayer_actionm(trim(action_str))
1113 call nclayer_error(
"Can't add new data - data have already been written and locked!")
1118 if (var_index == -1)
then 1121 call nclayer_error(
"Can't add new variable - definitions have already been written and locked!")
1124 write (*,
"(A, A, A, F)")
"NEW METADATA: ", metadata_name,
" | First value: ", metadata_value
1149 character(len=*),
intent(in) :: metadata_name
1150 real(r_double),
intent(in) :: metadata_value
1152 integer(i_long) :: var_index
1154 #ifdef ENABLE_ACTION_MSGS 1155 character(len=1000) :: action_str
1157 if (nclayer_enable_action)
then 1158 write(action_str,
"(A, F0.5, A)")
"nc_diag_metadata_rdouble(metadata_name = " // metadata_name //
", metadata_value = ", metadata_value,
")" 1159 call nclayer_actionm(trim(action_str))
1164 call nclayer_error(
"Can't add new data - data have already been written and locked!")
1169 if (var_index == -1)
then 1172 call nclayer_error(
"Can't add new variable - definitions have already been written and locked!")
1198 character(len=*),
intent(in) :: metadata_name
1199 character(len=*),
intent(in) :: metadata_value
1201 integer(i_long) :: var_index
1203 #ifdef ENABLE_ACTION_MSGS 1204 character(len=1000) :: action_str
1206 if (nclayer_enable_action)
then 1207 write(action_str,
"(A)")
"nc_diag_metadata_string(metadata_name = " // metadata_name //
", metadata_value = " // trim(metadata_value) //
")" 1208 call nclayer_actionm(trim(action_str))
1213 call nclayer_error(
"Can't add new data - data have already been written and locked!")
1218 if (var_index == -1)
then 1221 call nclayer_error(
"Can't add new variable - definitions have already been written and locked!")
1235 print *,
"len_trim(metadata_value) = ", len_trim(metadata_value)
1236 print *,
"diag_metadata_store%max_str_lens(var_index) = ",
diag_metadata_store%max_str_lens(var_index)
1240 call nclayer_error(
"Cannot expand variable string length after locking variable definitions!")
1257 call nclayer_error(
"Cannot change string size when trimming is disabled!")
subroutine nc_diag_metadata_resize_rdouble(addl_num_entries, update_acount_in)
integer(i_long), parameter nlayer_multi_base
integer, parameter, public i_byte
integer(i_byte), parameter nlayer_short
type(diag_metadata), allocatable diag_metadata_store
integer, parameter, public i_long
subroutine nc_diag_metadata_resize_rsingle(addl_num_entries, update_acount_in)
subroutine nc_diag_metadata_resize_string(addl_num_entries, update_acount_in)
integer(i_byte), parameter nlayer_double
subroutine nc_diag_metadata_resize_long(addl_num_entries, update_acount_in)
integer function max_len_string_array(str_arr, arr_length)
type(diag_varattr), allocatable diag_varattr_store
integer(i_byte), parameter nlayer_string
subroutine nc_diag_metadata_resize_iarr_type(addl_num_entries)
subroutine nc_diag_metadata_resize_byte(addl_num_entries, update_acount_in)
integer(i_long), parameter nlayer_compression
integer, parameter, public i_short
integer(i_byte), parameter nlayer_byte
subroutine nc_diag_metadata_resize_short(addl_num_entries, update_acount_in)
subroutine nc_diag_varattr_add_var(var_name, var_type, var_id)
integer(i_short), parameter nlayer_default_ent
integer, parameter, public r_double
integer(i_long), parameter nlayer_chunking
integer, parameter, public r_single
integer, parameter, public i_llong
subroutine nc_diag_metadata_resize_iarr(iarr_index, addl_num_entries, update_icount_in)
integer(i_byte), parameter nlayer_float
integer(i_byte), parameter nlayer_long
subroutine nc_diag_varattr_make_nobs_dim