[R-sig-Geo] Selecting variable within nc file in raster() or brick()

Jens Floeter jfloeter at uni-hamburg.de
Fri Jan 9 13:17:28 CET 2015


Hi Agus,

# i also find it difficult to work with 4D netcdf files and raster 
objects...they seem not be well suited for 4D data...
# you may try a loop to extact the variables (var1, var2,...) at 
different levels

n <- 11
for (i in 1:n) {
   names <- paste("outputname.brick.layer", i, sep=".")
   assign(names, brick("inputname.nc", varname ="var1", lvar = 3, level 
= i) )
}

# ....  and stack them later, eg.

stack.var1.var2 <- stack (outputname.brick.layer.1, 
outputname.brick.layer.2)

Best wishes !
jens


Am 09.01.2015 um 12:36 schrieb Agustin Lobo:
> Hi!
>
> For nc (v3) files having only 1 field variable along time such as
> open.ncdf(file.path(ncdir,nom1))
> [1] "file /media/alobo/FREECOM
> HDD/BIGFILES/IMASCOARSESTOCK/SMOS/_DELSMH001DIBEA.nc has 4
> dimensions:"
> [1] "time   Size: 257"
> [1] "lat   Size: 1100"
> [1] "lon   Size: 1600"
> [1] "time_strlen   Size: 20"
> [1] "------------------------"
> [1] "file /media/alobo/FREECOM
> HDD/BIGFILES/IMASCOARSESTOCK/SMOS/_DELSMH001DIBEA.nc has 1 variables:"
> [1] "float HighRes_Soil_Moisture[lon,lat,time]  Longname:1 km Soil
> Moisture Missval:-999"
>
> I can simply use
> nc1 <- brick(file.path(ncdir,nom1))
>
> But, what in the case of having several variables in the nc file, i.e.:
> open.ncdf(file.path(ncdir,nom5))
> [1] "file /media/alobo/FREECOM
> HDD/BIGFILES/IMASCOARSESTOCK/SMOS/_NRTSM003D025A_ALL_2013.nc has 4
> dimensions:"
> [1] "time   Size: 365"
> [1] "lat   Size: 44"
> [1] "lon   Size: 62"
> [1] "time_strlen   Size: 20"
> [1] "------------------------"
> [1] "file /media/alobo/FREECOM
> HDD/BIGFILES/IMASCOARSESTOCK/SMOS/_NRTSM003D025A_ALL_2013.nc has 4
> variables:"
> [1] "int L2_Points[lon,lat,time]  Longname:Number of Measures Missval:0"
> [1] "float SM[lon,lat,time]  Longname:Surface soil moisture Missval:-999"
> [1] "float SM_DQX[lon,lat,time]  Longname:Surface soil moisture_dqx
> Missval:-999"
> [1] "float VARIANCE_SM[lon,lat,time]  Longname:Variance of Surface
> soil moisture Missval:-999"
>
> Is there a way to select the desired variable within brick() ?
> Or should I use  get.var.ncdf() and then transform to raster?
> (the only problem in this case is that I have to rotate etc, while
> brick() does it all in the first case)
>
> Thanks
>
> Agus
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

-- 
*****************************************************
Jens Floeter
Institut für Hydrobiologie  & Fischereiwissenschaft
Universität Hamburg
Olbersweg 24
22767 Hamburg

Phone +49 (0)40 42838 6622
Fax   +49 (0)40 42838 6618



More information about the R-sig-Geo mailing list