FV3 Bundle
ioda_utils_mod.F90
Go to the documentation of this file.
1 !
2 ! (C) Copyright 2017 UCAR
3 !
4 ! This software is licensed under the terms of the Apache Licence Version 2.0
5 ! which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6 
7 !> Fortran module containing IODA utility programs
8 
10 
11  use kinds, only: kind_real
12 
13  implicit none
14 
15  private
16 
17  public missing_value
19 
20  !======================================================
21  ! Constants
22  !======================================================
23  real(kind=kind_real) :: missing_value = 9.0e36_kind_real !< default for NetCDF file converted from PrepBUFR, the actual missing value is 9.96921e+36
24 
25  !======================================================
26  ! Subroutines
27  !======================================================
28  contains
29 
30  !---------------------------------------------------------------------------------------
31  subroutine ioda_deselect_missing_values(ncid, vname, in_index, out_index)
32  use netcdf, only: nf90_float, nf90_double
33 
34  use nc_diag_read_mod, only: nc_diag_read_get_var
35  use nc_diag_read_mod, only: nc_diag_read_get_var_dims
36  use nc_diag_read_mod, only: nc_diag_read_get_var_type
37 
38  implicit none
39 
40  integer :: ncid !< netcdf file id
41  character(len=*) :: vname !< netcdf variable name
42  integer, dimension(:), intent(in) :: in_index !< vector of selection indices
43  integer, dimension(:), allocatable, intent(out) :: out_index !< output vector of selection indices
44 
45  integer :: ndim
46  integer, dimension(:), allocatable :: dims
47  integer :: vtype
48  real, dimension(:), allocatable :: var1d
49  real, dimension(:,:), allocatable :: var2d
50  real(kind_real), dimension(:), allocatable :: var1d_dbl
51  real(kind_real), dimension(:,:), allocatable :: var2d_dbl
52 
53  integer :: i
54  integer :: mcount
55  integer :: mlen
56  integer :: imiss
57 
58 
59  ! Read in the test variable and check for missing values.
60  if (allocated(dims)) deallocate(dims)
61  call nc_diag_read_get_var_dims(ncid, vname, ndim, dims)
62  vtype = nc_diag_read_get_var_type(ncid, vname)
63 
64  if (ndim == 1) then
65  allocate(var1d(dims(1)))
66  if (vtype == nf90_float) then
67  call nc_diag_read_get_var(ncid, vname, var1d)
68  elseif (vtype == nf90_double) then
69  allocate(var1d_dbl(dims(1)))
70  call nc_diag_read_get_var(ncid, vname, var1d_dbl)
71  var1d = real(var1d_dbl)
72  deallocate(var1d_dbl)
73  endif
74  else
75  allocate(var2d(dims(1), dims(2)))
76  if (vtype == nf90_float) then
77  call nc_diag_read_get_var(ncid, vname, var2d)
78  elseif (vtype == nf90_double) then
79  allocate(var2d_dbl(dims(1), dims(2)))
80  call nc_diag_read_get_var(ncid, vname, var2d_dbl)
81  var2d = real(var2d_dbl)
82  deallocate(var2d_dbl)
83  endif
84 
85  allocate(var1d(dims(2)))
86  var1d = var2d(1,:)
87  deallocate(var2d)
88  endif
89 
90  ! At this point, var1d contains a sample vector that can be checked
91  ! for missing values.
92 
93  ! Read the vector and count up the missing values so that a size
94  ! can be determined for the output indx vector.
95  mcount = 0
96  do i = 1, size(in_index)
97  if (var1d(in_index(i)) .ge. missing_value) then
98  mcount = mcount + 1
99  endif
100  enddo
101  mlen = size(in_index) - mcount
102 
103  ! Allocate the output index vector and go through the test vector again
104  ! to determine which indices to omit (due to missing values).
105  allocate(out_index(mlen))
106 
107  imiss = 0
108  do i = 1, size(in_index)
109  if (var1d(in_index(i)) .lt. missing_value) then
110  imiss = imiss + 1
111  out_index(imiss) = in_index(i)
112  endif
113  enddo
114 
115  deallocate(var1d)
116  return
117  endsubroutine ioda_deselect_missing_values
118 
119 end module ioda_utils_mod
Fortran module containing IODA utility programs.
real(kind=kind_real), public missing_value
default for NetCDF file converted from PrepBUFR, the actual missing value is 9.96921e+36 ...
integer, parameter, public kind_real
subroutine, public ioda_deselect_missing_values(ncid, vname, in_index, out_index)