Skip to content

Commit 84177e8

Browse files
committed
Add netcdf debugging utilities
1 parent d744c2f commit 84177e8

2 files changed

Lines changed: 203 additions & 0 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ else()
246246
src/offline/casa_cable.F90
247247
src/offline/cbl_model_driver_offline.F90
248248
src/offline/landuse_inout.F90
249+
src/offline/netcdf_utils.F90
249250
src/offline/spincasacnp.F90
250251
src/util/cable_climate_type_mod.F90
251252
src/util/masks_cbl.F90

src/offline/netcdf_utils.F90

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
! CSIRO Open Source Software License Agreement (variation of the BSD / MIT License)
2+
! Copyright (c) 2015, Commonwealth Scientific and Industrial Research Organisation
3+
! (CSIRO) ABN 41 687 119 230.
4+
5+
MODULE netcdf_utils
6+
!! Module for common NetCDF utility procedures.
7+
USE netcdf
8+
USE iso_fortran_env, ONLY: int32, real32, real64
9+
10+
IMPLICIT NONE
11+
12+
PRIVATE
13+
14+
PUBLIC :: to_netcdf
15+
16+
INTERFACE to_netcdf
17+
!* Overloads for the `to_netcdf` subroutine.
18+
!
19+
! Usage:
20+
! ```fortran
21+
! CALL to_netcdf("data.nc", values)
22+
! ```
23+
! where `values` is an array and its type is supported by the following overloads.
24+
MODULE PROCEDURE to_netcdf_int32_1d, to_netcdf_int32_2d, to_netcdf_int32_3d
25+
MODULE PROCEDURE to_netcdf_real32_1d, to_netcdf_real32_2d, to_netcdf_real32_3d
26+
MODULE PROCEDURE to_netcdf_real64_1d, to_netcdf_real64_2d, to_netcdf_real64_3d
27+
END INTERFACE to_netcdf
28+
29+
CONTAINS
30+
31+
SUBROUTINE check(status)
32+
!! Check NetCDF error status.
33+
INTEGER, INTENT(IN) :: status !! Error status
34+
IF (status /= NF90_NOERR) THEN
35+
PRINT *, trim(nf90_strerror(status))
36+
STOP
37+
END IF
38+
END SUBROUTINE check
39+
40+
SUBROUTINE to_netcdf_int32_1d(filename, values)
41+
!* Dump values to NetCDF file.
42+
! Any existing dataset with the same filename will be overwritten.
43+
CHARACTER (len=*), INTENT(IN) :: filename
44+
INTEGER (kind=int32), DIMENSION(:), INTENT(IN) :: values
45+
INTEGER :: ncid, varid, dimids(1), i
46+
CHARACTER (len=2) :: dimc
47+
CALL check( nf90_create(filename, NF90_CLOBBER, ncid) )
48+
DO i = 1, size(shape(values))
49+
WRITE (dimc, "(I2.2)") i
50+
CALL check( nf90_def_dim(ncid, "dim" // dimc, size(values, i), dimids(i)) )
51+
END DO
52+
CALL check( nf90_def_var(ncid, "values", NF90_INT, dimids, varid) )
53+
CALL check( nf90_enddef(ncid) )
54+
CALL check( nf90_put_var(ncid, varid, values) )
55+
CALL check( nf90_close(ncid) )
56+
END SUBROUTINE to_netcdf_int32_1d
57+
58+
SUBROUTINE to_netcdf_int32_2d(filename, values)
59+
!* Dump values to NetCDF file.
60+
! Any existing dataset with the same filename will be overwritten.
61+
CHARACTER (len=*), INTENT(IN) :: filename
62+
INTEGER (kind=int32), DIMENSION(:,:), INTENT(IN) :: values
63+
INTEGER :: ncid, varid, dimids(2), i
64+
CHARACTER (len=2) :: dimc
65+
CALL check( nf90_create(filename, NF90_CLOBBER, ncid) )
66+
DO i = 1, size(shape(values))
67+
WRITE (dimc, "(I2.2)") i
68+
CALL check( nf90_def_dim(ncid, "dim" // dimc, size(values, i), dimids(i)) )
69+
END DO
70+
CALL check( nf90_def_var(ncid, "values", NF90_INT, dimids, varid) )
71+
CALL check( nf90_enddef(ncid) )
72+
CALL check( nf90_put_var(ncid, varid, values) )
73+
CALL check( nf90_close(ncid) )
74+
END SUBROUTINE to_netcdf_int32_2d
75+
76+
SUBROUTINE to_netcdf_int32_3d(filename, values)
77+
!* Dump values to NetCDF file.
78+
! Any existing dataset with the same filename will be overwritten.
79+
CHARACTER (len=*), INTENT(IN) :: filename
80+
INTEGER (kind=int32), DIMENSION(:,:,:), INTENT(IN) :: values
81+
INTEGER :: ncid, varid, dimids(3), i
82+
CHARACTER (len=2) :: dimc
83+
CALL check( nf90_create(filename, NF90_CLOBBER, ncid) )
84+
DO i = 1, size(shape(values))
85+
WRITE (dimc, "(I2.2)") i
86+
CALL check( nf90_def_dim(ncid, "dim" // dimc, size(values, i), dimids(i)) )
87+
END DO
88+
CALL check( nf90_def_var(ncid, "values", NF90_INT, dimids, varid) )
89+
CALL check( nf90_enddef(ncid) )
90+
CALL check( nf90_put_var(ncid, varid, values) )
91+
CALL check( nf90_close(ncid) )
92+
END SUBROUTINE to_netcdf_int32_3d
93+
94+
SUBROUTINE to_netcdf_real32_1d(filename, values)
95+
!* Dump values to NetCDF file.
96+
! Any existing dataset with the same filename will be overwritten.
97+
CHARACTER (len=*), INTENT(IN) :: filename
98+
REAL (kind=real32), DIMENSION(:), INTENT(IN) :: values
99+
INTEGER :: ncid, varid, dimids(1), i
100+
CHARACTER (len=2) :: dimc
101+
CALL check( nf90_create(filename, NF90_CLOBBER, ncid) )
102+
DO i = 1, size(shape(values))
103+
WRITE (dimc, "(I2.2)") i
104+
CALL check( nf90_def_dim(ncid, "dim" // dimc, size(values, i), dimids(i)) )
105+
END DO
106+
CALL check( nf90_def_var(ncid, "values", NF90_FLOAT, dimids, varid) )
107+
CALL check( nf90_enddef(ncid) )
108+
CALL check( nf90_put_var(ncid, varid, values) )
109+
CALL check( nf90_close(ncid) )
110+
END SUBROUTINE to_netcdf_real32_1d
111+
112+
SUBROUTINE to_netcdf_real32_2d(filename, values)
113+
!* Dump values to NetCDF file.
114+
! Any existing dataset with the same filename will be overwritten.
115+
CHARACTER (len=*), INTENT(IN) :: filename
116+
REAL (kind=real32), DIMENSION(:,:), INTENT(IN) :: values
117+
INTEGER :: ncid, varid, dimids(2), i
118+
CHARACTER (len=2) :: dimc
119+
CALL check( nf90_create(filename, NF90_CLOBBER, ncid) )
120+
DO i = 1, size(shape(values))
121+
WRITE (dimc, "(I2.2)") i
122+
CALL check( nf90_def_dim(ncid, "dim" // dimc, size(values, i), dimids(i)) )
123+
END DO
124+
CALL check( nf90_def_var(ncid, "values", NF90_FLOAT, dimids, varid) )
125+
CALL check( nf90_enddef(ncid) )
126+
CALL check( nf90_put_var(ncid, varid, values) )
127+
CALL check( nf90_close(ncid) )
128+
END SUBROUTINE to_netcdf_real32_2d
129+
130+
SUBROUTINE to_netcdf_real32_3d(filename, values)
131+
!* Dump values to NetCDF file.
132+
! Any existing dataset with the same filename will be overwritten.
133+
CHARACTER (len=*), INTENT(IN) :: filename
134+
REAL (kind=real32), DIMENSION(:,:,:), INTENT(IN) :: values
135+
INTEGER :: ncid, varid, dimids(3), i
136+
CHARACTER (len=2) :: dimc
137+
CALL check( nf90_create(filename, NF90_CLOBBER, ncid) )
138+
DO i = 1, size(shape(values))
139+
WRITE (dimc, "(I2.2)") i
140+
CALL check( nf90_def_dim(ncid, "dim" // dimc, size(values, i), dimids(i)) )
141+
END DO
142+
CALL check( nf90_def_var(ncid, "values", NF90_FLOAT, dimids, varid) )
143+
CALL check( nf90_enddef(ncid) )
144+
CALL check( nf90_put_var(ncid, varid, values) )
145+
CALL check( nf90_close(ncid) )
146+
END SUBROUTINE to_netcdf_real32_3d
147+
148+
SUBROUTINE to_netcdf_real64_1d(filename, values)
149+
!* Dump values to NetCDF file.
150+
! Any existing dataset with the same filename will be overwritten.
151+
CHARACTER (len=*), INTENT(IN) :: filename
152+
REAL (kind=real64), DIMENSION(:), INTENT(IN) :: values
153+
INTEGER :: ncid, varid, dimids(1), i
154+
CHARACTER (len=2) :: dimc
155+
CALL check( nf90_create(filename, NF90_CLOBBER, ncid) )
156+
DO i = 1, size(shape(values))
157+
WRITE (dimc, "(I2.2)") i
158+
CALL check( nf90_def_dim(ncid, "dim" // dimc, size(values, i), dimids(i)) )
159+
END DO
160+
CALL check( nf90_def_var(ncid, "values", NF90_DOUBLE, dimids, varid) )
161+
CALL check( nf90_enddef(ncid) )
162+
CALL check( nf90_put_var(ncid, varid, values) )
163+
CALL check( nf90_close(ncid) )
164+
END SUBROUTINE to_netcdf_real64_1d
165+
166+
SUBROUTINE to_netcdf_real64_2d(filename, values)
167+
!* Dump values to NetCDF file.
168+
! Any existing dataset with the same filename will be overwritten.
169+
CHARACTER (len=*), INTENT(IN) :: filename
170+
REAL (kind=real64), DIMENSION(:,:), INTENT(IN) :: values
171+
INTEGER :: ncid, varid, dimids(2), i
172+
CHARACTER (len=2) :: dimc
173+
CALL check( nf90_create(filename, NF90_CLOBBER, ncid) )
174+
DO i = 1, size(shape(values))
175+
WRITE (dimc, "(I2.2)") i
176+
CALL check( nf90_def_dim(ncid, "dim" // dimc, size(values, i), dimids(i)) )
177+
END DO
178+
CALL check( nf90_def_var(ncid, "values", NF90_DOUBLE, dimids, varid) )
179+
CALL check( nf90_enddef(ncid) )
180+
CALL check( nf90_put_var(ncid, varid, values) )
181+
CALL check( nf90_close(ncid) )
182+
END SUBROUTINE to_netcdf_real64_2d
183+
184+
SUBROUTINE to_netcdf_real64_3d(filename, values)
185+
!* Dump values to NetCDF file.
186+
! Any existing dataset with the same filename will be overwritten.
187+
CHARACTER (len=*), INTENT(IN) :: filename
188+
REAL (kind=real64), DIMENSION(:,:,:), INTENT(IN) :: values
189+
INTEGER :: ncid, varid, dimids(3), i
190+
CHARACTER (len=2) :: dimc
191+
CALL check( nf90_create(filename, NF90_CLOBBER, ncid) )
192+
DO i = 1, size(shape(values))
193+
WRITE (dimc, "(I2.2)") i
194+
CALL check( nf90_def_dim(ncid, "dim" // dimc, size(values, i), dimids(i)) )
195+
END DO
196+
CALL check( nf90_def_var(ncid, "values", NF90_DOUBLE, dimids, varid) )
197+
CALL check( nf90_enddef(ncid) )
198+
CALL check( nf90_put_var(ncid, varid, values) )
199+
CALL check( nf90_close(ncid) )
200+
END SUBROUTINE to_netcdf_real64_3d
201+
202+
END MODULE netcdf_utils

0 commit comments

Comments
 (0)