[R] netCDF to GeoTIFF by layer in r

Ben Tupper btupper at bigelow.org
Wed Mar 28 16:53:08 CEST 2018


Hi Ahmed,

When reading from a ncdf file you can use the 'varname', 'lvar' and 'level' arguments - see the 'Details' section in the docs

https://www.rdocumentation.org/packages/raster/versions/2.6-7/topics/raster <https://www.rdocumentation.org/packages/raster/versions/2.6-7/topics/raster>

We can't tell what is in the ncdf file from what you report other than it has 4 bands (layers) and that the first is called 'Volumetric.water.content.at..33.kPa' If you want to know more about the what is in the file, then I suggest you use the ncdf4 package to open the file and explore it's contents - you should get a clear picture of it's contents from that.  Something like this (untested)...

library(ncdf4)
x <- nc_open(file.nc)
x
nc_close(x)

While print lots of info about the file.

Ben

> On Mar 28, 2018, at 10:37 AM, Ahmed Attia <ahmedatia80 at gmail.com> wrote:
> 
> Hi
> 
> I have a netCDF file of volumetric soil water content at four
> different soil layers and want to convert each soil layer in the
> netCDF file to a GeoTIFF layer. This code converts the netCDF file to
> one GeoTIFF layer, i.e. unclear which soil depth.
> 
> file.nc <- "C:/Soil_Weather_data/Agro/VMC21/VMC21.nc"
> 
> file.tiff <- "C:/Soil_Weather_data/Agro /VMC21/VMC21"
> 
> importnetcdf <- raster(file.nc)
> 
>> importnetcdf
> class       : RasterLayer
> band        : 1  (of  4  bands)
> #the bands here are the four soil layers
> dimensions  : 16800, 43200, 725760000  (nrow, ncol, ncell)
> resolution  : 0.00833333, 0.00833333  (x, y)
> extent      : -180, 179.9998, -56, 83.99995  (xmin, xmax, ymin, ymax)
> coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
> data source :
> C:\FAPSEP_Eucalyptus\FAPSEP\Soil_Weather_data\Agro_IBIS_Eucalipto\VMC21\VMC21.nc
> names       : Volumetric.water.content.at..33.kPa
> z-value     : 4.5
> zvar        : VMC2
> 
> 
> 
> VMC1<-writeRaster(importnetcdf,filename="file.tiff",format="GTiff",overwrite=TRUE,bylayer=TRUE,suffix="1:4")
>   #bylayer=TRUE, did not work.
> 
>> VMC1
> class       : RasterLayer
> dimensions  : 16800, 43200, 725760000  (nrow, ncol, ncell)
> resolution  : 0.00833333, 0.00833333  (x, y)
> extent      : -180, 179.9998, -56, 83.99995  (xmin, xmax, ymin, ymax)
> coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84
> +towgs84=0,0,0
> data source : C:\Users\Ahmed\Documents\file.tif
> names       : file
> values      : 2, 80  (min, max)
> 
> Any help!
> 
> 
> Ahmed Attia, Ph.D.
> Agronomist & Soil Scientist
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 

Ben Tupper
Bigelow Laboratory for Ocean Sciences
60 Bigelow Drive, P.O. Box 380
East Boothbay, Maine 04544
http://www.bigelow.org

Tick Forecasting: https://eco.bigelow.org/





	[[alternative HTML version deleted]]



More information about the R-help mailing list