I have raster layers from geotiff files with datatype INT2U, aka unsigned short. The original data, floating point, were encoded by multiplying by 100 and then rounding to both reduce file size and to facilitate quick display with software that didn't work with floating point values.
I need to extract the original data by dividing the raster values by 100. The problem is that the value 65535 was used by the software that geo-rectified the original data. When asking the raster what the NA value I get 'negative infinity' and INT2U values are all positive. The result is that the 65535 values are treated as valid values not NA. 65535 is the larger integer possible as an unsigned int.
> r <- rasters[[1]]
> NAvalue(r)
[1] -Inf
> maxValue(r)
[1] 65535
> minValue(r)
[1] 0
> r1 <- r/100
> maxValue(r1)
[1] 655.34
> NAvalue(r)
[1] -Inf
I have some code to check that a value is valid, that is its not in the corners of the image - the aircraft was flying NE to SW so after the images are geo-rectified the pixels in the corners of the resulting images are not valid data.
How can I decomm (restore the original data) setting the 65535 values to the standard NA?
As a follow-up to your comment @Nate Lockwood, the problem is not with the "INT2U" format. I have just prepared a small REPREX (see below) which shows that you can assign the value NA to a raster in INT2U format without any problem.
To be honest, I don't know where the problem you are experiencing comes from, but the cause is most likely something else.
Hope this will contribute to help you a little to find the origin of the problem, though.
REPREX:
library(raster)
#> Le chargement a nécessité le package : sp
# Creating a raster
r <- raster(ncols = 3, nrows = 3)
(values(r) <- seq(length(r)))
#> [1] 1 2 3 4 5 6 7 8 9
dataType(r)
#> [1] "FLT4S"
# Converting to INT2U format
r_convert <- tempfile("raster", fileext = ".tif")
writeRaster(r, r_convert, datatype = "INT2U", overwrite = TRUE)
r_INT2U <- raster(r_convert)
# Checking format of r_INT2u
dataType(r_INT2U)
#> [1] "INT2U"
# Creating object rc_INT2U to keep the original raster object 'r_INT2U'
rc_INT2U <- r_INT2U
rc_INT2U[rc_INT2U == 9] <- NA
values(rc_INT2U)
#> [1] 1 2 3 4 5 6 7 8 NA
Created on 2021-09-18 by the reprex package (v2.0.1)