I'm trying to test running a simple math operation client-side using xee, an xarray extension for the Google Earth Engine API. When I set up the Dask client and run the compute()
method I keep getting
EEException: Earth Engine client library not initialized. Run ee.Initialize()
.
I do run the ee.Initialize()
function after I set up the Dask client, but I'm assuming this function is bring ran outside the scope of the workers in my Dask cluster. So how do I run ee.Initialize()
within the scope of the cluster? I assumed that once I set up the cluster in my Jupyter notebook that any code run after the set up is run within the workers.
import ee
import xarray
from dask.distributed import Client
client = Client(n_workers=2, threads_per_worker=2, memory_limit='1GB')
client
ee.Initialize(opt_url='https://earthengine-highvolume.googleapis.com')
def prep_sr_l8(image):
# Develop masks for unwanted pixels (fill, cloud, cloud shadow).
qa_mask = image.select('QA_PIXEL').bitwiseAnd(int('11111', 2)).eq(0)
saturation_mask = image.select('QA_RADSAT').eq(0)
# Apply the scaling factors to the appropriate bands.
def get_factor_img(factor_names):
factor_list = image.toDictionary().select(factor_names).values()
return ee.Image.constant(factor_list)
scale_img = get_factor_img([
'REFLECTANCE_MULT_BAND_.|TEMPERATURE_MULT_BAND_ST_B10'])
offset_img = get_factor_img([
'REFLECTANCE_ADD_BAND_.|TEMPERATURE_ADD_BAND_ST_B10'])
scaled = image.select('SR_B.|ST_B10').multiply(scale_img).add(offset_img)
# Replace original bands with scaled bands and apply masks.
return image.addBands(scaled, None, True)\
.updateMask(qa_mask).updateMask(saturation_mask)
LTBMU = ee.FeatureCollection("projects/calfuels/assets/Boundaries/LTBMU_remove_NV_remove_lake")
ic = (ee.ImageCollection('LANDSAT/LC08/C02/T1_L2').map(prep_sr_l8).filterBounds(LTBMU.geometry())
.filterDate('2019-01-01', '2019-12-31'))
ic_xr = xarray.open_dataset(ic, engine = "ee", crs='EPSG:3310', scale = 30, chunks="auto")
ndvi = (ic_xr['SR_B5'] - ic_xr['SR_B4']) / (ic_xr['SR_B5'] - ic_xr['SR_B4'])
ic_xr['NDVI'] = ndvi
ic_xr_result = ic_xr.compute()