Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从R中的netCDF提取时间序列_R_Excel_Extract_Netcdf_R Raster - Fatal编程技术网

从R中的netCDF提取时间序列

从R中的netCDF提取时间序列,r,excel,extract,netcdf,r-raster,R,Excel,Extract,Netcdf,R Raster,我创造了这个 在1998-01-01至1998-12-31期间使用TRMM_3B42_日用品。这是我在R中使用的脚本: lon=seq(-91.875,-86.875,by= 0.25) lat=seq(13.875,16.875,by= 0.25) x_dim <- ncdim_def( "lon", "degrees_east", lon, create_dimvar=TRUE) y_dim <- ncdim_def( "lat", "degrees_north", lat,

我创造了这个 在1998-01-01至1998-12-31期间使用TRMM_3B42_日用品。这是我在R中使用的脚本:

lon=seq(-91.875,-86.875,by= 0.25)
lat=seq(13.875,16.875,by= 0.25)

x_dim <- ncdim_def( "lon", "degrees_east", lon, create_dimvar=TRUE)
y_dim <- ncdim_def( "lat", "degrees_north", lat, create_dimvar=TRUE)
t_dim <- ncdim_def( "time", "days since 1997-12-31 12:00:00.0 -0:00", 1:365, unlim=FALSE)
mv=9999.900390625 
precipitation_var <- ncvar_def("precipitation", "mm", list(y_dim,x_dim,t_dim), mv)


nrow = 13 
ncol = 21 

NA.matrix=matrix(rep(NA,nrow*ncol)) 

precip=array(NA.matrix,c(nrow,ncol, 1))
for (i in 1:length(test01)){precip_nc=nc_open(test01[i])
precip_get_nc=ncvar_get(precip_nc,"precipitation") 
precip=abind(precip,precip_get_nc)}

precip=precip[,,-1]  

PRECIPITATION_nc = nc_create("PRECIPITATION_1998.nc", precipitation_var)

precipitation_nc_put=ncvar_put (PRECIPITATION_nc, precipitation_var, precip)

nc_close(PRECIPITATION_nc)
lon=seq(-91.875,-86.875,by=0.25)
lat=序列(13.875,16.875,比=0.25)

通过创建包含要从中提取数据的点的
空间点
对象,然后执行
提取
操作,可以轻松完成点数据的x_dim提取。 关于其他主题:添加“X”是因为列名不能以数字开头,所以添加了一个字符。提取后,通过一些转置,可以很容易地改变水平顺序

例如,这应该是可行的(它还解决了“X”的问题,并将格式更改为“类似列”):

库(光栅)
图书馆(stringr)
图书馆(lubridate)
图书馆(tidyverse)
如果您想要一个“长”表,可以跳过b%
突变(日期=ymd(str_sub(名称(b),2)))%>%
作为_tible()
数据点
#一个tibble:365×3
日期'1``2`
1  1998-01-01     0     0
2  1998-01-02     0     0
3  1998-01-03     0     0
4  1998-01-04     0     0
5  1998-01-05     0     0
6  1998-01-06     0     0
7  1998-01-07     0     0
8  1998-01-08     0     0
9  1998-01-09     0     0
10 1998-01-10     0     0
# ... 还有355行
绘图(点数据$日期,点数据$`1`)
b <- brick('PRECIPITATION_1998.nc')
be <- crop(b, extent(13.875, 14.125, -91.875,-91.625))
a <- aggregate(be, dim(be)[2:1], na.rm=TRUE)
v <- values(a)
write.csv(v, 'precip.csv', row.names=FALSE)
library(raster)
library(stringr)
library(lubridate)
library(tidyverse)

b <- brick('/home/lb/Temp/buttami/PRECIPITATION_1998.nc')
lon = c(-91.875,-91.625)  # Array of x coordinates
lat <- c(13.875, 14.125)  # Array of y coordinates
points <- SpatialPoints(cbind(lat,lon)), # Build a spPoints object

# Etract and tidy
points_data <- b %>% 
  raster::extract(points, df = T) %>% 
  gather(date, value, -ID) %>% 
  spread(ID, value) %>%   # Can be skipped if you want a "long" table
  mutate(date = ymd(str_sub(names(b),2))) %>% 
  as_tibble()

points_data 

# A tibble: 365 × 3
         date   `1`   `2`
       <date> <dbl> <dbl>
1  1998-01-01     0     0
2  1998-01-02     0     0
3  1998-01-03     0     0
4  1998-01-04     0     0
5  1998-01-05     0     0
6  1998-01-06     0     0
7  1998-01-07     0     0
8  1998-01-08     0     0
9  1998-01-09     0     0
10 1998-01-10     0     0
# ... with 355 more rows

plot(points_data$date,points_data$`1`)