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
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
//! Point query endpoint handler.
//!
//! Returns interpolated values for one or more variables at a specific point in space-time.

use axum::{
    extract::{Query, State},
    http::StatusCode,
    response::{IntoResponse, Response},
    Json,
};
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use std::time::Instant;
use tracing::{debug, info, warn};

use crate::error::RossbyError;
use crate::logging::{generate_request_id, log_request_error};
use crate::state::AppState;

/// Query parameters for point endpoint
#[derive(Debug, Deserialize, Clone)]
pub struct PointQuery {
    // File-specific physical values
    /// Longitude coordinate (file-specific name)
    #[serde(default)]
    pub lon: Option<f64>,
    /// Latitude coordinate (file-specific name)
    #[serde(default)]
    pub lat: Option<f64>,
    /// Time value (file-specific name)
    #[serde(default)]
    pub time: Option<f64>,

    // Canonical physical values with underscore prefix
    /// Longitude coordinate (canonical name with underscore prefix)
    #[serde(rename = "_longitude", default)]
    pub _longitude: Option<f64>,
    /// Latitude coordinate (canonical name with underscore prefix)
    #[serde(rename = "_latitude", default)]
    pub _latitude: Option<f64>,
    /// Time value (canonical name with underscore prefix)
    #[serde(rename = "_time", default)]
    pub _time: Option<f64>,

    // Raw indices with double-underscore prefix
    /// Longitude index (canonical name with double-underscore prefix)
    #[serde(rename = "__longitude_index", default)]
    pub __longitude_index: Option<usize>,
    /// Latitude index (canonical name with double-underscore prefix)
    #[serde(rename = "__latitude_index", default)]
    pub __latitude_index: Option<usize>,
    /// Time index (canonical name with double-underscore prefix)
    #[serde(rename = "__time_index", default)]
    pub __time_index: Option<usize>,

    // Deprecated parameters
    /// Time index (0-based) - DEPRECATED, use __time_index instead
    #[serde(default)]
    pub time_index: Option<usize>,

    // Other parameters
    /// Comma-separated list of variables to query
    pub vars: String,
    /// Interpolation method (nearest, bilinear, bicubic)
    pub interpolation: Option<String>,
}

/// Response for point query
#[derive(Debug, Serialize)]
pub struct PointResponse {
    #[serde(flatten)]
    pub values: serde_json::Map<String, serde_json::Value>,
}

/// Handle GET /point requests
pub async fn point_handler(
    State(state): State<Arc<AppState>>,
    Query(params): Query<PointQuery>,
) -> Response {
    let request_id = generate_request_id();
    let start_time = Instant::now();

    // Log request parameters
    debug!(
        endpoint = "/point",
        request_id = %request_id,
        lon = ?params.lon,
        lat = ?params.lat,
        time = ?params.time,
        time_index = ?params.time_index,
        vars = %params.vars,
        interpolation = ?params.interpolation,
        "Processing point query"
    );

    match process_point_query(state, params.clone()) {
        Ok(response) => {
            // Log successful request
            let duration = start_time.elapsed();
            info!(
                endpoint = "/point",
                request_id = %request_id,
                duration_us = duration.as_micros() as u64,
                "Point query successful"
            );

            Json(response).into_response()
        }
        Err(error) => {
            // Log error
            log_request_error(
                &error,
                "/point",
                &request_id,
                Some(&format!("vars={}", params.vars)),
            );

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

/// Process a point query
fn process_point_query(
    state: Arc<AppState>,
    params: PointQuery,
) -> Result<PointResponse, RossbyError> {
    // Setup indices
    #[allow(unused_assignments)]
    let mut longitude_idx: Option<usize> = None;
    #[allow(unused_assignments)]
    let mut latitude_idx: Option<usize> = None;
    #[allow(unused_assignments)]
    let mut time_idx: Option<usize> = None;

    // Get longitude using raw index or physical value
    #[allow(unused_assignments)]
    let mut lon_value = None;
    if let Some(idx) = params.__longitude_index {
        // Use raw index directly
        let lon_coords = state
            .get_coordinate_checked("lon")
            .or_else(|_| state.get_coordinate_checked("_longitude"))
            .or_else(|_| state.get_coordinate_checked("longitude"))?;

        // Check if index is in bounds
        if idx >= lon_coords.len() {
            return Err(RossbyError::IndexOutOfBounds {
                param: "__longitude_index".to_string(),
                value: idx.to_string(),
                max: lon_coords.len() - 1,
            });
        }

        longitude_idx = Some(idx);
        lon_value = Some(lon_coords[idx]);
    } else {
        // Get longitude coordinate - try direct file-specific name first, then prefixed canonical name
        let lon = match (params.lon, params._longitude) {
            (Some(value), _) => value,
            (None, Some(value)) => value,
            (None, None) => {
                return Err(RossbyError::InvalidParameter {
                    param: "longitude".to_string(),
                    message: "Missing longitude coordinate. Provide either file-specific name (e.g., 'lon'), canonical name with underscore prefix (e.g., '_longitude'), or raw index with double-underscore prefix (e.g., '__longitude_index')".to_string(),
                })
            }
        };
        lon_value = Some(lon);
    }

    // Get latitude using raw index or physical value
    #[allow(unused_assignments)]
    let mut lat_value = None;
    if let Some(idx) = params.__latitude_index {
        // Use raw index directly
        let lat_coords = state
            .get_coordinate_checked("lat")
            .or_else(|_| state.get_coordinate_checked("_latitude"))
            .or_else(|_| state.get_coordinate_checked("latitude"))?;

        // Check if index is in bounds
        if idx >= lat_coords.len() {
            return Err(RossbyError::IndexOutOfBounds {
                param: "__latitude_index".to_string(),
                value: idx.to_string(),
                max: lat_coords.len() - 1,
            });
        }

        latitude_idx = Some(idx);
        lat_value = Some(lat_coords[idx]);
    } else {
        // Get latitude coordinate - try direct file-specific name first, then prefixed canonical name
        let lat = match (params.lat, params._latitude) {
            (Some(value), _) => value,
            (None, Some(value)) => value,
            (None, None) => {
                return Err(RossbyError::InvalidParameter {
                    param: "latitude".to_string(),
                    message: "Missing latitude coordinate. Provide either file-specific name (e.g., 'lat'), canonical name with underscore prefix (e.g., '_latitude'), or raw index with double-underscore prefix (e.g., '__latitude_index')".to_string(),
                })
            }
        };

        lat_value = Some(lat);
    }

    // Get time using raw index or physical value
    if let Some(idx) = params.__time_index {
        // Use raw index directly
        if idx >= state.time_dim_size() {
            return Err(RossbyError::IndexOutOfBounds {
                param: "__time_index".to_string(),
                value: idx.to_string(),
                max: state.time_dim_size() - 1,
            });
        }

        time_idx = Some(idx);
    } else if let Some(idx) = params.time_index {
        // Use deprecated time_index parameter (with warning)
        warn!(
            param = "time_index",
            deprecated_since = "0.1.0",
            replacement = "__time_index",
            "The 'time_index' parameter is deprecated. Please use '__time_index' instead."
        );

        if idx >= state.time_dim_size() {
            return Err(RossbyError::IndexOutOfBounds {
                param: "time_index".to_string(),
                value: idx.to_string(),
                max: state.time_dim_size() - 1,
            });
        }

        time_idx = Some(idx);
    } else if let Some(time_val) = params.time.or(params._time) {
        // Use physical time value - convert to index with exact match
        // Get time coordinates
        let _time_coords = state
            .get_coordinate_checked("time")
            .or_else(|_| state.get_coordinate_checked("_time"))?;

        // Find exact match for time value
        match state.find_coordinate_index_exact("time", time_val) {
            Ok(idx) => time_idx = Some(idx),
            Err(e) => return Err(e),
        }
    } else {
        // Default to time index 0
        time_idx = Some(0);
    }

    // Get time index (default to 0)
    let time_index = time_idx.unwrap_or(0);

    // Get the list of variables to query
    let variables: Vec<String> = params
        .vars
        .split(',')
        .map(|s| s.trim().to_string())
        .filter(|s| !s.is_empty())
        .collect();

    if variables.is_empty() {
        return Err(RossbyError::InvalidParameter {
            param: "vars".to_string(),
            message: "No variables specified".to_string(),
        });
    }

    // Get interpolation method (default to bilinear)
    let interpolation_method = params.interpolation.as_deref().unwrap_or("bilinear");
    let interpolator = crate::interpolation::get_interpolator(interpolation_method)?;

    // Results map
    let mut values = serde_json::Map::new();

    // Process each variable
    for var_name in variables {
        // Check if variable exists
        if !state.has_variable(&var_name) {
            return Err(RossbyError::VariableNotFound { name: var_name });
        }

        // Get variable dimensions
        let dimensions = state.get_variable_dimensions(&var_name)?;

        // Find dimension indices for lat, lon, and time with alias support
        let mut lat_dim_idx = None;
        let mut lon_dim_idx = None;
        let mut time_dim_idx = None;

        for (i, dim) in dimensions.iter().enumerate() {
            // Try to get the canonical name for this dimension
            let canonical = state.get_canonical_dimension_name(dim).unwrap_or(dim);

            if dim == "lat" || canonical == "latitude" {
                lat_dim_idx = Some(i);
            } else if dim == "lon" || canonical == "longitude" {
                lon_dim_idx = Some(i);
            } else if dim == "time" || canonical == "time" {
                time_dim_idx = Some(i);
            }
        }

        // Ensure we have lat and lon dimensions
        let lat_dim_idx = lat_dim_idx.ok_or_else(|| RossbyError::DataNotFound {
            message: format!("Variable {} does not have a lat dimension", var_name),
        })?;

        let lon_dim_idx = lon_dim_idx.ok_or_else(|| RossbyError::DataNotFound {
            message: format!("Variable {} does not have a lon dimension", var_name),
        })?;

        // Get the data array
        let data = state.get_variable_checked(&var_name)?;

        // Get coordinates using dimension aliases
        let lon_coords = state
            .get_coordinate_checked("lon")
            .or_else(|_| state.get_coordinate_checked("_longitude"))
            .or_else(|_| state.get_coordinate_checked("longitude"))?;

        let lat_coords = state
            .get_coordinate_checked("lat")
            .or_else(|_| state.get_coordinate_checked("_latitude"))
            .or_else(|_| state.get_coordinate_checked("latitude"))?;

        // Resolve indices from physical values if necessary
        let lon_idx = if let Some(idx) = longitude_idx {
            idx as f64
        } else {
            // Check if coordinates are within bounds
            let lon = lon_value.unwrap();
            if lon < *lon_coords.first().unwrap() || lon > *lon_coords.last().unwrap() {
                return Err(RossbyError::InvalidCoordinates {
                    message: format!(
                        "Longitude {} is outside the range [{}, {}]",
                        lon,
                        lon_coords.first().unwrap(),
                        lon_coords.last().unwrap()
                    ),
                });
            }

            // Find fractional index
            crate::interpolation::common::coord_to_index(lon, lon_coords)?
        };

        let lat_idx = if let Some(idx) = latitude_idx {
            idx as f64
        } else {
            // Check if coordinates are within bounds
            let lat = lat_value.unwrap();
            if lat < *lat_coords.first().unwrap() || lat > *lat_coords.last().unwrap() {
                return Err(RossbyError::InvalidCoordinates {
                    message: format!(
                        "Latitude {} is outside the range [{}, {}]",
                        lat,
                        lat_coords.first().unwrap(),
                        lat_coords.last().unwrap()
                    ),
                });
            }

            // Find fractional index
            crate::interpolation::common::coord_to_index(lat, lat_coords)?
        };

        // Set up the indices based on dimensionality
        let mut indices = vec![0.0; data.ndim()];
        indices[lon_dim_idx] = lon_idx;
        indices[lat_dim_idx] = lat_idx;

        // Set time index if present
        if let Some(idx) = time_dim_idx {
            indices[idx] = time_index as f64;
        }

        // Get the raw data as a slice
        let data_slice = data.as_slice().ok_or_else(|| RossbyError::DataNotFound {
            message: format!(
                "Cannot access data for variable {} as contiguous slice",
                var_name
            ),
        })?;

        // Interpolate the value
        let value = interpolator.interpolate(data_slice, data.shape(), &indices)?;

        // Add to results
        values.insert(
            var_name,
            serde_json::Value::Number(serde_json::Number::from_f64(value as f64).unwrap()),
        );
    }

    Ok(PointResponse { values })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::Config;
    use crate::state::{AttributeValue, Dimension, Metadata, Variable};
    use ndarray::{Array, IxDyn};
    use std::collections::HashMap;

    // Helper function to create a test AppState
    fn create_test_state() -> Arc<AppState> {
        // Create a simple 2D grid with known values
        // Data is a 2x3 grid (lat x lon) with values 1-6
        let data_array =
            Array::from_shape_vec(IxDyn(&[2, 3]), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0]).unwrap();

        // Create metadata
        let mut dimensions = HashMap::new();
        dimensions.insert(
            "lat".to_string(),
            Dimension {
                name: "lat".to_string(),
                size: 2,
                is_unlimited: false,
            },
        );
        dimensions.insert(
            "lon".to_string(),
            Dimension {
                name: "lon".to_string(),
                size: 3,
                is_unlimited: false,
            },
        );

        // Create variable metadata
        let mut variables = HashMap::new();
        let mut var_attributes = HashMap::new();
        var_attributes.insert(
            "units".to_string(),
            AttributeValue::Text("degrees_C".to_string()),
        );

        variables.insert(
            "temperature".to_string(),
            Variable {
                name: "temperature".to_string(),
                dimensions: vec!["lat".to_string(), "lon".to_string()],
                shape: vec![2, 3],
                attributes: var_attributes,
                dtype: "f32".to_string(),
            },
        );

        // Create coordinate values
        let mut coordinates = HashMap::new();
        coordinates.insert("lat".to_string(), vec![10.0, 20.0]);
        coordinates.insert("lon".to_string(), vec![100.0, 110.0, 120.0]);

        // Create metadata
        let metadata = Metadata {
            global_attributes: HashMap::new(),
            dimensions,
            variables,
            coordinates,
        };

        // Create data map
        let mut data = HashMap::new();
        data.insert("temperature".to_string(), data_array);

        // Create config
        let config = Config::default();

        // Create AppState
        Arc::new(AppState::new(config, metadata, data))
    }

    // Helper function to create a test AppState with dimension aliases
    fn create_test_state_with_aliases() -> Arc<AppState> {
        // Create a simple 2D grid with known values
        // Data is a 2x3 grid (lat x lon) with values 1-6
        let data_array =
            Array::from_shape_vec(IxDyn(&[2, 3]), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0]).unwrap();

        // Create metadata
        let mut dimensions = HashMap::new();
        dimensions.insert(
            "lat".to_string(),
            Dimension {
                name: "lat".to_string(),
                size: 2,
                is_unlimited: false,
            },
        );
        dimensions.insert(
            "lon".to_string(),
            Dimension {
                name: "lon".to_string(),
                size: 3,
                is_unlimited: false,
            },
        );

        // Create variable metadata
        let mut variables = HashMap::new();
        let mut var_attributes = HashMap::new();
        var_attributes.insert(
            "units".to_string(),
            AttributeValue::Text("degrees_C".to_string()),
        );

        variables.insert(
            "temperature".to_string(),
            Variable {
                name: "temperature".to_string(),
                dimensions: vec!["lat".to_string(), "lon".to_string()],
                shape: vec![2, 3],
                attributes: var_attributes,
                dtype: "f32".to_string(),
            },
        );

        // Create coordinate values
        let mut coordinates = HashMap::new();
        coordinates.insert("lat".to_string(), vec![10.0, 20.0]);
        coordinates.insert("lon".to_string(), vec![100.0, 110.0, 120.0]);

        // Create metadata
        let metadata = Metadata {
            global_attributes: HashMap::new(),
            dimensions,
            variables,
            coordinates,
        };

        // Create data map
        let mut data = HashMap::new();
        data.insert("temperature".to_string(), data_array);

        // Create config with dimension aliases
        let mut config = Config::default();
        let mut aliases = HashMap::new();
        aliases.insert("latitude".to_string(), "lat".to_string());
        aliases.insert("longitude".to_string(), "lon".to_string());
        config.data.dimension_aliases = aliases;

        // Create AppState
        Arc::new(AppState::new(config, metadata, data))
    }

    #[test]
    fn test_point_query_success() {
        let state = create_test_state();

        // Query a point at exact grid location (10.0, 100.0) - should be exactly 1.0
        let params = PointQuery {
            lon: Some(100.0),
            lat: Some(10.0),
            time: None,
            _longitude: None,
            _latitude: None,
            _time: None,
            __longitude_index: None,
            __latitude_index: None,
            __time_index: None,
            time_index: None,
            vars: "temperature".to_string(),
            interpolation: Some("nearest".to_string()),
        };

        let result = process_point_query(state.clone(), params).unwrap();
        let value = result.values.get("temperature").unwrap().as_f64().unwrap();
        assert_eq!(value, 1.0);

        // Query a point in between grid points with bilinear interpolation
        let params = PointQuery {
            lon: Some(105.0), // halfway between 100.0 and 110.0
            lat: Some(15.0),  // halfway between 10.0 and 20.0
            time: None,
            _longitude: None,
            _latitude: None,
            _time: None,
            __longitude_index: None,
            __latitude_index: None,
            __time_index: None,
            time_index: None,
            vars: "temperature".to_string(),
            interpolation: Some("bilinear".to_string()),
        };

        let result = process_point_query(state.clone(), params).unwrap();
        let value = result.values.get("temperature").unwrap().as_f64().unwrap();
        // Expected value: linear interpolation between 1.0, 2.0, 4.0, 5.0
        assert!((value - 3.0).abs() < 1e-5);
    }

    #[test]
    fn test_multiple_variables() {
        // For this test, we would need a more complex test state with multiple variables
        // For now, we'll just test the error case when an invalid variable is requested
        let state = create_test_state();

        let params = PointQuery {
            lon: Some(100.0),
            lat: Some(10.0),
            time: None,
            _longitude: None,
            _latitude: None,
            _time: None,
            __longitude_index: None,
            __latitude_index: None,
            __time_index: None,
            time_index: None,
            vars: "temperature,humidity".to_string(), // humidity doesn't exist
            interpolation: None,
        };

        let result = process_point_query(state.clone(), params);
        assert!(result.is_err());

        if let Err(RossbyError::VariableNotFound { name }) = result {
            assert_eq!(name, "humidity");
        } else {
            panic!("Expected VariableNotFound error");
        }
    }

    #[test]
    fn test_out_of_bounds() {
        let state = create_test_state();

        // Test out of bounds longitude
        let params = PointQuery {
            lon: Some(130.0), // outside the range of [100.0, 120.0]
            lat: Some(10.0),
            time: None,
            _longitude: None,
            _latitude: None,
            _time: None,
            __longitude_index: None,
            __latitude_index: None,
            __time_index: None,
            time_index: None,
            vars: "temperature".to_string(),
            interpolation: None,
        };

        let result = process_point_query(state.clone(), params);
        assert!(result.is_err());

        if let Err(RossbyError::InvalidCoordinates { .. }) = result {
            // Expected error
        } else {
            panic!("Expected InvalidCoordinates error");
        }

        // Test out of bounds latitude
        let params = PointQuery {
            lon: Some(100.0),
            lat: Some(30.0), // outside the range of [10.0, 20.0]
            time: None,
            _longitude: None,
            _latitude: None,
            _time: None,
            __longitude_index: None,
            __latitude_index: None,
            __time_index: None,
            time_index: None,
            vars: "temperature".to_string(),
            interpolation: None,
        };

        let result = process_point_query(state.clone(), params);
        assert!(result.is_err());

        if let Err(RossbyError::InvalidCoordinates { .. }) = result {
            // Expected error
        } else {
            panic!("Expected InvalidCoordinates error");
        }
    }

    #[test]
    fn test_invalid_interpolation() {
        let state = create_test_state();

        let params = PointQuery {
            lon: Some(100.0),
            lat: Some(10.0),
            time: None,
            _longitude: None,
            _latitude: None,
            _time: None,
            __longitude_index: None,
            __latitude_index: None,
            __time_index: None,
            time_index: None,
            vars: "temperature".to_string(),
            interpolation: Some("invalid_method".to_string()),
        };

        let result = process_point_query(state.clone(), params);
        assert!(result.is_err());

        if let Err(RossbyError::InvalidParameter { param, .. }) = result {
            assert_eq!(param, "interpolation");
        } else {
            panic!("Expected InvalidParameter error");
        }
    }

    #[test]
    fn test_empty_vars() {
        let state = create_test_state();

        let params = PointQuery {
            lon: Some(100.0),
            lat: Some(10.0),
            time: None,
            _longitude: None,
            _latitude: None,
            _time: None,
            __longitude_index: None,
            __latitude_index: None,
            __time_index: None,
            time_index: None,
            vars: "".to_string(), // Empty variable list
            interpolation: None,
        };

        let result = process_point_query(state.clone(), params);
        assert!(result.is_err());

        if let Err(RossbyError::InvalidParameter { param, .. }) = result {
            assert_eq!(param, "vars");
        } else {
            panic!("Expected InvalidParameter error");
        }
    }

    #[test]
    fn test_dimension_aliases() {
        // Test with prefixed canonical names
        let state = create_test_state();

        // Use _longitude and _latitude instead of lon and lat
        let params = PointQuery {
            lon: None,
            lat: None,
            time: None,
            _longitude: Some(100.0),
            _latitude: Some(10.0),
            _time: None,
            __longitude_index: None,
            __latitude_index: None,
            __time_index: None,
            time_index: None,
            vars: "temperature".to_string(),
            interpolation: Some("nearest".to_string()),
        };

        let result = process_point_query(state.clone(), params);
        assert!(
            result.is_ok(),
            "Query with prefixed canonical names failed: {:?}",
            result
        );
        let value = result
            .unwrap()
            .values
            .get("temperature")
            .unwrap()
            .as_f64()
            .unwrap();
        assert_eq!(value, 1.0);

        // Test with config-based aliases
        let state_with_aliases = create_test_state_with_aliases();

        // The AppState should now recognize 'longitude' and 'latitude' as aliases for 'lon' and 'lat'
        let params = PointQuery {
            lon: None,
            lat: None,
            time: None,
            _longitude: Some(120.0), // Using the right-most longitude to get value 6.0
            _latitude: Some(20.0),
            _time: None,
            __longitude_index: None,
            __latitude_index: None,
            __time_index: None,
            time_index: None,
            vars: "temperature".to_string(),
            interpolation: Some("nearest".to_string()),
        };

        let result = process_point_query(state_with_aliases.clone(), params);
        assert!(
            result.is_ok(),
            "Query with aliased names failed: {:?}",
            result
        );
        let value = result
            .unwrap()
            .values
            .get("temperature")
            .unwrap()
            .as_f64()
            .unwrap();
        assert_eq!(value, 6.0); // Value at [1, 2] = bottom right corner (lat=20.0, lon=120.0)
    }

    #[test]
    fn test_raw_indices() {
        let state = create_test_state();

        // Query using raw indices
        let params = PointQuery {
            lon: None,
            lat: None,
            time: None,
            _longitude: None,
            _latitude: None,
            _time: None,
            __longitude_index: Some(0), // First longitude (100.0)
            __latitude_index: Some(0),  // First latitude (10.0)
            __time_index: Some(0),      // First time index
            time_index: None,
            vars: "temperature".to_string(),
            interpolation: Some("nearest".to_string()),
        };

        let result = process_point_query(state.clone(), params);
        assert!(
            result.is_ok(),
            "Query with raw indices failed: {:?}",
            result
        );
        let value = result
            .unwrap()
            .values
            .get("temperature")
            .unwrap()
            .as_f64()
            .unwrap();
        assert_eq!(value, 1.0); // Should be the same as querying for (lon=100.0, lat=10.0)

        // Test index out of bounds
        let params = PointQuery {
            lon: None,
            lat: None,
            time: None,
            _longitude: None,
            _latitude: None,
            _time: None,
            __longitude_index: Some(3), // Out of bounds (max is 2)
            __latitude_index: Some(0),
            __time_index: None,
            time_index: None,
            vars: "temperature".to_string(),
            interpolation: None,
        };

        let result = process_point_query(state.clone(), params);
        assert!(result.is_err());

        if let Err(RossbyError::IndexOutOfBounds { param, value, max }) = result {
            assert_eq!(param, "__longitude_index");
            assert_eq!(value, "3");
            assert_eq!(max, 2);
        } else {
            panic!("Expected IndexOutOfBounds error");
        }
    }

    #[test]
    fn test_deprecated_time_index() {
        let state = create_test_state();

        // Query using deprecated time_index
        let params = PointQuery {
            lon: Some(100.0),
            lat: Some(10.0),
            time: None,
            _longitude: None,
            _latitude: None,
            _time: None,
            __longitude_index: None,
            __latitude_index: None,
            __time_index: None,
            time_index: Some(0), // Using deprecated parameter
            vars: "temperature".to_string(),
            interpolation: None,
        };

        let result = process_point_query(state.clone(), params);
        assert!(result.is_ok());
        let value = result
            .unwrap()
            .values
            .get("temperature")
            .unwrap()
            .as_f64()
            .unwrap();
        assert_eq!(value, 1.0);
    }

    #[test]
    fn test_mixed_query_params() {
        let state = create_test_state();

        // Mix of physical values and raw indices
        let params = PointQuery {
            lon: Some(100.0), // Physical value
            lat: None,
            time: None,
            _longitude: None,
            _latitude: None,
            _time: None,
            __longitude_index: None,
            __latitude_index: Some(0), // Raw index
            __time_index: Some(0),     // Raw index
            time_index: None,
            vars: "temperature".to_string(),
            interpolation: None,
        };

        let result = process_point_query(state.clone(), params);
        assert!(result.is_ok());
        let value = result
            .unwrap()
            .values
            .get("temperature")
            .unwrap()
            .as_f64()
            .unwrap();
        assert_eq!(value, 1.0);
    }
}