数据的提取 [英] Extraction of data

查看:24
本文介绍了数据的提取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图从netcdf文件中提取一个区域(经度为-45W到10E,纬度为30-40E)和一个点(经度=37度,经度=-40)以及整个地理区域2000-2003年期间的垂直剖面可变质量混合比(MMR)的月平均值。 任务:

  1. 提取2000-2003年区域和点位400-450、450-500、500-550、550-600百帕的月MMR。例如,在400 hPa时,GET(纬度、经度、MMR作为栅格图层)
  2. 以栅格格式提取两个气压级别之间的每月臭氧MMR。
library(ncdf4)
nc = nc_open("E:/ERA5/ERA_2000_2003.nc")
lat=ncvar_get (nc,"latitude") ; lon=ncvar_get(nc,"longitude")
t = ncvar_get(nc, "time")
pres = ncvar_get(nc, "level")
length(lat); length(lon);length(time);length(pres)
t <- as.POSIXct("1900-01-01 00:00")+as.difftime(t,units="hours")
x <- c(400, 450, 500, 550, 600) 

#I want ozone for longitude -45W to 10E and latitude (30-40E) at pressure level 500 
#for all years (12months*4 yrs= 48 months) 

oz = ncvar_get(nc,'o3',start=c(1,1,1,1),count=c(100,-1,x[3],-1))
lonIdx <- which( lon >= -40 & lon <= 10.00);
latIdx <- which( lat >= 30.00 & lat <= 45.00)

我收到以下错误:

1. Error: cannot allocate vector of size 558.5 Mb
oz = ncvar_get(nc,'o3',start=c(lonIdx,latIdx,1,1),count=c(length(lonIdx),length(latIdx),x[1),-1))
# and sometimes error messages like
2.Error in ncvar_get_inner(ncid2use, varid2use, nc$var[[li]]$missval, addOffset,  : 
 Error: 
variable has 4 dims, but start has 264 entries.  They must match!

3. Error in ncvar_get_inner(ncid2use, varid2use, nc$var[[li]]$missval, addOffset, : Error: variable has 4 dims, but start has 324 entries.  They must match!
Traceback:

1. ncvar_get(nc, "o3", start = c(lonIdx, latIdx, 1, 1), count = c(length(lonIdx), 
 .     length(latIdx), -1, -1))
2. ncvar_get_inner(ncid2use, varid2use, nc$var[[li]]$missval, addOffset, 
 .     scaleFact, start = start, count = count, verbose = verbose, 
 .     signedbyte = signedbyte, collapse_degen = collapse_degen, 
 .     raw_datavals = raw_datavals)
3. stop(paste("Error: variable has", ndims, "dims, but start has", 
 .     length(start), "entries.  They must match!"))

我们将非常感谢您的帮助。

谢谢

netcdf

从推荐答案对象中提取感兴趣的子集的两个选项是1)使用ncvar_get函数并定义了<[2-1]和count,或者2)使用ncvar_get提取整个数组,然后使用标准的base R数组索引到子集。我不太确定如何计算MMR,所以我展示了如何提取O3值并做一些基本的汇总。如果这不能具体实现这两个任务,希望它有足够的提示让您上手。

读入数据

library(ncdf4)

# read file 
nc <- nc_open("ERA5_2000_03.nc")

# extract variable levels
lat <- ncvar_get (nc, "latitude")
lon <- ncvar_get(nc, "longitude")
t <- ncvar_get(nc, "time")
t <- as.POSIXct("1900-01-01 00:00") + as.difftime(t, units = "hours")
pres <- ncvar_get(nc, "level")

定义感兴趣区域的索引

lonIdx <- which(lon >= -40 & lon <= 10)
latIdx <- which(lat >= 30 & lat <= 45)
presIdx <- which(pres >= 475 & pres <= 525) # subset to only 500
tIdx <- which(t == t) # i.e., no subset of t

提取所有月份和压力级别的o3

# Option 1 -- use ncvar_get()
extract1 <- ncvar_get(nc,
                      'o3',
                      start = c(lonIdx[1],latIdx[1], presIdx[1], tIdx[1]),
                      count = c(length(lonIdx),length(latIdx),length(presIdx), length(tIdx)))


# Option 2 -- subset using array indexing
o3 <- ncvar_get(nc,'o3')
extract2 <- o3[lonIdx, latIdx, presIdx, ]


# Check the outputs are identical
identical(extract1, extract2)
#>TRUE

您还可以选择命名数组维度,这有助于使结果更加直观

# Name the array dimensions
dimnames(o3) <- list('lon' = lon, 
                     'lat' = lat,
                     'pres' = pres,
                     'time' = as.character(as.Date(t)))

extract3 <- o3[lonIdx,latIdx, , ]

# Monthly mean for each level of pres across the entire region of interest
apply(extract3, c('pres', 'time'), mean)
#       time
# pres    2000-01-01   2000-02-01   2000-03-01
#   400 8.627874e-08 8.303606e-08 9.857230e-08
#   450 7.911534e-08 7.790138e-08 9.043681e-08
#   500 7.421088e-08 7.398450e-08 8.510393e-08
#   550 7.074213e-08 7.102885e-08 8.128490e-08
#   600 6.817185e-08 6.840323e-08 7.853805e-08
#   ...

# o3 levels at a specific long/lat
o3['-40', '37', ,]
#       time
# pres    2000-01-01   2000-02-01   2000-03-01
#   400 8.160814e-08 8.172732e-08 1.006738e-07
#   450 7.688993e-08 7.681632e-08 9.276743e-08
#   500 7.274836e-08 7.514602e-08 8.791778e-08
#   550 6.989851e-08 7.359140e-08 8.330298e-08
#   600 6.867163e-08 7.110260e-08 8.087377e-08

这篇关于数据的提取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆