| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | module chartmap_metadata | ||
| 2 | |||
| 3 | use, intrinsic :: iso_fortran_env, only: dp => real64 | ||
| 4 | use netcdf | ||
| 5 | implicit none | ||
| 6 | |||
| 7 | private | ||
| 8 | public :: chartmap_metadata_t | ||
| 9 | public :: read_chartmap_metadata, read_chartmap_cart_units | ||
| 10 | |||
| 11 | type :: chartmap_metadata_t | ||
| 12 | real(dp) :: rho_lcfs = -1.0_dp | ||
| 13 | real(dp) :: cart_scale_to_m = -1.0_dp | ||
| 14 | character(len=16) :: cart_units = "" | ||
| 15 | character(len=32) :: rho_convention = "" | ||
| 16 | end type chartmap_metadata_t | ||
| 17 | |||
| 18 | contains | ||
| 19 | |||
| 20 | 62 | subroutine check_nc(status, location) | |
| 21 | integer, intent(in) :: status | ||
| 22 | character(len=*), intent(in) :: location | ||
| 23 | |||
| 24 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
|
62 | if (status /= nf90_noerr) then |
| 25 | ✗ | print *, "NetCDF error at ", trim(location), ": ", & | |
| 26 | ✗ | trim(nf90_strerror(status)) | |
| 27 | ✗ | error stop "NetCDF operation failed" | |
| 28 | end if | ||
| 29 | 62 | end subroutine check_nc | |
| 30 | |||
| 31 | 13 | subroutine read_chartmap_metadata(filename, meta) | |
| 32 | character(len=*), intent(in) :: filename | ||
| 33 | type(chartmap_metadata_t), intent(out) :: meta | ||
| 34 | |||
| 35 | integer :: ncid, status, att_type, att_len | ||
| 36 | real(dp) :: rho_lcfs | ||
| 37 | |||
| 38 | meta%rho_lcfs = -1.0_dp | ||
| 39 | meta%cart_scale_to_m = -1.0_dp | ||
| 40 | 13 | meta%cart_units = "" | |
| 41 | 13 | meta%rho_convention = "" | |
| 42 | |||
| 43 | 13 | status = nf90_open(trim(filename), nf90_nowrite, ncid) | |
| 44 | 13 | call check_nc(status, "nf90_open(chartmap)") | |
| 45 | |||
| 46 | rho_lcfs = -1.0_dp | ||
| 47 | 13 | status = nf90_get_att(ncid, nf90_global, "rho_lcfs", rho_lcfs) | |
| 48 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
|
13 | if (status /= nf90_noerr .and. status /= nf90_enotatt) & |
| 49 | ✗ | call check_nc(status, "get_att rho_lcfs") | |
| 50 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 6 times.
|
13 | if (status == nf90_noerr) meta%rho_lcfs = rho_lcfs |
| 51 | |||
| 52 | status = nf90_inquire_attribute(ncid, nf90_global, "rho_convention", & | ||
| 53 | 13 | xtype=att_type, len=att_len) | |
| 54 |
4/6✓ Branch 0 taken 11 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 11 times.
✗ Branch 5 not taken.
|
13 | if (status == nf90_noerr .and. att_type == nf90_char .and. & |
| 55 | att_len >= 1) then | ||
| 56 |
1/2✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
|
11 | if (att_len <= len(meta%rho_convention)) then |
| 57 | status = nf90_get_att(ncid, nf90_global, "rho_convention", & | ||
| 58 | 11 | meta%rho_convention(1:att_len)) | |
| 59 | 11 | call check_nc(status, "get_att rho_convention") | |
| 60 | end if | ||
| 61 | end if | ||
| 62 | |||
| 63 | 13 | call read_cart_units(ncid, meta%cart_units, meta%cart_scale_to_m) | |
| 64 | |||
| 65 | 13 | status = nf90_close(ncid) | |
| 66 | 13 | call check_nc(status, "nf90_close(chartmap)") | |
| 67 | 13 | end subroutine read_chartmap_metadata | |
| 68 | |||
| 69 | 8 | subroutine read_chartmap_cart_units(filename, cart_units) | |
| 70 | character(len=*), intent(in) :: filename | ||
| 71 | character(len=*), intent(out) :: cart_units | ||
| 72 | |||
| 73 | integer :: ncid, status | ||
| 74 | real(dp) :: cart_scale_to_m | ||
| 75 | |||
| 76 | 4 | status = nf90_open(trim(filename), nf90_nowrite, ncid) | |
| 77 | 4 | call check_nc(status, "nf90_open(chartmap)") | |
| 78 | 4 | call read_cart_units(ncid, cart_units, cart_scale_to_m) | |
| 79 | 4 | status = nf90_close(ncid) | |
| 80 | 4 | call check_nc(status, "nf90_close(chartmap)") | |
| 81 | 4 | end subroutine read_chartmap_cart_units | |
| 82 | |||
| 83 | 17 | subroutine read_cart_units(ncid, cart_units, cart_scale_to_m) | |
| 84 | integer, intent(in) :: ncid | ||
| 85 | character(len=*), intent(out) :: cart_units | ||
| 86 | real(dp), intent(out) :: cart_scale_to_m | ||
| 87 | |||
| 88 | integer :: status, varid | ||
| 89 | character(len=128) :: units | ||
| 90 | |||
| 91 | 34 | status = nf90_inq_varid(ncid, "x", varid) | |
| 92 | 17 | call check_nc(status, "inq_varid x") | |
| 93 | 17 | units = "" | |
| 94 | 17 | status = nf90_get_att(ncid, varid, "units", units) | |
| 95 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
|
17 | if (status /= nf90_noerr) units = "" |
| 96 | |||
| 97 | 34 | select case (trim(adjustl(units))) | |
| 98 | case ("cm", "") | ||
| 99 | 15 | cart_scale_to_m = 0.01_dp | |
| 100 |
2/4✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
✗ Branch 3 not taken.
|
15 | cart_units = "cm" |
| 101 | case ("m") | ||
| 102 | 2 | cart_scale_to_m = 1.0_dp | |
| 103 |
2/4✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
2 | cart_units = "m" |
| 104 | case default | ||
| 105 | ✗ | print *, "chartmap_metadata: unsupported x.units=", trim(units) | |
| 106 |
3/5✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
|
34 | error stop "unsupported chartmap Cartesian units" |
| 107 | end select | ||
| 108 | 17 | end subroutine read_cart_units | |
| 109 | |||
| 110 | ✗ | end module chartmap_metadata | |
| 111 |