-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTest_TimeSeriesDownload.cs
More file actions
822 lines (697 loc) · 30.7 KB
/
Test_TimeSeriesDownload.cs
File metadata and controls
822 lines (697 loc) · 30.7 KB
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
/*
* NOTICE:
* The U.S. Army Corps of Engineers, Risk Management Center (USACE-RMC) makes no guarantees about
* the results, or appropriateness of outputs, obtained from Numerics.
*
* LIST OF CONDITIONS:
* Redistribution and use in source and binary forms, with or without modification, are permitted
* provided that the following conditions are met:
* ● Redistributions of source code must retain the above notice, this list of conditions, and the
* following disclaimer.
* ● Redistributions in binary form must reproduce the above notice, this list of conditions, and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
* ● The names of the U.S. Government, the U.S. Army Corps of Engineers, the Institute for Water
* Resources, or the Risk Management Center may not be used to endorse or promote products derived
* from this software without specific prior written permission. Nor may the names of its contributors
* be used to endorse or promote products derived from this software without specific prior
* written permission.
*
* DISCLAIMER:
* THIS SOFTWARE IS PROVIDED BY THE U.S. ARMY CORPS OF ENGINEERS RISK MANAGEMENT CENTER
* (USACE-RMC) "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL USACE-RMC BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Numerics.Data;
namespace Data.TimeSeriesAnalysis
{
/// <summary>
/// Provides integration and validation tests for the <see cref="TimeSeriesDownload"/> class,
/// including downloads from the Canadian Hydrometric Monitoring Network (CHMN),
/// the United States Geological Survey (USGS), and the Global Historical Climatology Network (GHCN).
/// </summary>
/// <remarks>
/// <para>
/// <b> Authors: </b>
/// <list type="bullet">
/// <item>Haden Smith, USACE Risk Management Center, cole.h.smith@usace.army.mil</item>
/// </list>
/// </para>
/// </remarks>
[TestClass]
[DoNotParallelize]
public class Test_TimeSeriesDownload
{
#region Station Lists
/// <summary>
/// CHMN station: Cold River at Merritt.
/// </summary>
private const string CHMN_1 = "08LG010";
/// <summary>
/// CHMN station: Lillooet River near Pemberton.
/// </summary>
private const string CHMN_2 = "08MG005";
/// <summary>
/// CHMN station: Capilano River above intake.
/// </summary>
private const string CHMN_3 = "08GA010";
/// <summary>
/// USGS site number for Little River near Durham, NH (example).
/// </summary>
private const string USGS_1 = "01134500";
/// <summary>
/// USGS site number for Kaweah River near Three Rivers, CA.
/// </summary>
private const string USGS_2 = "11274500";
/// <summary>
/// USGS site number for Potomac River near Washington, DC.
/// </summary>
private const string USGS_3 = "01614000";
/// <summary>
/// USGS site number for Mississippi River at St. Louis, MO.
/// </summary>
private const string USGS_4 = "07010000";
/// <summary>
/// USGS site number for Susquehanna River at Harrisburg, PA.
/// </summary>
private const string USGS_5 = "01570500";
/// <summary>
/// USGS site number for Potomac River at Little Falls near Washington, DC.
/// </summary>
private const string USGS_6 = "01646500";
/// <summary>
/// GHCN station: USC00040741.
/// </summary>
private const string GHCN_1 = "USC00040741";
/// <summary>
/// GHCN station: USC00042402.
/// </summary>
private const string GHCN_2 = "USC00042402";
/// <summary>
/// GHCN station: USC00046685.
/// </summary>
private const string GHCN_3 = "USC00046685";
/// <summary>
/// BOM station: Cotter River at Gingera.
/// </summary>
private const string BOM_1 = "410730";
/// <summary>
/// BOM station: Goodradigbee River at Wee Jasper.
/// </summary>
private const string BOM_2 = "410761";
/// <summary>
/// BOM station: Murray River at Doctors Point.
/// </summary>
private const string BOM_3 = "409202";
/// <summary>
/// Start date for deterministic test windows.
/// </summary>
private static readonly DateTime WinStart = new DateTime(2021, 11, 01);
/// <summary>
/// End date for deterministic test windows.
/// </summary>
private static readonly DateTime WinEnd = new DateTime(2021, 11, 30);
#endregion
#region Helper Methods
/// <summary>
/// Checks whether the system currently has an active Internet connection.
/// </summary>
/// <returns>True if an Internet connection is available; otherwise false.</returns>
private static async Task<bool> Online() => await TimeSeriesDownload.IsConnectedToInternet();
/// <summary>
/// Verifies that a time series has monotonically increasing, non-duplicated date indices.
/// </summary>
/// <param name="ts">The time series to validate.</param>
private static void AssertDailySeriesMonotonic(TimeSeries ts)
{
Assert.IsNotNull(ts, "Time series is null.");
Assert.IsGreaterThan(0, ts.Count);
DateTime? prev = null;
foreach (var pt in ts)
{
if (prev.HasValue)
Assert.IsGreaterThanOrEqualTo(prev.Value, pt.Index, "Dates not sorted chronologically.");
prev = pt.Index;
}
// Ensure all indices are unique
Assert.AreEqual(ts.Count, ts.Select(o => o.Index).Distinct().Count(), "Duplicate date indices detected.");
}
/// <summary>
/// Compares two numeric values within specified relative and absolute tolerances.
/// </summary>
/// <param name="a">First value.</param>
/// <param name="b">Second value.</param>
/// <param name="relTol">Relative tolerance (default = 1e-6).</param>
/// <param name="absTol">Absolute tolerance (default = 1e-9).</param>
private static void AssertRoughlyEqual(double a, double b, double relTol = 1e-6, double absTol = 1e-9)
{
if (double.IsNaN(a) && double.IsNaN(b)) return;
double diff = Math.Abs(a - b);
if (diff <= absTol) return;
double denom = Math.Max(Math.Abs(a), Math.Abs(b));
if (denom == 0)
Assert.IsLessThanOrEqualTo(absTol,diff);
else
Assert.IsLessThanOrEqualTo(relTol, diff / denom);
}
#endregion
#region CHMN (Canada) Tests
/// <summary>
/// Validates a full-period-of-record download for the CHMN Cold River station (flow).
/// </summary>
[TestMethod, TestCategory("Integration")]
public async Task CHMN_FullPor_ColdRiver_Flow()
{
if (!await Online()) return;
var ts = await TimeSeriesDownload.FromCHMN(CHMN_1);
AssertDailySeriesMonotonic(ts);
}
/// <summary>
/// Validates a full-period-of-record download for the CHMN Lillooet River station (flow).
/// </summary>
[TestMethod, TestCategory("Integration")]
public async Task CHMN_FullPor_Lillooet_Flow()
{
if (!await Online()) return;
var ts = await TimeSeriesDownload.FromCHMN(CHMN_2);
AssertDailySeriesMonotonic(ts);
}
/// <summary>
/// Validates a full-period-of-record download for the CHMN Capilano River station (flow).
/// </summary>
[TestMethod, TestCategory("Integration")]
public async Task CHMN_FullPor_Capilano_Flow()
{
if (!await Online()) return;
var ts = await TimeSeriesDownload.FromCHMN(CHMN_3);
AssertDailySeriesMonotonic(ts);
}
/// <summary>
/// Tests flow unit conversions (cms ↔ cfs) for CHMN data.
/// </summary>
[TestMethod, TestCategory("Integration")]
public async Task CHMN_UnitConversion_Flow_CmsCfs()
{
if (!await Online()) return;
var tsCms = await TimeSeriesDownload.FromCHMN(CHMN_1,
TimeSeriesDownload.TimeSeriesType.DailyDischarge,
TimeSeriesDownload.DischargeUnit.CubicMetersPerSecond,
startDate: WinStart, endDate: WinEnd);
var tsCfs = await TimeSeriesDownload.FromCHMN(CHMN_1,
TimeSeriesDownload.TimeSeriesType.DailyDischarge,
TimeSeriesDownload.DischargeUnit.CubicFeetPerSecond,
startDate: WinStart, endDate: WinEnd);
const double factor = 35.3146667;
for (int i = 0; i < tsCms.Count; i++)
{
if (double.IsNaN(tsCms[i].Value) || double.IsNaN(tsCfs[i].Value)) continue;
AssertRoughlyEqual(tsCfs[i].Value, tsCms[i].Value * factor);
}
}
/// <summary>
/// Tests stage unit conversions (m ↔ ft) for CHMN data.
/// </summary>
[TestMethod, TestCategory("Integration")]
public async Task CHMN_UnitConversion_Stage_MFt()
{
if (!await Online()) return;
var tsM = await TimeSeriesDownload.FromCHMN(CHMN_1,
TimeSeriesDownload.TimeSeriesType.DailyStage,
heightUnit: TimeSeriesDownload.HeightUnit.Meters,
startDate: WinStart, endDate: WinEnd);
var tsFt = await TimeSeriesDownload.FromCHMN(CHMN_1,
TimeSeriesDownload.TimeSeriesType.DailyStage,
heightUnit: TimeSeriesDownload.HeightUnit.Feet,
startDate: WinStart, endDate: WinEnd);
const double factor = 3.280839895;
for (int i = 0; i < tsM.Count; i++)
{
if (double.IsNaN(tsM[i].Value) || double.IsNaN(tsFt[i].Value)) continue;
AssertRoughlyEqual(tsFt[i].Value, tsM[i].Value * factor);
}
}
/// <summary>
/// Ensures CHMN rejects invalid station identifiers.
/// </summary>
[TestMethod]
public async Task CHMN_InvalidStation_Throws()
{
await Assert.ThrowsAsync<ArgumentException>(async () =>
await TimeSeriesDownload.FromCHMN("08LG01"));
}
/// <summary>
/// Ensures CHMN rejects unsupported time series types (field measurements not available).
/// </summary>
[TestMethod]
public async Task CHMN_UnsupportedType_Throws()
{
await Assert.ThrowsAsync<ArgumentException>(async () =>
await TimeSeriesDownload.FromCHMN(CHMN_1,
TimeSeriesDownload.TimeSeriesType.MeasuredDischarge));
}
/// <summary>
/// Tests CHMN instantaneous discharge download (real-time 5-minute data).
/// </summary>
[TestMethod, TestCategory("Integration")]
public async Task CHMN_InstantaneousDischarge_Works()
{
if (!await Online()) return;
var ts = await TimeSeriesDownload.FromCHMN(CHMN_2,
TimeSeriesDownload.TimeSeriesType.InstantaneousDischarge);
Assert.IsNotNull(ts, "Time series is null.");
Assert.IsGreaterThan(0, ts.Count);
}
/// <summary>
/// Tests CHMN instantaneous stage download (real-time 5-minute data).
/// </summary>
[TestMethod, TestCategory("Integration")]
public async Task CHMN_InstantaneousStage_Works()
{
if (!await Online()) return;
var ts = await TimeSeriesDownload.FromCHMN(CHMN_2,
TimeSeriesDownload.TimeSeriesType.InstantaneousStage);
Assert.IsNotNull(ts, "Time series is null.");
Assert.IsGreaterThan(0, ts.Count);
}
/// <summary>
/// Tests CHMN peak discharge download (annual instantaneous maximums).
/// </summary>
[TestMethod, TestCategory("Integration")]
public async Task CHMN_PeakDischarge_Works()
{
if (!await Online()) return;
var ts = await TimeSeriesDownload.FromCHMN(CHMN_1,
TimeSeriesDownload.TimeSeriesType.PeakDischarge);
Assert.IsNotNull(ts, "Time series is null.");
Assert.IsGreaterThan(0, ts.Count);
}
/// <summary>
/// Tests CHMN peak stage download (annual instantaneous maximums).
/// </summary>
[TestMethod, TestCategory("Integration")]
public async Task CHMN_PeakStage_Works()
{
if (!await Online()) return;
var ts = await TimeSeriesDownload.FromCHMN(CHMN_1,
TimeSeriesDownload.TimeSeriesType.PeakStage);
Assert.IsNotNull(ts, "Time series is null.");
Assert.IsGreaterThan(0, ts.Count);
}
#endregion
#region USGS Tests
/// <summary>
/// Tests full-period-of-record USGS daily discharge download.
/// </summary>
[TestMethod, TestCategory("Integration")]
public async Task USGS_FullPor_DailyDischarge()
{
if (!await Online()) return;
var (ts, raw) = await TimeSeriesDownload.FromUSGS(USGS_1);
AssertDailySeriesMonotonic(ts);
}
/// <summary>
/// Tests USGS daily stage download for correctness and continuity.
/// </summary>
[TestMethod, TestCategory("Integration")]
public async Task USGS_FullPor_DailyStage()
{
if (!await Online()) return;
var (ts, _) = await TimeSeriesDownload.FromUSGS(USGS_4, TimeSeriesDownload.TimeSeriesType.DailyStage);
AssertDailySeriesMonotonic(ts);
}
/// <summary>
/// Tests USGS peak discharge data retrieval for non-daily data.
/// </summary>
[TestMethod, TestCategory("Integration")]
public async Task USGS_PeakDischarge_Works()
{
if (!await Online()) return;
var (ts, raw) = await TimeSeriesDownload.FromUSGS(USGS_3,
TimeSeriesDownload.TimeSeriesType.PeakDischarge);
Assert.IsNotNull(ts, "Time series is null.");
Assert.IsNotNull(raw, "Raw text is null.");
}
/// <summary>
/// Ensures USGS rejects invalid station identifiers.
/// </summary>
[TestMethod]
public async Task USGS_InvalidStation_Throws()
{
await Assert.ThrowsAsync<ArgumentException>(async () =>
await TimeSeriesDownload.FromUSGS("1134500"));
}
/// <summary>
/// Ensures USGS rejects unsupported precipitation and snow types.
/// </summary>
[TestMethod]
public async Task USGS_UnsupportedType_Throws()
{
await Assert.ThrowsAsync<ArgumentException>(async () =>
await TimeSeriesDownload.FromUSGS(USGS_1,
TimeSeriesDownload.TimeSeriesType.DailyPrecipitation));
}
/// <summary>
/// Tests USGS field measurement discharge data retrieval from the OGC API.
/// </summary>
[TestMethod, TestCategory("Integration")]
public async Task USGS_MeasuredDischarge_Works()
{
if (!await Online()) return;
var (ts, raw) = await TimeSeriesDownload.FromUSGS(USGS_5,
TimeSeriesDownload.TimeSeriesType.MeasuredDischarge);
Assert.IsNotNull(ts, "Time series is null.");
Assert.IsNotNull(raw, "Raw text is null.");
Assert.IsGreaterThan(0, ts.Count);
Assert.IsFalse(string.IsNullOrEmpty(raw), "Raw text should contain JSON response.");
// Verify all values are positive (discharge must be > 0)
foreach (var pt in ts)
{
Assert.IsGreaterThan(0, pt.Value, $"Discharge value {pt.Value} at {pt.Index} should be positive.");
}
}
/// <summary>
/// Tests USGS field measurement stage (gage height) data retrieval from the OGC API.
/// </summary>
[TestMethod, TestCategory("Integration")]
public async Task USGS_MeasuredStage_Works()
{
if (!await Online()) return;
var (ts, raw) = await TimeSeriesDownload.FromUSGS(USGS_5,
TimeSeriesDownload.TimeSeriesType.MeasuredStage);
Assert.IsNotNull(ts, "Time series is null.");
Assert.IsNotNull(raw, "Raw text is null.");
Assert.IsGreaterThan(0, ts.Count);
Assert.IsFalse(string.IsNullOrEmpty(raw), "Raw text should contain JSON response.");
// Verify all values are positive (gage height must be > 0)
foreach (var pt in ts)
{
Assert.IsGreaterThan(0, pt.Value, $"Gage height value {pt.Value} at {pt.Index} should be positive.");
}
}
/// <summary>
/// Tests USGS peak stage data retrieval.
/// </summary>
[TestMethod, TestCategory("Integration")]
public async Task USGS_PeakStage_Works()
{
if (!await Online()) return;
var (ts, raw) = await TimeSeriesDownload.FromUSGS(USGS_3,
TimeSeriesDownload.TimeSeriesType.PeakStage);
Assert.IsNotNull(ts, "Time series is null.");
Assert.IsNotNull(raw, "Raw text is null.");
Assert.IsGreaterThan(0, ts.Count);
}
/// <summary>
/// Tests USGS instantaneous discharge download.
/// </summary>
[TestMethod, TestCategory("Integration")]
public async Task USGS_InstantaneousDischarge_Works()
{
if (!await Online()) return;
var (ts, _) = await TimeSeriesDownload.FromUSGS(USGS_6,
TimeSeriesDownload.TimeSeriesType.InstantaneousDischarge);
Assert.IsNotNull(ts, "Time series is null.");
Assert.IsGreaterThan(0, ts.Count);
}
/// <summary>
/// Tests USGS instantaneous stage download.
/// </summary>
[TestMethod, TestCategory("Integration")]
public async Task USGS_InstantaneousStage_Works()
{
if (!await Online()) return;
var (ts, _) = await TimeSeriesDownload.FromUSGS(USGS_6,
TimeSeriesDownload.TimeSeriesType.InstantaneousStage);
Assert.IsNotNull(ts, "Time series is null.");
Assert.IsGreaterThan(0, ts.Count);
}
#endregion
#region GHCN Tests
/// <summary>
/// Tests full-period-of-record GHCN daily precipitation download.
/// </summary>
[TestMethod, TestCategory("Integration")]
public async Task GHCN_FullPor_Precipitation()
{
if (!await Online()) return;
var ts = await TimeSeriesDownload.FromGHCN(GHCN_1);
AssertDailySeriesMonotonic(ts);
}
/// <summary>
/// Tests full-period-of-record GHCN daily snow download.
/// </summary>
[TestMethod, TestCategory("Integration")]
public async Task GHCN_FullPor_Snow()
{
if (!await Online()) return;
var ts = await TimeSeriesDownload.FromGHCN(GHCN_3,
TimeSeriesDownload.TimeSeriesType.DailySnow);
if (ts.Count > 0) AssertDailySeriesMonotonic(ts);
}
/// <summary>
/// Tests precipitation unit conversion between millimeters and inches.
/// </summary>
[TestMethod, TestCategory("Integration")]
public async Task GHCN_UnitConversion_Mm_In()
{
if (!await Online()) return;
var tsMm = await TimeSeriesDownload.FromGHCN(GHCN_2,
TimeSeriesDownload.TimeSeriesType.DailyPrecipitation,
TimeSeriesDownload.DepthUnit.Millimeters);
var tsIn = await TimeSeriesDownload.FromGHCN(GHCN_2,
TimeSeriesDownload.TimeSeriesType.DailyPrecipitation,
TimeSeriesDownload.DepthUnit.Inches);
const double factor = 25.4;
for (int i = 0; i < Math.Min(tsMm.Count, tsIn.Count); i++)
{
if (double.IsNaN(tsMm[i].Value) || double.IsNaN(tsIn[i].Value)) continue;
AssertRoughlyEqual(tsIn[i].Value, tsMm[i].Value / factor);
}
}
/// <summary>
/// Tests precipitation unit conversion between millimeters and centimeters.
/// </summary>
[TestMethod, TestCategory("Integration")]
public async Task GHCN_UnitConversion_Mm_Cm()
{
if (!await Online()) return;
var tsMm = await TimeSeriesDownload.FromGHCN(GHCN_1,
TimeSeriesDownload.TimeSeriesType.DailyPrecipitation,
TimeSeriesDownload.DepthUnit.Millimeters);
var tsCm = await TimeSeriesDownload.FromGHCN(GHCN_1,
TimeSeriesDownload.TimeSeriesType.DailyPrecipitation,
TimeSeriesDownload.DepthUnit.Centimeters);
for (int i = 0; i < Math.Min(tsMm.Count, tsCm.Count); i++)
{
if (double.IsNaN(tsMm[i].Value) || double.IsNaN(tsCm[i].Value)) continue;
AssertRoughlyEqual(tsCm[i].Value, tsMm[i].Value / 10.0);
}
}
/// <summary>
/// Ensures GHCN rejects invalid station identifiers.
/// </summary>
[TestMethod]
public async Task GHCN_InvalidStation_Throws()
{
await Assert.ThrowsAsync<ArgumentException>(async () =>
await TimeSeriesDownload.FromGHCN("USC0004074"));
}
/// <summary>
/// Ensures GHCN rejects unsupported stage-type requests.
/// </summary>
[TestMethod]
public async Task GHCN_UnsupportedType_Throws()
{
await Assert.ThrowsAsync<ArgumentException>(async () =>
await TimeSeriesDownload.FromGHCN(GHCN_1,
TimeSeriesDownload.TimeSeriesType.DailyStage));
}
#endregion
#region BOM (Australia) Tests
/// <summary>
/// Validates a full-period-of-record download for the BOM Cotter River station (discharge).
/// </summary>
[TestMethod, TestCategory("Integration")]
public async Task BOM_FullPor_CotterRiver_Discharge()
{
if (!await Online()) return;
var ts = await TimeSeriesDownload.FromABOM(BOM_1);
AssertDailySeriesMonotonic(ts);
}
/// <summary>
/// Validates a full-period-of-record download for the BOM Goodradigbee River station (discharge).
/// </summary>
[TestMethod, TestCategory("Integration")]
public async Task BOM_FullPor_Goodradigbee_Discharge()
{
if (!await Online()) return;
var ts = await TimeSeriesDownload.FromABOM(BOM_2);
AssertDailySeriesMonotonic(ts);
}
/// <summary>
/// Validates a full-period-of-record download for the BOM Murray River station (stage).
/// </summary>
[TestMethod, TestCategory("Integration")]
public async Task BOM_FullPor_MurrayRiver_Stage()
{
if (!await Online()) return;
var ts = await TimeSeriesDownload.FromABOM(BOM_3, TimeSeriesDownload.TimeSeriesType.DailyStage);
AssertDailySeriesMonotonic(ts);
}
/// <summary>
/// Tests discharge unit conversions (cms ↔ cfs) for BOM data.
/// </summary>
[TestMethod, TestCategory("Integration")]
public async Task BOM_UnitConversion_Discharge_CmsCfs()
{
if (!await Online()) return;
var tsCms = await TimeSeriesDownload.FromABOM(BOM_1,
TimeSeriesDownload.TimeSeriesType.DailyDischarge,
TimeSeriesDownload.DischargeUnit.CubicMetersPerSecond,
startDate: WinStart, endDate: WinEnd);
var tsCfs = await TimeSeriesDownload.FromABOM(BOM_1,
TimeSeriesDownload.TimeSeriesType.DailyDischarge,
TimeSeriesDownload.DischargeUnit.CubicFeetPerSecond,
startDate: WinStart, endDate: WinEnd);
const double factor = 35.3146667;
for (int i = 0; i < tsCms.Count; i++)
{
if (double.IsNaN(tsCms[i].Value) || double.IsNaN(tsCfs[i].Value)) continue;
AssertRoughlyEqual(tsCfs[i].Value, tsCms[i].Value * factor);
}
}
/// <summary>
/// Tests stage unit conversions (m ↔ ft) for BOM data.
/// </summary>
[TestMethod, TestCategory("Integration")]
public async Task BOM_UnitConversion_Stage_MFt()
{
if (!await Online()) return;
var tsM = await TimeSeriesDownload.FromABOM(BOM_3,
TimeSeriesDownload.TimeSeriesType.DailyStage,
heightUnit: TimeSeriesDownload.HeightUnit.Meters,
startDate: WinStart, endDate: WinEnd);
var tsFt = await TimeSeriesDownload.FromABOM(BOM_3,
TimeSeriesDownload.TimeSeriesType.DailyStage,
heightUnit: TimeSeriesDownload.HeightUnit.Feet,
startDate: WinStart, endDate: WinEnd);
const double factor = 3.280839895;
for (int i = 0; i < tsM.Count; i++)
{
if (double.IsNaN(tsM[i].Value) || double.IsNaN(tsFt[i].Value)) continue;
AssertRoughlyEqual(tsFt[i].Value, tsM[i].Value * factor);
}
}
/// <summary>
/// Ensures BOM rejects invalid station identifiers.
/// </summary>
[TestMethod]
public async Task BOM_InvalidStation_Throws()
{
await Assert.ThrowsAsync<ArgumentException>(async () =>
await TimeSeriesDownload.FromABOM("41073"));
}
/// <summary>
/// Ensures BOM rejects unsupported time series types.
/// </summary>
[TestMethod]
public async Task BOM_UnsupportedType_Throws()
{
await Assert.ThrowsAsync<ArgumentException>(async () =>
await TimeSeriesDownload.FromABOM(BOM_1,
TimeSeriesDownload.TimeSeriesType.PeakDischarge));
}
/// <summary>
/// Tests BOM with a windowed date range to verify date filtering works correctly.
/// </summary>
[TestMethod, TestCategory("Integration")]
public async Task BOM_WindowedDownload_Works()
{
if (!await Online()) return;
var ts = await TimeSeriesDownload.FromABOM(BOM_1,
TimeSeriesDownload.TimeSeriesType.DailyDischarge,
startDate: WinStart, endDate: WinEnd);
Assert.IsNotNull(ts, "Time series is null.");
Assert.IsGreaterThan(0, ts.Count);
// Verify data is within requested window (allowing for some timezone flexibility)
var firstDate = ts.First().Index;
var lastDate = ts.Last().Index;
Assert.IsGreaterThanOrEqualTo(WinStart.AddDays(-1), firstDate,
$"First date {firstDate} is before window start {WinStart}");
Assert.IsLessThanOrEqualTo(WinEnd.AddDays(1), lastDate,
$"Last date {lastDate} is after window end {WinEnd}");
}
/// <summary>
/// Validates instantaneous discharge download from BOM.
/// </summary>
[TestMethod, TestCategory("Integration")]
public async Task BOM_InstantaneousDischarge_Works()
{
if (!await Online()) return;
var ts = await TimeSeriesDownload.FromABOM(BOM_1,
TimeSeriesDownload.TimeSeriesType.InstantaneousDischarge,
startDate: WinStart, endDate: WinEnd);
Assert.IsNotNull(ts, "Time series is null.");
Assert.IsGreaterThan(0, ts.Count);
}
/// <summary>
/// Validates instantaneous stage download from BOM.
/// </summary>
[TestMethod, TestCategory("Integration")]
public async Task BOM_InstantaneousStage_Works()
{
if (!await Online()) return;
var ts = await TimeSeriesDownload.FromABOM(BOM_3,
TimeSeriesDownload.TimeSeriesType.InstantaneousStage,
startDate: WinStart, endDate: WinEnd);
Assert.IsNotNull(ts, "Time series is null.");
Assert.IsGreaterThan(0, ts.Count);
}
/// <summary>
/// Validates daily precipitation download from BOM.
/// </summary>
[TestMethod, TestCategory("Integration")]
public async Task BOM_DailyPrecipitation_Works()
{
if (!await Online()) return;
var ts = await TimeSeriesDownload.FromABOM(BOM_1,
TimeSeriesDownload.TimeSeriesType.DailyPrecipitation,
startDate: WinStart, endDate: WinEnd);
Assert.IsNotNull(ts, "Time series is null.");
Assert.IsGreaterThan(0, ts.Count);
}
/// <summary>
/// Tests precipitation unit conversions (mm ↔ inches) for BOM data.
/// </summary>
[TestMethod, TestCategory("Integration")]
public async Task BOM_UnitConversion_Precip_MmIn()
{
if (!await Online()) return;
var tsMm = await TimeSeriesDownload.FromABOM(BOM_1,
TimeSeriesDownload.TimeSeriesType.DailyPrecipitation,
depthUnit: TimeSeriesDownload.DepthUnit.Millimeters,
startDate: WinStart, endDate: WinEnd);
var tsIn = await TimeSeriesDownload.FromABOM(BOM_1,
TimeSeriesDownload.TimeSeriesType.DailyPrecipitation,
depthUnit: TimeSeriesDownload.DepthUnit.Inches,
startDate: WinStart, endDate: WinEnd);
const double factor = 25.4;
for (int i = 0; i < tsMm.Count; i++)
{
if (double.IsNaN(tsMm[i].Value) || double.IsNaN(tsIn[i].Value)) continue;
if (tsMm[i].Value == 0 && tsIn[i].Value == 0) continue;
AssertRoughlyEqual(tsMm[i].Value, tsIn[i].Value * factor);
}
}
#endregion
}
}