AloesR2512
AloesR2512

Reputation: 11

Layer names of netCDF files

I am working with big ncdf4 files. Example gridded temperature data as time series from UK met office Hadley data center: "CRUTEM.5.0.1.0 HadSST.4.0.0.0". Working with the ncdf4 dataset needs much memory if imported as an ncdf4 file and is very slow. I can read the file using the terra package as a SpatRaster. However the information on the layers is lost. The layers correspond to monthly data which can be read using ncdf4 helpers from the ncdf4 file.To use the date of the observation for analysis I need to combine the name of the layer with the observation date. This can be done by converting the terra::rast file into a dataframe and name the rows with the observation time. But this again not very efficient: Is there a way to rename the layers of the terra::rast fille. Thank You for helping

Upvotes: 0

Views: 228

Answers (1)

Robert Hijmans
Robert Hijmans

Reputation: 47491

You do not specify where you get the file or what you see. The least you should do is include the output from show(x) where x is your SpatRaster. With a similar file, I get:

r <- rast('CRUTEM.5.0.1.0.anomalies.nc')
r
#class       : SpatRaster 
#dimensions  : 36, 72, 2077  (nrow, ncol, nlyr)
#resolution  : 5, 5  (x, y)
#extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
#coord. ref. : lon/lat WGS 84 
#source      : CRUTEM.5.0.1.0.anomalies.nc:tas 
#varname     : tas (air_temperature_anomaly over land) 
#names       : tas_1, tas_2, tas_3, tas_4, tas_5, tas_6, ... 
#unit        :     K,     K,     K,     K,     K,     K, ... 
#time (days) : 1850-01-16 to 2023-01-16 

Showing that the time information is available. If you want to use time as part of a layer name you could do

names(r) <- paste0("tmp_", time(r))

Upvotes: 1

Related Questions