I'm trying to get the chlorophyll a values from a NetCDF file using R software, but all I'm getting are missing values, NA. I would like to know if I'm doing something wrong or if the file really just has missing chlorophyll a values. I can get the Longitude and Latitude values with this method.

The file I'm using is from here https://oceandata.sci.gsfc.nasa.gov/MODIS-Aqua/Mapped/Monthly/4km/chlor_a/ and I get missing values from any file I tried not only the one showed on the script.


clorofila<- "C:\\Users\\User\\Desktop\\files\\A20172132017243.L3m_MO_CHL_chlor_a_4km.nc"

cla <- open.nc(clorofila)


clor <- var.get.nc(cla,"chlor_a",start=c(1,1),count=c(8640,4320))
Long <- var.get.nc(cla,"lon")
Lat <- var.get.nc(cla, "lat")

With ncdf4 and raster I got the same results


clorofila10<- "C:\\Users\\User\\Desktop\\files\\A20172132017243.L3m_MO_CHL_chlor_a_4km.nc"

nc <- nc_open(clorofila10)

val <- ncvar_get(nc, "chlor_a")


clorofila10<- "C:\\Users\\User\\Desktop\\files\\A20172132017243.L3m_MO_CHL_chlor_a_4km.nc"
CHL1 <- raster(clorofila10, varname="chlor_a")
names(CHL1) <- 'chlor_a'

z <- getValues(CHL1)
  • You aren't actually showing us your results. If I do the same RNetCDF process as you, and then do all(is.na(clor)), I get FALSE, showing that not all the values are NA. There are a lot of NAs - 21 million compared to 16 million non-NA - but its not all NAs. Why do you think it is? – Spacedman Oct 13 '18 at 8:19
  • Reading the raster works for me too. Plotting it shows the NAs to be on the land, and the non-NAs to be in the seas and oceans. Is that what you expect from the dataset? – Spacedman Oct 13 '18 at 8:20

You don't show what the issue is here, but I don't see any problem.

NOTE: this code downloads a 50 Mb file

f <- "A20172132017243.L3m_MO_CHL_chlor_a_4km.nc"
u <- "https://oceandata.sci.gsfc.nasa.gov/cgi/getfile"
curl::curl_download(file.path(u, f), f)
r <- raster(f, varname = "chlor_a")

range(values(r), na.rm = TRUE)
##[1]  0.01220753 99.85372162

## some values will be NA
extract(r, cbind(c(140, 150), c(-42, -50)))
#[1] 0.2061136        NA

I definitely recommend not reading all the values, keep them in the raster and use extract, cellFromXY, and related functions.

To get it off disk as a raster use readAll(raster(f)).

| improve this answer | |
  • Thank you so much for your answer! My problem is that the result matrix from ncvar_get(nc, "chlor_a") or the result from getValues(CHL1) gives me all NA values from the begining till the end. Every single value is NA, and when I used the same functions previously on a Temperature netcdf file it gave me many temperature values intercalated with NA values. That was what I would expect in this case too, with chlor_a values. – TheBlueMagpie Oct 13 '18 at 2:43
  • I don't see that, the data is perfectly fine range(ncdf4::ncvar_get(ncdf4::nc_open(f), "chlor_a"), na.rm = TRUE) #[1] 0.01220753 99.85372162 – mdsumner Oct 13 '18 at 23:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.