-
Notifications
You must be signed in to change notification settings - Fork 0
/
icesat2_wse.py
129 lines (101 loc) · 4.26 KB
/
icesat2_wse.py
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#%%
# Standard library imports
import configparser
import requests
# Third-party library imports for data handling
import numpy as np
import pandas as pd
import geopandas as gpd
import h5py
import fsspec
import aiohttp
# Visualization libraries
import matplotlib.pyplot as plt
import seaborn as sns
from matplotlib import gridspec
# Geospatial and geodesy libraries
from pygeodesy.points import Numpy2LatLon
from pygeodesy.ellipsoidalKarney import LatLon
from pygeodesy.geoids import GeoidKarney
from shapely.geometry import LineString, Polygon
from shapely.geometry.geo import mapping
# Database connection library
from sqlalchemy import create_engine
# ICESat-2 data processing libraries
import sliderule
from sliderule import earthdata, icesat2
# STAC client for searching and accessing geospatial assets
from pystac_client import Client
rid = pd.read_csv('/Users/jakegearon/CursorProjects/sword_reaches/river_discharge_data.csv').reach_id.iloc[35]
rid_df = pd.read_csv('/Users/jakegearon/CursorProjects/sword_reaches/river_discharge_data.csv')
# Define the PostgreSQL connection parameters
db_params = {
'dbname': 'jakegearon',
'user': 'jakegearon',
'password': 'Derwood15',
'host': 'localhost',
'port': 5432
}
# Create a connection to the PostgreSQL database
engine = create_engine(f"postgresql+psycopg2://{db_params['user']}:{db_params['password']}@{db_params['host']}:{db_params['port']}/{db_params['dbname']}")
# Get the entire dataframe
sql = f"""
SELECT * FROM sword_reachesv16 WHERE reach_id = {rid};
"""
df = gpd.read_postgis(sql, engine, geom_col='geom', crs='EPSG:4326')
df_proj = df.to_crs(epsg=3857)
#icesat2.init("slideruleearth.io")
polygon = df_proj.buffer(df_proj['width']*3).to_crs(epsg=4326).unary_union
region = sliderule.toregion(polygon)
# Convert region to a shapely Polygon
region_polygon = Polygon([(point['lon'], point['lat']) for point in region['poly']])
# Simplify the polygon (adjust the tolerance as needed)
simplified_polygon = region_polygon.simplify(tolerance=0.01, preserve_topology=True)
# Convert the simplified polygon back to GeoJSON
region_geojson = mapping(simplified_polygon)
# Now use the simplified GeoJSON with the STAC API
catalog = Client.open("https://cmr.earthdata.nasa.gov/stac/NSIDC_ECS",)
query = catalog.search(
collections=["ATL13"], limit=10, intersects=region_geojson
)
items = list(query.items())
print(f"Found: {len(items):d} datasets")
url = items[0].assets['data'].href
# Custom session class to handle redirects with authentication
class SessionWithHeaderRedirection(requests.Session):
AUTH_HOST = 'urs.earthdata.nasa.gov'
def __init__(self, username, password):
super().__init__()
self.auth = (username, password)
def rebuild_auth(self, prepared_request, response):
headers = prepared_request.headers
url = prepared_request.url
if 'Authorization' in headers:
original_parsed = requests.utils.urlparse(response.request.url)
redirect_parsed = requests.utils.urlparse(url)
if (original_parsed.hostname != redirect_parsed.hostname) and \
redirect_parsed.hostname != self.AUTH_HOST and \
original_parsed.hostname != self.AUTH_HOST:
del headers['Authorization']
# Load credentials from config.ini
config = configparser.ConfigParser()
config.read('config.ini')
username = config.get('earthdata', 'username')
password = config.get('earthdata', 'password')
# Ensure that credentials are provided
assert username and password, "You must provide your Earthdata Login credentials"
# Assuming 'url' is the URL to the HDF5 file
with fsspec.open(url, mode='rb', client_kwargs={'auth': aiohttp.BasicAuth(username, password)}) as f:
with h5py.File(f, 'r') as hdf:
# Display the root keys in the HDF5 file
print("Keys in the HDF5 file:", list(hdf.keys()))
# Example: Access a dataset within the HDF5 file
# This assumes there's a dataset named 'data' at the root of the HDF5 file
# Adjust the string 'data' to match the actual dataset you're interested in
if 'data' in hdf:
data = hdf['data'][:]
print("Data from the 'data' dataset:", data)
else:
print("Dataset 'data' not found in the HDF5 file.")
#%%
#