### Verify earthaccess Installation Source: https://github.com/nsidc/earthaccess/blob/main/docs/quick-start.md This Python code snippet demonstrates how to check if the earthaccess library is installed correctly by importing it and printing its version number. This is typically done within a Python interpreter. ```python >>> import earthaccess >>> earthaccess.__version__ ``` -------------------------------- ### Install earthaccess using Mamba, Conda, or Pip Source: https://github.com/nsidc/earthaccess/blob/main/docs/quick-start.md Instructions for installing the earthaccess Python library. It can be installed using 'mamba', 'conda', or 'pip'. Mamba is recommended for its speed. Python 3.8 or higher is required. ```bash mamba install -c conda-forge earthaccess ``` ```bash conda install -c conda-forge earthaccess ``` ```bash python -m pip install earthaccess ``` -------------------------------- ### Download Data Files using earthaccess Source: https://github.com/nsidc/earthaccess/blob/main/docs/quick-start.md This Python code downloads data files found through an earthaccess search to a specified local directory. If the directory does not exist, it will be created automatically. Data can also be opened in-memory. ```python files = earthaccess.download(results, "./local_folder") ``` -------------------------------- ### Login to Earthdata Source: https://github.com/nsidc/earthaccess/blob/main/docs/quick-start.md This Python code logs into NASA Earthdata services using the earthaccess library. It can use credentials from a .netrc file or environment variables, or prompt for manual login if not configured. ```python import earthaccess earthaccess.login() ``` -------------------------------- ### Search for Data using earthaccess Source: https://github.com/nsidc/earthaccess/blob/main/docs/quick-start.md This Python code snippet searches for specific data using the earthaccess library. It allows filtering by short name, bounding box, temporal range, and the number of results. The results object can then be used for downloading. ```python results = earthaccess.search_data( short_name='ATL06', bounding_box=(-10, 20, 10, 50), temporal=("1999-02", "2019-03"), count=10 ) ``` -------------------------------- ### Install released package with pip Source: https://github.com/nsidc/earthaccess/blob/main/docs/contributing/releasing.md Command to verify the released package installation from PyPI. Replace X.Y.Z with the actual released version number to test installation. ```bash python -m pip install earthaccess==vX.Y.Z ``` -------------------------------- ### Environment Variable Setup for Earthdata Login Source: https://github.com/nsidc/earthaccess/blob/main/docs/user_guide/authenticate.md Set Earthdata Login credentials as environment variables for automatic authentication. Supports MacOS, Linux, and Windows with both temporary and permanent configuration options. Enables seamless credential management without hardcoding sensitive information in scripts. ```bash export EARTHDATA_USERNAME="username" export EARTHDATA_PASSWORD="password" ``` ```bash EARTHDATA_USERNAME="username" EARTHDATA_PASSWORD="password" ``` ```cmd setx EARTHDATA_USERNAME "username" setx EARTHDATA_PASSWORD "password" ``` -------------------------------- ### Create .netrc file manually across platforms Source: https://github.com/nsidc/earthaccess/blob/main/docs/user_guide/authenticate.md Manual creation of .netrc files for Earthdata Login authentication. Includes shell commands for MacOS/Linux and CMD commands for Windows to append credentials to the appropriate file and set secure permissions. ```bash echo "machine urs.earthdata.nasa.gov login password " >> $HOME/.netrc chmod 600 $HOME/.netrc ``` ```cmd setx HOME %USERPROFILE% echo "machine urs.earthdata.nasa.gov login password " >> %HOME%\_netrc ``` -------------------------------- ### Authenticate with Earthdata Login Source: https://github.com/nsidc/earthaccess/blob/main/docs/tutorials/virtual_dataset_tutorial_with_TEMPO_Level3.ipynb Handles authentication with the Earthdata Login system using the `earthaccess.login()` function. It checks if already authenticated and provides an interactive login with persistence if not. It also prints the installed earthaccess version. ```python auth = earthaccess.login() if not auth.authenticated: # Ask for credentials and persist them in a .netrc file. auth.login(strategy="interactive", persist=True) print(earthaccess.__version__) ``` -------------------------------- ### Install earthaccess Python Library Source: https://github.com/nsidc/earthaccess/blob/main/README.md Installs the earthaccess Python library using pip. Ensure you have Python and pip installed on your system before running this command. ```shell python -m pip install earthaccess ``` -------------------------------- ### Create .netrc file using earthaccess.persist Source: https://github.com/nsidc/earthaccess/blob/main/docs/user_guide/authenticate.md Automatically generates a .netrc file by prompting for credentials and persisting them to disk. Simplifies credential management by storing Earthdata Login credentials securely with proper file permissions. ```python earthaccess.login(persist=True) ``` -------------------------------- ### Import Libraries and Setup Plotting Source: https://github.com/nsidc/earthaccess/blob/main/docs/tutorials/virtual_dataset_tutorial_with_TEMPO_Level3.ipynb Imports necessary Python libraries including cartopy, earthaccess, matplotlib, numpy, and xarray. It also configures matplotlib for inline plotting and adjusts figure DPI for reduced output size. ```python import cartopy.crs as ccrs import earthaccess import matplotlib.pyplot as plt import numpy as np import xarray as xr from matplotlib import rcParams %config InlineBackend.figure_format = 'jpeg' rcParams["figure.dpi"] = ( 80 # Reduce figure resolution to keep the saved size of this notebook low. ) ``` -------------------------------- ### Login to UAT System with earthaccess Source: https://github.com/nsidc/earthaccess/blob/main/docs/user_guide/authenticate.md Configure earthaccess to use the User Acceptance Testing (UAT) environment by setting the system parameter during login. Requires EDL account with UAT authorization. UAT environment provides testing access to production-like NASA data endpoints for validation and development purposes. ```python import earthaccess erthaccess.login(system=earthaccess.UAT) ``` -------------------------------- ### Access NASA Earth Science Data with earthaccess Source: https://github.com/nsidc/earthaccess/blob/main/README.md Demonstrates the three core steps to access NASA Earth Science data using the earthaccess Python library: logging in, searching for data based on criteria, and downloading the results. This requires the earthaccess library to be installed. ```python import earthaccess # 1. Login earthaccess.login() # 2. Search results = earthaccess.search_data( short_name='ATL06', # ATLAS/ICESat-2 L3A Land Ice Height bounding_box=(-10, 20, 10, 50), # Only include files in area of interest... temporal=("1999-02", "2019-03"), # ...and time period of interest. count=10 ) # 3. Access files = earthaccess.download(results, "/tmp/my-download-folder") ``` -------------------------------- ### Perform HTTPS GET Request with Range Header Source: https://github.com/nsidc/earthaccess/blob/main/docs/howto/edl.ipynb Demonstrates performing an HTTPS GET request using the authenticated session created by earthaccess. It includes a 'Range' header to fetch specific bytes of the remote file. ```python headers = {"Range": "bytes=0-100"} r = session.get(lpcloud_url, headers=headers) r.text ``` -------------------------------- ### Search datasets by DAAC provider Source: https://github.com/nsidc/earthaccess/blob/main/docs/user_guide/search.md Demonstrates searching for datasets from a specific NASA Distributed Active Archive Center (DAAC) using the daac parameter. This example finds all datasets available at the National Snow and Ice Data Center (NSIDC). ```python results = earthaccess.search_datasets( daac="nsidc", ) ``` -------------------------------- ### Perform time-range and region searches with earthaccess (Python) Source: https://github.com/nsidc/earthaccess/blob/main/docs/user_guide/search.md Demonstrates how to search datasets by time range and by geographic region using earthaccess in Python. Covers temporal searches, bounding boxes, polygons, points, circles, and lines. Assumes earthaccess is installed and credentials are configured. ```python results = earthaccess.search_data(\n short_name=\"ATL06\",\n temporal=(\"2025-01-01\",\"2025-01-15\"),\n ) ``` ```python import datetime\n\nresults = earthaccess.search_data(\n short_name=\"ATL06\",\n temporal=(\n datetime.datetime(2025,1,1),\n datetime.datetime(2025,1,15)\n ),\n ) ``` ```python results = earthaccess.search_data(\n short_name=\"ATL06\",\n bounding_box=(-46.5, 61.0, -42.5, 63.0),\n ) ``` ```python polygon = [\n (-49.64860422604741, 69.23553485026147),\n (-49.667876114626296, 69.07309059285959),\n (-49.1722491331669, 69.03175841820749),\n (-47.53552489113113, 69.03872918462292),\n (-47.35616491854395, 69.22149993224824),\n (-48.1447695277283, 69.33507802083219),\n (-49.178671242118384, 69.29455117736225),\n (-49.64860422604741, 69.23553485026147),\n] ``` ```python results = earthaccess.search_data(\n short_name=\"ATL06\",\n polygon=polygon,\n ) ``` ```python import geopandas\n\ngdf = geopandas.read_file()\n ``` -------------------------------- ### Bump version using bump-my-version Source: https://github.com/nsidc/earthaccess/blob/main/docs/contributing/releasing.md Command to increase the version number using bump-my-version tool. This updates version numbers across multiple files as configured in pyproject.toml. The example shows bumping the minor version from 1.2.3 to 1.3.0. ```bash bump-my-version bump minor ``` -------------------------------- ### Get data type information Source: https://github.com/nsidc/earthaccess/blob/main/docs/user_guide/results.md Retrieve data type and format information for a dataset. Returns format details including type, format name, and description. For example, may return HDF5 format with HTTPS delivery method. ```python result[0].data_type() ``` -------------------------------- ### Create Virtual Dataset Reference File with earthaccess Source: https://github.com/nsidc/earthaccess/blob/main/docs/tutorials/dmrpp-virtualizarr.ipynb Opens a NASA dataset using `earthaccess.open_virtual_mfdataset` with `load=False`, creating a virtual dataset object. This object's metadata is then used to generate a kerchunk JSON reference file for rapid loading with xarray. ```python %%time mur_vds = earthaccess.open_virtual_mfdataset( results, access="indirect", load=False, concat_dim="time", coords="all", compat="override", combine_attrs="drop_conflicts", ) mur_vds ``` -------------------------------- ### Get Temporary S3 Credentials Source: https://github.com/nsidc/earthaccess/blob/main/docs/howto/edl.ipynb Generates temporary S3 credentials for accessing data from a specified DAAC (e.g., 'NSIDC'). These credentials are valid for 1 hour and can be used with libraries supporting S3 buckets. Note: This example is commented out for security reasons. ```python # s3_credentials = auth.get_s3_credentials("NSIDC") ``` -------------------------------- ### Stream NASA Earth Data as File Objects in Python Source: https://context7.com/nsidc/earthaccess/llms.txt Open remote data files as file-like objects for streaming access without full downloads. Designed for integration with libraries like xarray for direct analysis. Requires prior authentication and granule search. Incomplete example demonstrates the search setup for streaming. ```python import earthaccess import xarray as xr earthaccess.login() # Search for data granules = earthaccess.search_data( short_name="ATL08", bounding_box=(-180, 40, 180, 90), temporal=("2019-03", "2019-04"), count=10 ) ``` -------------------------------- ### Initialize and Authenticate Earthaccess Source: https://github.com/nsidc/earthaccess/blob/main/docs/tutorials/SSL.ipynb This snippet shows how to import the earthaccess library, check its version, and initiate the authentication process. It handles both authenticated and unauthenticated states, prompting for interactive login and persisting credentials if necessary. This is crucial for accessing NASA Earth data. ```python import earthaccess print(f"using earthaccess v{earthaccess.__version__}") auth = earthaccess.login() # are we authenticated? if not auth.authenticated: # ask for credentials and persist them in a .netrc file auth.login(strategy="interactive", persist=True) ``` -------------------------------- ### Import Libraries for earthaccess and xarray Source: https://github.com/nsidc/earthaccess/blob/main/docs/tutorials/dmrpp-virtualizarr.ipynb Imports the necessary earthaccess and xarray libraries for data manipulation and access. These are foundational for all subsequent operations in the notebook. ```python import earthaccess import xarray as xr ``` -------------------------------- ### Earthaccess Initialization and Imports Source: https://github.com/nsidc/earthaccess/blob/main/docs/user_guide/fsspec.ipynb This code block sets up the necessary environment for using the earthaccess library. It includes importing key libraries like xarray, pandas, and numpy, and performs the initial login to the earthaccess service. ```python %%time import time import earthaccess as ea import matplotlib.pyplot as plt import numpy as np import pandas as pd import tqdm import xarray as xr ea.login() ``` -------------------------------- ### Perform Partial HTTP GET Request Source: https://github.com/nsidc/earthaccess/blob/main/docs/tutorials/file-access.ipynb This snippet demonstrates how to perform a partial HTTP GET request using a pre-established HTTPS session. It fetches the first 100 bytes of a remote file using the 'Range' header. Requires the `requests` library and an active HTTPS session. ```python headers = {"Range": "bytes=0-100"} r = session.get(lpcloud_url, headers=headers) r ``` -------------------------------- ### Search and preprocess SWOT L2 dataset using earthaccess (Python) Source: https://github.com/nsidc/earthaccess/blob/main/docs/tutorials/dmrpp-virtualizarr.ipynb Shows how to query the SWOT L2 dataset with `earthaccess.search_data`, define a preprocessing function that adds cycle and pass dimensions, and open a virtual multi-file dataset with concatenation. Requires the `earthaccess` and `xarray` libraries; the code runs in a Jupyter environment supporting magic commands. Input is a search result object; output is an `xarray.Dataset` with combined passes. ```Python results = earthaccess.search_data( count=10, temporal=("2023"), short_name="SWOT_L2_LR_SSH_Expert_2.0" ) ``` ```Python %%time def preprocess(ds: xr.Dataset) -> xr.Dataset: # Add cycle number and pass_number as dimensions return ds.expand_dims(["cycle_num", "pass_num"]).assign_coords( cycle_num=[ds.attrs["cycle_number"]], pass_num=[ds.attrs["pass_number"]] ) swot = earthaccess.open_virtual_mfdataset( results, access="indirect", load=False, preprocess=preprocess, concat_dim="pass_num", coords="all", compat="override", combine_attrs="drop_conflicts", ) swot ``` -------------------------------- ### Search and Open Grouped NASA Dataset Source: https://github.com/nsidc/earthaccess/blob/main/docs/tutorials/dmrpp-virtualizarr.ipynb Searches for a specific NASA dataset using its DOI and opens it as a virtual dataset, specifying the 'product' group. This demonstrates how to access data organized into groups within a larger dataset. ```python # NASA TEMPO NO2 tropospheric and stratospheric columns V03 results = earthaccess.search_data(count=2, doi="10.5067/IS-40e/TEMPO/NO2_L2.003") len(results) earthaccess.open_virtual_dataset(results[0], group="product") ``` -------------------------------- ### Search Datasets and List Services (Python) Source: https://github.com/nsidc/earthaccess/blob/main/docs/user_guide/search.md This snippet demonstrates how to search for datasets using `earthaccess.search_datasets` and then iterate through the results to print available services for each dataset using the `dataset.services()` method. It assumes a 'datasets' variable is already populated. ```python results = earthaccess.search_datasets( short_name="MUR-JPL-L4-GLOB-v4.1", cloud_hosted=True, temporal=("2024-02-27T00:00:00Z", "2024-02-29T23:59:59Z"), ) for dataset in datasets: print(dataset.services()) ``` -------------------------------- ### Virtual Datasets with VirtualiZarr in Python Source: https://context7.com/nsidc/earthaccess/llms.txt Shows creation of virtual datasets using DMR++ metadata for data access without downloading via earthaccess. Requires login and search results with DMR++ support, offering single or multi-granule virtual datasets with options for concatenation and lazy loading. Enables export to kerchunk, limited by dataset compatibility and access modes. ```python import earthaccess earthaccess.login() # Search for data with DMR++ support results = earthaccess.search_data( count=5, temporal=("2024-01-01", "2024-01-05"), short_name="MUR-JPL-L4-GLOB-v4.1" ) # Open single virtual dataset vds = earthaccess.open_virtual_dataset( results[0], access="indirect" # Use "direct" when in AWS us-west-2 ) print(vds) # xarray Dataset with ManifestArrays # Open multiple granules as single dataset vds = earthaccess.open_virtual_mfdataset( results, access="indirect", load=False, concat_dim="time", coords="minimal", compat="override", combine_attrs="drop_conflicts" ) # Export to kerchunk reference file vds.virtualize.to_kerchunk("combined_ref.json", format="json") # Open with lazy loading vds_lazy = earthaccess.open_virtual_mfdataset( results, access="indirect", load=True, # Serialize references for lazy indexing reference_format="parquet", concat_dim="time" ) ``` -------------------------------- ### Load Dataset from Kerchunk File with xarray Source: https://github.com/nsidc/earthaccess/blob/main/docs/tutorials/dmrpp-virtualizarr.ipynb Loads the previously saved 'mur_kerchunk.json' file using xarray with the 'kerchunk' engine. This demonstrates rapid dataset loading from the virtual reference file, utilizing provided storage options for cloud access. ```python %%time fs = earthaccess.get_fsspec_https_session() ds = xr.open_dataset( "mur_kerchunk.json", engine="kerchunk", chunks={}, storage_options={ "remote_protocol": fs.protocol, "remote_options": fs.storage_options, }, ) print(ds) ``` -------------------------------- ### Download On-Prem Data Granules (Python) Source: https://github.com/nsidc/earthaccess/blob/main/docs/howto/onprem.md Authenticates with earthaccess, searches for granules of the 'NSIDC-0051' dataset for November 2022 in the northern hemisphere, and downloads them to the current directory. Requires the earthaccess library. ```python import earthaccess auth = earthaccess.login() results = earthaccess.search_data( short_name='NSIDC-0051', temporal=('2022-11-01', '2022-11-30'), bounding_box=(-180, 0, 180, 90) ) downloaded_files = earthaccess.download( results, local_path='.', ) ``` -------------------------------- ### Retrieve Data Links (Python) Source: https://github.com/nsidc/earthaccess/blob/main/docs/howto/access-data.md This snippet shows how to retrieve direct or external data links for granules using the earthaccess library. It's useful when integrating with existing workflows or different programming languages. The 'access' parameter determines whether to fetch 'direct' (for us-west-2 region) or 'external' links. ```python # if the data set is cloud-hosted there will be S3 links available. The access parameter accepts "direct" or "external". Direct access is only possible if you are in the us-west-2 region in the cloud. data_links = [granule.data_links(access="direct") for granule in results] # or if the data is an on-prem dataset data_links = [granule.data_links(access="external") for granule in results] ``` -------------------------------- ### Display Dataset Summary in Python Source: https://github.com/nsidc/earthaccess/blob/main/docs/user_guide/search.md This code displays a summary of the first search result using the summary method, which returns key fields like short-name, concept-id, version, file-type, and data access links. It requires having earthaccess search results stored in a variable named 'result'. The output is a dictionary with the dataset details. Note that 'get-data' links require user authentication via NASA's Earthdata Login. ```python pprint(result[0].summary()) ``` -------------------------------- ### Query DataGranules with earthaccess Source: https://github.com/nsidc/earthaccess/blob/main/docs/tutorials/restricted-datasets.ipynb DataGranules class performs authenticated queries for granules using concept_id, bounding_box, and temporal filters. hits() may undercount for restricted data; retrieve actual results with get(). Depends on Auth for ACL access; outputs list of granule metadata. ```python # An anonymous query to CMR Query = DataGranules().keyword('elevation') # An authenticated query to CMR Query = DataGranules(auth).keyword('elevation') ``` ```python Query = ( DataGranules(auth) .concept_id("C2153572614-NSIDC_CPRD") .bounding_box(-134.7, 58.9, -133.9, 59.2) .temporal("2020-03-01", "2020-03-30") ) # Unfortunately the hits() methods will behave the same for granule queries print(f"Granules found with hits(): {Query.hits()}") cloud_granules = Query.get() print(f"Actual number found: {len(cloud_granules)}") ``` -------------------------------- ### Search for datasets with earthaccess Source: https://github.com/nsidc/earthaccess/blob/main/docs/howto/search-services.md Uses earthaccess to search for datasets by short name, cloud hosting status, and temporal range. Returns dataset metadata including associated services. Requires knowledge of the dataset's short name. ```python import earthaccess datasets = earthaccess.search_datasets( short_name="MUR-JPL-L4-GLOB-v4.1", cloud_hosted=True, temporal=("2024-02-27T00:00:00Z", "2024-02-29T23:59:59Z"), ) ``` -------------------------------- ### Retrieve S3 Credentials for Cloud Data Access Source: https://github.com/nsidc/earthaccess/blob/main/docs/user_guide/authenticate.md Obtain S3 credentials from earthaccess for accessing NASA Earthdata Cloud data with other packages like xarray and h5coro. Handles authentication with specified DAAC (Distributed Active Archive Center). Enables cloud-native data workflows without manual credential management. ```python import earthaccess import xarray as xr import h5coro auth = earthaccess.login() s3_credentials = auth.get_s3_credentials(daac="NSIDC") s3url_atl23 = 'nsidc-cumulus-prod-protected/ATLAS/ATL23/001/2023/03/' \ '01/ATL23_20230401000000_10761801_001_01.h5' ds = xr.open_dataset(s3url_atl23, engine='h5coro', group='/mid_latitude/beam_1', credentials=s3_credentials) ``` -------------------------------- ### S3 Credentials and Filesystem Access in Python Source: https://context7.com/nsidc/earthaccess/llms.txt Illustrates obtaining temporary S3 credentials and filesystem instances for direct cloud data access using earthaccess and s3fs. Requires earthaccess login and optionally search results for credentials. Provides credentials dict and s3fs filesystem for bucket operations or xarray integration, limited by credential expiration and DAAC-specific access. ```python import earthaccess import s3fs earthaccess.login() # Get S3 credentials (valid for 1 hour) credentials = earthaccess.get_s3_credentials(daac="NSIDC") print(credentials) # {'accessKeyId': '...', 'secretAccessKey': '...', 'sessionToken': '...'} # Get credentials from search results granules = earthaccess.search_data( short_name="ATL08", cloud_hosted=True, count=1 ) credentials = earthaccess.get_s3_credentials(results=granules) # Get authenticated S3 filesystem s3_fs = earthaccess.get_s3_filesystem(provider="POCLOUD") # List files in S3 bucket files = s3_fs.ls("podaac-ops-cumulus-protected/MODIS_A-JPL-L2P-v2019.0/") # Read file directly from S3 with s3_fs.open("s3://bucket/path/to/file.nc", "rb") as f: data = f.read() # Use with xarray import xarray as xr s3_path = "s3://bucket/path/to/file.nc" with s3_fs.open(s3_path) as f: ds = xr.open_dataset(f) ``` -------------------------------- ### Search NASA Data with earthaccess Source: https://github.com/nsidc/earthaccess/blob/main/docs/tutorials/dmrpp-virtualizarr.ipynb Searches for NASA data using specified temporal ranges and short names. The `earthaccess.search_data` function returns a list of matching data granules. The number of results is then printed. ```python # NASA JPL Multiscale Ultrahigh Resolution (MUR) Sea Surface Temperature (SST) dataset - 0.01 degree resolution results = earthaccess.search_data( temporal=("2010-01-01", "2010-01-31"), short_name="MUR-JPL-L4-GLOB-v4.1" ) len(results) ``` -------------------------------- ### Search by Granule Name with Wildcards in EarthAccess Python Source: https://github.com/nsidc/earthaccess/blob/main/docs/user_guide/search.md Retrieves specific granules by exact or wildcard granule_name in earthaccess.search_data, useful for reproducing analyses or targeting RGT/cycle in datasets like ICESat-2 ATL10. Supports * for zero+ characters and ? for single; requires short_name. Filename formats vary by dataset; consult user guides for patterns. ```python results = earthaccess.search_data( short_name="ATL10", granule_name="ATL10-02_20181014000347_02350101_006_02.h5", ) ``` ```python results = earthaccess.search_data( short_name="ATL10", granule_name="ATL10-02_*_0235????_006_02.h5", ) len(results) ``` -------------------------------- ### Get Out-of-Region Data Links for a Granule Source: https://github.com/nsidc/earthaccess/blob/main/docs/tutorials/SSL.ipynb This snippet retrieves the data links for a granule, specifically requesting access from 'out of region'. This is useful when the data is cloud-hosted within a specific region (e.g., AWS) and needs to be accessed from outside that region, ensuring compatibility with various access strategies. ```python granules[0].data_links(access="out_of_region") ``` -------------------------------- ### Search for Data using EarthAccess Source: https://github.com/nsidc/earthaccess/blob/main/docs/user_guide/fsspec.ipynb This snippet shows how to search for data using the `ea.search_data()` function. It specifies search criteria, including short name, version, cloud hosting flag, temporal range, bounding box, and the number of results to return. The function returns a set of data collection results. ```python results = ea.search_data( short_name="ATL03", version="006", cloud_hosted=True, temporal=("2025-01", "2025-08"), bounding_box=(33.98, 31.19, 34.65, 31.51), count=1, ) results[0] ``` -------------------------------- ### Refine dataset search with multiple filters Source: https://github.com/nsidc/earthaccess/blob/main/docs/user_guide/search.md Shows how to combine multiple search parameters including platform, downloadable, and cloud_hosted flags to narrow results to AWS-hosted datasets available for direct download. This search refines the results from 82 to 59 datasets. ```python results = earthaccess.search_datasets( platform="icesat-2", downloadable=True, cloud_hosted=True, ) print(len(results)) # Output: 59 ``` -------------------------------- ### Direct S3 Access to xarray (Python) Source: https://github.com/nsidc/earthaccess/blob/main/docs/howto/access-data.md This snippet illustrates how to stream gridded EarthData directly into an xarray Dataset, optimized for users within the us-west-2 AWS region. It simplifies the process of working with cloud-hosted datasets, especially those without complex hierarchical variables. This method also supports data stored at NASA storage centers. ```python import xarray as xr files = earthaccess.open(results) ds = xr.open_mfdataset(files) ``` -------------------------------- ### Get Direct Data Links for a Granule Source: https://github.com/nsidc/earthaccess/blob/main/docs/tutorials/SSL.ipynb This code retrieves the direct HTTPS URL for accessing a specific data granule. It allows for downloading the data using standard HTTP methods, even if the data is hosted in a different region than the user's. This is essential for accessing cloud-hosted data remotely. ```python granules[0].data_links(access="direct") ``` -------------------------------- ### Query DataCollections with earthaccess Source: https://github.com/nsidc/earthaccess/blob/main/docs/tutorials/restricted-datasets.ipynb DataCollections class queries CMR for datasets, supporting anonymous or authenticated searches with parameters like keyword, short_name, version, and fields. For ACL-restricted collections, hits() undercounts; use len(get()) for actual count. Requires Auth for restricted NSIDC data; outputs collection metadata. ```python # An anonymous query to CMR Query = DataCollections().keyword('elevation') # An authenticated query to CMR Query = DataCollections(auth).keyword('elevation') ``` ```python # The first step is to create a DataCollections query Query = DataCollections() # Use chain methods to customize our query Query.short_name("ATL06").version("006") print(f"Collections found: {Query.hits()}") # filtering what UMM fields to print, to see the full record we omit the fields filters # meta is always included as collections = Query.fields(["ShortName", "Version"]).get(5) # Inspect some results printing just the ShortName and Abstract collections ``` ```python Query = DataCollections(auth) # Use chain methods to customize our query Query.short_name("ATL06").version("006") # This will say 1, even though we get 2 back. print(f"Collections found: {Query.hits()}") collections = Query.fields(["ShortName", "Version"]).get() # Inspect some results printing just the ShortName and Abstract collections ``` -------------------------------- ### Search for ATL03 Data V007 Source: https://github.com/nsidc/earthaccess/blob/main/docs/user_guide/fsspec.ipynb This snippet searches for ATL03 data with version 007, specifying cloud hosting, a temporal range, and a bounding box. It then prints the details of the first result found, including its spatial and temporal coverage, size, and data URL. ```python results = ea.search_data( short_name="ATL03", version="007", cloud_hosted=True, temporal=("2025-01", "2025-08"), bounding_box=(33.98, 31.19, 34.65, 31.51), count=1, ) results[0] ``` -------------------------------- ### Open Remote Files with Earthaccess and xarray Source: https://github.com/nsidc/earthaccess/blob/main/docs/tutorials/file-access.ipynb Demonstrates opening multiple remote files found via `earthaccess.search_data` and then opening one of them using `xarray`. This method streams data and integrates seamlessly with analysis libraries. Requires `earthaccess`, `xarray`, and `%%time` for performance measurement. ```python %%time import xarray as xr files = earthaccess.open(results[0:2]) ds = xr.open_dataset(files[0], group="/gt1r/land_ice_segments") ds ``` -------------------------------- ### Open and Load Earth Data Datasets with earthaccess in Python Source: https://github.com/nsidc/earthaccess/blob/main/docs/tutorials/SSL.ipynb This snippet uses earthaccess.open() to convert granules into file-like objects and loads them with xarray for analysis. It requires earthaccess and xarray libraries; input is a list of granules, output is an xarray Dataset. Note that performance depends on network connection and file size. ```python %%time import xarray as xr fileset = earthaccess.open(granules) print(f" Using {type(fileset[0])} filesystem") ds = xr.open_mfdataset(fileset, chunks={}) ds ``` -------------------------------- ### Open Cloud Optimized HDF5 with BlockCache Source: https://github.com/nsidc/earthaccess/blob/main/docs/user_guide/fsspec.ipynb Demonstrates opening a cloud-optimized HDF5 file using earthaccess with default settings, which now favor `BlockCache`. It then opens a specific group within the dataset using xarray and prints the cache info, highlighting the `BlockCache`'s performance characteristics. ```python %%time # best case scenario, block caching on a cloud optimized HDF5 file. file_handler = ea.open(results)[0] ds = xr.open_dataset(file_handler, group="/gt1l/heights") file_handler.f.cache ``` -------------------------------- ### Search Data with Earthaccess Source: https://github.com/nsidc/earthaccess/blob/main/docs/tutorials/file-access.ipynb Demonstrates searching for data using various criteria like short name, cloud hosting status, temporal range, and geographical polygon. Returns a list of results containing metadata for accessing files. Requires the earthaccess library. ```python results = earthaccess.search_data( short_name="ATL06", cloud_hosted=False, temporal=("2019-01", "2019-02"), polygon=[(-100, 40), (-110, 40), (-105, 38), (-100, 40)], ) results[0] ``` -------------------------------- ### Plot SST Data with xarray Source: https://github.com/nsidc/earthaccess/blob/main/docs/tutorials/dmrpp-virtualizarr.ipynb Selects a specific time slice and geographical region from the 'mur' dataset and plots the 'analysed_sst' variable using `pcolormesh`. This visualizes the Sea Surface Temperature data for a given area and time. ```python mur.isel(time=0).sel( lat=slice(20, 45), lon=slice(-95, -50) ).analysed_sst.plot.pcolormesh(x="lon", y="lat", cmap="plasma", figsize=(8, 4)) ``` -------------------------------- ### Search datasets by keyword using earthaccess search_datasets Source: https://github.com/nsidc/earthaccess/blob/main/docs/user_guide/search.md Demonstrates basic dataset search using the keyword parameter to find ICESat-2 related datasets. The search returns a Python list of results that can be examined using standard Python functions. Note that result counts may vary as NASA migrates data to the cloud by July 2026. ```python results = earthaccess.search_datasets( keyword="icesat-2" ) print(len(result)) # Output: 82 ``` -------------------------------- ### Authenticate and Initialize earthaccess Source: https://github.com/nsidc/earthaccess/blob/main/docs/tutorials/emit-earthaccess.ipynb Authenticates the user with NASA Earthdata Login (EDL) and initializes the earthaccess library. This is a prerequisite for accessing NASA's cloud-hosted data. It requires valid EDL credentials. ```python from pprint import pprint import earthaccess import xarray as xr print(f"using earthaccess version {earthaccess.__version__}") auth = earthaccess.login() ``` -------------------------------- ### Inspect Virtual Dataset Contents Source: https://github.com/nsidc/earthaccess/blob/main/docs/tutorials/dmrpp-virtualizarr.ipynb Prints the data access information for the 'analysed_sst' variable within the virtual dataset. It shows the underlying chunk structure and manifest details, illustrating how the data is organized in the cloud. ```python # Example of what's inside this virtual dataset print(mur_vds.analysed_sst.data) print(mur_vds.analysed_sst.data.manifest.dict()["0.0.1"]) ``` -------------------------------- ### Display Dataset Size Source: https://github.com/nsidc/earthaccess/blob/main/docs/tutorials/dmrpp-virtualizarr.ipynb Calculates and prints the size of the loaded xarray Dataset in Gigabytes (GB). This provides an estimate of the total data volume being accessed. ```python print(f"{mur.nbytes / 1e9} GB") ``` -------------------------------- ### Search datasets by short name and version Source: https://github.com/nsidc/earthaccess/blob/main/docs/user_guide/search.md Shows how to find a specific dataset version using short_name and version parameters. Demonstrates checking the result count and retrieving the unique concept ID identifier, which is essential for programmatically accessing specific dataset versions. ```python results = earthaccess.search_datasets( short_name="ATL03", version="007", ) # Check result count len(results) # Output: 1 # Get concept ID results[0].concept_id() # Output: 'C3326974349-NSIDC_CPRD' ```