Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
423 views
in Technique[技术] by (71.8m points)

netcdf4 - Extract one variable from NetCDF to create raster brick

I'm trying to extract a variable from a multivariable netcdf file and then create a raster brick for manipulation. I've read many posts about this process but could not find a working solution and hope this post may help others too. Library netcdf4 and raster used in my code below.

Here is a hyperlink to the netcdf file "test.nc".

This file consists of a suite of SST products used for coral reef research (i.e., multivariable). Here, I pick one for an example and show the problem. First, I set the working directory to where my .nc file is located and try a simple approach that doesn't work.

Approach 1:

b<-brick('test.nc')

Here is the warning message: Warning message:

In .varName(nc, varname, warn = warn) : varname used is: CRW_BAA If that is not correct, you can set it to one of: CRW_BAA, CRW_BAA_mask, CRW_BAA_7D_MAX, CRW_BAA_7D_MAX_mask, CRW_DHW, CRW_DHW_mask, CRW_HOTSPOT, CRW_HOTSPOT_mask, CRW_SEAICE, CRW_SST, CRW_SSTANOMALY, CRW_SSTANOMALY_mask

By default, the first variable was used to create my raster brick, but I want to create a brick from a different variable. So, I next did:

DHW <- ncvar_get(nc_data, "CRW_DHW")
b<-brick(DHW)
dim(b)

Which returns a different variable that has the proper dimensions. So, all good to this point but here comes my problem. I want to take the average for each pixel then create a simple plot using package raster.

map<-calc(b, fun=mean) # takes the average for each pixel through my timestamps
plot(test) # a simple plot

enter image description here

All looks good with the image, but I've lost my latitude and longitude which were 1 degrees each. Instead, the plot shows the correct distances of 1 for each axis, but with no true spatial reference.

This led me to try approach 2: Here, I extracted all my individual variables and tried to create a new raster.

nc_data <- nc_open('test.nc') # open netcdf file
lat <- ncvar_get(nc_data, "latitude") # extract
lon <- ncvar_get(nc_data, "longitude") # extract
time <- ncvar_get(nc_data, "time") # extract
time<-as.POSIXct(as.numeric(as.character(time)),origin="1970-01-01",tz="GMT") # set time zone
DHW <- ncvar_get(nc_data, "CRW_DHW") # finally extract my variable of interest

Now, I eventually wanted to create a new raster brick with these extractions and found a lot of differing guidance in many posts, unfortunately none that worked for me.

Greatly appreciate any guidance for the simplest way possible to switch netcdf variable(s) and create a new raster brick for manipulation that has appropriate spatial reference.

question from:https://stackoverflow.com/questions/66056259/extract-one-variable-from-netcdf-to-create-raster-brick

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...