terraproof 0.0.0-stage → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +356 -2
- package/bin/terraproof.js +4 -0
- package/dist/router/apply.js +147 -0
- package/dist/router/classify.js +111 -0
- package/dist/router/explain.js +9 -0
- package/dist/router/index.js +272 -0
- package/dist/router/resolve.js +239 -0
- package/dist/router/score.js +60 -0
- package/dist/router/types.js +1 -0
- package/dist/router/validate.js +81 -0
- package/instructions/engineering.md +14 -0
- package/instructions/security.md +14 -0
- package/instructions/simplicity.md +13 -0
- package/package.json +65 -4
- package/routing/DOMAIN_ROUTING.md +161 -0
- package/routing/ROUTER_DESIGN.md +173 -0
- package/routing/SKILL_MANIFEST.md +86 -0
- package/routing/checks.json +121 -0
- package/routing/collections.json +47 -0
- package/routing/registry.json +2211 -0
- package/routing/rules.json +64 -0
- package/skills/accuracy-assessment/SKILL.md +213 -0
- package/skills/accuracy-assessment/UPSTREAM.md +7 -0
- package/skills/accuracy-assessment/references/stratified-estimator.md +127 -0
- package/skills/accuracy-assessment/skill.json +103 -0
- package/skills/brainstorming/SKILL.md +250 -0
- package/skills/brainstorming/UPSTREAM.md +29 -0
- package/skills/brainstorming/scripts/frame-template.html +213 -0
- package/skills/brainstorming/scripts/helper.js +167 -0
- package/skills/brainstorming/scripts/server.cjs +723 -0
- package/skills/brainstorming/scripts/start-server.sh +209 -0
- package/skills/brainstorming/scripts/stop-server.sh +120 -0
- package/skills/brainstorming/skill.json +73 -0
- package/skills/brainstorming/spec-document-reviewer-prompt.md +49 -0
- package/skills/brainstorming/visual-companion.md +299 -0
- package/skills/crs-and-units/SKILL.md +200 -0
- package/skills/crs-and-units/UPSTREAM.md +7 -0
- package/skills/crs-and-units/skill.json +74 -0
- package/skills/design-postgis-tables/SKILL.md +494 -0
- package/skills/design-postgis-tables/UPSTREAM.md +21 -0
- package/skills/design-postgis-tables/skill.json +90 -0
- package/skills/gdal/SKILL.md +125 -0
- package/skills/gdal/UPSTREAM.md +22 -0
- package/skills/gdal/references/gdal-recipes.md +103 -0
- package/skills/gdal/skill.json +66 -0
- package/skills/geomaster/README.md +105 -0
- package/skills/geomaster/SKILL.md +366 -0
- package/skills/geomaster/UPSTREAM.md +21 -0
- package/skills/geomaster/references/advanced-gis.md +376 -0
- package/skills/geomaster/references/big-data.md +363 -0
- package/skills/geomaster/references/code-examples.md +531 -0
- package/skills/geomaster/references/coordinate-systems.md +364 -0
- package/skills/geomaster/references/core-libraries.md +273 -0
- package/skills/geomaster/references/data-sources.md +330 -0
- package/skills/geomaster/references/gis-software.md +369 -0
- package/skills/geomaster/references/industry-applications.md +420 -0
- package/skills/geomaster/references/machine-learning.md +462 -0
- package/skills/geomaster/references/programming-languages.md +456 -0
- package/skills/geomaster/references/remote-sensing.md +370 -0
- package/skills/geomaster/references/scientific-domains.md +416 -0
- package/skills/geomaster/references/specialized-topics.md +428 -0
- package/skills/geomaster/references/troubleshooting.md +439 -0
- package/skills/geomaster/skill.json +157 -0
- package/skills/geopandas/SKILL.md +250 -0
- package/skills/geopandas/UPSTREAM.md +22 -0
- package/skills/geopandas/references/crs-management.md +231 -0
- package/skills/geopandas/references/data-io.md +323 -0
- package/skills/geopandas/references/data-structures.md +207 -0
- package/skills/geopandas/references/geometric-operations.md +262 -0
- package/skills/geopandas/references/spatial-analysis.md +294 -0
- package/skills/geopandas/references/visualization.md +230 -0
- package/skills/geopandas/scripts/_common.py +605 -0
- package/skills/geopandas/scripts/crs_reprojection_plan.py +210 -0
- package/skills/geopandas/scripts/export_plan.py +305 -0
- package/skills/geopandas/scripts/geometry_validity_report.py +227 -0
- package/skills/geopandas/scripts/sensitive_coordinates_checklist.py +230 -0
- package/skills/geopandas/scripts/spatial_join_audit.py +368 -0
- package/skills/geopandas/scripts/vector_inventory.py +140 -0
- package/skills/geopandas/skill.json +116 -0
- package/skills/geoparquet-validation/SKILL.md +125 -0
- package/skills/geoparquet-validation/UPSTREAM.md +24 -0
- package/skills/geoparquet-validation/references/distribution-best-practices.md +118 -0
- package/skills/geoparquet-validation/references/gpio-commands.md +264 -0
- package/skills/geoparquet-validation/references/tool-comparison.md +155 -0
- package/skills/geoparquet-validation/skill.json +71 -0
- package/skills/geospatial-data-discovery/SKILL.md +49 -0
- package/skills/geospatial-data-discovery/UPSTREAM.md +15 -0
- package/skills/geospatial-data-discovery/skill.json +51 -0
- package/skills/geospatial-frontend/SKILL.md +86 -0
- package/skills/geospatial-frontend/UPSTREAM.md +23 -0
- package/skills/geospatial-frontend/references/design-system.md +283 -0
- package/skills/geospatial-frontend/references/user-preferences.md +96 -0
- package/skills/geospatial-frontend/skill.json +87 -0
- package/skills/geozarr/SKILL.md +244 -0
- package/skills/geozarr/UPSTREAM.md +21 -0
- package/skills/geozarr/skill.json +97 -0
- package/skills/networkx/SKILL.md +440 -0
- package/skills/networkx/UPSTREAM.md +22 -0
- package/skills/networkx/references/algorithms.md +384 -0
- package/skills/networkx/references/generators.md +385 -0
- package/skills/networkx/references/graph-basics.md +284 -0
- package/skills/networkx/references/io.md +457 -0
- package/skills/networkx/references/visualization.md +531 -0
- package/skills/networkx/skill.json +93 -0
- package/skills/raster-processing/SKILL.md +222 -0
- package/skills/raster-processing/UPSTREAM.md +7 -0
- package/skills/raster-processing/skill.json +81 -0
- package/skills/sar-basics/SKILL.md +202 -0
- package/skills/sar-basics/UPSTREAM.md +7 -0
- package/skills/sar-basics/references/calibration-lut.md +98 -0
- package/skills/sar-basics/skill.json +97 -0
- package/skills/satellite-preprocessing/SKILL.md +241 -0
- package/skills/satellite-preprocessing/UPSTREAM.md +7 -0
- package/skills/satellite-preprocessing/skill.json +84 -0
- package/skills/scientific-visualization/SKILL.md +285 -0
- package/skills/scientific-visualization/UPSTREAM.md +25 -0
- package/skills/scientific-visualization/assets/color_palettes.py +263 -0
- package/skills/scientific-visualization/assets/nature.mplstyle +68 -0
- package/skills/scientific-visualization/assets/presentation.mplstyle +68 -0
- package/skills/scientific-visualization/assets/publication.mplstyle +77 -0
- package/skills/scientific-visualization/assets/publisher_profiles.json +269 -0
- package/skills/scientific-visualization/references/color_palettes.md +227 -0
- package/skills/scientific-visualization/references/journal_requirements.md +169 -0
- package/skills/scientific-visualization/references/matplotlib_examples.md +336 -0
- package/skills/scientific-visualization/references/publication_guidelines.md +196 -0
- package/skills/scientific-visualization/references/sources.md +76 -0
- package/skills/scientific-visualization/scripts/_common.py +136 -0
- package/skills/scientific-visualization/scripts/export_plan.py +493 -0
- package/skills/scientific-visualization/scripts/figure_export.py +642 -0
- package/skills/scientific-visualization/scripts/image_metadata.py +731 -0
- package/skills/scientific-visualization/scripts/palette_audit.py +327 -0
- package/skills/scientific-visualization/scripts/style_presets.py +501 -0
- package/skills/scientific-visualization/scripts/style_preview.py +232 -0
- package/skills/scientific-visualization/skill.json +95 -0
- package/skills/spectral-indices/SKILL.md +235 -0
- package/skills/spectral-indices/UPSTREAM.md +7 -0
- package/skills/spectral-indices/skill.json +88 -0
- package/skills/stac-and-cloud-access/SKILL.md +209 -0
- package/skills/stac-and-cloud-access/UPSTREAM.md +7 -0
- package/skills/stac-and-cloud-access/references/run-manifest.md +94 -0
- package/skills/stac-and-cloud-access/skill.json +98 -0
- package/skills/statistical-analysis/SKILL.md +446 -0
- package/skills/statistical-analysis/UPSTREAM.md +24 -0
- package/skills/statistical-analysis/references/assumptions_and_diagnostics.md +379 -0
- package/skills/statistical-analysis/references/bayesian_statistics.md +686 -0
- package/skills/statistical-analysis/references/effect_sizes_and_power.md +649 -0
- package/skills/statistical-analysis/references/reporting_standards.md +482 -0
- package/skills/statistical-analysis/references/test_selection_guide.md +129 -0
- package/skills/statistical-analysis/scripts/assumption_checks.py +652 -0
- package/skills/statistical-analysis/skill.json +127 -0
- package/skills/systematic-debugging/SKILL.md +283 -0
- package/skills/systematic-debugging/UPSTREAM.md +28 -0
- package/skills/systematic-debugging/condition-based-waiting-example.ts +158 -0
- package/skills/systematic-debugging/condition-based-waiting.md +115 -0
- package/skills/systematic-debugging/defense-in-depth.md +122 -0
- package/skills/systematic-debugging/find-polluter.sh +72 -0
- package/skills/systematic-debugging/root-cause-tracing.md +169 -0
- package/skills/systematic-debugging/skill.json +88 -0
- package/skills/time-series-and-change/SKILL.md +217 -0
- package/skills/time-series-and-change/UPSTREAM.md +7 -0
- package/skills/time-series-and-change/references/change-methods.md +42 -0
- package/skills/time-series-and-change/skill.json +116 -0
- package/skills/vector-quality/SKILL.md +204 -0
- package/skills/vector-quality/UPSTREAM.md +7 -0
- package/skills/vector-quality/skill.json +82 -0
- package/skills/verification/SKILL.md +120 -0
- package/skills/verification/UPSTREAM.md +31 -0
- package/skills/verification/skill.json +67 -0
- package/skills/writing-plans/SKILL.md +171 -0
- package/skills/writing-plans/UPSTREAM.md +23 -0
- package/skills/writing-plans/plan-document-reviewer-prompt.md +49 -0
- package/skills/writing-plans/skill.json +80 -0
- package/sources/SOURCES.md +374 -0
- package/third_party/isaaccorley-geospatial-skills/LICENSE +176 -0
- package/third_party/isaaccorley-geospatial-skills/NOTICE.md +17 -0
- package/third_party/kdense-scientific-agent-skills/LICENSE +21 -0
- package/third_party/kdense-scientific-agent-skills/NOTICE.md +19 -0
- package/third_party/superpowers/LICENSE +21 -0
- package/third_party/superpowers/NOTICE.md +17 -0
- package/third_party/timescale-pg-aiguide/LICENSE +176 -0
- package/third_party/timescale-pg-aiguide/NOTICE.md +14 -0
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
# Remote Sensing Reference
|
|
2
|
+
|
|
3
|
+
Comprehensive guide to satellite data acquisition, processing, and analysis.
|
|
4
|
+
|
|
5
|
+
## Satellite Missions Overview
|
|
6
|
+
|
|
7
|
+
| Satellite | Operator | Resolution | Revisit | Key Features |
|
|
8
|
+
|-----------|----------|------------|---------|--------------|
|
|
9
|
+
| **Sentinel-2** | ESA | 10-60m | 5 days | 13 bands, free access |
|
|
10
|
+
| **Landsat 8/9** | USGS | 30m | 16 days | Historical archive (1972+) |
|
|
11
|
+
| **MODIS** | NASA | 250-1000m | Daily | Vegetation indices |
|
|
12
|
+
| **PlanetScope** | Planet | 3m | Daily | Commercial, high-res |
|
|
13
|
+
| **WorldView** | Maxar | 0.3m | Variable | Very high resolution |
|
|
14
|
+
| **Sentinel-1** | ESA | 5-40m | 6-12 days | SAR, all-weather |
|
|
15
|
+
| **Envisat** | ESA | 30m | 35 days | SAR (archival) |
|
|
16
|
+
|
|
17
|
+
## Sentinel-2 Processing
|
|
18
|
+
|
|
19
|
+
### Accessing Sentinel-2 Data
|
|
20
|
+
|
|
21
|
+
```python
|
|
22
|
+
import pystac_client
|
|
23
|
+
import planetary_computer
|
|
24
|
+
import odc.stac
|
|
25
|
+
import xarray as xr
|
|
26
|
+
|
|
27
|
+
# Search Sentinel-2 collection
|
|
28
|
+
catalog = pystac_client.Client.open(
|
|
29
|
+
"https://planetarycomputer.microsoft.com/api/stac/v1",
|
|
30
|
+
modifier=planetary_computer.sign_inplace,
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
# Define AOI and time range
|
|
34
|
+
bbox = [-122.5, 37.7, -122.3, 37.9]
|
|
35
|
+
search = catalog.search(
|
|
36
|
+
collections=["sentinel-2-l2a"],
|
|
37
|
+
bbox=bbox,
|
|
38
|
+
datetime="2023-01-01/2023-12-31",
|
|
39
|
+
query={"eo:cloud_cover": {"lt": 20}},
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
items = list(search.get_items())
|
|
43
|
+
print(f"Found {len(items)} items")
|
|
44
|
+
|
|
45
|
+
# Load as xarray dataset
|
|
46
|
+
data = odc.stac.load(
|
|
47
|
+
[items[0]],
|
|
48
|
+
bands=["B02", "B03", "B04", "B08", "B11"],
|
|
49
|
+
crs="EPSG:32610",
|
|
50
|
+
resolution=10,
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
print(data)
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### Calculating Spectral Indices
|
|
57
|
+
|
|
58
|
+
```python
|
|
59
|
+
import numpy as np
|
|
60
|
+
import rasterio
|
|
61
|
+
|
|
62
|
+
def ndvi(nir, red):
|
|
63
|
+
"""Normalized Difference Vegetation Index"""
|
|
64
|
+
return (nir - red) / (nir + red + 1e-8)
|
|
65
|
+
|
|
66
|
+
def evi(nir, red, blue):
|
|
67
|
+
"""Enhanced Vegetation Index"""
|
|
68
|
+
return 2.5 * (nir - red) / (nir + 6*red - 7.5*blue + 1)
|
|
69
|
+
|
|
70
|
+
def savi(nir, red, L=0.5):
|
|
71
|
+
"""Soil Adjusted Vegetation Index"""
|
|
72
|
+
return ((nir - red) / (nir + red + L)) * (1 + L)
|
|
73
|
+
|
|
74
|
+
def ndwi(green, nir):
|
|
75
|
+
"""Normalized Difference Water Index"""
|
|
76
|
+
return (green - nir) / (green + nir + 1e-8)
|
|
77
|
+
|
|
78
|
+
def mndwi(green, swir):
|
|
79
|
+
"""Modified NDWI for open water"""
|
|
80
|
+
return (green - swir) / (green + swir + 1e-8)
|
|
81
|
+
|
|
82
|
+
def nbr(nir, swir):
|
|
83
|
+
"""Normalized Burn Ratio"""
|
|
84
|
+
return (nir - swir) / (nir + swir + 1e-8)
|
|
85
|
+
|
|
86
|
+
def ndbi(swir, nir):
|
|
87
|
+
"""Normalized Difference Built-up Index"""
|
|
88
|
+
return (swir - nir) / (swir + nir + 1e-8)
|
|
89
|
+
|
|
90
|
+
# Batch processing
|
|
91
|
+
with rasterio.open('sentinel2.tif') as src:
|
|
92
|
+
# Sentinel-2 band mapping
|
|
93
|
+
B02 = src.read(1).astype(float) # Blue (10m)
|
|
94
|
+
B03 = src.read(2).astype(float) # Green (10m)
|
|
95
|
+
B04 = src.read(3).astype(float) # Red (10m)
|
|
96
|
+
B08 = src.read(4).astype(float) # NIR (10m)
|
|
97
|
+
B11 = src.read(5).astype(float) # SWIR1 (20m, resampled)
|
|
98
|
+
|
|
99
|
+
# Calculate indices
|
|
100
|
+
NDVI = ndvi(B08, B04)
|
|
101
|
+
EVI = evi(B08, B04, B02)
|
|
102
|
+
SAVI = savi(B08, B04, L=0.5)
|
|
103
|
+
NDWI = ndwi(B03, B08)
|
|
104
|
+
NBR = nbr(B08, B11)
|
|
105
|
+
NDBI = ndbi(B11, B08)
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
## Landsat Processing
|
|
109
|
+
|
|
110
|
+
### Landsat Collection 2
|
|
111
|
+
|
|
112
|
+
```python
|
|
113
|
+
import ee
|
|
114
|
+
|
|
115
|
+
# Initialize Earth Engine
|
|
116
|
+
ee.Initialize(project='your-project')
|
|
117
|
+
|
|
118
|
+
# Landsat 8 Collection 2 Level 2
|
|
119
|
+
landsat = ee.ImageCollection('LANDSAT/LC08/C02/T1_L2') \
|
|
120
|
+
.filterBounds(ee.Geometry.Point([-122.4, 37.7])) \
|
|
121
|
+
.filterDate('2020-01-01', '2023-12-31') \
|
|
122
|
+
.filter(ee.Filter.lt('CLOUD_COVER', 20))
|
|
123
|
+
|
|
124
|
+
# Apply scaling factors (Collection 2)
|
|
125
|
+
def apply_scale_factors(image):
|
|
126
|
+
optical = image.select('SR_B.').multiply(0.0000275).add(-0.2)
|
|
127
|
+
thermal = image.select('ST_B10').multiply(0.00341802).add(149.0)
|
|
128
|
+
return image.addBands(optical, None, True).addBands(thermal, None, True)
|
|
129
|
+
|
|
130
|
+
landsat_scaled = landsat.map(apply_scale_factors)
|
|
131
|
+
|
|
132
|
+
# Calculate NDVI
|
|
133
|
+
def add_ndvi(image):
|
|
134
|
+
ndvi = image.normalizedDifference(['SR_B5', 'SR_B4']).rename('NDVI')
|
|
135
|
+
return image.addBands(ndvi)
|
|
136
|
+
|
|
137
|
+
landsat_ndvi = landsat_scaled.map(add_ndvi)
|
|
138
|
+
|
|
139
|
+
# Get composite
|
|
140
|
+
composite = landsat_ndvi.median()
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
### Landsat Surface Temperature
|
|
144
|
+
|
|
145
|
+
```python
|
|
146
|
+
def land_surface_temperature(image):
|
|
147
|
+
"""Calculate land surface temperature from Landsat 8."""
|
|
148
|
+
# Brightness temperature
|
|
149
|
+
Tb = image.select('ST_B10')
|
|
150
|
+
|
|
151
|
+
# NDVI for emissivity
|
|
152
|
+
ndvi = image.normalizedDifference(['SR_B5', 'SR_B4'])
|
|
153
|
+
pv = ((ndvi - 0.2) / (0.5 - 0.2)) ** 2 # Proportion of vegetation
|
|
154
|
+
|
|
155
|
+
# Emissivity
|
|
156
|
+
em = 0.004 * pv + 0.986
|
|
157
|
+
|
|
158
|
+
# LST in Kelvin
|
|
159
|
+
lst = Tb.divide(1 + (0.00115 * Tb / 1.4388) * np.log(em))
|
|
160
|
+
|
|
161
|
+
# Convert to Celsius
|
|
162
|
+
lst_c = lst.subtract(273.15).rename('LST')
|
|
163
|
+
|
|
164
|
+
return image.addBands(lst_c)
|
|
165
|
+
|
|
166
|
+
landsat_lst = landsat_scaled.map(land_surface_temperature)
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
## SAR Processing (Synthetic Aperture Radar)
|
|
170
|
+
|
|
171
|
+
### Sentinel-1 GRD Processing
|
|
172
|
+
|
|
173
|
+
```python
|
|
174
|
+
import rasterio
|
|
175
|
+
from scipy.ndimage import gaussian_filter
|
|
176
|
+
import numpy as np
|
|
177
|
+
|
|
178
|
+
def process_sentinel1_grd(input_path, output_path):
|
|
179
|
+
"""Process Sentinel-1 GRD data."""
|
|
180
|
+
with rasterio.open(input_path) as src:
|
|
181
|
+
# Read VV and VH bands
|
|
182
|
+
vv = src.read(1).astype(float)
|
|
183
|
+
vh = src.read(2).astype(float)
|
|
184
|
+
|
|
185
|
+
# Convert to decibels
|
|
186
|
+
vv_db = 10 * np.log10(vv + 1e-8)
|
|
187
|
+
vh_db = 10 * np.log10(vh + 1e-8)
|
|
188
|
+
|
|
189
|
+
# Speckle filtering (Lee filter approximation)
|
|
190
|
+
def lee_filter(img, size=3):
|
|
191
|
+
"""Simple Lee filter for speckle reduction."""
|
|
192
|
+
# Local mean
|
|
193
|
+
mean = gaussian_filter(img, size)
|
|
194
|
+
# Local variance
|
|
195
|
+
sq_mean = gaussian_filter(img**2, size)
|
|
196
|
+
variance = sq_mean - mean**2
|
|
197
|
+
# Noise variance
|
|
198
|
+
noise_var = np.var(img) * 0.5
|
|
199
|
+
# Lee filter formula
|
|
200
|
+
return mean + (variance - noise_var) / (variance) * (img - mean)
|
|
201
|
+
|
|
202
|
+
vv_filtered = lee_filter(vv_db)
|
|
203
|
+
vh_filtered = lee_filter(vh_db)
|
|
204
|
+
|
|
205
|
+
# Calculate ratio
|
|
206
|
+
ratio = vv_db - vh_db # In dB: difference = ratio
|
|
207
|
+
|
|
208
|
+
# Save
|
|
209
|
+
profile = src.profile
|
|
210
|
+
profile.update(dtype=rasterio.float32, count=3)
|
|
211
|
+
|
|
212
|
+
with rasterio.open(output_path, 'w', **profile) as dst:
|
|
213
|
+
dst.write(vv_filtered.astype(np.float32), 1)
|
|
214
|
+
dst.write(vh_filtered.astype(np.float32), 2)
|
|
215
|
+
dst.write(ratio.astype(np.float32), 3)
|
|
216
|
+
|
|
217
|
+
# Usage
|
|
218
|
+
process_sentinel1_grd('S1A_IW_GRDH.tif', 'S1A_processed.tif')
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
### SAR Polarimetric Indices
|
|
222
|
+
|
|
223
|
+
```python
|
|
224
|
+
def calculate_sar_indices(vv, vh):
|
|
225
|
+
"""Calculate SAR-derived indices."""
|
|
226
|
+
# Backscatter ratio in dB
|
|
227
|
+
ratio_db = 10 * np.log10(vv / (vh + 1e-8) + 1e-8)
|
|
228
|
+
|
|
229
|
+
# Radar Vegetation Index
|
|
230
|
+
rvi = (4 * vh) / (vv + vh + 1e-8)
|
|
231
|
+
|
|
232
|
+
# Soil Moisture Index (approximation)
|
|
233
|
+
smi = vv / (vv + vh + 1e-8)
|
|
234
|
+
|
|
235
|
+
return ratio_db, rvi, smi
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
## Hyperspectral Imaging
|
|
239
|
+
|
|
240
|
+
### Hyperspectral Data Processing
|
|
241
|
+
|
|
242
|
+
```python
|
|
243
|
+
import spectral.io.envi as envi
|
|
244
|
+
import numpy as np
|
|
245
|
+
import matplotlib.pyplot as plt
|
|
246
|
+
|
|
247
|
+
# Load hyperspectral cube
|
|
248
|
+
hdr_path = 'hyperspectral.hdr'
|
|
249
|
+
img = envi.open(hdr_path)
|
|
250
|
+
data = img.load()
|
|
251
|
+
|
|
252
|
+
print(f"Data shape: {data.shape}") # (rows, cols, bands)
|
|
253
|
+
|
|
254
|
+
# Extract spectral signature at a pixel
|
|
255
|
+
pixel_signature = data[100, 100, :]
|
|
256
|
+
plt.plot(img.bands.centers, pixel_signature)
|
|
257
|
+
plt.xlabel('Wavelength (nm)')
|
|
258
|
+
plt.ylabel('Reflectance')
|
|
259
|
+
plt.show()
|
|
260
|
+
|
|
261
|
+
# Spectral indices for hyperspectral
|
|
262
|
+
def calculate_ndi(hyper_data, band1_idx, band2_idx):
|
|
263
|
+
"""Normalized Difference Index for any two bands."""
|
|
264
|
+
band1 = hyper_data[:, :, band1_idx]
|
|
265
|
+
band2 = hyper_data[:, :, band2_idx]
|
|
266
|
+
return (band1 - band2) / (band1 + band2 + 1e-8)
|
|
267
|
+
|
|
268
|
+
# Red Edge Position (REP)
|
|
269
|
+
def red_edge_position(hyper_data, wavelengths):
|
|
270
|
+
"""Calculate red edge position."""
|
|
271
|
+
# Find wavelength of maximum slope in red-edge region (680-750nm)
|
|
272
|
+
red_edge_idx = np.where((wavelengths >= 680) & (wavelengths <= 750))[0]
|
|
273
|
+
|
|
274
|
+
first_derivative = np.diff(hyper_data, axis=2)
|
|
275
|
+
rep_idx = np.argmax(first_derivative[:, :, red_edge_idx], axis=2)
|
|
276
|
+
return wavelengths[red_edge_idx][rep_idx]
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
## Image Preprocessing
|
|
280
|
+
|
|
281
|
+
### Atmospheric Correction
|
|
282
|
+
|
|
283
|
+
```python
|
|
284
|
+
# Using 6S (via Py6S)
|
|
285
|
+
from Py6S import *
|
|
286
|
+
|
|
287
|
+
# Create 6S instance
|
|
288
|
+
s = SixS()
|
|
289
|
+
|
|
290
|
+
# Set atmospheric conditions
|
|
291
|
+
s.atmos_profile = AtmosProfile.PredefinedType(AtmosProfile.MidlatitudeSummer)
|
|
292
|
+
s.aero_profile = AeroProfile.PredefinedType(AeroProfile.Continental)
|
|
293
|
+
|
|
294
|
+
# Set geometry
|
|
295
|
+
s.geometry = Geometry.User()
|
|
296
|
+
s.geometry.month = 6
|
|
297
|
+
s.geometry.day = 15
|
|
298
|
+
s.geometry.solar_z = 30
|
|
299
|
+
s.geometry.solar_a = 180
|
|
300
|
+
|
|
301
|
+
# Run simulation
|
|
302
|
+
s.run()
|
|
303
|
+
|
|
304
|
+
# Get correction coefficients
|
|
305
|
+
xa, xb, xc = s.outputs.coef_xa, s.outputs.coef_xb, s.outputs.coef_xc
|
|
306
|
+
|
|
307
|
+
def atmospheric_correction(dn, xa, xb, xc):
|
|
308
|
+
"""Apply 6S atmospheric correction."""
|
|
309
|
+
y = xa * dn - xb
|
|
310
|
+
y = y / (1 + xc * y)
|
|
311
|
+
return y
|
|
312
|
+
```
|
|
313
|
+
|
|
314
|
+
### Cloud Masking
|
|
315
|
+
|
|
316
|
+
```python
|
|
317
|
+
def sentinel2_cloud_mask(s2_image):
|
|
318
|
+
"""Generate cloud mask for Sentinel-2."""
|
|
319
|
+
# Simple cloud detection using spectral tests
|
|
320
|
+
scl = s2_image.select('SCL') # Scene Classification Layer
|
|
321
|
+
|
|
322
|
+
# Cloud classes: 8=Cloud, 9=Cloud medium, 10=Cloud high
|
|
323
|
+
cloud_mask = scl.gt(7).And(scl.lt(11))
|
|
324
|
+
|
|
325
|
+
# Additional test: Brightness threshold
|
|
326
|
+
brightness = s2_image.select(['B02','B03','B04','B08']).mean()
|
|
327
|
+
|
|
328
|
+
return cloud_mask.Or(brightness.gt(0.4))
|
|
329
|
+
|
|
330
|
+
# Apply mask
|
|
331
|
+
def apply_mask(image):
|
|
332
|
+
mask = sentinel2_cloud_mask(image)
|
|
333
|
+
return image.updateMask(mask.Not())
|
|
334
|
+
```
|
|
335
|
+
|
|
336
|
+
### Pan-Sharpening
|
|
337
|
+
|
|
338
|
+
```python
|
|
339
|
+
import cv2
|
|
340
|
+
import numpy as np
|
|
341
|
+
|
|
342
|
+
def gram_schmidt_pansharpen(ms, pan):
|
|
343
|
+
"""Gram-Schmidt pan-sharpening."""
|
|
344
|
+
# Multispectral: (H, W, bands)
|
|
345
|
+
# Panchromatic: (H, W)
|
|
346
|
+
|
|
347
|
+
# 1. Upsample MS to pan resolution
|
|
348
|
+
ms_up = cv2.resize(ms, (pan.shape[1], pan.shape[0]),
|
|
349
|
+
interpolation=cv2.INTER_CUBIC)
|
|
350
|
+
|
|
351
|
+
# 2. Simulate panchromatic from MS
|
|
352
|
+
weights = np.array([0.25, 0.25, 0.25, 0.25]) # Equal weights
|
|
353
|
+
simulated = np.sum(ms_up * weights.reshape(1, 1, -1), axis=2)
|
|
354
|
+
|
|
355
|
+
# 3. Gram-Schmidt orthogonalization
|
|
356
|
+
# (Simplified version)
|
|
357
|
+
for i in range(ms_up.shape[2]):
|
|
358
|
+
band = ms_up[:, :, i].astype(float)
|
|
359
|
+
mean_sim = np.mean(simulated)
|
|
360
|
+
mean_band = np.mean(band)
|
|
361
|
+
diff = band - mean_band
|
|
362
|
+
sim_diff = simulated - mean_sim
|
|
363
|
+
|
|
364
|
+
# Adjust
|
|
365
|
+
ms_up[:, :, i] = band + diff * (pan - simulated) / (np.std(sim_diff) + 1e-8)
|
|
366
|
+
|
|
367
|
+
return ms_up
|
|
368
|
+
```
|
|
369
|
+
|
|
370
|
+
For more code examples, see [code-examples.md](code-examples.md).
|