This repository has been archived by the owner on Sep 17, 2024. It is now read-only.
forked from ScienceCore/climaterisk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request ScienceCore#89 from ScienceCore/post_scipy_updates
Post scipy updates
- Loading branch information
Showing
40 changed files
with
1,942 additions
and
2,758 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
22 changes: 22 additions & 0 deletions
22
book/00_Introduction_Setup/00_Getting_NASA_EarthData_Credentials.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Getting NASA Earthdata Credentials | ||
|
||
|
||
This notebook lays out the process to obtain for NASA Earthdata credentials. | ||
|
||
**You can complete this step before the day of the actual tutorial.** | ||
|
||
|
||
## Brief Introduction | ||
|
||
|
||
The [NASA Earth Science Data Systems (ESDS) program oversees the lifecycle of Earth science data from all its Earth observation missions, from acquisition to processing and distribution. | ||
|
||
For the purposes of this guide, the NASA Earthdata website is the entry point that allows full, free and open access to NASA's Earth science data collections, in order to accelerate scientific progress for the benefit of society. To access the data through this portal, users must first define their access credentials. To create an EarthData account, follow these steps: | ||
|
||
+ Go to the Earth Nasa website: [`https://www.earthdata.nasa.gov/`](https://www.earthdata.nasa.gov/). Then, select the menu options "*Use Data*" and then "*Register*". Finally, navigate to [`https://urs.earthdata.nasa.gov/`](https://urs.earthdata.nasa.gov/). | ||
|
||
![earthdata_login](../assets/earthdata_login.png) | ||
|
||
+ Select the "*Register for a profile*" option, there choose a username and password. You will need these later, so choose ones that you remember well. You will also need to complete your profile to complete the registration, where you will be asked for information such as email, country, and affiliation. Finally, choose "*Register for Earthdata Login*". | ||
|
||
![earthdata_profile](../assets/earthdata_profile2.png) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# Using the 2i2c Hub | ||
|
||
|
||
This notebook lays out instructions to log into the cloud infrastructure ([JupyterHub](https://jupyter.org/hub)) provided by [2i2c](https://2i2c.org) for this tutorial. | ||
|
||
**You won't be able to complete this step until the actual day of the tutorial (you'll get the password then).** | ||
|
||
|
||
## 1. Logging into the 2i2c Hub | ||
|
||
|
||
To login to the JupyterHub provided by 2i2c, follow these steps: | ||
|
||
1. **Navigate to the 2i2c Hub:** Point your web browser to [this link](https://climaterisk.opensci.2i2c.cloud). | ||
|
||
2. **Log in with your Credentials:** | ||
|
||
+ **Username:** Feel free to choose any username you like. We suggest using your GitHub username to avoid conflicts. | ||
+ **Password:** *You'll receive the password on the day of the tutorial*. | ||
|
||
![2i2c_login](../assets/2i2c_login.png) | ||
|
||
3. **Logging In:** | ||
|
||
The login process might take a few minutes, especially if a new virtual workspace needs to be created just for you. | ||
|
||
![start_server2](../assets/start_server_2i2c.png) | ||
|
||
* **What to Expect:** | ||
|
||
By default, logging into [`https://climaterisk.opensci.2i2c.cloud`](https://climaterisk.opensci.2i2c.cloud) automatically clones a repository to work in. If the login is successful, you will see the following screen and are ready to start working. | ||
|
||
![work_environment_jupyter_lab](../assets/work_environment_jupyter_lab.png) | ||
|
||
**Notes:** Any files you work on will persist between sessions as long as you use the same username when logging in. | ||
|
||
## 2. Configuring the Cloud Environment to Access NASA EarthData from Python | ||
|
||
To access NASA's EarthData products from Python programs or Jupyter notebooks, it is necessary to save your NASA EarthData credentials in a special file called `.netrc` in your home directory. | ||
|
||
+ You can create this file by executing the script `make_netrc.py` in a terminal: | ||
```bash | ||
$ python make_netrc.py | ||
``` | ||
You can also choose to execute this script within this Jupyter notebook by executing the Python cell below (using the `%run` magic). | ||
|
||
Some caveats: | ||
+ The script won't execute if `~/.netrc` exists already. You can delete that file or rename it if you want to preserve the credentials within. | ||
+ The script prompts for your NASA EarthData username & password, so watch for the prompt if you execute it from a Jupyter notebook. | ||
```bash | ||
%run make_netrc.py | ||
``` | ||
+ Alternatively, you can create a file called `.netrc` in your home folder (i.e., `~/.netrc`) with content as follows: | ||
``` | ||
machine urs.earthdata.nasa.gov login USERNAME password PASSWORD | ||
``` | ||
Of course, you would replace `USERNAME` and `PASSWORD` in your `.netrc` file with your actual NASA EarthData account details. Once the `.netrc` file is saved with your correct credentials, it's good practice to restrict access to the file: | ||
```bash | ||
$ chmod 600 ~/.netrc | ||
``` | ||
|
||
|
||
## 3. Verifying Access to NASA EarthData Products | ||
<!-- #region --> | ||
To make sure everything is working properly, execute the script `test_netrc.py`: | ||
```bash | ||
$ python test_netrc.py | ||
``` | ||
Again, you can execute this directly from this notebook using the Python cell below: | ||
<!-- #endregion --> | ||
```bash | ||
%run test_netrc.py | ||
``` | ||
|
||
If that worked smoothly, you're done! You now have everything you need to explore NASA's Earth observation data through the EarthData portal! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# This script generates .netrc file, prompting user for EarthData credentials. | ||
# Delete or rename ~/.netrc before executing this script (it won't overwrite a | ||
# pre-existing ~/.netrc file). | ||
|
||
from getpass import getpass | ||
from pathlib import Path | ||
import sys | ||
|
||
NETRC_PATH = Path('~/.netrc').expanduser() | ||
TEMPLATE = " ".join(["machine", "urs.earthdata.nasa.gov", "login", | ||
"{USERNAME}", "password", "{PASSWORD}\n"]) | ||
|
||
if NETRC_PATH.exists(): | ||
print("Warning: ~/.netrc exists already (this script won't overwrite).") | ||
print(" Delete ~/.netrc first or back up to avoid losing credentials.") | ||
sys.exit(1) | ||
|
||
username = input("NASA EarthData login: ") | ||
password = getpass(prompt="NASA EarthData password: ") | ||
|
||
NETRC_PATH.write_text(TEMPLATE.format(USERNAME=username, PASSWORD=password)) | ||
NETRC_PATH.chmod(0o600) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Minimal test to verify NASA Earthdata credentials for downloading data products. | ||
# Requires a .netrc file in home directory containing valid credentials. | ||
|
||
import osgeo.gdal, rasterio | ||
from pystac_client import Client | ||
from warnings import filterwarnings | ||
filterwarnings("ignore") # suppress PySTAC warnings | ||
|
||
# Mandatory GDAL setup for accessing cloud data | ||
osgeo.gdal.SetConfigOption('GDAL_HTTP_COOKIEFILE','~/.gdal_cookies.txt') | ||
osgeo.gdal.SetConfigOption('GDAL_HTTP_COOKIEJAR', '~/.gdal_cookies.txt') | ||
osgeo.gdal.SetConfigOption('GDAL_DISABLE_READDIR_ON_OPEN','EMPTY_DIR') | ||
osgeo.gdal.SetConfigOption('CPL_VSIL_CURL_ALLOWED_EXTENSIONS','TIF, TIFF') | ||
|
||
# Define AOI (Area-Of-Interest) & time-window | ||
livingston_tx, delta = (-95.09, 30.69), 0.1 | ||
AOI = tuple(coord + sgn*delta for sgn in (-1,+1) for coord in livingston_tx) | ||
start, stop = '2024-04-30', '2024-05-31' | ||
WINDOW = f'{start}/{stop}' | ||
|
||
# Prepare PySTAC client | ||
STAC_URL, COLLECTIONS = 'https://cmr.earthdata.nasa.gov/stac', ["OPERA_L3_DSWX-HLS_V1"] | ||
catalog = Client.open(f'{STAC_URL}/POCLOUD/') | ||
|
||
print("Testing PySTAC search...") | ||
opts = dict(bbox=AOI, collections=COLLECTIONS, datetime=WINDOW) | ||
results = list(catalog.search(**opts).items_as_dicts()) | ||
test_uri = results[0]['assets']['0_B01_WTR']['href'] | ||
|
||
try: | ||
print(f"Search successful, accessing test data...") | ||
with rasterio.open(test_uri) as ds: _ = ds.profile | ||
print("Success! Your credentials file is correctly configured!") | ||
except: | ||
print(f"Could not access NASA EarthData asset: {test_uri}") | ||
print("Ensure that a .netrc file containing valid NASA Earthdata credentials exists in the user home directory.") |
Oops, something went wrong.