I work in R with HDF files and sometimes i use HEG tool to deal with HDF files.

But now NASA has introduced something else that is NetCDF files with VIIRS VNP02 data. I want to extract I04 and I05 bands from a .nc file attached herewith. the extracted bands should be projected with UTM WGS84 Zone 45. Link to example NetCDF (.nc) file

Can anyone help me how to do this in R.

  • What have you tried already and how did it fail?
    – Spacedman
    Sep 2, 2019 at 9:59
  • I think the data in this is not on a rectangular grid and there should be an accompanying "GEO" file with the coordinates, but I can't find it and I'm getting 502 Bad Gateway errors from the nasa sites currently...
    – Spacedman
    Sep 2, 2019 at 15:29
  • The data values are easy enough to extract but the locations are not straightforward. I think that since this is the lowest-level data in order to georeference each pixel you need to consider the elevation shape of the terrain that the sensor is looking at. There's probably another data product that has had this transformation done. Doing it yourself looks a bit complex...
    – Spacedman
    Sep 3, 2019 at 21:37
  • @Spacedman VIIRS VNP02 data can be considered equivalent to MODIS MOD02 data. In MOD02 data the Geo-locations are embedded and softwares like ENVI and HEG are helpful to extract desired band. But VNP02 data are being provided in NetCDF format and if the extraction of desired bands is this much difficult then i guess i have to wait till the easy file formats of VNP02 are available.
    – Lostman
    Sep 4, 2019 at 10:14
  • Getting the data out of one of those files is easy - its just the fine geolocation that isn't there. There's a lat-long bounding box and what I suspect is the four corners of the scans but its a non-rectilinear grid. If it even is a grid...
    – Spacedman
    Sep 4, 2019 at 11:27

1 Answer 1


The spatial coordinates for the data in VNP02IMG.A2018228.1948.001.2018229023940.nc seem to appear in the VNP03IMG.A2018228.1948.001.2018229003536.nc file.

This can be downloaded via https://ladsweb.modaps.eosdis.nasa.gov/archive/allData/5110/VNP03IMG/2018/228/VNP03IMG.A2018228.1948.001.2018229003536.nc but I'm not sure how you infer the last numbers from the VNP02 file. Note this matching is based on guesswork based on the two files having the same parts in the name and also basically the same geographic metadata and the same size grid:

$ gdalinfo VNP03IMG.A2018228.1948.001.2018229003536.nc | grep GRing
  GRingPointLatitude=28.95256 33.857861 54.62978 47.946575 
  GRingPointLongitude=103.91853 72.036797 72.617546 116.37637 
$ gdalinfo VNP02IMG.A2018228.1948.001.2018229023940.nc | grep GRing
  GRingPointLatitude=28.9526 33.857899 54.629799 47.946602 
  GRingPointLongitude=103.919 72.036797 72.6175 116.376 

Once got, you can read in the data using the HDF5 driver and point to the subdataset using the raster package:

I05 = raster(

Then similar lets you read the coordinates into a raster grid:

lat = raster(
lon = raster(

These are a bit large for me to plot (6464 x 6400 pixels each). I'll take a sample:

> s = sample(prod(dim(I05)), 100000)
> d = data.frame(lon=lon[][s], lat=lat[][s], I05=I05[][s])
> ggplot(d,aes(x=lon, y=lat)) + geom_point(aes(col=I05))

enter image description here

Getting rid of the large values that flag various conditions makes a neater plot:

> d= d[d$I05 < 65500,]
> dim(d)
[1] 87104     3

enter image description here

There you can see the basic structure even though what you have is 100,000 random dots. The underlying "grid" structure is warped to that curvilinear outline.

If you want to work with this as a non-rectilinear grid you'll need the stars package and a lot of RAM to deal with the size. I'd also see if you can get an authoritative reference on this before doing any serious work with it.

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.