1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
|
! ------------ Routines to create/open/close/redefine netcdf files ------------
! Replacement for fort-control.c
! Written by: Richard Weed, Ph.D.
! Center for Advanced Vehicular Systems
! Mississippi State University
! rweed@cavs.msstate.edu
! License (and other Lawyer Language)
! This software is released under the Apache 2.0 Open Source License. The
! full text of the License can be viewed at :
!
! http:www.apache.org/licenses/LICENSE-2.0.html
!
! The author grants to the University Corporation for Atmospheric Research
! (UCAR), Boulder, CO, USA the right to revise and extend the software
! without restriction. However, the author retains all copyrights and
! intellectual property rights explicitly stated in or implied by the
! Apache license
! Version 1.: Sept. 2005 - Initial Cray X1 version
! Version 2.: May, 2006 - Updated to support g95
! Version 3.: April, 2009 - Updated for netcdf 4.0.1
! Version 4.: April, 2010 - Updated for netcdf 4.1.1
! Version 5.: Feb. 2013 - Added nf_inq_path support for fortran 4.4
! Vertion 6.: Nov. 2013 - Added nf_set_log_level support
! Version 7.: May, 2014 - Ensure return error status checked from C API calls
! Version 8.: Jan., 2016 - General code cleanup. Changed name processing to
! reflect change in addCNullChar function. Added
! support for nc_open_mem
!-------------------------------- nf_create --------------------------------
Function nf_create(path, cmode, ncid) RESULT (status)
! Creates a new NetCDF file given a file name and a creation mode and returns
! the file id and a status flag
USE netcdf_nc_interfaces
Implicit NONE
Character(LEN=*), Intent(IN) :: path
Integer, Intent(IN) :: cmode
Integer, Intent(OUT) :: ncid
Integer :: status
Integer(C_INT) :: ccmode, cncid, cstatus
Character(LEN=(LEN(path)+1)) :: cpath
Integer :: ie
ccmode = cmode
cncid = 0
! Check for C null character on path and add one if not present.
cpath = addCNullChar(path, ie)
! Call nc_create to create file
cstatus = nc_create(cpath(1:ie), ccmode, cncid)
If (cstatus == NC_NOERR) Then
ncid = cncid
EndIf
status = cstatus
End Function nf_create
!-------------------------------- nf__create -------------------------------
Function nf__create(path, cmode, initialsz, chunksizehintp, ncid) &
RESULT(status)
! Creates a new NetCDF file and returns the file id and a status flag
! This is an alternate form of nf_create that allows user to input
! two additional tuning parameters
USE netcdf_nc_interfaces
Implicit NONE
Character(LEN=*), Intent(IN) :: path
Integer, Intent(IN) :: cmode, initialsz, chunksizehintp
Integer, Intent(OUT) :: ncid
Integer :: status
Integer(C_INT) :: ccmode, cncid, cstatus
Integer(C_SIZE_T) :: cinit, cchunk
Character(LEN=(LEN(path)+1)) :: cpath
Integer :: ie
ccmode = cmode
cchunk = chunksizehintp
cinit = initialsz
cncid = 0
! Check for C null character on path and add one if not present.
cpath = addCNullChar(path, ie)
! Call nc_create to create file
cstatus = nc__create(cpath(1:ie), ccmode, cinit, cchunk, cncid)
If (cstatus == NC_NOERR) Then
ncid = cncid
EndIf
status = cstatus
End Function nf__create
!-------------------------------- nf__create_mp ------------------------------
Function nf__create_mp(path, cmode, initialsz, basepe, chunksizehintp, ncid) &
RESULT(status)
! Creates a new NetCDF file and returns the file id and a status flag
! This is an alternate form of nf__create for shared memory MPP systems
! plus two additional tuning parameters
USE netcdf_nc_interfaces
Implicit NONE
Character(LEN=*), Intent(IN) :: path
Integer, Intent(IN) :: cmode, initialsz, chunksizehintp, basepe
Integer, Intent(OUT) :: ncid
Integer :: status
Integer(C_INT) :: ccmode, cncid, cstatus
Integer(C_INT), TARGET :: cbasepe
Integer(C_SIZE_T) :: cinit, cchunk
Type(C_PTR) :: cbasepeptr
Character(LEN=(LEN(path)+1)) :: cpath
Integer :: ie
ccmode = cmode
cchunk = chunksizehintp
cinit = initialsz
cncid = 0
cbasepe = basepe
cbasepeptr = C_LOC(cbasepe)
! Check for C null character on path and add one if not present.
cpath = addCNullChar(path, ie)
! Call nc_create_mp to create file for base pe
cstatus = nc__create_mp(cpath(1:ie), ccmode, cinit, cbasepeptr, &
cchunk, cncid)
If (cstatus == NC_NOERR) Then
ncid = cncid
EndIf
status = cstatus
End Function nf__create_mp
!-------------------------------- nf_open ----------------------------------
Function nf_open(path, mode, ncid) RESULT (status)
! Open an existing NetCDF file and return file id and a status flag
USE netcdf_nc_interfaces
Implicit NONE
Character(LEN=*), Intent(IN) :: path
Integer, Intent(IN) :: mode
Integer, Intent(INOUT) :: ncid
Integer :: status
Integer(C_INT) :: cmode, cncid, cstatus
Character(LEN=(LEN(path)+1)) :: cpath
Integer :: ie
cmode = mode
cncid = 0
! Check for C null character on path and add one if not present.
cpath = addCNullChar(path, ie)
! Call nc_create to create file
cstatus = nc_open(cpath(1:ie), cmode, cncid)
If (cstatus == NC_NOERR) Then
ncid = cncid
EndIf
status = cstatus
End Function nf_open
!-------------------------------- nf__open ---------------------------------
Function nf__open(path, mode, chunksizehintp, ncid) RESULT (status)
! Open an existing NetCDF file and return file id and a status flag
! Alternate form of nf_open with extra tuning parameter
USE netcdf_nc_interfaces
Implicit NONE
Character(LEN=*), Intent(IN) :: path
Integer, Intent(IN) :: mode, chunksizehintp
Integer, Intent(INOUT) :: ncid
Integer :: status
Integer(C_INT) :: cmode, cncid, cstatus
Integer(C_SIZE_T) :: cchunk
Character(LEN=(LEN(path)+1)) :: cpath
Integer :: ie
cmode = mode
cchunk = chunksizehintp
cncid = 0
! Check for C null character on path and add one if not present.
cpath = addCNullChar(path,ie)
! Call nc_create to create file
cstatus = nc__open(cpath(1:ie), cmode, cchunk, cncid)
If (cstatus == NC_NOERR) Then
ncid = cncid
EndIf
status = cstatus
End Function nf__open
!-------------------------------- nf__open_mp --------------------------------
Function nf__open_mp(path, mode, basepe, chunksizehintp, ncid) RESULT (status)
! Open an existing NetCDF file and return file id and a status flag
! Alternate form of nf__open with parameter to designate basepe on
! shared memory MPP systems.
USE netcdf_nc_interfaces
Implicit NONE
Character(LEN=*), Intent(IN) :: path
Integer, Intent(IN) :: mode, chunksizehintp, basepe
Integer, Intent(INOUT) :: ncid
Integer :: status
Integer(C_INT) :: cmode, cncid, cstatus
Integer(C_INT), TARGET :: cbasepe
Integer(C_SIZE_T) :: cchunk
Type(C_PTR) :: cbasepeptr
Character(LEN=(LEN(path)+1)) :: cpath
Integer :: ie
cmode = mode
cchunk = chunksizehintp
cncid = 0
cbasepe = basepe
cbasepeptr = C_LOC(cbasepe)
! Check for C null character on path and add one if not present.
cpath = addCNullChar(path, ie)
! Call nc_create to create file
cstatus = nc__open_mp(cpath(1:ie), cmode, cbasepeptr, cchunk, &
cncid)
If (cstatus == NC_NOERR) Then
ncid = cncid
EndIf
status = cstatus
End Function nf__open_mp
!-------------------------------- nf_open_mem --------------------------------
Function nf_open_mem(path, mode, size, memory, ncid) RESULT(status)
! Open a block of memory passed as an array of C_CHAR bytes as a
! netcdf file. Note the file can only be opened as read-only
USE netcdf_nc_interfaces
Implicit NONE
Character(LEN=*), Intent(IN) :: path
Integer, Intent(IN) :: mode
Integer, Intent(IN) :: size
Character(KIND=C_CHAR), Intent(IN), TARGET :: memory(*)
Integer, Intent(INOUT) :: ncid
Integer :: status
Integer(C_INT) :: cstatus, cmode, cncid
Character(LEN=LEN(path)+1) :: cpath
Integer(C_SIZE_T) :: csize
Type(C_PTR) :: cmemoryptr
Integer :: ie
cpath = addCNullChar(path, ie)
cmode = mode
csize = size
cmemoryptr = C_LOC(memory)
cstatus = nc_open_mem(cpath(1:ie), cmode, csize, cmemoryptr, cncid)
ncid = cncid
status = cstatus
End Function nf_open_mem
!-------------------------------- nf_inq_path ------------------------------
Function nf_inq_path(ncid, pathlen, path) RESULT(status)
! Inquire about file pathname and name length
USE netcdf_nc_interfaces
Implicit NONE
Integer, Intent(IN) :: ncid
Integer, Intent(INOUT) :: pathlen
Character(LEN=*), Intent(INOUT) :: path
Integer :: status
Integer(C_INT) :: cncid, cstatus
Integer(C_SIZE_T) :: cpathlen
Character(LEN=LEN(path)+1) :: tmppath
cncid = ncid
path = REPEAT(" ", LEN(path))
tmppath = REPEAT(" ", LEN(tmppath))
cstatus = nc_inq_path(cncid, cpathlen, tmppath)
If (cstatus == NC_NOERR) Then
pathlen = int(cpathlen)
If (pathlen > LEN(path)) pathlen = LEN(path)
path = stripCNullchar(tmppath, pathlen)
EndIf
status = cstatus
End Function nf_inq_path
!-------------------------------- nf_set_fill ------------------------------
Function nf_set_fill(ncid, fillmode, old_mode) RESULT(status)
! Sets fill mode for given netcdf file returns old mode if present
USE netcdf_nc_interfaces
Implicit NONE
Integer, Intent(IN) :: ncid, fillmode
Integer, Intent(OUT) :: old_mode
Integer :: status
Integer(C_INT) :: cncid, cfill, coldmode, cstatus
cncid = ncid
cfill = fillmode
coldmode = 0
cstatus = nc_set_fill(cncid, cfill, coldmode)
If (cstatus == NC_NOERR) Then
old_mode = coldmode
EndIf
status = cstatus
End Function nf_set_fill
!-------------------------------- nf_set_default_format --------------------
Function nf_set_default_format(newform, old_format) RESULT(status)
! Sets new default data format. Used to toggle between 64 bit offset and
! classic mode
USE netcdf_nc_interfaces
Implicit NONE
Integer, Intent(IN) :: newform
Integer, Intent(OUT) :: old_format
Integer :: status
Integer(C_INT) :: cnew, cold, cstatus
cnew = newform
cstatus = nc_set_default_format(cnew,cold)
If (cstatus == NC_NOERR) Then
old_format = cold
EndIf
status = cstatus
End Function nf_set_default_format
!-------------------------------- nf_redef ---------------------------------
Function nf_redef(ncid) RESULT(status)
! Reenter definition mode for NetCDF file id ncid
USE netcdf_nc_interfaces
Implicit NONE
Integer, Intent(IN) :: ncid
Integer :: status
Integer(C_INT) :: cncid, cstatus
cncid = ncid
cstatus = nc_redef(cncid)
status = cstatus
End Function nf_redef
!-------------------------------- nf_enddef --------------------------------
Function nf_enddef(ncid) RESULT(status)
! Exit definition mode for NetCDF file id ncid
USE netcdf_nc_interfaces
Implicit NONE
Integer, Intent(IN) :: ncid
Integer :: status
Integer(C_INT) :: cncid, cstatus
cncid = ncid
cstatus = nc_enddef(cncid)
status = cstatus
End Function nf_enddef
!-------------------------------- nf__enddef -------------------------------
Function nf__enddef(ncid, h_minfree, v_align, v_minfree, r_align) &
RESULT(status)
! Exit definition mode for NetCDF file id ncid. Alternate version
! with additional tuning parameters
USE netcdf_nc_interfaces
Implicit NONE
Integer, Intent(IN) :: ncid, h_minfree, v_align, v_minfree, r_align
Integer :: status
Integer(C_INT) :: cncid, cstatus
Integer(C_SIZE_T) :: chminfree, cvalign, cvminfree, cralign
cncid = ncid
chminfree = h_minfree
cvalign = v_align
cvminfree = v_minfree
cralign = r_align
cstatus = nc__enddef(cncid, chminfree, cvalign, cvminfree, cralign)
status = cstatus
End Function nf__enddef
!-------------------------------- nf_sync ----------------------------------
Function nf_sync(ncid) RESULT(status)
! synch up all open NetCDF files
USE netcdf_nc_interfaces
Implicit NONE
Integer, Intent(IN) :: ncid
Integer :: status
Integer(C_INT) :: cncid, cstatus
cncid = ncid
cstatus = nc_sync(cncid)
status = cstatus
End Function nf_sync
!-------------------------------- nf_abort ---------------------------------
Function nf_abort(ncid) RESULT(status)
! Abort netCDF file creation and exit
USE netcdf_nc_interfaces
Implicit NONE
Integer, Intent(IN) :: ncid
Integer :: status
Integer(C_INT) :: cncid, cstatus
cncid = ncid
cstatus = nc_abort(cncid)
status = cstatus
End Function nf_abort
!-------------------------------- nf_close ---------------------------------
Function nf_close(ncid) RESULT(status)
! Close netCDF file id ncid
USE netcdf_nc_interfaces
Implicit NONE
Integer, Intent(IN) :: ncid
Integer :: status
Integer(C_INT) :: cncid, cstatus
cncid = ncid
cstatus = nc_close(cncid)
status = cstatus
End Function nf_close
!-------------------------------- nf_delete --------------------------------
Function nf_delete(path) RESULT(status)
! Delete netCDF file id ncid
USE netcdf_nc_interfaces
Implicit NONE
Character(LEN=*), Intent(IN) :: path
Integer :: status
Integer(C_INT) :: cstatus
Character(LEN=(LEN(path)+1)) :: cpath
Integer :: ie
! Check for C null character on path and add one if not present.
cpath = addCNullChar(path,ie)
cstatus = nc_delete(cpath(1:ie))
status = cstatus
End Function nf_delete
!-------------------------------- nf_delete_mp -------------------------------
Function nf_delete_mp(path, pe) RESULT(status)
! Delete netCDF file id ncid. Alternate form of nf_delete for shared memory
! MPP systems.
USE netcdf_nc_interfaces
Implicit NONE
Character(LEN=*), Intent(IN) :: path
Integer, Intent(IN) :: pe
Integer :: status
Integer(C_INT) :: cstatus, cpe
Character(LEN=(LEN(path)+1)) :: cpath
Integer :: ie
cpe = pe
! Check for C null character on path and add one if not present.
cpath = addCNullChar(path,ie)
cstatus = nc_delete_mp(cpath(1:ie), cpe)
status = cstatus
End Function nf_delete_mp
!-------------------------------- nf_set_base_pe ------------------------------
Function nf_set_base_pe(ncid, pe) RESULT(status)
! Sets base pe number on shared memory MPP systems
Use netcdf_nc_interfaces
Implicit NONE
Integer, Intent(IN) :: ncid, pe
Integer :: status
Integer(C_INT) :: cncid, cpe, cstatus
cncid = ncid
cpe = pe
cstatus = nc_set_base_pe(cncid, cpe)
status = cstatus
End Function nf_set_base_pe
!-------------------------------- nf_inq_base_pe ------------------------------
Function nf_inq_base_pe(ncid, pe) RESULT(status)
! Gets previously set base pe number on shared memory MPP systems
Use netcdf_nc_interfaces
Implicit NONE
Integer, Intent(IN) :: ncid
Integer, Intent(OUT) :: pe
Integer :: status
Integer(C_INT) :: cncid, cpe, cstatus
cncid = ncid
cstatus = nc_inq_base_pe(cncid, cpe)
If (cstatus == NC_NOERR) Then
pe = cpe
EndIf
status = cstatus
End Function nf_inq_base_pe
|