LCOV - code coverage report
Current view: top level - src - mo_ncread.f90 (source / functions) Hit Total Coverage
Test: forces coverage Lines: 7 711 1.0 %
Date: 2024-03-13 19:03:28 Functions: 2 31 6.5 %

          Line data    Source code
       1             : !> \file mo_ncread.f90
       2             : !> \brief \copybrief mo_ncread
       3             : !> \details \copydetails mo_ncread
       4             : 
       5             : !> \brief Reading netcdf files
       6             : !> \details Subroutines for reading arrays from nc file using the netcdf4 library.
       7             : !> \author Stephan Thober, Matthias Cuntz
       8             : !> \date Nov 2011
       9             : !> \copyright Copyright 2005-\today, the CHS Developers, Sabine Attinger: All rights reserved.
      10             : !! FORCES is released under the LGPLv3+ license \license_note
      11             : module mo_NcRead
      12             : 
      13             :   use mo_kind, only : i4, i8, sp, dp
      14             : 
      15             :   ! functions and constants of netcdf4 library
      16             :   use netcdf, only : nf90_open, nf90_get_var, nf90_close, NF90_MAX_NAME, &
      17             :           nf90_get_att, nf90_inq_varid, nf90_inquire_variable, &
      18             :           nf90_inquire_dimension, NF90_NOWRITE, &
      19             :           nf90_noerr, nf90_strerror, nf90_inquire_attribute
      20             : 
      21             :   implicit none
      22             : 
      23             :   public :: Get_NcDim    ! get the dimensions of a Variable
      24             :   public :: Get_NcDimAtt ! get the attributes of the dimensions
      25             :   public :: Get_NcVarAtt ! get attributes of a variable
      26             :   public :: Get_NcVar    ! get the data of a Variable in a nc file
      27             :   public :: NcOpen       ! Open a file and get a handle back
      28             :   public :: NcClose      ! Close a file
      29             : 
      30             :   ! ------------------------------------------------------------------------------
      31             : 
      32             :   !>    \brief Read array from NC file
      33             : 
      34             :   !>    \details
      35             :   !!    Reads a 2 - 5 dimensional array from a nc file given
      36             :   !!    the variable name EXACTLY as specified in the file.  If the
      37             :   !!    array is not allocated when calling, Get_NcVar will
      38             :   !!    allocate it internally. If the dimension of the actual data
      39             :   !!    is less than the ones of the array, then the dimension
      40             :   !!    lengths of the array will be filled with ones.
      41             :   !!
      42             :   !!    \b Example
      43             :   !!
      44             :   !!    See test program in directory test_mo_NcRead.
      45             :   !!
      46             :   !!    \b Literature
      47             :   !!
      48             :   !!    1.  http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-f90.html
      49             :   !!
      50             :   !>    \param[in]  "character(len=*) :: Filename"                                  Name of the nc file.
      51             :   !>    \param[in]  "character(len=*) :: VarName"                                   Name of the Variable in the nc file.
      52             :   !>    \param[in]  "real(sp/dp), dimension(:,:[,:[,:[,:]]]), allocatable :: array" Array where data will be read.
      53             :   !>    \param[in]  "integer(i4), dimension(:) :: jdate"                            Starting indeces of first value to read.
      54             :   !!                                                                                `len` is the number of dimensions of
      55             :   !!                                                                                array, default is 1, see example
      56             :   !>    \param[in]  "integer(i4), dimension(:) :: a_count"                          Same size as jdate, specifies how
      57             :   !!                                                                                many values in each dimension
      58             :   !!                                                                                is going to be read
      59             :   !>    \param[in]  "integer(i4)               :: fid"                              File handle of opened netcdf file
      60             : 
      61             :   !>    \note  Output array is a floating point of 2-5 dimensions.\n
      62             :   !!            NOT yet tested for different compilers than intel11.1.075
      63             :   !!            CANNOT read packed data.\n
      64             :   !!            i1 indicates, that 1 byte integer is read [type is integer(1)].
      65             : 
      66             :   !>    \author Stephan Thober
      67             :   !>    \date Nov 2011
      68             :   !!     -  added comments
      69             :   !>    \date Mar 2012
      70             :   !!     -  corrected dynamical read of data
      71             :   !>    \date May 2012
      72             :   !!     -  fid
      73             :   !>    \date Nov 2012
      74             :   !!     -  write out Varname, when vartype is incorrect
      75             :   !>    \date Feb 2013
      76             :   !!     -  added 1 byte integer version
      77             :   !>    \date Mar 2014
      78             :   !!     -  added subroutines for allocatable arrays
      79             :   !>    \author Matthias Cuntz
      80             :   !>    \date Jan 2012
      81             :   !!     -  unified routines for different dimensions and data types
      82             : 
      83             :   ! ------------------------------------------------------------------------------
      84             : 
      85             :   interface Get_NcVar
      86             :     module procedure Get_NcVar_0d_sp, Get_NcVar_0d_dp, Get_NcVar_1d_sp, &
      87             :             Get_NcVar_1d_dp, Get_NcVar_2d_sp, Get_NcVar_2d_dp, &
      88             :             Get_NcVar_3d_sp, Get_NcVar_3d_dp, Get_NcVar_4d_sp, &
      89             :             Get_NcVar_4d_dp, Get_NcVar_5d_sp, Get_NcVar_5d_dp, &
      90             :             Get_NcVar_0d_i4, Get_NcVar_1d_i4, Get_NcVar_2d_i4, &
      91             :             Get_NcVar_3d_i4, Get_NcVar_4d_i4, Get_NcVar_5d_i4, &
      92             :             Get_NcVar_0d_i1, Get_NcVar_1d_i1, Get_NcVar_2d_i1, &
      93             :             Get_NcVar_3d_i1, Get_NcVar_4d_i1, Get_NcVar_5d_i1
      94             :   end interface Get_NcVar
      95             : 
      96             :   ! ------------------------------------------------------------------------------
      97             : 
      98             :   private
      99             : 
     100             :   ! ------------------------------------------------------------------------------
     101             : 
     102             : contains
     103             : 
     104             :   ! ------------------------------------------------------------------------------
     105             : 
     106             :   !>    \brief Dimension of netcdf variable.
     107             : 
     108             :   !>    \details
     109             :   !!    Gets the dimensions of variable in a netcdf file.
     110             :   !!
     111             :   !!    \b Literature
     112             :   !!
     113             :   !!    1.  http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-f90.html
     114             :   !!
     115             :   !>    \param[in]  "character(len=*) :: Filename"            Filename of netcdf file
     116             :   !>    \param[in]  "character(len=*) :: Variable"            Variable name exactly as specified in the file
     117             :   !>    \param[in]  "logical, optional       :: PrintInfo"    If given and true, information about dimension
     118             :   !!                                                          and their lengths will be printed to standard output.
     119             :   !>    \retval     "integer(i4), dimension(5) :: Get_NcDim"  Dimension length, -1 if dimension does not exist
     120             :   !>    \param[out] "integer(i4), optional :: ndims"                    # of dimensions
     121             : 
     122             :   !>    \author Stephan Thober
     123             :   !>    \date Dec 2011
     124             :   !>    \author Matthias Cuntz
     125             :   !>    \date Jan 2012
     126             :   !!      - ndims
     127           2 :   function Get_NcDim(Filename, Variable, PrintInfo, ndims)
     128             :     !
     129             :     implicit none
     130             :     !
     131             :     character(len = *), intent(in) :: Filename
     132             :     character(len = *), intent(in) :: Variable
     133             :     logical, optional, intent(in) :: PrintInfo
     134             :     integer(i4), optional, intent(out) :: ndims
     135             :     integer(i4), dimension(5) :: Get_NcDim
     136             :     !
     137             :     logical :: PrintFlag
     138             :     integer(i4) :: ncid    ! id of input stream
     139             :     integer(i4) :: varid   ! id of variable to be read
     140             :     integer(i4) :: vartype ! type of variable
     141             :     integer(i4) :: NumDims ! # of dimensions
     142             :     !
     143             :     ! Open NetCDF filename
     144           1 :     call check(nf90_open(Filename, NF90_NOWRITE, ncid))
     145             :     !
     146           0 :     PrintFlag = .false.
     147           0 :     if (present(PrintInfo)) PrintFlag = PrintInfo
     148             :     !
     149             :     ! Inquire file and check if VarName exists in the dataset,
     150             :     ! get number of dimensions and
     151             :     ! get the length of the dimensions
     152           0 :     call Get_Info(Variable, ncid, varid, vartype, Get_NcDim, Info = PrintFlag, ndims = NumDims)
     153           0 :     if (present(ndims)) ndims = NumDims
     154             :     !
     155             :     ! close File
     156           0 :     call check(nf90_close(ncid))
     157             :     !
     158           0 :   end function Get_NcDim
     159             : 
     160             :   ! ------------------------------------------------------------------
     161             : 
     162             :   !>    \brief Name and size of variable in netcdf.
     163             : 
     164             :   !>    \details
     165             :   !!    Gets the name and size of the dimensions of a variable in a netcdf file
     166             :   !!
     167             :   !!    \b Example
     168             :   !!
     169             :   !!         Filename = 'test.nc'
     170             :   !!         Varname  = 'data'
     171             :   !!         call Get_NcDimAtt(Filename, Varname, DimNames, DimLen)
     172             :   !!
     173             :   !!    See example in test directory.
     174             :   !!
     175             :   !!    \b Literature
     176             :   !!
     177             :   !!    1.  http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-f90.html
     178             :   !!
     179             :   !>    \param[in]  "character(len=*),  intent(in) :: Filename"   Filename of netcdf file.
     180             :   !>    \param[in]  "character(len=*),  intent(in) :: Variable"   Variable name exactly as specified in the file.
     181             :   !>    \param[out] "integer(i4), dimension(:), allocatable, optional, intent(out) :: DimLen"    Allocatable array with the size
     182             :   !!                                                                                             of the dimensions
     183             :   !>    \retval     "character(len=*), dimension(:), allocatable, intent(out)      :: DimName"   Allocatable array with the
     184             :   !!                                                                                             names of the dimensions.
     185             : 
     186             :   !>    \note  DimName and DimLen are both allocated within the subroutine, so please just provide an 1 dimensional
     187             :   !!            allocatable array to the subroutine.
     188             : 
     189             :   !>    \author Matthias Zink
     190             :   !>    \date Oct 2012
     191             : 
     192           0 :   subroutine Get_NcDimAtt(Filename, Variable, DimName, DimLen)
     193             :     !
     194             :     implicit none
     195             :     !
     196             :     character(len = *), intent(in) :: Filename
     197             :     character(len = *), intent(in) :: Variable
     198             :     character(len = *), dimension(:), allocatable, &
     199             :             intent(out) :: DimName
     200             :     integer(i4), dimension(:), allocatable, &
     201             :             optional, intent(out) :: DimLen
     202             :     !
     203             :     integer(i4), dimension(5) :: Get_NcDim
     204             :     !
     205             :     integer(i4) :: ncid    ! id of input stream
     206             :     integer(i4) :: varid   ! id of variable to be read
     207             :     integer(i4) :: vartype ! type of variable
     208             :     integer(i4) :: NumDims ! # of dimensions
     209             :     integer(i4) :: dimid
     210             :     integer(i4) :: len
     211             :     !
     212             :     ! Open NetCDF filename
     213           0 :     call check(nf90_open(Filename, NF90_NOWRITE, ncid))
     214             :     !
     215             :     ! Inquire file and check if VarName exists in the dataset,
     216             :     ! get number of dimensions and
     217             :     ! get the length of the dimensions
     218           0 :     call Get_Info(Variable, ncid, varid, vartype, Get_NcDim, Info = .FALSE., ndims = NumDims)
     219             :     !
     220           0 :     allocate(DimName(NumDims))
     221           0 :     if (present(DimLen)) allocate(DimLen(NumDims))
     222             :     !
     223           0 :     do dimid = 1, NumDims
     224           0 :       call check(nf90_inquire_dimension(ncid, dimid, DimName(dimid), len))
     225           0 :       if (present(DimLen)) DimLen(dimid) = len
     226             :     end do
     227             :     ! close File
     228           0 :     call check(nf90_close(ncid))
     229             :     !
     230           1 :   end subroutine Get_NcDimAtt
     231             : 
     232             :   ! ------------------------------------------------------------------
     233             : 
     234             :   !>    \brief Get attribute of netcdf variable.
     235             : 
     236             :   !>    \details
     237             :   !!    Gets the values of an particular attribute of an variable.
     238             :   !!
     239             :   !!    \b Example
     240             :   !!
     241             :   !!         Filename = 'test.nc'
     242             :   !!         VarName  = 'data'
     243             :   !!         AttName  = 'units'
     244             :   !!         call Get_NcDimAtt(Filename, Varname, AttName, AttValues)
     245             :   !!
     246             :   !!    See example in test directory.
     247             :   !!
     248             :   !!    \b Literature
     249             :   !!
     250             :   !!    1.  http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-f90.html
     251             : 
     252             :   !>    \param[in]  "character(len=*), intent(in)      :: FileName"     Filename of netcdf file.
     253             :   !>    \param[in]  "character(len=*), intent(in)      :: VarName"      Variable name exactly as specified in the file.
     254             :   !>    \param[in]  "character(len=*), intent(in)      :: AttName"      Attribute name exactly as specified for the Variable.
     255             :   !>    \param[in]  "integer(i4), optional, intent(in) :: fid"          File handle of opened netcdf file
     256             :   !>    \param[in]  "integer(i4), optional, intent(in) :: dtype"        Datatype (ineteger,float) see NetCDF convention
     257             :   !!                                                                    (unidata.ucar)
     258             :   !>    \retval     "character(len=*), intent(out)     :: AttValues"    Values of the Attribute.
     259             : 
     260             :   !>    \note AttValues are restricted to be of character type. \n
     261             :   !!           The name of the variable (VarName) has to be known beforehand. \n
     262             :   !!           The name of the attribute (AttName) has to be known beforehand.
     263             : 
     264             :   !>    \author Matthias Zink
     265             :   !>    \date Oct 2012
     266             :   !>    \author Matthias Cuntz & Juliane Mai
     267             :   !>    \date Nov 2014
     268             :   !!      - correct data type detection.
     269             : 
     270           0 :   subroutine Get_NcVarAtt(FileName, VarName, AttName, AttValues, fid, dtype)
     271             :     !
     272             :     implicit none
     273             :     !
     274             :     character(len = *), intent(in) :: FileName
     275             :     character(len = *), intent(in) :: VarName
     276             :     character(len = *), intent(in) :: AttName
     277             :     character(len = *), intent(out) :: AttValues
     278             :     integer(i4), optional, intent(in) :: fid
     279             :     integer(i4), optional, intent(out) :: dtype
     280             :     !
     281             :     integer(i4) :: ncid
     282             :     integer(i4) :: varid
     283             :     !
     284             :     integer(i4) :: type
     285             :     integer(i8) :: avint
     286           0 :     real(dp) :: avfloat
     287             :     character(256) :: avchar
     288             :     !
     289           0 :     if (present(fid)) then
     290           0 :       ncid = fid
     291             :     else
     292           0 :       call check(nf90_open(trim(Filename), NF90_NOWRITE, ncid))
     293             :     end if
     294             :     !
     295             :     ! Inquire file, check if VarName exists and get the id
     296           0 :     call check(nf90_inq_varid(ncid, trim(VarName), varid))
     297             :     ! get type of the attribute
     298           0 :     call check(nf90_inquire_attribute(ncid, varid, trim(AttName), type))
     299             :     !
     300             :     ! read attribute by type
     301           0 :     select case (type)
     302             :     case (1) ! 1 = NF90_BYTE
     303           0 :       call check(nf90_get_att(ncid, varid, trim(AttName), avint))
     304           0 :       write(AttValues, '(i4)')  avint
     305           0 :       AttValues = adjustl(trim(AttValues))
     306           0 :       if (present(dtype)) dtype = type
     307             :     case (2) ! NF90_CHAR
     308           0 :       call check(nf90_get_att(ncid, varid, trim(AttName), avchar))
     309           0 :       AttValues = adjustl(trim(avchar))
     310           0 :       if (present(dtype)) dtype = type
     311             :     case (3) ! NF90_SHORT
     312           0 :       call check(nf90_get_att(ncid, varid, trim(AttName), avint))
     313           0 :       write(AttValues, '(i6)')  avint
     314           0 :       AttValues = adjustl(trim(AttValues))
     315           0 :       if (present(dtype)) dtype = type
     316             :     case (4) ! NF90_INT
     317           0 :       call check(nf90_get_att(ncid, varid, trim(AttName), avint))
     318           0 :       write(AttValues, '(i11)')  avint
     319           0 :       AttValues = adjustl(trim(AttValues))
     320           0 :       if (present(dtype)) dtype = type
     321             :     case (5) ! NF90_FLOAT
     322           0 :       call check(nf90_get_att(ncid, varid, trim(AttName), avfloat))
     323           0 :       write(AttValues, '(E15.7)')  avfloat
     324           0 :       AttValues = adjustl(trim(AttValues))
     325           0 :       if (present(dtype)) dtype = type
     326             :     case (6) ! NF90_DOUBLE
     327           0 :       call check(nf90_get_att(ncid, varid, trim(AttName), avfloat))
     328           0 :       write(AttValues, '(E24.15)')  avfloat
     329           0 :       AttValues = adjustl(trim(AttValues))
     330           0 :       if (present(dtype)) dtype = type
     331             :     case DEFAULT
     332           0 :       print*, '***ERROR: mo_ncread: Mismatch in attribute datatype!'
     333             :     end select
     334             :     !
     335           0 :     if (.not. present(fid)) call check(nf90_close(ncid))
     336             :     !
     337           0 :   end subroutine Get_NcVarAtt
     338             : 
     339             :   ! ------------------------------------------------------------------------------
     340             : 
     341           0 :   subroutine Get_NcVar_0d_sp(Filename, VarName, Dat, fid)
     342             :     !
     343             :     implicit none
     344             :     !
     345             :     integer, parameter :: itype = 5 ! 5 = float, 6 = double
     346             :     !
     347             :     character(len = *), intent(in) :: Filename
     348             :     character(len = *), intent(in) :: VarName ! Variable name
     349             :     real(sp), intent(inout) :: Dat    ! array where values should be stored
     350             :     integer(i4), optional, intent(in) :: fid
     351             :     !
     352             :     integer(i4) :: ncid    ! id of input stream
     353             :     integer(i4) :: varid   ! id of variable to be read
     354             :     integer(i4) :: vartype ! type of variable
     355             :     !
     356             :     ! Open NetCDF filename
     357           0 :     if (present(fid)) then
     358           0 :       ncid = fid
     359             :     else
     360           0 :       call check(nf90_open(trim(Filename), NF90_NOWRITE, ncid))
     361             :     end if
     362             :     !
     363             :     ! Inquire file, check if VarName exists and get the id
     364           0 :     call Get_Info(Varname, ncid, varid, vartype)
     365             :     ! check variable type ( 5 equals float type, 6 equals double )
     366           0 :     if (vartype /= itype) then
     367           0 :       print *, 'Variable name: ', trim(Varname)
     368           0 :       print *, 'ERROR*** type of variable does not match argument type. subroutine Get_NcVar'
     369           0 :       stop
     370             :     end if
     371             :     !
     372             :     ! get values by varid
     373           0 :     call check(nf90_get_var(ncid, varid, Dat))
     374             :     !
     375             :     ! close File
     376           0 :     if (.not. present(fid)) call check(nf90_close(ncid))
     377             :     !
     378           0 :   end subroutine Get_NcVar_0d_sp
     379             : 
     380           0 :   subroutine Get_NcVar_0d_dp(Filename, VarName, Dat, fid)
     381             :     !
     382             :     implicit none
     383             :     !
     384             :     integer, parameter :: itype = 6 ! 5 = float, 6 = double
     385             :     !
     386             :     character(len = *), intent(in) :: Filename
     387             :     character(len = *), intent(in) :: VarName ! Variable name
     388             :     real(dp), intent(inout) :: Dat    ! array where values should be stored
     389             :     integer(i4), optional, intent(in) :: fid
     390             :     !
     391             :     integer(i4) :: ncid    ! id of input stream
     392             :     integer(i4) :: varid   ! id of variable to be read
     393             :     integer(i4) :: vartype ! type of variable
     394             :     !
     395             :     ! Open NetCDF filename
     396           0 :     if (present(fid)) then
     397           0 :       ncid = fid
     398             :     else
     399           0 :       call check(nf90_open(trim(Filename), NF90_NOWRITE, ncid))
     400             :     end if
     401             :     !
     402             :     ! Inquire file, check if VarName exists and get the id
     403           0 :     call Get_Info(Varname, ncid, varid, vartype)
     404             :     ! check variable type ( 5 equals float type, 6 equals double )
     405           0 :     if (vartype /= itype) then
     406           0 :       print *, 'Variable name: ', trim(Varname)
     407           0 :       print *, 'ERROR*** type of variable does not match argument type. subroutine Get_NcVar'
     408           0 :       stop
     409             :     end if
     410             :     !
     411             :     ! get values by varid
     412           0 :     call check(nf90_get_var(ncid, varid, Dat))
     413             :     !
     414             :     ! close File
     415           0 :     if (.not. present(fid)) call check(nf90_close(ncid))
     416             :     !
     417           0 :   end subroutine Get_NcVar_0d_dp
     418             : 
     419           0 :   subroutine Get_NcVar_1d_sp(Filename, VarName, Dat, start, a_count, fid)
     420             :     !
     421             :     implicit none
     422             :     !
     423             :     integer, parameter :: idims = 1
     424             :     integer, parameter :: itype = 5 ! 5 = float, 6 = double
     425             :     !
     426             :     character(len = *), intent(in) :: Filename
     427             :     character(len = *), intent(in) :: VarName ! Variable name
     428             :     real(sp), dimension(:), allocatable, intent(inout) :: Dat    ! array where values should be stored
     429             :     integer(i4), dimension(:), optional, intent(in) :: start
     430             :     integer(i4), dimension(:), optional, intent(in) :: a_count
     431             :     integer(i4), optional, intent(in) :: fid
     432             :     !
     433             :     integer(i4), dimension(5) :: Rstart
     434             :     integer(i4), dimension(5) :: Rcount
     435             :     integer(i4) :: ncid    ! id of input stream
     436             :     integer(i4) :: varid   ! id of variable to be read
     437             :     integer(i4) :: vartype ! type of variable
     438             :     integer(i4) :: i
     439             :     !
     440             :     ! Defaults for Options Start and Count
     441           0 :     Rstart = 1
     442           0 :     Rcount = 1
     443             :     !
     444             :     ! allocate Dat
     445           0 :     if (.not. allocated(Dat)) then
     446           0 :       if (.not. present(a_count)) then
     447           0 :         Rcount = Get_NcDim(Filename, Varname)
     448             :       else
     449           0 :         Rcount(1 : idims) = a_count(1 : idims)
     450             :       end if
     451           0 :       allocate(Dat(Rcount(1)))
     452             :     else
     453           0 :       Rcount(1 : idims) = shape(Dat)
     454             :     end if
     455             :     !
     456             :     ! Assign options Start and Count if present
     457           0 :     if (present(start)) then
     458           0 :       if (size(start) < size(shape(dat))) stop 'ERROR*** start has less values than data has dimensions. GetNcVar'
     459           0 :       if (size(start) > 5) stop 'ERROR*** start has dimension greater than 5. GetNcVar'
     460           0 :       Rstart(1 : size(start)) = start
     461             :     end if
     462             :     !
     463           0 :     if (present(a_count)) then
     464           0 :       if (size(a_count) < size(shape(dat))) stop 'ERROR*** count has less values than data has dimensions. GetNcVar'
     465           0 :       if (size(a_count) > 5) stop 'ERROR*** count has dimension greater than 5. GetNcVar'
     466           0 :       Rcount(1 : size(a_count)) = a_count
     467           0 :       do i = 1, idims
     468           0 :         if (size(Dat, i) < Rcount(i)) stop 'ERROR*** try to read more data in dimension than there is. Get_NcVar'
     469             :       end do
     470             :     end if
     471             :     !
     472             :     ! Open NetCDF filename
     473           0 :     if (present(fid)) then
     474           0 :       ncid = fid
     475             :     else
     476           0 :       call check(nf90_open(trim(Filename), NF90_NOWRITE, ncid))
     477             :     end if
     478             :     !
     479             :     ! Inquire file, check if VarName exists and get the id
     480           0 :     call Get_Info(Varname, ncid, varid, vartype)
     481             :     ! check variable type ( 5 equals float type, 6 equals double )
     482           0 :     if (vartype /= itype) then
     483           0 :       print *, 'Variable name: ', trim(Varname)
     484           0 :       print *, 'ERROR*** type of variable does not match argument type. subroutine Get_NcVar'
     485           0 :       stop
     486             :     end if
     487             :     !
     488             :     ! get values by varid
     489           0 :     call check(nf90_get_var(ncid, varid, Dat, Rstart, Rcount))
     490             :     !
     491             :     ! close File
     492           0 :     if (.not. present(fid)) call check(nf90_close(ncid))
     493             :     !
     494           0 :   end subroutine Get_NcVar_1d_sp
     495             : 
     496             : 
     497           0 :   subroutine Get_NcVar_1d_dp(Filename, VarName, Dat, start, a_count, fid)
     498             :     !
     499             :     implicit none
     500             :     !
     501             :     integer, parameter :: idims = 1
     502             :     integer, parameter :: itype = 6 ! 5 = float, 6 = double
     503             :     !
     504             :     character(len = *), intent(in) :: Filename
     505             :     character(len = *), intent(in) :: VarName ! Variable name
     506             :     real(dp), dimension(:), allocatable, intent(inout) :: Dat    ! array where values should be stored
     507             :     integer(i4), dimension(:), optional, intent(in) :: start
     508             :     integer(i4), dimension(:), optional, intent(in) :: a_count
     509             :     integer(i4), optional, intent(in) :: fid
     510             :     !
     511             :     integer(i4), dimension(5) :: Rstart
     512             :     integer(i4), dimension(5) :: Rcount
     513             :     integer(i4) :: ncid    ! id of input stream
     514             :     integer(i4) :: varid   ! id of variable to be read
     515             :     integer(i4) :: vartype ! type of variable
     516             :     integer(i4) :: i
     517             :     !
     518             :     ! Defaults for Options Start and Count
     519           0 :     Rstart = 1
     520           0 :     Rcount = 1
     521             :     !
     522             :     ! allocate Dat
     523           0 :     if (.not. allocated(Dat)) then
     524           0 :       if (.not. present(a_count)) then
     525           0 :         Rcount = Get_NcDim(Filename, Varname)
     526             :       else
     527           0 :         Rcount(1 : idims) = a_count(1 : idims)
     528             :       end if
     529           0 :       allocate(Dat(Rcount(1)))
     530             :     else
     531           0 :       Rcount(1 : idims) = shape(Dat)
     532             :     end if
     533             :     !
     534             :     ! Assign options Start and Count if present
     535           0 :     if (present(start)) then
     536           0 :       if (size(start) < size(shape(dat))) stop 'ERROR*** start has less values than data has dimensions. GetNcVar'
     537           0 :       if (size(start) > 5) stop 'ERROR*** start has dimension greater than 5. GetNcVar'
     538           0 :       Rstart(1 : size(start)) = start
     539             :     end if
     540             :     !
     541           0 :     if (present(a_count)) then
     542           0 :       if (size(a_count) < size(shape(dat))) stop 'ERROR*** count has less values than data has dimensions. GetNcVar'
     543           0 :       if (size(a_count) > 5) stop 'ERROR*** count has dimension greater than 5. GetNcVar'
     544           0 :       Rcount(1 : size(a_count)) = a_count
     545           0 :       do i = 1, idims
     546           0 :         if (size(Dat, i) < Rcount(i)) stop 'ERROR*** try to read more data in dimension than there is. Get_NcVar'
     547             :       end do
     548             :     end if
     549             :     !
     550             :     ! Open NetCDF filename
     551           0 :     if (present(fid)) then
     552           0 :       ncid = fid
     553             :     else
     554           0 :       call check(nf90_open(trim(Filename), NF90_NOWRITE, ncid))
     555             :     end if
     556             :     !
     557             :     ! Inquire file, check if VarName exists and get the id
     558           0 :     call Get_Info(Varname, ncid, varid, vartype)
     559             :     ! check variable type ( 5 equals float type, 6 equals double )
     560           0 :     if (vartype /= itype) then
     561           0 :       print *, 'Variable name: ', trim(Varname)
     562           0 :       print *, 'ERROR*** type of variable does not match argument type. subroutine Get_NcVar'
     563           0 :       stop
     564             :     end if
     565             :     !
     566             :     ! get values by varid
     567           0 :     call check(nf90_get_var(ncid, varid, Dat, Rstart, Rcount))
     568             :     !
     569             :     ! close File
     570           0 :     if (.not. present(fid)) call check(nf90_close(ncid))
     571             :     !
     572           0 :   end subroutine Get_NcVar_1d_dp
     573             : 
     574             : 
     575           0 :   subroutine Get_NcVar_2d_sp(Filename, VarName, Dat, start, a_count, fid)
     576             :     !
     577             :     implicit none
     578             :     !
     579             :     integer, parameter :: idims = 2
     580             :     integer, parameter :: itype = 5 ! 5 = float, 6 = double
     581             :     !
     582             :     character(len = *), intent(in) :: Filename
     583             :     character(len = *), intent(in) :: VarName ! Variable name
     584             :     !real(sp),    dimension(:,:), allocatable, intent(inout) :: Dat    ! array where values should be stored
     585             :     real(sp), dimension(:, :), allocatable, intent(inout) :: Dat    ! array where values should be stored
     586             :     integer(i4), dimension(:), optional, intent(in) :: start
     587             :     integer(i4), dimension(:), optional, intent(in) :: a_count
     588             :     integer(i4), optional, intent(in) :: fid
     589             :     !
     590             :     integer(i4), dimension(5) :: Rstart
     591             :     integer(i4), dimension(5) :: Rcount
     592             :     integer(i4) :: ncid    ! id of input stream
     593             :     integer(i4) :: varid   ! id of variable to be read
     594             :     integer(i4) :: vartype ! type of variable
     595             :     integer(i4) :: i
     596             :     !
     597             :     ! Defaults for Options Start and Count
     598           0 :     Rstart = 1
     599           0 :     Rcount = 1
     600             :     !
     601             :     ! allocate Dat
     602           0 :     if (.not. allocated(Dat)) then
     603           0 :       if (.not. present(a_count)) then
     604           0 :         Rcount = Get_NcDim(Filename, Varname)
     605             :       else
     606           0 :         Rcount(1 : idims) = a_count(1 : idims)
     607             :       end if
     608           0 :       allocate(Dat(Rcount(1), Rcount(2)))
     609             :     else
     610           0 :       Rcount(1 : idims) = shape(Dat)
     611             :     end if
     612             :     !
     613             :     ! Assign options Start and Count if present
     614           0 :     if (present(start)) then
     615           0 :       if (size(start) < size(shape(dat))) stop 'ERROR*** start has less values than data has dimensions. GetNcVar'
     616           0 :       if (size(start) > 5) stop 'ERROR*** start has dimension greater than 5. GetNcVar'
     617           0 :       Rstart(1 : size(start)) = start
     618             :     end if
     619             :     !
     620           0 :     if (present(a_count)) then
     621           0 :       if (size(a_count) < size(shape(dat))) stop 'ERROR*** a_count has less values than data has dimensions. GetNcVar'
     622           0 :       if (size(a_count) > 5) stop 'ERROR*** a_count has dimension greater than 5. GetNcVar'
     623           0 :       Rcount(1 : size(a_count)) = a_count
     624           0 :       do i = 1, idims
     625           0 :         if (size(Dat, i) < Rcount(i)) stop 'ERROR*** try to read more data in dimension than there is. Get_NcVar'
     626             :       end do
     627             :     end if
     628             :     !
     629             :     ! Open NetCDF filename
     630           0 :     if (present(fid)) then
     631           0 :       ncid = fid
     632             :     else
     633           0 :       call check(nf90_open(trim(Filename), NF90_NOWRITE, ncid))
     634             :     end if
     635             :     !
     636             :     ! Inquire file, check if VarName exists and get the id
     637           0 :     call Get_Info(Varname, ncid, varid, vartype)
     638             :     ! check variable type ( 5 equals float type, 6 equals double )
     639           0 :     if (vartype /= itype) then
     640           0 :       print *, 'Variable name: ', trim(Varname)
     641           0 :       print *, 'ERROR*** type of variable does not match argument type. subroutine Get_NcVar'
     642           0 :       stop
     643             :     end if
     644             :     !
     645             :     ! get values by varid
     646           0 :     call check(nf90_get_var(ncid, varid, Dat, Rstart, Rcount))
     647             :     !
     648             :     ! close File
     649           0 :     if (.not. present(fid)) call check(nf90_close(ncid))
     650             :     !
     651           0 :   end subroutine Get_NcVar_2d_sp
     652             : 
     653             : 
     654           0 :   subroutine Get_NcVar_2d_dp(Filename, VarName, Dat, start, a_count, fid)
     655             :     !
     656             :     implicit none
     657             :     !
     658             :     integer, parameter :: idims = 2
     659             :     integer, parameter :: itype = 6 ! 5 = float, 6 = double
     660             :     !
     661             :     character(len = *), intent(in) :: Filename
     662             :     character(len = *), intent(in) :: VarName ! Variable name
     663             :     real(dp), dimension(:, :), allocatable, intent(inout) :: Dat    ! array where values should be stored
     664             :     integer(i4), dimension(:), optional, intent(in) :: start
     665             :     integer(i4), dimension(:), optional, intent(in) :: a_count
     666             :     integer(i4), optional, intent(in) :: fid
     667             :     !
     668             :     integer(i4), dimension(5) :: Rstart
     669             :     integer(i4), dimension(5) :: Rcount
     670             :     integer(i4) :: ncid    ! id of input stream
     671             :     integer(i4) :: varid   ! id of variable to be read
     672             :     integer(i4) :: vartype ! type of variable
     673             :     integer(i4) :: i
     674             :     !
     675             :     ! Defaults for Options Start and Count
     676           0 :     Rstart = 1
     677           0 :     Rcount = 1
     678             :     !
     679             :     ! allocate Dat
     680           0 :     if (.not. allocated(Dat)) then
     681           0 :       if (.not. present(a_count)) then
     682           0 :         Rcount = Get_NcDim(Filename, Varname)
     683             :       else
     684           0 :         Rcount(1 : idims) = a_count(1 : idims)
     685             :       end if
     686           0 :       allocate(Dat(Rcount(1), Rcount(2)))
     687             :     else
     688           0 :       Rcount(1 : idims) = shape(Dat)
     689             :     end if
     690             :     !
     691             :     ! Assign options Start and Count if present
     692           0 :     if (present(start)) then
     693           0 :       if (size(start) < size(shape(dat))) stop 'ERROR*** start has less values than data has dimensions. GetNcVar'
     694           0 :       if (size(start) > 5) stop 'ERROR*** start has dimension greater than 5. GetNcVar'
     695           0 :       Rstart(1 : size(start)) = start
     696             :     end if
     697             :     !
     698           0 :     if (present(a_count)) then
     699           0 :       if (size(a_count) < size(shape(dat))) stop 'ERROR*** a_count has less values than data has dimensions. GetNcVar'
     700           0 :       if (size(a_count) > 5) stop 'ERROR*** a_count has dimension greater than 5. GetNcVar'
     701           0 :       Rcount(1 : size(a_count)) = a_count
     702           0 :       do i = 1, idims
     703           0 :         if (size(Dat, i) < Rcount(i)) stop 'ERROR*** try to read more data in dimension than there is. Get_NcVar'
     704             :       end do
     705             :     end if
     706             :     !
     707             :     ! Open NetCDF filename
     708           0 :     if (present(fid)) then
     709           0 :       ncid = fid
     710             :     else
     711           0 :       call check(nf90_open(trim(Filename), NF90_NOWRITE, ncid))
     712             :     end if
     713             :     !
     714             :     ! Inquire file, check if VarName exists and get the id
     715           0 :     call Get_Info(Varname, ncid, varid, vartype)
     716             :     ! check variable type ( 5 equals float type, 6 equals double )
     717           0 :     if (vartype /= itype) then
     718           0 :       print *, 'Variable name: ', trim(Varname)
     719           0 :       print *, 'ERROR*** type of variable does not match argument type. subroutine Get_NcVar'
     720           0 :       stop
     721             :     end if
     722             :     !
     723             :     ! get values by varid
     724           0 :     call check(nf90_get_var(ncid, varid, Dat, Rstart, Rcount))
     725             :     !
     726             :     ! close File
     727           0 :     if (.not. present(fid)) call check(nf90_close(ncid))
     728             :     !
     729           0 :   end subroutine Get_NcVar_2d_dp
     730             : 
     731             : 
     732           0 :   subroutine Get_NcVar_3d_sp(Filename, VarName, Dat, start, a_count, fid)
     733             :     !
     734             :     implicit none
     735             :     !
     736             :     integer, parameter :: idims = 3
     737             :     integer, parameter :: itype = 5 ! 5 = float, 6 = double
     738             :     !
     739             :     character(len = *), intent(in) :: Filename
     740             :     character(len = *), intent(in) :: VarName ! Variable name
     741             :     real(sp), dimension(:, :, :), allocatable, intent(inout) :: Dat    ! array where values should be stored
     742             :     integer(i4), dimension(:), optional, intent(in) :: start
     743             :     integer(i4), dimension(:), optional, intent(in) :: a_count
     744             :     integer(i4), optional, intent(in) :: fid
     745             :     !
     746             :     integer(i4), dimension(5) :: Rstart
     747             :     integer(i4), dimension(5) :: Rcount
     748             :     integer(i4) :: ncid    ! id of input stream
     749             :     integer(i4) :: varid   ! id of variable to be read
     750             :     integer(i4) :: vartype ! type of variable
     751             :     integer(i4) :: i
     752             : 
     753             :     !
     754             :     ! Defaults for Options Start and Count
     755           0 :     Rstart = 1
     756           0 :     Rcount = 1
     757             :     !
     758             :     ! allocate Dat
     759           0 :     if (.not. allocated(Dat)) then
     760           0 :       if (.not. present(a_count)) then
     761           0 :         Rcount = Get_NcDim(Filename, Varname)
     762             :       else
     763           0 :         Rcount(1 : idims) = a_count(1 : idims)
     764             :       end if
     765           0 :       allocate(Dat(Rcount(1), Rcount(2), Rcount(3)))
     766             :     else
     767           0 :       Rcount(1 : idims) = shape(Dat)
     768             :     end if
     769             :     !
     770             :     ! Assign options Start and Count if present
     771           0 :     if (present(start)) then
     772           0 :       if (size(start) < size(shape(dat))) stop 'ERROR*** start has less values than data has dimensions. GetNcVar'
     773           0 :       if (size(start) > 5) stop 'ERROR*** start has dimension greater than 5. GetNcVar'
     774           0 :       Rstart(1 : size(start)) = start
     775             :     end if
     776             :     !
     777           0 :     if (present(a_count)) then
     778           0 :       if (size(a_count) < size(shape(dat))) stop 'ERROR*** a_count has less values than data has dimensions. GetNcVar'
     779           0 :       if (size(a_count) > 5) stop 'ERROR*** a_count has dimension greater than 5. GetNcVar'
     780           0 :       Rcount(1 : size(a_count)) = a_count
     781           0 :       do i = 1, idims
     782           0 :         if (size(Dat, i) < Rcount(i)) stop 'ERROR*** try to read more data in dimension than there is. Get_NcVar'
     783             :       end do
     784             :     end if
     785             : 
     786             :     !
     787             :     ! Open NetCDF filename
     788           0 :     if (present(fid)) then
     789           0 :       ncid = fid
     790             :     else
     791           0 :       call check(nf90_open(trim(Filename), NF90_NOWRITE, ncid))
     792             :     end if
     793             :     !
     794             :     ! Inquire file, check if VarName exists and get the id
     795           0 :     call Get_Info(Varname, ncid, varid, vartype)
     796             :     ! check variable type ( 5 equals float type, 6 equals double )
     797           0 :     if (vartype /= itype) then
     798           0 :       print *, 'Variable name: ', trim(Varname)
     799           0 :       print *, 'ERROR*** type of variable does not match argument type. subroutine Get_NcVar'
     800           0 :       stop
     801             :     end if
     802             :     !
     803             :     ! get values by varid
     804           0 :     call check(nf90_get_var(ncid, varid, Dat, Rstart, Rcount))
     805             :     !
     806             :     ! close File
     807           0 :     if (.not. present(fid)) call check(nf90_close(ncid))
     808             :     !
     809           0 :   end subroutine Get_NcVar_3d_sp
     810             : 
     811             : 
     812           0 :   subroutine Get_NcVar_3d_dp(Filename, VarName, Dat, start, a_count, fid)
     813             :     !
     814             :     implicit none
     815             :     !
     816             :     integer, parameter :: idims = 3
     817             :     integer, parameter :: itype = 6 ! 5 = float, 6 = double
     818             :     !
     819             :     character(len = *), intent(in) :: Filename
     820             :     character(len = *), intent(in) :: VarName ! Variable name
     821             :     real(dp), dimension(:, :, :), allocatable, intent(inout) :: Dat    ! array where values should be stored
     822             :     integer(i4), dimension(:), optional, intent(in) :: start
     823             :     integer(i4), dimension(:), optional, intent(in) :: a_count
     824             :     integer(i4), optional, intent(in) :: fid
     825             :     !
     826             :     integer(i4), dimension(5) :: Rstart
     827             :     integer(i4), dimension(5) :: Rcount
     828             :     integer(i4) :: ncid    ! id of input stream
     829             :     integer(i4) :: varid   ! id of variable to be read
     830             :     integer(i4) :: vartype ! type of variable
     831             :     integer(i4) :: i
     832             :     !
     833             :     ! Defaults for Options Start and Count
     834           0 :     Rstart = 1
     835           0 :     Rcount = 1
     836             :     !
     837             :     ! allocate Dat
     838           0 :     if (.not. allocated(Dat)) then
     839           0 :       if (.not. present(a_count)) then
     840           0 :         Rcount = Get_NcDim(Filename, Varname)
     841             :       else
     842           0 :         Rcount(1 : idims) = a_count(1 : idims)
     843             :       end if
     844           0 :       allocate(Dat(Rcount(1), Rcount(2), Rcount(3)))
     845             :     else
     846           0 :       Rcount(1 : idims) = shape(Dat)
     847             :     end if
     848             :     !
     849             :     ! Assign options Start and Count if present
     850           0 :     if (present(start)) then
     851           0 :       if (size(start) < size(shape(dat))) stop 'ERROR*** start has less values than data has dimensions. GetNcVar'
     852           0 :       if (size(start) > 5) stop 'ERROR*** start has dimension greater than 5. GetNcVar'
     853           0 :       Rstart(1 : size(start)) = start
     854             :     end if
     855             :     !
     856           0 :     if (present(a_count)) then
     857           0 :       if (size(a_count) < size(shape(dat))) stop 'ERROR*** a_count has less values than data has dimensions. GetNcVar'
     858           0 :       if (size(a_count) > 5) stop 'ERROR*** a_count has dimension greater than 5. GetNcVar'
     859           0 :       Rcount(1 : size(a_count)) = a_count
     860           0 :       do i = 1, idims
     861           0 :         if (size(Dat, i) < Rcount(i)) stop 'ERROR*** try to read more data in dimension than there is. Get_NcVar'
     862             :       end do
     863             :     end if
     864             :     !
     865             :     ! Open NetCDF filename
     866           0 :     if (present(fid)) then
     867           0 :       ncid = fid
     868             :     else
     869           0 :       call check(nf90_open(trim(Filename), NF90_NOWRITE, ncid))
     870             :     end if
     871             :     !
     872             :     ! Inquire file, check if VarName exists and get the id
     873           0 :     call Get_Info(Varname, ncid, varid, vartype)
     874             :     ! check variable type ( 5 equals float type, 6 equals double )
     875           0 :     if (vartype /= itype) then
     876           0 :       print *, 'Variable name: ', trim(Varname)
     877           0 :       print *, 'ERROR*** type of variable does not match argument type. subroutine Get_NcVar'
     878           0 :       stop
     879             :     end if
     880             :     !
     881             :     ! get values by varid
     882           0 :     call check(nf90_get_var(ncid, varid, Dat, Rstart, Rcount))
     883             :     !
     884             :     ! close File
     885           0 :     if (.not. present(fid)) call check(nf90_close(ncid))
     886             :     !
     887           0 :   end subroutine Get_NcVar_3d_dp
     888             : 
     889             : 
     890           0 :   subroutine Get_NcVar_4d_sp(Filename, VarName, Dat, start, a_count, fid)
     891             :     !
     892             :     implicit none
     893             :     !
     894             :     integer, parameter :: idims = 4
     895             :     integer, parameter :: itype = 5 ! 5 = float, 6 = double
     896             :     !
     897             :     character(len = *), intent(in) :: Filename
     898             :     character(len = *), intent(in) :: VarName ! Variable name
     899             :     real(sp), dimension(:, :, :, :), allocatable, intent(inout) :: Dat    ! array where values should be stored
     900             :     integer(i4), dimension(:), optional, intent(in) :: start
     901             :     integer(i4), dimension(:), optional, intent(in) :: a_count
     902             :     integer(i4), optional, intent(in) :: fid
     903             :     !
     904             :     integer(i4), dimension(5) :: Rstart
     905             :     integer(i4), dimension(5) :: Rcount
     906             :     integer(i4) :: ncid    ! id of input stream
     907             :     integer(i4) :: varid   ! id of variable to be read
     908             :     integer(i4) :: vartype ! type of variable
     909             :     integer(i4) :: i
     910             :     !
     911             :     ! Defaults for Options Start and Count
     912           0 :     Rstart = 1
     913           0 :     Rcount = 1
     914             :     !
     915             :     ! allocate Dat
     916           0 :     if (.not. allocated(Dat)) then
     917           0 :       if (.not. present(a_count)) then
     918           0 :         Rcount = Get_NcDim(Filename, Varname)
     919             :       else
     920           0 :         Rcount(1 : idims) = a_count(1 : idims)
     921             :       end if
     922           0 :       allocate(Dat(Rcount(1), Rcount(2), Rcount(3), Rcount(4)))
     923             :     else
     924           0 :       Rcount(1 : idims) = shape(Dat)
     925             :     end if
     926             :     !
     927             :     ! Assign options Start and Count if present
     928           0 :     if (present(start)) then
     929           0 :       if (size(start) < size(shape(dat))) stop 'ERROR*** start has less values than data has dimensions. GetNcVar'
     930           0 :       if (size(start) > 5) stop 'ERROR*** start has dimension greater than 5. GetNcVar'
     931           0 :       Rstart(1 : size(start)) = start
     932             :     end if
     933             :     !
     934           0 :     if (present(a_count)) then
     935           0 :       if (size(a_count) < size(shape(dat))) stop 'ERROR*** a_count has less values than data has dimensions. GetNcVar'
     936           0 :       if (size(a_count) > 5) stop 'ERROR*** a_count has dimension greater than 5. GetNcVar'
     937           0 :       Rcount(1 : size(a_count)) = a_count
     938           0 :       do i = 1, idims
     939           0 :         if (size(Dat, i) < Rcount(i)) stop 'ERROR*** try to read more data in dimension than there is. Get_NcVar'
     940             :       end do
     941             :     end if
     942             :     !
     943             :     ! Open NetCDF filename
     944           0 :     if (present(fid)) then
     945           0 :       ncid = fid
     946             :     else
     947           0 :       call check(nf90_open(trim(Filename), NF90_NOWRITE, ncid))
     948             :     end if
     949             :     !
     950             :     ! Inquire file, check if VarName exists and get the id
     951           0 :     call Get_Info(Varname, ncid, varid, vartype)
     952             :     ! check variable type ( 5 equals float type, 6 equals double )
     953           0 :     if (vartype /= itype) then
     954           0 :       print *, 'Variable name: ', trim(Varname)
     955           0 :       print *, 'ERROR*** type of variable does not match argument type. subroutine Get_NcVar'
     956           0 :       stop
     957             :     end if
     958             :     !
     959             :     ! get values by varid
     960           0 :     call check(nf90_get_var(ncid, varid, Dat, Rstart, Rcount))
     961             :     !
     962             :     ! close File
     963           0 :     if (.not. present(fid)) call check(nf90_close(ncid))
     964             :     !
     965           0 :   end subroutine Get_NcVar_4d_sp
     966             : 
     967             : 
     968           0 :   subroutine Get_NcVar_4d_dp(Filename, VarName, Dat, start, a_count, fid)
     969             :     !
     970             :     implicit none
     971             :     !
     972             :     integer, parameter :: idims = 4
     973             :     integer, parameter :: itype = 6 ! 5 = float, 6 = double
     974             :     !
     975             :     character(len = *), intent(in) :: Filename
     976             :     character(len = *), intent(in) :: VarName ! Variable name
     977             :     real(dp), dimension(:, :, :, :), allocatable, intent(inout) :: Dat    ! array where values should be stored
     978             :     integer(i4), dimension(:), optional, intent(in) :: start
     979             :     integer(i4), dimension(:), optional, intent(in) :: a_count
     980             :     integer(i4), optional, intent(in) :: fid
     981             :     !
     982             :     integer(i4), dimension(5) :: Rstart
     983             :     integer(i4), dimension(5) :: Rcount
     984             :     integer(i4) :: ncid    ! id of input stream
     985             :     integer(i4) :: varid   ! id of variable to be read
     986             :     integer(i4) :: vartype ! type of variable
     987             :     integer(i4) :: i
     988             :     !
     989             :     ! Defaults for Options Start and Count
     990           0 :     Rstart = 1
     991           0 :     Rcount = 1
     992             :     !
     993             :     ! allocate Dat
     994           0 :     if (.not. allocated(Dat)) then
     995           0 :       if (.not. present(a_count)) then
     996           0 :         Rcount = Get_NcDim(Filename, Varname)
     997             :       else
     998           0 :         Rcount(1 : idims) = a_count(1 : idims)
     999             :       end if
    1000           0 :       allocate(Dat(Rcount(1), Rcount(2), Rcount(3), Rcount(4)))
    1001             :     else
    1002           0 :       Rcount(1 : idims) = shape(Dat)
    1003             :     end if
    1004             :     !
    1005             :     ! Assign options Start and Count if present
    1006           0 :     if (present(start)) then
    1007           0 :       if (size(start) < size(shape(dat))) stop 'ERROR*** start has less values than data has dimensions. GetNcVar'
    1008           0 :       if (size(start) > 5) stop 'ERROR*** start has dimension greater than 5. GetNcVar'
    1009           0 :       Rstart(1 : size(start)) = start
    1010             :     end if
    1011             :     !
    1012           0 :     if (present(a_count)) then
    1013           0 :       if (size(a_count) < size(shape(dat))) stop 'ERROR*** a_count has less values than data has dimensions. GetNcVar'
    1014           0 :       if (size(a_count) > 5) stop 'ERROR*** a_count has dimension greater than 5. GetNcVar'
    1015           0 :       Rcount(1 : size(a_count)) = a_count
    1016           0 :       do i = 1, idims
    1017           0 :         if (size(Dat, i) < Rcount(i)) stop 'ERROR*** try to read more data in dimension than there is. Get_NcVar'
    1018             :       end do
    1019             :     end if
    1020             :     !
    1021             :     ! Open NetCDF filename
    1022           0 :     if (present(fid)) then
    1023           0 :       ncid = fid
    1024             :     else
    1025           0 :       call check(nf90_open(trim(Filename), NF90_NOWRITE, ncid))
    1026             :     end if
    1027             :     !
    1028             :     ! Inquire file, check if VarName exists and get the id
    1029           0 :     call Get_Info(Varname, ncid, varid, vartype)
    1030             :     ! check variable type ( 5 equals float type, 6 equals double )
    1031           0 :     if (vartype /= itype) then
    1032           0 :       print *, 'Variable name: ', trim(Varname)
    1033           0 :       print *, 'ERROR*** type of variable does not match argument type. subroutine Get_NcVar'
    1034           0 :       stop
    1035             :     end if
    1036             :     !
    1037             :     ! get values by varid
    1038           0 :     call check(nf90_get_var(ncid, varid, Dat, Rstart, Rcount))
    1039             :     !
    1040             :     ! close File
    1041           0 :     if (.not. present(fid)) call check(nf90_close(ncid))
    1042             :     !
    1043           0 :   end subroutine Get_NcVar_4d_dp
    1044             : 
    1045             : 
    1046           0 :   subroutine Get_NcVar_5d_sp(Filename, VarName, Dat, start, a_count, fid)
    1047             :     !
    1048             :     implicit none
    1049             :     !
    1050             :     integer, parameter :: idims = 5
    1051             :     integer, parameter :: itype = 5 ! 5 = float, 6 = double
    1052             :     !
    1053             :     character(len = *), intent(in) :: Filename
    1054             :     character(len = *), intent(in) :: VarName ! Variable name
    1055             :     real(sp), dimension(:, :, :, :, :), allocatable, intent(inout) :: Dat    ! array where values should be stored
    1056             :     integer(i4), dimension(:), optional, intent(in) :: start
    1057             :     integer(i4), dimension(:), optional, intent(in) :: a_count
    1058             :     integer(i4), optional, intent(in) :: fid
    1059             :     !
    1060             :     integer(i4), dimension(5) :: Rstart
    1061             :     integer(i4), dimension(5) :: Rcount
    1062             :     integer(i4) :: ncid    ! id of input stream
    1063             :     integer(i4) :: varid   ! id of variable to be read
    1064             :     integer(i4) :: vartype ! type of variable
    1065             :     integer(i4) :: i
    1066             :     !
    1067             :     ! Defaults for Options Start and Count
    1068           0 :     Rstart = 1
    1069           0 :     Rcount = 1
    1070             :     !
    1071             :     ! allocate Dat
    1072           0 :     if (.not. allocated(Dat)) then
    1073           0 :       if (.not. present(a_count)) then
    1074           0 :         Rcount = Get_NcDim(Filename, Varname)
    1075             :       else
    1076           0 :         Rcount(1 : idims) = a_count(1 : idims)
    1077             :       end if
    1078           0 :       allocate(Dat(Rcount(1), Rcount(2), Rcount(3), Rcount(4), Rcount(5)))
    1079             :     else
    1080           0 :       Rcount(1 : idims) = shape(Dat)
    1081             :     end if
    1082             :     !
    1083             :     ! Assign options Start and Count if present
    1084           0 :     if (present(start)) then
    1085           0 :       if (size(start) < size(shape(dat))) stop 'ERROR*** start has less values than data has dimensions. GetNcVar'
    1086           0 :       if (size(start) > 5) stop 'ERROR*** start has dimension greater than 5. GetNcVar'
    1087           0 :       Rstart(1 : size(start)) = start
    1088             :     end if
    1089             :     !
    1090           0 :     if (present(a_count)) then
    1091           0 :       if (size(a_count) < size(shape(dat))) stop 'ERROR*** a_count has less values than data has dimensions. GetNcVar'
    1092           0 :       if (size(a_count) > 5) stop 'ERROR*** a_count has dimension greater than 5. GetNcVar'
    1093           0 :       Rcount(1 : size(a_count)) = a_count
    1094           0 :       do i = 1, idims
    1095           0 :         if (size(Dat, i) < Rcount(i)) stop 'ERROR*** try to read more data in dimension than there is. Get_NcVar'
    1096             :       end do
    1097             :     end if
    1098             :     !
    1099             :     ! Open NetCDF filename
    1100           0 :     if (present(fid)) then
    1101           0 :       ncid = fid
    1102             :     else
    1103           0 :       call check(nf90_open(trim(Filename), NF90_NOWRITE, ncid))
    1104             :     end if
    1105             :     !
    1106             :     ! Inquire file, check if VarName exists and get the id
    1107           0 :     call Get_Info(Varname, ncid, varid, vartype)
    1108             :     ! check variable type ( 5 equals float type, 6 equals double )
    1109           0 :     if (vartype /= itype) then
    1110           0 :       print *, 'Variable name: ', trim(Varname)
    1111           0 :       print *, 'ERROR*** type of variable does not match argument type. subroutine Get_NcVar'
    1112           0 :       stop
    1113             :     end if
    1114             :     !
    1115             :     ! get values by varid
    1116           0 :     call check(nf90_get_var(ncid, varid, Dat, Rstart, Rcount))
    1117             :     !
    1118             :     ! close File
    1119           0 :     if (.not. present(fid)) call check(nf90_close(ncid))
    1120             :     !
    1121           0 :   end subroutine Get_NcVar_5d_sp
    1122             : 
    1123             : 
    1124           0 :   subroutine Get_NcVar_5d_dp(Filename, VarName, Dat, start, a_count, fid)
    1125             :     !
    1126             :     implicit none
    1127             :     !
    1128             :     integer, parameter :: idims = 5
    1129             :     integer, parameter :: itype = 6 ! 5 = float, 6 = double
    1130             :     !
    1131             :     character(len = *), intent(in) :: Filename
    1132             :     character(len = *), intent(in) :: VarName ! Variable name
    1133             :     real(dp), dimension(:, :, :, :, :), allocatable, intent(inout) :: Dat    ! array where values should be stored
    1134             :     integer(i4), dimension(:), optional, intent(in) :: start
    1135             :     integer(i4), dimension(:), optional, intent(in) :: a_count
    1136             :     integer(i4), optional, intent(in) :: fid
    1137             :     !
    1138             :     integer(i4), dimension(5) :: Rstart
    1139             :     integer(i4), dimension(5) :: Rcount
    1140             :     integer(i4) :: ncid    ! id of input stream
    1141             :     integer(i4) :: varid   ! id of variable to be read
    1142             :     integer(i4) :: vartype ! type of variable
    1143             :     integer(i4) :: i
    1144             :     !
    1145             :     ! Defaults for Options Start and Count
    1146           0 :     Rstart = 1
    1147           0 :     Rcount = 1
    1148             :     !
    1149             :     ! allocate Dat
    1150           0 :     if (.not. allocated(Dat)) then
    1151           0 :       if (.not. present(a_count)) then
    1152           0 :         Rcount = Get_NcDim(Filename, Varname)
    1153             :       else
    1154           0 :         Rcount(1 : idims) = a_count(1 : idims)
    1155             :       end if
    1156           0 :       allocate(Dat(Rcount(1), Rcount(2), Rcount(3), Rcount(4), Rcount(5)))
    1157             :     else
    1158           0 :       Rcount(1 : idims) = shape(Dat)
    1159             :     end if
    1160             :     !
    1161             :     ! Assign options Start and Count if present
    1162           0 :     if (present(start)) then
    1163           0 :       if (size(start) < size(shape(dat))) stop 'ERROR*** start has less values than data has dimensions. GetNcVar'
    1164           0 :       if (size(start) > 5) stop 'ERROR*** start has dimension greater than 5. GetNcVar'
    1165           0 :       Rstart(1 : size(start)) = start
    1166             :     end if
    1167             :     !
    1168           0 :     if (present(a_count)) then
    1169           0 :       if (size(a_count) < size(shape(dat))) stop 'ERROR*** a_count has less values than data has dimensions. GetNcVar'
    1170           0 :       if (size(a_count) > 5) stop 'ERROR*** a_count has dimension greater than 5. GetNcVar'
    1171           0 :       Rcount(1 : size(a_count)) = a_count
    1172           0 :       do i = 1, idims
    1173           0 :         if (size(Dat, i) < Rcount(i)) stop 'ERROR*** try to read more data in dimension than there is. Get_NcVar'
    1174             :       end do
    1175             :     end if
    1176             :     !
    1177             :     ! Open NetCDF filename
    1178           0 :     if (present(fid)) then
    1179           0 :       ncid = fid
    1180             :     else
    1181           0 :       call check(nf90_open(trim(Filename), NF90_NOWRITE, ncid))
    1182             :     end if
    1183             :     !
    1184             :     ! Inquire file, check if VarName exists and get the id
    1185           0 :     call Get_Info(Varname, ncid, varid, vartype)
    1186             :     ! check variable type ( 5 equals float type, 6 equals double )
    1187           0 :     if (vartype /= itype) then
    1188           0 :       print *, 'Variable name: ', trim(Varname)
    1189           0 :       print *, 'ERROR*** type of variable does not match argument type. subroutine Get_NcVar'
    1190           0 :       stop
    1191             :     end if
    1192             :     !
    1193             :     ! get values by varid
    1194           0 :     call check(nf90_get_var(ncid, varid, Dat, Rstart, Rcount))
    1195             :     !
    1196             :     ! close File
    1197           0 :     if (.not. present(fid)) call check(nf90_close(ncid))
    1198             :     !
    1199           0 :   end subroutine Get_NcVar_5d_dp
    1200             : 
    1201             : 
    1202           0 :   subroutine Get_NcVar_0d_i4(Filename, VarName, Dat, fid)
    1203             :     !
    1204             :     implicit none
    1205             :     !
    1206             :     integer, parameter :: itype = 4 ! 5 = float, 6 = double
    1207             :     !
    1208             :     character(len = *), intent(in) :: Filename
    1209             :     character(len = *), intent(in) :: VarName ! Variable name
    1210             :     integer(i4), intent(inout) :: Dat    ! array where values should be stored
    1211             :     integer(i4), optional, intent(in) :: fid
    1212             :     !
    1213             :     integer(i4) :: ncid    ! id of input stream
    1214             :     integer(i4) :: varid   ! id of variable to be read
    1215             :     integer(i4) :: vartype ! type of variable
    1216             :     !
    1217             :     ! Open NetCDF filename
    1218           0 :     if (present(fid)) then
    1219           0 :       ncid = fid
    1220             :     else
    1221           0 :       call check(nf90_open(trim(Filename), NF90_NOWRITE, ncid))
    1222             :     end if
    1223             :     !
    1224             :     ! Inquire file, check if VarName exists and get the id
    1225           0 :     call Get_Info(Varname, ncid, varid, vartype)
    1226             :     ! check variable type ( 5 equals float type, 6 equals double )
    1227           0 :     if (vartype /= itype) then
    1228           0 :       print *, 'Variable name: ', trim(Varname)
    1229           0 :       print *, 'ERROR*** type of variable does not match argument type. subroutine Get_NcVar'
    1230           0 :       stop
    1231             :     end if
    1232             :     !
    1233             :     ! get values by varid
    1234           0 :     call check(nf90_get_var(ncid, varid, Dat))
    1235             :     !
    1236             :     ! close File
    1237           0 :     if (.not. present(fid)) call check(nf90_close(ncid))
    1238             :     !
    1239           0 :   end subroutine Get_NcVar_0d_i4
    1240             : 
    1241             : 
    1242           0 :   subroutine Get_NcVar_1d_i4(Filename, VarName, Dat, start, a_count, fid)
    1243             :     !
    1244             :     implicit none
    1245             :     !
    1246             :     integer, parameter :: idims = 1
    1247             :     integer, parameter :: itype = 4 ! 3 = single 5 = float, 6 = double
    1248             :     !
    1249             :     character(len = *), intent(in) :: Filename
    1250             :     character(len = *), intent(in) :: VarName ! Variable name
    1251             :     integer(i4), dimension(:), allocatable, intent(inout) :: Dat    ! array where values should be stored
    1252             :     integer(i4), dimension(:), optional, intent(in) :: start
    1253             :     integer(i4), dimension(:), optional, intent(in) :: a_count
    1254             :     integer(i4), optional, intent(in) :: fid
    1255             :     !
    1256             :     integer(i4), dimension(5) :: Rstart
    1257             :     integer(i4), dimension(5) :: Rcount
    1258             :     integer(i4) :: ncid    ! id of input stream
    1259             :     integer(i4) :: varid   ! id of variable to be read
    1260             :     integer(i4) :: vartype ! type of variable
    1261             :     integer(i4) :: i
    1262             :     !
    1263             :     ! Defaults for Options Start and Count
    1264           0 :     Rstart = 1
    1265           0 :     Rcount = 1
    1266             :     !
    1267             :     ! allocate Dat
    1268           0 :     if (.not. allocated(Dat)) then
    1269           0 :       if (.not. present(a_count)) then
    1270           0 :         Rcount = Get_NcDim(Filename, Varname)
    1271             :       else
    1272           0 :         Rcount(1 : idims) = a_count(1 : idims)
    1273             :       end if
    1274           0 :       allocate(Dat(Rcount(1)))
    1275             :     else
    1276           0 :       Rcount(1 : idims) = shape(Dat)
    1277             :     end if
    1278             :     !
    1279             :     ! Assign options Start and Count if present
    1280           0 :     if (present(start)) then
    1281           0 :       if (size(start) /= idims) stop 'ERROR*** size of start does not equal dimensions of data. GetNcVar'
    1282           0 :       Rstart = start
    1283             :     end if
    1284             :     !
    1285           0 :     if (present(a_count)) then
    1286           0 :       if (size(a_count) /= idims) stop 'ERROR*** size of a_count does not equal dimensions of data. GetNcVar'
    1287           0 :       Rcount = a_count
    1288           0 :       do i = 1, idims
    1289           0 :         if (size(Dat, i) /= Rcount(i)) stop 'ERROR*** size mismatch. Get_NcVar'
    1290             :       end do
    1291             :     end if
    1292             :     !
    1293             :     ! Open NetCDF filename
    1294           0 :     if (present(fid)) then
    1295           0 :       ncid = fid
    1296             :     else
    1297           0 :       call check(nf90_open(trim(Filename), NF90_NOWRITE, ncid))
    1298             :     end if
    1299             :     !
    1300             :     ! Inquire file, check if VarName exists and get the id
    1301           0 :     call Get_Info(Varname, ncid, varid, vartype)
    1302             :     ! check variable type ( 5 equals float type, 6 equals double )
    1303           0 :     if (vartype /= itype) then
    1304           0 :       print *, 'Variable name: ', trim(Varname)
    1305           0 :       print *, 'ERROR*** type of variable does not match argument type. subroutine Get_NcVar'
    1306           0 :       stop
    1307             :     end if
    1308             :     !
    1309             :     ! get values by varid
    1310           0 :     call check(nf90_get_var(ncid, varid, Dat, Rstart, Rcount))
    1311             :     !
    1312             :     ! close File
    1313           0 :     if (.not. present(fid)) call check(nf90_close(ncid))
    1314             :     !
    1315           0 :   end subroutine Get_NcVar_1d_i4
    1316             : 
    1317             : 
    1318           0 :   subroutine Get_NcVar_2d_i4(Filename, VarName, Dat, start, a_count, fid)
    1319             :     !
    1320             :     implicit none
    1321             :     !
    1322             :     integer, parameter :: idims = 2
    1323             :     integer, parameter :: itype = 4 ! 3 = single, 5 = float, 6 = double
    1324             :     !
    1325             :     character(len = *), intent(in) :: Filename
    1326             :     character(len = *), intent(in) :: VarName ! Variable name
    1327             :     integer(i4), dimension(:, :), allocatable, intent(inout) :: Dat    ! array where values should be stored
    1328             :     integer(i4), dimension(:), optional, intent(in) :: start
    1329             :     integer(i4), dimension(:), optional, intent(in) :: a_count
    1330             :     integer(i4), optional, intent(in) :: fid
    1331             :     !
    1332             :     integer(i4), dimension(5) :: Rstart
    1333             :     integer(i4), dimension(5) :: Rcount
    1334             :     integer(i4) :: ncid    ! id of input stream
    1335             :     integer(i4) :: varid   ! id of variable to be read
    1336             :     integer(i4) :: vartype ! type of variable
    1337             :     integer(i4) :: i
    1338             :     !
    1339             :     ! Defaults for Options Start and Count
    1340           0 :     Rstart = 1
    1341           0 :     Rcount = 1
    1342             :     !
    1343             :     ! allocate Dat
    1344           0 :     if (.not. allocated(Dat)) then
    1345           0 :       if (.not. present(a_count)) then
    1346           0 :         Rcount = Get_NcDim(Filename, Varname)
    1347             :       else
    1348           0 :         Rcount(1 : idims) = a_count(1 : idims)
    1349             :       end if
    1350           0 :       allocate(Dat(Rcount(1), Rcount(2)))
    1351             :     else
    1352           0 :       Rcount(1 : idims) = shape(Dat)
    1353             :     end if
    1354             :     !
    1355             :     ! Assign options Start and Count if present
    1356           0 :     if (present(start)) then
    1357           0 :       if (size(start) /= idims) stop 'ERROR*** size of start does not equal dimensions of data. GetNcVar'
    1358           0 :       Rstart = start
    1359             :     end if
    1360             :     !
    1361           0 :     if (present(a_count)) then
    1362           0 :       if (size(a_count) /= idims) stop 'ERROR*** size of a_count does not equal dimensions of data. GetNcVar'
    1363           0 :       Rcount = a_count
    1364           0 :       do i = 1, idims
    1365           0 :         if (size(Dat, i) /= Rcount(i)) stop 'ERROR*** size mismatch. Get_NcVar'
    1366             :       end do
    1367             :     end if
    1368             :     !
    1369             :     ! Open NetCDF filename
    1370           0 :     if (present(fid)) then
    1371           0 :       ncid = fid
    1372             :     else
    1373           0 :       call check(nf90_open(trim(Filename), NF90_NOWRITE, ncid))
    1374             :     end if
    1375             :     !
    1376             :     ! Inquire file, check if VarName exists and get the id
    1377           0 :     call Get_Info(Varname, ncid, varid, vartype)
    1378             :     ! check variable type ( 5 equals float type, 6 equals double )
    1379           0 :     if (vartype /= itype) then
    1380           0 :       print *, 'Variable name: ', trim(Varname)
    1381           0 :       print *, 'ERROR*** type of variable does not match argument type. subroutine Get_NcVar'
    1382           0 :       stop
    1383             :     end if
    1384             :     !
    1385             :     ! get values by varid
    1386           0 :     call check(nf90_get_var(ncid, varid, Dat, Rstart, Rcount))
    1387             :     !
    1388             :     ! close File
    1389           0 :     if (.not. present(fid)) call check(nf90_close(ncid))
    1390             :     !
    1391           0 :   end subroutine Get_NcVar_2d_i4
    1392             : 
    1393             : 
    1394           0 :   subroutine Get_NcVar_3d_i4(Filename, VarName, Dat, start, a_count, fid)
    1395             :     !
    1396             :     implicit none
    1397             :     !
    1398             :     integer, parameter :: idims = 3
    1399             :     integer, parameter :: itype = 4 ! 3 = single, 5 = float, 6 = double
    1400             :     !
    1401             :     character(len = *), intent(in) :: Filename
    1402             :     character(len = *), intent(in) :: VarName ! Variable name
    1403             :     integer(i4), dimension(:, :, :), allocatable, intent(inout) :: Dat    ! array where values should be stored
    1404             :     integer(i4), dimension(:), optional, intent(in) :: start
    1405             :     integer(i4), dimension(:), optional, intent(in) :: a_count
    1406             :     integer(i4), optional, intent(in) :: fid
    1407             :     !
    1408             :     integer(i4), dimension(5) :: Rstart
    1409             :     integer(i4), dimension(5) :: Rcount
    1410             :     integer(i4) :: ncid    ! id of input stream
    1411             :     integer(i4) :: varid   ! id of variable to be read
    1412             :     integer(i4) :: vartype ! type of variable
    1413             :     integer(i4) :: i
    1414             :     !
    1415             :     ! Defaults for Options Start and Count
    1416           0 :     Rstart = 1
    1417           0 :     Rcount = 1
    1418             :     !
    1419             :     ! allocate Dat
    1420           0 :     if (.not. allocated(Dat)) then
    1421           0 :       if (.not. present(a_count)) then
    1422           0 :         Rcount = Get_NcDim(Filename, Varname)
    1423             :       else
    1424           0 :         Rcount(1 : idims) = a_count(1 : idims)
    1425             :       end if
    1426           0 :       allocate(Dat(Rcount(1), Rcount(2), Rcount(3)))
    1427             :     else
    1428           0 :       Rcount(1 : idims) = shape(Dat)
    1429             :     end if
    1430             :     !
    1431             :     ! Assign options Start and Count if present
    1432           0 :     if (present(start)) then
    1433           0 :       if (size(start) /= idims) stop 'ERROR*** size of start does not equal dimensions of data. GetNcVar'
    1434           0 :       Rstart = start
    1435             :     end if
    1436             :     !
    1437           0 :     if (present(a_count)) then
    1438           0 :       if (size(a_count) /= idims) stop 'ERROR*** size of a_count does not equal dimensions of data. GetNcVar'
    1439           0 :       Rcount = a_count
    1440           0 :       do i = 1, idims
    1441           0 :         if (size(Dat, i) /= Rcount(i)) stop 'ERROR*** size mismatch. Get_NcVar'
    1442             :       end do
    1443             :     end if
    1444             :     !
    1445             :     ! Open NetCDF filename
    1446           0 :     if (present(fid)) then
    1447           0 :       ncid = fid
    1448             :     else
    1449           0 :       call check(nf90_open(trim(Filename), NF90_NOWRITE, ncid))
    1450             :     end if
    1451             :     !
    1452             :     ! Inquire file, check if VarName exists and get the id
    1453           0 :     call Get_Info(Varname, ncid, varid, vartype)
    1454             :     ! check variable type ( 5 equals float type, 6 equals double )
    1455           0 :     if (vartype /= itype) then
    1456           0 :       print *, 'Variable name: ', trim(Varname)
    1457           0 :       print *, 'ERROR*** type of variable does not match argument type. subroutine Get_NcVar'
    1458           0 :       stop
    1459             :     end if
    1460             :     !
    1461             :     ! get values by varid
    1462           0 :     call check(nf90_get_var(ncid, varid, Dat, Rstart, Rcount))
    1463             :     !
    1464             :     ! close File
    1465           0 :     if (.not. present(fid)) call check(nf90_close(ncid))
    1466             :     !
    1467           0 :   end subroutine Get_NcVar_3d_i4
    1468             : 
    1469             : 
    1470           0 :   subroutine Get_NcVar_4d_i4(Filename, VarName, Dat, start, a_count, fid)
    1471             :     !
    1472             :     implicit none
    1473             :     !
    1474             :     integer, parameter :: idims = 4
    1475             :     integer, parameter :: itype = 4 ! 3 = single, 5 = float, 6 = double
    1476             :     !
    1477             :     character(len = *), intent(in) :: Filename
    1478             :     character(len = *), intent(in) :: VarName ! Variable name
    1479             :     integer(i4), dimension(:, :, :, :), allocatable, intent(inout) :: Dat    ! array where values should be stored
    1480             :     integer(i4), dimension(:), optional, intent(in) :: start
    1481             :     integer(i4), dimension(:), optional, intent(in) :: a_count
    1482             :     integer(i4), optional, intent(in) :: fid
    1483             :     !
    1484             :     integer(i4), dimension(5) :: Rstart
    1485             :     integer(i4), dimension(5) :: Rcount
    1486             :     integer(i4) :: ncid    ! id of input stream
    1487             :     integer(i4) :: varid   ! id of variable to be read
    1488             :     integer(i4) :: vartype ! type of variable
    1489             :     integer(i4) :: i
    1490             :     !
    1491             :     ! Defaults for Options Start and Count
    1492           0 :     Rstart = 1
    1493           0 :     Rcount = 1
    1494             :     !
    1495             :     ! allocate Dat
    1496           0 :     if (.not. allocated(Dat)) then
    1497           0 :       if (.not. present(a_count)) then
    1498           0 :         Rcount = Get_NcDim(Filename, Varname)
    1499             :       else
    1500           0 :         Rcount(1 : idims) = a_count(1 : idims)
    1501             :       end if
    1502           0 :       allocate(Dat(Rcount(1), Rcount(2), Rcount(3), Rcount(4)))
    1503             :     else
    1504           0 :       Rcount(1 : idims) = shape(Dat)
    1505             :     end if
    1506             :     !
    1507             :     ! Assign options Start and Count if present
    1508           0 :     if (present(start)) then
    1509           0 :       if (size(start) /= idims) stop 'ERROR*** size of start does not equal dimensions of data. GetNcVar'
    1510           0 :       Rstart = start
    1511             :     end if
    1512             :     !
    1513           0 :     if (present(a_count)) then
    1514           0 :       if (size(a_count) /= idims) stop 'ERROR*** size of a_count does not equal dimensions of data. GetNcVar'
    1515           0 :       Rcount = a_count
    1516           0 :       do i = 1, idims
    1517           0 :         if (size(Dat, i) /= Rcount(i)) stop 'ERROR*** size mismatch. Get_NcVar'
    1518             :       end do
    1519             :     end if
    1520             :     !
    1521             :     ! Open NetCDF filename
    1522           0 :     if (present(fid)) then
    1523           0 :       ncid = fid
    1524             :     else
    1525           0 :       call check(nf90_open(trim(Filename), NF90_NOWRITE, ncid))
    1526             :     end if
    1527             :     !
    1528             :     ! Inquire file, check if VarName exists and get the id
    1529           0 :     call Get_Info(Varname, ncid, varid, vartype)
    1530             :     ! check variable type ( 5 equals float type, 6 equals double )
    1531           0 :     if (vartype /= itype) then
    1532           0 :       print *, 'Variable name: ', trim(Varname)
    1533           0 :       print *, 'ERROR*** type of variable does not match argument type. subroutine Get_NcVar'
    1534           0 :       stop
    1535             :     end if
    1536             :     !
    1537             :     ! get values by varid
    1538           0 :     call check(nf90_get_var(ncid, varid, Dat, Rstart, Rcount))
    1539             :     !
    1540             :     ! close File
    1541           0 :     if (.not. present(fid)) call check(nf90_close(ncid))
    1542             :     !
    1543           0 :   end subroutine Get_NcVar_4d_i4
    1544             : 
    1545             : 
    1546           0 :   subroutine Get_NcVar_5d_i4(Filename, VarName, Dat, start, a_count, fid)
    1547             :     !
    1548             :     implicit none
    1549             :     !
    1550             :     integer, parameter :: idims = 5
    1551             :     integer, parameter :: itype = 4 ! 3 = single, 5 = float, 6 = double
    1552             :     !
    1553             :     character(len = *), intent(in) :: Filename
    1554             :     character(len = *), intent(in) :: VarName ! Variable name
    1555             :     integer(i4), dimension(:, :, :, :, :), allocatable, intent(inout) :: Dat    ! array where values should be stored
    1556             :     integer(i4), dimension(:), optional, intent(in) :: start
    1557             :     integer(i4), dimension(:), optional, intent(in) :: a_count
    1558             :     integer(i4), optional, intent(in) :: fid
    1559             :     !
    1560             :     integer(i4), dimension(5) :: Rstart
    1561             :     integer(i4), dimension(5) :: Rcount
    1562             :     integer(i4) :: ncid    ! id of input stream
    1563             :     integer(i4) :: varid   ! id of variable to be read
    1564             :     integer(i4) :: vartype ! type of variable
    1565             :     integer(i4) :: i
    1566             :     !
    1567             :     ! Defaults for Options Start and Count
    1568           0 :     Rstart = 1
    1569           0 :     Rcount = 1
    1570             :     !
    1571             :     ! allocate Dat
    1572           0 :     if (.not. allocated(Dat)) then
    1573           0 :       if (.not. present(a_count)) then
    1574           0 :         Rcount = Get_NcDim(Filename, Varname)
    1575             :       else
    1576           0 :         Rcount(1 : idims) = a_count(1 : idims)
    1577             :       end if
    1578           0 :       allocate(Dat(Rcount(1), Rcount(2), Rcount(3), Rcount(4), Rcount(5)))
    1579             :     else
    1580           0 :       Rcount(1 : idims) = shape(Dat)
    1581             :     end if
    1582             :     !
    1583             :     ! Assign options Start and Count if present
    1584           0 :     if (present(start)) then
    1585           0 :       if (size(start) /= idims) stop 'ERROR*** size of start does not equal dimensions of data. GetNcVar'
    1586           0 :       Rstart = start
    1587             :     end if
    1588             :     !
    1589           0 :     if (present(a_count)) then
    1590           0 :       if (size(a_count) /= idims) stop 'ERROR*** size of a_count does not equal dimensions of data. GetNcVar'
    1591           0 :       Rcount = a_count
    1592           0 :       do i = 1, idims
    1593           0 :         if (size(Dat, i) /= Rcount(i)) stop 'ERROR*** size mismatch. Get_NcVar'
    1594             :       end do
    1595             :     end if
    1596             :     !
    1597             :     ! Open NetCDF filename
    1598           0 :     if (present(fid)) then
    1599           0 :       ncid = fid
    1600             :     else
    1601           0 :       call check(nf90_open(trim(Filename), NF90_NOWRITE, ncid))
    1602             :     end if
    1603             :     !
    1604             :     ! Inquire file, check if VarName exists and get the id
    1605           0 :     call Get_Info(Varname, ncid, varid, vartype)
    1606             :     ! check variable type ( 5 equals float type, 6 equals double )
    1607           0 :     if (vartype /= itype) then
    1608           0 :       print *, 'Variable name: ', trim(Varname)
    1609           0 :       print *, 'ERROR*** type of variable does not match argument type. subroutine Get_NcVar'
    1610           0 :       stop
    1611             :     end if
    1612             :     !
    1613             :     ! get values by varid
    1614           0 :     call check(nf90_get_var(ncid, varid, Dat, Rstart, Rcount))
    1615             :     !
    1616             :     ! close File
    1617           0 :     if (.not. present(fid)) call check(nf90_close(ncid))
    1618             :     !
    1619           0 :   end subroutine Get_NcVar_5d_i4
    1620             : 
    1621           0 :   subroutine Get_NcVar_0d_i1(Filename, VarName, Dat, fid)
    1622             :     !
    1623             :     implicit none
    1624             :     !
    1625             :     integer, parameter :: itype = 1 ! 5 = float, 6 = double
    1626             :     !
    1627             :     character(len = *), intent(in) :: Filename
    1628             :     character(len = *), intent(in) :: VarName ! Variable name
    1629             :     integer(1), intent(inout) :: Dat    ! array where values should be stored
    1630             :     integer(i4), optional, intent(in) :: fid
    1631             :     !
    1632             :     integer(i4) :: ncid    ! id of input stream
    1633             :     integer(i4) :: varid   ! id of variable to be read
    1634             :     integer(i4) :: vartype ! type of variable
    1635             :     !
    1636             :     ! Open NetCDF filename
    1637           0 :     if (present(fid)) then
    1638           0 :       ncid = fid
    1639             :     else
    1640           0 :       call check(nf90_open(trim(Filename), NF90_NOWRITE, ncid))
    1641             :     end if
    1642             :     !
    1643             :     ! Inquire file, check if VarName exists and get the id
    1644           0 :     call Get_Info(Varname, ncid, varid, vartype)
    1645             :     ! check variable type ( 5 equals float type, 6 equals double )
    1646           0 :     if (vartype /= itype) then
    1647           0 :       print *, 'Variable name: ', trim(Varname)
    1648           0 :       print *, 'ERROR*** type of variable does not match argument type. subroutine Get_NcVar'
    1649           0 :       stop
    1650             :     end if
    1651             :     !
    1652             :     ! get values by varid
    1653           0 :     call check(nf90_get_var(ncid, varid, Dat))
    1654             :     !
    1655             :     ! close File
    1656           0 :     if (.not. present(fid)) call check(nf90_close(ncid))
    1657             :     !
    1658           0 :   end subroutine Get_NcVar_0d_i1
    1659             : 
    1660             : 
    1661           0 :   subroutine Get_NcVar_1d_i1(Filename, VarName, Dat, start, a_count, fid)
    1662             :     !
    1663             :     implicit none
    1664             :     !
    1665             :     integer, parameter :: idims = 1
    1666             :     integer, parameter :: itype = 1 ! 3 = single 5 = float, 6 = double
    1667             :     !
    1668             :     character(len = *), intent(in) :: Filename
    1669             :     character(len = *), intent(in) :: VarName ! Variable name
    1670             :     integer(1), dimension(:), allocatable, intent(inout) :: Dat    ! array where values should be stored
    1671             :     integer(i4), dimension(:), optional, intent(in) :: start
    1672             :     integer(i4), dimension(:), optional, intent(in) :: a_count
    1673             :     integer(i4), optional, intent(in) :: fid
    1674             :     !
    1675             :     integer(i4), dimension(5) :: Rstart
    1676             :     integer(i4), dimension(5) :: Rcount
    1677             :     integer(i4) :: ncid    ! id of input stream
    1678             :     integer(i4) :: varid   ! id of variable to be read
    1679             :     integer(i4) :: vartype ! type of variable
    1680             :     integer(i4) :: i
    1681             :     !
    1682             :     ! Defaults for Options Start and Count
    1683           0 :     Rstart = 1
    1684           0 :     Rcount = 1
    1685             :     !
    1686             :     ! allocate Dat
    1687           0 :     if (.not. allocated(Dat)) then
    1688           0 :       if (.not. present(a_count)) then
    1689           0 :         Rcount = Get_NcDim(Filename, Varname)
    1690             :       else
    1691           0 :         Rcount(1 : idims) = a_count(1 : idims)
    1692             :       end if
    1693           0 :       allocate(Dat(Rcount(1)))
    1694             :     else
    1695           0 :       Rcount(1 : idims) = shape(Dat)
    1696             :     end if
    1697             :     !
    1698             :     ! Assign options Start and Count if present
    1699           0 :     if (present(start)) then
    1700           0 :       if (size(start) /= idims) stop 'ERROR*** size of start does not equal dimensions of data. GetNcVar'
    1701           0 :       Rstart = start
    1702             :     end if
    1703             :     !
    1704           0 :     if (present(a_count)) then
    1705           0 :       if (size(a_count) /= idims) stop 'ERROR*** size of a_count does not equal dimensions of data. GetNcVar'
    1706           0 :       Rcount = a_count
    1707           0 :       do i = 1, idims
    1708           0 :         if (size(Dat, i) /= Rcount(i)) stop 'ERROR*** size mismatch. Get_NcVar'
    1709             :       end do
    1710             :     end if
    1711             :     !
    1712             :     ! Open NetCDF filename
    1713           0 :     if (present(fid)) then
    1714           0 :       ncid = fid
    1715             :     else
    1716           0 :       call check(nf90_open(trim(Filename), NF90_NOWRITE, ncid))
    1717             :     end if
    1718             :     !
    1719             :     ! Inquire file, check if VarName exists and get the id
    1720           0 :     call Get_Info(Varname, ncid, varid, vartype)
    1721             :     ! check variable type ( 5 equals float type, 6 equals double )
    1722           0 :     if (vartype /= itype) then
    1723           0 :       print *, 'Variable name: ', trim(Varname)
    1724           0 :       print *, 'ERROR*** type of variable does not match argument type. subroutine Get_NcVar'
    1725           0 :       stop
    1726             :     end if
    1727             :     !
    1728             :     ! get values by varid
    1729           0 :     call check(nf90_get_var(ncid, varid, Dat, Rstart, Rcount))
    1730             :     !
    1731             :     ! close File
    1732           0 :     if (.not. present(fid)) call check(nf90_close(ncid))
    1733             :     !
    1734           0 :   end subroutine Get_NcVar_1d_i1
    1735             : 
    1736             : 
    1737           0 :   subroutine Get_NcVar_2d_i1(Filename, VarName, Dat, start, a_count, fid)
    1738             :     !
    1739             :     implicit none
    1740             :     !
    1741             :     integer, parameter :: idims = 2
    1742             :     integer, parameter :: itype = 1 ! 1 = 1 byte, 3 = single, 5 = float, 6 = double
    1743             :     !
    1744             :     character(len = *), intent(in) :: Filename
    1745             :     character(len = *), intent(in) :: VarName ! Variable name
    1746             :     integer(1), dimension(:, :), allocatable, intent(inout) :: Dat    ! array where values should be stored
    1747             :     integer(i4), dimension(:), optional, intent(in) :: start
    1748             :     integer(i4), dimension(:), optional, intent(in) :: a_count
    1749             :     integer(i4), optional, intent(in) :: fid
    1750             :     !
    1751             :     integer(i4), dimension(5) :: Rstart
    1752             :     integer(i4), dimension(5) :: Rcount
    1753             :     integer(i4) :: ncid    ! id of input stream
    1754             :     integer(i4) :: varid   ! id of variable to be read
    1755             :     integer(i4) :: vartype ! type of variable
    1756             :     integer(i4) :: i
    1757             :     !
    1758             :     ! Defaults for Options Start and Count
    1759           0 :     Rstart = 1
    1760           0 :     Rcount = 1
    1761             :     !
    1762             :     ! allocate Dat
    1763           0 :     if (.not. allocated(Dat)) then
    1764           0 :       if (.not. present(a_count)) then
    1765           0 :         Rcount = Get_NcDim(Filename, Varname)
    1766             :       else
    1767           0 :         Rcount(1 : idims) = a_count(1 : idims)
    1768             :       end if
    1769           0 :       allocate(Dat(Rcount(1), Rcount(2)))
    1770             :     else
    1771           0 :       Rcount(1 : idims) = shape(Dat)
    1772             :     end if
    1773             :     !
    1774             :     ! Assign options Start and Count if present
    1775           0 :     if (present(start)) then
    1776           0 :       if (size(start) /= idims) stop 'ERROR*** size of start does not equal dimensions of data. GetNcVar'
    1777           0 :       Rstart = start
    1778             :     end if
    1779             :     !
    1780           0 :     if (present(a_count)) then
    1781           0 :       if (size(a_count) /= idims) stop 'ERROR*** size of a_count does not equal dimensions of data. GetNcVar'
    1782           0 :       Rcount = a_count
    1783           0 :       do i = 1, idims
    1784           0 :         if (size(Dat, i) /= Rcount(i)) stop 'ERROR*** size mismatch. Get_NcVar'
    1785             :       end do
    1786             :     end if
    1787             :     !
    1788             :     ! Open NetCDF filename
    1789           0 :     if (present(fid)) then
    1790           0 :       ncid = fid
    1791             :     else
    1792           0 :       call check(nf90_open(trim(Filename), NF90_NOWRITE, ncid))
    1793             :     end if
    1794             :     !
    1795             :     ! Inquire file, check if VarName exists and get the id
    1796           0 :     call Get_Info(Varname, ncid, varid, vartype)
    1797             :     ! check variable type ( 5 equals float type, 6 equals double )
    1798           0 :     if (vartype /= itype) then
    1799           0 :       print *, 'Variable name: ', trim(Varname)
    1800           0 :       print *, 'ERROR*** type of variable does not match argument type. subroutine Get_NcVar'
    1801           0 :       stop
    1802             :     end if
    1803             :     !
    1804             :     ! get values by varid
    1805           0 :     call check(nf90_get_var(ncid, varid, Dat, Rstart, Rcount))
    1806             :     !
    1807             :     ! close File
    1808           0 :     if (.not. present(fid)) call check(nf90_close(ncid))
    1809             :     !
    1810           0 :   end subroutine Get_NcVar_2d_i1
    1811             : 
    1812             : 
    1813           0 :   subroutine Get_NcVar_3d_i1(Filename, VarName, Dat, start, a_count, fid)
    1814             :     !
    1815             :     implicit none
    1816             :     !
    1817             :     integer, parameter :: idims = 3
    1818             :     integer, parameter :: itype = 1 ! 3 = single, 5 = float, 6 = double
    1819             :     !
    1820             :     character(len = *), intent(in) :: Filename
    1821             :     character(len = *), intent(in) :: VarName ! Variable name
    1822             :     integer(1), dimension(:, :, :), allocatable, intent(inout) :: Dat    ! array where values should be stored
    1823             :     integer(i4), dimension(:), optional, intent(in) :: start
    1824             :     integer(i4), dimension(:), optional, intent(in) :: a_count
    1825             :     integer(i4), optional, intent(in) :: fid
    1826             :     !
    1827             :     integer(i4), dimension(5) :: Rstart
    1828             :     integer(i4), dimension(5) :: Rcount
    1829             :     integer(i4) :: ncid    ! id of input stream
    1830             :     integer(i4) :: varid   ! id of variable to be read
    1831             :     integer(i4) :: vartype ! type of variable
    1832             :     integer(i4) :: i
    1833             :     !
    1834             :     ! Defaults for Options Start and Count
    1835           0 :     Rstart = 1
    1836           0 :     Rcount = 1
    1837             :     !
    1838             :     ! allocate Dat
    1839           0 :     if (.not. allocated(Dat)) then
    1840           0 :       if (.not. present(a_count)) then
    1841           0 :         Rcount = Get_NcDim(Filename, Varname)
    1842             :       else
    1843           0 :         Rcount(1 : idims) = a_count(1 : idims)
    1844             :       end if
    1845           0 :       allocate(Dat(Rcount(1), Rcount(2), Rcount(3)))
    1846             :     else
    1847           0 :       Rcount(1 : idims) = shape(Dat)
    1848             :     end if
    1849             :     !
    1850             :     ! Assign options Start and Count if present
    1851           0 :     if (present(start)) then
    1852           0 :       if (size(start) /= idims) stop 'ERROR*** size of start does not equal dimensions of data. GetNcVar'
    1853           0 :       Rstart = start
    1854             :     end if
    1855             :     !
    1856           0 :     if (present(a_count)) then
    1857           0 :       if (size(a_count) /= idims) stop 'ERROR*** size of a_count does not equal dimensions of data. GetNcVar'
    1858           0 :       Rcount = a_count
    1859           0 :       do i = 1, idims
    1860           0 :         if (size(Dat, i) /= Rcount(i)) stop 'ERROR*** size mismatch. Get_NcVar'
    1861             :       end do
    1862             :     end if
    1863             :     !
    1864             :     ! Open NetCDF filename
    1865           0 :     if (present(fid)) then
    1866           0 :       ncid = fid
    1867             :     else
    1868           0 :       call check(nf90_open(trim(Filename), NF90_NOWRITE, ncid))
    1869             :     end if
    1870             :     !
    1871             :     ! Inquire file, check if VarName exists and get the id
    1872           0 :     call Get_Info(Varname, ncid, varid, vartype)
    1873             :     ! check variable type ( 5 equals float type, 6 equals double )
    1874           0 :     if (vartype /= itype) then
    1875           0 :       print *, 'Variable name: ', trim(Varname)
    1876           0 :       print *, 'ERROR*** type of variable does not match argument type. subroutine Get_NcVar'
    1877           0 :       stop
    1878             :     end if
    1879             :     !
    1880             :     ! get values by varid
    1881           0 :     call check(nf90_get_var(ncid, varid, Dat, Rstart, Rcount))
    1882             :     !
    1883             :     ! close File
    1884           0 :     if (.not. present(fid)) call check(nf90_close(ncid))
    1885             :     !
    1886           0 :   end subroutine Get_NcVar_3d_i1
    1887             : 
    1888             : 
    1889           0 :   subroutine Get_NcVar_4d_i1(Filename, VarName, Dat, start, a_count, fid)
    1890             :     !
    1891             :     implicit none
    1892             :     !
    1893             :     integer, parameter :: idims = 4
    1894             :     integer, parameter :: itype = 1 ! 1 = 1 byte, 3 = single, 5 = float, 6 = double
    1895             :     !
    1896             :     character(len = *), intent(in) :: Filename
    1897             :     character(len = *), intent(in) :: VarName ! Variable name
    1898             :     integer(1), dimension(:, :, :, :), allocatable, intent(inout) :: Dat    ! array where values should be stored
    1899             :     integer(i4), dimension(:), optional, intent(in) :: start
    1900             :     integer(i4), dimension(:), optional, intent(in) :: a_count
    1901             :     integer(i4), optional, intent(in) :: fid
    1902             :     !
    1903             :     integer(i4), dimension(5) :: Rstart
    1904             :     integer(i4), dimension(5) :: Rcount
    1905             :     integer(i4) :: ncid    ! id of input stream
    1906             :     integer(i4) :: varid   ! id of variable to be read
    1907             :     integer(i4) :: vartype ! type of variable
    1908             :     integer(i4) :: i
    1909             :     !
    1910             :     ! Defaults for Options Start and Count
    1911           0 :     Rstart = 1
    1912           0 :     Rcount = 1
    1913             :     !
    1914             :     ! allocate Dat
    1915           0 :     if (.not. allocated(Dat)) then
    1916           0 :       if (.not. present(a_count)) then
    1917           0 :         Rcount = Get_NcDim(Filename, Varname)
    1918             :       else
    1919           0 :         Rcount(1 : idims) = a_count(1 : idims)
    1920             :       end if
    1921           0 :       allocate(Dat(Rcount(1), Rcount(2), Rcount(3), Rcount(4)))
    1922             :     else
    1923           0 :       Rcount(1 : idims) = shape(Dat)
    1924             :     end if
    1925             :     !
    1926             :     ! Assign options Start and Count if present
    1927           0 :     if (present(start)) then
    1928           0 :       if (size(start) /= idims) stop 'ERROR*** size of start does not equal dimensions of data. GetNcVar'
    1929           0 :       Rstart = start
    1930             :     end if
    1931             :     !
    1932           0 :     if (present(a_count)) then
    1933           0 :       if (size(a_count) /= idims) stop 'ERROR*** size of a_count does not equal dimensions of data. GetNcVar'
    1934           0 :       Rcount = a_count
    1935           0 :       do i = 1, idims
    1936           0 :         if (size(Dat, i) /= Rcount(i)) stop 'ERROR*** size mismatch. Get_NcVar'
    1937             :       end do
    1938             :     end if
    1939             :     !
    1940             :     ! Open NetCDF filename
    1941           0 :     if (present(fid)) then
    1942           0 :       ncid = fid
    1943             :     else
    1944           0 :       call check(nf90_open(trim(Filename), NF90_NOWRITE, ncid))
    1945             :     end if
    1946             :     !
    1947             :     ! Inquire file, check if VarName exists and get the id
    1948           0 :     call Get_Info(Varname, ncid, varid, vartype)
    1949             :     ! check variable type ( 5 equals float type, 6 equals double )
    1950           0 :     if (vartype /= itype) then
    1951           0 :       print *, 'Variable name: ', trim(Varname)
    1952           0 :       print *, 'ERROR*** type of variable does not match argument type. subroutine Get_NcVar'
    1953           0 :       stop
    1954             :     end if
    1955             :     !
    1956             :     ! get values by varid
    1957           0 :     call check(nf90_get_var(ncid, varid, Dat, Rstart, Rcount))
    1958             :     !
    1959             :     ! close File
    1960           0 :     if (.not. present(fid)) call check(nf90_close(ncid))
    1961             :     !
    1962           0 :   end subroutine Get_NcVar_4d_i1
    1963             : 
    1964             : 
    1965           0 :   subroutine Get_NcVar_5d_i1(Filename, VarName, Dat, start, a_count, fid)
    1966             :     !
    1967             :     implicit none
    1968             :     !
    1969             :     integer, parameter :: idims = 5
    1970             :     integer, parameter :: itype = 1 ! 1 = 1 byte, 3 = single, 5 = float, 6 = double
    1971             :     !
    1972             :     character(len = *), intent(in) :: Filename
    1973             :     character(len = *), intent(in) :: VarName ! Variable name
    1974             :     integer(1), dimension(:, :, :, :, :), allocatable, intent(inout) :: Dat    ! array where values should be stored
    1975             :     integer(i4), dimension(:), optional, intent(in) :: start
    1976             :     integer(i4), dimension(:), optional, intent(in) :: a_count
    1977             :     integer(i4), optional, intent(in) :: fid
    1978             :     !
    1979             :     integer(i4), dimension(5) :: Rstart
    1980             :     integer(i4), dimension(5) :: Rcount
    1981             :     integer(i4) :: ncid    ! id of input stream
    1982             :     integer(i4) :: varid   ! id of variable to be read
    1983             :     integer(i4) :: vartype ! type of variable
    1984             :     integer(i4) :: i
    1985             :     !
    1986             :     ! Defaults for Options Start and Count
    1987           0 :     Rstart = 1
    1988           0 :     Rcount = 1
    1989             :     !
    1990             :     ! allocate Dat
    1991           0 :     if (.not. allocated(Dat)) then
    1992           0 :       if (.not. present(a_count)) then
    1993           0 :         Rcount = Get_NcDim(Filename, Varname)
    1994             :       else
    1995           0 :         Rcount(1 : idims) = a_count(1 : idims)
    1996             :       end if
    1997           0 :       allocate(Dat(Rcount(1), Rcount(2), Rcount(3), Rcount(4), Rcount(5)))
    1998             :     else
    1999           0 :       Rcount(1 : idims) = shape(Dat)
    2000             :     end if
    2001             :     !
    2002             :     ! Assign options Start and Count if present
    2003           0 :     if (present(start)) then
    2004           0 :       if (size(start) /= idims) stop 'ERROR*** size of start does not equal dimensions of data. GetNcVar'
    2005           0 :       Rstart = start
    2006             :     end if
    2007             :     !
    2008           0 :     if (present(a_count)) then
    2009           0 :       if (size(a_count) /= idims) stop 'ERROR*** size of a_count does not equal dimensions of data. GetNcVar'
    2010           0 :       Rcount = a_count
    2011           0 :       do i = 1, idims
    2012           0 :         if (size(Dat, i) /= Rcount(i)) stop 'ERROR*** size mismatch. Get_NcVar'
    2013             :       end do
    2014             :     end if
    2015             :     !
    2016             :     ! Open NetCDF filename
    2017           0 :     if (present(fid)) then
    2018           0 :       ncid = fid
    2019             :     else
    2020           0 :       call check(nf90_open(trim(Filename), NF90_NOWRITE, ncid))
    2021             :     end if
    2022             :     !
    2023             :     ! Inquire file, check if VarName exists and get the id
    2024           0 :     call Get_Info(Varname, ncid, varid, vartype)
    2025             :     ! check variable type ( 5 equals float type, 6 equals double )
    2026           0 :     if (vartype /= itype) then
    2027           0 :       print *, 'Variable name: ', trim(Varname)
    2028           0 :       print *, 'ERROR*** type of variable does not match argument type. subroutine Get_NcVar'
    2029           0 :       stop
    2030             :     end if
    2031             :     !
    2032             :     ! get values by varid
    2033           0 :     call check(nf90_get_var(ncid, varid, Dat, Rstart, Rcount))
    2034             :     !
    2035             :     ! close File
    2036           0 :     if (.not. present(fid)) call check(nf90_close(ncid))
    2037             :     !
    2038           0 :   end subroutine Get_NcVar_5d_i1
    2039             :   ! ------------------------------------------------------------------------------
    2040             : 
    2041             :   !>    \brief Open netcdf file
    2042             : 
    2043             :   !>    \details Opens a netcdf file and returns file handle
    2044             :   !!
    2045             :   !!    \b Example
    2046             :   !!
    2047             :   !!    \code{.f90}
    2048             :   !!    id = NcOpen(Fname)
    2049             :   !!    \endcode
    2050             :   !!
    2051             :   !!    \b Literature
    2052             :   !!
    2053             :   !!    1. http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-f90.html
    2054             : 
    2055             :   !>    \param[in] "character(len=*) :: Fname"    Filename of file to open
    2056             :   !>    \param[out] "integer(i4)      :: NcOpen"  id of opened stream
    2057             : 
    2058             :   !>    \author Stephan Thober,
    2059             :   !>    \date May 2012
    2060             : 
    2061           0 :   function NcOpen(Fname)
    2062             :     !
    2063             :     implicit none
    2064             :     !
    2065             :     character(len = *), intent(in) :: Fname
    2066             :     integer(i4) :: NcOpen
    2067             :     !
    2068           0 :     call check(nf90_open(trim(Fname), NF90_NOWRITE, NcOpen))
    2069             :     !
    2070           0 :   end function NcOpen
    2071             : 
    2072             :   ! ------------------------------------------------------------------------------
    2073             : 
    2074             :   !>    \brief Closes netcdf file
    2075             : 
    2076             :   !>    \details Closes a netcdf file by file handle
    2077             :   !!
    2078             :   !!    \b Example
    2079             :   !!
    2080             :   !!    \code{.f90}
    2081             :   !!    call NcClose(ncid)
    2082             :   !!    \endcode
    2083             :   !!
    2084             :   !!    \b Literature
    2085             :   !!
    2086             :   !!    1. http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-f90.html
    2087             : 
    2088             :   !>    \param[in] "ncid" - file handle of open netcdf file
    2089             : 
    2090             :   !>    \author Stephan Thober
    2091             :   !>    \date May 2012
    2092             : 
    2093           0 :   subroutine NcClose(ncid)
    2094             :     !
    2095             :     implicit none
    2096             :     !
    2097             :     integer(i4), intent(in) :: ncid
    2098             :     !
    2099           0 :     call check(nf90_close(ncid))
    2100             :     !
    2101           0 :   end subroutine NcClose
    2102             : 
    2103             :   ! ------------------------------------------------------------------------------
    2104             :   !>    \brief Get variable info from netcdf files
    2105             : 
    2106             :   !>    \details This subroutine is PRIVATE and therefore does not exist outside of this module.
    2107             :   !!
    2108             :   !!    This subroutine inquires the nc file. Given the Variable name and the stream
    2109             :   !!    of the nc file, this subroutine determines the variable id, the kind of the
    2110             :   !!    variable and the length of the dimensions in the file.
    2111             :   !!
    2112             :   !!    See: http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-f90/ -> NF90-INQUIRE
    2113             :   !!    for detailed information.
    2114             : 
    2115             :   !>    \author Matthias Cuntz,
    2116             :   !>    \date Nov 2012
    2117             :   !!      - default dimension length -1
    2118             : 
    2119           0 :   subroutine Get_Info(Varname, ncid, varid, xtype, dl, Info, ndims)
    2120             :     !
    2121             :     implicit none
    2122             :     !
    2123             :     character(len = *), intent(in) :: Varname
    2124             :     integer(i4), intent(in) :: ncid
    2125             :     integer(i4), intent(out) :: varid    ! variable id of data to be read
    2126             :     integer(i4), intent(out) :: xtype    ! type of the variable
    2127             :     integer(i4), dimension(:), optional, intent(inout) :: dl
    2128             :     logical, optional, intent(in) :: Info
    2129             :     integer(i4), optional, intent(out) :: ndims    ! Number of Dimensions for specific variable
    2130             :     !
    2131           0 :     integer(i4), dimension(:), allocatable :: DimID   ! Id of dimension
    2132             :     character(NF90_MAX_NAME) :: name    ! name of Variables in the file
    2133             :     integer(i4) :: NumDims ! Number of Dimensions for specific variable
    2134             :     integer(i4) :: n       ! loop index
    2135             :     character(256) :: form    ! output format
    2136             :     integer(i4) :: itmp
    2137             :     !
    2138           0 :     call check(nf90_inq_varid(ncid, Varname, varid))
    2139           0 :     call check(nf90_inquire_variable(ncid, varid, ndims = NumDims))
    2140           0 :     if (present(ndims)) ndims = NumDims
    2141             :     !
    2142             :     ! get the dimension Ids and the data type of the variable
    2143           0 :     allocate(DimId(NumDims))
    2144           0 :     call check(nf90_inquire_variable(ncid, varid, xtype = xtype, dimids = DimId))
    2145             :     !
    2146           0 :     if (present(dl)) then
    2147             :       ! check consistency of dimensions
    2148           0 :       if (NumDims > size(dl)) &
    2149           0 :               stop 'ERROR*** Dimension size of Variable is greater than dims of array. subroutine Get_Info'
    2150             :       ! go through dimension ids and get its length
    2151           0 :       dl(:) = -1 ! initialise
    2152           0 :       dimloop : do n = 1, NumDims
    2153           0 :         call check(nf90_inquire_dimension(ncid, DimId(n), name, itmp))
    2154           0 :         dl(n) = itmp
    2155           0 :         if (present(info)) then
    2156           0 :           if (info) then
    2157           0 :             write(form, '(a12,I03.3,a1)') "(a10,i1,a4,a", len(trim(name)), ")"
    2158           0 :             write(*, form) 'Dimension ', n, ' is ', trim(name)
    2159           0 :             write(*, '(a14,i5.5)') 'The Length is ', dl(n)
    2160             :           end if
    2161             :         end if
    2162             :       end do dimloop
    2163             :       !
    2164             :     end if
    2165             :     !
    2166           0 :   end subroutine Get_Info
    2167             : 
    2168             :   ! -----------------------------------------------------------------------------
    2169             :   !  private error checking routine
    2170           1 :   subroutine check(status)
    2171             :     !
    2172             :     implicit none
    2173             :     !
    2174             :     integer(i4), intent(in) :: status
    2175             :     !
    2176           1 :     if (status /= nf90_noerr) then
    2177           1 :       write(*, *) trim(nf90_strerror(status))
    2178           1 :       stop
    2179             :     end if
    2180             :     !
    2181           0 :   end subroutine check
    2182             : 
    2183             : end module mo_NcRead

Generated by: LCOV version 1.16