Reproduce the Central Park HLS NDVI Exercise in Python
This Python companion uses the same scene and two sample rectangles as the QGIS HLS exercise. It follows one date from reflectance bands through a masked NDVI raster to two reproducible summaries. For the index’s meaning and limits, start with spectral bands and indices and NDVI Monitoring.
The result describes spectral greenness on one date. It does not measure plant health, biomass, or a trend.
What you need
- Python 3.11 or later.
- The three HLS GeoTIFFs listed below. Direct downloads from NASA LP DAAC require a free Earthdata Login. The files and software are free; no Google account, Earth Engine project, or paid GIS software is needed.
- About 150 MB of disk space for inputs and outputs. The script loads the 3,660 × 3,660 bands into arrays, so allow several hundred megabytes of available memory.
Make a working folder and a small virtual environment. These commands use macOS/Linux:
In Windows PowerShell, use:
New-Item -ItemType Directory -Force central-park-python-ndvi
Set-Location central-park-python-ndvi
New-Item -ItemType Directory -Force input, output
py -3.11 -m venv .venv
./.venv/Scripts/python.exe -m pip install numpy rasterio
Download the fixed scene
Use NASA Earthdata Search to find the HLS S30 Version 2.0 granule HLS.S30.T18TWL.2024170T154941.v2.0, observed on 18 June 2024. Select only:
Save them in input/ without renaming. The red and near-infrared rasters should be signed 16-bit reflectance with fill -9999; Fmask should be an unsigned byte with fill 255. All three should have 3,660 rows and columns, 30 m pixels, EPSG:32618, and the same grid. If one differs, stop and check the granule and band before continuing.
HLS reflectance uses a scale factor of 0.0001; the same factor applies to both bands and cancels in their normalized ratio. The S30 product uses 12000 as a saturation flag. Fmask bits 1–4 flag cloud, adjacency, shadow, and snow/ice; bit 5 flags water; bits 6–7 encode aerosol level. This workflow excludes cloud, adjacency, shadow, snow/ice, fill, saturation, and the highest aerosol code. It keeps water pixels so the reservoir remains a comparison sample. NASA’s current S30 product page and HLS Version 2.0 User Guide define the band values and quality bits.
Create the analysis script
Save the following as analyze.py in central-park-python-ndvi/. It checks that the inputs share one grid, writes a Float32 NDVI GeoTIFF, and records input hashes, processing choices, software versions, and sample results in output/run.json.
= .
= /
= /
=
=
=
=
=
return
=
=
=
return , ,
, , =
, , =
, , =
=
=
=
=
= +
=
=
=
= /
=
=
=
=
=
=
=
=
=
Run it from the working folder:
In Windows PowerShell, use the environment’s Python executable:
./.venv/Scripts/python.exe analyze.py
Expected output:
Great Lawn: 101 valid pixels; median NDVI 0.7441
Reservoir: 100 valid pixels; median NDVI 0.1403
geometry_mask uses Rasterio’s default selection when all_touched=False: pixels selected by their centers and by the Bresenham line algorithm along polygon boundaries. For these fixed rectangles and this grid, its counts and medians reproduce the QGIS reference. If you change the polygons or grid, recheck edge-pixel counts against the zonal-statistics method you choose. The script fails if the grids, fill values, counts, or four-decimal medians differ; check the exact granule, mask, and sample coordinates before changing an expected value. For a visual check of the lawn and reservoir rectangles, follow the QGIS exercise’s map step.
Interpret the result
The Great Lawn sample has the higher median NDVI in this scene. That is a single-date spectral comparison, not evidence of a long-term vegetation change or a measure of plant condition. Review NASA’s data use and citation guidance when sharing results, and name the product, granule, date, bands, mask, and sample areas with the values.
References
- HLS S30 product description — NASA Goddard Space Flight Center (n.d.)
- HLS Data Access and Tools — NASA Goddard Space Flight Center (n.d.)
- Harmonized Landsat and Sentinel-2 Product User Guide, Version 2.0 — Ju et al. (2026), NASA Goddard Space Flight Center and U.S. Geological Survey EROS
- Data Use and Citation Guidance — NASA Earthdata (n.d.)
- Rasterio Features API — Rasterio Developers (n.d.)
- GDAL Raster Calculator — QGIS Documentation, version 4.2
- Raster Analysis — QGIS Documentation, version 4.2
- Saving a Vector Layer — QGIS Documentation, version 4.2