Fwd: netCDF to GeoTIFF by layer in r

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Fwd: netCDF to GeoTIFF by layer in r

Ahmed Attia
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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: netCDF to GeoTIFF by layer in r

Ben Tupper-2
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 <[hidden email]> 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]]
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: netCDF to GeoTIFF by layer in r

Ahmed Attia
Hi Ben,

Adding 'band' to raster provided what I want;

importnetcdf <- raster(file.nc,band=4)
writeRaster(importnetcdf,file.tiff,"GTiff",overwrite=FALSE)

Thanks for your help.

Ahmed



Ahmed Attia, Ph.D.
Agronomist & Soil Scientist





On Wed, Mar 28, 2018 at 11:53 AM, Ben Tupper <[hidden email]> wrote:

> 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
>
> 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 <[hidden email]> 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_Euca
> lipto\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]]
>
> ______________________________________________
> [hidden email] 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/posti
> ng-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>
> Ben Tupper
> Bigelow Laboratory for Ocean Sciences
> 60 Bigelow Drive
> <https://maps.google.com/?q=60+Bigelow+Drive&entry=gmail&source=g>, P.O.
> Box 380
> East Boothbay, Maine 04544
> http://www.bigelow.org
>
> Tick Forecasting: https://eco.bigelow.org/
>
>
>
>
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.