portedportable, not yet portedexecuted, not portableexecutable, not hit by this run
| 1 | ! This file is part of MOM6, the Modular Ocean Model version 6. | |
| 2 | ! See the LICENSE file for licensing information. | |
| 3 | ! SPDX-License-Identifier: Apache-2.0 | |
| 4 | ||
| 5 | !> MOM6 interface to netCDF operations | |
| 6 | module MOM_netcdf | |
| 7 | ||
| 8 | use, intrinsic :: iso_fortran_env, only : real32, real64 | |
| 9 | ||
| 10 | use netcdf, only : nf90_create, nf90_open, nf90_close | |
| 11 | use netcdf, only : nf90_sync | |
| 12 | use netcdf, only : NF90_CLOBBER, NF90_NOCLOBBER, NF90_WRITE, NF90_NOWRITE | |
| 13 | use netcdf, only : nf90_enddef | |
| 14 | use netcdf, only : nf90_def_dim, nf90_def_var | |
| 15 | use netcdf, only : NF90_UNLIMITED | |
| 16 | use netcdf, only : nf90_get_var | |
| 17 | use netcdf, only : nf90_put_var, nf90_put_att | |
| 18 | use netcdf, only : NF90_FLOAT, NF90_DOUBLE | |
| 19 | use netcdf, only : nf90_strerror, NF90_NOERR | |
| 20 | use netcdf, only : NF90_GLOBAL | |
| 21 | use netcdf, only : nf90_inquire, nf90_inquire_dimension, nf90_inquire_variable | |
| 22 | use netcdf, only : nf90_inq_dimids, nf90_inq_varids | |
| 23 | use netcdf, only : NF90_MAX_NAME | |
| 24 | ||
| 25 | use MOM_error_handler, only : MOM_error, FATAL | |
| 26 | use MOM_io_infra, only : READONLY_FILE, WRITEONLY_FILE | |
| 27 | use MOM_io_infra, only : APPEND_FILE, OVERWRITE_FILE | |
| 28 | ||
| 29 | implicit none ; private | |
| 30 | ||
| 31 | public :: netcdf_file_type | |
| 32 | public :: netcdf_axis | |
| 33 | public :: netcdf_field | |
| 34 | public :: open_netcdf_file | |
| 35 | public :: close_netcdf_file | |
| 36 | public :: flush_netcdf_file | |
| 37 | public :: register_netcdf_axis | |
| 38 | public :: register_netcdf_field | |
| 39 | public :: write_netcdf_field | |
| 40 | public :: write_netcdf_axis | |
| 41 | public :: write_netcdf_attribute | |
| 42 | public :: get_netcdf_size | |
| 43 | public :: get_netcdf_fields | |
| 44 | public :: get_netcdf_filename | |
| 45 | public :: read_netcdf_field | |
| 46 | ||
| 47 | ||
| 48 | !> Internal time value used to indicate an uninitialized time | |
| 49 | real, parameter :: NULLTIME = -1 | |
| 50 | ! NOTE: For now, we use the FMS-compatible value, but may change in the future. | |
| 51 | ||
| 52 | ||
| 53 | !> netCDF file abstraction | |
| 54 | type :: netcdf_file_type | |
| 55 | private | |
| 56 | integer :: ncid | |
| 57 | !< netCDF file ID | |
| 58 | character(len=:), allocatable :: filename | |
| 59 | !< netCDF filename | |
| 60 | logical :: define_mode | |
| 61 | !< True if file is in define mode. | |
| 62 | integer :: time_id | |
| 63 | !< Time axis variable ID | |
| 64 | real :: time | |
| 65 | !< Current model time | |
| 66 | integer :: time_level | |
| 67 | !< Current time level for output | |
| 68 | end type netcdf_file_type | |
| 69 | ||
| 70 | ||
| 71 | !> Dimension axis for a netCDF file | |
| 72 | type :: netcdf_axis | |
| 73 | private | |
| 74 | character(len=:), allocatable, public :: label | |
| 75 | !< Axis label name | |
| 76 | real, allocatable :: points(:) | |
| 77 | !< Grid points along the axis | |
| 78 | integer :: dimid | |
| 79 | !< netCDF dimension ID associated with axis | |
| 80 | integer :: varid | |
| 81 | !< netCDF variable ID associated with axis | |
| 82 | end type netcdf_axis | |
| 83 | ||
| 84 | ||
| 85 | !> Field variable for a netCDF file | |
| 86 | type netcdf_field | |
| 87 | private | |
| 88 | character(len=:), allocatable, public :: label | |
| 89 | !< Variable name | |
| 90 | integer :: varid | |
| 91 | !< netCDF variable ID for field | |
| 92 | end type netcdf_field | |
| 93 | ||
| 94 | ||
| 95 | !> Write values to a field of a netCDF file | |
| 96 | interface write_netcdf_field | |
| 97 | module procedure write_netcdf_field_4d | |
| 98 | module procedure write_netcdf_field_3d | |
| 99 | module procedure write_netcdf_field_2d | |
| 100 | module procedure write_netcdf_field_1d | |
| 101 | module procedure write_netcdf_field_0d | |
| 102 | end interface write_netcdf_field | |
| 103 | ||
| 104 | contains | |
| 105 | ||
| 106 | 2 | subroutine open_netcdf_file(handle, filename, mode) |
| 107 | type(netcdf_file_type), intent(inout) :: handle | |
| 108 | !< netCDF file handle | |
| 109 | character(len=*), intent(in) :: filename | |
| 110 | !< netCDF filename | |
| 111 | integer, intent(in), optional :: mode | |
| 112 | !< Input MOM I/O mode | |
| 113 | ||
| 114 | integer :: io_mode | |
| 115 | ! MOM I/O mode | |
| 116 | integer :: cmode | |
| 117 | ! netCDF creation mode | |
| 118 | integer :: rc | |
| 119 | ! nf90_create return code | |
| 120 | character(len=:), allocatable :: msg | |
| 121 | ! netCDF error message buffer | |
| 122 | ||
| 123 | ! I/O configuration | |
| 124 | 2 | io_mode = WRITEONLY_FILE |
| 125 | 2 | if (present(mode)) io_mode = mode |
| 126 | ||
| 127 | ! Translate the MOM I/O config to the netCDF mode | |
| 128 | 0 | select case(io_mode) |
| 129 | case (WRITEONLY_FILE) | |
| 130 | 0 | rc = nf90_create(filename, nf90_noclobber, handle%ncid) |
| 131 | 0 | handle%define_mode = .true. |
| 132 | case (OVERWRITE_FILE) | |
| 133 | 2 | rc = nf90_create(filename, nf90_clobber, handle%ncid) |
| 134 | 2 | handle%define_mode = .true. |
| 135 | case (APPEND_FILE) | |
| 136 | 0 | rc = nf90_open(filename, nf90_write, handle%ncid) |
| 137 | 0 | handle%define_mode = .false. |
| 138 | case (READONLY_FILE) | |
| 139 | 0 | rc = nf90_open(filename, nf90_nowrite, handle%ncid) |
| 140 | 0 | handle%define_mode = .false. |
| 141 | case default | |
| 142 | call MOM_error(FATAL, & | |
| 143 | 2 | 'open_netcdf_file: File ' // filename // ': Unknown mode.') |
| 144 | end select | |
| 145 | 2 | call check_netcdf_call(rc, 'open_netcdf_file', 'File ' // filename) |
| 146 | ||
| 147 | 2 | handle%filename = filename |
| 148 | ||
| 149 | ! FMS writes the filename as an attribute | |
| 150 | 4 | if (any(io_mode == [WRITEONLY_FILE, OVERWRITE_FILE])) & |
| 151 | 2 | call write_netcdf_attribute(handle, 'filename', filename) |
| 152 | 2 | end subroutine open_netcdf_file |
| 153 | ||
| 154 | ||
| 155 | !> Close an opened netCDF file. | |
| 156 | 1 | subroutine close_netcdf_file(handle) |
| 157 | type(netcdf_file_type), intent(in) :: handle | |
| 158 | ||
| 159 | integer :: rc | |
| 160 | ||
| 161 | 1 | rc = nf90_close(handle%ncid) |
| 162 | call check_netcdf_call(rc, 'close_netcdf_file', & | |
| 163 | 1 | 'File "' // handle%filename // '"') |
| 164 | 1 | end subroutine close_netcdf_file |
| 165 | ||
| 166 | ||
| 167 | !> Flush buffered output to the netCDF file | |
| 168 | 3 | subroutine flush_netcdf_file(handle) |
| 169 | type(netcdf_file_type), intent(in) :: handle | |
| 170 | ||
| 171 | integer :: rc | |
| 172 | ||
| 173 | 3 | rc = nf90_sync(handle%ncid) |
| 174 | call check_netcdf_call(rc, 'flush_netcdf_file', & | |
| 175 | 3 | 'File "' // handle%filename // '"') |
| 176 | 3 | end subroutine flush_netcdf_file |
| 177 | ||
| 178 | ||
| 179 | !> Change netCDF mode of handle from 'define' to 'write'. | |
| 180 | 2 | subroutine enable_netcdf_write(handle) |
| 181 | type(netcdf_file_type), intent(inout) :: handle | |
| 182 | ||
| 183 | integer :: rc | |
| 184 | ||
| 185 | 2 | if (handle%define_mode) then |
| 186 | 2 | rc = nf90_enddef(handle%ncid) |
| 187 | call check_netcdf_call(rc, 'enable_netcdf_write', & | |
| 188 | 2 | 'File "' // handle%filename // '"') |
| 189 | 2 | handle%define_mode = .false. |
| 190 | endif | |
| 191 | 2 | end subroutine enable_netcdf_write |
| 192 | ||
| 193 | ||
| 194 | !> Register a netCDF variable | |
| 195 | 20 | function register_netcdf_field(handle, label, axes, longname, units) & |
| 196 | result(field) | |
| 197 | type(netcdf_file_type), intent(in) :: handle | |
| 198 | !< netCDF file handle | |
| 199 | character(len=*), intent(in) :: label | |
| 200 | !< netCDF field name in the file | |
| 201 | type(netcdf_axis), intent(in) :: axes(:) | |
| 202 | !< Axes along which field is defined | |
| 203 | character(len=*), intent(in) :: longname | |
| 204 | !< Long name of the netCDF field | |
| 205 | character(len=*), intent(in) :: units | |
| 206 | !< Field units of measurement | |
| 207 | type(netcdf_field) :: field | |
| 208 | !< netCDF field | |
| 209 | ||
| 210 | integer :: rc | |
| 211 | ! netCDF function return code | |
| 212 | integer :: i | |
| 213 | ! Loop index | |
| 214 | 20 | integer, allocatable :: dimids(:) |
| 215 | ! netCDF dimension IDs of axes | |
| 216 | integer :: xtype | |
| 217 | ! netCDF data type | |
| 218 | ||
| 219 | ! Gather the axis netCDF dimension IDs | |
| 220 | 20 | allocate(dimids(size(axes))) |
| 221 | 68 | dimids(:) = [(axes(i)%dimid, i = 1, size(axes))] |
| 222 | ||
| 223 | 20 | field%label = label |
| 224 | ||
| 225 | ! Determine the corresponding netCDF data type | |
| 226 | ! TODO: Support a `pack`-like argument | |
| 227 | select case (kind(1.0)) | |
| 228 | case (real32) | |
| 229 | 20 | xtype = NF90_FLOAT |
| 230 | case (real64) | |
| 231 | 20 | xtype = NF90_DOUBLE |
| 232 | case default | |
| 233 | call MOM_error(FATAL, "register_netcdf_field: Unknown kind(real).") | |
| 234 | end select | |
| 235 | ||
| 236 | ! Register the field variable | |
| 237 | 20 | rc = nf90_def_var(handle%ncid, label, xtype, dimids, field%varid) |
| 238 | call check_netcdf_call(rc, 'register_netcdf_field', & | |
| 239 | 20 | 'File "' // handle%filename // '", Field "' // label // '"') |
| 240 | ||
| 241 | ! Assign attributes | |
| 242 | ||
| 243 | 20 | rc = nf90_put_att(handle%ncid, field%varid, 'long_name', longname) |
| 244 | call check_netcdf_call(rc, 'register_netcdf_field', & | |
| 245 | 'Attribute "long_name" of variable "' // label // '" in file "' & | |
| 246 | 20 | // handle%filename // '"') |
| 247 | ||
| 248 | 20 | rc = nf90_put_att(handle%ncid, field%varid, 'units', units) |
| 249 | call check_netcdf_call(rc, 'register_netcdf_field', & | |
| 250 | 'Attribute "units" of variable "' // label // '" in file "' & | |
| 251 | 20 | // handle%filename // '"') |
| 252 | 40 | end function register_netcdf_field |
| 253 | ||
| 254 | ||
| 255 | !> Create an axis and associated dimension in a netCDF file | |
| 256 | 5 | function register_netcdf_axis(handle, label, units, longname, points, & |
| 257 | cartesian, sense) result(axis) | |
| 258 | type(netcdf_file_type), intent(inout) :: handle | |
| 259 | !< netCDF file handle | |
| 260 | character(len=*), intent(in) :: label | |
| 261 | !< netCDF axis name in the file | |
| 262 | character(len=*), intent(in), optional :: units | |
| 263 | !< Axis units of measurement | |
| 264 | character(len=*), intent(in), optional :: longname | |
| 265 | !< Long name of the axis | |
| 266 | real, intent(in), optional :: points(:) | |
| 267 | !< Values of axis points (for fixed axes) | |
| 268 | character(len=*), intent(in), optional :: cartesian | |
| 269 | !< Character denoting axis direction: X, Y, Z, T, or N for none | |
| 270 | integer, intent(in), optional :: sense | |
| 271 | !< Axis direction; +1 if axis increases upward or -1 if downward | |
| 272 | ||
| 273 | type(netcdf_axis) :: axis | |
| 274 | !< netCDF coordinate axis | |
| 275 | ||
| 276 | integer :: xtype | |
| 277 | ! netCDF external data type | |
| 278 | integer :: rc | |
| 279 | ! netCDF function return code | |
| 280 | logical :: unlimited | |
| 281 | ! True if the axis is unlimited in size (e.g. time) | |
| 282 | integer :: axis_size | |
| 283 | ! Either the number of points in the axis, or unlimited flag | |
| 284 | integer :: axis_sense | |
| 285 | ! Axis direction; +1 if axis increases upward or -1 if downward | |
| 286 | 5 | character(len=:), allocatable :: sense_attr |
| 287 | ! CF-compiant value of sense attribute (as 'positive') | |
| 288 | ||
| 289 | ! Create the axis dimension | |
| 290 | 5 | unlimited = .false. |
| 291 | 5 | if (present(cartesian)) then |
| 292 | 5 | if (cartesian == 'T') unlimited = .true. |
| 293 | endif | |
| 294 | ||
| 295 | ! Either the axis is explicitly set with data or is declared as unlimited | |
| 296 | 5 | if (present(points) .eqv. unlimited) then |
| 297 | call MOM_error(FATAL, & | |
| 298 | 0 | "Axis must either have explicit points or be a time axis ('T').") |
| 299 | endif | |
| 300 | ||
| 301 | 5 | axis%label = label |
| 302 | ||
| 303 | 5 | if (present(points)) then |
| 304 | 4 | axis_size = size(points) |
| 305 | 4 | allocate(axis%points(axis_size)) |
| 306 | 306 | axis%points(:) = points(:) |
| 307 | else | |
| 308 | 1 | axis_size = NF90_UNLIMITED |
| 309 | endif | |
| 310 | ||
| 311 | 5 | rc = nf90_def_dim(handle%ncid, label, axis_size, axis%dimid) |
| 312 | call check_netcdf_call(rc, 'register_netcdf_axis', & | |
| 313 | 5 | 'Dimension "' // label // '" in file "' // handle%filename // '"') |
| 314 | ||
| 315 | ! Determine the corresponding netCDF data type | |
| 316 | ! TODO: Support a `pack`-like argument | |
| 317 | select case (kind(1.0)) | |
| 318 | case (real32) | |
| 319 | xtype = NF90_FLOAT | |
| 320 | case (real64) | |
| 321 | 5 | xtype = NF90_DOUBLE |
| 322 | case default | |
| 323 | call MOM_error(FATAL, "register_netcdf_axis: Unknown kind(real).") | |
| 324 | end select | |
| 325 | ||
| 326 | ! Create a variable corresponding to the axis | |
| 327 | 5 | rc = nf90_def_var(handle%ncid, label, xtype, axis%dimid, axis%varid) |
| 328 | call check_netcdf_call(rc, 'register_netcdf_axis', & | |
| 329 | 5 | 'Variable ' // label // ' in file ' // handle%filename) |
| 330 | ||
| 331 | ! Define the time axis, if available | |
| 332 | 5 | if (unlimited) then |
| 333 | 1 | handle%time_id = axis%varid |
| 334 | 1 | handle%time_level = 0 |
| 335 | 1 | handle%time = NULLTIME |
| 336 | endif | |
| 337 | ||
| 338 | ! Assign attributes if present | |
| 339 | 5 | if (present(longname)) then |
| 340 | 5 | rc = nf90_put_att(handle%ncid, axis%varid, 'long_name', longname) |
| 341 | call check_netcdf_call(rc, 'register_netcdf_axis', & | |
| 342 | 'Attribute ''long_name'' of variable ' // label // ' in file ' & | |
| 343 | 5 | // handle%filename) |
| 344 | endif | |
| 345 | ||
| 346 | 5 | if (present(units)) then |
| 347 | 5 | rc = nf90_put_att(handle%ncid, axis%varid, 'units', units) |
| 348 | call check_netcdf_call(rc, 'register_netcdf_axis', & | |
| 349 | 'Attribute ''units'' of variable ' // label // ' in file ' & | |
| 350 | 5 | // handle%filename) |
| 351 | endif | |
| 352 | ||
| 353 | 5 | if (present(cartesian)) then |
| 354 | 5 | rc = nf90_put_att(handle%ncid, axis%varid, 'cartesian_axis', cartesian) |
| 355 | call check_netcdf_call(rc, 'register_netcdf_axis', & | |
| 356 | 'Attribute ''cartesian_axis'' of variable ' // label // ' in file ' & | |
| 357 | 5 | // handle%filename) |
| 358 | endif | |
| 359 | ||
| 360 | 5 | axis_sense = 0 |
| 361 | 5 | if (present(sense)) axis_sense = sense |
| 362 | ||
| 363 | 5 | if (axis_sense /= 0) then |
| 364 | 4 | select case (axis_sense) |
| 365 | case (1) | |
| 366 | 4 | sense_attr = 'up' |
| 367 | case (-1) | |
| 368 | 0 | sense_attr = 'down' |
| 369 | case default | |
| 370 | call MOM_error(FATAL, 'register_netcdf_axis: sense must be either ' & | |
| 371 | 4 | // '0, 1, or -1.') |
| 372 | end select | |
| 373 | 4 | rc = nf90_put_att(handle%ncid, axis%varid, 'positive', sense_attr) |
| 374 | call check_netcdf_call(rc, 'register_netcdf_axis', & | |
| 375 | 'Attribute "positive" of variable "' // label // '" in file "' & | |
| 376 | 4 | // handle%filename // '"') |
| 377 | endif | |
| 378 | 10 | end function register_netcdf_axis |
| 379 | ||
| 380 | ||
| 381 | !> Write a 4D array to a compatible netCDF field | |
| 382 | 0 | subroutine write_netcdf_field_4d(handle, field, values, time) |
| 383 | type(netcdf_file_type), intent(inout) :: handle | |
| 384 | !< netCDF file handle | |
| 385 | type(netcdf_field), intent(in) :: field | |
| 386 | !< Field metadata | |
| 387 | real, intent(in) :: values(:,:,:,:) | |
| 388 | !< Field values | |
| 389 | real, intent(in), optional :: time | |
| 390 | !< Timestep index to write data | |
| 391 | ||
| 392 | integer :: rc | |
| 393 | ! netCDF return code | |
| 394 | integer :: start(5) | |
| 395 | ! Start indices, if timestep is included | |
| 396 | ||
| 397 | ! Verify write mode | |
| 398 | 0 | if (handle%define_mode) & |
| 399 | 0 | call enable_netcdf_write(handle) |
| 400 | ||
| 401 | 0 | if (present(time)) then |
| 402 | 0 | call update_netcdf_timestep(handle, time) |
| 403 | 0 | start(:4) = 1 |
| 404 | 0 | start(5) = handle%time_level |
| 405 | 0 | rc = nf90_put_var(handle%ncid, field%varid, values, start) |
| 406 | else | |
| 407 | 0 | rc = nf90_put_var(handle%ncid, field%varid, values) |
| 408 | endif | |
| 409 | call check_netcdf_call(rc, 'write_netcdf_file', & | |
| 410 | 0 | 'File "' // handle%filename // '", Field "' // field%label // '"') |
| 411 | 0 | end subroutine write_netcdf_field_4d |
| 412 | ||
| 413 | ||
| 414 | !> Write a 3D array to a compatible netCDF field | |
| 415 | 0 | subroutine write_netcdf_field_3d(handle, field, values, time) |
| 416 | type(netcdf_file_type), intent(inout) :: handle | |
| 417 | !< netCDF file handle | |
| 418 | type(netcdf_field), intent(in) :: field | |
| 419 | !< Field metadata | |
| 420 | real, intent(in) :: values(:,:,:) | |
| 421 | !< Field values | |
| 422 | real, intent(in), optional :: time | |
| 423 | !< Timestep index to write data | |
| 424 | ||
| 425 | integer :: rc | |
| 426 | ! netCDF return code | |
| 427 | integer :: start(4) | |
| 428 | ! Start indices, if timestep is included | |
| 429 | ||
| 430 | ! Verify write mode | |
| 431 | 0 | if (handle%define_mode) & |
| 432 | 0 | call enable_netcdf_write(handle) |
| 433 | ||
| 434 | 0 | if (present(time)) then |
| 435 | 0 | call update_netcdf_timestep(handle, time) |
| 436 | 0 | start(:3) = 1 |
| 437 | 0 | start(4) = handle%time_level |
| 438 | 0 | rc = nf90_put_var(handle%ncid, field%varid, values, start) |
| 439 | else | |
| 440 | 0 | rc = nf90_put_var(handle%ncid, field%varid, values) |
| 441 | endif | |
| 442 | call check_netcdf_call(rc, 'write_netcdf_file', & | |
| 443 | 0 | 'File "' // handle%filename // '", Field "' // field%label // '"') |
| 444 | 0 | end subroutine write_netcdf_field_3d |
| 445 | ||
| 446 | ||
| 447 | !> Write a 2D array to a compatible netCDF field | |
| 448 | 0 | subroutine write_netcdf_field_2d(handle, field, values, time) |
| 449 | type(netcdf_file_type), intent(inout) :: handle | |
| 450 | !< netCDF file handle | |
| 451 | type(netcdf_field), intent(in) :: field | |
| 452 | !< Field metadata | |
| 453 | real, intent(in) :: values(:,:) | |
| 454 | !< Field values | |
| 455 | real, intent(in), optional :: time | |
| 456 | !< Timestep index to write data | |
| 457 | ||
| 458 | integer :: rc | |
| 459 | ! netCDF return code | |
| 460 | integer :: start(3) | |
| 461 | ! Start indices, if timestep is included | |
| 462 | ||
| 463 | ! Verify write mode | |
| 464 | 0 | if (handle%define_mode) & |
| 465 | 0 | call enable_netcdf_write(handle) |
| 466 | ||
| 467 | 0 | if (present(time)) then |
| 468 | 0 | call update_netcdf_timestep(handle, time) |
| 469 | 0 | start(:2) = 1 |
| 470 | 0 | start(3) = handle%time_level |
| 471 | 0 | rc = nf90_put_var(handle%ncid, field%varid, values, start) |
| 472 | else | |
| 473 | 0 | rc = nf90_put_var(handle%ncid, field%varid, values) |
| 474 | endif | |
| 475 | call check_netcdf_call(rc, 'write_netcdf_file', & | |
| 476 | 0 | 'File "' // handle%filename // '", Field "' // field%label // '"') |
| 477 | 0 | end subroutine write_netcdf_field_2d |
| 478 | ||
| 479 | ||
| 480 | !> Write a 1D array to a compatible netCDF field | |
| 481 | 14 | subroutine write_netcdf_field_1d(handle, field, values, time) |
| 482 | type(netcdf_file_type), intent(inout) :: handle | |
| 483 | !< netCDF file handle | |
| 484 | type(netcdf_field), intent(in) :: field | |
| 485 | !< Field metadata | |
| 486 | real, intent(in) :: values(:) | |
| 487 | !< Field values | |
| 488 | real, intent(in), optional :: time | |
| 489 | !< Timestep index to write data | |
| 490 | ||
| 491 | integer :: rc | |
| 492 | ! netCDF return code | |
| 493 | integer :: start(2) | |
| 494 | ! Start indices, if timestep is included | |
| 495 | ||
| 496 | ! Verify write mode | |
| 497 | 14 | if (handle%define_mode) & |
| 498 | 0 | call enable_netcdf_write(handle) |
| 499 | ||
| 500 | 14 | if (present(time)) then |
| 501 | 12 | call update_netcdf_timestep(handle, time) |
| 502 | 12 | start(1) = 1 |
| 503 | 12 | start(2) = handle%time_level |
| 504 | 12 | rc = nf90_put_var(handle%ncid, field%varid, values, start) |
| 505 | else | |
| 506 | 2 | rc = nf90_put_var(handle%ncid, field%varid, values) |
| 507 | endif | |
| 508 | call check_netcdf_call(rc, 'write_netcdf_file', & | |
| 509 | 14 | 'File "' // handle%filename // '", Field "' // field%label // '"') |
| 510 | 14 | end subroutine write_netcdf_field_1d |
| 511 | ||
| 512 | ||
| 513 | !> Write a scalar to a compatible netCDF field | |
| 514 | 42 | subroutine write_netcdf_field_0d(handle, field, scalar, time) |
| 515 | type(netcdf_file_type), intent(inout) :: handle | |
| 516 | !< netCDF file handle | |
| 517 | type(netcdf_field), intent(in) :: field | |
| 518 | !< Field metadata | |
| 519 | real, intent(in) :: scalar | |
| 520 | !< Field values | |
| 521 | real, intent(in), optional :: time | |
| 522 | !< Timestep index to write data | |
| 523 | ||
| 524 | integer :: rc | |
| 525 | ! netCDF return code | |
| 526 | integer :: start(1) | |
| 527 | ! Start indices, if timestep is included | |
| 528 | ||
| 529 | ! Verify write mode | |
| 530 | 42 | if (handle%define_mode) & |
| 531 | 0 | call enable_netcdf_write(handle) |
| 532 | ||
| 533 | 42 | if (present(time)) then |
| 534 | 42 | call update_netcdf_timestep(handle, time) |
| 535 | 42 | start(1) = handle%time_level |
| 536 | 42 | rc = nf90_put_var(handle%ncid, field%varid, scalar, start) |
| 537 | else | |
| 538 | 0 | rc = nf90_put_var(handle%ncid, field%varid, scalar) |
| 539 | endif | |
| 540 | call check_netcdf_call(rc, 'write_netcdf_file', & | |
| 541 | 42 | 'File "' // handle%filename // '", Field "' // field%label // '"') |
| 542 | 42 | end subroutine write_netcdf_field_0d |
| 543 | ||
| 544 | ||
| 545 | !> Write axis points to associated netCDF variable | |
| 546 | 4 | subroutine write_netcdf_axis(handle, axis) |
| 547 | type(netcdf_file_type), intent(inout) :: handle | |
| 548 | !< netCDF file handle | |
| 549 | type(netcdf_axis), intent(in) :: axis | |
| 550 | !< field variable | |
| 551 | ||
| 552 | integer :: rc | |
| 553 | ! netCDF return code | |
| 554 | ||
| 555 | ! Verify write mode | |
| 556 | 4 | if (handle%define_mode) & |
| 557 | 2 | call enable_netcdf_write(handle) |
| 558 | ||
| 559 | 4 | rc = nf90_put_var(handle%ncid, axis%varid, axis%points) |
| 560 | call check_netcdf_call(rc, 'write_netcdf_axis', & | |
| 561 | 4 | 'File "' // handle%filename // '", Axis "' // axis%label // '"') |
| 562 | 4 | end subroutine write_netcdf_axis |
| 563 | ||
| 564 | ||
| 565 | !> Write a global attribute to a netCDF file | |
| 566 | 2 | subroutine write_netcdf_attribute(handle, label, attribute) |
| 567 | type(netcdf_file_type), intent(in) :: handle | |
| 568 | !< netCDF file handle | |
| 569 | character(len=*), intent(in) :: label | |
| 570 | !< File attribute | |
| 571 | character(len=*), intent(in) :: attribute | |
| 572 | !< File attribute value | |
| 573 | ||
| 574 | integer :: rc | |
| 575 | ! netCDF return code | |
| 576 | ||
| 577 | 4 | rc = nf90_put_att(handle%ncid, NF90_GLOBAL, label, attribute) |
| 578 | call check_netcdf_call(rc, 'write_netcdf_attribute', & | |
| 579 | 2 | 'File "' // handle%filename // '", Attribute "' // label // '"') |
| 580 | 2 | end subroutine write_netcdf_attribute |
| 581 | ||
| 582 | ||
| 583 | ! This is a thin interface to nf90_inquire, designed to mirror the existing | |
| 584 | ! I/O API. A more axis-aware system might not need this, but for now it's here | |
| 585 | !> Get the number of dimensions, variables, and timesteps in a netCDF file | |
| 586 | 0 | subroutine get_netcdf_size(handle, ndims, nvars, nsteps) |
| 587 | type(netcdf_file_type), intent(in) :: handle | |
| 588 | !< netCDF input file | |
| 589 | integer, intent(out), optional :: ndims | |
| 590 | !< number of dimensions in the file | |
| 591 | integer, intent(out), optional :: nvars | |
| 592 | !< number of variables in the file | |
| 593 | integer, intent(out), optional :: nsteps | |
| 594 | !< number of values in the file's unlimited axis | |
| 595 | ||
| 596 | integer :: rc | |
| 597 | ! netCDF return code | |
| 598 | integer :: unlimited_dimid | |
| 599 | ! netCDF dimension ID for unlimited time axis | |
| 600 | ||
| 601 | rc = nf90_inquire(handle%ncid, & | |
| 602 | nDimensions=ndims, & | |
| 603 | nVariables=nvars, & | |
| 604 | unlimitedDimId=unlimited_dimid & | |
| 605 | 0 | ) |
| 606 | call check_netcdf_call(rc, 'get_netcdf_size', & | |
| 607 | 0 | 'File "' // handle%filename // '"') |
| 608 | ||
| 609 | 0 | rc = nf90_inquire_dimension(handle%ncid, unlimited_dimid, len=nsteps) |
| 610 | call check_netcdf_call(rc, 'get_netcdf_size', & | |
| 611 | 0 | 'File "' // handle%filename // '"') |
| 612 | 0 | end subroutine get_netcdf_size |
| 613 | ||
| 614 | ||
| 615 | !> Get the metadata of the registered fields in a netCDF file | |
| 616 | 0 | subroutine get_netcdf_fields(handle, axes, fields) |
| 617 | type(netcdf_file_type), intent(inout) :: handle | |
| 618 | !< netCDF file handle | |
| 619 | type(netcdf_axis), intent(inout), allocatable :: axes(:) | |
| 620 | !< netCDF file axes | |
| 621 | type(netcdf_field), intent(inout), allocatable :: fields(:) | |
| 622 | !< netCDF file fields | |
| 623 | ||
| 624 | integer :: ndims | |
| 625 | ! Number of netCDF dimensions | |
| 626 | integer :: nvars | |
| 627 | ! Number of netCDF dimensions | |
| 628 | 0 | type(netcdf_field), allocatable :: vars(:) |
| 629 | ! netCDF variables in handle | |
| 630 | integer :: nfields | |
| 631 | ! Number of fields in the file (i.e. non-axis variables) | |
| 632 | 0 | integer, allocatable :: dimids(:) |
| 633 | ! netCDF dimension IDs of file | |
| 634 | 0 | integer, allocatable :: varids(:) |
| 635 | ! netCDF variable IDs of file | |
| 636 | integer :: unlim_dimid | |
| 637 | ! netCDF dimension ID for the unlimited axis variable, if present | |
| 638 | integer :: unlim_index | |
| 639 | ! Index of the unlimited axis in axes(:), if present | |
| 640 | character(len=NF90_MAX_NAME) :: label | |
| 641 | ! Current dimension or variable label | |
| 642 | integer :: len | |
| 643 | ! Current dimension length | |
| 644 | integer :: rc | |
| 645 | ! netCDF return code | |
| 646 | integer :: grp_ndims, grp_nvars | |
| 647 | ! Group-based counts for nf90_inq_* (unused) | |
| 648 | logical :: is_axis | |
| 649 | ! True if the current variable is an axis | |
| 650 | integer :: i, j, n | |
| 651 | ||
| 652 | integer, save :: no_parent_groups = 0 | |
| 653 | ! Flag indicating exclusion of parent groups in netCDF file | |
| 654 | ! NOTE: This must be passed as a variable, and cannot be declared as a | |
| 655 | ! parameter. | |
| 656 | ||
| 657 | rc = nf90_inquire(handle%ncid, & | |
| 658 | nDimensions=ndims, & | |
| 659 | nVariables=nvars, & | |
| 660 | unlimitedDimId=unlim_dimid & | |
| 661 | 0 | ) |
| 662 | call check_netcdf_call(rc, 'get_netcdf_fields', & | |
| 663 | 0 | 'File "' // handle%filename // '"') |
| 664 | ||
| 665 | 0 | allocate(dimids(ndims)) |
| 666 | 0 | rc = nf90_inq_dimids(handle%ncid, grp_ndims, dimids, no_parent_groups) |
| 667 | call check_netcdf_call(rc, 'get_netcdf_fields', & | |
| 668 | 0 | 'File "' // handle%filename // '"') |
| 669 | ||
| 670 | 0 | allocate(varids(nvars)) |
| 671 | 0 | rc = nf90_inq_varids(handle%ncid, grp_nvars, varids) |
| 672 | call check_netcdf_call(rc, 'get_netcdf_fields', & | |
| 673 | 0 | 'File "' // trim(handle%filename) // '"') |
| 674 | ||
| 675 | ! Initialize unlim_index with an unreachable value (outside [1,ndims]) | |
| 676 | 0 | unlim_index = -1 |
| 677 | ||
| 678 | 0 | allocate(axes(ndims)) |
| 679 | 0 | do i = 1, ndims |
| 680 | 0 | rc = nf90_inquire_dimension(handle%ncid, dimids(i), name=label, len=len) |
| 681 | call check_netcdf_call(rc, 'get_netcdf_fields', & | |
| 682 | 0 | 'File "' // trim(handle%filename) // '"') |
| 683 | ||
| 684 | ! Check for the unlimited axis | |
| 685 | 0 | if (dimids(i) == unlim_dimid) unlim_index = i |
| 686 | ||
| 687 | 0 | axes(i)%dimid = dimids(i) |
| 688 | 0 | axes(i)%label = trim(label) |
| 689 | 0 | allocate(axes(i)%points(len)) |
| 690 | enddo | |
| 691 | ||
| 692 | ! We cannot know if every axis also has a variable representation, so we | |
| 693 | ! over-allocate vars(:) and fill as fields are identified. | |
| 694 | 0 | allocate(vars(nvars)) |
| 695 | ||
| 696 | 0 | nfields = 0 |
| 697 | 0 | do i = 1, nvars |
| 698 | 0 | rc = nf90_inquire_variable(handle%ncid, varids(i), name=label) |
| 699 | call check_netcdf_call(rc, 'get_netcdf_fields', & | |
| 700 | 0 | 'File "' // trim(handle%filename) // '"') |
| 701 | ||
| 702 | ! Check if variable is an axis | |
| 703 | 0 | is_axis = .false. |
| 704 | 0 | do j = 1, ndims |
| 705 | 0 | if (label == axes(j)%label) then |
| 706 | 0 | rc = nf90_get_var(handle%ncid, varids(i), axes(j)%points) |
| 707 | call check_netcdf_call(rc, 'get_netcdf_fields', & | |
| 708 | 0 | 'File "' // trim(handle%filename) // '"') |
| 709 | 0 | axes(j)%varid = varids(i) |
| 710 | ||
| 711 | 0 | if (j == unlim_index) then |
| 712 | 0 | handle%time_id = varids(i) |
| 713 | 0 | handle%time_level = size(axes(j)%points) |
| 714 | 0 | handle%time = NULLTIME |
| 715 | endif | |
| 716 | ||
| 717 | 0 | is_axis = .true. |
| 718 | 0 | exit |
| 719 | endif | |
| 720 | enddo | |
| 721 | 0 | if (is_axis) cycle |
| 722 | ||
| 723 | 0 | nfields = nfields + 1 |
| 724 | 0 | vars(nfields)%label = trim(label) |
| 725 | 0 | vars(nfields)%varid = varids(i) |
| 726 | enddo | |
| 727 | ||
| 728 | 0 | allocate(fields(nfields)) |
| 729 | 0 | fields(:) = vars(:nfields) |
| 730 | 0 | end subroutine get_netcdf_fields |
| 731 | ||
| 732 | !> Return the name of a file from a netCDF handle | |
| 733 | 0 | function get_netcdf_filename(handle) |
| 734 | type(netcdf_file_type), intent(in) :: handle !< A netCDF file handle | |
| 735 | character(len=:), allocatable :: get_netcdf_filename !< The name of the file that this handle refers to. | |
| 736 | ||
| 737 | 0 | get_netcdf_filename = handle%filename |
| 738 | ||
| 739 | 0 | end function |
| 740 | ||
| 741 | !> Read the values of a field from a netCDF file | |
| 742 | 0 | subroutine read_netcdf_field(handle, field, values, bounds) |
| 743 | type(netcdf_file_type), intent(in) :: handle | |
| 744 | type(netcdf_field), intent(in) :: field | |
| 745 | real, intent(out) :: values(:,:) | |
| 746 | integer, optional, intent(in) :: bounds(2,2) | |
| 747 | ||
| 748 | integer :: rc | |
| 749 | ! netCDF return code | |
| 750 | integer :: istart(2) | |
| 751 | ! Axis start index | |
| 752 | integer :: icount(2) | |
| 753 | ! Axis index count | |
| 754 | ||
| 755 | 0 | if (present(bounds)) then |
| 756 | 0 | istart(:) = bounds(1,:) |
| 757 | 0 | icount(:) = bounds(2,:) - bounds(1,:) + 1 |
| 758 | 0 | rc = nf90_get_var(handle%ncid, field%varid, values, start=istart, count=icount) |
| 759 | else | |
| 760 | 0 | rc = nf90_get_var(handle%ncid, field%varid, values) |
| 761 | endif | |
| 762 | call check_netcdf_call(rc, 'read_netcdf_field', & | |
| 763 | 0 | 'File "' // trim(handle%filename) // '", Field "' // trim(field%label) // '"') |
| 764 | 0 | end subroutine read_netcdf_field |
| 765 | ||
| 766 | ||
| 767 | !> Set the current timestep of an open netCDF file | |
| 768 | 54 | subroutine update_netcdf_timestep(handle, time) |
| 769 | type(netcdf_file_type), intent(inout) :: handle | |
| 770 | !< netCDF file handle | |
| 771 | real, intent(in) :: time | |
| 772 | !< New model time | |
| 773 | ||
| 774 | integer :: start(1) | |
| 775 | !< Time axis start index array | |
| 776 | integer :: rc | |
| 777 | !< netCDF return code | |
| 778 | ||
| 779 | 54 | if (time > handle%time + epsilon(time)) then |
| 780 | 3 | handle%time = time |
| 781 | 3 | handle%time_level = handle%time_level + 1 |
| 782 | ||
| 783 | ! Write new value to time axis | |
| 784 | 6 | start = [handle%time_level] |
| 785 | 3 | rc = nf90_put_var(handle%ncid, handle%time_id, time, start=start) |
| 786 | call check_netcdf_call(rc, 'update_netcdf_timestep', & | |
| 787 | 3 | 'File "' // handle%filename // '"') |
| 788 | endif | |
| 789 | 54 | end subroutine update_netcdf_timestep |
| 790 | ||
| 791 | ||
| 792 | !> Check netCDF function return codes, report the error log, and abort the run. | |
| 793 | 162 | subroutine check_netcdf_call(ncerr, header, message) |
| 794 | integer, intent(in) :: ncerr | |
| 795 | !< netCDF error code | |
| 796 | character(len=*), intent(in) :: header | |
| 797 | !< Message header (usually calling subroutine) | |
| 798 | character(len=*), intent(in) :: message | |
| 799 | !< Error message (usually action which instigated the error) | |
| 800 | ||
| 801 | 162 | character(len=:), allocatable :: errmsg |
| 802 | ! Full error message, including netCDF message | |
| 803 | ||
| 804 | 162 | if (ncerr /= NF90_NOERR) then |
| 805 | errmsg = trim(header) // ": " // trim(message) // new_line('/') & | |
| 806 | 0 | // trim(nf90_strerror(ncerr)) |
| 807 | 0 | call MOM_error(FATAL, errmsg) |
| 808 | endif | |
| 809 | 162 | end subroutine check_netcdf_call |
| 810 | ||
| 811 | 0 | end module MOM_netcdf |