0

I am using the package raster to read ncdf files, everything works fine aside from the behavior that I get when I just type the brickRaster name.

#load a .nc file
temp=brick(temp.nc)

temp
class       : RasterBrick
dimensions  : 180, 360, 64800, 2928  (nrow, ncol, ncell, nlayers)
resolution  : 1, 1  (x, y)
extent      : 0, 360, -90, 90  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84
data source : /fhgfs/data/work/crct/bfb16057/pgf/1.0deg/3hourly/tas_3hourly_1948-1948.nc
names       : X1948.01.01.00.00.00, X1948.01.01.03.00.00, X1948.01.01.06.00.00, X1948.01.01.09.00.00, X1948.01.01.12.00.00, X1948.01.01.15.00.00, X1948.01.01.18.00.00, X1948.01.01.21.00.00, X1948.01.02.00.00.00, X1948.01.02.03.00.00, X1948.01.02.06.00.00, X1948.01.02.09.00.00, X1948.01.02.12.00.00, X1948.01.02.15.00.00, X1948.01.02.18.00.00, ...
Date/time   : 1948-01-01 00:00:00, 1948-12-31 21:00:00 (min, max)
varname     : tas
level       : 1

but when I reload the workspace and just directly type temp and let R load the libraries, I only get these info

temp
Loading required package: raster
Loading required package: sp
class       : RasterBrick
dimensions  : 180, 360, 64800  (nrow, ncol, ncell)
resolution  : 1, 1  (x, y)
extent      : 0, 360, -90, 90  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84

and there is no way to get it to display the other information, I have to restart the session and load first library(raster). Another issue is that sometimes it displays min/max, and sometimes it doesn't, and sometimes the formatting it's all wacky (here it shows fine but in my R). Here is a brick I create using the setValues function when I lost all layer names.

>temp
class       : RasterBrick
dimensions  : 180, 360, 64800, 2928  (nrow, ncol, ncell, nlayers)
resolution  : 1, 1  (x, y)
extent      : 0, 360, -90, 90  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84
data source : /tmp3/RtmpHJiJol/raster/r_tmp_2016-08-18_123238_52879_95962.grd
names       :      layer.1,      layer.2,      layer.3,      layer.4,      layer.5,      layer.6,      layer.7,      layer.8,      layer.9,     layer.10,     layer.11,     layer.12,     layer.13,     layer.14,     layer.15, ...
min values  : 1.786824e-09, 1.786824e-09, 1.786253e-09, 1.785112e-09, 1.773258e-09, 1.377443e-09, 1.369023e-09, 2.394694e-09, 2.461454e-09, 2.507693e-09, 2.537011e-09, 2.585615e-09, 2.623897e-09, 2.651393e-09, 2.654030e-09, ...
max values  :     1.000675,     1.000675,     1.000675,     1.000675,     1.000671,     1.000674,     1.000672,     1.000668,     1.000675,     1.000675,     1.000672,     1.000675,     1.000655,     1.000675,     1.000671, ...
Herman Toothrot
  • 1,463
  • 3
  • 23
  • 53

1 Answers1

2

Saving and reloading a workspace is bad practice. It creates a lot of problems, and it is unfortunate that R suggests you to do that. Instead, in each session you should start from scratch using a script to reproduce what you need. If that takes a lot of time, then save intermediate files to disk.

min and max values are shown if they are known (provided by the file). Standard ncdf files do not provide this information.

The loss of layer names after setValues is not a display issue (and it could be considered a feature, not a bug). Here is houw that works:

library(raster)
r <- raster(ncol=10, nrow=10)
vals <- 1:ncell(r)
r[] <- vals
s <- stack(r,r)
names(s) <- c('a', 'b')

setValues sets new layernames based on the matrix it gets.

x <- setValues(s, cbind(vals, vals))
names(x)
#[1] "vals.1" "vals.2"    

You can of course change them

names(x) <- names(s)
#[1] "a" "b"
names(x)

Or alternatively, provide them by naming the columns of the matrix:

x <- setValues(s, cbind(a=vals, b=vals))
names(x)
#[1] "a" "b"

I cannot comment on wacky formatting if you do not show it.

Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63