View source: R/extract_ts_wald.R
extract_ts_wald | R Documentation |
For every spatial point provided the function extracts a time series values from the given netcdf4 file.
Some ndcf files in the WALD database appear to have dimensions ordered by (latitude, longitude, time), Furthermore raster reads the pixels of array data in the reverse order of the latitude values. This function applies these conversions to an sp object so that the data extract through raster will match up perfectly. The following takes a sp object as one would normally experience it.
extract_ts_wald( points, filelocation, varname, crs = CRS("+proj=longlat +datum=WGS84"), nl = NULL ) sp_2_waldncdfcoords(spobj, nc, crs = CRS("+proj=longlat +datum=WGS84")) extract_point_wald_manual(long, lat, nc, varname, nl = NULL)
points |
A Spatial Points Data Frame |
filelocation |
A string specifying the location of the netcdf4 file |
varname |
The name of the variable in the netcdf4 file to be converted into the values of the time series. |
crs |
The spatial coordinate system (in PROJ.4 format) of the ncdf file (ignoring the order of the dimensions and reversed latitudes). |
nl |
Integer. Number of layers to extract from the ncdf file. |
spobj |
a SpatialPolygons, SpatialPoints, SpatialLine, or SpatialGrid etc object |
nc |
Open netcdf file or the location of netcdf file |
long |
Longitude of point |
lat |
Latitude of point |
Some netcdf4 files have dimensions ordered as 'latitude, longitude, time'. The package raster currently has a bug and cannot extract these correctly so the function involves a work around. Each extraction is checked using custom built extraction function.
The transform assumes that the way raster reads the ncdf values is such that: (1) the latlong coordinates assigned to each pixel in the netcdf file are switched in order. (2) the latitude in the netcdf file is reversed by the function f(x) = -x - 54 where '-54' is the sum of the end points of the range in latitudes.
Following the CF conventions for netCDF file, it is assumed that the list of longitude and latitude represents the centres of pixels. The pixel that contains the point can then be found using the closest longitude and latitude, when the point is within the grid.
A matrix. Each row corresponds to the same row in the points
SPDF. Each column corresponds to a layer in the ncdf file, typically a time point..
sp_2_waldncdfcoords
: Transform an sp object to the spatial coordinates of a (latitude, longitude, time) ncdf file as interpreted by the package raster
extract_point_wald_manual
: Extract values at a single point from a netCDF file in WGS84 longlat coordinates without using the raster package.
this function should be checked with a person knowledgable of the WALD data and its projection.
source("./R/sites_2_sp_points.R") sws_sites <- readRDS("./private/data/sws_sites.rds") points <- sws_sites_2_spdf(sws_sites) filelocation <- "http://dapds00.nci.org.au/thredds/dodsC/ub8/au/OzWALD/annual/OzWALD.annual.Pg.AnnualSums.nc" varname = "AnnualSums" crs = CRS("+proj=longlat +datum=WGS84") tseries <- extract_ts_wald(points, filelocation, varname = varname, crs = crs) countries <- rworldmap::getMap(resolution = "high") #do this for a world map polygon row.names(countries) <- unlist(lapply(countries@polygons, function(x) attr(x, 'ID'))) #a polishing fix required of this data frame auspoly <- countries[16, ] #Australia outline sp_2_waldncdfcoords(auspoly) long <- 148.0779 lat <- -35.13167 nc <- "http://dapds00.nci.org.au/thredds/dodsC/ub8/au/OzWALD/annual/OzWALD.annual.Pg.AnnualSums.nc" extract_point_wald_manual(long, lat, nc)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.