rossby 0.0.3

A blazingly fast, in-memory, NetCDF-to-API server
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
//! Image generation endpoint handler.
//!
//! Returns a PNG/JPEG image rendering of a variable over a specified region and time.

use axum::{
    extract::{Query, State},
    http::{header, HeaderMap, StatusCode},
    response::{IntoResponse, Response},
    Json,
};
use image::{ImageBuffer, RgbaImage};
use ndarray::ArrayView2;
use serde::Deserialize;
use std::collections::HashMap;
use std::io::Cursor;
use std::sync::Arc;
use std::time::Instant;
use tracing::{debug, info};

use crate::colormaps::{
    self, adjust_for_dateline_crossing, handle_dateline_crossing_bbox, parse_bbox, resample_data,
    Colormap, MapProjection,
};
use crate::error::{Result, RossbyError};
use crate::logging::{generate_request_id, log_request_error};
use crate::state::AppState;

/// Default image dimensions
const DEFAULT_WIDTH: u32 = 800;
const DEFAULT_HEIGHT: u32 = 600;

/// Default colormap
const DEFAULT_COLORMAP: &str = "viridis";

/// Default output format
const DEFAULT_FORMAT: &str = "png";

/// Query parameters for image endpoint
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ImageQuery {
    /// Variable name to render
    pub var: String,
    /// Time index (0-based)
    pub time_index: Option<usize>,
    /// Time physical value (preferred over time_index)
    pub time: Option<f64>,
    /// Raw time index (preferred over time_index, used by experts)
    pub __time_index: Option<usize>,
    /// Level/pressure value (for 3D+ data)
    pub level: Option<f64>,
    /// Raw level index
    pub __level_index: Option<usize>,
    /// Bounding box as "min_lon,min_lat,max_lon,max_lat"
    pub bbox: Option<String>,
    /// Image width in pixels
    pub width: Option<u32>,
    /// Image height in pixels
    pub height: Option<u32>,
    /// Colormap name (e.g., viridis, plasma, coolwarm)
    pub colormap: Option<String>,
    /// Interpolation method for resampling (deprecated, use resampling instead)
    pub interpolation: Option<String>,
    /// Output format (png or jpeg)
    pub format: Option<String>,
    /// Map centering (eurocentric, americas, pacific, or custom longitude)
    pub center: Option<String>,
    /// Allow bounding boxes that cross the dateline/prime meridian
    pub wrap_longitude: Option<bool>,
    /// Upsampling/downsampling quality (auto, nearest, bilinear, bicubic)
    pub resampling: Option<String>,
    /// Whether to enhance pole regions to reduce distortion
    pub enhance_poles: Option<bool>,
    /// Extra fields for arbitrary dimension values and indices
    #[serde(flatten)]
    pub extra: HashMap<String, serde_json::Value>,
}

// Note: parse_bbox function now imported from colormaps::geoutil
// Note: normalize_longitude function now imported from colormaps::geoutil
// Note: adjust_bbox_for_center replaced by handle_dateline_crossing_bbox from colormaps::geoutil

/// Generate an image from 2D data array using specified colormap and interpolation method
fn generate_image(
    data: ArrayView2<f32>,
    width: u32,
    height: u32,
    colormap: &dyn Colormap,
    resampling: &str,
) -> Result<RgbaImage> {
    // Find min/max values for normalization
    let mut min_val = f32::INFINITY;
    let mut max_val = f32::NEG_INFINITY;

    for &val in data.iter() {
        if val.is_finite() {
            min_val = min_val.min(val);
            max_val = max_val.max(val);
        }
    }

    // Create a new image buffer
    let mut img = ImageBuffer::new(width, height);

    // Get the interpolator based on the resampling method
    let interpolator = match resampling {
        "nearest" => crate::interpolation::get_interpolator("nearest")?,
        "bilinear" => crate::interpolation::get_interpolator("bilinear")?,
        "bicubic" => crate::interpolation::get_interpolator("bicubic")?,
        "auto" => {
            // Automatically select the best interpolation method based on the scaling factor
            let scale_x = width as f32 / data.shape()[1] as f32;
            let scale_y = height as f32 / data.shape()[0] as f32;
            let scale = scale_x.max(scale_y);

            if scale <= 0.5 {
                // Downsampling by more than 2x: use bilinear to avoid aliasing
                crate::interpolation::get_interpolator("bilinear")?
            } else if scale <= 1.0 {
                // Slight downsampling: use bilinear
                crate::interpolation::get_interpolator("bilinear")?
            } else if scale <= 2.0 {
                // Slight upsampling: use bilinear
                crate::interpolation::get_interpolator("bilinear")?
            } else {
                // Significant upsampling: use bicubic for smoother results
                crate::interpolation::get_interpolator("bicubic")?
            }
        }
        // Default to bilinear for any other value
        _ => crate::interpolation::get_interpolator("bilinear")?,
    };

    let data_height = data.shape()[0];
    let data_width = data.shape()[1];

    // Flatten the 2D array for the interpolator
    let flat_data: Vec<f32> = data.iter().cloned().collect();
    let shape = vec![data_height, data_width];

    // NetCDF data typically has coordinates where:
    // - First dimension (data_height) corresponds to latitude, with index 0 at the bottom (south)
    // - Second dimension (data_width) corresponds to longitude, with index 0 at the left (west)
    //
    // For proper display on screen:
    // - Image y=0 should map to the top row of data (north, highest latitude)
    // - Image y=height-1 should map to the bottom row of data (south, lowest latitude)
    // - Image x=0 should map to the left column of data (west, lowest longitude)
    // - Image x=width-1 should map to the right column of data (east, highest longitude)

    for y in 0..height {
        for x in 0..width {
            // Map image coordinates to data coordinates (fractional indices)
            // The previous fix corrected the upside-down issue but introduced left-right flipping
            // We need to use direct mapping for both lat and lon for proper orientation

            // For longitude (x): direct mapping (left-to-right)
            let data_x = x as f64 * (data_width - 1) as f64 / (width - 1) as f64;

            // For latitude (y): direct mapping (don't invert)
            let data_y = y as f64 * (data_height - 1) as f64 / (height - 1) as f64;

            // Perform interpolation to get the value at this pixel
            let indices = vec![data_y, data_x];
            let data_value = interpolator
                .interpolate(&flat_data, &shape, &indices)
                .unwrap_or(f32::NAN);

            // Map value to color
            let color = if data_value.is_finite() {
                colormap.map(data_value, min_val, max_val)
            } else {
                // Use transparent black for NaN/missing values
                [0, 0, 0, 0]
            };

            // Set pixel color
            img.put_pixel(x, y, image::Rgba(color));
        }
    }

    Ok(img)
}

/// Handle GET /image requests
pub async fn image_handler(
    State(state): State<Arc<AppState>>,
    Query(params): Query<ImageQuery>,
) -> Response {
    // Include all query parameters in the log for diagnostic purposes
    debug!(
        endpoint = "/image",
        request_id = %generate_request_id(),
        query_params = ?params,
        "Received image request with all parameters"
    );
    let request_id = generate_request_id();
    let start_time = Instant::now();

    // Log request parameters
    debug!(
        endpoint = "/image",
        request_id = %request_id,
        var = %params.var,
        time_index = ?params.time_index,
        bbox = ?params.bbox,
        width = ?params.width,
        height = ?params.height,
        colormap = ?params.colormap,
        format = ?params.format,
        "Processing image request"
    );

    // Process the request
    match generate_image_response(state.clone(), &params) {
        Ok(response) => {
            // Log successful request
            let duration = start_time.elapsed();
            // Determine the actual bbox used (either from params or full domain)
            let bbox_str = match &params.bbox {
                Some(bbox) => bbox.clone(),
                None => {
                    let (min_lon, min_lat, max_lon, max_lat) = state
                        .get_lat_lon_bounds()
                        .unwrap_or((0.0, -90.0, 360.0, 90.0));
                    format!(
                        "{:.2},{:.2},{:.2},{:.2}",
                        min_lon, min_lat, max_lon, max_lat
                    )
                }
            };

            // Determine the time index - similar logic as in generate_image_response
            let time_index = if let Some(raw_index) = params.__time_index {
                raw_index
            } else if let Some(time_val) = params.time {
                match state.find_coordinate_index_exact("time", time_val) {
                    Ok(idx) => idx,
                    Err(_) => state
                        .find_coordinate_index("time", time_val)
                        .unwrap_or_else(|_| params.time_index.unwrap_or(0)),
                }
            } else {
                params.time_index.unwrap_or(0)
            };

            // Get the actual time value used (if available)
            let time_value_str = if let Some(time_val) = params.time {
                format!("{}", time_val)
            } else if let Some(time_coords) = state.get_coordinate("time") {
                if time_index < time_coords.len() {
                    format!("{}", time_coords[time_index])
                } else {
                    "unknown".to_string()
                }
            } else {
                "unknown".to_string()
            };

            info!(
                endpoint = "/image",
                request_id = %request_id,
                var = %params.var,
                time_index = time_index,
                time_value = %time_value_str,
                bbox = %bbox_str,
                width = params.width.unwrap_or(DEFAULT_WIDTH),
                height = params.height.unwrap_or(DEFAULT_HEIGHT),
                duration_ms = duration.as_millis() as u64,
                "Image generation successful"
            );

            response
        }
        Err(RossbyError::InvalidVariables { names }) => {
            // Log error
            log_request_error(
                &RossbyError::InvalidVariables {
                    names: names.clone(),
                },
                "/image",
                &request_id,
                Some(&format!("Invalid variables: {}", names.join(", "))),
            );

            (
                StatusCode::BAD_REQUEST,
                Json(serde_json::json!({
                    "error": format!("Invalid variable(s): [{}]", names.join(", ")),
                    "request_id": request_id
                })),
            )
                .into_response()
        }
        Err(error) => {
            // Log error
            log_request_error(&error, "/image", &request_id, None);

            (
                StatusCode::BAD_REQUEST,
                Json(serde_json::json!({
                    "error": error.to_string(),
                    "request_id": request_id
                })),
            )
                .into_response()
        }
    }
}

/// Helper function to generate image response
fn generate_image_response(state: Arc<AppState>, params: &ImageQuery) -> Result<Response> {
    let operation_start = Instant::now();

    // Get variable name from query
    let var_name = params.var.clone();
    debug!(
        var_name = %var_name,
        "Checking variable validity"
    );

    // Verify variable exists
    if !state.has_variable(&var_name) {
        return Err(RossbyError::InvalidVariables {
            names: vec![var_name],
        });
    }

    // Verify variable is suitable for image rendering (must have latitude and longitude dimensions)
    let var_meta = state.get_variable_metadata_checked(&var_name)?;

    // Check for common latitude dimension names (lat, latitude)
    let has_lat = var_meta
        .dimensions
        .iter()
        .any(|d| d == "lat" || d == "latitude");

    // Check for common longitude dimension names (lon, longitude)
    let has_lon = var_meta
        .dimensions
        .iter()
        .any(|d| d == "lon" || d == "longitude");

    if !has_lat || !has_lon {
        return Err(RossbyError::VariableNotSuitableForImage { name: var_name });
    }

    // Determine time index based on priority:
    // 1. Raw index (__time_index) - most specific
    // 2. Physical value (time) - preferred for normal use
    // 3. Legacy time_index - deprecated but supported
    // 4. Default to 0
    let time_index = if let Some(raw_index) = params.__time_index {
        // Use the raw index directly
        raw_index
    } else if let Some(time_val) = params.time {
        // Convert physical time value to index
        match state.find_coordinate_index_exact("time", time_val) {
            Ok(idx) => idx,
            Err(RossbyError::PhysicalValueNotFound {
                dimension,
                value,
                available,
            }) => {
                return Err(RossbyError::PhysicalValueNotFound {
                    dimension,
                    value,
                    available,
                });
            }
            Err(_) => {
                // Fall back to closest match if exact match fails
                state.find_coordinate_index("time", time_val)?
            }
        }
    } else {
        // Fall back to legacy time_index or default
        params.time_index.unwrap_or(0)
    };

    // Check time index is in bounds
    if time_index >= state.time_dim_size() {
        return Err(RossbyError::IndexOutOfBounds {
            param: "time_index".to_string(),
            value: time_index.to_string(),
            max: state.time_dim_size() - 1,
        });
    }

    // Get map projection (default to eurocentric)
    let projection = match params.center.as_deref().unwrap_or("eurocentric") {
        "eurocentric" => MapProjection::Eurocentric,
        "americas" => MapProjection::Americas,
        "pacific" => MapProjection::Pacific,
        custom => {
            // Try to parse as a custom projection (e.g., "custom:45.0")
            if custom.starts_with("custom:") {
                let parts: Vec<&str> = custom.split(':').collect();
                if parts.len() == 2 {
                    if let Ok(center_lon) = parts[1].parse::<f32>() {
                        MapProjection::Custom(center_lon)
                    } else {
                        return Err(RossbyError::InvalidParameter {
                            param: "center".to_string(),
                            message: format!("Invalid custom center longitude: {}", parts[1]),
                        });
                    }
                } else {
                    MapProjection::parse_projection(custom)?
                }
            } else if let Ok(center_lon) = custom.parse::<f32>() {
                // Directly specify center longitude as a number
                MapProjection::Custom(center_lon)
            } else {
                return Err(RossbyError::InvalidParameter {
                    param: "center".to_string(),
                    message: format!("Invalid map center: {}. Valid values are 'eurocentric', 'americas', 'pacific', or a custom longitude value", custom),
                });
            }
        }
    };

    // Get longitude wrapping setting (default to false)
    let wrap_longitude = params.wrap_longitude.unwrap_or(false);

    // Parse bounding box (if provided)
    let (min_lon, min_lat, max_lon, max_lat) = if let Some(ref bbox) = params.bbox {
        parse_bbox(bbox)?
    } else {
        // Use full domain if no bbox specified
        state.get_lat_lon_bounds()?
    };

    // Handle dateline crossing and adjust bounding box for the selected projection
    let ((adj_min_lon, adj_min_lat, adj_max_lon, adj_max_lat), crosses_dateline) = if wrap_longitude
    {
        handle_dateline_crossing_bbox(min_lon, min_lat, max_lon, max_lat, &projection)?
    } else if min_lon > max_lon {
        // If not explicitly allowing wrapping, but bbox crosses the dateline, return an error
        return Err(RossbyError::InvalidParameter {
                param: "bbox".to_string(),
                message: "Bounding box crosses the dateline but wrap_longitude is not enabled. Set wrap_longitude=true to handle this case.".to_string(),
            });
    } else {
        ((min_lon, min_lat, max_lon, max_lat), false)
    };

    // Get image dimensions
    let width = params.width.unwrap_or(DEFAULT_WIDTH);
    let height = params.height.unwrap_or(DEFAULT_HEIGHT);

    // Get colormap
    let colormap_name = params.colormap.as_deref().unwrap_or(DEFAULT_COLORMAP);
    let colormap = colormaps::get_colormap(colormap_name)?;

    // Get resampling method (default to auto)
    // Fall back to interpolation parameter for backward compatibility
    let resampling = params
        .resampling
        .as_deref()
        .or(params.interpolation.as_deref())
        .unwrap_or("auto");

    // Get output format
    let format = params
        .format
        .as_deref()
        .unwrap_or(DEFAULT_FORMAT)
        .to_lowercase();
    if format != "png" && format != "jpeg" {
        return Err(RossbyError::InvalidParameter {
            param: "format".to_string(),
            message: "Format must be 'png' or 'jpeg'".to_string(),
        });
    }

    // Get the coordinate arrays for the region - try both common naming conventions
    let lon_coords = if state.has_coordinate("lon") {
        state.get_coordinate_checked("lon")?
    } else {
        state.get_coordinate_checked("longitude")?
    };

    let _lat_coords = if state.has_coordinate("lat") {
        state.get_coordinate_checked("lat")?
    } else {
        state.get_coordinate_checked("latitude")?
    };

    // Extract all dimension values from the query parameters
    // This includes explicitly defined parameters like time, level
    // as well as any extra dimensions in the flattened HashMap
    let mut dim_indices = HashMap::new();

    // Handle explicit time dimension
    if let Some(raw_index) = params.__time_index {
        // Raw index takes precedence
        dim_indices.insert("time".to_string(), raw_index);
    } else if let Some(time_val) = params.time {
        // Physical value - convert to index
        match state.find_coordinate_index_exact("time", time_val) {
            Ok(idx) => {
                dim_indices.insert("time".to_string(), idx);
            }
            Err(_) => {
                // Fall back to closest match or error
                let idx = state.find_coordinate_index("time", time_val)?;
                dim_indices.insert("time".to_string(), idx);
            }
        }
    } else if let Some(time_idx) = params.time_index {
        // Legacy time_index
        dim_indices.insert("time".to_string(), time_idx);
    }

    // Handle explicit level dimension
    if let Some(raw_index) = params.__level_index {
        dim_indices.insert("level".to_string(), raw_index);
    } else if let Some(level_val) = params.level {
        // Try to find with common level dimension names
        let level_names = ["level", "lev", "plev", "pressure", "height"];

        for &level_name in &level_names {
            if let Ok(idx) = state.find_coordinate_index_exact(level_name, level_val) {
                dim_indices.insert(level_name.to_string(), idx);
                break;
            } else if let Ok(idx) = state.find_coordinate_index(level_name, level_val) {
                dim_indices.insert(level_name.to_string(), idx);
                break;
            }
        }
    }

    // Process any additional dimensions from the flattened extra HashMap
    for (key, value) in &params.extra {
        // Skip standard parameters we've already processed
        if [
            "var",
            "time_index",
            "time",
            "__time_index",
            "level",
            "__level_index",
            "bbox",
            "width",
            "height",
            "colormap",
            "interpolation",
            "format",
            "center",
            "wrap_longitude",
            "resampling",
            "enhance_poles",
        ]
        .contains(&key.as_str())
        {
            continue;
        }

        // Check if this is a raw index parameter (starts with __)
        if key.starts_with("__") && key.ends_with("_index") {
            let dim_name = key.trim_start_matches("__").trim_end_matches("_index");
            if let Some(index) = value.as_u64() {
                dim_indices.insert(dim_name.to_string(), index as usize);
            }
            continue;
        }

        // Otherwise treat as a physical value and try to find the corresponding dimension
        if let Some(val) = value.as_f64() {
            // Try with common dimension prefixes/patterns
            let dim_name = key;
            if let Ok(idx) = state.find_coordinate_index_exact(dim_name, val) {
                dim_indices.insert(dim_name.to_string(), idx);
            } else if let Ok(idx) = state.find_coordinate_index(dim_name, val) {
                dim_indices.insert(dim_name.to_string(), idx);
            }
        }
    }

    // Debug log all the dimension indices we're using
    debug!(
        var_name = %var_name,
        dimensions = ?dim_indices,
        "Using these dimension indices for slicing"
    );

    // Get data slice for the specified dimensions and spatial bounds
    let mut data = state.get_data_slice_with_dims(
        &var_name,
        adj_min_lon,
        adj_min_lat,
        adj_max_lon,
        adj_max_lat,
        &dim_indices,
    )?;

    // Handle dateline crossing by duplicating data if needed
    let mut _adjusted_lon_coords = lon_coords.to_vec();
    if crosses_dateline && !data.is_empty() {
        // Adjust the data array to handle dateline crossing
        // Make sure we're using safe handling with proper error checking
        match adjust_for_dateline_crossing(&data.view(), lon_coords, crosses_dateline) {
            Ok((new_data, new_lon_coords)) => {
                data = new_data;
                _adjusted_lon_coords = new_lon_coords;
            }
            Err(e) => {
                eprintln!("Warning: Failed to adjust for dateline crossing: {}", e);
                // Continue with the original data - better to show something than error out
            }
        }
    }

    // Resample data if needed (when the target resolution differs significantly from the data resolution)
    if resampling != "none" {
        // Check if we need to resample
        let data_width = data.shape()[1];
        let data_height = data.shape()[0];

        // If the data dimensions are very different from the requested image dimensions,
        // resample the data to improve performance and quality
        if (data_width as f32 / width as f32).abs() > 2.0
            || (data_height as f32 / height as f32).abs() > 2.0
        {
            // Resample to dimensions closer to the target image
            let target_width = (width as f32 * 0.8).min(data_width as f32) as usize;
            let target_height = (height as f32 * 0.8).min(data_height as f32) as usize;

            data = resample_data(&data.view(), target_width, target_height)?;
        }
    }

    // Generate the image with the specified interpolation method
    debug!(
        width = width,
        height = height,
        data_shape = ?data.shape(),
        resampling = %resampling,
        "Generating image from data"
    );

    let image_gen_start = Instant::now();
    let img = generate_image(data.view(), width, height, colormap.as_ref(), resampling)?;

    let image_gen_duration = image_gen_start.elapsed();
    debug!(
        duration_ms = image_gen_duration.as_millis() as u64,
        "Image generation completed"
    );

    // Note: Pole enhancement feature is not yet implemented
    // This will be added in a future update

    // Encode the image to the specified format
    debug!(
        format = %format,
        "Encoding image"
    );

    let encoding_start = Instant::now();
    let mut buffer = Cursor::new(Vec::new());

    match format.as_str() {
        "png" => {
            image::DynamicImage::ImageRgba8(img.clone())
                .write_to(&mut buffer, image::ImageFormat::Png)
                .map_err(|e| RossbyError::ImageGeneration {
                    message: format!("Failed to encode PNG: {}", e),
                })?;
        }
        "jpeg" => {
            image::DynamicImage::ImageRgb8(image::DynamicImage::ImageRgba8(img).to_rgb8())
                .write_to(&mut buffer, image::ImageFormat::Jpeg)
                .map_err(|e| RossbyError::ImageGeneration {
                    message: format!("Failed to encode JPEG: {}", e),
                })?;
        }
        _ => unreachable!(), // We've already validated the format
    }

    let encoding_duration = encoding_start.elapsed();
    debug!(
        format = %format,
        encoding_duration_ms = encoding_duration.as_millis() as u64,
        "Image encoded successfully"
    );

    // Set appropriate headers
    let mut headers = HeaderMap::new();
    headers.insert(
        header::CONTENT_TYPE,
        match format.as_str() {
            "png" => "image/png",
            "jpeg" => "image/jpeg",
            _ => unreachable!(),
        }
        .parse()
        .unwrap(),
    );

    // Log overall processing time
    let total_duration = operation_start.elapsed();
    info!(
        var_name = %var_name,
        time_index = time_index,
        bbox = %format!("{:.2},{:.2},{:.2},{:.2}", min_lon, min_lat, max_lon, max_lat),
        format = %format,
        width = width,
        height = height,
        total_duration_ms = total_duration.as_millis() as u64,
        "Image response generated"
    );

    // Return the image
    Ok((StatusCode::OK, headers, buffer.into_inner()).into_response())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_bbox() {
        // Valid bbox
        let result = parse_bbox("10.5,20.5,30.5,40.5");
        assert!(result.is_ok());
        let (min_lon, min_lat, max_lon, max_lat) = result.unwrap();
        assert_eq!(min_lon, 10.5);
        assert_eq!(min_lat, 20.5);
        assert_eq!(max_lon, 30.5);
        assert_eq!(max_lat, 40.5);

        // Invalid format
        assert!(parse_bbox("10.5,20.5,30.5").is_err());

        // Invalid numbers
        assert!(parse_bbox("10.5,20.5,not_a_number,40.5").is_err());

        // Invalid latitude values
        assert!(parse_bbox("10.5,40.5,30.5,20.5").is_err()); // min_lat > max_lat
    }

    #[test]
    fn test_map_projections() {
        use std::str::FromStr;

        // Test converting string to MapProjection
        assert!(MapProjection::parse_projection("eurocentric").is_ok());
        assert!(MapProjection::parse_projection("americas").is_ok());
        assert!(MapProjection::parse_projection("pacific").is_ok());
        assert!(MapProjection::parse_projection("custom:45.0").is_ok());

        // Also test the FromStr implementation
        assert!(MapProjection::from_str("eurocentric").is_ok());
        assert!(MapProjection::from_str("americas").is_ok());
        assert!(MapProjection::from_str("pacific").is_ok());
        assert!(MapProjection::from_str("custom:45.0").is_ok());

        // Test invalid projections
        assert!(MapProjection::parse_projection("invalid").is_err());
        assert!(MapProjection::from_str("invalid").is_err());
    }

    #[test]
    fn test_dateline_crossing() {
        // Test bbox that crosses the dateline
        let ((min_lon, _min_lat, max_lon, _max_lat), crosses) =
            handle_dateline_crossing_bbox(170.0, 20.0, -170.0, 40.0, &MapProjection::Eurocentric)
                .unwrap();

        assert!(crosses); // Should detect crossing

        // When crosses_dateline is true, the longitudes are not adjusted to make max_lon > min_lon
        // Instead, the client code needs to handle this special case differently
        // So we update our test to check for the expected behavior: when crosses_dateline is true,
        // the original coordinates are preserved
        assert_eq!(min_lon, 170.0);
        assert_eq!(max_lon, -170.0);
    }

    #[test]
    fn test_image_orientation() {
        // In NetCDF files, latitude typically increases from south to north
        // This means the first row in the data array is the southernmost latitude (index 0)
        // And the last row is the northernmost latitude (index data_height-1)
        //
        // When rendered to an image, we want:
        // - North (highest latitude) at the top of the image
        // - South (lowest latitude) at the bottom of the image
        // - West (lowest longitude) at the left of the image
        // - East (highest longitude) at the right of the image

        // Create a test data array where values increase from south to north and west to east
        let data = ndarray::array![
            [1.0, 2.0, 3.0], // Row 0 (south, lowest latitude)
            [4.0, 5.0, 6.0], // Row 1 (middle latitude)
            [7.0, 8.0, 9.0]  // Row 2 (north, highest latitude)
        ];

        // Generate a 3x3 image with this data
        let colormap = colormaps::get_colormap("viridis").unwrap();
        let img = generate_image(data.view(), 3, 3, colormap.as_ref(), "nearest").unwrap();

        // Get the pixel values to check orientation
        let top_left = img.get_pixel(0, 0);
        let top_right = img.get_pixel(2, 0);
        let bottom_left = img.get_pixel(0, 2);
        let bottom_right = img.get_pixel(2, 2);

        // Convert the RGBA values to intensity (just for comparison purposes)
        let intensity = |pixel: &image::Rgba<u8>| -> u32 {
            let rgba = pixel.0;
            rgba[0] as u32 + rgba[1] as u32 + rgba[2] as u32
        };

        // Check that the image has the correct orientation with direct mapping:
        // - Top of image (y=0) should map to south (lowest latitude, row 0 of data)
        // - Bottom of image (y=height-1) should map to north (highest latitude, row 2 of data)
        // - Left of image (x=0) should map to west (lowest longitude, column 0 of data)
        // - Right of image (x=width-1) should map to east (highest longitude, column 2 of data)

        // For correctly oriented geographic data with direct mapping:
        assert!(intensity(top_left) < intensity(top_right)); // West to East increases (direct x mapping)
        assert!(intensity(top_left) < intensity(bottom_left)); // South to North increases (direct y mapping)
        assert!(intensity(bottom_left) < intensity(bottom_right)); // West to East increases (direct x mapping)
        assert!(intensity(top_right) < intensity(bottom_right)); // South to North increases (direct y mapping)
    }
}