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

Agustin Lobo alobolistas at gmail.com
Fri Jan 9 14:09:27 CET 2015


hmm.. strange, I do not get the same behaviour for this file:

nom5 <- "_NRTSM003D025A_ALL_2013.nc"
info5 <- open.ncdf(file.path(ncdir,nom5))
print(info5)
[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"

nc5 <- brick(file.path(ncdir,nom5), varname="SM")
Error in .rasterObjectFromCDF(x, type = objecttype, band = band, ...) :
  cells are not equally spaced; you should extract values as points

I doubt the message is true (those files are satellite images),
although cannot tell for sure because I've not made the nc file
myself.
Also, the following works and does not complain about the spacing:

nc5 <- get.var.ncdf(info5, "SM", verbose = F)
r5 <- aperm(nc5[,ncol(nc5):1,],c(2,1,3))
r5 <- brick(r5)
plot(r5[[200]])

I can use this as a workaround, but it would be cleaner using brick() directly.

I've put the file here in case somebody would like to check:
https://dl.dropboxusercontent.com/u/3180464/_NRTSM003D025A_ALL_2013.nc

Agus

On Fri, Jan 9, 2015 at 12:48 PM, Michael Sumner <mdsumner at gmail.com> wrote:
> Use the varname argument to choose a specific variable. Try
>
> brick(file.path(ncdir,nom5))
>
> it will tell you what to do. The level argument provides a similar facility
> for slicing from a 4d var, btw.
>
> Cheers, Mike
>
> On Fri, 9 Jan 2015 22:37 Agustin Lobo <alobolistas at gmail.com> wrote:
>
>> 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
>>
>
>         [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo



More information about the R-sig-Geo mailing list