Shubham2004's picture
Create app.py
15fe7ea verified
raw
history blame
3.12 kB
import streamlit as st
import h5py
import os
# Define the path to the HDF5 file
filename = r'C:\Users\Lenovo\Desktop\Machine-Learning\Soil Moisture\SMAP_L4_SM_aup_20230121T000000_Vv7030_001.h5'
with h5py.File(filename, 'r') as test_file:
print(list(test_file.keys()))
# Update this path
# Check if the file exists
if not os.path.isfile(filename):
st.error("File not found: " + filename)
else:
try:
# Attempt to open the HDF5 file
with h5py.File(filename, 'r') as h5:
# Display keys in the HDF5 file
st.write("Keys in the HDF5 file:", list(h5.keys()))
# Add any other code to explore the datasets here
for key in h5.keys():
st.write(f"Exploring group: {key}")
for dataset_name in h5[key].keys():
dataset = h5[key][dataset_name]
st.write(f" - {dataset_name}: shape {dataset.shape}, type {dataset.dtype}")
except Exception as e:
st.error(f"An error occurred: {str(e)}")
import h5py
import numpy as np
import pandas as pd
import streamlit as st
import plotly.express as px
with h5py.File(filename, 'r', swmr=False) as h5:
filename = 'Soil Moisture\SMAP_L4_SM_aup_20230121T000000_Vv7030_001.h5'
with h5py.File(filename, 'r') as h5:
# Assuming 'sm_surface_analysis' is the soil moisture dataset
soil_moisture = h5['Analysis_Data/sm_surface_analysis'][:]
lat = h5['cell_lat'][:]
lon = h5['cell_lon'][:]
time = h5['time'][:]
# Create a Pandas DataFrame for easier handling
df = pd.DataFrame({
'Latitude': lat.flatten(),
'Longitude': lon.flatten(),
'Soil Moisture': soil_moisture.flatten()
})
# Streamlit App
st.title("Soil Moisture Data Dashboard")
st.write("This dashboard displays soil moisture levels based on latitude and longitude.")
# User input for latitude and longitude range
min_lat, max_lat = st.slider("Select Latitude Range", float(df['Latitude'].min()), float(df['Latitude'].max()), (float(df['Latitude'].min()), float(df['Latitude'].max())))
min_lon, max_lon = st.slider("Select Longitude Range", float(df['Longitude'].min()), float(df['Longitude'].max()), (float(df['Longitude'].min()), float(df['Longitude'].max())))
# Filter data based on user selection
filtered_data = df[(df['Latitude'] >= min_lat) & (df['Latitude'] <= max_lat) & (df['Longitude'] >= min_lon) & (df['Longitude'] <= max_lon)]
# Display filtered data
st.write(f"Displaying data for Latitude between {min_lat} and {max_lat} and Longitude between {min_lon} and {max_lon}")
st.dataframe(filtered_data)
# Plot the data on a map
fig = px.scatter_mapbox(filtered_data, lat='Latitude', lon='Longitude', color='Soil Moisture',
color_continuous_scale=px.colors.cyclical.IceFire, size_max=15, zoom=3)
# Configure the map layout
fig.update_layout(mapbox_style="open-street-map")
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
st.plotly_chart(fig)