Skip to content

Instantly share code, notes, and snippets.

@fivejjs
Forked from cbur24/crop_mask_feature_layer.py
Created February 15, 2021 07:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fivejjs/60a466b27262221630b9a596e5d9949e to your computer and use it in GitHub Desktop.
Save fivejjs/60a466b27262221630b9a596e5d9949e to your computer and use it in GitHub Desktop.
Feature layer function for production run of eastern crop-mask
def gm_mads_two_seasons(ds1, ds2):
"""
Feature layer function for production run of
eastern crop-mask
"""
def fun(ds, era):
#normalise SR and edev bands
for band in ds.data_vars:
if band not in ['sdev', 'bcdev']:
ds[band] = ds[band] / 10000
gm_mads = calculate_indices(ds,
index=['NDVI','LAI','MNDWI'],
drop=False,
normalise=False,
collection='s2')
gm_mads['sdev'] = -np.log(gm_mads['sdev'])
gm_mads['bcdev'] = -np.log(gm_mads['bcdev'])
gm_mads['edev'] = -np.log(gm_mads['edev'])
#rainfall climatology
if era == '_S1':
chirps = assign_crs(xr.open_rasterio('/g/data/CHIRPS/cumulative_alltime/CHPclim_jan_jun_cumulative_rainfall.nc'), crs='epsg:4326')
if era == '_S2':
chirps = assign_crs(xr.open_rasterio('/g/data/CHIRPS/cumulative_alltime/CHPclim_jul_dec_cumulative_rainfall.nc'), crs='epsg:4326')
chirps = xr_reproject(chirps,ds.geobox,"bilinear")
gm_mads['rain'] = chirps
for band in gm_mads.data_vars:
gm_mads = gm_mads.rename({band:band+era})
return gm_mads
epoch1 = fun(ds1, era='_S1')
epoch2 = fun(ds2, era='_S2')
#slope
url_slope = "https://deafrica-data.s3.amazonaws.com/ancillary/dem-derivatives/cog_slope_africa.tif"
slope = rio_slurp_xarray(url_slope, gbox=ds1.geobox)
slope = slope.to_dataset(name='slope')
result = xr.merge([epoch1,
epoch2,
slope],compat='override')
result = result.astype(np.float32)
return result.squeeze()
#call the function on the two 6-month gm+tmads
model_input = gm_mads_two_seasons(gm_s1, gm_s2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment