Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import ee | |
| from datetime import datetime | |
| # Earth Engine Authentication | |
| service_account = 'earth-engine-service-account@ee-esmaeilkiani1387.iam.gserviceaccount.com' | |
| credentials = ee.ServiceAccountCredentials(service_account, 'ee-esmaeilkiani1387-1b2c5e812a1d.json') | |
| ee.Initialize(credentials) | |
| # Define a function to fetch historical temperature data | |
| def get_historical_temperature(aoi, start_date, end_date): | |
| dataset = ee.ImageCollection("ECMWF/ERA5/DAILY") \ | |
| .filterBounds(aoi) \ | |
| .filterDate(start_date, end_date) \ | |
| .select('mean_2m_air_temperature') | |
| # Calculate daily mean temperature (in Celsius) | |
| temp_collection = dataset.map(lambda image: image.subtract(273.15).rename('daily_mean_temp')) | |
| # Reduce the collection to mean values over the area | |
| mean_temp = temp_collection.mean().reduceRegion( | |
| reducer=ee.Reducer.mean(), | |
| geometry=aoi, | |
| scale=1000, | |
| bestEffort=True | |
| ) | |
| return mean_temp.getInfo() | |
| # Define the area of interest (AOI) | |
| # Example: Assume aoi is defined based on user's GeoJSON or coordinates | |
| aoi = ee.Geometry.Point([48.73168141056203, 31.53180450320103]) # Example coordinates for a specific farm location | |
| # Set start and end dates | |
| start_date = "2023-01-01" | |
| end_date = "2023-12-31" | |
| # Fetch and display temperature data | |
| temp_data = get_historical_temperature(aoi, start_date, end_date) | |
| st.write("Historical Temperature Data (°C):", temp_data['daily_mean_temp']) | |