问题
A newbie question related to R. How do I extract time series data for a particular location using R from an netdcf file. So for example, the following snapshot shows that the time series for location (1,2) is 13,28,43.
Thanks in advance.
回答1:
This might do it, where "my.variable"
is the name of the variable you are interested in:
library(survival)
library(RNetCDF)
library(ncdf)
library(date)
setwd("c:/users/mmiller21/Netcdf")
my.data <- open.nc("my.netCDF.file.nc");
my.time <- var.get.nc(my.data, "time")
n.latitudes <- length(var.get.nc(my.data, "lat"))
n.longitudes <- length(var.get.nc(my.data, "lon"))
n.dates <- trunc(length(my.time))
n.dates
my.object <- var.get.nc(my.data, "my.variable")
my.array <- array(my.object, dim = c(n.latitudes, n.longitudes, n.dates))
my.array[,,1:5]
my.vector <- my.array[1, 2, 1:n.dates] # first latitude, second longitude
head(my.vector)
baseDate <- att.get.nc(my.data, "NC_GLOBAL", "base_date")
bdInt <- as.integer(baseDate[1])
year <- date.mdy(seq(mdy.date(1, 1, bdInt), by = 1,
length.out = length(my.vector)))$year
head(year)
回答2:
your_data <- read.csv("")
#Subsetting your data
location12 <- subset(your_data, latitude == 1 & column2_value == 2)
Data_location12 <- table(location12)
timeseries12 <- ts(Data_location12)
This should work.
来源:https://stackoverflow.com/questions/18819112/use-r-to-extract-time-series-from-netcdf-data