# Standard library imports
import logging
import time
import os
from datetime import datetime, timedelta
from pathlib import Path
# Set up logger
= logging.getLogger(__name__)
logger "β
Standard library imports complete") logger.info(
Overview
This guide shows you how to use the pre-built helper functions from Week 1 without needing to copy-paste all the code from Section 2. All the functions are already available in the geogfm.c01
module!
The Week 1 session defines 13+ helper functions in Section 2. Instead of re-running all those cells every time, you can simply import them from geogfm.c01
and start analyzing data immediately!
For Git and SSH key setup on the UCSB AI Sandbox, see the Git & SSH Setup on HPC cheatsheet.
Installation
Before you can import from geogfm.c01
, you need to install the geogfm
package in editable mode. This lets Python find the module files.
Option 1: Install from Project Root (Recommended)
If youβre working in a notebook in the book/
or nbs/
directory:
# Install the geogfm package in editable mode
!pip install -e ..
Option 2: Install with Absolute Path
If youβre working from anywhere else, use the absolute path to the project root:
# Replace with your actual path to the geoAI directory
!pip install -e /Users/your-username/dev/geoAI
Option 3: Install from Terminal
You can also install from the terminal before opening your notebook:
cd /path/to/geoAI
pip install -e .
Verify Installation
After installation, verify that the package is available:
# Check if geogfm can be imported
import geogfm
import logging
= logging.getLogger(__name__)
logger f"β
geogfm installed at: {geogfm.__file__}")
logger.info(
# Check if c01 module exists
from geogfm import c01
f"β
c01 module available with {len(dir(c01))} attributes") logger.info(
- The
-e
flag installs in βeditableβ mode, meaning changes to the source code are immediately available without reinstalling - You only need to install once per environment (unless you switch conda/virtual environments)
- If you get import errors, make sure youβve run the Week 1 notebook to generate the
geogfm/c01.py
file via tangling
Complete Setup (Sections 3-10)
To run the entire workflow (Sections 3-10), use this comprehensive setup:
Step 1: Standard Library Imports
Step 2: Core Data Science Libraries
# Core data science libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
"β
Data science libraries loaded") logger.info(
Step 3: Geospatial Libraries
# Geospatial libraries
import rasterio
import xarray as xr
import rioxarray
from pystac_client import Client
import planetary_computer as pc
from shapely.geometry import box
"β
Geospatial libraries loaded") logger.info(
Step 4: Interactive Mapping
# Interactive mapping
import folium
from folium.plugins import MeasureControl, Fullscreen
"β
Mapping libraries loaded") logger.info(
Step 5: Import Week 1 Helper Functions
This is where the magic happens - all 13+ helper functions in one import!
# Import ALL helper functions from tangled Week 1 module
from geogfm.c01 import (
# Environment and authentication (Section 2.1-2.4.1)
verify_environment,
setup_planetary_computer_auth,
# Data discovery (Section 2.4.2)
search_sentinel2_scenes,
search_STAC_scenes,
# Data loading (Section 2.4.3)
load_sentinel2_bands,
# Spatial processing (Section 2.4.4)
get_subset_from_scene,
get_scene_info,
create_scene_tiles,
test_subset_functionality,
# Data processing (Section 2.4.5)
normalize_band,
create_rgb_composite,
calculate_ndvi,
calculate_band_statistics,
# Visualization (Section 2.4.6)
plot_band_comparison,
# Export/Import (Section 2.4.7)
save_geotiff,
export_analysis_results,
load_week1_data
)
"β
Week 1 helper functions imported") logger.info(
2025-10-09 16:22:05,847 - INFO - Analysis results exported to: /Users/kellycaylor/dev/geoAI/book/extras/cheatsheets/week1_output
2025-10-09 16:22:05,847 - INFO - Data exported - use load_week1_data() to reload
2025-10-09 16:22:05,848 - INFO - β
Week 1 helper functions imported
Step 6: Configure Matplotlib
# Configure matplotlib for publication-quality plots
plt.rcParams.update({'figure.figsize': (10, 6),
'figure.dpi': 100,
'font.size': 10,
'axes.titlesize': 12,
'axes.labelsize': 10,
'xtick.labelsize': 9,
'ytick.labelsize': 9,
'legend.fontsize': 9
})
"β
Matplotlib configured") logger.info(
2025-10-09 16:22:05,852 - INFO - β
Matplotlib configured
Step 7: Setup Logging and Authentication
# Configure logging
logging.basicConfig(=logging.INFO,
levelformat='%(asctime)s - %(levelname)s - %(message)s'
)= logging.getLogger(__name__)
logger
# Authenticate to Planetary Computer
= setup_planetary_computer_auth()
auth_status f"Planetary Computer authentication status: {'Authenticated' if auth_status else 'Anonymous'}")
logger.info(
"\n" + "="*60)
logger.info("β
ALL IMPORTS COMPLETE - READY TO RUN SECTIONS 3-10!")
logger.info("="*60) logger.info(
2025-10-09 16:22:05,857 - INFO - Using anonymous access (basic rate limits)
2025-10-09 16:22:05,857 - INFO - Planetary Computer authentication status: Anonymous
2025-10-09 16:22:05,858 - INFO -
============================================================
2025-10-09 16:22:05,858 - INFO - β
ALL IMPORTS COMPLETE - READY TO RUN SECTIONS 3-10!
2025-10-09 16:22:05,858 - INFO - ============================================================
Quick Reference: Available Functions
Hereβs what you have available after importing from geogfm.c01
:
Authentication & Environment
verify_environment()
- Check that required packages are installedsetup_planetary_computer_auth()
- Authenticate to Planetary Computer
Data Discovery
search_sentinel2_scenes()
- Search for Sentinel-2 scenessearch_STAC_scenes()
- General-purpose STAC search
Data Loading
load_sentinel2_bands()
- Load bands with retry logic and subsetting
Spatial Processing
get_subset_from_scene()
- Extract spatial subsets using percentagesget_scene_info()
- Get scene characteristicscreate_scene_tiles()
- Create systematic tile gridtest_subset_functionality()
- Test data loading pipeline
Data Processing
normalize_band()
- Percentile-based normalizationcreate_rgb_composite()
- Create RGB compositescalculate_ndvi()
- Calculate NDVI from NIR and Red bandscalculate_band_statistics()
- Comprehensive band statistics
Visualization
plot_band_comparison()
- Multi-panel band visualization
Export/Import
save_geotiff()
- Export georeferenced GeoTIFFexport_analysis_results()
- Save complete analysis resultsload_week1_data()
- Reload processed data
Complete Copy-Paste Block
For convenience, hereβs everything in one block you can copy-paste:
# Standard library imports
import logging
import time
import os
from datetime import datetime, timedelta
from pathlib import Path
# Core data science libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# Geospatial libraries
import rasterio
import xarray as xr
import rioxarray
from pystac_client import Client
import planetary_computer as pc
from shapely.geometry import box
# Interactive mapping
import folium
from folium.plugins import MeasureControl, Fullscreen
# Import ALL helper functions from tangled Week 1 module
from geogfm.c01 import (
verify_environment, setup_planetary_computer_auth,
search_sentinel2_scenes, search_STAC_scenes,
load_sentinel2_bands,
get_subset_from_scene, get_scene_info, create_scene_tiles, test_subset_functionality,
normalize_band, create_rgb_composite, calculate_ndvi, calculate_band_statistics,
plot_band_comparison,
save_geotiff, export_analysis_results, load_week1_data
)
# Configure matplotlib
plt.rcParams.update({'figure.figsize': (10, 6),
'figure.dpi': 100,
'font.size': 10,
'axes.titlesize': 12,
'axes.labelsize': 10,
'xtick.labelsize': 9,
'ytick.labelsize': 9,
'legend.fontsize': 9
})
# Configure logging
=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logging.basicConfig(level= logging.getLogger(__name__)
logger
# Authenticate to Planetary Computer
= setup_planetary_computer_auth()
auth_status f"Authentication: {'β
Authenticated' if auth_status else 'β οΈ Anonymous'}")
logger.info(
"\n" + "="*60)
logger.info("β
ALL IMPORTS COMPLETE - READY TO RUN SECTIONS 3-10!")
logger.info("="*60) logger.info(
2025-10-09 16:22:05,864 - INFO - Using anonymous access (basic rate limits)
2025-10-09 16:22:05,864 - INFO - Authentication: β οΈ Anonymous
2025-10-09 16:22:05,864 - INFO -
============================================================
2025-10-09 16:22:05,864 - INFO - β
ALL IMPORTS COMPLETE - READY TO RUN SECTIONS 3-10!
2025-10-09 16:22:05,865 - INFO - ============================================================
Example Usage
Once imported, you can immediately start working with satellite data:
# Define your area of interest
= [-120.5, 34.3, -119.5, 34.7]
santa_barbara_bbox
# Search for scenes
= search_sentinel2_scenes(
scenes =santa_barbara_bbox,
bbox="2024-06-01/2024-09-30",
date_range=20,
cloud_cover_max=10
limit
)
f"Found {len(scenes)} scenes!")
logger.info(
# Load the best scene
= scenes[0]
best_scene = get_subset_from_scene(best_scene, x_range=(30, 70), y_range=(30, 70))
subset_bbox
= load_sentinel2_bands(
band_data
best_scene,=['B04', 'B03', 'B02', 'B08'],
bands=subset_bbox
subset_bbox
)
# Calculate NDVI
= calculate_ndvi(band_data['B08'], band_data['B04'])
ndvi
# Create RGB composite
= create_rgb_composite(band_data['B04'], band_data['B03'], band_data['B02'])
rgb
"β
Analysis complete!") logger.info(
2025-10-09 16:22:08,949 - INFO - Found 40 Sentinel-2 scenes (cloud cover < 20%)
2025-10-09 16:22:08,950 - INFO - Found 40 scenes!
2025-10-09 16:24:52,195 - INFO - Successfully loaded 4 bands: ['B04', 'B03', 'B02', 'B08']
2025-10-09 16:24:52,850 - INFO - β
Analysis complete!
Troubleshooting
Import Errors
If you get ModuleNotFoundError: No module named 'geogfm'
:
Install the package in editable mode:
!pip install -e .. # If in book/ or nbs/ directory # OR !pip install -e /path/to/geoAI # With absolute path
Verify installation:
import sys import logging = logging.getLogger(__name__) logger "Python paths:") logger.info(for p in sys.path: f" {p}") logger.info( # Check if geogfm is installed !pip show geogfm
Check your working directory:
import os import logging = logging.getLogger(__name__) logger f"Current directory: {os.getcwd()}") logger.info(
Missing c01.py File
If you get ImportError: cannot import name 'setup_planetary_computer_auth' from 'geogfm.c01'
:
Verify the c01.py file exists:
import os import logging = logging.getLogger(__name__) logger = "geogfm/c01.py" c01_path if os.path.exists(c01_path): f"β {c01_path} exists") logger.info(else: f"β {c01_path} not found - you need to run the Week 1 notebook to generate it") logger.error(
The file is generated by βtanglingβ - run the Week 1 notebook (
c01-geospatial-data-foundations.qmd
) to generategeogfm/c01.py
automatically.
Function Not Found
If a specific function isnβt available, check that the geogfm/c01.py
file has been generated by running the tangle process on the Week 1 notebook.
# Check what's available in c01
from geogfm import c01
import logging
= logging.getLogger(__name__)
logger "Available functions:")
logger.info(= [name for name in dir(c01) if not name.startswith('_')]
functions for func in sorted(functions):
f" - {func}") logger.info(
Authentication Issues
If Planetary Computer authentication fails, check:
Environment variable or .env file:
import os import logging = logging.getLogger(__name__) logger = os.getenv('PC_SDK_SUBSCRIPTION_KEY') or os.getenv('PLANETARY_COMPUTER_API_KEY') key if key: f"β API key found (length: {len(key)})") logger.info(else: "β No API key found - create a .env file with:") logger.error(" PC_SDK_SUBSCRIPTION_KEY=your_key_here") logger.error(
Check .env file location:
from pathlib import Path import logging = logging.getLogger(__name__) logger = Path('.env') env_file if env_file.exists(): f"β .env file found at: {env_file.absolute()}") logger.info(else: f"β .env file not found in: {Path.cwd()}") logger.warning(
Internet connectivity - Make sure you have an active connection
Service status - Check Planetary Computer status
Dependency Issues
If youβre missing required packages:
# Install all dependencies from the environment file
!pip install rasterio xarray rioxarray folium pystac-client planetary-computer matplotlib numpy pandas geopandas
Next Steps
Once you have everything imported:
- Section 3: Explore STAC catalogs and discover available datasets
- Section 4: Define your area of interest with interactive maps
- Section 5: Search for and select optimal satellite scenes
- Section 6: Load and validate satellite data
- Section 7: Create visualizations and calculate indices
- Section 8: Build interactive maps with your results
- Section 9: Export your analysis for future use
Happy analyzing! π°οΈ