简体   繁体   English

从R中的NetCDF文件中提取所有级别

[英]Extracting all levels from NetCDF file in R

I am trying to extract all the levels from a particular NetCDF file in R. I can do this manually by extracting each level as one line of code then combining them as a data frame. 我试图从R中的特定NetCDF文件中提取所有级别。我可以通过将每个级别提取为一行代码然后将它们组合为数据帧来手动执行此操作。 But this is very long when I have many files. 但是,当我有很多文件时,这很长。 Is it possible to extract all 43 layers in one file? 是否可以在一个文件中提取所有43个图层?

I have used this How to extract all levels from a netcdf file using the raster package? 我使用过如何使用光栅包从netcdf文件中提取所有级别? and Plotting netcdf file with levels in R as a guidance 以R中的级别绘制netcdf文件作为指导

In essence the nitrate data from 本质上是来自的硝酸盐数据
https://www.nodc.noaa.gov/cgi-bin/OC5/woa18/woa18oxnu.pl has the concentration at 43 different depths. https://www.nodc.noaa.gov/cgi-bin/OC5/woa18/woa18oxnu.pl的浓度为43个不同的深度。 Is it possible to extract all the depths for a particular location? 是否可以提取特定位置的所有深度?

I can do this for one level. 我可以做一个级别。 But each level represents a depth. 但每个级别代表一个深度。 Is it possible to get all levels? 是否可以获得所有级别?

I also do not understand the 3rd warning message: In .getCRSfromGridMap4(atts) : cannot process these parts of the CRS: epsg_code=EPSG:4326 我也不明白第三条警告信息:在.getCRSfromGridMap4(atts):无法处理CRS的这些部分:epsg_code = EPSG:4326

I get a different result (0.5 for Jan level 1) but my colleague gets 1.4 for Jan level 1). 我得到了一个不同的结果(1月1日为0.5),但我的同事在1月级1获得1.4。 Is my error due to the above warning? 我的错误是由于上述警告吗?

#this works
Nit_Jan <- brick("~woa18_all_n01_01.nc", stopIfNotEqualSpaced =    
FALSE, varname = "n_an", level = 1)

#this doesn't
Nit_Jan <- brick("~woa18_all_n01_01.nc", stopIfNotEqualSpaced = 
FALSE, varname = "n_an", level = 1:43)

Warning messages:
1: In if (level <= 0) { :
the condition has length > 1 and only the first element will be used 
2: In if (oldlevel != level) { :
the condition has length > 1 and only the first element will be used
3: In .getCRSfromGridMap4(atts) : cannot process these parts of the   
CRS:epsg_code=EPSG:4326

I would like to plot nitrate by depth 我想按深度绘制硝酸盐

There is some confusion here because the file has "levels" (a fourth dimension) but the number of levels is one (so no fourth dimension). 这里有一些混淆,因为文件有“级别”(第四维)但级别数是一(所以没有第四维)。 The code should probably detect that, but for now you have to add lvar=4 to get the desired object. 代码可能应该检测到,但是现在你必须添加lvar=4才能获得所需的对象。

library(raster)
f <- "woa18_all_n01_01.nc"
b <- brick(f, var="n_oa", lvar=4)
b
#class      : RasterBrick 
#dimensions : 180, 360, 64800, 43  (nrow, ncol, ncell, nlayers)
#resolution : 1, 1  (x, y)
#extent     : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
#crs        : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
#source     : woa18_all_n01_01.nc 
#names      : X0, X5, X10, X15, X20, X25, X30, X35, X40, X45, X50, X55, X60, X65, X70, ... 
#meters     : 0, 800 (min, max)
#varname    : n_oa 
#level      : 1 

And now you can do 现在你可以做到

pt <- cbind(121.5, 27.5)
e <- extract(b, pt)
e[1:5]
#[1] 10.43725 10.37617 10.23662 13.76292 13.65862

Warning #3 警告#3

3: In .getCRSfromGridMap4(atts) : cannot process these parts of the CRS:epsg_code=EPSG:4326 3:在.getCRSfromGridMap4(atts)中:无法处理CRS的这些部分:epsg_code = EPSG:4326

can be ignored; 可以忽略; but I will fix that in the next version. 但我会在下一个版本中修复它。 I think the best thing here is 我认为这里最好的是

crs(b) <- "+init=EPSG:4326"

PS: The development version of raster now behaves better: PS: raster开发版现在表现得更好:

f <- "woa18_all_n01_01.nc"
brick(f, var="n_oa")
#class      : RasterBrick 
#dimensions : 180, 360, 64800, 43  (nrow, ncol, ncell, nlayers)
#resolution : 1, 1  (x, y)
#extent     : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
#crs        : +init=EPSG:4326 +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
#source     : woa18_all_n01_01.nc 
#names      : X0, X5, X10, X15, X20, X25, X30, X35, X40, X45, X50, X55, X60, X65, X70, ... 
#depth (meters): 0, 800 (min, max)
#varname    : n_oa 
#level      : 1 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM