Functions and Subroutines#
This page documents the public procedures re-exported by nc4f. Optional
err arguments receive an error_type; when omitted, an operation failure
follows the library’s fail-fast policy. See Exposed NC
Constants for constants used with error_type.
CLOSE_NETCDF – Close a NetCDF File#
Synopsis |
|---|
|
Class |
|---|
Subroutine. |
Description |
|---|
Close an open dataset and release its allocated model metadata. |
Arguments |
|---|
|
|
Example |
|---|
use, non_intrinsic :: nc4f
implicit none (type, external)
type(netcdf_type) :: nc
nc = open_netcdf("input.nc")
call close_netcdf(nc)
|
DATARRAY – Construct a Variable#
Synopsis |
|---|
|
Class |
|---|
Generic function. |
Description |
|---|
Construct a |
Arguments |
|---|
|
|
|
|
|
Return |
|---|
|
Example |
|---|
program main
use, non_intrinsic :: nc4f
implicit none (type, external)
type(dimension_type) :: lon, lat, tme
type(variable_type) :: var
logical, parameter :: UNLIMITED = .true.
real, allocatable, target :: values(:, :, :)
integer :: nlon, nlat, ntme, i, j, k
nlon = 360
nlat = 181
ntme = 24
allocate (values(ntme, nlat, nlon))
do concurrent (i = 1:ntme, j = 1:nlat, k = 1:nlon)
values(i, j, k) = 0. !> Dummy values
end do
var = datarray("temperature", values, &
dims=["time" .dim. (ntme .and. UNLIMITED), &
"latitude" .dim. nlat, &
"longitude" .dim. nlon], &
atts=["units" .att. "K", &
"description" .att. "Temperature in Kelvin", &
"missing value" .att. -1], &
deep=.false.)
print *, var
call to_netcdf("var.nc", var)
end program main
|
DATASET – Construct a Group#
Synopsis |
|---|
|
Class |
|---|
Generic function. |
Description |
|---|
Construct a |
Arguments |
|---|
|
|
|
|
|
Return |
|---|
|
Example |
|---|
use, non_intrinsic :: nc4f
implicit none (type, external)
type(dimension_type) :: point
type(variable_type) :: var
type(group_type) :: root
real, target :: values(1) = [273.15]
point = "point" .dim. 1
var = datarray("temperature", values, [point])
root = dataset("/", [var], atts=["title".att."Example dataset"])
|
EXTRACT – Access Model Data#
Synopsis |
|---|
|
Class |
|---|
Generic subroutine. |
Description |
|---|
Associate a typed Fortran pointer with an attribute or variable’s active byte storage. The pointer’s type and rank must match the stored NetCDF data and remains valid only while the source storage remains valid. |
Arguments |
|---|
|
|
|
Example |
|---|
use, non_intrinsic :: nc4f
implicit none (type, external)
type(dimension_type) :: point
type(variable_type) :: var
real, pointer :: values(:)
real, target :: source(1) = [273.15]
point = "point" .dim. 1
var = datarray("temperature", source, [point])
call extract(var, values)
values = values + 273.15
|
GET_ATTRIBUTE – Read Attributes#
Synopsis |
|---|
|
Class |
|---|
Generic function. |
Description |
|---|
Read one named global or variable attribute, or all attributes attached to a group or variable. |
Arguments |
|---|
|
|
|
|
Return |
|---|
|
Example |
|---|
use, non_intrinsic :: nc4f
implicit none (type, external)
type(netcdf_type) :: nc
type(group_type) :: atmosphere
type(variable_type) :: temperature
type(attribute_type) :: title, units
nc = open_netcdf("input.nc")
atmosphere = get_group(nc, "atmosphere")
temperature = inquire_variable(atmosphere, "temperature")
title = get_attribute(nc, "title")
units = get_attribute(atmosphere, temperature, "units")
|
GET_GROUP – Get a Child Group#
Synopsis |
|---|
|
Class |
|---|
Function. |
Description |
|---|
Return a direct child group by local name. |
Arguments |
|---|
|
|
|
Return |
|---|
|
Example |
|---|
use, non_intrinsic :: nc4f
implicit none (type, external)
type(netcdf_type) :: nc
type(group_type) :: atmosphere
nc = open_netcdf("input.nc")
atmosphere = get_group(nc, "atmosphere")
|
GET_VARIABLE – Read a Variable#
Synopsis |
|---|
|
Class |
|---|
Function. |
Description |
|---|
Read variable metadata and values. |
Arguments |
|---|
|
|
|
|
|
|
Return |
|---|
|
Example |
|---|
use, non_intrinsic :: nc4f
implicit none (type, external)
type(netcdf_type) :: nc
type(variable_type) :: pressure, slice
nc = open_netcdf("input.nc")
pressure = get_variable(nc, "P")
slice = get_variable(nc, "temperature", start=[1, 1], count=[12, 1])
|
INITIALIZE – Allocate Model Storage#
Synopsis |
|---|
|
Class |
|---|
Generic subroutine. |
Description |
|---|
Initialize a writable attribute or variable when values will be supplied later. From-scratch forms always allocate owned storage. Mold forms use the mold’s metadata and default to fresh owned storage; |
Arguments |
|---|
|
|
|
|
|
|
|
|
|
Example |
|---|
use, non_intrinsic :: nc4f
implicit none (type, external)
type(variable_type) :: total, output
real, pointer :: total_values(:), output_values(:)
call initialize(output, "mean_pressure", mold=total)
call extract(output, output_values)
output_values = 0.5 * total_values
|
INQUIRE_DIMENSIONS – Get Dimensions#
Synopsis |
|---|
|
Class |
|---|
Generic function. |
Description |
|---|
Return an allocatable array of a group’s dimensions or the dimensions attached to a variable, ordered for Fortran use. |
Arguments |
|---|
|
|
|
Return |
|---|
|
Example |
|---|
use, non_intrinsic :: nc4f
implicit none (type, external)
type(netcdf_type) :: nc
type(variable_type) :: temperature
type(dimension_type), allocatable :: dims(:), variable_dims(:)
nc = open_netcdf("input.nc")
temperature = inquire_variable(nc, "temperature")
dims = inquire_dimensions(nc)
variable_dims = inquire_dimensions(nc, temperature)
|
INQUIRE_GROUP – Materialize Group Metadata#
Synopsis |
|---|
|
Class |
|---|
Subroutine. |
Description |
|---|
Materialize selected local metadata in place. The |
Arguments |
|---|
|
|
|
|
|
|
|
Example |
|---|
use, non_intrinsic :: nc4f
implicit none (type, external)
type(group_type) :: atmosphere
call inquire_group(atmosphere, inq_dims=.true., inq_atts=.true., &
& inq_vars=.true., inq_grps=.true., recur=.true.)
|
INQUIRE_SUBGROUPS – List Child Groups#
Synopsis |
|---|
|
Class |
|---|
Function. |
Description |
|---|
Return an allocatable array of direct child groups with identifiers and names populated. |
Arguments |
|---|
|
|
Return |
|---|
|
Example |
|---|
use, non_intrinsic :: nc4f
implicit none (type, external)
type(netcdf_type) :: nc
type(group_type), allocatable :: children(:)
nc = open_netcdf("input.nc")
children = inquire_subgroups(nc)
|
INQUIRE_VARIABLE – Read Variable Metadata#
Synopsis |
|---|
|
Class |
|---|
Function. |
Description |
|---|
Return a variable’s metadata, including dimensions and attributes, without reading its data buffer. |
Arguments |
|---|
|
|
|
Return |
|---|
|
Example |
|---|
use, non_intrinsic :: nc4f
implicit none (type, external)
type(group_type) :: atmosphere
type(variable_type) :: temperature
temperature = inquire_variable(atmosphere, "temperature")
|
OPEN_NETCDF – Open a NetCDF File#
Synopsis |
|---|
|
Class |
|---|
Function. |
Description |
|---|
Open or create a NetCDF dataset. |
Arguments |
|---|
|
|
|
Return |
|---|
|
Example |
|---|
use, non_intrinsic :: nc4f
implicit none (type, external)
type(netcdf_type) :: nc
nc = open_netcdf("input.nc", mode="r")
|
SHAPE – Get Variable Extents#
Synopsis |
|---|
|
Class |
|---|
Function. |
Description |
|---|
Return allocatable default-integer dimension extents in Fortran order. |
Arguments |
|---|
|
Return |
|---|
|
Example |
|---|
use, non_intrinsic :: nc4f
implicit none (type, external)
type(variable_type) :: var
integer, allocatable :: extents(:)
extents = shape(var)
|
SIZE – Get Element Count#
Synopsis |
|---|
|
Class |
|---|
Function. |
Description |
|---|
Return the total element count as |
Arguments |
|---|
|
|
Return |
|---|
|
Example |
|---|
use, non_intrinsic :: nc4f
implicit none (type, external)
type(variable_type) :: var
integer(int64) :: elements
elements = size(var)
print *, size(var), size(var, dim=1)
|
SUM – Add Compatible Variables#
Synopsis |
|---|
|
Class |
|---|
Function. |
Description |
|---|
Return the element-wise sum of a rank-one array of compatible, materialized variables. The result uses the first variable’s metadata. |
Arguments |
|---|
|
Return |
|---|
|
Example |
|---|
use, non_intrinsic :: nc4f
implicit none (type, external)
type(variable_type) :: pressure, base_pressure, total
total = sum([pressure, base_pressure])
|
TO_NETCDF – Write a NetCDF File#
Synopsis |
|---|
|
Class |
|---|
Generic subroutine. |
Description |
|---|
Write a scalar or rank-one array of variables, or a scalar or rank-one array of groups. A scalar group becomes the file root and its name is ignored. A group array becomes direct children of a new root; no first-level group may be named |
Arguments |
|---|
|
|
|
|
|
Example |
|---|
use, non_intrinsic :: nc4f
implicit none (type, external)
type(group_type) :: root
type(attribute_type) :: title
title = "title" .att. "Output"
call to_netcdf("output.nc", root, atts=[title])
|