netcdf-fortran
netcdf-fortran copied to clipboard
Error when adding _FillValue to nf90_short variable
Hello!
I seem unable to add the attribute _FillValue to a NF90_SHORT variable, although it works if I declare it as N90_INT. Why is this?
CALL check(nf90_create("AU.nc", nf90_netcdf4, ncid))
!This fails
CALL check(nf90_def_var(ncid, "PP", nf90_short, (/LonID, LatID/), PP_ID))
CALL check(nf90_put_att(ncid, MIN_PP_ID, "_FillValue", -999.9))
!This works
CALL check(nf90_def_var(ncid, "PP", nf90_int, (/LonID, LatID/), PP_ID))
CALL check(nf90_put_att(ncid, MIN_PP_ID, "_FillValue", -999.9))
I think NF90_INT in NetCDF-Fortran corresponds to integers in Fortran90, -999.9 will be converted to -999 during nf90_put_att and it works. I have checked NF90_SHORT and found that it corresponds to short type in Fortran90 but I cannot find further information about short type in Fortran. I guess nf90_put_att cannot convert -999.9 to short type. I will be back after obtaining more investigation.
NetCDF requires the type of fill value matches the type of defined variable. The value -999.9 used in your call to nf90_put_att will be "treated" as a real (because of F90 function overloading), which conflicts the above NetCDF requirement. The solution is to typecast -999.9 to integer*2, e.g. int2(-999.9) in GNU Fortran, (which corresponds to short in C), i.e.
CALL check(nf90_put_att(ncid, PP_ID, "_FillValue", INT2(-999.9)))
Also, you probably meant to use PP_ID, instead of MIN_PP_ID.