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

Summary Spacetime allows the user to operate on cubes mathematically and functionally through the cube_smahser() function. This gives spacetime a lot of control through the use of user defined functions, pre-defined functions, user specified equations and variables.

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", length=101, scale = "year")

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

# what does the cube look like
cube.get_data_array()
<xarray.DataArray (variables: 2, lat: 149, lon: 297, time: 101)>
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.2370000e+03,  1.2070000e+03,  1.1830000e+03],
         [ 2.0893887e+02,  2.0893887e+02,  3.6000000e+02, ...,
           8.3000000e+02,  8.1100000e+02,  7.6800000e+02],
         ...,
         [-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 ... 1964-11-23T17:31:44

Mathmatical Operations

We can use a combination of cubes, numpy arrays and scalar values to rescale values in a cube or compute some important cell-wise quantity. Here we take our cube, add 1000 to each cell and raise it to the 2nd power. Parent cube is the cube whose structure you want the output cube to use.

# plot the cube and output the data set in dataframe format that made the plot
t=plot_cube(cube=cube, plot_type = "space", summary = "mean")

# convert a cube into a dataframe
#df = cube_to_dataframe(cube=answer)