I'm using the earthaccess Python library (version 0.10.0) that's available on PyPi to download HLS data.
I'm able to search for items but when I try to download an asset (i.e., a tif that pertains to a band), I get a 403 error.
My code is below.
import earthaccess
import geopandas as gpd
from osgeo import gdal
# Ordering: blue, green, red, nir08, swir16, swir22, ndvi, high, low, vege, soil, qa
COLLECTIONS = ["HLSL30", "HLSS30"]
COMMON_BANDS = ["blue", "green", "red", "nir08", "swir16", "swir22", "qa"]
COMMON_NDVI_BANDS = ["blue", "green", "red", "nir08", "swir16", "swir22", "ndvi", "qa"]
L8_BANDS = ["B02", "B03", "B04", "B05", "B06", "B07", "Fmask"]
S2_BANDS = ["B02", "B03", "B04", "B8A", "B11", "B12", "Fmask"]
NODATA_VALUE = -9999
START_DATE = "2014-01-01"
END_DATE = "2021-12-31"
MAX_RETRIES = 5
def make_item_dict(items):
from collections import defaultdict
item_di = defaultdict(lambda: [])
for item in items:
date = dayofyear_to_date(item["meta"]["native-id"])
item_di[date].append(item)
return item_di
def parse_asset_link(item_link: str) -> Tuple[str, str, str, str]:
"""
Parse earthaccess asset link to extract sensor and band name.
Arguments:
item_link: earthaccess item link.
Returns:
Sensor, common band name, canonical band name, & item link.
Used by: parse_item_links.
"""
parts = Path(item_link).stem.split(".")
sensor = parts[1]
canonical = parts[-1]
if (sensor == "S30") and (canonical in S2_BANDS):
common_ix = S2_BANDS.index(canonical)
elif (sensor == "L30") and (canonical in L8_BANDS):
common_ix = L8_BANDS.index(canonical)
else:
return None
common = COMMON_BANDS[common_ix]
return (sensor, common, canonical, item_link)
gdal.SetConfigOption("GDAL_HTTP_COOKIEFILE", str(args.cookies_src))
gdal.SetConfigOption("GDAL_HTTP_COOKIEJAR", str(args.cookies_src))
gdal.SetConfigOption("GDAL_DISABLE_READDIR_ON_OPEN", "EMPTY_DIR")
gdal.SetConfigOption("CPL_VSIL_CURL_ALLOWED_EXTENSIONS", "TIF")
gdal.SetConfigOption("GDAL_HTTP_UNSAFESSL", "YES")
chunksize = dict(band=1, x=args.chunksize, y=args.chunksize)
earthaccess.login(strategy="netrc", persist=True)
logger.info("Load region model and query earthaccess.")
output_bands = LSMA_BANDS if run_lsma else COMMON_NDVI_BANDS
# Example geojson link here: https://github.com/pubgeo/IARPA-SMART/blob/add_annotations/annotations/empty_region_models/BR_R002.geojson
region_model = gpd.read_file(args.region_model_src) # crs is already 4326
bbox = xmin, ymin, xmax, ymax = tuple(list(region_model.total_bounds))
polygon = Polygon.from_bounds(xmin, ymin, xmax, ymax)
bounds_gdf = gpd.GeoDataFrame(dict(geometry=[polygon]), crs=4326)
temporal = (f"{START_DATE}T00:00:00", f"{END_DATE}T23:59:59")
items = earthaccess.search_data(
short_name=COLLECTIONS,
bounding_box=bbox,
temporal=temporal,
count=args.results_count,
)
item_dict = make_item_dict(items)
for date, item_li in item_dict.items():
# we can have multiple items for one date depending on bounding box
for item in item_li:
asset_links = item.data_links()
for asset_link in asset_links:
parsed = parse_asset_link(asset_link)
_, common, canonical, href = parsed
for i in range(MAX_RETRIES):
xarr = rxr.open_rasterio(href, chunks=chunksize, masked=True).squeeze("band", drop=True)
# ^^this is where I get the 403 error
Unable to download HLS data: 403 error but authenticated
Re: Unable to download HLS data: 403 error but authenticated
I am having trouble downloading any of the OPERA DIST products as well from LP DAAC - pretty sure this is related.
I am getting forbidden for each link I get from stac or from earthdata search (web GUI). For instance these links:
https://data.lpdaac.earthdatacloud.nasa.gov/lp-prod-protected/OPERA_L3_DIST-ALERT-HLS_V1/OPERA_L3_DIST-ALERT-HLS_T10TEK_20240809T185919Z_20240811T090427Z_S2B_30_v1/OPERA_L3_DIST-ALERT-HLS_T10TEK_20240809T185919Z_20240811T090427Z_S2B_30_v1_VEG-DIST-STATUS.tif
https://data.lpdaac.earthdatacloud.nasa.gov/lp-prod-protected/OPERA_L3_DIST-ALERT-HLS_V1/OPERA_L3_DIST-ALERT-HLS_T37SBV_20231111T082059Z_20231223T034114Z_S2B_30_v1/OPERA_L3_DIST-ALERT-HLS_T37SBV_20231111T082059Z_20231223T034114Z_S2B_30_v1_VEG-IND.tif
I am getting forbidden for each link I get from stac or from earthdata search (web GUI). For instance these links:
https://data.lpdaac.earthdatacloud.nasa.gov/lp-prod-protected/OPERA_L3_DIST-ALERT-HLS_V1/OPERA_L3_DIST-ALERT-HLS_T10TEK_20240809T185919Z_20240811T090427Z_S2B_30_v1/OPERA_L3_DIST-ALERT-HLS_T10TEK_20240809T185919Z_20240811T090427Z_S2B_30_v1_VEG-DIST-STATUS.tif
https://data.lpdaac.earthdatacloud.nasa.gov/lp-prod-protected/OPERA_L3_DIST-ALERT-HLS_V1/OPERA_L3_DIST-ALERT-HLS_T37SBV_20231111T082059Z_20231223T034114Z_S2B_30_v1/OPERA_L3_DIST-ALERT-HLS_T37SBV_20231111T082059Z_20231223T034114Z_S2B_30_v1_VEG-IND.tif
-
- Posts: 422
- Joined: Mon Sep 30, 2019 10:00 am America/New_York
- Has thanked: 31 times
- Been thanked: 8 times
- Contact:
Re: Unable to download HLS data: 403 error but authenticated
Hello @cmarshak and @pgcar we believe the 403 issue has been resolved. Could you please try again and let us know if you are still having issues?
Subscribe to the LP DAAC listserv by sending a blank email to lpdaac-join@lists.nasa.gov.
Sign up for the Landsat listserv to receive the most up to date information about Landsat data: https://public.govdelivery.com/accounts/USDOIGS/subscriber/new#tab1.
Sign up for the Landsat listserv to receive the most up to date information about Landsat data: https://public.govdelivery.com/accounts/USDOIGS/subscriber/new#tab1.