0

so I pulled an hourly time series from a netcdf file and wanted to convert it to monthly data for some runs I'm doing. I keep getting the error in the title whenever I try to use the Zoo package to convert. I've tried running R as an administrator because someone else suggested that, but still can't get zoo to read the file. Any advice? Thanks!

Here's the code I use:

library(ncdf4)
library(zoo)

Clim <- nc_open("CANESM5_erw.nc")

Temp <- ncvar_get(Clim, varid="T2")
String <- toString(Temp)
Mon <- read.zoo(String, FUN = as.Date.yearmon, aggregate = mean)

I have tried running R as an administrator and formatting Temp as an array, a string, and a table.

1
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. Commented Jan 23 at 5:54

1 Answer 1

0

You are extracting an array of data from a netCDF file and then convert that data to a single string to feed to read.zoo() which is expecting a file name. This is where your error comes from.

On a more profound level, you are using the wrong packages. The zoo package is very powerful but it is not the right tool here because CanESM5 does not use a regular calendar for its "time" dimension but rather the 365_day model calendar in which no year has a leap day. The ncdf4 package is not particularly useful either, in that it does not interpret the data using the CF Metadata Conventions. Rather, you should use the ncdfCF package and then the job is easy and correct:

library(ncdfCF)

ds <- open_ncdf("~/tas_day_CanESM5_ssp434_r1i1p1f1_gn_20150101-20241231.nc"
mon <- ds[["tas"]]$summarise("tas_month", "month", mean)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.