import os
import fiona
import asyncio
import aiohttp
import geopandas as gpd
import pandas as pd
from shapely.geometry import Point
import time
import logging
import diskcache as dc
import numpy as np
from datetime import datetime
import json
async with aiohttp.ClientSession() as session:
remaining_indices = set(range(len(grid_points))) - processed_indices
for i in range(0, len(remaining_indices), batch_size):
batch_indices = list(remaining_indices)[i:i+batch_size]
batch = grid_points.iloc[batch_indices]
tasks = [fetch_solar_data(session, point.y, point.x, cache) for point in batch.geometry]
results = await asyncio.gather(*tasks)
valid_results = [r for r in results if r is not None]
if valid_results:
progress_tracker.update(batch_indices, valid_results)
solar_data.extend(valid_results)
cache.close()
return solar_data
def create_grid_points(geojson_path='india-soi.geojson', resolution=15000, checkpoint_manager=None):
if checkpoint_manager:
existing_points = checkpoint_manager.load_grid_points()
if existing_points is not None:
logger.info("Loading existing grid points from checkpoint")
return existing_points
try:
india = gpd.read_file(geojson_path)
if india.crs != 'EPSG:4326':
india = india.to_crs('EPSG:4326')
india_geom = india.geometry.buffer(0).unary_union
bounds = india_geom.bounds
mid_lat = (bounds[1] + bounds[3]) / 2
deg_resolution = resolution / 111000
lon_resolution = deg_resolution / np.cos(np.radians(mid_lat))
lons = np.arange(bounds[0], bounds[2], lon_resolution)
lats = np.arange(bounds[1], bounds[3], deg_resolution)
valid_points = []
total_points = len(lons) * len(lats)
processed = 0
sindex = india.sindex # Spatial index for faster contains checks
for lon in lons:
for lat in lats:
point = Point(lon, lat)
processed += 1
possible_matches_index = list(sindex.intersection(point.bounds))
possible_matches = india.iloc[possible_matches_index]
if any(possible_matches.contains(point)):
valid_points.append(point)
if processed % 5000 == 0:
logger.info(
f"Processed {processed}/{total_points} points "
f"({(processed/total_points)*100:.1f}%) - "
f"Valid points so far: {len(valid_points)}"
)
if not valid_points:
raise ValueError("No valid points found within boundary")
points_gdf = gpd.GeoDataFrame(geometry=valid_points, crs='EPSG:4326')
points_gdf['latitude'] = points_gdf.geometry.y
points_gdf['longitude'] = points_gdf.geometry.x
if checkpoint_manager:
checkpoint_manager.save_grid_points(points_gdf)
return points_gdf
except Exception as e:
logger.error(f"Error in create_grid_points: {e}")
raise
# Combine checkpoint data with final results
result_df = pd.DataFrame(solar_data)
output_file = 'india_solar_data.csv'
result_df.to_csv(output_file, index=False)
logger.info(f"Saved complete solar data to {output_file}")
except Exception as e:
logger.error(f"Error in main: {e}")
if name == "main":
asyncio.run(main())
The above is my file 'collect_solar_data.py' which is in the root directory of the repo 'raydium'
below is my file 'visualize_solar_data.py' which is also in root directory of the repo 'raydium'
import geopandas as gpd
import pandas as pd
import numpy as np
import folium
import branca.colormap as cm
import matplotlib.pyplot as plt
from rasterio.transform import from_bounds
from rasterio.features import geometry_mask
from shapely.geometry import Point
import logging
from scipy.ndimage import gaussian_filter
def create_solar_map(solar_data_path='india_solar_data.csv', geojson_path='india-soi.geojson'):
try:
# Load and prepare data
logger.info("Loading data...")
solar_df = pd.read_csv(solar_data_path)
india = gpd.read_file(geojson_path)
# Ensure input data is in WGS84
if india.crs != 'EPSG:4326':
india = india.to_crs('EPSG:4326')
# Create GeoDataFrame from solar data
solar_gdf = gpd.GeoDataFrame(
solar_df,
geometry=[Point(xy) for xy in zip(solar_df.longitude, solar_df.latitude)],
crs='EPSG:4326'
)
# Use Asia South Albers Equal Area projection
india_proj = india.to_crs('ESRI:102028')
solar_gdf_proj = solar_gdf.to_crs('ESRI:102028')
# Calculate bounds and create raster
bounds = india_proj.total_bounds
resolution = 2000 # 2km resolution
width = int((bounds[2] - bounds[0]) / resolution)
height = int((bounds[3] - bounds[1]) / resolution)
transform = from_bounds(bounds[0], bounds[1], bounds[2], bounds[3], width, height)
# Initialize raster
raster_data = np.zeros((height, width), dtype=np.float32)
weight_matrix = np.zeros((height, width), dtype=np.float32)
# Populate raster with improved interpolation
logger.info("Populating raster with solar potential data...")
for idx, row in solar_gdf_proj.iterrows():
x, y = row.geometry.x, row.geometry.y
col, row_idx = ~transform * (x, y)
col, row_idx = int(col), int(row_idx)
if 0 <= row_idx < height and 0 <= col < width:
raster_data[row_idx, col] += row['potential']
weight_matrix[row_idx, col] += 1
# Average values where multiple points exist
mask = weight_matrix > 0
raster_data[mask] = raster_data[mask] / weight_matrix[mask]
# Create and apply mask for India's boundaries
logger.info("Applying geometry mask...")
geometry = [feature['geometry'] for feature in india_proj.__geo_interface__['features']]
mask_raster = geometry_mask(geometry, out_shape=(height, width), transform=transform, invert=True)
# Apply mask
raster_data = np.ma.masked_array(raster_data, ~mask_raster)
# Apply smoothing
logger.info("Applying Gaussian filter for smoothing...")
filled_data = raster_data.filled(np.nan)
smoothed_data = gaussian_filter(filled_data, sigma=1)
smoothed_masked = np.ma.masked_array(smoothed_data, ~mask_raster)
# Calculate value ranges using percentiles
logger.info("Calculating percentile-based vmin and vmax...")
valid_data = smoothed_masked.compressed()
if valid_data.size == 0:
logger.error("No valid data available after masking. Exiting.")
return
# Ensure we have valid data and proper sorting of vmin/vmax
vmin = float(np.nanpercentile(valid_data, 2))
vmax = float(np.nanpercentile(valid_data, 98))
logger.info(f"Initial vmin (2nd percentile): {vmin}")
logger.info(f"Initial vmax (98th percentile): {vmax}")
# Ensure vmin and vmax are valid and properly ordered
if not (np.isfinite(vmin) and np.isfinite(vmax)):
logger.warning("vmin or vmax is not finite. Using min and max of valid data.")
vmin = float(np.nanmin(valid_data))
vmax = float(np.nanmax(valid_data))
if vmin >= vmax:
logger.warning(f"vmin ({vmin}) >= vmax ({vmax}). Adjusting values.")
vmin = float(np.nanmin(valid_data))
vmax = float(np.nanmax(valid_data))
if vmin >= vmax:
vmin = 0.0
vmax = 1.0
logger.warning(f"Setting default range: vmin={vmin}, vmax={vmax}")
# Create matplotlib visualization
logger.info("Creating matplotlib visualization...")
fig, ax = plt.subplots(figsize=(20, 20), dpi=300)
ax.set_axis_off()
# Plot with correct orientation
img = ax.imshow(
smoothed_masked,
extent=india_proj.total_bounds,
cmap='plasma',
origin='upper',
interpolation='nearest',
vmin=vmin,
vmax=vmax
)
# Add boundary outline
india_proj.boundary.plot(ax=ax, color='black', linewidth=1)
# Add colorbar
plt.colorbar(img, ax=ax, label='Solar Potential (kWh/m²/year)')
# Save high-resolution image
plt.savefig('solar_potential_high_res.png',
bbox_inches='tight',
pad_inches=0.1,
dpi=300,
facecolor='white',
edgecolor='none')
plt.close(fig)
# Create interactive Folium map
logger.info("Creating interactive Folium map...")
india_wgs84 = india_proj.to_crs('EPSG:4326')
bounds_latlon = india_wgs84.total_bounds
center_lat = (bounds_latlon[1] + bounds_latlon[3]) / 2
center_lon = (bounds_latlon[0] + bounds_latlon[2]) / 2
m = folium.Map(
location=[center_lat, center_lon],
zoom_start=5,
tiles='CartoDB positron'
)
# Create colormap with guaranteed sorted thresholds
colors = ['yellow', 'orange', 'red']
colormap = cm.LinearColormap(
colors=colors,
vmin=vmin,
vmax=vmax,
caption='Solar Potential (kWh/m²/year)',
index=np.linspace(vmin, vmax, len(colors)) # Ensure proper spacing
)
# Add image overlay
img_bounds = [
[bounds_latlon[1], bounds_latlon[0]],
[bounds_latlon[3], bounds_latlon[2]]
]
folium.raster_layers.ImageOverlay(
name='Solar Potential',
image='solar_potential_high_res.png',
bounds=img_bounds,
opacity=0.7,
interactive=True,
cross_origin=False,
zindex=1,
).add_to(m)
# Add boundary overlay
folium.GeoJson(
india_wgs84.__geo_interface__,
style_function=lambda x: {
'fillColor': 'none',
'color': 'black',
'weight': 1.5,
'fillOpacity': 0
},
name='India Boundary'
).add_to(m)
colormap.add_to(m)
folium.LayerControl().add_to(m)
# Save interactive map
m.save('india_solar_potential.html')
logger.info("Map creation completed successfully!")
except Exception as e:
logger.exception(f"An error occurred: {e}")
if name == "main":
create_solar_map()
I want you to edit the run_raydium.yml file included below to be compatible with the checkpoint system introduced in collect_solar_data.py, while also adding in manual trigger to run Github Actions:
name: Collect and Visualize Solar Data
on:
push:
branches:
- main
schedule:
- cron: '0 0 * * *' # Every day at midnight UTC
To update your GitHub Actions workflow (run_raydium.yml) to be compatible with the checkpoint system introduced in collect_solar_data.py and to add a manual trigger, follow these steps:
Add Manual Trigger: Incorporate a workflow_dispatch event to allow manual execution of the workflow.
Persist Checkpoints: Ensure that the checkpoints/ directory is tracked and committed after each run. This allows the workflow to resume from where it left off in case of interruptions.
Commit Checkpoint Files: Modify the commit steps to include the checkpoints/ directory alongside your other data files.
Below is the updated run_raydium.yml with the necessary modifications:
name: Collect and Visualize Solar Data
on:
push:
branches:
- main
schedule:
- cron: '0 0 * * *' # Every day at midnight UTC
workflow_dispatch: # Enables manual trigger
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
with:
persist-credentials: true
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.10'
- name: Install system dependencies
run: |
sudo add-apt-repository ppa:ubuntugis/ppa -y
sudo apt-get update
sudo apt-get install -y gdal-bin libgdal-dev python3-gdal
- name: Set environment variables
run: |
echo "GDAL_VERSION=$(gdal-config --version)" >> $GITHUB_ENV
echo "CPLUS_INCLUDE_PATH=/usr/include/gdal" >> $GITHUB_ENV
echo "C_INCLUDE_PATH=/usr/include/gdal" >> $GITHUB_ENV
- name: Install Python dependencies
run: |
python -m pip install --upgrade pip wheel setuptools
# Install GDAL first
pip install GDAL==$(gdal-config --version)
# Install Fiona explicitly
pip install fiona==1.9.3
# Install the rest of the requirements
pip install -r requirements.txt
# Add debug step
- name: Debug Python environment
run: |
python -c "import fiona; print('Fiona version:', fiona.__version__)"
python -c "import geopandas; print('GeoPandas version:', geopandas.__version__)"
python -c "import os; print('GeoJSON exists:', os.path.exists('india-soi.geojson'))"
ls -la *.geojson
- name: Run collect_solar_data.py
run: |
python collect_solar_data.py
- name: Commit solar data and checkpoints
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
# Add solar data and all checkpoint files
git add india_solar_data.csv checkpoints/
# Commit if there are changes
git diff --quiet && git diff --staged --quiet || git commit -m "Update solar data and checkpoints [skip ci]"
# Push changes to the repository
git push
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Run visualize_solar_data.py
run: |
python visualize_solar_data.py
- name: Commit visualization files
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
# Add visualization outputs
git add solar_potential_high_res.png india_solar_potential.html
# Commit if there are changes
git diff --quiet && git diff --staged --quiet || git commit -m "Update solar visualization [skip ci]"
# Push changes to the repository
git push
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Detailed Breakdown of Changes
Manual Trigger (workflow_dispatch):
on:
push:
branches:
- main
schedule:
- cron: '0 0 * * *' # Every day at midnight UTC
workflow_dispatch: # Enables manual trigger
This addition allows you to manually trigger the workflow from the GitHub Actions tab in your repository.
Commit Checkpoint Files:
- name: Commit solar data and checkpoints
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
# Add solar data and all checkpoint files
git add india_solar_data.csv checkpoints/
# Commit if there are changes
git diff --quiet && git diff --staged --quiet || git commit -m "Update solar data and checkpoints [skip ci]"
# Push changes to the repository
git push
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
git add india_solar_data.csv checkpoints/: This command adds both the india_solar_data.csv file and the entire checkpoints/ directory to the staging area. Ensuring that all checkpoint files are included allows the workflow to resume from the last checkpoint in subsequent runs.
Commit Logic: The commit only occurs if there are changes to prevent unnecessary commits.
[skip ci]: This tag in the commit message prevents triggering other workflows that might be set to run on push events, avoiding potential infinite loops.
Ensuring checkpoints/ is Tracked:
Make sure that the checkpoints/ directory is not listed in your .gitignore file. This ensures that Git tracks the checkpoint files and includes them in commits.
Optional Enhancements:
Handling Large Checkpoints: If your checkpoints/ directory grows large, consider using Git LFS to manage large files.
Branch Specific Checkpoints: If multiple branches are used, ensure that checkpoints are managed per branch to avoid conflicts.
Testing the Workflow:
Additional Recommendations
Repository Size Management: Regularly monitor the size of your repository, especially if the checkpoints/ directory contains large files. Implement strategies to manage and clean up old checkpoints if necessary.
Security Considerations: Ensure that no sensitive data is inadvertently stored in the checkpoints/ directory, as these files are part of the repository and can be accessed by anyone with repository access.
Error Handling: Enhance error handling in your scripts to manage scenarios where checkpoint files might be corrupted or out of sync.
By implementing these changes, your GitHub Actions workflow will be able to efficiently handle the checkpoint system, ensuring robustness and the ability to resume data collection seamlessly. Additionally, the manual trigger provides flexibility for on-demand executions.