[5068] | 1 | ! --------------------------------------------- |
---|
| 2 | ! This module serves as a wrapper around netcdf. |
---|
| 3 | ! It serves two primary functions: |
---|
| 4 | ! 1) Turn netcdf into a "real" fortran module, without the INCLUDE call |
---|
[5069] | 5 | ! 2) Handle the NC_DOUBLE CPP key. Ideally, this key should ONLY appear here (WIP). TODO |
---|
| 6 | ! Ideally, the "real" netcdf module/headers should ONLY be called here. (WIP) TODO |
---|
[5068] | 7 | ! --------------------------------------------- |
---|
[5069] | 8 | ! TODO check that none of the wrapped functions remain elsewhere |
---|
| 9 | ! TODO check all uses of `use netcdf` + netcdf.inc |
---|
[5068] | 10 | |
---|
| 11 | MODULE lmdz_netcdf |
---|
| 12 | USE, INTRINSIC :: ISO_FORTRAN_ENV, ONLY : REAL64, REAL32 |
---|
| 13 | USE netcdf |
---|
| 14 | IMPLICIT NONE |
---|
| 15 | ! Note: as we want to expose netcdf through this module, we don't make all PRIVATE by default as usual |
---|
| 16 | ! Instead, explicitely make PRIVATE the relevant items. |
---|
[5069] | 17 | PRIVATE CPP_NC_DOUBLE |
---|
[5068] | 18 | |
---|
| 19 | INCLUDE 'netcdf.inc' |
---|
| 20 | |
---|
| 21 | #ifdef NC_DOUBLE |
---|
[5069] | 22 | LOGICAL, PARAMETER :: CPP_NC_DOUBLE = .TRUE. ! Define a variable to reduce use of preprocessor ahead |
---|
[5068] | 23 | INTEGER, PARAMETER :: NF90_FORMAT = NF90_DOUBLE |
---|
| 24 | INTEGER, PARAMETER :: REAL_FORMAT = REAL64 |
---|
| 25 | #else |
---|
[5069] | 26 | LOGICAL, PARAMETER :: CPP_NC_DOUBLE = .FALSE. |
---|
[5068] | 27 | INTEGER, PARAMETER :: NF90_FORMAT = NF90_FLOAT |
---|
| 28 | INTEGER, PARAMETER :: REAL_FORMAT = REAL32 |
---|
| 29 | #endif |
---|
| 30 | CONTAINS |
---|
| 31 | |
---|
[5069] | 32 | ! Note: below, we use the same declarations as the fortran netcdf lib, hence the use of (*) |
---|
| 33 | |
---|
[5068] | 34 | ! We'd like to use "nf_put_var", but it already exists as a legacy nc4 function |
---|
[5069] | 35 | ! CPP_NC_DOUBLE wrapper around nf_put_var_real, nf_put_var_double |
---|
[5068] | 36 | INTEGER FUNCTION nf_put_var_rd(ncid, varid, vals) |
---|
| 37 | INTEGER, INTENT(IN) :: ncid, varid |
---|
| 38 | REAl(REAL_FORMAT), INTENT(IN) :: vals(*) ! (*) as declared in netcdf lib |
---|
[5069] | 39 | |
---|
| 40 | IF (CPP_NC_DOUBLE) THEN |
---|
| 41 | nf_put_var_rd = nf_put_var_double(ncid, varid, vals) |
---|
| 42 | ELSE |
---|
| 43 | nf_put_var_rd = nf_put_var_real(ncid, varid, vals) |
---|
| 44 | END IF |
---|
[5068] | 45 | END FUNCTION nf_put_var_rd |
---|
| 46 | |
---|
[5069] | 47 | ! CPP_NC_DOUBLE wrapper around nf_put_vara_real, nf_put_vara_double |
---|
[5068] | 48 | INTEGER FUNCTION nf_put_vara_rd(ncid, varid, start, counts, vals) |
---|
| 49 | INTEGER, INTENT(IN) :: ncid, varid |
---|
| 50 | INTEGER, INTENT(IN) :: start(*), counts(*) |
---|
[5069] | 51 | REAl(REAL_FORMAT), INTENT(IN) :: vals(*) |
---|
| 52 | |
---|
| 53 | IF (CPP_NC_DOUBLE) THEN |
---|
| 54 | nf_put_vara_rd = nf_put_vara_double(ncid, varid, start, counts, vals) |
---|
| 55 | ELSE |
---|
| 56 | nf_put_vara_rd = nf_put_vara_real(ncid, varid, start, counts, vals) |
---|
| 57 | END IF |
---|
[5068] | 58 | END FUNCTION nf_put_vara_rd |
---|
| 59 | |
---|
[5069] | 60 | ! CPP_NC_DOUBLE wrapper around nf_get_vara_real, nf_get_vara_double |
---|
| 61 | INTEGER FUNCTION nf_get_vara_rd(ncid, varid, start, counts, vals) |
---|
| 62 | INTEGER, INTENT(IN) :: ncid, varid |
---|
| 63 | INTEGER, INTENT(IN) :: start(*), counts(*) |
---|
| 64 | REAl(REAL_FORMAT), INTENT(OUT) :: vals(*) |
---|
[5068] | 65 | |
---|
[5069] | 66 | IF (CPP_NC_DOUBLE) THEN |
---|
| 67 | nf_get_vara_rd = nf_get_vara_double(ncid, varid, start, counts, vals) |
---|
| 68 | ELSE |
---|
| 69 | nf_get_vara_rd = nf_get_vara_real(ncid, varid, start, counts, vals) |
---|
| 70 | END IF |
---|
| 71 | END FUNCTION nf_get_vara_rd |
---|
| 72 | |
---|
| 73 | END MODULE lmdz_netcdf |
---|