基于Python读取CSV日期调用NASA API获取卫星影像的可行性问询
Hey Bethany, great question—this is totally doable, and there are actually several NASA APIs that are perfect for what you want to build! Let me break this down step by step so you can get started confidently, even as a new programmer.
First off, here are the key NASA APIs you’ll want to explore:
- NASA Earthdata Search API: This is your go-to for accessing popular satellite datasets like Landsat and Sentinel. You’ll need a free Earthdata account to get an API key, but registration takes just a minute. It lets you filter imagery by date, location, and sensor type, then returns direct download links.
- NASA GIBS (Global Imagery Browse Services): If you want pre-rendered image tiles (instead of raw satellite data), GIBS is perfect. It’s great for quickly grabbing daily global or regional imagery without dealing with heavy raw files.
- MODIS API: If your project focuses on MODIS satellite data (which provides daily global coverage), this API is built specifically for that—super straightforward for date-based queries.
Let’s walk through the core parts of your project with simple, beginner-friendly code snippets:
1. Prep Work
- Sign up for a free NASA Earthdata account to get your API key.
- Install the essential Python libraries via pip:
These will handle API calls and reading your CSV file.pip install requests pandas
2. Read Dates from CSV
Using pandas makes reading CSV files a breeze. Assuming your CSV has a column named date with dates in YYYY-MM-DD format:
import pandas as pd # Load your CSV file df = pd.read_csv("your_dates_file.csv") # Extract dates into a list we can loop through target_dates = df["date"].tolist() # If your dates are in a different format, convert them like this: # df["date"] = pd.to_datetime(df["date"]).dt.strftime("%Y-%m-%d")
3. Call the API to Get Image Links
Let’s use the Earthdata Search API as an example. Here’s a basic loop to fetch imagery for each date:
import requests import time api_key = "your_earthdata_api_key_here" base_url = "https://cmr.earthdata.nasa.gov/search/granules.json" for date in target_dates: # Set up parameters (this example uses Landsat 8 data) params = { "short_name": "LANDSAT_8_C1", # Specify the satellite/sensor "temporal": f"{date}T00:00:00Z/{date}T23:59:59Z", # Date range for the day "api_key": api_key } # Send the request to the API response = requests.get(base_url, params=params) response_data = response.json() # Check if we found imagery for this date if response_data["feed"]["entry"]: # Grab the first download link (adjust if you need specific bands/files) download_link = response_data["feed"]["entry"][0]["links"][0]["href"] print(f"Found imagery for {date}: {download_link}") # Add a small delay to avoid hitting API rate limits time.sleep(1) else: print(f"No imagery found for {date}")
4. Download and Save the Images
Once you have the download link, use urllib or requests to save the file to your computer:
import urllib.request import os # Create a folder to store images if it doesn't exist os.makedirs("satellite_images", exist_ok=True) for date in target_dates: # (Repeat the API call logic above to get download_link) save_path = f"satellite_images/{date}_satellite.tif" urllib.request.urlretrieve(download_link, save_path) print(f"Saved image to {save_path}")
- API Rate Limits: Most NASA APIs limit how many requests you can send in a minute. Adding
time.sleep(1)between requests helps avoid getting blocked. - File Size: Raw satellite images can be huge (hundreds of MB to GB). Make sure you have enough storage space before batch downloading.
- Test First: Don’t jump into processing all your CSV dates at once. Test with 1-2 dates first to work out kinks in your code.
- Visualization (Optional): If you want to view the TIFF files, install
rasterioandmatplotlib—they make it easy to render satellite imagery in Python.
You’ve got this! This project is totally achievable for a new programmer, and the NASA APIs are well-documented if you need to dive deeper into specific parameters or datasets.
内容的提问来源于stack exchange,提问作者Bethany

