-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnetcdf_dim.f90
100 lines (90 loc) · 3.3 KB
/
netcdf_dim.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
! Copyright (C) 2013 David Dickinson
! This program is free software: you can redistribute it and/or modify
! it under the terms of the GNU General Public License as published by
! the Free Software Foundation, either version 3 of the License, or
! (at your option) any later version.
! This program is distributed in the hope that it will be useful,
! but WITHOUT ANY WARRANTY; without even the implied warranty of
! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
! GNU General Public License for more details.
! You should have received a copy of the GNU General Public License
! along with this program. If not, see <http://www.gnu.org/licenses/>.
!TODO:
! Error checking
! Preprocessor directives
module netcdf_dim
!<DD> A module containing the netcdf dimension type definition
!and associated routines.
!</DD>
use netcdf, only: NF90_UNLIMITED
implicit none
public :: ncdf_dim_type, dim_max_name_len, dim_complex_name
private
integer,parameter :: dim_max_name_len=6 !Longest name allowed
character(len=2),parameter :: dim_complex_name='ri' !What do we call the
!special dimension used to hold real and imaginary components?
type :: ncdf_dim_type
integer :: id !The id to use in calls to netcdf routines
integer :: size !The dimension size
character(len=dim_max_name_len) :: name
contains
private
procedure,public :: init !Basic initialisation
procedure,public :: free !Free up memory
procedure,public :: is_unlimited !Is this an unlimited dimension?
procedure,public :: get_size !How big is this dimension
end type ncdf_dim_type
contains
!>Initialise the netcdf dimension object
subroutine init(self,name,size,unlimited)
implicit none
class(ncdf_dim_type), intent(inout) :: self
character(len=*), intent(in) :: name
integer,intent(in),optional :: size
logical,intent(in),optional :: unlimited
logical :: size_set
!Initialise variables
self%name=trim(name)
size_set=.false.
!Set the size, if we specify unlimited
!then we disregard the size variable
if(present(unlimited))then
if(unlimited) then
self%size=NF90_UNLIMITED
size_set=.true.
endif
endif
if(.not.size_set)then
if(present(size))then
self%size=size
size_set=.true.
else
!Perhaps we should stop here, at the very least print an error
print*,"Error: Attempted to make a dimension without a size, setting to unlimited but could cause problems."
self%size=NF90_UNLIMITED
endif
endif
end subroutine init
!Free objects
subroutine free(self)
implicit none
class(ncdf_dim_type), intent(inout) :: self
!Nothing to do for this object at the moment
end subroutine free
!Check if this is an unlimited dimension
function is_unlimited(self)
implicit none
class(ncdf_dim_type), intent(in) :: self
logical :: is_unlimited
is_unlimited=(self%size.eq.NF90_UNLIMITED)
end function is_unlimited
!Check dimension length
function get_size(self,ncid)
use netcdf, only: nf90_inquire_dimension
implicit none
class(ncdf_dim_type), intent(in) :: self
integer, intent(in) :: ncid
integer :: get_size,status
status=nf90_inquire_dimension(ncid,self%id,len=get_size)
end function get_size
end module netcdf_dim