Unidata / netcdf-fortran

Official GitHub repository for netCDF-Fortran libraries, which depend on the netCDF C library. Install the netCDF C library first.
Other
244 stars 98 forks source link

Error when adding _FillValue to nf90_short variable #80

Open caiomattos opened 6 years ago

caiomattos commented 6 years ago

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))
FeiYao-Edinburgh commented 5 years ago

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.

wkliao commented 5 years ago

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.