Fixed date extraction.
This commit is contained in:
parent
b14aab37a8
commit
d925378ff4
5 changed files with 228 additions and 103 deletions
|
|
@ -9,18 +9,21 @@ from rasterio.warp import Resampling
|
|||
from rasterio.vrt import WarpedVRT
|
||||
from rasterio import shutil as rio_shutil
|
||||
|
||||
|
||||
def _import_efast():
|
||||
"""Lazy import of efast to avoid import errors when not using efast functions."""
|
||||
try:
|
||||
import efast
|
||||
from efast.s2_processing import distance_to_clouds
|
||||
from efast.s3_processing import reproject_and_crop_s3
|
||||
|
||||
return efast, distance_to_clouds, reproject_and_crop_s3
|
||||
except ImportError:
|
||||
raise ImportError(
|
||||
"efast package not found. Install with: pip install git+https://github.com/DHI-GRAS/efast.git"
|
||||
)
|
||||
|
||||
|
||||
RESOLUTION_RATIO = 21
|
||||
|
||||
|
||||
|
|
@ -33,11 +36,22 @@ def _load_clouds(clouds_file):
|
|||
return clouds
|
||||
|
||||
|
||||
def _reproject_raster_to_target(src_path, dst_path, target_bounds, target_crs, width, height, resampling=Resampling.cubic):
|
||||
def _reproject_raster_to_target(
|
||||
src_path,
|
||||
dst_path,
|
||||
target_bounds,
|
||||
target_crs,
|
||||
width,
|
||||
height,
|
||||
resampling=Resampling.cubic,
|
||||
):
|
||||
dst_transform = rasterio.transform.from_bounds(
|
||||
target_bounds.left, target_bounds.bottom,
|
||||
target_bounds.right, target_bounds.top,
|
||||
width, height
|
||||
target_bounds.left,
|
||||
target_bounds.bottom,
|
||||
target_bounds.right,
|
||||
target_bounds.top,
|
||||
width,
|
||||
height,
|
||||
)
|
||||
with rasterio.open(src_path) as src:
|
||||
vrt_options = {
|
||||
|
|
@ -88,7 +102,9 @@ def prepare_s2(season, site_position, site_name, date_range=None):
|
|||
with rasterio.open(temp_normalized, "w", **profile) as dst:
|
||||
dst.write(data)
|
||||
|
||||
_reproject_raster_to_target(temp_normalized, refl_dst, target_bounds, target_crs, s2_width, s2_height)
|
||||
_reproject_raster_to_target(
|
||||
temp_normalized, refl_dst, target_bounds, target_crs, s2_width, s2_height
|
||||
)
|
||||
temp_normalized.unlink()
|
||||
|
||||
_, distance_to_clouds, _ = _import_efast()
|
||||
|
|
@ -149,6 +165,8 @@ def run_efast(season, site_position, site_name, date_range=None):
|
|||
fusion_output_dir.mkdir(parents=True, exist_ok=True)
|
||||
print(f"[EFAST] Starting fusion: {site_name} ({lat:.6f}, {lon:.6f}), {season}")
|
||||
|
||||
efast, _, _ = _import_efast()
|
||||
|
||||
start_str, end_str = datetime_range.split("/")
|
||||
start_date = datetime.strptime(start_str, "%Y-%m-%d")
|
||||
end_date = datetime.strptime(end_str, "%Y-%m-%d")
|
||||
|
|
@ -157,18 +175,25 @@ def run_efast(season, site_position, site_name, date_range=None):
|
|||
while current_date <= end_date:
|
||||
date_str = current_date.strftime("%Y%m%d")
|
||||
output_file = fusion_output_dir / f"REFL_{date_str}.tif"
|
||||
if output_file.exists():
|
||||
print(f"[EFAST] Skipping {date_str} (exists)")
|
||||
else:
|
||||
try:
|
||||
efast.fusion(
|
||||
current_date, s3_output_dir, s2_output_dir, fusion_output_dir,
|
||||
product="REFL", max_days=30, date_position=2,
|
||||
minimum_acquisition_importance=0.0, ratio=RESOLUTION_RATIO,
|
||||
)
|
||||
print(f"[EFAST] Saved: {output_file}" if output_file.exists() else f"[EFAST] No output for {date_str} (insufficient nearby data)")
|
||||
except Exception as e:
|
||||
print(f"[EFAST] Error processing {date_str}: {e}")
|
||||
try:
|
||||
efast.fusion(
|
||||
current_date,
|
||||
s3_output_dir,
|
||||
s2_output_dir,
|
||||
fusion_output_dir,
|
||||
product="REFL",
|
||||
max_days=30,
|
||||
date_position=2,
|
||||
minimum_acquisition_importance=0.0,
|
||||
ratio=RESOLUTION_RATIO,
|
||||
)
|
||||
print(
|
||||
f"[EFAST] Saved: {output_file}"
|
||||
if output_file.exists()
|
||||
else f"[EFAST] No output for {date_str} (insufficient nearby data)"
|
||||
)
|
||||
except Exception as e:
|
||||
print(f"[EFAST] Error processing {date_str}: {e}")
|
||||
current_date += timedelta(days=1)
|
||||
|
||||
print("[EFAST] Completed")
|
||||
|
|
|
|||
87
ndvi.py
87
ndvi.py
|
|
@ -38,14 +38,26 @@ def _get_ndvi_value(ndvi_file, site_position):
|
|||
with rasterio.open(ndvi_file) as src:
|
||||
lon, lat = site_position[1], site_position[0]
|
||||
x, y = transform_coords("EPSG:4326", src.crs, [lon], [lat])
|
||||
|
||||
# Check if point is within bounds
|
||||
if not (
|
||||
src.bounds.left <= x[0] <= src.bounds.right
|
||||
and src.bounds.bottom <= y[0] <= src.bounds.top
|
||||
):
|
||||
return None # Point is outside raster bounds
|
||||
|
||||
samples = list(src.sample([(x[0], y[0])]))
|
||||
if samples:
|
||||
value = float(samples[0][0])
|
||||
if value != 0 and not np.isnan(value):
|
||||
return value
|
||||
# Return the raw value even if 0 or NaN for diagnostic purposes
|
||||
# Check if it's actually nodata (using raster's nodata value)
|
||||
if src.nodata is not None and value == src.nodata:
|
||||
return None # This is nodata, not a valid 0 value
|
||||
if np.isnan(value):
|
||||
return None # NaN is invalid
|
||||
# 0 is a valid NDVI value (no vegetation), so return it
|
||||
return value
|
||||
except Exception:
|
||||
except Exception as e:
|
||||
print(f"Error sampling {ndvi_file.name}: {e}")
|
||||
pass
|
||||
return None
|
||||
|
||||
|
|
@ -56,21 +68,41 @@ def _create_timeseries_for_dir(output_dir, site_position, source_name):
|
|||
|
||||
for ndvi_file in sorted(output_dir.glob("*.geotiff")):
|
||||
filename = ndvi_file.name
|
||||
date_str = filename.split("_")[0]
|
||||
try:
|
||||
date = datetime.strptime(date_str, "%Y%m%d").isoformat()
|
||||
except ValueError:
|
||||
# Extract date from filename
|
||||
# Format examples:
|
||||
# - YYYYMMDD_ndvi.geotiff -> date is at [0]
|
||||
# - YYYYMMDD_0.geotiff -> date is at [0]
|
||||
# - composite_YYYYMMDD.geotiff -> date is at [1]
|
||||
parts = filename.replace(".geotiff", "").split("_")
|
||||
date_str = None
|
||||
|
||||
# Try to find a date pattern (8 digits)
|
||||
for part in parts:
|
||||
if len(part) == 8 and part.isdigit():
|
||||
date_str = part
|
||||
break
|
||||
|
||||
if date_str:
|
||||
try:
|
||||
date = datetime.strptime(date_str, "%Y%m%d").isoformat()
|
||||
except ValueError:
|
||||
date = date_str
|
||||
else:
|
||||
# Fallback: use first part (for old MSIL2A_ndvi.geotiff files)
|
||||
date_str = parts[0]
|
||||
date = date_str
|
||||
print(
|
||||
f"[NDVI-{source_name}] Warning: Could not extract date from {filename}, using '{date_str}'"
|
||||
)
|
||||
|
||||
ndvi_value = _get_ndvi_value(ndvi_file, site_position)
|
||||
if ndvi_value is None:
|
||||
print(f"[NDVI-{source_name}] Warning: Could not sample {filename}")
|
||||
elif ndvi_value == 0:
|
||||
print(f"[NDVI-{source_name}] Warning: Could not sample {filename} (NoData)")
|
||||
ndvi_value = None # Set to None for timeseries
|
||||
elif np.isnan(ndvi_value):
|
||||
print(f"[NDVI-{source_name}] Warning: Could not sample {filename} (NaN)")
|
||||
ndvi_value = None # Set to None for timeseries
|
||||
print(
|
||||
f"[NDVI-{source_name}] Warning: Could not sample {filename} (outside bounds or nodata)"
|
||||
)
|
||||
# Note: 0 is a valid NDVI value (no vegetation), so we keep it
|
||||
# The _get_ndvi_value function now properly distinguishes between
|
||||
# valid 0 values and nodata values
|
||||
|
||||
timeseries.append({"date": date, "filename": filename, "ndvi": ndvi_value})
|
||||
|
||||
|
|
@ -98,20 +130,20 @@ def _process_ndvi_files(
|
|||
try:
|
||||
with rasterio.open(geotiff_file) as src:
|
||||
if src.count < 4:
|
||||
print(f"[NDVI-{source_name}] Skipping {geotiff_file.name} (only {src.count} band(s), need 4+)")
|
||||
print(
|
||||
f"[NDVI-{source_name}] Skipping {geotiff_file.name} (only {src.count} band(s), need 4+)"
|
||||
)
|
||||
continue
|
||||
except Exception as e:
|
||||
print(f"[NDVI-{source_name}] Skipping {geotiff_file.name} (error reading: {e})")
|
||||
print(
|
||||
f"[NDVI-{source_name}] Skipping {geotiff_file.name} (error reading: {e})"
|
||||
)
|
||||
continue
|
||||
|
||||
output_file = output_dir / (
|
||||
output_namer(geotiff_file) if output_namer else geotiff_file.name
|
||||
)
|
||||
|
||||
if output_file.exists():
|
||||
print(f"[NDVI-{source_name}] Skipping {geotiff_file.name} (exists)")
|
||||
continue
|
||||
|
||||
_calculate_and_write_ndvi(geotiff_file, output_file)
|
||||
print(f"[NDVI-{source_name}] Saved: {output_file}")
|
||||
|
||||
|
|
@ -132,7 +164,18 @@ def create_ndvi_timeseries_raw(season, site_position, site_name):
|
|||
def _get_output_name_prepared(geotiff_file):
|
||||
if geotiff_file.suffix == ".tif":
|
||||
if "REFL" in geotiff_file.stem:
|
||||
date_str = geotiff_file.stem.split("_")[1]
|
||||
# For S2: S2A_MSIL2A_20240101_REFL -> date is at index [2]
|
||||
# For S3: composite_20240101.tif -> date is at index [1] after removing .tif
|
||||
parts = geotiff_file.stem.split("_")
|
||||
if len(parts) >= 3 and parts[0].startswith("S2"):
|
||||
# S2 format: S2A_MSIL2A_YYYYMMDD_REFL
|
||||
date_str = parts[2]
|
||||
elif len(parts) >= 2 and parts[0] == "composite":
|
||||
# S3 format: composite_YYYYMMDD
|
||||
date_str = parts[1]
|
||||
else:
|
||||
# Fallback: try index [1] for other formats
|
||||
date_str = parts[1] if len(parts) > 1 else parts[0]
|
||||
return f"{date_str}_ndvi.geotiff"
|
||||
return geotiff_file.name.replace(".tif", ".geotiff")
|
||||
return geotiff_file.name
|
||||
|
|
|
|||
6
run.py
6
run.py
|
|
@ -24,11 +24,11 @@ def run_pipeline(season, site_position, site_name):
|
|||
# detect_clouds(season, site_name)
|
||||
|
||||
print(f"Preparing data for EFAST fusion for {site_name}, {season}")
|
||||
# prepare_s2(season, site_position, site_name)
|
||||
# prepare_s3(season, site_position, site_name)
|
||||
prepare_s2(season, site_position, site_name)
|
||||
prepare_s3(season, site_position, site_name)
|
||||
|
||||
# print(f"Running EFAST fusion for {site_name}, {season}")
|
||||
# run_efast(season, site_position, site_name)
|
||||
run_efast(season, site_position, site_name)
|
||||
|
||||
# print(f"Generating NDVI for prepared outputs: {site_name}, {season}")
|
||||
generate_ndvi_prepared(season, site_position, site_name)
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@
|
|||
.timeseries-label { font-size: 12px; margin-bottom: 5px; color: #666; }
|
||||
.timeseries { width: 100%; height: 120px; border: 1px solid #ccc; margin-bottom: 10px; }
|
||||
.map-label { font-size: 12px; margin-bottom: 5px; color: #666; }
|
||||
.map-date { font-size: 11px; margin-top: 5px; color: #999; }
|
||||
.map { height: 500px; border: 1px solid #ccc; }
|
||||
.leaflet-image-layer { image-rendering: pixelated; }
|
||||
.leaflet-control-attribution { display: none; }
|
||||
|
|
@ -28,6 +29,10 @@
|
|||
<div class="slider-container">
|
||||
<input type="range" id="dateSlider" min="0" max="365" value="0">
|
||||
<div id="dateDisplay">2024-01-01</div>
|
||||
<label style="display: flex; align-items: center; justify-content: center; gap: 8px; margin-top: 10px;">
|
||||
<input type="checkbox" id="showClouds" checked>
|
||||
<span>Show cloud-covered data</span>
|
||||
</label>
|
||||
</div>
|
||||
<div class="maps">
|
||||
<div class="map-container">
|
||||
|
|
@ -35,8 +40,10 @@
|
|||
<div class="timeseries-label">NDVI Timeseries</div>
|
||||
<canvas id="s2timeseries" class="timeseries"></canvas>
|
||||
<div class="map-label">RGB Imagery</div>
|
||||
<div id="s2rgbdate" class="map-date"></div>
|
||||
<div id="s2map" class="map"></div>
|
||||
<div class="map-label">NDVI Imagery</div>
|
||||
<div id="s2ndvidate" class="map-date"></div>
|
||||
<div id="s2ndvimap" class="map"></div>
|
||||
</div>
|
||||
<div class="map-container">
|
||||
|
|
@ -44,8 +51,10 @@
|
|||
<div class="timeseries-label">NDVI Timeseries</div>
|
||||
<canvas id="s3timeseries" class="timeseries"></canvas>
|
||||
<div class="map-label">RGB Imagery</div>
|
||||
<div id="s3rgbdate" class="map-date"></div>
|
||||
<div id="s3map" class="map"></div>
|
||||
<div class="map-label">NDVI Imagery</div>
|
||||
<div id="s3ndvidate" class="map-date"></div>
|
||||
<div id="s3ndvimap" class="map"></div>
|
||||
</div>
|
||||
</div>
|
||||
|
|
@ -71,13 +80,17 @@
|
|||
const overlays = { s2: null, s3: null };
|
||||
const ndviOverlays = { s2: null, s3: null };
|
||||
let timeseries = { s2: [], s3: [] };
|
||||
let clouds = { s2: new Set(), s3: new Set() };
|
||||
const showCloudsCheckbox = document.getElementById("showClouds");
|
||||
|
||||
async function loadTimeseries() {
|
||||
const [s2, s3] = await Promise.all([
|
||||
fetch("../data/innsbruck/2024/ndvi/s2/timeseries.json").then(r => r.json()),
|
||||
fetch("../data/innsbruck/2024/ndvi/s3/timeseries.json").then(r => r.json())
|
||||
const [s2, s3, cloudData] = await Promise.all([
|
||||
fetch("../data/innsbruck/2024/raw/ndvi/s2/timeseries.json").then(r => r.json()),
|
||||
fetch("../data/innsbruck/2024/raw/ndvi/s3/timeseries.json").then(r => r.json()),
|
||||
fetch("../data/innsbruck/2024/clouds.json").then(r => r.json()).catch(() => ({ s2: [], s3: [] }))
|
||||
]);
|
||||
timeseries = { s2, s3 };
|
||||
clouds = { s2: new Set(cloudData.s2 || []), s3: new Set(cloudData.s3 || []) };
|
||||
drawTimeseries();
|
||||
}
|
||||
|
||||
|
|
@ -93,7 +106,10 @@
|
|||
const plotW = w - pad * 2, plotH = h - pad * 2;
|
||||
|
||||
ctx.clearRect(0, 0, w, h);
|
||||
const data = timeseries[source].filter(t => t.ndvi !== null);
|
||||
let data = timeseries[source].filter(t => t.ndvi !== null);
|
||||
if (!showCloudsCheckbox.checked && clouds[source]) {
|
||||
data = data.filter(t => !clouds[source].has(t.filename));
|
||||
}
|
||||
if (!data.length) continue;
|
||||
|
||||
const dates = data.map(t => new Date(t.date));
|
||||
|
|
@ -160,8 +176,9 @@
|
|||
const date = d.toISOString().split("T")[0].replace(/-/g, "");
|
||||
for (let i = 0; i < 3; i++) {
|
||||
const filename = `${date}_${i}.geotiff`;
|
||||
if (!showCloudsCheckbox.checked && clouds[source] && clouds[source].has(filename)) continue;
|
||||
try {
|
||||
const res = await fetch(`../data/innsbruck/2024/${source}/${filename}`);
|
||||
const res = await fetch(`../data/innsbruck/2024/raw/${source}/${filename}`);
|
||||
if (res.ok) return filename;
|
||||
} catch {}
|
||||
}
|
||||
|
|
@ -176,7 +193,8 @@
|
|||
}
|
||||
|
||||
async function loadGeotiff(source, filename) {
|
||||
const tiff = await GeoTIFF.fromArrayBuffer(await (await fetch(`../data/innsbruck/2024/${source}/${filename}`)).arrayBuffer());
|
||||
const path = `../data/innsbruck/2024/raw/${source}/${filename}`;
|
||||
const tiff = await GeoTIFF.fromArrayBuffer(await (await fetch(path)).arrayBuffer());
|
||||
const image = await tiff.getImage();
|
||||
const rasters = await image.readRasters();
|
||||
const width = image.getWidth();
|
||||
|
|
@ -219,10 +237,14 @@
|
|||
if (overlays[source]) maps[source].removeLayer(overlays[source]);
|
||||
overlays[source] = L.imageOverlay(canvas.toDataURL(), bounds, { opacity: 0.95 }).addTo(maps[source]);
|
||||
maps[source].fitBounds(bounds);
|
||||
|
||||
const dateStr = filename.split("_")[0];
|
||||
const date = `${dateStr.slice(0,4)}-${dateStr.slice(4,6)}-${dateStr.slice(6,8)}`;
|
||||
document.getElementById(`${source}rgbdate`).textContent = date;
|
||||
}
|
||||
|
||||
async function loadNDVI(source, filename) {
|
||||
const tiff = await GeoTIFF.fromArrayBuffer(await (await fetch(`../data/innsbruck/2024/ndvi/${source}/${filename}`)).arrayBuffer());
|
||||
const tiff = await GeoTIFF.fromArrayBuffer(await (await fetch(`../data/innsbruck/2024/raw/ndvi/${source}/${filename}`)).arrayBuffer());
|
||||
const image = await tiff.getImage();
|
||||
const data = Array.from((await image.readRasters())[0]);
|
||||
const width = image.getWidth();
|
||||
|
|
@ -257,6 +279,10 @@
|
|||
if (ndviOverlays[source]) ndvimaps[source].removeLayer(ndviOverlays[source]);
|
||||
ndviOverlays[source] = L.imageOverlay(canvas.toDataURL(), bounds, { opacity: 0.95 }).addTo(ndvimaps[source]);
|
||||
ndvimaps[source].fitBounds(bounds);
|
||||
|
||||
const dateStr = filename.split("_")[0];
|
||||
const date = `${dateStr.slice(0,4)}-${dateStr.slice(4,6)}-${dateStr.slice(6,8)}`;
|
||||
document.getElementById(`${source}ndvidate`).textContent = date;
|
||||
}
|
||||
|
||||
const fmtDate = (d) => `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, "0")}-${String(d.getDate()).padStart(2, "0")}`;
|
||||
|
|
@ -275,8 +301,9 @@
|
|||
const date = d.toISOString().split("T")[0].replace(/-/g, "");
|
||||
for (let i = 0; i < 3; i++) {
|
||||
const filename = `${date}_${i}.geotiff`;
|
||||
if (!showCloudsCheckbox.checked && clouds[source] && clouds[source].has(filename)) continue;
|
||||
try {
|
||||
const res = await fetch(`../data/innsbruck/2024/ndvi/${source}/${filename}`);
|
||||
const res = await fetch(`../data/innsbruck/2024/raw/ndvi/${source}/${filename}`);
|
||||
if (res.ok) return filename;
|
||||
} catch {}
|
||||
}
|
||||
|
|
@ -288,7 +315,10 @@
|
|||
async function updateImages() {
|
||||
const date = dateFromDays(parseInt(slider.value));
|
||||
dateDisplay.textContent = date;
|
||||
history.replaceState({}, "", `?date=${date}`);
|
||||
const params = new URLSearchParams();
|
||||
params.set("date", date);
|
||||
if (!showCloudsCheckbox.checked) params.set("hideClouds", "1");
|
||||
history.replaceState({}, "", `?${params}`);
|
||||
drawTimeseries();
|
||||
for (const source of ["s2", "s3"]) {
|
||||
const filename = await findFile(date, source);
|
||||
|
|
@ -310,9 +340,12 @@
|
|||
}
|
||||
}
|
||||
|
||||
const urlDate = new URLSearchParams(location.search).get("date");
|
||||
const urlParams = new URLSearchParams(location.search);
|
||||
const urlDate = urlParams.get("date");
|
||||
if (urlDate) slider.value = daysFromDate(urlDate);
|
||||
if (urlParams.get("hideClouds") === "1") showCloudsCheckbox.checked = false;
|
||||
slider.addEventListener("input", updateImages);
|
||||
showCloudsCheckbox.addEventListener("change", updateImages);
|
||||
loadTimeseries().then(updateImages);
|
||||
</script>
|
||||
</body>
|
||||
|
|
|
|||
|
|
@ -29,10 +29,6 @@
|
|||
<div class="slider-container">
|
||||
<input type="range" id="dateSlider" min="0" max="365" value="0">
|
||||
<div id="dateDisplay">2024-01-01</div>
|
||||
<label style="display: flex; align-items: center; justify-content: center; gap: 8px; margin-top: 10px;">
|
||||
<input type="checkbox" id="showClouds" checked>
|
||||
<span>Show cloud-covered data</span>
|
||||
</label>
|
||||
</div>
|
||||
<div class="maps">
|
||||
<div class="map-container">
|
||||
|
|
@ -93,18 +89,14 @@
|
|||
const overlays = { s2: null, fusion: null, s3: null };
|
||||
const ndviOverlays = { s2: null, fusion: null, s3: null };
|
||||
let timeseries = { s2: [], fusion: [], s3: [] };
|
||||
let clouds = { s2: new Set(), s3: new Set() };
|
||||
const showCloudsCheckbox = document.getElementById("showClouds");
|
||||
|
||||
async function loadTimeseries() {
|
||||
const [s2, fusion, s3, cloudData] = await Promise.all([
|
||||
fetch("../data/innsbruck/2024/ndvi/s2/timeseries.json").then(r => r.json()),
|
||||
fetch("../data/innsbruck/2024/ndvi/fusion/timeseries.json").then(r => r.json()).catch(() => []),
|
||||
fetch("../data/innsbruck/2024/ndvi/s3/timeseries.json").then(r => r.json()),
|
||||
fetch("../data/innsbruck/2024/clouds.json").then(r => r.json()).catch(() => ({ s2: [], s3: [] }))
|
||||
const [s2, fusion, s3] = await Promise.all([
|
||||
fetch("../data/innsbruck/2024/prepared/ndvi/s2/timeseries.json").then(r => r.json()),
|
||||
fetch("../data/innsbruck/2024/prepared/ndvi/fusion/timeseries.json").then(r => r.json()).catch(() => []),
|
||||
fetch("../data/innsbruck/2024/prepared/ndvi/s3/timeseries.json").then(r => r.json())
|
||||
]);
|
||||
timeseries = { s2, fusion, s3 };
|
||||
clouds = { s2: new Set(cloudData.s2 || []), s3: new Set(cloudData.s3 || []) };
|
||||
drawTimeseries();
|
||||
}
|
||||
|
||||
|
|
@ -120,21 +112,29 @@
|
|||
const plotW = w - pad * 2, plotH = h - pad * 2;
|
||||
|
||||
ctx.clearRect(0, 0, w, h);
|
||||
let data = timeseries[source].filter(t => t.ndvi !== null);
|
||||
if (!showCloudsCheckbox.checked && clouds[source]) {
|
||||
data = data.filter(t => !clouds[source].has(t.filename));
|
||||
}
|
||||
if (!data.length) continue;
|
||||
// Get all data with valid dates (dates are now in ISO format from JSON)
|
||||
let data = timeseries[source].filter(t => {
|
||||
if (!t.date) return false;
|
||||
const date = new Date(t.date);
|
||||
return !isNaN(date.getTime());
|
||||
});
|
||||
|
||||
// Filter to only entries with non-null NDVI values for plotting
|
||||
const dataWithNdvi = data.filter(t => t.ndvi !== null);
|
||||
if (!dataWithNdvi.length) continue;
|
||||
|
||||
// Use data with NDVI for plotting
|
||||
data = dataWithNdvi;
|
||||
|
||||
const dates = data.map(t => new Date(t.date));
|
||||
const minDate = new Date(Math.min(...dates));
|
||||
const maxDate = new Date(Math.max(...dates));
|
||||
const dateRange = maxDate - minDate;
|
||||
const dateRange = maxDate - minDate || 1; // Avoid division by zero
|
||||
|
||||
const ndvi = data.map(t => t.ndvi);
|
||||
const minNdvi = Math.min(...ndvi);
|
||||
const maxNdvi = Math.max(...ndvi);
|
||||
const ndviRange = maxNdvi - minNdvi;
|
||||
const ndviRange = maxNdvi - minNdvi || 1; // Avoid division by zero
|
||||
|
||||
const x = (d) => pad + ((new Date(d) - minDate) / dateRange) * plotW;
|
||||
const y = (v) => pad + plotH - ((v - minNdvi) / ndviRange) * plotH;
|
||||
|
|
@ -169,7 +169,9 @@
|
|||
ctx.lineTo(xPos, pad + plotH);
|
||||
ctx.stroke();
|
||||
|
||||
const closest = data.reduce((c, t) =>
|
||||
const validData = data.filter(t => !isNaN(new Date(t.date).getTime()));
|
||||
if (validData.length === 0) continue;
|
||||
const closest = validData.reduce((c, t) =>
|
||||
Math.abs(new Date(t.date) - new Date(currentDate)) < Math.abs(new Date(c.date) - new Date(currentDate)) ? t : c
|
||||
);
|
||||
if (closest && closest.ndvi !== null) {
|
||||
|
|
@ -191,18 +193,21 @@
|
|||
if (source === "fusion") {
|
||||
const filename = `REFL_${date}.tif`;
|
||||
try {
|
||||
const res = await fetch(`../data/innsbruck/2024/efast/fusion/${filename}`);
|
||||
const res = await fetch(`../data/innsbruck/2024/prepared/fusion/${filename}`);
|
||||
if (res.ok) return filename;
|
||||
} catch {}
|
||||
} else if (source === "s2") {
|
||||
const filename = `S2A_MSIL2A_${date}_REFL.tif`;
|
||||
try {
|
||||
const res = await fetch(`../data/innsbruck/2024/prepared/${source}/${filename}`);
|
||||
if (res.ok) return filename;
|
||||
} catch {}
|
||||
} else if (source === "s3") {
|
||||
const filename = `composite_${date}.tif`;
|
||||
try {
|
||||
const res = await fetch(`../data/innsbruck/2024/prepared/${source}/${filename}`);
|
||||
if (res.ok) return filename;
|
||||
} catch {}
|
||||
} else {
|
||||
for (let i = 0; i < 3; i++) {
|
||||
const filename = `${date}_${i}.geotiff`;
|
||||
if (!showCloudsCheckbox.checked && clouds[source] && clouds[source].has(filename)) continue;
|
||||
try {
|
||||
const res = await fetch(`../data/innsbruck/2024/${source}/${filename}`);
|
||||
if (res.ok) return filename;
|
||||
} catch {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -215,7 +220,7 @@
|
|||
}
|
||||
|
||||
async function loadGeotiff(source, filename) {
|
||||
const path = source === "fusion" ? `../data/innsbruck/2024/efast/fusion/${filename}` : `../data/innsbruck/2024/${source}/${filename}`;
|
||||
const path = `../data/innsbruck/2024/prepared/${source}/${filename}`;
|
||||
const tiff = await GeoTIFF.fromArrayBuffer(await (await fetch(path)).arrayBuffer());
|
||||
const image = await tiff.getImage();
|
||||
const rasters = await image.readRasters();
|
||||
|
|
@ -260,13 +265,23 @@
|
|||
overlays[source] = L.imageOverlay(canvas.toDataURL(), bounds, { opacity: 0.95 }).addTo(maps[source]);
|
||||
maps[source].fitBounds(bounds);
|
||||
|
||||
const dateStr = source === "fusion" ? filename.split("_")[1] : filename.split("_")[0];
|
||||
let dateStr;
|
||||
if (source === "fusion") {
|
||||
// REFL_20240101.tif -> extract 20240101
|
||||
dateStr = filename.split("_")[1].replace(".tif", "");
|
||||
} else if (source === "s2") {
|
||||
// S2A_MSIL2A_20240101_REFL.tif -> extract 20240101
|
||||
dateStr = filename.split("_")[2];
|
||||
} else if (source === "s3") {
|
||||
// composite_20240101.tif -> extract 20240101
|
||||
dateStr = filename.split("_")[1].replace(".tif", "");
|
||||
}
|
||||
const date = `${dateStr.slice(0,4)}-${dateStr.slice(4,6)}-${dateStr.slice(6,8)}`;
|
||||
document.getElementById(`${source}rgbdate`).textContent = date;
|
||||
}
|
||||
|
||||
async function loadNDVI(source, filename) {
|
||||
const tiff = await GeoTIFF.fromArrayBuffer(await (await fetch(`../data/innsbruck/2024/ndvi/${source}/${filename}`)).arrayBuffer());
|
||||
async function loadNDVI(source, filename, dateStr) {
|
||||
const tiff = await GeoTIFF.fromArrayBuffer(await (await fetch(`../data/innsbruck/2024/prepared/ndvi/${source}/${filename}`)).arrayBuffer());
|
||||
const image = await tiff.getImage();
|
||||
const data = Array.from((await image.readRasters())[0]);
|
||||
const width = image.getWidth();
|
||||
|
|
@ -302,8 +317,16 @@
|
|||
ndviOverlays[source] = L.imageOverlay(canvas.toDataURL(), bounds, { opacity: 0.95 }).addTo(ndvimaps[source]);
|
||||
ndvimaps[source].fitBounds(bounds);
|
||||
|
||||
const dateStr = source === "fusion" ? filename.split("_")[0] : filename.split("_")[0];
|
||||
const date = `${dateStr.slice(0,4)}-${dateStr.slice(4,6)}-${dateStr.slice(6,8)}`;
|
||||
let extractedDateStr;
|
||||
if (source === "fusion") {
|
||||
extractedDateStr = filename.split("_")[0];
|
||||
} else if (source === "s2") {
|
||||
// S2 NDVI files are now named YYYYMMDD_ndvi.geotiff
|
||||
extractedDateStr = filename.split("_")[0];
|
||||
} else if (source === "s3") {
|
||||
extractedDateStr = filename.split("_")[1].split(".")[0];
|
||||
}
|
||||
const date = `${extractedDateStr.slice(0,4)}-${extractedDateStr.slice(4,6)}-${extractedDateStr.slice(6,8)}`;
|
||||
document.getElementById(`${source}ndvidate`).textContent = date;
|
||||
}
|
||||
|
||||
|
|
@ -321,21 +344,25 @@
|
|||
const d = new Date(target);
|
||||
d.setDate(d.getDate() + offset * dir);
|
||||
const date = d.toISOString().split("T")[0].replace(/-/g, "");
|
||||
if (source === "fusion") {
|
||||
if (source === "s2") {
|
||||
// S2 NDVI files are now named YYYYMMDD_ndvi.geotiff
|
||||
const filename = `${date}_ndvi.geotiff`;
|
||||
try {
|
||||
const res = await fetch(`../data/innsbruck/2024/ndvi/fusion/${filename}`);
|
||||
const res = await fetch(`../data/innsbruck/2024/prepared/ndvi/s2/${filename}`);
|
||||
if (res.ok) return filename;
|
||||
} catch {}
|
||||
} else if (source === "fusion") {
|
||||
const filename = `${date}_ndvi.geotiff`;
|
||||
try {
|
||||
const res = await fetch(`../data/innsbruck/2024/prepared/ndvi/fusion/${filename}`);
|
||||
if (res.ok) return filename;
|
||||
} catch {}
|
||||
} else if (source === "s3") {
|
||||
const filename = `composite_${date}.geotiff`;
|
||||
try {
|
||||
const res = await fetch(`../data/innsbruck/2024/prepared/ndvi/s3/${filename}`);
|
||||
if (res.ok) return filename;
|
||||
} catch {}
|
||||
} else {
|
||||
for (let i = 0; i < 3; i++) {
|
||||
const filename = `${date}_${i}.geotiff`;
|
||||
if (!showCloudsCheckbox.checked && clouds[source] && clouds[source].has(filename)) continue;
|
||||
try {
|
||||
const res = await fetch(`../data/innsbruck/2024/ndvi/${source}/${filename}`);
|
||||
if (res.ok) return filename;
|
||||
} catch {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -347,7 +374,6 @@
|
|||
dateDisplay.textContent = date;
|
||||
const params = new URLSearchParams();
|
||||
params.set("date", date);
|
||||
if (!showCloudsCheckbox.checked) params.set("hideClouds", "1");
|
||||
history.replaceState({}, "", `?${params}`);
|
||||
drawTimeseries();
|
||||
for (const source of ["s2", "fusion", "s3"]) {
|
||||
|
|
@ -362,7 +388,7 @@
|
|||
const ndviFilename = await findNDVIFile(date, source);
|
||||
if (ndviFilename) {
|
||||
try {
|
||||
await loadNDVI(source, ndviFilename);
|
||||
await loadNDVI(source, ndviFilename, date);
|
||||
} catch (e) {
|
||||
console.error(`Error loading NDVI ${source}:`, e);
|
||||
}
|
||||
|
|
@ -373,9 +399,7 @@
|
|||
const urlParams = new URLSearchParams(location.search);
|
||||
const urlDate = urlParams.get("date");
|
||||
if (urlDate) slider.value = daysFromDate(urlDate);
|
||||
if (urlParams.get("hideClouds") === "1") showCloudsCheckbox.checked = false;
|
||||
slider.addEventListener("input", updateImages);
|
||||
showCloudsCheckbox.addEventListener("change", updateImages);
|
||||
loadTimeseries().then(updateImages);
|
||||
</script>
|
||||
</body>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue