-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnetcdf_var.f90
115 lines (102 loc) · 3.44 KB
/
netcdf_var.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
! 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:
! Attributes
! Error checking
! Preprocessor directives
module netcdf_var
!<DD> A module containing the netcdf variable type definition
!and associated routines.
!</DD>
use netcdf_dim, only: dim_max_name_len
use nc_utils, only: kind_nf
implicit none
public :: ncdf_var_type, var_max_name_len
private
integer,parameter :: var_max_name_len=200 !Longest name allowed
type :: ncdf_var_type
!Could have an array of attributes here
integer :: id !The id to use in calls to netcdf routines
integer(kind_nf) :: type !The variable type
character(len=var_max_name_len) :: name
character(len=dim_max_name_len),dimension(:),allocatable :: dim_names
!Meta data
integer :: ndim
logical :: is_complex
contains
private
procedure,public :: init !Basic initialisation
procedure,public :: free !Free up memory
end type ncdf_var_type
contains
!>Initialise the netcdf variable object
subroutine init(self,name,dims,type_int,type_str,complx)
use nc_utils, only : Type_Str_To_Int
use netcdf_dim, only: dim_complex_name
implicit none
class(ncdf_var_type), intent(inout) :: self
character(len=*), intent(in) :: name
character(len=*),dimension(:),intent(in) :: dims
integer,intent(in),optional :: type_int
character(len=*),intent(in),optional :: type_str
logical, intent(in), optional :: complx
logical :: my_complex
integer :: tmp,ndim,i,ct
!Initialise the complex state
my_complex=.false.
if(present(complx)) my_complex=complx
!Set the data type
if(present(type_int))then
self%type=type_int
elseif(present(type_str))then
self%type=Type_Str_To_Int(type_str,my_complex)
else
self%type=-10
endif
self%is_complex=my_complex
!Count not empty dimensions
ndim=0
tmp=size(dims)
do i=1,tmp
if(trim(dims(i)).ne.'') ndim=ndim+1
enddo
!If we're complex then we need to add in a new dimension to
!represent the two components as netcdf doesn't have a native
!complex data type
if(self%is_complex) ndim=ndim+1
!Initialise variables
self%name=trim(name)
self%ndim=ndim
!If we have dimensions then save the names
if(ndim.gt.0)then
allocate(self%dim_names(ndim))
ct=0
!Add the complex dimension if required
if(self%is_complex) then
ct=ct+1
self%dim_names(ct)=dim_complex_name
endif
do i=1,tmp
if(trim(dims(i)).ne.'')then
ct=ct+1
self%dim_names(ct)=trim(dims(i))
endif
enddo
endif
end subroutine init
!Free objects
subroutine free(self)
implicit none
class(ncdf_var_type), intent(inout) :: self
if(allocated(self%dim_names)) deallocate(self%dim_names)
end subroutine free
end module netcdf_var