FV3 Bundle
ncdc_vars.F90
Go to the documentation of this file.
1 module ncdc_vars
2  use ncd_kinds, only: i_long
6  use ncdc_dims, only: dim_sizes, nc_diag_cat_lookup_dim
8  use ncdc_climsg, only: ncdc_error
9  use netcdf, only: nf90_byte, nf90_short, nf90_int, nf90_float, &
10  nf90_double, nf90_char
11 
12  implicit none
13 
14  integer(i_long), parameter :: var_start_size = 1024
15 
16  contains
17  function nc_diag_cat_lookup_var(var_name) result(ind)
18  character(len=*), intent(in) :: var_name
19  integer(i_long) :: i, ind
20 
21  ind = -1
22 
23  if (allocated(var_names)) then
24  do i = 1, var_arr_total
25  if (var_names(i) == var_name) then
26  ind = i
27  exit
28  end if
29  end do
30  end if
31  end function nc_diag_cat_lookup_var
32 
33  subroutine nc_diag_cat_metadata_add_var(var_name, var_type, var_ndims, var_dims)
34  character(len=*), intent(in) :: var_name
35  integer(i_long) , intent(in) :: var_type
36  integer(i_long) , intent(in) :: var_ndims
37  character(len=*), intent(in) :: var_dims(:)
38 
39  integer(i_long) :: var_index, i
40  character(len=1000) :: err_string
41 
42  var_index = nc_diag_cat_lookup_var(trim(var_name))
43 
44  ! If we can't find it, it's new! Make sure we have enough
45  ! space for it...
46  if (var_index == -1) then
47 #ifdef DEBUG
48  print *, "NEW VAR! Var = " // trim(var_name)
49 #endif
50 
52 
53  if (var_arr_total >= var_arr_size) then
54  if (allocated(var_names)) then
61  else
62  allocate(var_names(var_start_size))
63  allocate(var_types(var_start_size))
64  allocate(var_dim_names(var_start_size))
66  allocate(var_counters(var_start_size))
67  allocate(var_hasunlim(var_start_size))
69  end if
70  end if
71 
72 #ifdef DEBUG
73  write (*, "(A)", advance="NO") "DEBUG DUMP:"
74 
75  do i = 1, var_arr_total - 1
76  if (i /= 1) write (*, "(A)", advance="NO") ", "
77  write (*, "(A)", advance="NO") var_names(i)
78  end do
79 
80  print *, "NEW var_index: ", var_arr_total
81 #endif
82 
83  var_index = var_arr_total
84 
85  ! Add name
86  var_names(var_index) = var_name
87  var_types(var_index) = var_type
88  var_counters(var_index) = 0
89  end if
90 
91  if (allocated(var_dim_names(var_index)%dim_names)) then
92  ! Just do a sanity check!
93  if (var_types(var_index) /= var_type) &
94  call ncdc_error("Variable type changed!" // &
95  char(10) // " " // &
96  "(Type of variable '" // var_name // "' changed from " // &
97  trim(nc_diag_cat_metadata_type_to_str(var_types(var_index))) // &
98  char(10) // " " // &
99  "to " // &
101  "!)")
102 
103  if (var_dim_names(var_index)%num_names /= var_ndims) then
104  write (err_string, "(A, I0, A, I0, A)") &
105  "Variable ndims changed!" // &
106  char(10) // " " // &
107  "(Variable '" // var_name // "' changed ndims from ", &
108  var_dim_names(var_index)%num_names, &
109  char(10) // " " // &
110  "to ", &
111  var_ndims, &
112  "!)"
113  call ncdc_error(trim(err_string))
114  end if
115 
116  do i = 1, var_ndims
117  if (var_dim_names(var_index)%dim_names(i) /= var_dims(i)) &
118  call ncdc_error("Variable dimensions changed!" // &
119  char(10) // " " // &
120  "(Variable '" // var_name // "' changed dimension from " // &
121  trim(var_dim_names(var_index)%dim_names(i)) // &
122  char(10) // " " // &
123  "to " // &
124  trim(var_dims(i)) // &
125  "!)")
126  end do
127  else
128  var_dim_names(var_index)%num_names = var_ndims
129  allocate(var_dim_names(var_index)%dim_names(var_ndims))
130  allocate(var_dim_names(var_index)%output_dim_ids(var_ndims))
131  var_dim_names(var_index)%dim_names(1:var_ndims) = var_dims(1:var_ndims)
132  var_hasunlim(var_index) = .false.
133 
134  do i = 1, var_ndims
135  if (dim_sizes(nc_diag_cat_lookup_dim(var_dim_names(var_index)%dim_names(i))) == -1) then
136  var_hasunlim(var_index) = .true.
137  exit
138  end if
139  end do
140 
141  end if
142  end subroutine nc_diag_cat_metadata_add_var
143 
144  function nc_diag_cat_metadata_type_to_str(var_type) result(nc_str)
145  integer(i_long) :: var_type
146  character(len=11) :: nc_str
147 
148  nc_str = "(invalid)"
149 
150  if (var_type == nf90_byte) nc_str = "NF90_BYTE"
151  if (var_type == nf90_short) nc_str = "NF90_SHORT"
152  if (var_type == nf90_int) nc_str = "NF90_INT (LONG)"
153  if (var_type == nf90_float) nc_str = "NF90_FLOAT"
154  if (var_type == nf90_double) nc_str = "NF90_DOUBLE"
155  if (var_type == nf90_char) nc_str = "NF90_CHAR"
157 end module ncdc_vars
integer(i_long) var_arr_total
Definition: ncdc_state.F90:43
integer(i_long), dimension(:), allocatable var_types
Definition: ncdc_state.F90:36
integer, parameter, public i_long
Definition: ncd_kinds.F90:47
integer(i_long) function nc_diag_cat_lookup_dim(dim_name)
Definition: ncdc_dims.F90:14
character(len=100), dimension(:), allocatable var_names
Definition: ncdc_state.F90:35
subroutine ncdc_error(err)
Definition: ncdc_climsg.F90:29
integer(i_long) var_arr_size
Definition: ncdc_state.F90:44
subroutine nc_diag_cat_metadata_add_var(var_name, var_type, var_ndims, var_dims)
Definition: ncdc_vars.F90:34
integer(i_long), dimension(:), allocatable var_counters
Definition: ncdc_state.F90:39
integer(i_long) function nc_diag_cat_lookup_var(var_name)
Definition: ncdc_vars.F90:18
logical, dimension(:), allocatable var_hasunlim
Definition: ncdc_state.F90:40
integer(i_long), parameter var_start_size
Definition: ncdc_vars.F90:14
type(nc_diag_cat_dim_names), dimension(:), allocatable var_dim_names
Definition: ncdc_state.F90:37
integer(i_long), dimension(:), allocatable var_output_ids
Definition: ncdc_state.F90:38
character(len=11) function nc_diag_cat_metadata_type_to_str(var_type)
Definition: ncdc_vars.F90:145