Use R to extract time series from netcdf data

邮差的信 提交于 2021-02-10 14:18:14

问题


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.


回答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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!