简体   繁体   English

如何使用 R 中的纬度/经度边界从 netCDF 文件中获取子集

[英]How to take a subset from a netCDF file using latitude/longitude boundaries in R

I have a netCDF file that I wish to extract a subset from defined by latitude/longitude boundaries (ie a lat/long defined box), using the 'ncdf' package in R.我有一个 netCDF 文件,我希望使用 R 中的“ncdf”包从纬度/经度边界定义的子集(即纬度/经度定义的框)中提取子集。

A summary of my netCDF file is below.我的 netCDF 文件的摘要如下。 It has two dimensions (latitude and longitude) and 1 variable (10U_GDS4_SFC).它有两个维度(纬度和经度)和 1 个变量 (10U_GDS4_SFC)。 It is essentially a lat/long grid containing wind values:它本质上是一个包含风值的纬度/经度网格:

[1] "file example.nc has 2 dimensions:"
[1] "lat_0   Size: 1280"
[1] "lon_1   Size: 2560"
[1] "------------------------"
[1] "file example.nc has 1 variables:"
[1] "float 10U_GDS4_SFC[lon_1,lat_0]  Longname:10 metre U wind component Missval:1e+30"

The latitude variable runs from +90 to -90 and the longitude variable runs form 0 to 360.纬度变量从 +90 到 -90,经度变量从 0 到 360。

I wish to extract a subset of the overall grid using the following geographical corner boundaries:我希望使用以下地理角边界提取整个网格的子集:

bottom left corner: Lat: 34.5˚, Long: 355˚, top left corner: Lat: 44.5˚, Long: 355˚, top right corner: Lat: 44.5˚, Long: 12˚, bottom right corner: Lat: 34.5˚ , Long: 12˚左下角:Lat:34.5˚,Long:355˚,左上角:Lat:44.5˚,Long:355˚,右上角:Lat:44.5˚,Long:12˚,右下角:Lat:34.5˚ , 长: 12˚

I am aware that parts of a variable can be extracted using the get.var.ncdf() command (example below):我知道可以使用get.var.ncdf()命令(下面的示例)提取变量的get.var.ncdf()

z1 = get.var.ncdf(example.nc, "10U_GDS4_SFC", start=c(11,26), count=c(5,5))

However, I can't work out how lat/long can be incorporated so that I end up with a subsetted spatial grid containing variable values.但是,我无法确定如何合并纬度/经度,以便最终得到包含变量值的子集空间网格。 I am new to working with netCDF values in R, and any advice would be greatly appreciated.我是 R 中使用 netCDF 值的新手,任何建议将不胜感激。 Many thanks!非常感谢!

In principle you are 2/3 of the way there.原则上你是那里的 2/3。 You can of course create the starting indices using something like this:您当然可以使用以下内容创建起始索引:

require(ncdf4)

ncFile <- nc_open( MyNetCDF )
LonStartIdx <- which( ncFile$dim$lon$vals == 355)
LatStartIdx <- which( ncFile$dim$lat$vals == 34.5)

Do the same for the counts.对计数做同样的事情。 Then, read the variable you want然后,读取你想要的变量

MyVariable <- ncvar_get( ncFile, varName, start=c( LonStartIdx, LatStartIdx), count=...)

However in your case you are out of luck as far as I know.但是,就您而言,据我所知,您运气不佳。 The reading / writing netcdf routines do their stuff sequentially.读/写 netcdf 例程按顺序执行它们的工作。 Your grid wraps around since you have coordinates that go from 0 - 360 in longitude and you are interested in a box that contains the zero meridian.您的网格会环绕,因为您的坐标在经度范围为 0 - 360,并且您对包含零子午线的框感兴趣。

For you (assuming you have not too much data) it would make more sense to read in the full grid into R, and then use either subset or create indices using which and cut out your "box" in R.对您而言(假设您没有太多数据),将完整网格读入 R,然后使用subset或使用which创建索引并切出 R 中的“框”会更有意义。

ncFile <- nc_open( MyNetCDF )
LonIdx <- which( ncFile$dim$lon$vals > 355 | ncFile$dim$lon$vals < 10)
LatIdx <- which( ncFile$dim$lat$vals > 34.5 & ncFile$dim$lat$vals < 44.5)
MyVariable <- ncvar_get( ncFile, varName)[ LonIdx, LatIdx]
nc_close(ncFile)

Remark: I prefer ncdf4 , I find the syntax a bit easier to remember (and there was another advantage over the older netcdf R-package that I have forgotten...)备注:我更喜欢ncdf4 ,我发现语法更容易记住(并且我忘记了旧的 netcdf R-package 的另一个优势......)

Ok.好的。 Comments cannot be as long as I would need them, so I updated the answer No worries.评论不能像我需要的那样长,所以我更新了答案不用担心。 Let's go through the questions step by step.让我们一步一步来回答这些问题。

  • The which function way will work. which函数方式会起作用。 I use it myself.我自己用。
  • The data will be in a similar format as in the netcf file, but I am not too sure if there is some problem with the 0 meridian (I guess yes).数据的格式与 netcf 文件中的格式相似,但我不太确定 0 子午线是否有问题(我猜是的)。 You might have to swap the two halves by doing something like this (replace the corresponding line in the 2nd example)您可能必须通过执行以下操作来交换两半(替换第二个示例中的相应行)

     LonIdx <- c(which( ncFile$dim$lon$vals > 355) , which( ncFile$dim$lon$vals < 10) )

    This changes the order of the coordinate indices so that the Western part comes first and then the Eastern.这会更改坐标索引的顺序,使西部先出现,然后是东部。

  • Reformatting everything to a 2x3 data frame is possible.可以将所有内容重新格式化为 2x3 数据帧。 Take the data my 2nd code example returns (will be a matrix, [lon x lat]. Also get the values of the coordinates from获取我的第二个代码示例返回的数据(将是一个矩阵,[lon x lat]。同时从

    lon <- ncFile$dim$lon$val[LonIdx]

    (or how longitude is called in your example, same for lat ). (或在您的示例中如何调用lat ,与lat相同)。 Then assemble the matrix using然后使用组装矩阵

    cbind( rep(lat, each=length(lon)), rep(lon,length(lat)), c(myVariable) )
  • The coordinates will of course be the same as in the netcdf file...坐标当然与 netcdf 文件中的相同......

You need to samity check the last cbind, as I am only about 98% confident that I have not messed up the coordinates.您需要仔细检查最后一个 cbind,因为我只有 98% 的信心没有弄乱坐标。 In the R scripts I found on my desktop I use loops, which are... evil... This should be (a bit?) faster and is also more sensible.在我在桌面上找到的 R 脚本中,我使用循环,它们是……邪恶的……这应该(有点?)更快,也更明智。

You can also use CDO to extract the area from the bash command line first and then read the file in R:也可以先使用CDO从bash命令行中提取区域,然后在R中读取文件:

cdo sellonlatbox,-5,12,34.5,44.5 in.nc out.nc 

I note in the above discussion that there was a problem concerning the order of the latitudes.我在上面的讨论中注意到纬度顺序存在问题。 You can also use the CDO command "invertlat" to sort that out for you.您还可以使用 CDO 命令“invertlat”来为您解决这个问题。

If you are using Linux this can be achieved easily using nctoolkit ( https://nctoolkit.readthedocs.io/en/latest/ ):如果您使用的是 Linux,这可以使用 nctoolkit ( https://nctoolkit.readthedocs.io/en/latest/ ) 轻松实现:

import nctoolkit as nc
data = nc.open_data("example.nc")    
data.clip(lon = [-12, -5], lat = [35.4, 44.5])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何正确地从 R 中的 NETCDF 文件重新定位 RasterBrick 中错位的经度和纬度? - How do I, correctly, reposition misplaced longitude and latitude in a RasterBrick from NETCDF file in R? 使用纬度/经度边界在R中绘制netcdf的子集 - Plot subset of netcdf in R using lat/lon boundaries 如何更改 R 中 Netcdf 文件中的经度格式? - How to change Longitude format in a Netcdf file in R? 如何使用形状文件中的纬度和经度查找城市? - How to find city using latitude and longitude from shape file? 从paleoView导入R中的netcdf时只有正纬度和经度可能错误投影 - Only positive latitude and longitude when importing netcdf in R from paleoView probably wrong projection 使用lat / lon从netCDF文件中提取子集并将其转换为R中的.csv - Extracting subset from netCDF file using lat/lon and converting into .csv in R 如何使用 R 将区域从 shapefile 传输到 netcdf 文件? - How to transfer regions from a shapefile to a netcdf file using R? R:从JSON文件提取纬度,经度和时间 - R: Extracting latitude, longitude and time from JSON file 使用r中的选择器小工具从地图对象中获取纬度和经度 - Get latitude and longitude from map object using selector gadget in r 如何使用R在地图上显示经度和纬度线? - How to display longitude and latitude lines on a map using R?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM