-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod_netCDF_IO.f90
executable file
·235 lines (177 loc) · 11.4 KB
/
mod_netCDF_IO.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
!------------------------------------------------------------------------------!
! !
! PROGRAM : mod_netCDF_IO.f90 !
! !
! PURPOSE : Read and write the netCDF files !
! !
! 2019.02.21.K.Noh !
! !
!------------------------------------------------------------------------------!
MODULE mod_netCDF_IO
USE netcdf
IMPLICIT NONE
INTEGER :: N1, N2, N3, N
INTEGER :: varid, ncid, id1, id2, id3, dim1, dim2, dim3
REAL(KIND=8) :: missing
CHARACTER(LEN=128) :: path_name, file_name, dir_name, var_name
CHARACTER(LEN=128) :: dim1_name, dim2_name, dim3_name
CHARACTER(LEN=128) :: dim1_unit, dim2_unit, dim3_unit
SAVE
CONTAINS
!------------------------------------------------------------------------------!
! !
! SUBROUTINE : netCDF_setup !
! !
! PURPOSE : Initial setup for reading/writing netCDF files !
! !
! 2019.02.21.K.Noh !
! !
!------------------------------------------------------------------------------!
SUBROUTINE netCDF_setup
IMPLICIT NONE
dir_name = "DATA/OISST_v2"
END SUBROUTINE netCDF_setup
!------------------------------------------------------------------------------!
! !
! SUBROUTINE : netCDF_read_1d !
! !
! PURPOSE : Reading the netCDF files !
! !
! 2019.02.21.K.Noh !
! !
!------------------------------------------------------------------------------!
SUBROUTINE netCDF_read_1d(data_input,ind_str)
IMPLICIT NONE
INTEGER,INTENT(IN) :: ind_str
REAL(KIND=8),INTENT(INOUT) :: data_input(1:N)
path_name = "./"//TRIM(dir_name)//"/"//TRIM(file_name)
!< Open the file, NF90_NOWRITE : read-only access to files
CALL CHECK( NF90_OPEN(path_name, NF90_NOWRITE, ncid) )
!< Get the varid of the data variable, based on its name
CALL CHECK( NF90_INQ_VARID(ncid, TRIM(var_name), varid) )
!< Read the data
CALL CHECK( NF90_GET_VAR(ncid, varid, data_input, &
start = (/ind_str/)) )
!< Close the file
CALL CHECK( NF90_CLOSE(ncid) )
END SUBROUTINE netCDF_read_1d
!------------------------------------------------------------------------------!
! !
! SUBROUTINE : netCDF_read_3d !
! !
! PURPOSE : Reading the netCDF files !
! !
! 2019.02.21.K.Noh !
! !
!------------------------------------------------------------------------------!
SUBROUTINE netCDF_read_3d(data_input, ind_str1, ind_str2, ind_str3)
IMPLICIT NONE
INTEGER,INTENT(IN) :: ind_str1, ind_str2, ind_str3
REAL(KIND=8),INTENT(INOUT) :: data_input(1:N1,1:N2,1:N3)
path_name = "./"//TRIM(dir_name)//"/"//TRIM(file_name)
!< Open the file, NF90_NOWRITE : read-only access to files
CALL CHECK( NF90_OPEN(path_name, NF90_NOWRITE, ncid) )
!< Get the varid of the data variable, based on its name
CALL CHECK( NF90_INQ_VARID(ncid, TRIM(var_name), varid) )
!< Read the data
CALL CHECK( NF90_GET_VAR(ncid, varid, data_input, &
start = (/ind_str1, ind_str2, ind_str3/)) )
!< Close the file
CALL CHECK( NF90_CLOSE(ncid) )
END SUBROUTINE netCDF_read_3d
!------------------------------------------------------------------------------!
! !
! SUBROUTINE : netCDF_write_1d !
! !
! PURPOSE : Writing the 1D netCDF files !
! !
! 2019.05.13.K.Noh !
! !
!------------------------------------------------------------------------------!
SUBROUTINE netCDF_write_1d(data_output,data_dim3)
IMPLICIT NONE
REAL(KIND=8),INTENT(IN) :: data_dim3(1:N3)
REAL(KIND=8),INTENT(IN) :: data_output(1:N3)
path_name = "./"//TRIM(dir_name)//"/"//TRIM(file_name)
!<Open the output file
CALL CHECK( NF90_CREATE(path_name, NF90_SHARE, ncid) )
!<Define dimensions
CALL CHECK( NF90_DEF_DIM(ncid, TRIM(dim3_name), N3, dim3) )
!<Define the coordinate variables & output variable
CALL CHECK( NF90_DEF_VAR(ncid,TRIM(dim3_name),NF90_DOUBLE,dim3,id3))
CALL CHECK( NF90_DEF_VAR(ncid, var_name, NF90_DOUBLE, &
(/dim3/), varid) )
!<Assign missing values
CALL CHECK( NF90_PUT_ATT(ncid,varid,'_FillValue', missing) )
!<Add dimension's units
CALL CHECK( NF90_PUT_ATT(ncid,id3,"units",TRIM(dim3_unit)) )
!<End define mode
CALL CHECK( NF90_ENDDEF(ncid) )
!<Write the coordinate variable & output variable
CALL CHECK( NF90_PUT_VAR(ncid, id3, data_dim3) )
CALL CHECK( NF90_PUT_VAR(ncid, varid, data_output) )
!<Close the file
CALL CHECK( NF90_CLOSE(ncid) )
END SUBROUTINE netCDF_write_1d
!------------------------------------------------------------------------------!
! !
! SUBROUTINE : netCDF_write_3d !
! !
! PURPOSE : Writing the 3D netCDF files !
! !
! 2019.02.21.K.Noh !
! !
!------------------------------------------------------------------------------!
SUBROUTINE netCDF_write_3d(data_output,data_dim1,data_dim2,data_dim3)
IMPLICIT NONE
REAL(KIND=8),INTENT(IN) :: data_dim1(1:N1)
REAL(KIND=8),INTENT(IN) :: data_dim2(1:N2)
REAL(KIND=8),INTENT(IN) :: data_dim3(1:N3)
REAL(KIND=8),INTENT(IN) :: data_output(1:N1,1:N2,1:N3)
path_name = "./"//TRIM(dir_name)//"/"//TRIM(file_name)
!<Open the output file
CALL CHECK( NF90_CREATE(path_name, NF90_SHARE, ncid) )
!<Define dimensions
CALL CHECK( NF90_DEF_DIM(ncid, TRIM(dim1_name), N1, dim1) )
CALL CHECK( NF90_DEF_DIM(ncid, TRIM(dim2_name), N2, dim2) )
CALL CHECK( NF90_DEF_DIM(ncid, TRIM(dim3_name), N3, dim3) )
!<Define the coordinate variables & output variable
CALL CHECK( NF90_DEF_VAR(ncid,TRIM(dim1_name),NF90_DOUBLE,dim1,id1))
CALL CHECK( NF90_DEF_VAR(ncid,TRIM(dim2_name),NF90_DOUBLE,dim2,id2))
CALL CHECK( NF90_DEF_VAR(ncid,TRIM(dim3_name),NF90_DOUBLE,dim3,id3))
CALL CHECK( NF90_DEF_VAR(ncid, var_name, NF90_DOUBLE, &
(/dim1,dim2,dim3/), varid) )
!<Assign missing values
CALL CHECK( NF90_PUT_ATT(ncid,varid,'_FillValue', missing) )
!<Add dimension's units
CALL CHECK( NF90_PUT_ATT(ncid,id1,"units",TRIM(dim1_unit)) )
CALL CHECK( NF90_PUT_ATT(ncid,id2,"units",TRIM(dim2_unit)) )
CALL CHECK( NF90_PUT_ATT(ncid,id3,"units",TRIM(dim3_unit)) )
!<End define mode
CALL CHECK( NF90_ENDDEF(ncid) )
!<Write the coordinate variable & output variable
CALL CHECK( NF90_PUT_VAR(ncid, id1, data_dim1) )
CALL CHECK( NF90_PUT_VAR(ncid, id2, data_dim2) )
CALL CHECK( NF90_PUT_VAR(ncid, id3, data_dim3) )
CALL CHECK( NF90_PUT_VAR(ncid, varid, data_output) )
!<Close the file
CALL CHECK( NF90_CLOSE(ncid) )
END SUBROUTINE netCDF_write_3d
!------------------------------------------------------------------------------!
! !
! SUBROUTINE : CHECK !
! !
! PURPOSE : Checking up the netcdf module's function & subroutine !
! !
! 2019.02.21.K.Noh !
! !
!------------------------------------------------------------------------------!
SUBROUTINE CHECK(status)
IMPLICIT NONE
INTEGER,INTENT(IN) :: status
IF (status /= NF90_NOERR) THEN
WRITE(*,*) TRIM(NF90_STRERROR(status))
STOP "STOPPED"
END IF
END SUBROUTINE CHECK
END MODULE mod_netCDF_IO