Skip to content

Commit

Permalink
Formatting with flake8
Browse files Browse the repository at this point in the history
  • Loading branch information
lunasilvestre committed Sep 11, 2023
1 parent 6e5faa9 commit ee3936a
Show file tree
Hide file tree
Showing 22 changed files with 353 additions and 325 deletions.
3 changes: 3 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[flake8]
max-line-length = 88
extend-ignore = E203
23 changes: 10 additions & 13 deletions ee_lst/aster_bare_emiss.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
aster = ee.Image("NASA/ASTER_GED/AG100_003")

# Calculate ASTER FVC from NDVI
aster_ndvi = aster.select('ndvi').multiply(0.01)
aster_ndvi = aster.select("ndvi").multiply(0.01)
aster_fvc = aster_ndvi.expression(
'((ndvi - ndvi_bg) / (ndvi_vg - ndvi_bg)) ** 2',
{'ndvi': aster_ndvi, 'ndvi_bg': 0.2, 'ndvi_vg': 0.86}
"((ndvi - ndvi_bg) / (ndvi_vg - ndvi_bg)) ** 2",
{"ndvi": aster_ndvi, "ndvi_bg": 0.2, "ndvi_vg": 0.86},
)
aster_fvc = aster_fvc.where(aster_fvc.lt(0.0), 0.0)
aster_fvc = aster_fvc.where(aster_fvc.gt(1.0), 1.0)
Expand All @@ -25,30 +25,27 @@ def emiss_bare_band(band, image):
- ee.Image: Bare ground emissivity of the specified band
"""
return image.expression(
'(EM - 0.99 * fvc) / (1.0 - fvc)',
{
'EM': aster.select(band).multiply(0.001),
'fvc': aster_fvc
}
"(EM - 0.99 * fvc) / (1.0 - fvc)",
{"EM": aster.select(band).multiply(0.001), "fvc": aster_fvc},
).clip(image.geometry())


# Define functions for each band
def emiss_bare_band10(image):
return emiss_bare_band('emissivity_band10', image)
return emiss_bare_band("emissivity_band10", image)


def emiss_bare_band11(image):
return emiss_bare_band('emissivity_band11', image)
return emiss_bare_band("emissivity_band11", image)


def emiss_bare_band12(image):
return emiss_bare_band('emissivity_band12', image)
return emiss_bare_band("emissivity_band12", image)


def emiss_bare_band13(image):
return emiss_bare_band('emissivity_band13', image)
return emiss_bare_band("emissivity_band13", image)


def emiss_bare_band14(image):
return emiss_bare_band('emissivity_band14', image)
return emiss_bare_band("emissivity_band14", image)
27 changes: 12 additions & 15 deletions ee_lst/broadband_emiss.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
emiss_bare_band11,
emiss_bare_band12,
emiss_bare_band13,
emiss_bare_band14
emiss_bare_band14,
)


Expand All @@ -28,24 +28,21 @@ def add_band(dynamic, image):
def compute_emissivity(orig_band, emiss_bare_func):
orig = aster.select(orig_band).multiply(0.001)
dynam = image.expression(
'fvc * 0.99 + (1 - fvc) * em_bare',
{
'fvc': image.select('FVC'),
'em_bare': emiss_bare_func(image)
}
"fvc * 0.99 + (1 - fvc) * em_bare",
{"fvc": image.select("FVC"), "em_bare": emiss_bare_func(image)},
)
return ee.Image(ee.Algorithms.If(dynamic, dynam, orig))

em10 = compute_emissivity('emissivity_band10', emiss_bare_band10)
em11 = compute_emissivity('emissivity_band11', emiss_bare_band11)
em12 = compute_emissivity('emissivity_band12', emiss_bare_band12)
em13 = compute_emissivity('emissivity_band13', emiss_bare_band13)
em14 = compute_emissivity('emissivity_band14', emiss_bare_band14)
em10 = compute_emissivity("emissivity_band10", emiss_bare_band10)
em11 = compute_emissivity("emissivity_band11", emiss_bare_band11)
em12 = compute_emissivity("emissivity_band12", emiss_bare_band12)
em13 = compute_emissivity("emissivity_band13", emiss_bare_band13)
em14 = compute_emissivity("emissivity_band14", emiss_bare_band14)

bbe = image.expression(
'0.128 + 0.014 * em10 + 0.145 * em11 + 0.241 * em12 + \
0.467 * em13 + 0.004 * em14',
{'em10': em10, 'em11': em11, 'em12': em12, 'em13': em13, 'em14': em14}
"0.128 + 0.014 * em10 + 0.145 * em11 + 0.241 * em12 + \
0.467 * em13 + 0.004 * em14",
{"em10": em10, "em11": em11, "em12": em12, "em13": em13, "em14": em14},
)

return image.addBands(bbe.rename('BBE'))
return image.addBands(bbe.rename("BBE"))
11 changes: 8 additions & 3 deletions ee_lst/cloudmask.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# import ee


def mask_sr(image):
"""
Apply cloud mask to surface reflectance Landsat image.
Expand All @@ -11,8 +12,12 @@ def mask_sr(image):
- ee.Image: Cloud-masked Landsat image.
"""

cloud_mask = image.select('QA_PIXEL').bitwiseAnd(1 << 3) \
.Or(image.select('QA_PIXEL').bitwiseAnd(1 << 4)).eq(0)
cloud_mask = (
image.select("QA_PIXEL")
.bitwiseAnd(1 << 3)
.Or(image.select("QA_PIXEL").bitwiseAnd(1 << 4))
.eq(0)
)
return image.updateMask(cloud_mask)


Expand All @@ -27,5 +32,5 @@ def mask_toa(image):
- ee.Image: Cloud-masked Landsat image.
"""

cloud_mask = image.select('QA_PIXEL').bitwiseAnd(1 << 3).eq(0)
cloud_mask = image.select("QA_PIXEL").bitwiseAnd(1 << 3).eq(0)
return image.updateMask(cloud_mask)
66 changes: 27 additions & 39 deletions ee_lst/compute_emissivity.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,67 +17,55 @@ def add_emissivity_band(landsat, use_ndvi, image):
- ee.Image: Image with added EM band
"""

c13 = {
'L4': 0.3222,
'L5': -0.0723,
'L7': 0.2147,
'L8': 0.6820
}.get(landsat, 0.6820) # Default to L8 if not found
c13 = {"L4": 0.3222, "L5": -0.0723, "L7": 0.2147, "L8": 0.6820}.get(
landsat, 0.6820
) # Default to L8 if not found

c14 = {
'L4': 0.6498,
'L5': 1.0521,
'L7': 0.7789,
'L8': 0.2578
}.get(landsat, 0.2578) # Default to L8 if not found
c14 = {"L4": 0.6498, "L5": 1.0521, "L7": 0.7789, "L8": 0.2578}.get(
landsat, 0.2578
) # Default to L8 if not found

c = {
'L4': 0.0272,
'L5': 0.0195,
'L7': 0.0059,
'L8': 0.0584
}.get(landsat, 0.0584) # Default to L8 if not found
c = {"L4": 0.0272, "L5": 0.0195, "L7": 0.0059, "L8": 0.0584}.get(
landsat, 0.0584
) # Default to L8 if not found

# Get ASTER emissivity and convolve to Landsat band
emiss_bare = image.expression(
'c13 * EM13 + c14 * EM14 + c',
"c13 * EM13 + c14 * EM14 + c",
{
'EM13': emiss_bare_band13(image),
'EM14': emiss_bare_band14(image),
'c13': ee.Image(c13),
'c14': ee.Image(c14),
'c': ee.Image(c)
}
"EM13": emiss_bare_band13(image),
"EM14": emiss_bare_band14(image),
"c13": ee.Image(c13),
"c14": ee.Image(c14),
"c": ee.Image(c),
},
)

# Compute the dynamic emissivity for Landsat
EMd = image.expression(
'fvc * 0.99 + (1 - fvc) * em_bare',
{
'fvc': image.select('FVC'),
'em_bare': emiss_bare
}
"fvc * 0.99 + (1 - fvc) * em_bare",
{"fvc": image.select("FVC"), "em_bare": emiss_bare},
)

# Compute emissivity directly from ASTER without vegetation correction
aster = ee.Image("NASA/ASTER_GED/AG100_003").clip(image.geometry())
EM0 = image.expression(
'c13 * EM13 + c14 * EM14 + c',
"c13 * EM13 + c14 * EM14 + c",
{
'EM13': aster.select('emissivity_band13').multiply(0.001),
'EM14': aster.select('emissivity_band14').multiply(0.001),
'c13': ee.Image(c13),
'c14': ee.Image(c14),
'c': ee.Image(c)
}
"EM13": aster.select("emissivity_band13").multiply(0.001),
"EM14": aster.select("emissivity_band14").multiply(0.001),
"c13": ee.Image(c13),
"c14": ee.Image(c14),
"c": ee.Image(c),
},
)

# Select which emissivity to output based on user selection
EM = EMd if use_ndvi else EM0

# Prescribe emissivity of water and snow/ice bodies
qa = image.select('QA_PIXEL')
qa = image.select("QA_PIXEL")
EM = EM.where(qa.bitwiseAnd(1 << 7), 0.99)
EM = EM.where(qa.bitwiseAnd(1 << 5), 0.989)

return image.addBands(EM.rename('EM'))
return image.addBands(EM.rename("EM"))
12 changes: 4 additions & 8 deletions ee_lst/compute_fvc.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,16 @@ def add_fvc_band(landsat, image):
- ee.Image: Image with added FVC band
"""

ndvi = image.select('NDVI')
ndvi = image.select("NDVI")

# Compute FVC
fvc = image.expression(
'((ndvi - ndvi_bg) / (ndvi_vg - ndvi_bg)) ** 2',
{
'ndvi': ndvi,
'ndvi_bg': 0.2,
'ndvi_vg': 0.86
}
"((ndvi - ndvi_bg) / (ndvi_vg - ndvi_bg)) ** 2",
{"ndvi": ndvi, "ndvi_bg": 0.2, "ndvi_vg": 0.86},
)

# Apply constraints to FVC values
fvc = fvc.where(fvc.lt(0.0), 0.0)
fvc = fvc.where(fvc.gt(1.0), 1.0)

return image.addBands(fvc.rename('FVC'))
return image.addBands(fvc.rename("FVC"))
20 changes: 10 additions & 10 deletions ee_lst/compute_ndvi.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@ def add_ndvi_band(landsat, image):
"""

# Choose bands based on the Landsat satellite ID
if landsat in ['L8', 'L9']:
nir = 'SR_B5'
red = 'SR_B4'
if landsat in ["L8", "L9"]:
nir = "SR_B5"
red = "SR_B4"
else:
nir = 'SR_B4'
red = 'SR_B3'
nir = "SR_B4"
red = "SR_B3"

# Compute NDVI
ndvi = image.expression(
'(nir - red) / (nir + red)',
"(nir - red) / (nir + red)",
{
'nir': image.select(nir).multiply(0.0000275).add(-0.2),
'red': image.select(red).multiply(0.0000275).add(-0.2)
}
).rename('NDVI')
"nir": image.select(nir).multiply(0.0000275).add(-0.2),
"red": image.select(red).multiply(0.0000275).add(-0.2),
},
).rename("NDVI")

return image.addBands(ndvi)
Loading

0 comments on commit ee3936a

Please sign in to comment.