Home > OS >  How i can convert ncdf (of Sentinel_5_L2) files to geotiff?
How i can convert ncdf (of Sentinel_5_L2) files to geotiff?

Time:11-18

I am trying to do a geotiff file with a netcdf file. I have this file (Sentinel_5_L2). But my code is not run. Anyone know as create the geotiff file?

File https://wetransfer.com/downloads/7c5692bef082bc64999e10d8a7a91e8f20211116115821/7774f8

This is the error

Error in .local(x, ...) : 
  unused arguments (xmin = -179.996887207031, ymin = c(....)

This is the code.

library(ncdf4) 
library(raster) 
library(rgdal) 
library(ggplot2) 

nc_data <- nc_open("TROPOSIF_L2B_2018-06-01.nc")

lon <- ncvar_get(nc_data, "PRODUCT/longitude", verbose=TRUE)
lat <- ncvar_get(nc_data, "PRODUCT/latitude", verbose=TRUE)
SIF_743 <- ncvar_get(nc_data,"PRODUCT/SIF_743", verbose=TRUE)

r <- raster(t(SIF_743), xmin=min(lon), xmx=max(lon), ymin=(lat), ymx=max(lat), crs=CRS(" init=epsg:4326"))

r <- flip(r, direction='y')

plot(r)

CodePudding user response:

It is probably easier, and more reliable, to do this with gdal:

gdal_translate NETCDF:"Input_FileName.nc":variable_name Output_FileName.tif

Relevant guidance: https://nsidc.org/support/how/how-convert-golive-netcdf-variables-geotiff

CodePudding user response:

You can do

library(terra)
nc <- rast("TROPOSIF_L2B_2018-06-01.nc")
nc <- writeRaster(nc, "TROPOSIF_L2B_2018-06-01.tif")

terra uses GDAL under the hood.

Or perhaps with the older raster package

library(raster)
ncr <- brick("TROPOSIF_L2B_2018-06-01.nc")
ncr <- writeRaster(ncr, "TROPOSIF_L2B_2018-06-01.tif")

However, with your files

r <- rast("TROPOSIF_L2B_2018-06-01.nc")
#Warning message:
#[rast] GDAL did not find an extent. Cells not equally spaced?
r
#class       : SpatRaster 
#dimensions  : 2711578, 7, 1  (nrow, ncol, nlyr)
#resolution  : 0.1428571, 3.687889e-07  (x, y)
#extent      : 0, 1, 0, 1  (xmin, xmax, ymin, ymax)
#coord. ref. : lon/lat WGS 84 
#source      : TOA_RFL 
#varname     : TOA_RFL (TOA Reflectance at atmospheric windows within 665-785 nm) 
#name        : TOA_RFL 
#unit        :       - 

In other words, you can force these data to a raster but since they are not regularly spaced, that may not be correct. But perhaps that is OK for your use case. In principle, you could read the data in as points and the rasterize or some such solution.

  • Related