umbersar
umbersar

Reputation: 1931

Use R to extract time series from netcdf data

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.

Sample netcdf

Thanks in advance.

Upvotes: 1

Views: 6611

Answers (2)

Hari Narayan Singh
Hari Narayan Singh

Reputation: 1

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.

Upvotes: 0

Mark Miller
Mark Miller

Reputation: 13103

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)

Upvotes: 1

Related Questions