Skip to content
TheChatArchiveThe Chat Archive
o1-mini-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

import asyncio import aiohttp import numpy as np import pandas as pd import geopandas as…

  1. User

    import asyncio import aiohttp import numpy as np import pandas as pd import geopandas as gpd import folium from shapely.geometry import Point, box import time import branca.colormap as cm import matplotlib.pyplot as plt from matplotlib.colors import ListedColormap import logging import diskcache as dc from tqdm.asyncio import tqdm_asyncio import json import rasterio from rasterio.transform import from_bounds from rasterio.features import geometry_mask from rasterio.warp import calculate_default_transform, reproject, Resampling import pyproj from affine import Affine

    Logging setup

    logging.basicConfig( level=logging.INFO, format='%(asctime)s [%(levelname)s] %(message)s', handlers=[ logging.FileHandler("solar_map.log"), logging.StreamHandler() ] ) logger = logging.getLogger(name)

    class RateLimiter: def init(self, max_calls, period): self.max_calls = max_calls self.period = period self.calls = [] self.lock = asyncio.Lock()

    async def acquire(self):
        async with self.lock:
            current = time.time()
            while self.calls and self.calls[0] <= current - self.period:
                self.calls.pop(0)
            if len(self.calls) >= self.max_calls:
                sleep_time = self.period - (current - self.calls[0]) + 0.1
                logger.info(f"Rate limit reached. Sleeping for {sleep_time:.2f} seconds.")
                await asyncio.sleep(sleep_time)
            self.calls.append(time.time())
    

    rate_limiter = RateLimiter(max_calls=1, period=2) # 1 call every 2 seconds

    async def fetch_and_calculate(session, latitude, longitude, cache): """ Fetch solar data from NASA POWER API for the given latitude and longitude. Calculate solar potential and return the result.

    Args:
        session (aiohttp.ClientSession): The HTTP session for making requests.
        latitude (float): Latitude of the point.
        longitude (float): Longitude of the point.
        cache (diskcache.Cache): Cache for storing and retrieving fetched data.
    
    Returns:
        dict: Dictionary containing solar potential and coordinates, or None if failed.
    """
    # Define the cache key based on coordinates
    cache_key = f"solar_data_{latitude}_{longitude}"
    
    # Check if data is already cached
    if cache_key in cache:
        logger.info(f"Cache hit for coordinates: ({latitude}, {longitude})")
        solar_potential = cache[cache_key]
    else:
        logger.info(f"Fetching data for coordinates: ({latitude}, {longitude})")
        # NASA POWER API endpoint for solar radiation
        api_url = "https://power.larc.nasa.gov/api/temporal/daily/point"
    
        # Define API parameters
        params = {
            "parameters": "ALLSKY_SFC_SW_DWN",  # All-sky surface shortwave downwelling radiation
            "community": "SB",
            "longitude": longitude,
            "latitude": latitude,
            "start": "20200101",
            "end": "20201231",
            "format": "JSON"
        }
    
        try:
            await rate_limiter.acquire()  # Ensure rate limiting
            async with session.get(api_url, params=params) as response:
                if response.status != 200:
                    logger.error(f"Failed to fetch data for ({latitude}, {longitude}). Status: {response.status}")
                    return None
                data = await response.json()
    
                # Extract the solar radiation data
                solar_radiation = data.get("properties", {}).get("parameter", {}).get("ALLSKY_SFC_SW_DWN", {})
                if not solar_radiation:
                    logger.error(f"No solar radiation data found for ({latitude}, {longitude})")
                    return None
    
                # Calculate the average solar potential (in kWh/m²/year)
                total_radiation = sum(solar_radiation.values())
                average_radiation = total_radiation / len(solar_radiation) * 0.0036  # Convert from MJ/m²/day to kWh/m²/year
                solar_potential = round(average_radiation, 2)
    
                # Store in cache
                cache[cache_key] = solar_potential
        except Exception as e:
            logger.error(f"Exception while fetching data for ({latitude}, {longitude}): {e}")
            return None
    
    return {
        "latitude": latitude,
        "longitude": longitude,
        "potential": solar_potential
    }
    

    async def create_india_solar_map_async(geojson_path='india-soi.geojson'): logger.info(f"Reading GeoJSON file from: {geojson_path}")

    # Read and reproject the GeoJSON to a suitable projection for India
    india = gpd.read_file(geojson_path)
    
    # Convert to Asia South Albers Equal Area Conic projection
    india_proj = india.to_crs('EPSG:24370')  # This projection is suitable for India
    bounds = india_proj.total_bounds
    
    # Calculate grid parameters
    resolution = 5000  # 5km resolution
    width = int((bounds[2] - bounds[0]) / resolution)
    height = int((bounds[3] - bounds[1]) / resolution)
    
    # Create the transform matrix for the raster
    transform = from_bounds(bounds[0], bounds[1], bounds[2], bounds[3], width, height)
    
    # Create grid points in the projected CRS
    x_coords = np.linspace(bounds[0], bounds[2], width)
    y_coords = np.linspace(bounds[1], bounds[3], height)
    xx, yy = np.meshgrid(x_coords, y_coords)
    
    # Create point geometries for each grid cell
    points = [Point(x, y) for x, y in zip(xx.flatten(), yy.flatten())]
    grid_gdf = gpd.GeoDataFrame(geometry=points, crs=india_proj.crs)
    
    # Create mask for points within India
    mask = grid_gdf.within(india_proj.unary_union)
    valid_points = grid_gdf[mask]
    
    # Convert points back to WGS84 for API calls
    valid_points_wgs84 = valid_points.to_crs('EPSG:4326')
    
    # Setup cache and session
    cache = dc.Cache('nasa_power_cache')
    connector = aiohttp.TCPConnector(limit=10)
    timeout = aiohttp.ClientTimeout(total=60)
    
    logger.info("Starting data collection with asynchronous requests...")
    solar_data = []
    
    async with aiohttp.ClientSession(connector=connector, timeout=timeout) as session:
        tasks = [
            fetch_and_calculate(session, point.y, point.x, cache)
            for point in valid_points_wgs84.geometry
        ]
        
        for future in tqdm_asyncio.as_completed(tasks, total=len(tasks), desc="Processing Points"):
            result = await future
            if result:
                solar_data.append(result)
    
    cache.close()
    
    # Create DataFrame and convert to GeoDataFrame
    solar_df = pd.DataFrame(solar_data)
    solar_gdf = gpd.GeoDataFrame(
        solar_df,
        geometry=[Point(xy) for xy in zip(solar_df.longitude, solar_df.latitude)],
        crs='EPSG:4326'
    )
    
    # Project to the same CRS as the boundary
    solar_gdf_proj = solar_gdf.to_crs(india_proj.crs)
    
    # Create empty raster
    raster_data = np.zeros((height, width), dtype=np.float32)
    raster_data[:] = np.nan
    
    # Fill raster with solar potential values
    for idx, row in solar_gdf_proj.iterrows():
        # Get raster indices for point
        col_float, row_float = ~transform * (row.geometry.x, row.geometry.y)
        col, row_idx = int(col_float), int(row_float)
        if 0 <= row_idx < height and 0 <= col < width:
            raster_data[row_idx, col] = row['potential']
    
    # Create mask from India boundary
    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)
    
    # Create visualization
    fig, ax = plt.subplots(figsize=(20, 24))
    ax.set_axis_off()
    
    # Create colormap
    colormap = cm.LinearColormap(
        ['#440154', '#482878', '#3E4989', '#31688E', '#26828E',
         '#1F9E89', '#35B779', '#6DCD59', '#B4DE2C', '#FDE725'],
        vmin=np.nanmin(raster_data),
        vmax=np.nanmax(raster_data),
        caption='Solar Potential (kWh/m²/year)'
    )
    
    # Plot the raster
    img = ax.imshow(
        raster_data,
        extent=[bounds[0], bounds[2], bounds[1], bounds[3]],
        cmap=ListedColormap(colormap.colors),
        origin='lower'
    )
    
    # Plot boundary
    india_proj.boundary.plot(ax=ax, color='black', linewidth=1)
    
    # Save high-resolution image
    image_path = 'solar_potential_high_res.png'
    plt.savefig(image_path, bbox_inches='tight', pad_inches=0, dpi=300)
    plt.close(fig)
    
    # Create Folium map
    center_lat = (india.total_bounds[1] + india.total_bounds[3]) / 2
    center_lon = (india.total_bounds[0] + india.total_bounds[2]) / 2
    m = folium.Map(location=[center_lat, center_lon], zoom_start=5, tiles='CartoDB positron')
    
    # Add the raster overlay
    img_bounds = [[india.total_bounds[1], india.total_bounds[0]], 
                  [india.total_bounds[3], india.total_bounds[2]]]
    
    folium.raster_layers.ImageOverlay(
        name='Solar Potential',
        image=image_path,
        bounds=img_bounds,
        opacity=0.7,
        interactive=True,
        cross_origin=False,
        zindex=1,
    ).add_to(m)
    
    # Add boundary overlay
    folium.GeoJson(
        india,
        style_function=lambda x: {
            'fillColor': 'none',
            'color': 'black',
            'weight': 1.5,
            'fillOpacity': 0
        },
        name='India Boundary'
    ).add_to(m)
    
    # Add colormap and layer control
    colormap.add_to(m)
    folium.LayerControl().add_to(m)
    
    # Save outputs
    output_csv = 'india_solar_data.csv'
    output_html = 'india_solar_potential.html'
    solar_df.to_csv(output_csv, index=False)
    m.save(output_html)
    
    return m, solar_df
    

    async def main_async(): try: logger.info("Starting solar potential map generation with improved alignment...") solar_map, solar_data = await create_india_solar_map_async() logger.info("Process completed successfully!") logger.info("Map has been saved as 'india_solar_potential.html'") logger.info("Raw data has been saved as 'india_solar_data.csv'") except Exception as e: logger.error(f"An error occurred: {e}")

    def main(): asyncio.run(main_async())

    if name == "main": main()

    The above is my file raydium.py

    This is my workflow file run_raydium.py: name: Run Raydium Solar Map Generation on: workflow_dispatch:

    schedule:

    - cron: '0 0 * * *'

    jobs: build: runs-on: ubuntu-latest steps: - name: Checkout Repository uses: actions/checkout@v3

      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: '3.10'
    
      # Install GDAL dependencies first
      - name: Install GDAL dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y gdal-bin libgdal-dev python3-gdal
    
      # Get GDAL version and set environment variables
      - name: Set GDAL environment variables
        run: |
          export GDAL_VERSION=$(gdal-config --version)
          export CPLUS_INCLUDE_PATH=/usr/include/gdal
          export C_INCLUDE_PATH=/usr/include/gdal
          echo "GDAL_VERSION=$GDAL_VERSION" >> $GITHUB_ENV
          echo "CPLUS_INCLUDE_PATH=$CPLUS_INCLUDE_PATH" >> $GITHUB_ENV
          echo "C_INCLUDE_PATH=$C_INCLUDE_PATH" >> $GITHUB_ENV
    
      - name: Cache pip dependencies
        uses: actions/cache@v3
        with:
          path: ~/.cache/pip
          key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
          restore-keys: |
            ${{ runner.os }}-pip-
    
      - name: Cache NASA POWER data
        uses: actions/cache@v3
        with:
          path: nasa_power_cache
          key: ${{ runner.os }}-nasa_power_cache-${{ hashFiles('**/raydium.py') }}
          restore-keys: |
            ${{ runner.os }}-nasa_power_cache-
    
      # Install dependencies in the correct order
      - name: Install Dependencies
        run: |
          python -m pip install --upgrade pip
          pip install wheel setuptools
          pip install GDAL==$(gdal-config --version)
          pip install fiona==1.9.4
          pip install -r requirements.txt
    
      - name: Verify 'india-soi.geojson' Presence
        run: |
          if [ -f india-soi.geojson ]; then
            echo "'india-soi.geojson' found."
          else
            echo "'india-soi.geojson' not found. Please ensure it exists in the repository root."
            exit 1
          fi
    
      - name: List Files Before Script Execution
        run: |
          echo "Listing files before running the script:"
          ls -la
    
      - name: Run Python Script
        run: |
          echo "Executing raydium.py..."
          python raydium.py
    
      - name: List Files After Script Execution
        run: |
          echo "Listing files after running the script:"
          ls -la
    
      - name: Display log file
        if: always()  # Changed to always() to see logs even if script fails
        run: |
          if [ -f "solar_map.log" ]; then
            echo "Displaying solar_map.log contents:"
            cat solar_map.log
          else
            echo "solar_map.log does not exist."
          fi
    
      - name: Configure Git
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
    
      - name: Add Output Files
        run: |
          git add india_solar_potential.html india_solar_data.csv solar_potential_high_res.png solar_map.log || echo "No output files to add."
    
      - name: Commit Changes
        run: |
          git commit -m "Update solar map outputs via GitHub Actions" || echo "No changes to commit"
    
      - name: Push Changes
        uses: ad-m/github-push-action@v0.6.0
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          branch: ${{ github.ref }}
    

    And for some reason Github Actions is stuck on to Executing raydium.py... 2024-11-08 05:55:57,523 [INFO] Starting solar potential map generation with improved alignment... 2024-11-08 05:55:57,523 [INFO] Reading GeoJSON file from: india-soi.geojson

    For the past 4 hours. I think it will timeout because it has not even started to get the data yet, much less visualise it. Can you help?

  2. Assistant

    It appears that your GitHub Actions workflow is getting stuck when executing raydium.py, specifically after reading the GeoJSON file. This issue can be caused by several factors, such as processing an excessively large number of grid points, resource exhaustion, or blocking operations within your asynchronous tasks. Below are detailed steps to diagnose and resolve the problem:


    1. Diagnose the Root Cause

    a. Excessive Grid Points

    Your script creates a grid with a 5 km resolution over India. Given India's approximate area of 3.287 million km², this results in millions of grid points. For example:

    • Grid Dimensions:

      • Width: ( \frac{\text{Longitude Range}}{5000} )
      • Height: ( \frac{\text{Latitude Range}}{5000} )
      • Total Points: Width × Height ≈ Millions
    • Implications:

      • Memory Consumption: Creating millions of asynchronous tasks can exhaust the memory of the GitHub Actions runner.
      • API Rate Limiting: With a rate limiter set to 1 call every 2 seconds, fetching data for millions of points would take an impractical amount of time (e.g., ~2400 hours for 4.3 million points).

    b. Blocking Operations

    Ensure that there are no blocking operations within your asynchronous functions that might prevent the event loop from progressing.


    2. Implement Solutions

    a. Reduce Grid Resolution

    Increase the grid resolution to reduce the number of grid points.

    • Change:

      resolution = 10000  # Changed from 5000 to 10000 for 10 km resolution
      
    • Impact: Reduces the number of grid points by a factor of 4, making the task more manageable.

    b. Limit the Processing Area

    If feasible, focus on specific regions within India rather than the entire country to further reduce the number of grid points.

    c. Implement Batching

    Process grid points in batches to prevent overwhelming the system and manage memory usage efficiently.

    • Example Implementation:
      BATCH_SIZE = 1000  # Define an appropriate batch size
      
      for i in range(0, len(tasks), BATCH_SIZE):
          batch = tasks[i:i+BATCH_SIZE]
          results = await asyncio.gather(*batch)
          solar_data.extend([res for res in results if res])
      

    d. Optimize Asynchronous Requests

    Adjust the concurrency and manage task creation more effectively.

    • Use asyncio.Semaphore:

      semaphore = asyncio.Semaphore(100)  # Limit to 100 concurrent tasks
      
      async def fetch_with_semaphore(session, point, cache):
          async with semaphore:
              return await fetch_and_calculate(session, point.y, point.x, cache)
      
      tasks = [
          fetch_with_semaphore(session, point, cache)
          for point in valid_points_wgs84.geometry
      ]
      
    • Batch Processing with tqdm_asyncio:

      for future in tqdm_asyncio.as_completed(tasks, total=len(tasks), desc="Processing Points"):
          result = await future
          if result:
              solar_data.append(result)
      

    e. Enhance Logging for Better Monitoring

    Add detailed logs to monitor the progress and identify where the script might be stalling.

    • Example Logs:
      logger.info(f"GeoJSON read successfully. Projected bounds: {bounds}")
      logger.info(f"Total grid points created: {len(grid_gdf)}")
      logger.info(f"Valid points within India: {len(valid_points)}")
      logger.info(f"Converted points to WGS84.")
      logger.info(f"Number of tasks to create: {len(tasks)}")
      

    f. Implement Timeouts and Error Handling

    Ensure that your asynchronous requests have appropriate timeouts and handle exceptions gracefully to prevent hangs.

    • Modify fetch_and_calculate:
      async def fetch_and_calculate(session, latitude, longitude, cache):
          # ... [existing code]
      
          try:
              await rate_limiter.acquire()  # Ensure rate limiting
              async with session.get(api_url, params=params, timeout=10) as response:
                  if response.status != 200:
                      logger.error(f"Failed to fetch data for ({latitude}, {longitude}). Status: {response.status}")
                      return None
                  data = await response.json()
                  # ... [rest of the processing]
          except asyncio.TimeoutError:
              logger.error(f"Timeout fetching data for ({latitude}, {longitude})")
              return None
          except Exception as e:
              logger.error(f"Exception while fetching data for ({latitude}, {longitude}): {e}")
              return None
          ```
      

    g. Verify Cache Functionality

    Ensure that the caching mechanism with diskcache is functioning correctly to prevent redundant API calls, which can save time and resources.

    • Check Cache Restoration in GitHub Actions: Ensure that the nasa_power_cache is being correctly cached and restored within your GitHub Actions workflow.

    3. Update GitHub Actions Workflow

    a. Adjust Workflow to Handle Reduced Load

    Ensure that your workflow can handle the reduced number of grid points effectively.

    • Example Adjustments:
      • Increase Timeout: If necessary, adjust the job timeout in the workflow (default is 6 hours).
      • Monitor Logs: Use enhanced logging to monitor the progress within GitHub Actions.

    b. Example Workflow with Adjustments

    name: Run Raydium Solar Map Generation
    on:
      workflow_dispatch:
    
    jobs:
      build:
        runs-on: ubuntu-latest
        steps:
          - name: Checkout Repository
            uses: actions/checkout@v3
    
          - name: Set up Python
            uses: actions/setup-python@v4
            with:
              python-version: '3.10'
    
          # Install GDAL dependencies first
          - name: Install GDAL dependencies
            run: |
              sudo apt-get update
              sudo apt-get install -y gdal-bin libgdal-dev python3-gdal
    
          # Get GDAL version and set environment variables
          - name: Set GDAL environment variables
            run: |
              export GDAL_VERSION=$(gdal-config --version)
              export CPLUS_INCLUDE_PATH=/usr/include/gdal
              export C_INCLUDE_PATH=/usr/include/gdal
              echo "GDAL_VERSION=$GDAL_VERSION" >> $GITHUB_ENV
              echo "CPLUS_INCLUDE_PATH=$CPLUS_INCLUDE_PATH" >> $GITHUB_ENV
              echo "C_INCLUDE_PATH=$C_INCLUDE_PATH" >> $GITHUB_ENV
    
          - name: Cache pip dependencies
            uses: actions/cache@v3
            with:
              path: ~/.cache/pip
              key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
              restore-keys: |
                ${{ runner.os }}-pip-
    
          - name: Cache NASA POWER data
            uses: actions/cache@v3
            with:
              path: nasa_power_cache
              key: ${{ runner.os }}-nasa_power_cache-${{ hashFiles('**/raydium.py') }}
              restore-keys: |
                ${{ runner.os }}-nasa_power_cache-
    
          # Install dependencies in the correct order
          - name: Install Dependencies
            run: |
              python -m pip install --upgrade pip
              pip install wheel setuptools
              pip install GDAL==$(gdal-config --version)
              pip install fiona==1.9.4
              pip install -r requirements.txt
    
          - name: Verify 'india-soi.geojson' Presence
            run: |
              if [ -f india-soi.geojson ]; then
                echo "'india-soi.geojson' found."
              else
                echo "'india-soi.geojson' not found. Please ensure it exists in the repository root."
                exit 1
              fi
    
          - name: List Files Before Script Execution
            run: |
              echo "Listing files before running the script:"
              ls -la
    
          - name: Run Python Script
            run: |
              echo "Executing raydium.py..."
              python raydium.py
    
          - name: List Files After Script Execution
            run: |
              echo "Listing files after running the script:"
              ls -la
    
          - name: Display log file
            if: always()  # Changed to always() to see logs even if script fails
            run: |
              if [ -f "solar_map.log" ]; then
                echo "Displaying solar_map.log contents:"
                cat solar_map.log
              else
                echo "solar_map.log does not exist."
              fi
    
          - name: Configure Git
            run: |
              git config user.name "github-actions[bot]"
              git config user.email "github-actions[bot]@users.noreply.github.com"
    
          - name: Add Output Files
            run: |
              git add india_solar_potential.html india_solar_data.csv solar_potential_high_res.png solar_map.log || echo "No output files to add."
    
          - name: Commit Changes
            run: |
              git commit -m "Update solar map outputs via GitHub Actions" || echo "No changes to commit"
    
          - name: Push Changes
            uses: ad-m/github-push-action@v0.6.0
            with:
              github_token: ${{ secrets.GITHUB_TOKEN }}
              branch: ${{ github.ref }}
    

    4. Example Script with Optimizations

    Below is a refined version of your raydium.py script incorporating the suggested optimizations:

    import asyncio
    import aiohttp
    import numpy as np
    import pandas as pd
    import geopandas as gpd
    import folium
    from shapely.geometry import Point
    import time
    import branca.colormap as cm
    import matplotlib.pyplot as plt
    from matplotlib.colors import ListedColormap
    import logging
    import diskcache as dc
    from tqdm.asyncio import tqdm_asyncio
    import json
    import rasterio
    from rasterio.transform import from_bounds
    from rasterio.features import geometry_mask
    from rasterio.warp import calculate_default_transform, reproject, Resampling
    import pyproj
    from affine import Affine
    
    # Logging setup
    logging.basicConfig(
        level=logging.INFO,
        format='%(asctime)s [%(levelname)s] %(message)s',
        handlers=[
            logging.FileHandler("solar_map.log"),
            logging.StreamHandler()
        ]
    )
    logger = logging.getLogger(__name__)
    
    class RateLimiter:
        def __init__(self, max_calls, period):
            self.max_calls = max_calls
            self.period = period
            self.calls = []
            self.lock = asyncio.Lock()
    
        async def acquire(self):
            async with self.lock:
                current = time.time()
                while self.calls and self.calls[0] <= current - self.period:
                    self.calls.pop(0)
                if len(self.calls) >= self.max_calls:
                    sleep_time = self.period - (current - self.calls[0]) + 0.1
                    logger.info(f"Rate limit reached. Sleeping for {sleep_time:.2f} seconds.")
                    await asyncio.sleep(sleep_time)
                self.calls.append(time.time())
    
    rate_limiter = RateLimiter(max_calls=1, period=2)  # 1 call every 2 seconds
    
    async def fetch_and_calculate(session, latitude, longitude, cache):
        """
        Fetch solar data from NASA POWER API for the given latitude and longitude.
        Calculate solar potential and return the result.
    
        Args:
            session (aiohttp.ClientSession): The HTTP session for making requests.
            latitude (float): Latitude of the point.
            longitude (float): Longitude of the point.
            cache (diskcache.Cache): Cache for storing and retrieving fetched data.
    
        Returns:
            dict: Dictionary containing solar potential and coordinates, or None if failed.
        """
        cache_key = f"solar_data_{latitude}_{longitude}"
        if cache_key in cache:
            logger.info(f"Cache hit for coordinates: ({latitude}, {longitude})")
            solar_potential = cache[cache_key]
        else:
            logger.info(f"Fetching data for coordinates: ({latitude}, {longitude})")
            api_url = "https://power.larc.nasa.gov/api/temporal/daily/point"
            params = {
                "parameters": "ALLSKY_SFC_SW_DWN",
                "community": "SB",
                "longitude": longitude,
                "latitude": latitude,
                "start": "20200101",
                "end": "20201231",
                "format": "JSON"
            }
            try:
                await rate_limiter.acquire()
                async with session.get(api_url, params=params, timeout=10) as response:
                    if response.status != 200:
                        logger.error(f"Failed to fetch data for ({latitude}, {longitude}). Status: {response.status}")
                        return None
                    data = await response.json()
                    solar_radiation = data.get("properties", {}).get("parameter", {}).get("ALLSKY_SFC_SW_DWN", {})
                    if not solar_radiation:
                        logger.error(f"No solar radiation data found for ({latitude}, {longitude})")
                        return None
                    total_radiation = sum(solar_radiation.values())
                    average_radiation = total_radiation / len(solar_radiation) * 0.0036  # Convert from MJ/m²/day to kWh/m²/year
                    solar_potential = round(average_radiation, 2)
                    cache[cache_key] = solar_potential
            except asyncio.TimeoutError:
                logger.error(f"Timeout fetching data for ({latitude}, {longitude})")
                return None
            except Exception as e:
                logger.error(f"Exception while fetching data for ({latitude}, {longitude}): {e}")
                return None
    
        return {
            "latitude": latitude,
            "longitude": longitude,
            "potential": solar_potential
        }
    
    async def create_india_solar_map_async(geojson_path='india-soi.geojson'):
        logger.info(f"Reading GeoJSON file from: {geojson_path}")
        india = gpd.read_file(geojson_path)
        india_proj = india.to_crs('EPSG:24370')  # Asia South Albers Equal Area Conic
        bounds = india_proj.total_bounds
        logger.info(f"Projected bounds: {bounds}")
    
        resolution = 10000  # 10 km resolution
        width = int((bounds[2] - bounds[0]) / resolution)
        height = int((bounds[3] - bounds[1]) / resolution)
        logger.info(f"Grid resolution set to {resolution} meters.")
        logger.info(f"Grid dimensions: width={width}, height={height}")
    
        transform = from_bounds(bounds[0], bounds[1], bounds[2], bounds[3], width, height)
        x_coords = np.linspace(bounds[0], bounds[2], width)
        y_coords = np.linspace(bounds[1], bounds[3], height)
        xx, yy = np.meshgrid(x_coords, y_coords)
        points = [Point(x, y) for x, y in zip(xx.flatten(), yy.flatten())]
        grid_gdf = gpd.GeoDataFrame(geometry=points, crs=india_proj.crs)
        logger.info("Grid points created.")
    
        mask = grid_gdf.within(india_proj.unary_union)
        valid_points = grid_gdf[mask]
        logger.info(f"Valid points within India: {len(valid_points)}")
    
        valid_points_wgs84 = valid_points.to_crs('EPSG:4326')
        logger.info("Converted points to WGS84.")
    
        cache = dc.Cache('nasa_power_cache')
        connector = aiohttp.TCPConnector(limit=100)  # Adjusted for higher concurrency
        timeout = aiohttp.ClientTimeout(total=60)
        logger.info("Starting data collection with asynchronous requests...")
        solar_data = []
    
        semaphore = asyncio.Semaphore(100)  # Limit to 100 concurrent tasks
    
        async def fetch_with_semaphore(session, point):
            async with semaphore:
                return await fetch_and_calculate(session, point.y, point.x, cache)
    
        async with aiohttp.ClientSession(connector=connector, timeout=timeout) as session:
            tasks = [
                fetch_with_semaphore(session, point)
                for point in valid_points_wgs84.geometry
            ]
            for idx, future in enumerate(tqdm_asyncio.as_completed(tasks, total=len(tasks), desc="Processing Points"), 1):
                result = await future
                if result:
                    solar_data.append(result)
                if idx % 1000 == 0:
                    logger.info(f"Processed {idx} points.")
    
        cache.close()
        logger.info("Data collection completed.")
    
        # Create DataFrame and convert to GeoDataFrame
        solar_df = pd.DataFrame(solar_data)
        solar_gdf = gpd.GeoDataFrame(
            solar_df,
            geometry=[Point(xy) for xy in zip(solar_df.longitude, solar_df.latitude)],
            crs='EPSG:4326'
        )
        logger.info("Solar data converted to GeoDataFrame.")
    
        # Project to the same CRS as the boundary
        solar_gdf_proj = solar_gdf.to_crs(india_proj.crs)
        logger.info("Solar GeoDataFrame projected to CRS.")
    
        # Create empty raster
        raster_data = np.zeros((height, width), dtype=np.float32)
        raster_data[:] = np.nan
    
        # Fill raster with solar potential values
        for idx, row in solar_gdf_proj.iterrows():
            col_float, row_float = ~transform * (row.geometry.x, row.geometry.y)
            col, row_idx = int(col_float), int(row_float)
            if 0 <= row_idx < height and 0 <= col < width:
                raster_data[row_idx, col] = row['potential']
            if idx % 100000 == 0 and idx > 0:
                logger.info(f"Filled raster data up to index {idx}.")
    
        logger.info("Raster data filled with solar potentials.")
    
        # Create mask from India boundary
        geometry = [feature['geometry'] for feature in india_proj.__geo_interface__['features']]
        mask_raster = geometry_mask(geometry, out_shape=(height, width), transform=transform, invert=True)
        raster_data = np.ma.masked_array(raster_data, ~mask_raster)
        logger.info("Applied boundary mask to raster data.")
    
        # Create visualization
        fig, ax = plt.subplots(figsize=(20, 24))
        ax.set_axis_off()
    
        # Create colormap
        colormap = cm.LinearColormap(
            ['#440154', '#482878', '#3E4989', '#31688E', '#26828E',
             '#1F9E89', '#35B779', '#6DCD59', '#B4DE2C', '#FDE725'],
            vmin=np.nanmin(raster_data),
            vmax=np.nanmax(raster_data),
            caption='Solar Potential (kWh/m²/year)'
        )
    
        # Plot the raster
        img = ax.imshow(
            raster_data,
            extent=[bounds[0], bounds[2], bounds[1], bounds[3]],
            cmap=ListedColormap(colormap.colors),
            origin='lower'
        )
    
        # Plot boundary
        india_proj.boundary.plot(ax=ax, color='black', linewidth=1)
        logger.info("Plotted raster and boundary.")
    
        # Save high-resolution image
        image_path = 'solar_potential_high_res.png'
        plt.savefig(image_path, bbox_inches='tight', pad_inches=0, dpi=300)
        plt.close(fig)
        logger.info(f"Saved high-resolution image to {image_path}.")
    
        # Create Folium map
        center_lat = (india.total_bounds[1] + india.total_bounds[3]) / 2
        center_lon = (india.total_bounds[0] + india.total_bounds[2]) / 2
        m = folium.Map(location=[center_lat, center_lon], zoom_start=5, tiles='CartoDB positron')
    
        # Add the raster overlay
        img_bounds = [[india.total_bounds[1], india.total_bounds[0]], 
                      [india.total_bounds[3], india.total_bounds[2]]]
    
        folium.raster_layers.ImageOverlay(
            name='Solar Potential',
            image=image_path,
            bounds=img_bounds,
            opacity=0.7,
            interactive=True,
            cross_origin=False,
            zindex=1,
        ).add_to(m)
    
        # Add boundary overlay
        folium.GeoJson(
            india,
            style_function=lambda x: {
                'fillColor': 'none',
                'color': 'black',
                'weight': 1.5,
                'fillOpacity': 0
            },
            name='India Boundary'
        ).add_to(m)
    
        # Add colormap and layer control
        colormap.add_to(m)
        folium.LayerControl().add_to(m)
    
        # Save outputs
        output_csv = 'india_solar_data.csv'
        output_html = 'india_solar_potential.html'
        solar_df.to_csv(output_csv, index=False)
        m.save(output_html)
        logger.info(f"Saved solar data to {output_csv} and map to {output_html}.")
    
        return m, solar_df
    
    async def main_async():
        try:
            logger.info("Starting solar potential map generation with improved alignment...")
            solar_map, solar_data = await create_india_solar_map_async()
            logger.info("Process completed successfully!")
            logger.info("Map has been saved as 'india_solar_potential.html'")
            logger.info("Raw data has been saved as 'india_solar_data.csv'")
        except Exception as e:
            logger.error(f"An error occurred: {e}")
    
    def main():
        asyncio.run(main_async())
    
    if __name__ == "__main__":
        main()
    

    5. Verify and Test

    a. Local Testing

    Before pushing changes to GitHub, run the script locally with the updated grid resolution and ensure it executes successfully within a reasonable timeframe.

    b. Incremental Workflow Testing

    1. Commit and Push: After making changes, push them to your repository.
    2. Trigger GitHub Actions: Manually trigger the workflow using the workflow_dispatch event.
    3. Monitor Logs: Observe the GitHub Actions logs to ensure the script progresses beyond reading the GeoJSON.
    4. Adjust as Needed: If issues persist, further adjust the grid resolution or batching parameters.

    c. Gradual Scale-Up

    Once the script runs successfully with a reduced grid, gradually increase the resolution to cover more points, ensuring the workflow remains stable.


    6. Additional Recommendations

    a. Utilize Caching Effectively

    Ensure that the cache is correctly restored and utilized within GitHub Actions to avoid redundant API calls, which can save significant time.

    b. Optimize Data Storage

    Consider compressing output files or storing them in a more efficient format if they become large.

    c. Parallelize Independent Tasks

    While maintaining controlled concurrency, explore opportunities to parallelize tasks that don't interfere with each other to enhance performance.


    By implementing the above strategies, you should be able to resolve the issue of the GitHub Actions workflow getting stuck and successfully generate the solar potential map for India. Adjusting the grid resolution and managing asynchronous tasks effectively will be key to optimizing performance and ensuring the workflow completes within the allocated time.