| 1 |
6 Variables {#f90-variables} |
| 2 |
=========== |
| 3 |
|
| 4 |
[TOC] |
| 5 |
|
| 6 |
6.1 Variables Introduction {#f90-variables-introduction} |
| 7 |
=========== |
| 8 |
|
| 9 |
Variables for a netCDF dataset are defined when the dataset is created, |
| 10 |
while the netCDF dataset is in define mode. Other variables may be added |
| 11 |
later by reentering define mode. A netCDF variable has a name, a type, |
| 12 |
and a shape, which are specified when it is defined. A variable may also |
| 13 |
have values, which are established later in data mode. |
| 14 |
|
| 15 |
Ordinarily, the name, type, and shape are fixed when the variable is |
| 16 |
first defined. The name may be changed, but the type and shape of a |
| 17 |
variable cannot be changed. However, a variable defined in terms of the |
| 18 |
unlimited dimension can grow without bound in that dimension. |
| 19 |
|
| 20 |
A netCDF variable in an open netCDF dataset is referred to by a small |
| 21 |
integer called a variable ID. |
| 22 |
|
| 23 |
Variable IDs reflect the order in which variables were defined within a |
| 24 |
netCDF dataset. Variable IDs are 1, 2, 3,..., in the order in which the |
| 25 |
variables were defined. A function is available for getting the variable |
| 26 |
ID from the variable name and vice-versa. |
| 27 |
|
| 28 |
Attributes (see [Attributes](#Attributes)) may be associated with a |
| 29 |
variable to specify such properties as units. |
| 30 |
|
| 31 |
Operations supported on variables are: |
| 32 |
|
| 33 |
- Create a variable, given its name, data type, and shape. |
| 34 |
- Get a variable ID from its name. |
| 35 |
- Get a variable’s name, data type, shape, and number of attributes |
| 36 |
from its ID. |
| 37 |
- Put a data value into a variable, given variable ID, indices, |
| 38 |
and value. |
| 39 |
- Put an array of values into a variable, given variable ID, corner |
| 40 |
indices, edge lengths, and a block of values. |
| 41 |
- Put a subsampled or mapped array-section of values into a variable, |
| 42 |
given variable ID, corner indices, edge lengths, stride vector, |
| 43 |
index mapping vector, and a block of values. |
| 44 |
- Get a data value from a variable, given variable ID and indices. |
| 45 |
- Get an array of values from a variable, given variable ID, corner |
| 46 |
indices, and edge lengths. |
| 47 |
- Get a subsampled or mapped array-section of values from a variable, |
| 48 |
given variable ID, corner indices, edge lengths, stride vector, and |
| 49 |
index mapping vector. |
| 50 |
- Rename a variable. |
| 51 |
|
| 52 |
|
| 53 |
6.2 Language Types Corresponding to netCDF external data types {#f90-language-types-corresponding-to-netcdf-external-data-types} |
| 54 |
=========== |
| 55 |
|
| 56 |
The following table gives the netCDF external data types and the |
| 57 |
corresponding type constants for defining variables in the FORTRAN |
| 58 |
interface: |
| 59 |
|
| 60 |
Type | FORTRAN API Mnemonic | Bits |
| 61 |
-------| ----------------------| ------ |
| 62 |
byte | NF90_BYTE | 8 |
| 63 |
char | NF90_CHAR | 8 |
| 64 |
short | NF90_SHORT | 16 |
| 65 |
int | NF90_INT | 32 |
| 66 |
float | NF90_FLOAT | 32 |
| 67 |
double | NF90_DOUBLE | 64 |
| 68 |
|
| 69 |
|
| 70 |
The first column gives the netCDF external data type, which is the same |
| 71 |
as the CDL data type. The next column gives the corresponding Fortran 90 |
| 72 |
parameter for use in netCDF functions (the parameters are defined in the |
| 73 |
netCDF Fortran 90 module netcdf.f90). The last column gives the number |
| 74 |
of bits used in the external representation of values of the |
| 75 |
corresponding type. |
| 76 |
|
| 77 |
Note that there are no netCDF types corresponding to 64-bit integers or |
| 78 |
to characters wider than 8 bits in the current version of the netCDF |
| 79 |
library. |
| 80 |
|
| 81 |
|
| 82 |
6.3 Create a Variable: `NF90_DEF_VAR` {#f90-create-a-variable-nf90_def_var} |
| 83 |
=========== |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
The function NF90\_DEF\_VAR adds a new variable to an open netCDF |
| 88 |
dataset in define mode. It returns (as an argument) a variable ID, given |
| 89 |
the netCDF ID, the variable name, the variable type, the number of |
| 90 |
dimensions, and a list of the dimension IDs. |
| 91 |
|
| 92 |
Optional arguments allow additional settings for variables in |
| 93 |
netCDF-4/HDF5 files. These parameters allow data compression and control |
| 94 |
of the layout of the data on disk for performance tuning. These |
| 95 |
parameters may also be used to set the chunk sizes to get chunked |
| 96 |
storage, or to set the contiguous flag to get contiguous storage. |
| 97 |
|
| 98 |
Variables that make use of one or more unlimited dimensions, |
| 99 |
compression, or checksums must use chunking. Such variables are created |
| 100 |
with default chunk sizes of 1 for each unlimited dimension and the |
| 101 |
dimension length for other dimensions, except that if the resulting |
| 102 |
chunks are too large, the default chunk sizes for non-record dimensions |
| 103 |
are reduced. |
| 104 |
|
| 105 |
All parameters after the varid are optional, and only supported if |
| 106 |
netCDF was built with netCDF-4 features enabled, and if the variable is |
| 107 |
in a netCDF-4/HDF5 file. |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
## Usage |
| 112 |
|
| 113 |
|
| 114 |
~~~~.fortran |
| 115 |
|
| 116 |
|
| 117 |
function nf90_def_var(ncid, name, xtype, dimids, varid, contiguous, & |
| 118 |
chunksizes, deflate_level, shuffle, fletcher32, endianness, & |
| 119 |
cache_size, cache_nelems, cache_preemption) |
| 120 |
integer, intent(in) :: ncid |
| 121 |
character (len = *), intent(in) :: name |
| 122 |
integer, intent( in) :: xtype |
| 123 |
integer, scalar or dimension(:), intent(in), optional :: dimids |
| 124 |
integer, intent(out) :: varid |
| 125 |
logical, optional, intent(in) :: contiguous |
| 126 |
integer, optional, dimension(:), intent(in) :: chunksizes |
| 127 |
integer, optional, intent(in) :: deflate_level |
| 128 |
logical, optional, intent(in) :: shuffle, fletcher32 |
| 129 |
integer, optional, intent(in) :: endianness |
| 130 |
integer, optional, intent(in) :: cache_size, cache_nelems, cache_preemption |
| 131 |
integer, optional, intent(in) :: quantize_mode, nsd |
| 132 |
integer :: nf90_def_var |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
~~~~ |
| 137 |
|
| 138 |
`ncid` |
| 139 |
|
| 140 |
: NetCDF ID, from a previous call to NF90\_OPEN or NF90\_CREATE. |
| 141 |
|
| 142 |
`name` |
| 143 |
|
| 144 |
: Variable name. |
| 145 |
|
| 146 |
`xtype` |
| 147 |
|
| 148 |
: One of the set of predefined netCDF external data types. The type of |
| 149 |
this parameter, is defined in the netCDF header file. |
| 150 |
|
| 151 |
The valid netCDF external data types are NF90\_BYTE, NF90\_CHAR, |
| 152 |
NF90\_SHORT, NF90\_INT, NF90\_FLOAT, and NF90\_DOUBLE. If the file |
| 153 |
is a NetCDF-4/HDF5 file, the additional types NF90\_UBYTE, |
| 154 |
NF90\_USHORT, NF90\_UINT, NF90\_INT64, NF90\_UINT64, and |
| 155 |
NF90\_STRING may be used, as well as a user defined type ID. |
| 156 |
|
| 157 |
`dimids` |
| 158 |
|
| 159 |
: Scalar or vector of dimension IDs corresponding to the |
| 160 |
variable dimensions. For example, a vector of 2 dimension IDs |
| 161 |
specifies a 2-dimensional matrix. |
| 162 |
|
| 163 |
If an integer is passed for this parameter, a 1-D variable |
| 164 |
is created. |
| 165 |
|
| 166 |
If this parameter is not passed (or is a 1D array of size zero) it |
| 167 |
means the variable is a scalar with no dimensions. |
| 168 |
|
| 169 |
For classic data model files, if the ID of the unlimited dimension |
| 170 |
is included, it must be first. In expanded model netCDF4/HDF5 files, |
| 171 |
there may be any number of unlimited dimensions, and they may be |
| 172 |
used in any element of the dimids array. |
| 173 |
|
| 174 |
This argument is optional, and if absent specifies a scalar with |
| 175 |
no dimensions. |
| 176 |
|
| 177 |
`varid` |
| 178 |
|
| 179 |
: Returned variable ID. |
| 180 |
|
| 181 |
`storage` |
| 182 |
|
| 183 |
: If NF90\_CONTIGUOUS, then contiguous storage is used for |
| 184 |
this variable. Variables that use deflation, shuffle filter, or |
| 185 |
checksums, or that have one or more unlimited dimensions cannot use |
| 186 |
contiguous storage. |
| 187 |
|
| 188 |
If NF90\_CHUNKED, then chunked storage is used for this variable. |
| 189 |
Chunk sizes may be specified with the chunksizes parameter. Default |
| 190 |
sizes will be used if chunking is required and this function is |
| 191 |
not called. |
| 192 |
|
| 193 |
If NF90\_COMPACT, then compact storage is used for this variable. |
| 194 |
Compact storage may only be used on fix-sized variables with a |
| 195 |
total data size 64 MB or less. Compact datasets are stored with |
| 196 |
the file metadata, and can be read very quickly on a large number |
| 197 |
of parallel processors. |
| 198 |
|
| 199 |
By default contiguous storage is used for fix-sized variables when |
| 200 |
conpression, chunking, shuffle, and checksums are not used. |
| 201 |
|
| 202 |
`chunksizes` |
| 203 |
|
| 204 |
: An array of chunk number of elements. This array has the number of |
| 205 |
elements along each dimension of the data chunk. The array must have |
| 206 |
the one chunksize for each dimension in the variable. |
| 207 |
|
| 208 |
The total size of a chunk must be less than 4 GiB. That is, the |
| 209 |
product of all chunksizes and the size of the data (or the size of |
| 210 |
nc\_vlen\_t for VLEN types) must be less than 4 GiB. (This is a very |
| 211 |
large chunk size in any case.) |
| 212 |
|
| 213 |
If not provided, but chunked data are needed, then default |
| 214 |
chunksizes will be chosen. For more information see [{No value for |
| 215 |
‘n-man’}](netcdf.html#Chunking) in {No value for ‘n-man’}. |
| 216 |
|
| 217 |
`shuffle` |
| 218 |
|
| 219 |
: If non-zero, turn on the shuffle filter. |
| 220 |
|
| 221 |
`deflate_level` |
| 222 |
|
| 223 |
: If the deflate parameter is non-zero, set the deflate level to |
| 224 |
this value. Must be between 1 and 9. |
| 225 |
|
| 226 |
`fletcher32` |
| 227 |
|
| 228 |
: Set to true to turn on fletcher32 checksums for this variable. |
| 229 |
|
| 230 |
`endianness` |
| 231 |
|
| 232 |
: Set to NF90\_ENDIAN\_LITTLE for little-endian format, |
| 233 |
NF90\_ENDIAN\_BIG for big-endian format, and NF90\_ENDIAN\_NATIVE |
| 234 |
(the default) for the native endianness of the platform. |
| 235 |
|
| 236 |
`cache_size` |
| 237 |
|
| 238 |
: The size of the per-variable cache in MegaBytes. |
| 239 |
|
| 240 |
`cache_nelems` |
| 241 |
|
| 242 |
: The number slots in the per-variable chunk cache (should be a prime |
| 243 |
number larger than the number of chunks in the cache). |
| 244 |
|
| 245 |
`cache_preemption` |
| 246 |
|
| 247 |
: The preemtion value must be between 0 and 100 inclusive and |
| 248 |
indicates how much chunks that have been fully read are favored |
| 249 |
for preemption. A value of zero means fully read chunks are treated |
| 250 |
no differently than other chunks (the preemption is strictly LRU) |
| 251 |
while a value of 100 means fully read chunks are always preempted |
| 252 |
before other chunks. |
| 253 |
|
| 254 |
`quantize_mode` |
| 255 |
|
| 256 |
: Set to nf90_noquantize (0) for no quantization (the default), or |
| 257 |
nf90_quantize_bitgroom (1) for bitgroom quantization. Quantization |
| 258 |
allows users to specify a number of significant digits (NSD) in |
| 259 |
nf90_float and nf90_double variables. Bits beyond those needed to |
| 260 |
maintain the NSD are set to zero or one (alternating). This causes |
| 261 |
the data to compress better when compression is applied. |
| 262 |
|
| 263 |
`nsd` |
| 264 |
|
| 265 |
: If quantize_mode is set to nf90_quantize_bitgroom, then this is the |
| 266 |
number of significant digits to be maintained in the data. |
| 267 |
|
| 268 |
## Return Codes |
| 269 |
|
| 270 |
NF90\_DEF\_VAR returns the value NF90\_NOERR if no errors occurred. |
| 271 |
Otherwise, the returned status indicates an error. |
| 272 |
|
| 273 |
- NF90\_EBADNAME The specified variable name is the name of another |
| 274 |
existing variable. |
| 275 |
- NF90\_EBADTYPE The specified type is not a valid netCDF type. |
| 276 |
- NF90\_EMAXDIMS The specified number of dimensions is negative or |
| 277 |
more than the constant NF90\_MAX\_VAR\_DIMS, the maximum number of |
| 278 |
dimensions permitted for a netCDF variable. (Does not apply to |
| 279 |
netCDF-4/HDF5 files unless they were created with the |
| 280 |
CLASSIC\_MODE flag.) |
| 281 |
- NF90\_EBADDIM One or more of the dimension IDs in the list of |
| 282 |
dimensions is not a valid dimension ID for the netCDF dataset. |
| 283 |
- NF90\_EMAXVARS The number of variables would exceed the constant |
| 284 |
NF90\_MAX\_VARS, the maximum number of variables permitted in a |
| 285 |
classic netCDF dataset. (Does not apply to netCDF-4/HDF5 files |
| 286 |
unless they were created with the CLASSIC\_MODE flag.) |
| 287 |
- NF90\_BADID The specified netCDF ID does not refer to an open |
| 288 |
netCDF dataset. |
| 289 |
- NF90\_ENOTNC4 NetCDF-4 operation attempted on a files that is not a |
| 290 |
netCDF-4/HDF5 file. Only variables in NetCDF-4/HDF5 files may use |
| 291 |
compression, chunking, and endianness control. |
| 292 |
- NF90\_ENOTVAR Can’t find this variable. |
| 293 |
- NF90\_EINVAL Invalid input. This may be because contiguous storage |
| 294 |
is requested for a variable that has compression, checksums, |
| 295 |
chunking, or one or more unlimited dimensions. |
| 296 |
- NF90\_ELATEDEF This variable has already been the subject of a |
| 297 |
NF90\_ENDDEF call. Once enddef has been called, it is impossible to |
| 298 |
set the chunking for a variable. (In netCDF-4/HDF5 files |
| 299 |
NF90\_ENDDEF will be called automatically for any data read |
| 300 |
or write.) |
| 301 |
- NF90\_ENOTINDEFINE Not in define mode. This is returned for netCDF |
| 302 |
classic or 64-bit offset files, or for netCDF-4 files, when they |
| 303 |
were been created with NF90\_STRICT\_NC3 flag. (see section |
| 304 |
[NF90\_CREATE](#NF90_005fCREATE)). |
| 305 |
- NF90\_ESTRICTNC3 Trying to create a var some place other than the |
| 306 |
root group in a netCDF file with NF90\_STRICT\_NC3 turned on. |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
### Example |
| 311 |
|
| 312 |
Here is an example using NF90\_DEF\_VAR to create a variable named rh of |
| 313 |
type double with three dimensions, time, lat, and lon in a new netCDF |
| 314 |
dataset named foo.nc: |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
~~~~.fortran |
| 319 |
|
| 320 |
use netcdf |
| 321 |
implicit none |
| 322 |
integer :: status, ncid |
| 323 |
integer :: LonDimId, LatDimId, TimeDimId |
| 324 |
integer :: RhVarId |
| 325 |
... |
| 326 |
status = nf90_create("foo.nc", nf90_NoClobber, ncid) |
| 327 |
if(status /= nf90_NoErr) call handle_error(status) |
| 328 |
... |
| 329 |
! Define the dimensions |
| 330 |
status = nf90_def_dim(ncid, "lat", 5, LatDimId) |
| 331 |
if(status /= nf90_NoErr) call handle_error(status) |
| 332 |
status = nf90_def_dim(ncid, "lon", 10, LonDimId) |
| 333 |
if(status /= nf90_NoErr) call handle_error(status) |
| 334 |
status = nf90_def_dim(ncid, "time", nf90_unlimited, TimeDimId) |
| 335 |
if(status /= nf90_NoErr) call handle_error(status) |
| 336 |
... |
| 337 |
! Define the variable |
| 338 |
status = nf90_def_var(ncid, "rh", nf90_double, & |
| 339 |
(/ LonDimId, LatDimID, TimeDimID /), RhVarId) |
| 340 |
if(status /= nf90_NoErr) call handle_error(status) |
| 341 |
|
| 342 |
|
| 343 |
~~~~ |
| 344 |
|
| 345 |
|
| 346 |
In the following example, from nf\_test/f90tst\_vars2.f90, chunking, |
| 347 |
checksums, and endianness control are all used in a netCDF-4/HDF5 file. |
| 348 |
|
| 349 |
|
| 350 |
~~~~.fortran |
| 351 |
|
| 352 |
|
| 353 |
! Create the netCDF file. |
| 354 |
call check(nf90_create(FILE_NAME, nf90_netcdf4, ncid, cache_nelems = CACHE_NELEMS, & |
| 355 |
cache_size = CACHE_SIZE)) |
| 356 |
|
| 357 |
! Define the dimensions. |
| 358 |
call check(nf90_def_dim(ncid, "x", NX, x_dimid)) |
| 359 |
call check(nf90_def_dim(ncid, "y", NY, y_dimid)) |
| 360 |
dimids = (/ y_dimid, x_dimid /) |
| 361 |
|
| 362 |
! Define some variables. |
| 363 |
chunksizes = (/ NY, NX /) |
| 364 |
call check(nf90_def_var(ncid, VAR1_NAME, NF90_INT, dimids, varid1, chunksizes = chunksizes, & |
| 365 |
shuffle = .TRUE., fletcher32 = .TRUE., endianness = nf90_endian_big, deflate_level = DEFLATE_LEVEL)) |
| 366 |
call check(nf90_def_var(ncid, VAR2_NAME, NF90_INT, dimids, varid2, contiguous = .TRUE.)) |
| 367 |
call check(nf90_def_var(ncid, VAR3_NAME, NF90_INT64, varid3)) |
| 368 |
call check(nf90_def_var(ncid, VAR4_NAME, NF90_INT, x_dimid, varid4, contiguous = .TRUE.)) |
| 369 |
|
| 370 |
|
| 371 |
~~~~ |
| 372 |
|
| 373 |
|
| 374 |
6.4 Define Fill Parameters for a Variable: `nf90_def_var_fill` {#f90-define-fill-parameters-for-a-variable-nf90_def_var_fill} |
| 375 |
=========== |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
The function NF90\_DEF\_VAR\_FILL sets the fill parameters for a |
| 380 |
variable in a netCDF-4 file. |
| 381 |
|
| 382 |
This function must be called after the variable is defined, but before |
| 383 |
NF90\_ENDDEF is called. |
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
## Usage |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
~~~~.fortran |
| 392 |
|
| 393 |
NF90_DEF_VAR_FILL(INTEGER NCID, INTEGER VARID, INTEGER NO_FILL, FILL_VALUE); |
| 394 |
|
| 395 |
~~~~ |
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
`NCID` |
| 400 |
|
| 401 |
: NetCDF ID, from a previous call to NF90\_OPEN or NF90\_CREATE. |
| 402 |
|
| 403 |
`VARID` |
| 404 |
|
| 405 |
: Variable ID. |
| 406 |
|
| 407 |
`NO_FILL` |
| 408 |
|
| 409 |
: Set to non-zero value to set no\_fill mode on a variable. When this |
| 410 |
mode is on, fill values will not be written for the variable. This |
| 411 |
is helpful in high performance applications. For netCDF-4/HDF5 files |
| 412 |
(whether classic model or not), this may only be changed after the |
| 413 |
variable is defined, but before it is committed to disk (i.e. before |
| 414 |
the first NF90\_ENDDEF after the NF90\_DEF\_VAR.) For classic and |
| 415 |
64-bit offset file, the no\_fill mode may be turned on and off at |
| 416 |
any time. |
| 417 |
|
| 418 |
`FILL_VALUE` |
| 419 |
|
| 420 |
: A value which will be used as the fill value for the variable. Must |
| 421 |
be the same type as the variable. This will be written to a |
| 422 |
\_FillValue attribute, created for this purpose. If NULL, this |
| 423 |
argument will be ignored. |
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
## Return Codes |
| 428 |
|
| 429 |
`NF90_NOERR` |
| 430 |
|
| 431 |
: No error. |
| 432 |
|
| 433 |
`NF90_BADID` |
| 434 |
|
| 435 |
: Bad ncid. |
| 436 |
|
| 437 |
`NF90_ENOTNC4` |
| 438 |
|
| 439 |
: Not a netCDF-4 file. |
| 440 |
|
| 441 |
`NF90_ENOTVAR` |
| 442 |
|
| 443 |
: Can’t find this variable. |
| 444 |
|
| 445 |
`NF90_ELATEDEF` |
| 446 |
|
| 447 |
: This variable has already been the subject of a NF90\_ENDDEF call. |
| 448 |
In netCDF-4 files NF90\_ENDDEF will be called automatically for any |
| 449 |
data read or write. Once enddef has been called, it is impossible to |
| 450 |
set the fill for a variable. |
| 451 |
|
| 452 |
`NF90_ENOTINDEFINE` |
| 453 |
|
| 454 |
: Not in define mode. This is returned for netCDF classic or 64-bit |
| 455 |
offset files, or for netCDF-4 files, when they were been created |
| 456 |
with NF90\_STRICT\_NC3 flag. (see section |
| 457 |
[NF90\_CREATE](#NF90_005fCREATE)). |
| 458 |
|
| 459 |
`NF90_EPERM` |
| 460 |
|
| 461 |
: Attempt to create object in read-only file. |
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
## Example |
| 466 |
|
| 467 |
|
| 468 |
6.5 Learn About Fill Parameters for a Variable: `NF90_INQ_VAR_FILL` {#f90-learn-about-fill-parameters-for-a-variable-nf90_inq_var_fill} |
| 469 |
=========== |
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
The function NF90\_INQ\_VAR\_FILL returns the fill settings for a |
| 474 |
variable in a netCDF-4 file. |
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
## Usage |
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
~~~~.fortran |
| 483 |
|
| 484 |
NF90_INQ_VAR_FILL(INTEGER NCID, INTEGER VARID, INTEGER NO_FILL, FILL_VALUE) |
| 485 |
|
| 486 |
~~~~ |
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
`NCID` |
| 491 |
|
| 492 |
: NetCDF ID, from a previous call to NF90\_OPEN or NF90\_CREATE. |
| 493 |
|
| 494 |
`VARID` |
| 495 |
|
| 496 |
: Variable ID. |
| 497 |
|
| 498 |
`NO_FILL` |
| 499 |
|
| 500 |
: An integer which will get a 1 if no\_fill mode is set for this |
| 501 |
variable, and a zero if it is not set |
| 502 |
|
| 503 |
`FILL_VALUE` |
| 504 |
|
| 505 |
: This will get the fill value for this variable. This parameter will |
| 506 |
be ignored if it is NULL. |
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
## Return Codes |
| 511 |
|
| 512 |
`NF90_NOERR` |
| 513 |
|
| 514 |
: No error. |
| 515 |
|
| 516 |
`NF90_BADID` |
| 517 |
|
| 518 |
: Bad ncid. |
| 519 |
|
| 520 |
`NF90_ENOTNC4` |
| 521 |
|
| 522 |
: Not a netCDF-4 file. |
| 523 |
|
| 524 |
`NF90_ENOTVAR` |
| 525 |
|
| 526 |
: Can’t find this variable. |
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
## Example |
| 531 |
|
| 532 |
6.6 Define Filter Parameters for a Variable: `nf90_def_var_filter` {#f90-define-filter-parameters-for-a-variable-nf90_def_var_filter} |
| 533 |
=========== |
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
The function nf90\_def\_var\_filter sets the filter parameters for a |
| 538 |
variable in a netCDF-4 file. |
| 539 |
|
| 540 |
This function must be called after the variable is defined, but before |
| 541 |
nf90\_enddef is called. |
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
## Usage |
| 546 |
|
| 547 |
|
| 548 |
~~~~.fortran |
| 549 |
|
| 550 |
function nf90_def_var_filter(ncid, varid, filterid, nparams, params); |
| 551 |
integer, intent(in) :: ncid, varid, filterid, nparams |
| 552 |
integer, dimension(:), intent(in) :: params |
| 553 |
|
| 554 |
~~~~ |
| 555 |
|
| 556 |
|
| 557 |
`ncid` |
| 558 |
|
| 559 |
: NetCDF ID, from a previous call to nf90\_open or nf90\_create. |
| 560 |
|
| 561 |
`varid` |
| 562 |
|
| 563 |
: Variable ID. |
| 564 |
|
| 565 |
`filterid` |
| 566 |
|
| 567 |
: The HDFGroup assigned filter id (e.g. 307 for bzip2). |
| 568 |
|
| 569 |
`nparams` |
| 570 |
|
| 571 |
: The number of parameters for the filter |
| 572 |
|
| 573 |
`params` |
| 574 |
|
| 575 |
: A vector of parameters of size nparams. |
| 576 |
|
| 577 |
|
| 578 |
## Return Codes |
| 579 |
|
| 580 |
`NF90_NOERR` |
| 581 |
|
| 582 |
: No error. |
| 583 |
|
| 584 |
`NF90_BADID` |
| 585 |
|
| 586 |
: Bad ncid. |
| 587 |
|
| 588 |
`NF90_ENOTNC4` |
| 589 |
|
| 590 |
: Not a netCDF-4 file. |
| 591 |
|
| 592 |
`NF90_ENOTVAR` |
| 593 |
|
| 594 |
: Can’t find this variable. |
| 595 |
|
| 596 |
`NF90_ELATEDEF` |
| 597 |
|
| 598 |
: This variable has already been the subject of a NF90\_ENDDEF call. |
| 599 |
In netCDF-4 files NF90\_ENDDEF will be called automatically for any |
| 600 |
data read or write. Once enddef has been called, it is impossible to |
| 601 |
set the fill for a variable. |
| 602 |
|
| 603 |
`NF90_ENOTINDEFINE` |
| 604 |
|
| 605 |
: Not in define mode. This is returned for netCDF classic or 64-bit |
| 606 |
offset files, or for netCDF-4 files, when they were been created |
| 607 |
with NF90\_STRICT\_NC3 flag. (see section |
| 608 |
[NF90\_CREATE](#NF90_005fCREATE)). |
| 609 |
|
| 610 |
`NF90_EPERM` |
| 611 |
|
| 612 |
: Attempt to create object in read-only file. |
| 613 |
|
| 614 |
`NF90_EFILTER` |
| 615 |
|
| 616 |
: Invalid filter id or parameters |
| 617 |
|
| 618 |
|
| 619 |
## Example |
| 620 |
|
| 621 |
6.7 Get information about any Filter associated with a variable: `nf90_inq_var_filter` {#f90-get-information-about-a-variable-from-its-id-nf90_inq_var_filter} |
| 622 |
=========== |
| 623 |
|
| 624 |
|
| 625 |
The function nf90\_inq\_var\_filter gets the filter id and parameters for a |
| 626 |
variable in a netCDF-4 file. |
| 627 |
|
| 628 |
|
| 629 |
## Usage |
| 630 |
|
| 631 |
|
| 632 |
~~~~.fortran |
| 633 |
|
| 634 |
function nf90_inq_var_filter(ncid, varid, filterid, nparams, params); |
| 635 |
integer, intent(in) :: ncid, varid, filterid, nparams |
| 636 |
integer, intent(out) :: filterid, nparams |
| 637 |
integer, dimension(:), intent(out) :: params |
| 638 |
|
| 639 |
~~~~ |
| 640 |
|
| 641 |
|
| 642 |
`ncid` |
| 643 |
|
| 644 |
: NetCDF ID, from a previous call to nf90\_open or nf90\_create. |
| 645 |
|
| 646 |
`varid` |
| 647 |
|
| 648 |
: Variable ID. |
| 649 |
|
| 650 |
`filterid` |
| 651 |
|
| 652 |
: Store the the HDFGroup assigned filter id |
| 653 |
|
| 654 |
`nparams` |
| 655 |
|
| 656 |
: Stor the number of parameters for the filter |
| 657 |
|
| 658 |
`params` |
| 659 |
|
| 660 |
: Store the vector of parameters of size nparams. |
| 661 |
|
| 662 |
## Return Codes |
| 663 |
|
| 664 |
`NF90_NOERR` |
| 665 |
|
| 666 |
: No error. |
| 667 |
|
| 668 |
`NF90_BADID` |
| 669 |
|
| 670 |
: Bad ncid. |
| 671 |
|
| 672 |
`NF90_ENOTNC4` |
| 673 |
|
| 674 |
: Not a netCDF-4 file. |
| 675 |
|
| 676 |
`NF90_ENOTVAR` |
| 677 |
|
| 678 |
: Can’t find this variable. |
| 679 |
|
| 680 |
`NF90_ELATEDEF` |
| 681 |
|
| 682 |
: This variable has already been the subject of a NF90\_ENDDEF call. |
| 683 |
In netCDF-4 files NF90\_ENDDEF will be called automatically for any |
| 684 |
data read or write. Once enddef has been called, it is impossible to |
| 685 |
set the fill for a variable. |
| 686 |
|
| 687 |
`NF90_ENOTINDEFINE` |
| 688 |
|
| 689 |
: Not in define mode. This is returned for netCDF classic or 64-bit |
| 690 |
offset files, or for netCDF-4 files, when they were been created |
| 691 |
with NF90\_STRICT\_NC3 flag. (see section |
| 692 |
[NF90\_CREATE](#NF90_005fCREATE)). |
| 693 |
|
| 694 |
`NF90_EPERM` |
| 695 |
|
| 696 |
: Attempt to create object in read-only file. |
| 697 |
|
| 698 |
`NF90_EFILTER` |
| 699 |
|
| 700 |
: Invalid filter id or parameters |
| 701 |
|
| 702 |
|
| 703 |
## Example |
| 704 |
|
| 705 |
|
| 706 |
6.8 Get Information about a Variable from Its ID: NF90_INQUIRE_VARIABLE {#f90-get-information-about-a-variable-from-its-id-nf90_inquire_variable} |
| 707 |
=========== |
| 708 |
|
| 709 |
NF90\_INQUIRE\_VARIABLE returns information about a netCDF variable |
| 710 |
given its ID. Information about a variable includes its name, type, |
| 711 |
number of dimensions, a list of dimension IDs describing the shape of |
| 712 |
the variable, and the number of variable attributes that have been |
| 713 |
assigned to the variable. |
| 714 |
|
| 715 |
All parameters after nAtts are optional, and only supported if netCDF |
| 716 |
was built with netCDF-4 features enabled, and if the variable is in a |
| 717 |
netCDF-4/HDF5 file. |
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
## Usage |
| 722 |
|
| 723 |
|
| 724 |
~~~~.fortran |
| 725 |
|
| 726 |
|
| 727 |
function nf90_inquire_variable(ncid, varid, name, xtype, ndims, dimids, nAtts, & |
| 728 |
contiguous, chunksizes, deflate_level, shuffle, fletcher32, endianness) |
| 729 |
integer, intent(in) :: ncid, varid |
| 730 |
character (len = *), optional, intent(out) :: name |
| 731 |
integer, optional, intent(out) :: xtype, ndims |
| 732 |
integer, dimension(:), optional, intent(out) :: dimids |
| 733 |
integer, optional, intent(out) :: nAtts |
| 734 |
logical, optional, intent(out) :: contiguous |
| 735 |
integer, optional, dimension(:), intent(out) :: chunksizes |
| 736 |
integer, optional, intent(out) :: deflate_level |
| 737 |
logical, optional, intent(out) :: shuffle, fletcher32 |
| 738 |
integer, optional, intent(out) :: endianness |
| 739 |
integer, optional, intent(in) :: quantize_mode, nsd |
| 740 |
integer :: nf90_inquire_variable |
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
~~~~ |
| 745 |
|
| 746 |
`ncid` |
| 747 |
|
| 748 |
: NetCDF ID, from a previous call to NF90\_OPEN or NF90\_CREATE. |
| 749 |
|
| 750 |
`varid` |
| 751 |
|
| 752 |
: Variable ID. |
| 753 |
|
| 754 |
`name` |
| 755 |
|
| 756 |
: Returned variable name. The caller must allocate space for the |
| 757 |
returned name. The maximum possible length, in characters, of a |
| 758 |
variable name is given by the predefined constant NF90\_MAX\_NAME. |
| 759 |
|
| 760 |
`xtype` |
| 761 |
|
| 762 |
: Returned variable type, one of the set of predefined netCDF external |
| 763 |
data types. The valid netCDF external data types are NF90\_BYTE, |
| 764 |
NF90\_CHAR, NF90\_SHORT, NF90\_INT, NF90\_FLOAT, AND NF90\_DOUBLE. |
| 765 |
|
| 766 |
`ndims` |
| 767 |
|
| 768 |
: Returned number of dimensions the variable was defined as using. For |
| 769 |
example, 2 indicates a matrix, 1 indicates a vector, and 0 means the |
| 770 |
variable is a scalar with no dimensions. |
| 771 |
|
| 772 |
`dimids` |
| 773 |
|
| 774 |
: Returned vector of \*ndimsp dimension IDs corresponding to the |
| 775 |
variable dimensions. The caller must allocate enough space for a |
| 776 |
vector of at least \*ndimsp integers to be returned. The maximum |
| 777 |
possible number of dimensions for a variable is given by the |
| 778 |
predefined constant NF90\_MAX\_VAR\_DIMS. |
| 779 |
|
| 780 |
`natts` |
| 781 |
|
| 782 |
: Returned number of variable attributes assigned to this variable. |
| 783 |
|
| 784 |
`contiguous` |
| 785 |
|
| 786 |
: On return, set to NF90\_CONTIGUOUS if this variable uses contiguous |
| 787 |
storage, NF90\_CHUNKED if it uses chunked storage. |
| 788 |
|
| 789 |
`chunksizes` |
| 790 |
|
| 791 |
: An array of chunk sizes. The array must have the one element for |
| 792 |
each dimension in the variable. |
| 793 |
|
| 794 |
`shuffle` |
| 795 |
|
| 796 |
: True if the shuffle filter is turned on for this variable. |
| 797 |
|
| 798 |
`deflate_level` |
| 799 |
|
| 800 |
: The deflate\_level from 0 to 9. A value of zero indicates no |
| 801 |
deflation is in use. |
| 802 |
|
| 803 |
`fletcher32` |
| 804 |
|
| 805 |
: Set to true if the fletcher32 checksum filter is turned on for |
| 806 |
this variable. |
| 807 |
|
| 808 |
`endianness` |
| 809 |
|
| 810 |
: Will be set to NF90\_ENDIAN\_LITTLE if this variable is stored in |
| 811 |
little-endian format, NF90\_ENDIAN\_BIG if it is stored in |
| 812 |
big-endian format, and NF90\_ENDIAN\_NATIVE if the endianness is not |
| 813 |
set, and the variable is not created yet. |
| 814 |
|
| 815 |
`quantize_mode` |
| 816 |
|
| 817 |
: Will be set to nf90_noquantize (0) for no quantization, or |
| 818 |
nf90_quantize_bitgroom (1) for bitgroom quantization. Quantization |
| 819 |
allows users to specify a number of significant digits (NSD) in |
| 820 |
nf90_float and nf90_double variables. Bits beyond those needed to |
| 821 |
maintain the NSD are set to zero or one (alternating). This causes |
| 822 |
the data to compress better when compression is applied. |
| 823 |
|
| 824 |
`nsd` |
| 825 |
|
| 826 |
: If quantize_mode is set to nf90_quantize_bitgroom, then this will be |
| 827 |
set to the number of significant digits to be maintained in the |
| 828 |
data. |
| 829 |
|
| 830 |
|
| 831 |
These functions return the value NF90\_NOERR if no errors occurred. |
| 832 |
Otherwise, the returned status indicates an error. Possible causes of |
| 833 |
errors include: |
| 834 |
|
| 835 |
- The variable ID is invalid for the specified netCDF dataset. |
| 836 |
- The specified netCDF ID does not refer to an open netCDF dataset. |
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
## Example |
| 841 |
|
| 842 |
Here is an example using NF90\_INQ\_VAR to find out about a variable |
| 843 |
named rh in an existing netCDF dataset named foo.nc: |
| 844 |
|
| 845 |
|
| 846 |
~~~~.fortran |
| 847 |
|
| 848 |
|
| 849 |
use netcdf |
| 850 |
implicit none |
| 851 |
integer :: status, ncid, & |
| 852 |
RhVarId & |
| 853 |
numDims, numAtts |
| 854 |
integer, dimension(nf90_max_var_dims) :: rhDimIds |
| 855 |
... |
| 856 |
status = nf90_open("foo.nc", nf90_NoWrite, ncid) |
| 857 |
if(status /= nf90_NoErr) call handle_error(status) |
| 858 |
... |
| 859 |
status = nf90_inq_varid(ncid, "rh", RhVarId) |
| 860 |
if(status /= nf90_NoErr) call handle_err(status) |
| 861 |
status = nf90_inquire_variable(ncid, RhVarId, ndims = numDims, natts = numAtts) |
| 862 |
if(status /= nf90_NoErr) call handle_err(status) |
| 863 |
status = nf90_inquire_variable(ncid, RhVarId, dimids = rhDimIds(:numDims)) |
| 864 |
if(status /= nf90_NoErr) call handle_err(status) |
| 865 |
|
| 866 |
|
| 867 |
~~~~ |
| 868 |
|
| 869 |
|
| 870 |
6.9 Get the ID of a variable from the name: NF90_INQ_VARID {#f90-get-the-id-of-a-variable-from-the-name-nf90_inq_varid} |
| 871 |
=========== |
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
Given the name of a varaible, nf90\_inq\_varid finds the variable ID. |
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
## Usage |
| 880 |
|
| 881 |
|
| 882 |
~~~~.fortran |
| 883 |
|
| 884 |
|
| 885 |
function nf90_inq_varid(ncid, name, varid) |
| 886 |
integer, intent(in) :: ncid |
| 887 |
character (len = *), intent( in) :: name |
| 888 |
integer, intent(out) :: varid |
| 889 |
integer :: nf90_inq_varid |
| 890 |
|
| 891 |
|
| 892 |
~~~~ |
| 893 |
|
| 894 |
|
| 895 |
`ncid` |
| 896 |
|
| 897 |
: NetCDF ID, from a previous call to NF90\_OPEN or NF90\_CREATE. |
| 898 |
|
| 899 |
`name` |
| 900 |
|
| 901 |
: The variable name. The maximum possible length, in characters, of a |
| 902 |
variable name is given by the predefined constant NF90\_MAX\_NAME. |
| 903 |
|
| 904 |
`varid` |
| 905 |
|
| 906 |
: Variable ID. |
| 907 |
|
| 908 |
These functions return the value NF90\_NOERR if no errors occurred. |
| 909 |
Otherwise, the returned status indicates an error. Possible causes of |
| 910 |
errors include: |
| 911 |
|
| 912 |
- Variable not found. |
| 913 |
- The specified netCDF ID does not refer to an open netCDF dataset. |
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
## Example |
| 918 |
|
| 919 |
Here is an example using NF90\_INQ\_VARID to find out about a variable |
| 920 |
named rh in an existing netCDF dataset named foo.nc: |
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
~~~~.fortran |
| 925 |
|
| 926 |
use netcdf |
| 927 |
implicit none |
| 928 |
integer :: status, ncid, & |
| 929 |
RhVarId & |
| 930 |
numDims, numAtts |
| 931 |
integer, dimension(nf90_max_var_dims) :: rhDimIds |
| 932 |
... |
| 933 |
status = nf90_open("foo.nc", nf90_NoWrite, ncid) |
| 934 |
if(status /= nf90_NoErr) call handle_error(status) |
| 935 |
... |
| 936 |
status = nf90_inq_varid(ncid, "rh", RhVarId) |
| 937 |
if(status /= nf90_NoErr) call handle_err(status) |
| 938 |
status = nf90_inquire_variable(ncid, RhVarId, ndims = numDims, natts = numAtts) |
| 939 |
if(status /= nf90_NoErr) call handle_err(status) |
| 940 |
status = nf90_inquire_variable(ncid, RhVarId, dimids = rhDimIds(:numDims)) |
| 941 |
if(status /= nf90_NoErr) call handle_err(status) |
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
~~~~ |
| 946 |
|
| 947 |
|
| 948 |
6.10 Writing Data Values: NF90_PUT_VAR {#f90-writing-data-values-nf90_put_var} |
| 949 |
=========== |
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
The function NF90\_PUT\_VAR puts one or more data values into the |
| 954 |
variable of an open netCDF dataset that is in data mode. Required inputs |
| 955 |
are the netCDF ID, the variable ID, and one or more data values. |
| 956 |
Optional inputs may indicate the starting position of the data values in |
| 957 |
the netCDF variable (argument start), the sampling frequency with which |
| 958 |
data values are written into the netCDF variable (argument stride), and |
| 959 |
a mapping between the dimensions of the data array and the netCDF |
| 960 |
variable (argument map). The values to be written are associated with |
| 961 |
the netCDF variable by assuming that the first dimension of the netCDF |
| 962 |
variable varies fastest in the Fortran 90 interface. Data values are |
| 963 |
converted to the external type of the variable, if necessary. |
| 964 |
|
| 965 |
Take care when using the simplest forms of this interface with record |
| 966 |
variables (variables that use the NF90\_UNLIMITED dimension) when you |
| 967 |
don’t specify how many records are to be written. If you try to write |
| 968 |
all the values of a record variable into a netCDF file that has no |
| 969 |
record data yet (hence has 0 records), nothing will be written. |
| 970 |
Similarly, if you try to write all the values of a record variable from |
| 971 |
an array but there are more records in the file than you assume, more |
| 972 |
in-memory data will be accessed than you expect, which may cause a |
| 973 |
segmentation violation. To avoid such problems, it is better to specify |
| 974 |
start and count arguments for variables that use the NF90\_UNLIMITED |
| 975 |
dimension. |
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
### Usage |
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
~~~~.fortran |
| 984 |
|
| 985 |
function nf90_put_var(ncid, varid, values, start, count, stride, map) |
| 986 |
integer, intent( in) :: ncid, varid |
| 987 |
any valid type, scalar or array of any rank, & |
| 988 |
intent( in) :: values |
| 989 |
integer, dimension(:), optional, intent( in) :: start, count, stride, map |
| 990 |
integer :: nf90_put_var |
| 991 |
|
| 992 |
|
| 993 |
~~~~ |
| 994 |
|
| 995 |
|
| 996 |
`ncid` |
| 997 |
|
| 998 |
: NetCDF ID, from a previous call to NF90\_OPEN or NF90\_CREATE. |
| 999 |
|
| 1000 |
`varid` |
| 1001 |
|
| 1002 |
: Variable ID. |
| 1003 |
|
| 1004 |
`values` |
| 1005 |
|
| 1006 |
: The data value(s) to be written. The data may be of any type, and |
| 1007 |
may be a scalar or an array of any rank. You cannot put CHARACTER |
| 1008 |
data into a numeric variable or numeric data into a text variable. |
| 1009 |
For numeric data, if the type of data differs from the netCDF |
| 1010 |
variable type, type conversion will occur. See [Type |
| 1011 |
Conversion](netcdf.html#Type-Conversion) in NetCDF Users Guide. |
| 1012 |
|
| 1013 |
`start` |
| 1014 |
|
| 1015 |
: A vector of integers specifying the index in the variable where the |
| 1016 |
first (or only) of the data values will be written. The indices are |
| 1017 |
relative to 1, so for example, the first data value of a variable |
| 1018 |
would have index (1, 1, ..., 1). The elements of start correspond, |
| 1019 |
in order, to the variable’s dimensions. Hence, if the variable is a |
| 1020 |
record variable, the last index would correspond to the starting |
| 1021 |
record number for writing the data values. |
| 1022 |
|
| 1023 |
By default, start(:) = 1. |
| 1024 |
|
| 1025 |
`count` |
| 1026 |
|
| 1027 |
: A vector of integers specifying the number of indices selected along |
| 1028 |
each dimension. To write a single value, for example, specify count |
| 1029 |
as (1, 1, ..., 1). The elements of count correspond, in order, to |
| 1030 |
the variable’s dimensions. Hence, if the variable is a record |
| 1031 |
variable, the last element of count corresponds to a count of the |
| 1032 |
number of records to write. |
| 1033 |
|
| 1034 |
By default, count(:numDims) = shape(values) and count(numDims + 1:) |
| 1035 |
= 1, where numDims = size(shape(values)). |
| 1036 |
|
| 1037 |
`stride` |
| 1038 |
|
| 1039 |
: A vector of integers that specifies the sampling interval along each |
| 1040 |
dimension of the netCDF variable. The elements of the stride vector |
| 1041 |
correspond, in order, to the netCDF variable’s dimensions (stride(1) |
| 1042 |
gives the sampling interval along the most rapidly varying dimension |
| 1043 |
of the netCDF variable). Sampling intervals are specified in |
| 1044 |
type-independent units of elements (a value of 1 selects consecutive |
| 1045 |
elements of the netCDF variable along the corresponding dimension, a |
| 1046 |
value of 2 selects every other element, etc.). |
| 1047 |
|
| 1048 |
By default, stride(:) = 1. |
| 1049 |
|
| 1050 |
`imap` |
| 1051 |
|
| 1052 |
: A vector of integers that specifies the mapping between the |
| 1053 |
dimensions of a netCDF variable and the in-memory structure of the |
| 1054 |
internal data array. The elements of the index mapping vector |
| 1055 |
correspond, in order, to the netCDF variable’s dimensions (map(1) |
| 1056 |
gives the distance between elements of the internal array |
| 1057 |
corresponding to the most rapidly varying dimension of the |
| 1058 |
netCDF variable). Distances between elements are specified in units |
| 1059 |
of elements. |
| 1060 |
|
| 1061 |
By default, edgeLengths = shape(values), and map = (/ 1, |
| 1062 |
(product(edgeLengths(:i)), i = 1, size(edgeLengths) - 1) /), that |
| 1063 |
is, there is no mapping. |
| 1064 |
|
| 1065 |
Use of Fortran 90 intrinsic functions (including reshape, transpose, |
| 1066 |
and spread) may let you avoid using this argument. |
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
## Errors |
| 1071 |
|
| 1072 |
NF90\_PUT\_VAR1\_ type returns the value NF90\_NOERR if no errors |
| 1073 |
occurred. Otherwise, the returned status indicates an error. Possible |
| 1074 |
causes of errors include: |
| 1075 |
|
| 1076 |
- The variable ID is invalid for the specified netCDF dataset. |
| 1077 |
- The specified indices were out of range for the rank of the |
| 1078 |
specified variable. For example, a negative index or an index that |
| 1079 |
is larger than the corresponding dimension length will cause |
| 1080 |
an error. |
| 1081 |
- The specified value is out of the range of values representable by |
| 1082 |
the external data type of the variable. |
| 1083 |
- The specified netCDF is in define mode rather than data mode. |
| 1084 |
- The specified netCDF ID does not refer to an open netCDF dataset. |
| 1085 |
|
| 1086 |
|
| 1087 |
|
| 1088 |
## Example |
| 1089 |
|
| 1090 |
Here is an example using NF90\_PUT\_VAR to set the (4,3,2) element of |
| 1091 |
the variable named rh to 0.5 in an existing netCDF dataset named foo.nc. |
| 1092 |
For simplicity in this example, we assume that we know that rh is |
| 1093 |
dimensioned with lon, lat, and time, so we want to set the value of rh |
| 1094 |
that corresponds to the fourth lon value, the third lat value, and the |
| 1095 |
second time value: |
| 1096 |
|
| 1097 |
|
| 1098 |
|
| 1099 |
~~~~.fortran |
| 1100 |
|
| 1101 |
use netcdf |
| 1102 |
implicit none |
| 1103 |
integer :: ncId, rhVarId, status |
| 1104 |
... |
| 1105 |
status = nf90_open("foo.nc", nf90_Write, ncid) |
| 1106 |
if(status /= nf90_NoErr) call handle_err(status) |
| 1107 |
... |
| 1108 |
status = nf90_inq_varid(ncid, "rh", rhVarId) |
| 1109 |
if(status /= nf90_NoErr) call handle_err(status) |
| 1110 |
status = nf90_put_var(ncid, rhVarId, 0.5, start = (/ 4, 3, 2 /) ) |
| 1111 |
if(status /= nf90_NoErr) call handle_err(status) |
| 1112 |
|
| 1113 |
|
| 1114 |
~~~~ |
| 1115 |
|
| 1116 |
|
| 1117 |
In this example we use NF90\_PUT\_VAR to add or change all the values of |
| 1118 |
the variable named rh to 0.5 in an existing netCDF dataset named foo.nc. |
| 1119 |
We assume that we know that rh is dimensioned with lon, lat, and time. |
| 1120 |
In this example we query the netCDF file to discover the lengths of the |
| 1121 |
dimensions, then use the Fortran 90 intrinsic function reshape to create |
| 1122 |
a temporary array of data values which is the same shape as the netCDF |
| 1123 |
variable. |
| 1124 |
|
| 1125 |
|
| 1126 |
~~~~.fortran |
| 1127 |
|
| 1128 |
|
| 1129 |
use netcdf |
| 1130 |
implicit none |
| 1131 |
integer :: ncId, rhVarId,status, & |
| 1132 |
lonDimID, latDimId, timeDimId, & |
| 1133 |
numLons, numLats, numTimes, & |
| 1134 |
i |
| 1135 |
integer, dimension(nf90_max_var_dims) :: dimIDs |
| 1136 |
... |
| 1137 |
status = nf90_open("foo.nc", nf90_Write, ncid) |
| 1138 |
if(status /= nf90_NoErr) call handle_err(status) |
| 1139 |
... |
| 1140 |
status = nf90_inq_varid(ncid, "rh", rhVarId) |
| 1141 |
if(status /= nf90_NoErr) call handle_err(status) |
| 1142 |
! How big is the netCDF variable, that is, what are the lengths of |
| 1143 |
! its constituent dimensions? |
| 1144 |
status = nf90_inquire_variable(ncid, rhVarId, dimids = dimIDs) |
| 1145 |
if(status /= nf90_NoErr) call handle_err(status) |
| 1146 |
status = nf90_inquire_dimension(ncid, dimIDs(1), len = numLons) |
| 1147 |
if(status /= nf90_NoErr) call handle_err(status) |
| 1148 |
status = nf90_inquire_dimension(ncid, dimIDs(2), len = numLats) |
| 1149 |
if(status /= nf90_NoErr) call handle_err(status) |
| 1150 |
status = nf90_inquire_dimension(ncid, dimIDs(3), len = numTimes) |
| 1151 |
if(status /= nf90_NoErr) call handle_err(status) |
| 1152 |
... |
| 1153 |
! Make a temporary array the same shape as the netCDF variable. |
| 1154 |
status = nf90_put_var(ncid, rhVarId, & |
| 1155 |
reshape( & |
| 1156 |
(/ (0.5, i = 1, numLons * numLats * numTimes) /) , & |
| 1157 |
shape = (/ numLons, numLats, numTimes /) ) |
| 1158 |
if(status /= nf90_NoErr) call handle_err(status) |
| 1159 |
|
| 1160 |
|
| 1161 |
~~~~ |
| 1162 |
|
| 1163 |
|
| 1164 |
Here is an example using NF90\_PUT\_VAR to add or change a section of |
| 1165 |
the variable named rh to 0.5 in an existing netCDF dataset named foo.nc. |
| 1166 |
For simplicity in this example, we assume that we know that rh is |
| 1167 |
dimensioned with lon, lat, and time, that there are ten lon values, five |
| 1168 |
lat values, and three time values, and that we want to replace all the |
| 1169 |
values at the last time. |
| 1170 |
|
| 1171 |
|
| 1172 |
|
| 1173 |
~~~~.fortran |
| 1174 |
|
| 1175 |
use netcdf |
| 1176 |
implicit none |
| 1177 |
integer :: ncId, rhVarId, status |
| 1178 |
integer, parameter :: numLons = 10, numLats = 5, numTimes = 3 |
| 1179 |
real, dimension(numLons, numLats) & |
| 1180 |
:: rhValues |
| 1181 |
... |
| 1182 |
status = nf90_open("foo.nc", nf90_Write, ncid) |
| 1183 |
if(status /= nf90_NoErr) call handle_err(status) |
| 1184 |
... |
| 1185 |
status = nf90_inq_varid(ncid, "rh", rhVarId) |
| 1186 |
if(status /= nf90_NoErr) call handle_err(status) |
| 1187 |
! Fill in all values at the last time |
| 1188 |
rhValues(:, :) = 0.5 |
| 1189 |
status = nf90_put_var(ncid, rhVarId,rhvalues, & |
| 1190 |
start = (/ 1, 1, numTimes /), & |
| 1191 |
count = (/ numLats, numLons, 1 /)) |
| 1192 |
if(status /= nf90_NoErr) call handle_err(status) |
| 1193 |
|
| 1194 |
|
| 1195 |
~~~~ |
| 1196 |
|
| 1197 |
|
| 1198 |
Here is an example of using NF90\_PUT\_VAR to write every other point of |
| 1199 |
a netCDF variable named rh having dimensions (6, 4). |
| 1200 |
|
| 1201 |
|
| 1202 |
|
| 1203 |
~~~~.fortran |
| 1204 |
|
| 1205 |
use netcdf |
| 1206 |
implicit none |
| 1207 |
integer :: ncId, rhVarId, status |
| 1208 |
integer, parameter :: numLons = 6, numLats = 4 |
| 1209 |
real, dimension(numLons, numLats) & |
| 1210 |
:: rhValues = 0.5 |
| 1211 |
... |
| 1212 |
status = nf90_open("foo.nc", nf90_Write, ncid) |
| 1213 |
if(status /= nf90_NoErr) call handle_err(status) |
| 1214 |
... |
| 1215 |
status = nf90_inq_varid(ncid, "rh", rhVarId) |
| 1216 |
if(status /= nf90_NoErr) call handle_err(status) |
| 1217 |
... |
| 1218 |
! Fill in every other value using an array section |
| 1219 |
status = nf90_put_var(ncid, rhVarId, rhValues(::2, ::2), & |
| 1220 |
stride = (/ 2, 2 /)) |
| 1221 |
if(status /= nf90_NoErr) call handle_err(status) |
| 1222 |
|
| 1223 |
|
| 1224 |
~~~~ |
| 1225 |
|
| 1226 |
|
| 1227 |
The following map vector shows the default mapping between a 2x3x4 |
| 1228 |
netCDF variable and an internal array of the same shape: |
| 1229 |
|
| 1230 |
|
| 1231 |
~~~~.fortran |
| 1232 |
|
| 1233 |
|
| 1234 |
real, dimension(2, 3, 4):: a ! same shape as netCDF variable |
| 1235 |
integer, dimension(3) :: map = (/ 1, 2, 6 /) |
| 1236 |
! netCDF dimension inter-element distance |
| 1237 |
! ---------------- ---------------------- |
| 1238 |
! most rapidly varying 1 |
| 1239 |
! intermediate 2 (= map(1)*2) |
| 1240 |
! most slowly varying 6 (= map(2)*3) |
| 1241 |
|
| 1242 |
|
| 1243 |
~~~~ |
| 1244 |
|
| 1245 |
|
| 1246 |
Using the map vector above obtains the same result as simply not passing |
| 1247 |
a map vector at all. |
| 1248 |
|
| 1249 |
Here is an example of using nf90\_put\_var to write a netCDF variable |
| 1250 |
named rh whose dimensions are the transpose of the Fortran 90 array: |
| 1251 |
|
| 1252 |
|
| 1253 |
~~~~.fortran |
| 1254 |
|
| 1255 |
|
| 1256 |
use netcdf |
| 1257 |
implicit none |
| 1258 |
integer :: ncId, rhVarId, status |
| 1259 |
integer, parameter :: numLons = 6, numLats = 4 |
| 1260 |
real, dimension(numLons, numLats) :: rhValues |
| 1261 |
! netCDF variable has dimensions (numLats, numLons) |
| 1262 |
... |
| 1263 |
status = nf90_open("foo.nc", nf90_Write, ncid) |
| 1264 |
if(status /= nf90_NoErr) call handle_err(status) |
| 1265 |
... |
| 1266 |
status = nf90_inq_varid(ncid, "rh", rhVarId) |
| 1267 |
if(status /= nf90_NoErr) call handle_err(status) |
| 1268 |
... |
| 1269 |
!Write transposed values: map vector would be (/ 1, numLats /) for |
| 1270 |
! no transposition |
| 1271 |
status = nf90_put_var(ncid, rhVarId,rhValues, map = (/ numLons, 1 /)) |
| 1272 |
if(status /= nf90_NoErr) call handle_err(status) |
| 1273 |
|
| 1274 |
|
| 1275 |
~~~~ |
| 1276 |
|
| 1277 |
|
| 1278 |
The same effect can be obtained more simply using Fortran 90 intrinsic |
| 1279 |
functions: |
| 1280 |
|
| 1281 |
|
| 1282 |
|
| 1283 |
~~~~.fortran |
| 1284 |
|
| 1285 |
use netcdf |
| 1286 |
implicit none |
| 1287 |
integer :: ncId, rhVarId, status |
| 1288 |
integer, parameter :: numLons = 6, numLats = 4 |
| 1289 |
real, dimension(numLons, numLats) :: rhValues |
| 1290 |
! netCDF variable has dimensions (numLats, numLons) |
| 1291 |
... |
| 1292 |
status = nf90_open("foo.nc", nf90_Write, ncid) |
| 1293 |
if(status /= nf90_NoErr) call handle_err(status) |
| 1294 |
... |
| 1295 |
status = nf90_inq_varid(ncid, "rh", rhVarId) |
| 1296 |
if(status /= nf90_NoErr) call handle_err(status) |
| 1297 |
... |
| 1298 |
status = nf90_put_var(ncid, rhVarId, transpose(rhValues)) |
| 1299 |
if(status /= nf90_NoErr) call handle_err(status) |
| 1300 |
|
| 1301 |
|
| 1302 |
~~~~ |
| 1303 |
|
| 1304 |
|
| 1305 |
|
| 1306 |
6.11 Reading Data Values: NF90_GET_VAR {#f90-reading-data-values-nf90_get_var} |
| 1307 |
=========== |
| 1308 |
|
| 1309 |
|
| 1310 |
|
| 1311 |
The function NF90\_GET\_VAR gets one or more data values from a netCDF |
| 1312 |
variable of an open netCDF dataset that is in data mode. Required inputs |
| 1313 |
are the netCDF ID, the variable ID, and a specification for the data |
| 1314 |
values into which the data will be read. Optional inputs may indicate |
| 1315 |
the starting position of the data values in the netCDF variable |
| 1316 |
(argument start), the sampling frequency with which data values are read |
| 1317 |
from the netCDF variable (argument stride), and a mapping between the |
| 1318 |
dimensions of the data array and the netCDF variable (argument map). The |
| 1319 |
values to be read are associated with the netCDF variable by assuming |
| 1320 |
that the first dimension of the netCDF variable varies fastest in the |
| 1321 |
Fortran 90 interface. Data values are converted from the external type |
| 1322 |
of the variable, if necessary. |
| 1323 |
|
| 1324 |
Take care when using the simplest forms of this interface with record |
| 1325 |
variables (variables that use the NF90\_UNLIMITED dimension) when you |
| 1326 |
don’t specify how many records are to be read. If you try to read all |
| 1327 |
the values of a record variable into an array but there are more records |
| 1328 |
in the file than you assume, more data will be read than you expect, |
| 1329 |
which may cause a segmentation violation. To avoid such problems, it is |
| 1330 |
better to specify the optional start and count arguments for variables |
| 1331 |
that use the NF90\_UNLIMITED dimension. |
| 1332 |
|
| 1333 |
In netCDF classic model the maximum integer size is NF90\_INT, the |
| 1334 |
4-byte signed integer. Reading variables into an eight-byte integer |
| 1335 |
array from a classic model file will read from an NF90\_INT. Reading |
| 1336 |
variables into an eight-byte integer in a netCDF-4/HDF5 (without classic |
| 1337 |
model flag) will read from an NF90\_INT64 |
| 1338 |
|
| 1339 |
|
| 1340 |
|
| 1341 |
## Usage |
| 1342 |
|
| 1343 |
|
| 1344 |
|
| 1345 |
~~~~.fortran |
| 1346 |
|
| 1347 |
function nf90_get_var(ncid, varid, values, start, count, stride, map) |
| 1348 |
integer, intent( in) :: ncid, varid |
| 1349 |
any valid type, scalar or array of any rank, & |
| 1350 |
intent(out) :: values |
| 1351 |
integer, dimension(:), optional, intent( in) :: start, count, stride, map |
| 1352 |
integer :: nf90_get_var |
| 1353 |
|
| 1354 |
|
| 1355 |
~~~~ |
| 1356 |
|
| 1357 |
|
| 1358 |
`ncid` |
| 1359 |
|
| 1360 |
: NetCDF ID, from a previous call to NF90\_OPEN or NF90\_CREATE. |
| 1361 |
|
| 1362 |
`varid` |
| 1363 |
|
| 1364 |
: Variable ID. |
| 1365 |
|
| 1366 |
`values` |
| 1367 |
|
| 1368 |
: The data value(s) to be read. The data may be of any type, and may |
| 1369 |
be a scalar or an array of any rank. You cannot read CHARACTER data |
| 1370 |
from a numeric variable or numeric data from a text variable. For |
| 1371 |
numeric data, if the type of data differs from the netCDF variable |
| 1372 |
type, type conversion will occur. See [Type |
| 1373 |
Conversion](netcdf.html#Type-Conversion) in NetCDF Users Guide. |
| 1374 |
|
| 1375 |
`start` |
| 1376 |
|
| 1377 |
: A vector of integers specifying the index in the variable from which |
| 1378 |
the first (or only) of the data values will be read. The indices are |
| 1379 |
relative to 1, so for example, the first data value of a variable |
| 1380 |
would have index (1, 1, ..., 1). The elements of start correspond, |
| 1381 |
in order, to the variable’s dimensions. Hence, if the variable is a |
| 1382 |
record variable, the last index would correspond to the starting |
| 1383 |
record number for writing the data values. |
| 1384 |
|
| 1385 |
By default, start(:) = 1. |
| 1386 |
|
| 1387 |
`count` |
| 1388 |
|
| 1389 |
: A vector of integers specifying the number of indices selected along |
| 1390 |
each dimension. To read a single value, for example, specify count |
| 1391 |
as (1, 1, ..., 1). The elements of count correspond, in order, to |
| 1392 |
the variable’s dimensions. Hence, if the variable is a record |
| 1393 |
variable, the last element of count corresponds to a count of the |
| 1394 |
number of records to read. |
| 1395 |
|
| 1396 |
By default, count(:numDims) = shape(values) and count(numDims + 1:) |
| 1397 |
= 1, where numDims = size(shape(values)). |
| 1398 |
|
| 1399 |
`stride` |
| 1400 |
|
| 1401 |
: A vector of integers that specifies the sampling interval along each |
| 1402 |
dimension of the netCDF variable. The elements of the stride vector |
| 1403 |
correspond, in order, to the netCDF variable’s dimensions (stride(1) |
| 1404 |
gives the sampling interval along the most rapidly varying dimension |
| 1405 |
of the netCDF variable). Sampling intervals are specified in |
| 1406 |
type-independent units of elements (a value of 1 selects consecutive |
| 1407 |
elements of the netCDF variable along the corresponding dimension, a |
| 1408 |
value of 2 selects every other element, etc.). |
| 1409 |
|
| 1410 |
By default, stride(:) = 1. |
| 1411 |
|
| 1412 |
`map` |
| 1413 |
|
| 1414 |
: A vector of integers that specifies the mapping between the |
| 1415 |
dimensions of a netCDF variable and the in-memory structure of the |
| 1416 |
internal data array. The elements of the index mapping vector |
| 1417 |
correspond, in order, to the netCDF variable’s dimensions (map(1) |
| 1418 |
gives the distance between elements of the internal array |
| 1419 |
corresponding to the most rapidly varying dimension of the |
| 1420 |
netCDF variable). Distances between elements are specified in units |
| 1421 |
of elements. |
| 1422 |
|
| 1423 |
By default, edgeLengths = shape(values), and map = (/ 1, |
| 1424 |
(product(edgeLengths(:i)), i = 1, size(edgeLengths) - 1) /), that |
| 1425 |
is, there is no mapping. |
| 1426 |
|
| 1427 |
Use of Fortran 90 intrinsic functions (including reshape, transpose, |
| 1428 |
and spread) may let you avoid using this argument. |
| 1429 |
|
| 1430 |
|
| 1431 |
|
| 1432 |
## Errors |
| 1433 |
|
| 1434 |
NF90\_GET\_VAR returns the value NF90\_NOERR if no errors occurred. |
| 1435 |
Otherwise, the returned status indicates an error. Possible causes of |
| 1436 |
errors include: |
| 1437 |
|
| 1438 |
- The variable ID is invalid for the specified netCDF dataset. |
| 1439 |
- The assumed or specified start, count, and stride generate an index |
| 1440 |
which is out of range. Note that no error checking is possible on |
| 1441 |
the map vector. |
| 1442 |
- One or more of the specified values are out of the range of values |
| 1443 |
representable by the desired type. |
| 1444 |
- The specified netCDF is in define mode rather than data mode. |
| 1445 |
- The specified netCDF ID does not refer to an open netCDF dataset. |
| 1446 |
|
| 1447 |
(As noted above, another possible source of error is using this |
| 1448 |
interface to read all the values of a record variable without specifying |
| 1449 |
the number of records. If there are more records in the file than you |
| 1450 |
assume, more data will be read than you expect!) |
| 1451 |
|
| 1452 |
|
| 1453 |
|
| 1454 |
## Example |
| 1455 |
|
| 1456 |
Here is an example using NF90\_GET\_VAR to read the (4,3,2) element of |
| 1457 |
the variable named rh from an existing netCDF dataset named foo.nc. For |
| 1458 |
simplicity in this example, we assume that we know that rh is |
| 1459 |
dimensioned with lon, lat, and time, so we want to read the value of rh |
| 1460 |
that corresponds to the fourth lon value, the third lat value, and the |
| 1461 |
second time value: |
| 1462 |
|
| 1463 |
|
| 1464 |
|
| 1465 |
~~~~.fortran |
| 1466 |
|
| 1467 |
use netcdf |
| 1468 |
implicit none |
| 1469 |
integer :: ncId, rhVarId, status |
| 1470 |
real :: rhValue |
| 1471 |
... |
| 1472 |
status = nf90_open("foo.nc", nf90_NoWrite, ncid) |
| 1473 |
if(status /= nf90_NoErr) call handle_err(status) |
| 1474 |
- |
| 1475 |
status = nf90_inq_varid(ncid, "rh", rhVarId) |
| 1476 |
if(status /= nf90_NoErr) call handle_err(status) |
| 1477 |
status = nf90_get_var(ncid, rhVarId, rhValue, start = (/ 4, 3, 2 /) ) |
| 1478 |
if(status /= nf90_NoErr) call handle_err(status) |
| 1479 |
|
| 1480 |
|
| 1481 |
~~~~ |
| 1482 |
|
| 1483 |
|
| 1484 |
In this example we use NF90\_GET\_VAR to read all the values of the |
| 1485 |
variable named rh from an existing netCDF dataset named foo.nc. We |
| 1486 |
assume that we know that rh is dimensioned with lon, lat, and time. In |
| 1487 |
this example we query the netCDF file to discover the lengths of the |
| 1488 |
dimensions, then allocate a Fortran 90 array the same shape as the |
| 1489 |
netCDF variable. |
| 1490 |
|
| 1491 |
|
| 1492 |
|
| 1493 |
|
| 1494 |
use netcdf |
| 1495 |
implicit none |
| 1496 |
integer :: ncId, rhVarId, & |
| 1497 |
lonDimID, latDimId, timeDimId, & |
| 1498 |
numLons, numLats, numTimes, & |
| 1499 |
status |
| 1500 |
integer, dimension(nf90_max_var_dims) :: dimIDs |
| 1501 |
real, dimension(:, :, :), allocatable :: rhValues |
| 1502 |
... |
| 1503 |
status = nf90_open("foo.nc", nf90_NoWrite, ncid) |
| 1504 |
if(status /= nf90_NoErr) call handle_err(status) |
| 1505 |
... |
| 1506 |
status = nf90_inq_varid(ncid, "rh", rhVarId) |
| 1507 |
if(status /= nf90_NoErr) call handle_err(status) |
| 1508 |
! How big is the netCDF variable, that is, what are the lengths of |
| 1509 |
! its constituent dimensions? |
| 1510 |
status = nf90_inquire_variable(ncid, rhVarId, dimids = dimIDs) |
| 1511 |
if(status /= nf90_NoErr) call handle_err(status) |
| 1512 |
status = nf90_inquire_dimension(ncid, dimIDs(1), len = numLons) |
| 1513 |
if(status /= nf90_NoErr) call handle_err(status) |
| 1514 |
status = nf90_inquire_dimension(ncid, dimIDs(2), len = numLats) |
| 1515 |
if(status /= nf90_NoErr) call handle_err(status) |
| 1516 |
status = nf90_inquire_dimension(ncid, dimIDs(3), len = numTimes) |
| 1517 |
if(status /= nf90_NoErr) call handle_err(status) |
| 1518 |
allocate(rhValues(numLons, numLats, numTimes)) |
| 1519 |
... |
| 1520 |
status = nf90_get_var(ncid, rhVarId, rhValues) |
| 1521 |
if(status /= nf90_NoErr) call handle_err(status) |
| 1522 |
|
| 1523 |
|
| 1524 |
|
| 1525 |
|
| 1526 |
Here is an example using NF90\_GET\_VAR to read a section of the |
| 1527 |
variable named rh from an existing netCDF dataset named foo.nc. For |
| 1528 |
simplicity in this example, we assume that we know that rh is |
| 1529 |
dimensioned with lon, lat, and time, that there are ten lon values, five |
| 1530 |
lat values, and three time values, and that we want to replace all the |
| 1531 |
values at the last time. |
| 1532 |
|
| 1533 |
|
| 1534 |
~~~~.fortran |
| 1535 |
|
| 1536 |
|
| 1537 |
use netcdf |
| 1538 |
implicit none |
| 1539 |
integer :: ncId, rhVarId, status |
| 1540 |
integer, parameter :: numLons = 10, numLats = 5, numTimes = 3 |
| 1541 |
real, dimension(numLons, numLats, numTimes) & |
| 1542 |
:: rhValues |
| 1543 |
... |
| 1544 |
status = nf90_open("foo.nc", nf90_NoWrite, ncid) |
| 1545 |
if(status /= nf90_NoErr) call handle_err(status) |
| 1546 |
... |
| 1547 |
status = nf90_inq_varid(ncid, "rh", rhVarId) |
| 1548 |
if(status /= nf90_NoErr) call handle_err(status) |
| 1549 |
!Read the values at the last time by passing an array section |
| 1550 |
status = nf90_get_var(ncid, rhVarId, rhValues(:, :, 3), & |
| 1551 |
start = (/ 1, 1, numTimes /), & |
| 1552 |
count = (/ numLons, numLats, 1 /)) |
| 1553 |
if(status /= nf90_NoErr) call handle_err(status) |
| 1554 |
|
| 1555 |
|
| 1556 |
~~~~ |
| 1557 |
|
| 1558 |
|
| 1559 |
Here is an example of using NF90\_GET\_VAR to read every other point of |
| 1560 |
a netCDF variable named rh having dimensions (6, 4). |
| 1561 |
|
| 1562 |
|
| 1563 |
~~~~.fortran |
| 1564 |
|
| 1565 |
|
| 1566 |
use netcdf |
| 1567 |
implicit none |
| 1568 |
integer :: ncId, rhVarId, status |
| 1569 |
integer, parameter :: numLons = 6, numLats = 4 |
| 1570 |
real, dimension(numLons, numLats) & |
| 1571 |
:: rhValues |
| 1572 |
... |
| 1573 |
status = nf90_open("foo.nc", nf90_NoWrite, ncid) |
| 1574 |
if(status /= nf90_NoErr) call handle_err(status) |
| 1575 |
... |
| 1576 |
status = nf90_inq_varid(ncid, "rh", rhVarId) |
| 1577 |
if(status /= nf90_NoErr) call handle_err(status) |
| 1578 |
... |
| 1579 |
! Read every other value into an array section |
| 1580 |
status = nf90_get_var(ncid, rhVarId, rhValues(::2, ::2) & |
| 1581 |
stride = (/ 2, 2 /)) |
| 1582 |
if(status /= nf90_NoErr) call handle_err(status) |
| 1583 |
|
| 1584 |
|
| 1585 |
|
| 1586 |
~~~~ |
| 1587 |
|
| 1588 |
The following map vector shows the default mapping between a 2x3x4 |
| 1589 |
netCDF variable and an internal array of the same shape: |
| 1590 |
|
| 1591 |
|
| 1592 |
~~~~.fortran |
| 1593 |
|
| 1594 |
|
| 1595 |
real, dimension(2, 3, 4):: a ! same shape as netCDF variable |
| 1596 |
integer, dimension(3) :: map = (/ 1, 2, 6 /) |
| 1597 |
! netCDF dimension inter-element distance |
| 1598 |
! ---------------- ---------------------- |
| 1599 |
! most rapidly varying 1 |
| 1600 |
! intermediate 2 (= map(1)*2) |
| 1601 |
! most slowly varying 6 (= map(2)*3) |
| 1602 |
|
| 1603 |
|
| 1604 |
|
| 1605 |
|
| 1606 |
~~~~ |
| 1607 |
|
| 1608 |
Using the map vector above obtains the same result as simply not passing |
| 1609 |
a map vector at all. |
| 1610 |
|
| 1611 |
Here is an example of using nf90\_get\_var to read a netCDF variable |
| 1612 |
named rh whose dimensions are the transpose of the Fortran 90 array: |
| 1613 |
|
| 1614 |
|
| 1615 |
|
| 1616 |
~~~~.fortran |
| 1617 |
|
| 1618 |
use netcdf |
| 1619 |
implicit none |
| 1620 |
integer :: ncId, rhVarId, status |
| 1621 |
integer, parameter :: numLons = 6, numLats = 4 |
| 1622 |
real, dimension(numLons, numLats) :: rhValues |
| 1623 |
! netCDF variable has dimensions (numLats, numLons) |
| 1624 |
... |
| 1625 |
status = nf90_open("foo.nc", nf90_NoWrite, ncid) |
| 1626 |
if(status /= nf90_NoErr) call handle_err(status) |
| 1627 |
... |
| 1628 |
status = nf90_inq_varid(ncid, "rh", rhVarId) |
| 1629 |
if(status /= nf90_NoErr) call handle_err(status) |
| 1630 |
... |
| 1631 |
! Read transposed values: map vector would be (/ 1, numLats /) for |
| 1632 |
! no transposition |
| 1633 |
status = nf90_get_var(ncid, rhVarId,rhValues, map = (/ numLons, 1 /)) |
| 1634 |
if(status /= nf90_NoErr) call handle_err(status) |
| 1635 |
|
| 1636 |
|
| 1637 |
~~~~ |
| 1638 |
|
| 1639 |
|
| 1640 |
The same effect can be obtained more simply, though using more memory, |
| 1641 |
using Fortran 90 intrinsic functions: |
| 1642 |
|
| 1643 |
|
| 1644 |
~~~~.fortran |
| 1645 |
|
| 1646 |
|
| 1647 |
use netcdf |
| 1648 |
implicit none |
| 1649 |
integer :: ncId, rhVarId, status |
| 1650 |
integer, parameter :: numLons = 6, numLats = 4 |
| 1651 |
real, dimension(numLons, numLats) :: rhValues |
| 1652 |
! netCDF variable has dimensions (numLats, numLons) |
| 1653 |
real, dimension(numLons, numLats) :: tempValues |
| 1654 |
... |
| 1655 |
status = nf90_open("foo.nc", nf90_NoWrite, ncid) |
| 1656 |
if(status /= nf90_NoErr) call handle_err(status) |
| 1657 |
... |
| 1658 |
status = nf90_inq_varid(ncid, "rh", rhVarId) |
| 1659 |
if(status /= nf90_NoErr) call handle_err(status) |
| 1660 |
... |
| 1661 |
status = nf90_get_var(ncid, rhVarId, tempValues)) |
| 1662 |
if(status /= nf90_NoErr) call handle_err(status) |
| 1663 |
rhValues(:, :) = transpose(tempValues) |
| 1664 |
|
| 1665 |
|
| 1666 |
~~~~ |
| 1667 |
|
| 1668 |
|
| 1669 |
|
| 1670 |
|
| 1671 |
6.12 Reading and Writing Character String Values {#f90-reading-and-writing-character-string-values} |
| 1672 |
=========== |
| 1673 |
|
| 1674 |
Character strings are not a primitive netCDF external data type under |
| 1675 |
the classic netCDF data model, in part because FORTRAN does not support |
| 1676 |
the abstraction of variable-length character strings (the FORTRAN LEN |
| 1677 |
function returns the static length of a character string, not its |
| 1678 |
dynamic length). As a result, a character string cannot be written or |
| 1679 |
read as a single object in the netCDF interface. Instead, a character |
| 1680 |
string must be treated as an array of characters, and array access must |
| 1681 |
be used to read and write character strings as variable data in netCDF |
| 1682 |
datasets. Furthermore, variable-length strings are not supported by the |
| 1683 |
netCDF classic interface except by convention; for example, you may |
| 1684 |
treat a zero byte as terminating a character string, but you must |
| 1685 |
explicitly specify the length of strings to be read from and written to |
| 1686 |
netCDF variables. |
| 1687 |
|
| 1688 |
Character strings as attribute values are easier to use, since the |
| 1689 |
strings are treated as a single unit for access. However, the value of a |
| 1690 |
character-string attribute in the classic netCDF interface is still an |
| 1691 |
array of characters with an explicit length that must be specified when |
| 1692 |
the attribute is defined. |
| 1693 |
|
| 1694 |
When you define a variable that will have character-string values, use a |
| 1695 |
character-position dimension as the most quickly varying dimension for |
| 1696 |
the variable (the first dimension for the variable in Fortran 90). The |
| 1697 |
length of the character-position dimension will be the maximum string |
| 1698 |
length of any value to be stored in the character-string variable. Space |
| 1699 |
for maximum-length strings will be allocated in the disk representation |
| 1700 |
of character-string variables whether you use the space or not. If two |
| 1701 |
or more variables have the same maximum length, the same |
| 1702 |
character-position dimension may be used in defining the variable |
| 1703 |
shapes. |
| 1704 |
|
| 1705 |
To write a character-string value into a character-string variable, use |
| 1706 |
either entire variable access or array access. The latter requires that |
| 1707 |
you specify both a corner and a vector of edge lengths. The |
| 1708 |
character-position dimension at the corner should be one for Fortran 90. |
| 1709 |
If the length of the string to be written is n, then the vector of edge |
| 1710 |
lengths will specify n in the character-position dimension, and one for |
| 1711 |
all the other dimensions: (n, 1, 1, ..., 1). |
| 1712 |
|
| 1713 |
In Fortran 90, fixed-length strings may be written to a netCDF dataset |
| 1714 |
without a terminating character, to save space. Variable-length strings |
| 1715 |
should follow the C convention of writing strings with a terminating |
| 1716 |
zero byte so that the intended length of the string can be determined |
| 1717 |
when it is later read by either C or Fortran 90 programs. It is the |
| 1718 |
users responsibility to provide such null termination. |
| 1719 |
|
| 1720 |
If you are writing data in the default prefill mode (see next section), |
| 1721 |
you can ensure that simple strings represented as 1-dimensional |
| 1722 |
character arrays are null terminated in the netCDF file by writing fewer |
| 1723 |
characters than the length declared when the variable was defined. That |
| 1724 |
way, the extra unwritten characters will be filled with the default |
| 1725 |
character fill value, which is a null byte. The Fortran intrinsic TRIM |
| 1726 |
function can be used to trim trailing blanks from the character string |
| 1727 |
argument to NF90\_PUT\_VAR to make the argument shorter than the |
| 1728 |
declared length. If prefill is not on, the data writer must explicitly |
| 1729 |
provide a null terminating byte. |
| 1730 |
|
| 1731 |
Here is an example illustrating this way of writing strings to character |
| 1732 |
array variables: |
| 1733 |
|
| 1734 |
|
| 1735 |
|
| 1736 |
~~~~.fortran |
| 1737 |
|
| 1738 |
use netcdf |
| 1739 |
implicit none |
| 1740 |
integer status |
| 1741 |
integer :: ncid, oceanStrLenID, oceanId |
| 1742 |
integer, parameter :: MaxOceanNameLen = 20 |
| 1743 |
character, (len = MaxOceanNameLen):: ocean |
| 1744 |
... |
| 1745 |
status = nf90_create("foo.nc", nf90_NoClobber, ncid) |
| 1746 |
if(status /= nf90_NoErr) call handle_err(status) |
| 1747 |
... |
| 1748 |
status = nf90_def_dim(ncid, "oceanStrLen", MaxOceanNameLen, oceanStrLenId) |
| 1749 |
if(status /= nf90_NoErr) call handle_err(status) |
| 1750 |
... |
| 1751 |
status = nf90_def_var(ncid, "ocean", nf90_char, (/ oceanStrLenId /), oceanId) |
| 1752 |
if(status /= nf90_NoErr) call handle_err(status) |
| 1753 |
... |
| 1754 |
! Leave define mode, which prefills netCDF variables with fill values |
| 1755 |
status = nf90_enddef(ncid) |
| 1756 |
if (status /= nf90_noerr) call handle_err(status) |
| 1757 |
... |
| 1758 |
! Note that this assignment adds blank fill |
| 1759 |
ocean = "Pacific" |
| 1760 |
! Using trim removes trailing blanks, prefill provides null |
| 1761 |
! termination, so C programs can later get intended string. |
| 1762 |
status = nf90_put_var(ncid, oceanId, trim(ocean)) |
| 1763 |
if(status /= nf90_NoErr) call handle_err(status) |
| 1764 |
|
| 1765 |
|
| 1766 |
~~~~ |
| 1767 |
|
| 1768 |
|
| 1769 |
6.13 Fill Values {#f90-fill-values} |
| 1770 |
=========== |
| 1771 |
|
| 1772 |
What happens when you try to read a value that was never written in an |
| 1773 |
open netCDF dataset? You might expect that this should always be an |
| 1774 |
error, and that you should get an error message or an error status |
| 1775 |
returned. You do get an error if you try to read data from a netCDF |
| 1776 |
dataset that is not open for reading, if the variable ID is invalid for |
| 1777 |
the specified netCDF dataset, or if the specified indices are not |
| 1778 |
properly within the range defined by the dimension lengths of the |
| 1779 |
specified variable. Otherwise, reading a value that was not written |
| 1780 |
returns a special fill value used to fill in any undefined values when a |
| 1781 |
netCDF variable is first written. |
| 1782 |
|
| 1783 |
You may ignore fill values and use the entire range of a netCDF external |
| 1784 |
data type, but in this case you should make sure you write all data |
| 1785 |
values before reading them. If you know you will be writing all the data |
| 1786 |
before reading it, you can specify that no prefilling of variables with |
| 1787 |
fill values will occur by calling writing. This may provide a |
| 1788 |
significant performance gain for netCDF writes. |
| 1789 |
|
| 1790 |
The variable attribute \_FillValue may be used to specify the fill value |
| 1791 |
for a variable. There are default fill values for each type, defined in |
| 1792 |
module netcdf: NF90\_FILL\_CHAR, NF90\_FILL\_INT1 (same as |
| 1793 |
NF90\_FILL\_BYTE), NF90\_FILL\_INT2 (same as NF90\_FILL\_SHORT), |
| 1794 |
NF90\_FILL\_INT, NF90\_FILL\_REAL (same as NF90\_FILL\_FLOAT), and |
| 1795 |
NF90\_FILL\_DOUBLE |
| 1796 |
|
| 1797 |
The netCDF byte and character types have different default fill values. |
| 1798 |
The default fill value for characters is the zero byte, a useful value |
| 1799 |
for detecting the end of variable-length C character strings. If you |
| 1800 |
need a fill value for a byte variable, it is recommended that you |
| 1801 |
explicitly define an appropriate \_FillValue attribute, as generic |
| 1802 |
utilities such as ncdump will not assume a default fill value for byte |
| 1803 |
variables. |
| 1804 |
|
| 1805 |
Type conversion for fill values is identical to type conversion for |
| 1806 |
other values: attempting to convert a value from one type to another |
| 1807 |
type that can’t represent the value results in a range error. Such |
| 1808 |
errors may occur on writing or reading values from a larger type (such |
| 1809 |
as double) to a smaller type (such as float), if the fill value for the |
| 1810 |
larger type cannot be represented in the smaller type. |
| 1811 |
|
| 1812 |
|
| 1813 |
6.14 NF90_RENAME_VAR {#f90-nf90_rename_var} |
| 1814 |
=========== |
| 1815 |
|
| 1816 |
|
| 1817 |
|
| 1818 |
The function NF90\_RENAME\_VAR changes the name of a netCDF variable in |
| 1819 |
an open netCDF dataset. If the new name is longer than the old name, the |
| 1820 |
netCDF dataset must be in define mode. You cannot rename a variable to |
| 1821 |
have the name of any existing variable. |
| 1822 |
|
| 1823 |
|
| 1824 |
|
| 1825 |
## Usage |
| 1826 |
|
| 1827 |
|
| 1828 |
|
| 1829 |
~~~~.fortran |
| 1830 |
|
| 1831 |
function nf90_rename_var(ncid, varid, newname) |
| 1832 |
integer, intent( in) :: ncid, varid |
| 1833 |
character (len = *), intent( in) :: newname |
| 1834 |
integer :: nf90_rename_var |
| 1835 |
|
| 1836 |
~~~~ |
| 1837 |
|
| 1838 |
|
| 1839 |
|
| 1840 |
`ncid` |
| 1841 |
|
| 1842 |
: NetCDF ID, from a previous call to NF90\_OPEN or NF90\_CREATE. |
| 1843 |
|
| 1844 |
`varid` |
| 1845 |
|
| 1846 |
: Variable ID. |
| 1847 |
|
| 1848 |
`newname` |
| 1849 |
|
| 1850 |
: New name for the specified variable. |
| 1851 |
|
| 1852 |
|
| 1853 |
|
| 1854 |
## Errors |
| 1855 |
|
| 1856 |
NF90\_RENAME\_VAR returns the value NF90\_NOERR if no errors occurred. |
| 1857 |
Otherwise, the returned status indicates an error. Possible causes of |
| 1858 |
errors include: |
| 1859 |
|
| 1860 |
- The new name is in use as the name of another variable. |
| 1861 |
- The variable ID is invalid for the specified netCDF dataset. |
| 1862 |
- The specified netCDF ID does not refer to an open netCDF dataset. |
| 1863 |
|
| 1864 |
|
| 1865 |
|
| 1866 |
## Example |
| 1867 |
|
| 1868 |
Here is an example using NF90\_RENAME\_VAR to rename the variable rh to |
| 1869 |
rel\_hum in an existing netCDF dataset named foo.nc: |
| 1870 |
|
| 1871 |
|
| 1872 |
~~~~.fortran |
| 1873 |
|
| 1874 |
|
| 1875 |
use netcdf |
| 1876 |
implicit none |
| 1877 |
integer :: ncId, rhVarId, status |
| 1878 |
... |
| 1879 |
status = nf90_open("foo.nc", nf90_Write, ncid) |
| 1880 |
if(status /= nf90_NoErr) call handle_err(status) |
| 1881 |
... |
| 1882 |
status = nf90_inq_varid(ncid, "rh", rhVarId) |
| 1883 |
if(status /= nf90_NoErr) call handle_err(status) |
| 1884 |
status = nf90_redef(ncid) ! Enter define mode to change variable name |
| 1885 |
if(status /= nf90_NoErr) call handle_err(status) |
| 1886 |
status = nf90_rename_var(ncid, rhVarId, "rel_hum") |
| 1887 |
if(status /= nf90_NoErr) call handle_err(status) |
| 1888 |
status = nf90_enddef(ncid) ! Leave define mode |
| 1889 |
if(status /= nf90_NoErr) call handle_err(status) |
| 1890 |
|
| 1891 |
|
| 1892 |
|
| 1893 |
~~~~ |
| 1894 |
|
| 1895 |
|
| 1896 |
6.15 Change between Collective and Independent Parallel Access: NF90_VAR_PAR_ACCESS {#f90-change-between-collective-and-independent-parallel-access-nf90_var_par_access} |
| 1897 |
=========== |
| 1898 |
|
| 1899 |
|
| 1900 |
|
| 1901 |
The function NF90\_VAR\_PAR\_ACCESS changes whether read/write |
| 1902 |
operations on a parallel file system are performed collectively or |
| 1903 |
independently (the default) on the variable. This function can only be |
| 1904 |
called if the file was created (see [NF90\_CREATE](#NF90_005fCREATE)) or |
| 1905 |
opened (see [NF90\_OPEN](#NF90_005fOPEN)) for parallel I/O. |
| 1906 |
|
| 1907 |
This function is only available if the netCDF library was built with |
| 1908 |
parallel I/O enabled. |
| 1909 |
|
| 1910 |
Calling this function affects only the open file - information about |
| 1911 |
whether a variable is to be accessed collectively or independently is |
| 1912 |
not written to the data file. Every time you open a file on a parallel |
| 1913 |
file system, all variables default to independent operations. The change |
| 1914 |
of a variable to collective access lasts only as long as that file is |
| 1915 |
open. |
| 1916 |
|
| 1917 |
The variable can be changed from collective to independent, and back, as |
| 1918 |
often as desired. |
| 1919 |
|
| 1920 |
Classic and 64-bit offset files, when opened for parallel access, use |
| 1921 |
the parallel-netcdf (a.k.a. pnetcdf) library, which does not allow |
| 1922 |
per-variable changes of access mode - the entire file must be access |
| 1923 |
independently or collectively. For classic and 64-bit offset files, the |
| 1924 |
nf90\_var\_par\_access function changes the access for all variables in |
| 1925 |
the file. |
| 1926 |
|
| 1927 |
|
| 1928 |
|
| 1929 |
## Usage |
| 1930 |
|
| 1931 |
|
| 1932 |
|
| 1933 |
~~~~.fortran |
| 1934 |
|
| 1935 |
function nf90_var_par_access(ncid, varid, access) |
| 1936 |
integer, intent(in) :: ncid |
| 1937 |
integer, intent(in) :: varid |
| 1938 |
integer, intent(in) :: access |
| 1939 |
integer :: nf90_var_par_access |
| 1940 |
end function nf90_var_par_access |
| 1941 |
|
| 1942 |
~~~~ |
| 1943 |
|
| 1944 |
|
| 1945 |
|
| 1946 |
`ncid` |
| 1947 |
|
| 1948 |
: NetCDF ID, from a previous call to NF90\_OPEN (see |
| 1949 |
[NF90\_OPEN](#NF90_005fOPEN)) or NF90\_CREATE (see |
| 1950 |
[NF90\_CREATE](#NF90_005fCREATE)). |
| 1951 |
|
| 1952 |
`varid` |
| 1953 |
|
| 1954 |
: Variable ID. |
| 1955 |
|
| 1956 |
`access` |
| 1957 |
|
| 1958 |
: NF90\_INDEPENDENT to set this variable to independent operations. |
| 1959 |
NF90\_COLLECTIVE to set it to collective operations. |
| 1960 |
|
| 1961 |
|
| 1962 |
|
| 1963 |
## Return Values |
| 1964 |
|
| 1965 |
`NF90_NOERR` |
| 1966 |
|
| 1967 |
: No error. |
| 1968 |
|
| 1969 |
`NF90_ENOTVAR` |
| 1970 |
|
| 1971 |
: No variable found. |
| 1972 |
|
| 1973 |
`NF90_NOPAR` |
| 1974 |
|
| 1975 |
: File not opened for parallel access. |
| 1976 |
|
| 1977 |
|
| 1978 |
|
| 1979 |
## Example |
| 1980 |
|
| 1981 |
This example comes from test program nf\_test/f90tst\_parallel.f90. For |
| 1982 |
this test to be run, netCDF must have been built with a parallel-enabled |
| 1983 |
HDF5, and –enable-parallel-tests must have been used when configuring |
| 1984 |
netcdf. |
| 1985 |
|
| 1986 |
|
| 1987 |
|
| 1988 |
~~~~.fortran |
| 1989 |
|
| 1990 |
! Reopen the file. |
| 1991 |
call handle_err(nf90_open(FILE_NAME, nf90_nowrite, ncid, comm = MPI_COMM_WORLD, & |
| 1992 |
info = MPI_INFO_NULL)) |
| 1993 |
|
| 1994 |
! Set collective access on this variable. This will cause all |
| 1995 |
! reads/writes to happen together on every processor. |
| 1996 |
call handle_err(nf90_var_par_access(ncid, varid, nf90_collective)) |
| 1997 |
|
| 1998 |
! Read this processor's data. |
| 1999 |
call handle_err(nf90_get_var(ncid, varid, data_in, start = start, count = count)) |
| 2000 |
|
| 2001 |
~~~~ |