# Batch Report Generation Guide

## Overview

The batch generation system automates lightning report generation for multiple wind farms by fetching data from the API and processing them in batch.

## Setup

### 1. Install Dependencies

```bash
pip install -r requirements.txt
```

### 2. Create .env File

Create a `.env` file in the project root with your API key:

```env
API_KEY=your_api_key_here
```

### 3. Create Configuration File

Copy `wind_farms_config.example.json` to `wind_farms_config.json` and configure your wind farms:

```bash
cp wind_farms_config.example.json wind_farms_config.json
```

Edit `wind_farms_config.json` with your farms' details.

## Configuration

### API Configuration

```json
{
  "api_config": {
    "base_url": "https://risk.tarla.io/api",
    "timeout_seconds": 30,
    "retry_attempts": 3,
    "default_query_range": {
      "method": "current_month"
    }
  }
}
```

### Wind Farm Configuration

Each farm can have:

- **enabled**: `true` or `false` - Controls whether to generate report
- **location_bounds.method**: `"auto"` (calculate from turbines) or `"manual"` (specify)
- **location_bounds.padding_km**: Extra buffer beyond max distance ring (default: 5km)
- **date_range.method**: `"auto"` (use query_range to fetch, then detect from data) or `"manual"` (specify dates)

### Query Range Options (for auto date_range)

- `"current_month"`: First day of current month to today
- `"last_month"`: Entire previous month
- `"days_back"`: Last N days (requires `days` parameter)
- `"custom"`: Specific dates (requires `start_date` and `end_date`)

## Usage

### Process All Enabled Farms

```bash
python batch_generate.py --config wind_farms_config.json
```

### List Farms

```bash
python batch_generate.py --config wind_farms_config.json --list-farms
```

### Process Specific Farm

```bash
python batch_generate.py --config wind_farms_config.json --farm-id dagpazari_RES
```

### Process All Farms (Ignore Enabled Flag)

```bash
python batch_generate.py --config wind_farms_config.json --force-all
```

### Process Disabled Farm

```bash
python batch_generate.py --config wind_farms_config.json --farm-id disabled_farm --force
```

## How It Works

### Location Bounds Auto-Calculation

1. **Calculate Centroid**: Average of all turbine coordinates
2. **Find Max Distance**: Maximum distance from centroid to any turbine
3. **Add Distance Ring**: Add max distance ring (e.g., 30km)
4. **Add Padding**: Add padding (e.g., 5km)
5. **Result**: Center (centroid) + Radius (total distance)

## Output

- Reports are saved to each farm's `output_directory` in the config
- Batch summary saved to `reports/batch_summary_YYYY-MM-DD.json`
- Log file: `batch_generation_YYYY-MM-DD.log`

## API Endpoints

The system uses:
- Lightning data: `https://risk.tarla.io/api/lightning-data/historical/`
- Storm data: `https://risk.tarla.io/api/storm-data/historical/`

Parameters:
- `queryType=circle`
- `centerLongitude` (longitude first)
- `centerLatitude`
- `radius` (in meters)
- `startDate=YYYY-MM-DD`
- `endDate=YYYY-MM-DD`

## Troubleshooting

### API Key Not Found

Ensure `.env` file exists with `API_KEY=your_key`

### No Data Fetched

- Check API credentials
- Verify date range is correct
- Check if location bounds cover the area
- Verify API endpoint URLs

### Farm Skipped

- Check if `enabled: false` in config
- Use `--force` flag to process disabled farms

