netcdf-fortran
netcdf-fortran copied to clipboard
Unable to add _FillValue to INT64 variable
Using version 4.4.4 for the Fortran library and 4.4.1 of the C library compiled with either ifort 15.0.2 or gfortran 4.7.4, the following program:
program test
use netcdf
implicit none
character(len=30) :: path="trash.nc"
integer :: ierr, ncid, dim_var(1), varid
integer, parameter :: k = selected_int_kind(18)
integer, parameter :: len = 3
integer, parameter :: s(1) = [1]
integer, parameter :: c(1) = [len]
integer(kind=k) :: data(l)
! Make data
do ierr=1,len
data(ierr) = ierr
end do
! Make simple netcdf file
ierr = nf90_create(path, IOR(NF90_NETCDF4, NF90_CLOBBER), ncid)
ierr = nf90_def_dim(ncid, "len", len, dim_var(1))
! Make variable
ierr = nf90_def_var(ncid, "test", NF90_INT64, dim_var, varid)
! This will succeed, but make a LONG variable rather than INT64
ierr = nf90_put_att(ncid, varid, 'scale_factor', int(2, kind=k))
! This will fail
ierr = nf90_put_att(ncid, varid, '_FillValue', int(1, kind=k))
if (ierr /= NF90_NOERR) then
write(*,*) trim(nf90_strerror(ierr))
stop 5
end if
ierr = nf90_enddef(ncid)
! Write data
ierr = nf90_put_var(ncid, varid, data, s, c, s)
ierr = nf90_close(ncid)
end program test
exists prematurely returning,
NetCDF: Not a valid data type or _FillValue type mismatch STOP 5
The program runs as expected if k=2 and the variable is defined for NF90_SHORT or k=4 and NF90_INT.
My best guess is that here nf90_get_att_one_EightByteInt()
casts values
to an int before passing to the C code. If that's intended as a feature, could the documentation be updated to indicate that INT64 isn't intended for general use?
Thank you; I believe it should be available to use, although I am not a Fortran expert. I will investigate this and see what we can do to fix it. If an obvious fix suggests itself to you, please feel free to submit a pull request as well! Thanks, I'll address this for the next release one way or the other.
I did a quick look at the code and it looks to me like fixing this is going be a prolonged effort. As near as I can tell, this code was written before int64 was available, so it assumes that all integers are either 4 or 8 bytes (ala cray). The notion of having 4-byte and 8-byte integers mixed was not considered. I hope this problem is isolated to the attribute code, but not having looked, I bet it is a problem for int64 typed variables as well. How urgent is this?
I wouldn't say it's massively urgent as I've added the necessary variable to my outputs, but until I can add attributes it won't be CF-compliant, which will be a problem in a few months when the compliance review happens.
Ok. I will put it on my stack.
I believe this affects not only fill values but also variable and attribute values. Given both NetCDF-4 and CDF5 formats support NC_INT64 data type, it is expected netcdf-fortran include F77 functions like nf_put_vara_int8, nf_get_vara_int8, etc. so F90 functions can also call them accordingly. Will this issue be addressed in the next release?