rnetcdfncdf4opendapthredds

Writing an ncdf4 file to disk after accessing it through OPeNDAP using R?


I am accessing a ncdf file from a thredds server and subsetting using openDAP, however while I can open and access the file connection, I cannot seem to copy the entire *.nc file to my local disk. Is there a way to do this in R? both packages ncdf4 and RNetCDF don't seem to have dedicated functionality for this, even though it seems like a common use-case?

This is what I've tried:

url = "https://thredds.met.no/thredds/dodsC/metpparchivev3/2020/01/01/met_analysis_1_0km_nordic_20200101T00Z.nc?x[663:1:674],y[732:1:750],latitude[732:1:750][663:1:674],longitude[732:1:750][663:1:674],altitude[732:1:750][663:1:674],air_temperature_2m[0:1:0][732:1:750][663:1:674]"

ncin = ncdf4::nc_open(url)
filepath = "file.nc"
test <- ncdf4::nc_create(filepath , vars = ncin$var)
ncdf4::nc_close(ncin)

However the created file.nc seems to be non-functional. What am I doing wrong?


Solution

  • In your code you only open the remote file and create the local file but you do not do any of the actual reading and writing of data.

    Your url already selects variables and a subset of data so all you need to add to your code is the actual reading and writing. That looks like this:

    > url = "https://thredds.met.no/thredds/dodsC/metpparchivev3/2020/01/01/met_analysis_1_0km_nordic_20200101T00Z.nc?x[663:1:674],y[732:1:750],latitude[732:1:750][663:1:674],longitude[732:1:750][663:1:674],altitude[732:1:750][663:1:674],air_temperature_2m[0:1:0][732:1:750][663:1:674]"
    
    > ncin <- ncdf4::nc_open(url)
    > filepath <- "file.nc"
    > test <- ncdf4::nc_create(filepath, vars = ncin$var)
    
    > for (v %in% ncin$var) {
    +   var <- ncvar_get(ncin, v)
    +   ncvar_put(test, var)
    + }
    
    > ncdf4::nc_close(ncin)
    > ncdf4::nc_close(test)
    

    Note that this includes the time variable because that is associated with the dimension time of variable air_temperature_2m. Given that this dimension has a length of 1, you will never see it as it is a degenerate dimension and it is dropped by default. ncdf4 doesn't like these kinds of variables and throws an error if you explicitly select it.

    With package ncdfCF I can see that it is a discrete axis, the value of which is 1, but the axis doesn't have any attributes so there is no meaning attached to the axis beyond its value. If I open the same resource but without the selection attributes in the url I do get to see the attributes:

    > library(ndcfCF)
    
    > url <- "https://thredds.met.no/thredds/dodsC/metpparchivev3/2020/01/01/met_analysis_1_0km_nordic_20200101T00Z.nc"
    
    > test <- open_ncdf(url)
    > test[["time"]]
    <Time axis> [0] time
    Length   : 1 (unlimited)
    Axis     : T 
    Range    : 2020-01-01 ... 2020-01-01 (seconds) 
    Bounds   : (not set) 
    
    Attributes:
     id name          type    length value                                   
     0  units         NC_CHAR 40     seconds since 1970-01-01 00:00:00 +00:00
     1  standard_name NC_CHAR  4     time                                    
     2  _ChunkSizes   NC_INT   1     512 
    

    Unfortunately ncdfCF doesn't write netCDF files so you can't use it in your code. Do keep in mind, though, that you lose the attributes with this approach. With ncdf4 you can of course manually read and copy the attributes of the variables with the ncatt_get()\ncatt_put() pair of calls.