Date created: August 1, 2022
Author: P. Alexander Burnham

Load Python Environment

#load the reticulate package  

# activate environment
use_condaenv(condaenv = "myCondaEnvironment", conda = "auto", required = FALSE)

Check Python Version

import platform

# make sure python 3.9 is loaded
## 3.9.12

Load Spacetime Files

from spacetime.input.readData import read_data
from spacetime.scale.rasterAlign import raster_align
from spacetime.scale.rasterTrim import raster_trim
from spacetime.objects.fileObject import file_object
from spacetime.operations.cubeSmasher import cube_smasher
from spacetime.operations.makeCube import make_cube
from spacetime.operations.loadCube import load_cube
from import plot_cube
from spacetime.operations.time import cube_time, return_time, scale_time, select_time
from spacetime.operations.cubeToDataframe import cube_to_dataframe
import matplotlib.pyplot as plt

Aim: Here we will explore a common use case that Spacetime addresses. We will be merging different files, each containing a different year of data that need to be aligned, trimmed and stacked into a spatiotemporal cube. We will run each line of Spacetime code and then explore what it has done to the files for illustration purposes. Much more code is depicted here than would be necessary in a script for processing data, although it is always good to check that things are going to plan as you write lines of code. We will write the final object out as a cleaned data cube that could than be read back into Spacetime for further processing, sent to other users for additional analyses, or used in other programs for plotting, modeling, analyses etc.

Stacking two differently formatted files

# read files
data = ["walkthrough_data/LULC_1995.tif", "walkthrough_data/India_cropped-area_1km_2016.tif"]

# load data
fo = read_data(data) 

# let's query some attributes of this file object
fo.get_band_number() # check number of bands
## [1, 1]
fo.get_epsg_code() # check the epsg codes
## ['EPSG:32644', 'EPSG:4326']
fo.get_dims() # check the raster dimensions
## [(29484, 33555), (2373, 3269)]
fo.get_pixel_size() # check pixel size
## [100.0, 0.008868160000000002]
fo.get_nodata_value() # check no data values
## [127.0, -1.7e+308]

Let’s plot the two files in the object and see what they look like

# Get the array data
arrays = fo.get_data_array()

# plot file 1
plt.imshow(arrays[0], vmin=0, vmax=17, cmap='gray')

# plot file 2