-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod_nc.F
executable file
·346 lines (295 loc) · 12 KB
/
mod_nc.F
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
module mod_nc
use mod_xc ! HYCOM communication interface
use mod_za ! HYCOM I/O interface
use mod_cb_arrays ! HYCOM saved arrays
implicit none
include 'netcdf.inc'
!--- Module to output Netcdf variable (debug purposes) ---
contains
subroutine ncput_2d(var_nc2d,var_name,file_name)
use mod_xc ! HYCOM communication interface
use mod_cb_arrays ! HYCOM saved arrays
!--- To output 2d variables
implicit none
include 'netcdf.inc'
real*8, dimension(1-nbdy:idm+nbdy,1-nbdy:jdm+nbdy )::
& var_nc2d
character(len=*) :: file_name,var_name
CHARACTER*(NF_MAX_NAME) recname
integer :: ncid,i,j,nrec
integer , parameter :: ndims=3
integer , parameter :: nrecs=5
character(len=*), parameter :: lvl_name = 'layer'
character(len=*), parameter :: lat_name = 'latitude'
character(len=*), parameter :: lon_name = 'longitude'
character(len=*), parameter :: rec_name = 'time'
character(len=*), parameter :: units = 'units'
character(len=*), parameter :: lat_units = 'degrees_north'
character(len=*), parameter :: lon_units = 'degrees_east'
character(len=*), parameter :: time_units = 'step'
integer :: lvl_dimid, lon_dimid, lat_dimid, rec_dimid,
& lon_varid, lat_varid, var_varid, retval,rec_varid
integer, dimension(ndims) :: dimids, start, count
integer, dimension(2) :: cdimids
real*8 :: date
logical :: wcoord
! --------------------------------------------------------------
! OPEN NETCDF FILE
! --------------------------------------------------------------
if (mnproc.eq.1) then
!--- Create the file
retval = nf_create(file_name, nf_noclobber, ncid)
!--- Check if file exists or not
if (retval .eq. nf_eexist) then
retval = nf_open(file_name, nf_write, ncid)
retval = nf_inq_varid(ncid,var_name,var_varid)
if (retval .ne. nf_noerr) call handle_err(retval)
wcoord = .false.
else
!--- Define the dimensions
retval = nf_def_dim(ncid,lat_name,jtdm,lat_dimid)
if (retval .ne. nf_noerr) call handle_err(retval)
retval = nf_def_dim(ncid,lon_name,itdm,lon_dimid)
if (retval .ne. nf_noerr) call handle_err(retval)
retval = nf_def_dim(ncid,rec_name,nf_unlimited,rec_dimid)
if (retval .ne. nf_noerr) call handle_err(retval)
!--- Define the coordinate variables
cdimids(1)=lon_dimid
cdimids(2)=lat_dimid
! cdimids(3)=rec_dimid
retval = nf_def_var(ncid, lat_name, nf_real, 2, cdimids,
& lat_varid)
if (retval .ne. nf_noerr) call handle_err(retval)
retval = nf_def_var(ncid, lon_name, nf_real, 2, cdimids,
& lon_varid)
if (retval .ne. nf_noerr) call handle_err(retval)
!--- Assign units attributes to coordinate variables
retval = nf_put_att_text(ncid, lat_varid, units,
& len(lat_units), lat_units)
if (retval .ne. nf_noerr) call handle_err(retval)
retval = nf_put_att_text(ncid, lon_varid, units,
& len(lon_units), lon_units)
!--- Assign dimensions of the netCDF variables
dimids(1) = lon_dimid
dimids(2) = lat_dimid
dimids(3) = rec_dimid
!--- Define the netCDF variables var_nc2d.
retval = nf_def_var(ncid, var_name,nf_double,ndims,dimids,
& var_varid)
if (retval .ne. nf_noerr) call handle_err(retval)
!--- End define mode
retval = nf_enddef(ncid)
if (retval .ne. nf_noerr) call handle_err(retval)
wcoord = .true.
endif ! .nf_eexist.
! --- check time status
retval = nf_inq_dimid (ncid, rec_name, rec_varid)
if (retval .ne. nf_noerr) call handle_err(retval)
retval = nf_inq_dimlen(ncid, rec_varid, nrec)
if (retval .ne. nf_noerr) call handle_err(retval)
endif ! mnproc.eq.1
!--- Write the coordinate lon/lat
do j=1,jj
do i=1,ii
util1(i,j) = plat(i,j)
enddo
enddo
call xcaget(worknc1, util1, 1) !! get value from all procs
if (mnproc.eq.1) then
if (wcoord) then
retval = nf_put_var_double(ncid, lat_varid, worknc1)
if (retval .ne. nf_noerr) call handle_err(retval)
endif ! wcoord
endif ! mnproc
do j=1,jj
do i=1,ii
util1(i,j) = plon(i,j)
enddo
enddo
call xcaget(worknc1, util1, 1) !! get value from all procs
if (mnproc.eq.1) then
if (wcoord) then
retval = nf_put_var_double(ncid, lon_varid, worknc1)
if (retval .ne. nf_noerr) call handle_err(retval)
endif !wcoord
endif ! mnproc
!--- Write one timestep of data
if (mnproc.eq.1) then
count(1) = itdm
count(2) = jtdm
count(3) = 1
start(1) = 1
start(2) = 1
start(3) = nrec+1
endif ! mnproc
!--- Write var_nc2d
do j=1,jj
do i=1,ii
util1(i,j) = var_nc2d(i,j)
enddo
enddo
call xcaget(worknc1, util1, 1) !! get value from all procs
if (mnproc.eq.1) then
retval = nf_put_vara_double(ncid, var_varid, start, count,
& worknc1(1:itdm,1:jtdm))
if (retval .ne. nf_noerr) call handle_err(retval)
endif !mnproc
!--- Close the file
if (mnproc.eq.1) then
retval = nf_close(ncid)
if (retval .ne. nf_noerr) call handle_err(retval)
endif ! mnproc
end subroutine ncput_2d
subroutine ncput_3d(var_nc3d,var_name,file_name)
use mod_xc ! HYCOM communication interface
use mod_cb_arrays ! HYCOM saved arrays
!--- To output 3d variables
implicit none
include 'netcdf.inc'
real*8,dimension(1-nbdy:idm+nbdy,1-nbdy:jdm+nbdy,kdm),intent(in)::
& var_nc3d
character(len=*), intent(in) :: file_name,var_name
CHARACTER*(NF_MAX_NAME) recname
integer :: ncid,i,j,k,nrec
integer , parameter :: ndims=4
integer , parameter :: nrecs=5
character(len=*), parameter :: lvl_name = 'layer'
character(len=*), parameter :: lat_name = 'latitude'
character(len=*), parameter :: lon_name = 'longitude'
character(len=*), parameter :: rec_name = 'time'
character(len=*), parameter :: units = 'units'
character(len=*), parameter :: lat_units = 'degrees_north'
character(len=*), parameter :: lon_units = 'degrees_east'
integer :: lvl_dimid, lon_dimid, lat_dimid, rec_dimid,
& lon_varid, lat_varid, var_varid, retval
integer, dimension(ndims) :: dimids, start, count,rec_varid
integer, dimension(2) :: cdimids
logical :: wcoord
! --------------------------------------------------------------
! OPEN NETCDF FILE
! --------------------------------------------------------------
if (mnproc.eq.1) then
!--- Create the file
retval = nf_create(file_name, nf_noclobber, ncid)
!--- Check if file exists or not
if (retval .eq. nf_eexist) then
retval = nf_open(file_name, nf_write, ncid)
retval = nf_inq_varid(ncid,var_name,var_varid)
if (retval .ne. nf_noerr) call handle_err(retval)
wcoord = .false.
else
!--- Check if file exists or not
! if (retval .eq. nf_eexist) then
! retval= nf_open(file_name, nf_write, ncid)
! else
!--- Define the dimensions
retval = nf_def_dim(ncid,lvl_name,kdm ,lvl_dimid)
if (retval .ne. nf_noerr) call handle_err(retval)
retval = nf_def_dim(ncid,lat_name,jtdm,lat_dimid)
if (retval .ne. nf_noerr) call handle_err(retval)
retval = nf_def_dim(ncid,lon_name,itdm,lon_dimid)
if (retval .ne. nf_noerr) call handle_err(retval)
retval = nf_def_dim(ncid,rec_name,nf_unlimited,rec_dimid)
if (retval .ne. nf_noerr) call handle_err(retval)
!--- Define the coordinate variables
cdimids(1)=lon_dimid
cdimids(2)=lat_dimid
retval = nf_def_var(ncid, lat_name, nf_real, 2, cdimids,
& lat_varid)
if (retval .ne. nf_noerr) call handle_err(retval)
retval = nf_def_var(ncid, lon_name, nf_real, 2, cdimids,
& lon_varid)
if (retval .ne. nf_noerr) call handle_err(retval)
!--- Assign units attributes to coordinate variables
retval = nf_put_att_text(ncid, lat_varid, units,
& len(lat_units), lat_units)
if (retval .ne. nf_noerr) call handle_err(retval)
retval = nf_put_att_text(ncid, lon_varid, units,
& len(lon_units), lon_units)
if (retval .ne. nf_noerr) call handle_err(retval)
!--- Assign dimensions of the netCDF variables
dimids(1) = lon_dimid
dimids(2) = lat_dimid
dimids(3) = lvl_dimid
dimids(4) = rec_dimid
!--- Define the netCDF variables var_nc2d.
retval = nf_def_var(ncid,var_name,nf_double,ndims,dimids,
& var_varid)
if (retval .ne. nf_noerr) call handle_err(retval)
!--- End define mode
retval = nf_enddef(ncid)
if (retval .ne. nf_noerr) call handle_err(retval)
wcoord = .true.
endif ! .nf_exist.
! --- check time status
retval = nf_inq_dimid (ncid, rec_name, rec_varid)
if (retval .ne. nf_noerr) call handle_err(retval)
retval = nf_inq_dimlen(ncid, rec_varid, nrec)
if (retval .ne. nf_noerr) call handle_err(retval)
endif ! mnproc.eq.1
!--- Write the coordinate lon/lat
do j=1,jj
do i=1,ii
util1(i,j) = plat(i,j)
enddo
enddo
call xcaget(worknc1, util1, 1) !! get value from all procs
if (mnproc.eq.1) then
if (wcoord) then
retval = nf_put_var_double(ncid, lat_varid, worknc1)
if (retval .ne. nf_noerr) call handle_err(retval)
endif ! wcoord
endif ! mnproc
do j=1,jj
do i=1,ii
util1(i,j) = plon(i,j)
enddo
enddo
call xcaget(worknc1, util1, 1) !! get value from all procs
if (mnproc.eq.1) then
if (wcoord) then
retval = nf_put_var_double(ncid, lon_varid, worknc1)
if (retval .ne. nf_noerr) call handle_err(retval)
endif ! wcoord
endif ! mnproc
!--- Write one timestep of data
if (mnproc.eq.1) then
count(1) = itdm
count(2) = jtdm
count(3) = kdm
count(4) = 1
start(1) = 1
start(2) = 1
start(3) = 1
start(4) = nrec+1
endif ! mnproc
!--- Write var_nc3d
do k=1,kdm
do j=1,jj
do i=1,ii
util1(i,j) = var_nc3d(i,j,k)
enddo
enddo
call xcaget(worknc1, util1, 1) !! get value from all procs
worknc2(:,:,k)=worknc1
enddo
if (mnproc.eq.1) then
retval = nf_put_vara_double(ncid, var_varid, start, count,
& worknc2(1:itdm,1:jtdm,1:kdm))
if (retval .ne. nf_noerr) call handle_err(retval)
endif ! mnproc
!--- Close the file
if (mnproc.eq.1) then
retval = nf_close(ncid)
if (retval .ne. nf_noerr) call handle_err(retval)
endif ! mnproc
end subroutine ncput_3d
subroutine handle_err(errcode)
implicit none
!--- To handle errors in the creation and filling of the netcdf
include 'netcdf.inc'
integer errcode
print *, 'Error: ', nf_strerror(errcode)
stop 2
end subroutine handle_err
end module mod_nc