FV3 Bundle
nc_diag_res.f90
Go to the documentation of this file.
1 ! NetCDF Diag Resource file library
2 
3 module nc_diag_res
4  ! Library to read a resource file and check if a variable is
5  ! enabled within the resource file.
6  !
7  ! This library reads a JSON resource file with the following format:
8  ! {
9  ! "variables" : {
10  ! "some_var" : true,
11  ! "more_var" : false
12  ! }
13  ! }
14  !
15  ! Based on this sample file, we can check whether a certain variable
16  ! is enabled or not using this library:
17  !
18  ! call nc_diag_load_resource_file("resource.json")
19  ! ! This will return true:
20  ! if (nc_diag_load_check_variable("some_var")) then
21  ! print *, "This variable exists!"
22  ! ! Do some other things here
23  ! end if
24  ! ! This will return false:
25  ! if (nc_diag_load_check_variable("more_var")) then
26  ! print *, "This variable exists!"
27  ! ! Do some other things here
28  ! end if
29  ! ! Note that we can specify non-existent variables - these
30  ! ! will also return false.
31  ! if (nc_diag_load_check_variable("hmmm_var")) then
32  ! print *, "This variable exists!"
33  ! ! Do some other things here
34  ! end if
35  ! call nc_diag_close_resource_file
36 
37  use ncdres_climsg, only: ncdres_error
38  use nc_diag_fson, only: ncdf_value, ncdf_parse, &
39  ncdf_get, ncdf_destroy
40 
41  implicit none
42 
43  type(ncdf_value), pointer :: nc_diag_json => null()
44 
45  contains
46  ! Opens a given resource file for reading.
47  !
48  ! Given the resource file name, open the file and set everything
49  ! up for reading the file. This includes any internal memory
50  ! allocation required for reading the resource file.
51  !
52  ! In order for memory allocation to be freed, the
53  ! subroutine nc_diag_close_resource_file MUST be called.
54  !
55  ! If a resource file is already open, this will raise an error
56  ! and the program will terminate.
57  !
58  ! Args:
59  ! filename (character(len=*)): resource file name to load.
60  !
61  ! Raises:
62  ! Resource file already open error if there is already a
63  ! resource file currently open.
64  !
65  subroutine nc_diag_load_resource_file(filename)
66  character(len=*), intent(in) :: filename
67 
68  if (associated(nc_diag_json)) &
69  call ncdres_error("Resource file already open!")
70 
71  nc_diag_json => ncdf_parse(filename)
72  end subroutine nc_diag_load_resource_file
73 
74  ! Lookup a variable and check its status.
75  !
76  ! Given the variable name, lookup its status within the JSON
77  ! resource file.
78  !
79  ! If the variable is present in the JSON file, and it is
80  ! enabled, this will return true. Otherwise, if the variable
81  ! doesn't exist in the resource file, or it is disabled,
82  ! this will return false.
83  !
84  ! Args:
85  ! var_name (character(len=*)): variable name to lookup
86  ! within the resource file.
87  !
88  ! Returns:
89  ! var_enabled (logical): whether the variable is enabled or
90  ! not within the resource file.
91  !
92  function nc_diag_load_check_variable(var_name) result(var_enabled)
93  character(len=*), intent(in) :: var_name
94  logical :: var_enabled
95 
96  character(len=1024) :: var_str
97 
98  write (var_str, "(A)") "variables." // var_name
99 
100  var_enabled = .false.
101 
102  call ncdf_get(nc_diag_json, trim(var_str), var_enabled)
103  end function nc_diag_load_check_variable
104 
105  ! Closes the current resource file.
106  !
107  ! Closes a previously opened resource file. This will free any
108  ! resources allocated towards the previous resource file, and
109  ! allow for opening a new resource file.
110  !
111  ! If no file has been opened previously, or if the file is
112  ! already closed, this will raise an error and the program will
113  ! terminate.
114  !
115  ! Raises:
116  ! No resource file open error will occur if there is no
117  ! resource file currently open.
118  !
119  subroutine nc_diag_close_resource_file
120  if (associated(nc_diag_json)) then
121  call ncdf_destroy(nc_diag_json)
122  nullify(nc_diag_json)
123  else
124  call ncdres_error("No resource file open!")
125  end if
126  end subroutine nc_diag_close_resource_file
127 end module nc_diag_res
subroutine nc_diag_close_resource_file
logical function nc_diag_load_check_variable(var_name)
Definition: nc_diag_res.f90:93
type(ncdf_value), pointer nc_diag_json
Definition: nc_diag_res.f90:43
subroutine ncdres_error(err)
subroutine nc_diag_load_resource_file(filename)
Definition: nc_diag_res.f90:66