Date created: June 10, 2022
Author: P. Alexander Burnham

Summary Once a cube has been adequately cleaned and analysed, it may be written out as a .nc4 file. These files, and any .nc4 file written to disk by spacetime can then be loaded back onto any machine with a spacetime installation. This will recall the cube object as it was right before it was written out. This is a useful tool for passing datasets between collaborates and/or other machines. A user can pass a cube to another person to take over or you can just pick up where you left off.

Create a cube

Let’s start by creating a cube as we have done previously

# read files and load data
data = ["demoData/Carya_ovata_sim_disc_10km.tif", "demoData/Carya_ovata_sim_disc_1km.tif"]
ds = read_data(data)

We will rescale and trim the data sets

# scale data
scaledData = raster_align(ds)
trimmedData = raster_trim(scaledData)

Finally, we will make a cube Object. Each file will be assigned to a variable (10km and 1km).

# set up time vec for 101 years
yearObj = cube_time(start="2000-12-31", length=101, scale = "month")

# make cube
cube = make_cube(data = trimmedData, fileName = "monthCube.nc4", organizeFiles = "filestovar", organizeBands="bandstotime", timeObj = yearObj, varNames = ["10km", "1km"])

Subset the Cube

Let’s create new cube that only contains time slices between 2000-12-31 and 205-12-31.

# use cube smahser to multiply cube by 5
selectedTime = select_time(cube=cube, range=["2000-12-31", "2005-12-31"])

Let’s write this cube back out. We use the same function make_cube() to write out a new cube as you would to compile a cube from a file object. The function recognizes which case is being used automatically.

# write out the cube
subCube = make_cube(data = selectedTime, fileName = "subCube.nc4")

# lets look at the dataset
subCube.get_data_array()
<xarray.DataArray (variables: 2, lat: 149, lon: 297, time: 61)>
array([[[[-3.4000000e+38, -3.4000000e+38, -3.4000000e+38, ...,
          -3.4000000e+38, -3.4000000e+38, -3.4000000e+38],
         [ 0.0000000e+00,  0.0000000e+00,  0.0000000e+00, ...,
           0.0000000e+00,  0.0000000e+00,  0.0000000e+00],
         [ 0.0000000e+00,  0.0000000e+00,  0.0000000e+00, ...,
           0.0000000e+00,  0.0000000e+00,  0.0000000e+00],
         ...,
         [-3.4000000e+38, -3.4000000e+38, -3.4000000e+38, ...,
          -3.4000000e+38, -3.4000000e+38, -3.4000000e+38],
         [-3.4000000e+38, -3.4000000e+38, -3.4000000e+38, ...,
          -3.4000000e+38, -3.4000000e+38, -3.4000000e+38],
         [-3.4000000e+38, -3.4000000e+38, -3.4000000e+38, ...,
          -3.4000000e+38, -3.4000000e+38, -3.4000000e+38]],

        [[-3.4000000e+38, -3.4000000e+38, -3.4000000e+38, ...,
          -3.4000000e+38, -3.4000000e+38, -3.4000000e+38],
         [ 0.0000000e+00,  0.0000000e+00,  0.0000000e+00, ...,
           0.0000000e+00,  0.0000000e+00,  0.0000000e+00],
         [ 0.0000000e+00,  0.0000000e+00,  0.0000000e+00, ...,
           0.0000000e+00,  0.0000000e+00,  0.0000000e+00],
...
          -3.4000000e+38, -3.4000000e+38, -3.4000000e+38],
         [-3.4000000e+38, -3.4000000e+38, -3.4000000e+38, ...,
          -3.4000000e+38, -3.4000000e+38, -3.4000000e+38],
         [-3.4000000e+38, -3.4000000e+38, -3.4000000e+38, ...,
          -3.4000000e+38, -3.4000000e+38, -3.4000000e+38]],

        [[-3.4000000e+38, -3.4000000e+38, -3.4000000e+38, ...,
          -3.4000000e+38, -3.4000000e+38, -3.4000000e+38],
         [ 8.1205005e+02,  8.1205005e+02,  1.2280000e+03, ...,
           1.6400000e+03,  1.6000000e+03,  1.5820000e+03],
         [ 2.0893887e+02,  2.0893887e+02,  3.6000000e+02, ...,
           1.4490000e+03,  1.4210000e+03,  1.4390000e+03],
         ...,
         [-3.4000000e+38, -3.4000000e+38, -3.4000000e+38, ...,
          -3.4000000e+38, -3.4000000e+38, -3.4000000e+38],
         [-3.4000000e+38, -3.4000000e+38, -3.4000000e+38, ...,
          -3.4000000e+38, -3.4000000e+38, -3.4000000e+38],
         [-3.4000000e+38, -3.4000000e+38, -3.4000000e+38, ...,
          -3.4000000e+38, -3.4000000e+38, -3.4000000e+38]]]],
      dtype=float32)
Coordinates:
  * variables  (variables) <U4 '10km' '1km'
  * lon        (lon) float32 -83.75 -83.67 -83.58 -83.5 ... -59.25 -59.17 -59.08
  * lat        (lat) float32 49.0 48.92 48.83 48.75 ... 36.92 36.83 36.75 36.67
  * time       (time) datetime64[ns] 2000-12-31 2001-01-31 ... 2005-12-31

We can see that we now have 61 time points and our cube file named “subCube.nc4” is written to disk. We could now close our session with our progress saved. Lets reopen the cube using the load_cube() function.

# reload the cube
reloaded_subCube = load_cube(file="subCube.nc4")

# check to make sure the data look the same
reloaded_subCube.get_data_array()
<xarray.DataArray (variables: 2, lat: 149, lon: 297, time: 61)>
array([[[[-3.4000000e+38, -3.4000000e+38, -3.4000000e+38, ...,
          -3.4000000e+38, -3.4000000e+38, -3.4000000e+38],
         [ 0.0000000e+00,  0.0000000e+00,  0.0000000e+00, ...,
           0.0000000e+00,  0.0000000e+00,  0.0000000e+00],
         [ 0.0000000e+00,  0.0000000e+00,  0.0000000e+00, ...,
           0.0000000e+00,  0.0000000e+00,  0.0000000e+00],
         ...,
         [-3.4000000e+38, -3.4000000e+38, -3.4000000e+38, ...,
          -3.4000000e+38, -3.4000000e+38, -3.4000000e+38],
         [-3.4000000e+38, -3.4000000e+38, -3.4000000e+38, ...,
          -3.4000000e+38, -3.4000000e+38, -3.4000000e+38],
         [-3.4000000e+38, -3.4000000e+38, -3.4000000e+38, ...,
          -3.4000000e+38, -3.4000000e+38, -3.4000000e+38]],

        [[-3.4000000e+38, -3.4000000e+38, -3.4000000e+38, ...,
          -3.4000000e+38, -3.4000000e+38, -3.4000000e+38],
         [ 0.0000000e+00,  0.0000000e+00,  0.0000000e+00, ...,
           0.0000000e+00,  0.0000000e+00,  0.0000000e+00],
         [ 0.0000000e+00,  0.0000000e+00,  0.0000000e+00, ...,
           0.0000000e+00,  0.0000000e+00,  0.0000000e+00],
...
          -3.4000000e+38, -3.4000000e+38, -3.4000000e+38],
         [-3.4000000e+38, -3.4000000e+38, -3.4000000e+38, ...,
          -3.4000000e+38, -3.4000000e+38, -3.4000000e+38],
         [-3.4000000e+38, -3.4000000e+38, -3.4000000e+38, ...,
          -3.4000000e+38, -3.4000000e+38, -3.4000000e+38]],

        [[-3.4000000e+38, -3.4000000e+38, -3.4000000e+38, ...,
          -3.4000000e+38, -3.4000000e+38, -3.4000000e+38],
         [ 8.1205005e+02,  8.1205005e+02,  1.2280000e+03, ...,
           1.6400000e+03,  1.6000000e+03,  1.5820000e+03],
         [ 2.0893887e+02,  2.0893887e+02,  3.6000000e+02, ...,
           1.4490000e+03,  1.4210000e+03,  1.4390000e+03],
         ...,
         [-3.4000000e+38, -3.4000000e+38, -3.4000000e+38, ...,
          -3.4000000e+38, -3.4000000e+38, -3.4000000e+38],
         [-3.4000000e+38, -3.4000000e+38, -3.4000000e+38, ...,
          -3.4000000e+38, -3.4000000e+38, -3.4000000e+38],
         [-3.4000000e+38, -3.4000000e+38, -3.4000000e+38, ...,
          -3.4000000e+38, -3.4000000e+38, -3.4000000e+38]]]],
      dtype=float32)
Coordinates:
  * variables  (variables) <U4 '10km' '1km'
  * lon        (lon) float32 -83.75 -83.67 -83.58 -83.5 ... -59.25 -59.17 -59.08
  * lat        (lat) float32 49.0 48.92 48.83 48.75 ... 36.92 36.83 36.75 36.67
  * time       (time) datetime64[ns] 2000-12-31 2001-01-31 ... 2005-12-31

The reloaded data looks just like the original dataset. This file could be passed to another user and they would be able to access the new cube using spacetime on their own machine.