Skip to content

Commit

Permalink
Linting
Browse files Browse the repository at this point in the history
  • Loading branch information
lunasilvestre committed Sep 10, 2023
1 parent c6e526c commit 65f0ea9
Show file tree
Hide file tree
Showing 21 changed files with 230 additions and 109 deletions.
3 changes: 2 additions & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"vscode-icons-team.vscode-icons",
"googlecloudtools.cloudcode",
"redhat.vscode-yaml",
"johnpapa.vscode-cloak"
"johnpapa.vscode-cloak",
"ms-python.black-formatter"
]
}
}
Expand Down
6 changes: 6 additions & 0 deletions ee_lst/aster_bare_emiss.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
aster_fvc = aster_fvc.where(aster_fvc.lt(0.0), 0.0)
aster_fvc = aster_fvc.where(aster_fvc.gt(1.0), 1.0)


def emiss_bare_band(band, image):
"""
Calculate bare ground emissivity for a specific ASTER band.
Expand All @@ -31,18 +32,23 @@ def emiss_bare_band(band, image):
}
).clip(image.geometry())


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


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


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


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


def emiss_bare_band14(image):
return emiss_bare_band('emissivity_band14', image)
10 changes: 7 additions & 3 deletions ee_lst/broadband_emiss.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@
emiss_bare_band14
)


def add_band(dynamic, image):
"""
Compute broad-band emissivity from ASTER GED.
Parameters:
- dynamic (bool): If True, use vegetation cover correction. If False, use original ASTER GED emissivity.
- dynamic (bool): If True, use vegetation cover correction.
If False, use original ASTER GED emissivity.
- image (ee.Image): Input image to clip the ASTER data to its geometry.
Returns:
- ee.Image: Input image with an additional band 'BBE' for broad-band emissivity.
- ee.Image: Input image with an additional band 'BBE'
for broad-band emissivity.
"""

# Get ASTER emissivity
Expand All @@ -40,7 +43,8 @@ def compute_emissivity(orig_band, emiss_bare_func):
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',
'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}
)

Expand Down
12 changes: 6 additions & 6 deletions ee_lst/cloudmask.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import ee
# import ee

def mask_sr(image):
"""
Expand All @@ -10,11 +10,12 @@ def mask_sr(image):
Returns:
- ee.Image: Cloud-masked Landsat image.
"""
# Placeholder for cloud mask logic. The actual logic will depend on the content of the original `cloudmask.js`.
# For example:
cloud_mask = image.select('QA_PIXEL').bitwiseAnd(1 << 3).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)


def mask_toa(image):
"""
Apply cloud mask to top-of-atmosphere reflectance Landsat image.
Expand All @@ -25,7 +26,6 @@ def mask_toa(image):
Returns:
- ee.Image: Cloud-masked Landsat image.
"""
# Placeholder for cloud mask logic. The actual logic will depend on the content of the original `cloudmask.js`.
# For example:

cloud_mask = image.select('QA_PIXEL').bitwiseAnd(1 << 3).eq(0)
return image.updateMask(cloud_mask)
4 changes: 3 additions & 1 deletion ee_lst/compute_emissivity.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import ee
from ee_lst.aster_bare_emiss import emiss_bare_band13, emiss_bare_band14


def add_emissivity_band(landsat, use_ndvi, image):
"""
Compute surface emissivity for a given Landsat image using ASTER and FVC.
Parameters:
- landsat (str): ID of the Landsat satellite (e.g., 'L8')
- use_ndvi (bool): If True, NDVI values are used to obtain a dynamic emissivity;
- use_ndvi (bool): If True, NDVI values are used to
obtain a dynamic emissivity;
if False, emissivity is obtained directly from ASTER
- image (ee.Image): Input Landsat image with FVC band
Expand Down
9 changes: 6 additions & 3 deletions ee_lst/compute_fvc.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import ee
# import ee


def add_fvc_band(landsat, image):
"""
Compute Fraction of Vegetation Cover (FVC) for a given Landsat image using NDVI.
Compute Fraction of Vegetation Cover (FVC) for a
given Landsat image using NDVI.
Parameters:
- landsat (str): ID of the Landsat satellite (e.g., 'L8'). Currently not used but kept for consistency.
- landsat (str): ID of the Landsat satellite (e.g., 'L8').
Currently not used but kept for consistency.
- image (ee.Image): Input Landsat image with NDVI band
Returns:
Expand Down
3 changes: 2 additions & 1 deletion ee_lst/compute_ndvi.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ee
# import ee


def add_ndvi_band(landsat, image):
"""
Expand Down
21 changes: 13 additions & 8 deletions ee_lst/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,36 @@
'TOA': 'LANDSAT/LT04/C02/T1_TOA',
'SR': 'LANDSAT/LT04/C02/T1_L2',
'TIR': ['B6'],
'VISW': ['SR_B1', 'SR_B2', 'SR_B3', 'SR_B4', 'SR_B5', 'SR_B7', 'QA_PIXEL']
'VISW': ['SR_B1', 'SR_B2', 'SR_B3', 'SR_B4', 'SR_B5', 'SR_B7',
'QA_PIXEL']
},
'L5': {
'TOA': 'LANDSAT/LT05/C02/T1_TOA',
'SR': 'LANDSAT/LT05/C02/T1_L2',
'TIR': ['B6'],
'VISW': ['SR_B1','SR_B2','SR_B3','SR_B4','SR_B5','SR_B7','QA_PIXEL']
'VISW': ['SR_B1', 'SR_B2', 'SR_B3', 'SR_B4', 'SR_B5', 'SR_B7',
'QA_PIXEL']
},
'L7': {
'TOA': 'LANDSAT/LE07/C02/T1_TOA',
'SR': 'LANDSAT/LE07/C02/T1_L2',
'TIR': ['B6_VCID_1','B6_VCID_2'],
'VISW': ['SR_B1','SR_B2','SR_B3','SR_B4','SR_B5','SR_B7','QA_PIXEL']
'TIR': ['B6_VCID_1', 'B6_VCID_2'],
'VISW': ['SR_B1', 'SR_B2', 'SR_B3', 'SR_B4', 'SR_B5', 'SR_B7',
'QA_PIXEL']
},
'L8': {
'TOA': 'LANDSAT/LC08/C02/T1_TOA',
'SR': 'LANDSAT/LC08/C02/T1_L2',
'TIR': ['B10','B11'],
'VISW': ['SR_B1','SR_B2','SR_B3','SR_B4','SR_B5','SR_B6','SR_B7','QA_PIXEL']
'TIR': ['B10', 'B11'],
'VISW': ['SR_B1', 'SR_B2', 'SR_B3', 'SR_B4', 'SR_B5', 'SR_B6',
'SR_B7', 'QA_PIXEL']
},
'L9': {
'TOA': 'LANDSAT/LC09/C02/T1_TOA',
'SR': 'LANDSAT/LC09/C02/T1_L2',
'TIR': ['B10','B11'],
'VISW': ['SR_B1','SR_B2','SR_B3','SR_B4','SR_B5','SR_B6','SR_B7','QA_PIXEL']
'TIR': ['B10', 'B11'],
'VISW': ['SR_B1', 'SR_B2', 'SR_B3', 'SR_B4', 'SR_B5', 'SR_B6', 'SR_B7',
'QA_PIXEL']
}
}

Expand Down
35 changes: 22 additions & 13 deletions ee_lst/landsat_lst.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,22 @@
from ee_lst.smw_algorithm import add_lst_band
from ee_lst.constants import LANDSAT_BANDS


def initialize_ee():
if not ee.data._initialized:
try:
ee.Initialize()
except Exception as e:
except Exception:
print("Please authenticate Google Earth Engine first.")
ee.Authenticate()
ee.Initialize()

def fetch_landsat_collection(landsat, date_start, date_end, geometry, use_ndvi):

def fetch_landsat_collection(landsat, date_start, date_end,
geometry, use_ndvi):
"""
Fetches a Landsat collection based on the provided parameters and applies several transformations.
Fetches a Landsat collection based on the provided parameters
and applies several transformations.
Parameters:
- landsat: Name of the Landsat collection (e.g., 'L8')
Expand All @@ -35,27 +39,32 @@ def fetch_landsat_collection(landsat, date_start, date_end, geometry, use_ndvi):

# Check if the provided Landsat collection is valid
if landsat not in LANDSAT_BANDS.keys():
raise ValueError(f"Invalid Landsat constellation: {landsat}. Valid options are: {list(LANDSAT_BANDS.keys())}")
raise ValueError(f"Invalid Landsat constellation: {landsat}. \
Valid options are: {list(LANDSAT_BANDS.keys())}")

collection_dict = LANDSAT_BANDS[landsat]

# Load TOA Radiance/Reflectance
landsat_toa = ee.ImageCollection(collection_dict['TOA']).filterDate(date_start, date_end).filterBounds(geometry)
landsat_toa = (ee.ImageCollection(collection_dict['TOA'])
.filterDate(date_start, date_end)
.filterBounds(geometry))

# Load Surface Reflectance collection for NDVI and apply transformations
landsat_sr = (ee.ImageCollection(collection_dict['SR'])
.filterDate(date_start, date_end)
.filterBounds(geometry)
.map(mask_sr)
.map(lambda image: add_ndvi_band(landsat, image))
.map(lambda image: add_fvc_band(landsat, image))
.map(add_tpw_band)
.map(lambda image: add_emissivity_band(landsat, use_ndvi, image)))
.filterDate(date_start, date_end)
.filterBounds(geometry)
.map(mask_sr)
.map(lambda image: add_ndvi_band(landsat, image))
.map(lambda image: add_fvc_band(landsat, image))
.map(add_tpw_band)
.map(lambda image: add_emissivity_band(landsat,
use_ndvi, image)))

# Combine collections
tir = collection_dict['TIR']
visw = collection_dict['VISW'] + ['NDVI', 'FVC', 'TPW', 'TPWpos', 'EM']
landsat_all = landsat_sr.select(visw).combine(landsat_toa.select(tir), True)
landsat_all = landsat_sr.select(visw).combine(landsat_toa.select(tir),
True)

# Compute the LST
landsat_lst = landsat_all.map(lambda image: add_lst_band(landsat, image))
Expand Down
32 changes: 21 additions & 11 deletions ee_lst/ncep_tpw.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import ee


def add_tpw_band(image):
"""
Add total precipitable water values and index for the LUT of SMW algorithm coefficients to the image.
Add total precipitable water values and index for
the LUT of SMW algorithm coefficients to the image.
Parameters:
- image (ee.Image): Image for which to interpolate the TPW data. Needs the 'system:time_start' image property.
- image (ee.Image): Image for which to interpolate the TPW data.
Needs the 'system:time_start' image property.
Returns:
- ee.Image: Image with added 'TPW' and 'TPWpos' bands.
Expand All @@ -15,25 +18,32 @@ def add_tpw_band(image):
year = date.get('year')
month = date.get('month')
day = date.get('day')
dateString = year.format().cat('-').cat(month.format()).cat('-').cat(day.format())
dateString = year.format() \
.cat('-').cat(month.format()) \
.cat('-').cat(day.format())
date1 = ee.Date(dateString)
date2 = date1.advance(1, 'days')

def datedist(img):
return img.set('DateDist', ee.Number(img.get('system:time_start')).subtract(date.millis()).abs())

return img.set('DateDist', ee.Number(img.get('system:time_start'))
.subtract(date.millis()).abs())

tpw_collection = (ee.ImageCollection('NCEP_RE/surface_wv')
.filterDate(date1.format('yyyy-MM-dd'), date2.format('yyyy-MM-dd'))
.map(datedist))
.filterDate(date1.format('yyyy-MM-dd'),
date2.format('yyyy-MM-dd'))
.map(datedist))

closest = tpw_collection.sort('DateDist').toList(2)

tpw1 = ee.Image(closest.get(0)).select('pr_wtr') if closest.size() else ee.Image.constant(-999.0)
tpw2 = ee.Image(closest.get(1)).select('pr_wtr') if ee.Number(closest.size()).gt(1) else tpw1
tpw1 = ee.Image(closest.get(0)).select('pr_wtr') \
if closest.size() else ee.Image.constant(-999.0)
tpw2 = ee.Image(closest.get(1)).select('pr_wtr') \
if ee.Number(closest.size()).gt(1) else tpw1

time1 = ee.Number(tpw1.get('DateDist')).divide(21600000) if ee.Number(closest.size()).gt(0) else ee.Number(1.0)
time2 = ee.Number(tpw2.get('DateDist')).divide(21600000) if ee.Number(closest.size()).gt(1) else ee.Number(0.0)
time1 = ee.Number(tpw1.get('DateDist')).divide(21600000) \
if ee.Number(closest.size()).gt(0) else ee.Number(1.0)
time2 = ee.Number(tpw2.get('DateDist')).divide(21600000) \
if ee.Number(closest.size()).gt(1) else ee.Number(0.0)

tpw = tpw1.expression('tpw1*time2+tpw2*time1', {
'tpw1': tpw1,
Expand Down
17 changes: 12 additions & 5 deletions ee_lst/smw_algorithm.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import ee
# import ee
from ee_lst.constants import SMW_COEFFICIENTS, LANDSAT_BANDS


def get_lookup_table(coeff, prop_1, prop_2):
"""
Create a lookup between two columns in a list of dictionaries.
Expand All @@ -9,6 +10,7 @@ def get_lookup_table(coeff, prop_1, prop_2):
prop_2_list = [item[prop_2] for item in coeff]
return prop_1_list, prop_2_list


def add_lst_band(landsat, image):
"""
Apply the Statistical Mono-Window algorithm to compute the LST.
Expand All @@ -22,17 +24,22 @@ def add_lst_band(landsat, image):
"""

# Select algorithm coefficients
coeff_SMW = SMW_COEFFICIENTS.get(landsat, SMW_COEFFICIENTS['L9']) # Default to L9 if not found
# Default to L9 if not found
coeff_SMW = SMW_COEFFICIENTS.get(landsat,
SMW_COEFFICIENTS['L9'])

# Create lookups for the algorithm coefficients
A_lookup = get_lookup_table(coeff_SMW, 'TPWpos', 'A')
B_lookup = get_lookup_table(coeff_SMW, 'TPWpos', 'B')
C_lookup = get_lookup_table(coeff_SMW, 'TPWpos', 'C')

# Map coefficients to the image using the TPW bin position
A_img = image.remap(A_lookup[0], A_lookup[1], 0.0, 'TPWpos').resample('bilinear')
B_img = image.remap(B_lookup[0], B_lookup[1], 0.0, 'TPWpos').resample('bilinear')
C_img = image.remap(C_lookup[0], C_lookup[1], 0.0, 'TPWpos').resample('bilinear')
A_img = image.remap(A_lookup[0], A_lookup[1], 0.0, 'TPWpos') \
.resample('bilinear')
B_img = image.remap(B_lookup[0], B_lookup[1], 0.0, 'TPWpos') \
.resample('bilinear')
C_img = image.remap(C_lookup[0], C_lookup[1], 0.0, 'TPWpos') \
.resample('bilinear')

# Select TIR band
tir = LANDSAT_BANDS[landsat]['TIR'][0]
Expand Down
Loading

0 comments on commit 65f0ea9

Please sign in to comment.