简体   繁体   中英

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. But this is very long when I have many files. Is it possible to extract all 43 layers in one file?

I have used this How to extract all levels from a netcdf file using the raster package? and Plotting netcdf file with levels in R as a guidance

In essence the nitrate data from
https://www.nodc.noaa.gov/cgi-bin/OC5/woa18/woa18oxnu.pl has the concentration at 43 different depths. 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

I get a different result (0.5 for Jan level 1) but my colleague gets 1.4 for Jan level 1). 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.

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: In .getCRSfromGridMap4(atts) : cannot process these parts of the 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:

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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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