1

I am trying to get specific variables out of a NETcdf file using ncdf4 and the ncvar_get() function. When I open the file and print() it I can see the available variables but when I try get them out I get

"vobjtovarid4: error #F: I could not find the requsted var (or dimvar) in the file!"

[1] "var (or dimvar) name: precipitation"

[1] "file name: /Users/megmccann/Documents/Prec data/Prec data/cru_ts3.24.1901.2015.pre.dat.nc"

Error in vobjtovarid4(nc, varid, verbose = verbose, allowdimvar = TRUE) : Variable not found

Here is my script:

prec.ex.nc <- nc_open("/Users/megmccann/Documents/Prec data/Prec data/cru_ts3.24.1901.2015.pre.dat.nc")

print(prec.ex.nc)

File /Users/megmccann/Documents/Prec data/Prec data/cru_ts3.24.1901.2015.pre.dat.nc (NC_FORMAT_CLASSIC):

 1 variables (excluding dimension variables):
    double pre[lon,lat,time]   
        long_name: precipitation
        units: mm/month
        correlation_decay_distance: 450
        _FillValue: 9.96920996838687e+36
        missing_value: 9.96920996838687e+36

 3 dimensions:
    lon  Size:720
        long_name: longitude
        units: degrees_east
    lat  Size:360
        long_name: latitude
        units: degrees_north
    time  Size:1380   *** is unlimited ***
        long_name: time
        units: days since 1900-1-1
        calendar: gregorian

9 global attributes:
    Conventions: CF-1.4
    title: CRU TS3.24 Precipitation
    institution: Data held at British Atmospheric Data Centre, RAL, UK.
    source: Run ID = 1610031104

Data generated by BADC from: pre.1609291528.dtb history: Wed 19 Oct 2016 10:53:41 BST : User ianharris : Program makegridsauto.for called by update.for references: Information on the data is available at http://badc.nerc.ac.uk/data/cru/ comment: Access to these data is available to any registered CEDA user. contact: BADC acknowledgement: This update was supported by NERC NCAS and by NERC SMURPHS (NE/N006348/1)

precipitation <- ncvar_get(prec.ex.nc, "precipitation")

longitude <- ncvar_get(prec.ex.nc, "longitude")

latitude <- ncvar_get(prec.ex.nc, "latitude")

t <- ncvar_get(prec.ex.nc, "time")

Where I get the error code for all variables except time? I used the exact same code for a different NETcdf file for Temperature_anomalies and it worked fine?

Cœur
  • 37,241
  • 25
  • 195
  • 267
MMcCann
  • 31
  • 3
  • Is it because you are trying to use the long names? Try "lon" instead of "longitude". The long and short name for "time" is the same which might explain why "time" works... – Spacedman Nov 01 '16 at 16:20
  • was the error spelling _requsted_ or _requested_ (like in https://github.com/PecanProject/pecan/issues/725)? – Cœur Aug 11 '17 at 16:03
  • I ran across the same error while using the raster package - I was unable to reproduce it using the ncdf4 package. Rather than rewrite this question I created a new one with a reproducible example and sample file https://stackoverflow.com/q/67944884/199217 – David LeBauer Jun 12 '21 at 01:29

0 Answers0