[R-sig-Geo] Extract spatial mean of subset of netCDF.

Benjamin.Galuardi galuardi at mit.edu
Fri Dec 19 17:55:22 CET 2014


Hi Aseem,

I can offer the following advice:

1) there is no

dt.ras.c

variable created in your code. It would be helpful to include that line 
in the question.

2) do you need a raster brick? unless you have very large data, this 
step seems unnecessary.
3) are you looking for a mean across layers or a mean for each layer 
within the stack?
4) try using 'extract'
    mean( extract(raster, extent), na.rm=T)
4a) use na.rm=T! I'm not sure but perhaps this is causing problems.
5) double check the import. if the orientation of the raster is not what 
you think it is, you ,ay be extracting the wrong area.

hope that helps

Ben

On 12/6/2014 11:47 AM, Aseem Sharma wrote:
> Hi,
>
> I am  new with raster work in R.
>
> I am trying to get the spatial mean of a subset (define by the extent) of
> larger domain netCDF data file using raster and ncdf packages
>
> The netCDF data of Snow Water Equivalent(SWE) were obtained from
> http://www.globsnow.info/swe/archive_v2.0/. The two files I have used are
> nc1
> <https://www.dropbox.com/s/tk388w67tjg704s/GlobSnow_SWE_L3A_19900101_v2.0.nc?dl=0>
> and nc2
> <https://www.dropbox.com/s/pft2dlio96n6ncw/GlobSnow_SWE_L3A_19900102_v2.0.nc?dl=0>
> .
>
> The codes I used are
>
> library(raster)
>
> library(ncdf)
>
> nc1<-"C:\\Users\\bunug_000.BUNU\\Documents\\Dropbox\\Public\\
> GlobSnow_SWE_L3A_19900101_v2.0.nc"
>
> nc2<-"C:\\Users\\bunug_000.BUNU\\Documents\\Dropbox\\Public\\
> GlobSnow_SWE_L3A_19900102_v2.0.nc"
>
>
>
> about_nc<-open.ncdf(nc1)
>
> print(about_nc)
>
>
>
> nc_data<-stack(nc1,nc2,varname="SWE")
>
> print(nc_data)
>
> dim(nc_data)
>
> nc_data1<-brick(nc_data)
>
> plot(nc_data1)
>
> subsetd<-extent(-78,-74,50,52)
>
> subsetd
>
> crop_data <- crop(nc_data1, subsetd)
>
> dim(dt.ras.c)
>
>
>
> dt.anm<-as.data.frame(cbind(unname(cellStats(dt.ras.c,
> stat="mean")),seq(1:2))) # use unname to separate named numebr to just
> numbers
>
> str(dt.anm)
>
> colnames(dt.anm)<-c("Mean_SWE","ID")
>
>
> What is not right here so that I am not getting mean SWE. It should not be
> -1.
>
> Any suggestions will be highly appreciated.
>
> Thank you.
>
>
> ------------------
>
> "Namaste नमस्ते"
>
> Aseem Sharma
>
> Graduate Research Assistant
>
> Northern Hydrometeorology Group(NHG)
>
> Natural Resources and Environmental Studies Institute(NRESi)
>
> University of Northern British Columbia
>
> Prince George, BC, V2N 4Z9, Canada
>
> Tel: 250-960-5427
>
> Web: http://www.unbc.ca/
>
>
>   "All know the Way, but few actually walk it."
> "सबैको कल्याण होस् ।"
>
> 	[[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

-- 
==================================================
Benjamin Galuardi
PhD Student
NMFS-Seagrant Population Dynamics Fellow
SMAST, UMass Dartmouth



More information about the R-sig-Geo mailing list