本文將介紹如何從 NASA Earthdata 下載衛星資料,包含帳號設定、資料篩選、權限設定,以及使用 R 語言進行批次下載的完整流程。
下載 NASA 資料
步驟一:登入並選取資料
- 前往 NASA Earthdata 登入
- 若帳號被鎖定,需轉換 VPN
- 選取想要的資料集
- 點選 Subset/Get Data
- 若要篩選變數,在 Download Method 選擇 Get File Subsets using OPeNDAP
- 選取時間段、經緯度範圍及想要的變數
- 點選 Get Data 下載
下載完成會匯出文字檔,第一行是 pdf 連結可自行刪除或在讀檔時忽略,其餘都是資料的連結(.nc4 檔)。
步驟二:設定權限
- 前往 NASA Earthdata 個人檔案
- 進入 Applications → Authorized Apps → APPROVE MORE APPLICATIONS
- 開啟 NASA GESDISC DATA ARCHIVE 的權限
- 開啟後可先點一個連結測試是否可成功下載
步驟三:取得 Token(若使用 R 下載)
- 前往 NASA Earthdata 個人檔案
- 進入 Generate Token 取得 Token
- 執行以下程式碼進行批次下載
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
| # ---- 套件 ----
library(readr)
library(httr)
library(terra)
library(stringr)
# ---- 你的 Bearer Token ----
token <- "你的Token"
# ---- 載入連結清單 ----
txt_path <- "文字檔名稱.txt"
urls <- read_lines(txt_path)
urls <- urls[urls != ""] # 移除空行
cat("共讀入", length(urls), "筆連結。\n")
# ---- 建立下載資料夾 ----
dir.create("data", showWarnings = FALSE)
# ---- 逐一下載 ----
for (i in seq_along(urls)) {
# 從 URL 抓出年月,例如 198001 → 1980-01
ym_raw <- str_extract(urls[i], "(19|20)\\d{4}")
ym_fmt <- paste0(substr(ym_raw, 1, 4), "-", substr(ym_raw, 5, 6))
file_name <- paste0(ym_fmt, ".nc4")
dest_path <- file.path("data", file_name)
if (!file.exists(dest_path)) {
cat("下載中:", file_name, "\n")
r <- GET(
urls[i],
add_headers(Authorization = paste("Bearer", token)),
write_disk(dest_path, overwrite = TRUE),
timeout(600)
)
if (r$status_code == 200) {
cat("✅ 成功:", file_name, "\n")
} else {
cat("⚠️ 失敗:", file_name, "(HTTP", r$status_code, ")\n")
}
} else {
cat("⏭️ 已存在:", file_name, "\n")
}
}
# ---- 檢查結果 ----
downloaded <- list.files("data", pattern = "\\.nc4$", full.names = TRUE)
cat("共下載完成", length(downloaded), "個檔案。\n")
|
步驟四:檢視下載結果
下載完成後,所有的 .nc4 檔會在 data 資料夾內。