-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
2446 lines (1961 loc) · 105 KB
/
MainWindow.xaml.cs
File metadata and controls
2446 lines (1961 loc) · 105 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
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
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.IO;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Shell;
using YamlDotNet.Serialization;
using LiveCharts;
using LiveCharts.Wpf;
// Added
using Microsoft.Win32;
// Custom
using static SWAT__Toolbox.observations_classes;
using static SWAT__Toolbox.observations_functions;
using static SWAT__Toolbox.parameters_module;
using static SWAT__Toolbox.toolbox_functions;
using static SWAT__Toolbox.home_module;
using static SWAT__Toolbox.run_model_module;
using static SWAT__Toolbox.model_evaluation_module;
using static SWAT__Toolbox.mannual_calibration_module;
using static SWAT__Toolbox.sensitivity_analysis_module;
using Path = System.IO.Path;
using YamlDotNet.RepresentationModel;
using YamlDotNet.Serialization.NamingConventions;
using System.Diagnostics;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using MaterialDesignThemes.Wpf;
using System.Globalization;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Media.Media3D;
using MaterialDesignColors;
using System.ComponentModel;
using System.Windows.Threading;
using LiveCharts.Defaults;
using System.Data;
namespace SWAT__Toolbox
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
ObservableCollection<parameter> selected_parameters;
ObservableCollection<recent_project> recent_projects;
ObservableCollection<observation> selected_observations;
project current_project = new project();
public SeriesCollection SeriesCollection { get; set; }
public SeriesCollection SeriesCollection_auto { get; set; }
public string[] Labels { get; set; }
public string[] Labels_auto { get; set; }
public string active_tab = "home";
public SeriesCollection last_performance_range { get; set; }
public MainWindow()
{
InitializeComponent();
SeriesCollection = new SeriesCollection
{
new LineSeries
{
Title = "Observations",
Values = new ChartValues<double> { },
PointGeometry = null
},
new LineSeries
{
Title = "Simulations",
Values = new ChartValues<double> {},
PointGeometry = null
},
};
DataContext = this;
SeriesCollection_auto = new SeriesCollection
{
new LineSeries
{
Title = "Observations",
Values = new ChartValues<double> { },
PointGeometry = null
},
new LineSeries
{
Title = "Simulations",
Values = new ChartValues<double> { },
PointGeometry = null
},
};
last_performance_range = new SeriesCollection
{
new LineSeries
{
AreaLimit = -10,
Values = new ChartValues<ObservableValue>
{
new ObservableValue(3),
new ObservableValue(20),
new ObservableValue(6),
new ObservableValue(7),
new ObservableValue(3),
new ObservableValue(4),
new ObservableValue(2),
new ObservableValue(5),
new ObservableValue(20),
new ObservableValue(2),
new ObservableValue(10),
new ObservableValue(6),
new ObservableValue(7),
new ObservableValue(3),
new ObservableValue(4),
new ObservableValue(2),
new ObservableValue(5),
new ObservableValue(8)
}
}
};
DataContext = this;
active_tab = "home";
ui_sensitivity_is_running.Visibility = Visibility.Hidden;
ui_calibration_automatic_is_running.Visibility = Visibility.Collapsed;
// set column number format
ui_model_check_hydrology_image.Source = new BitmapImage(new Uri($@"{Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory)}\\assets\\analysis_hydrology.dll"));
//ui init
WindowChrome.SetWindowChrome(this, new WindowChrome());
var paletteHelper = new PaletteHelper();
//Retrieve the app's existing theme
ITheme theme = paletteHelper.GetTheme();
//Change the base theme to Ligh
theme.SetBaseTheme(Theme.Light);
// //Change all of the primary colors to Red
// theme.SetPrimaryColor(System.Windows.Media.Color.FromRgb(21, 101, 192));
//
//Change all of the secondary colors to Orange
theme.SetSecondaryColor(Colors.Orange);
// //You can also change a single color on the theme, and optionally set the corresponding foreground color
// theme.PrimaryMid = new ColorPair(Colors.Brown, Colors.White);
//Change the app's current theme
paletteHelper.SetTheme(theme);
//set up nutrients
ui_model_check_nutrients_image.Source = new BitmapImage(new Uri($@"{Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory)}\\assets\\analysis_nutrients.dll"));
//set up water balance
ui_model_check_hydrology_image.Source = new BitmapImage(new Uri($@"{Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory)}\\assets\\analysis_hydrology.dll"));
ui_model_check_pages.SelectedIndex = 1;
selected_parameters = new ObservableCollection<parameter>();
selected_observations = new ObservableCollection<observation>();
recent_projects = new ObservableCollection<recent_project>();
ui_selected_parameters.ItemsSource = selected_parameters;
ui_selected_observations.ItemsSource = selected_observations;
ui_calibration_manual_performance.ItemsSource = current_project.current_observations;
ui_calibration_automatic_performance.ItemsSource = current_project.current_observations;
this.DataContext = this;
// user doubleclicked project file
string[] args = Environment.GetCommandLineArgs();
if (args.Length > 1)
{
current_project = new project();
try
{
ui_calibration_automatic_observation_selection.SelectedIndex = current_project.selected_eval_index;
ui_sensitivity_observation_selection.SelectedIndex = current_project.selected_eval_index;
}
catch (Exception)
{
}
// try to open an existing project, ignore if cancelled
using (StreamReader streamReader = new StreamReader(args[1]))
{
var deserializer = new DeserializerBuilder()
.Build();
//Deserializer deserializer = new Deserializer();
current_project = (project)deserializer.Deserialize(streamReader, typeof(project));
}
ui_home_current_project_name.Visibility = Visibility.Visible;
ui_home_current_project_name.Text = $@"{current_project.project_name}";
ui_home_current_project_name_titlebar.Visibility = Visibility.Visible;
ui_home_current_project_name_titlebar.Text = $@" - {current_project.project_name}";
ui_home_current_project_txtinout.Visibility = Visibility.Visible;
ui_home_current_project_txtinout.Text = $@"{current_project.txtinout}";
selected_parameters = current_project.current_parameters;
selected_observations = current_project.current_observations;
ui_selected_parameters.ItemsSource = selected_parameters;
ui_selected_observations.ItemsSource = selected_observations;
ui_calibration_manual_performance.ItemsSource = current_project.current_observations;
ui_calibration_automatic_performance.ItemsSource = current_project.current_observations;
//update ui
//home
//run_model
ui_run_model_start_date.SelectedDate = current_project.run_date_start;
ui_run_model_end_date.SelectedDate = current_project.run_date_end;
ui_run_model_warmup_period.Text = current_project.run_warmup.ToString();
//load recents
// try to open an existing file, create if it does not exist
//binding checkboxes for printing
set_binding_contexts();
}
try // see if you can read an existing file if not, you can skip
{
using (StreamReader streamReader = new StreamReader(
$@"{Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)}\SWAT+ Toolbox\recents.dll"
))
{
var deserializer = new DeserializerBuilder()
.Build();
//Deserializer deserializer = new Deserializer();
recent_projects = (ObservableCollection<recent_project>)deserializer.Deserialize(streamReader, typeof(ObservableCollection<recent_project>));
}
recent_projects = sort_recents(recent_projects, current_project);
ui_home_recent_listdatagrid.ItemsSource = recent_projects;
ui_home_recent_listdatagrid.DataContext = recent_projects;
ui_home_recent_listdatagrid.Items.Refresh();
recent_projects = sort_recents(recent_projects, current_project);
}
catch (Exception)
{
}
}
private void WindowMinimise(object sender, RoutedEventArgs e)
{
this.WindowState = WindowState.Minimized;
}
private void RestoreMaximise(object sender, RoutedEventArgs e)
{
if (this.WindowState == WindowState.Normal)
{
this.WindowState = WindowState.Maximized;
}
else
{
this.WindowState = WindowState.Normal;
}
}
private void CloseWindow(object sender, RoutedEventArgs e)
{
this.Close();
}
private void navigate_homepage(object sender, RoutedEventArgs e)
{
pages.SelectedIndex = 0;
active_tab = "home";
ui_master_progress_bar.Visibility = Visibility.Visible;
}
private void navigate_runmodel(object sender, RoutedEventArgs e)
{
pages.SelectedIndex = 1;
if (current_project.project_name == null)
{
ui_run_model_grid.IsEnabled = false;
} else
{
ui_run_model_grid.IsEnabled = true;
}
active_tab = "run_model";
ui_master_progress_bar.Visibility = Visibility.Collapsed;
}
private void navigate_parameters(object sender, RoutedEventArgs e)
{
pages.SelectedIndex = 2;
if (current_project.project_name == null)
{
ui_parameters_grid.IsEnabled = false;
}
else
{
ui_parameters_grid.IsEnabled = true;
}
active_tab = "parameters";
ui_master_progress_bar.Visibility = Visibility.Visible;
}
private void navigate_observations(object sender, RoutedEventArgs e)
{
pages.SelectedIndex = 3;
if (current_project.project_name == null)
{
ui_observations_grid.IsEnabled = false;
}
else
{
ui_observations_grid.IsEnabled = true;
}
active_tab = "observations";
ui_master_progress_bar.Visibility = Visibility.Visible;
}
private void navigate_sensitivity(object sender, RoutedEventArgs e)
{
pages.SelectedIndex = 4;
active_tab = "sensitivity";
ui_master_progress_bar.Visibility = Visibility.Visible;
if (current_project.project_name == null)
{
ui_sensitivity_grid.IsEnabled = false;
}
else
{
ui_sensitivity_grid.IsEnabled = true;
if (ui_sensitivity_observation_selection.SelectedItem == null)
{
try
{
ui_sensitivity_observation_selection.SelectedIndex = 0;
}
catch (Exception)
{
}
}
}
}
private void navigate_calibration(object sender, RoutedEventArgs e)
{
pages.SelectedIndex = 5;
active_tab = "calibration";
ui_master_progress_bar.Visibility = Visibility.Visible;
if (current_project.project_name == null)
{
ui_calibration_grid.IsEnabled = false;
}
else
{
ui_calibration_grid.IsEnabled = true;
if (ui_calibration_automatic_observation_selection.SelectedItem == null)
{
try
{
ui_calibration_automatic_observation_selection.SelectedIndex = 0;
}
catch (Exception)
{
}
}
}
}
private void navigate_model_check(object sender, RoutedEventArgs e)
{
pages.SelectedIndex = 6;
active_tab = "model_check";
ui_master_progress_bar.Visibility = Visibility.Visible;
if (current_project.project_name == null)
{
ui_model_check_grid.IsEnabled = false;
}
else
{
ui_model_check_grid.IsEnabled = true;
}
}
//parameters page block
// mapping info https://github.com/ThinkGeo/Desktop-Maps
private void list_available_parameters(object sender, EventArgs e)
{
// All, Hydrologic Response Unit, Aquifer, Routing, HRU LTE, Soil, Reservoir, SWQ
ui_parameter_name.ItemsSource = null;
ui_parameter_name.Items.Clear();
ui_parameter_name.DisplayMemberPath = "name";
ui_parameter_name.SelectedValuePath = "name";
if (ui_parameter_group.SelectedIndex == 0)
{
ui_parameter_name.ItemsSource = all_parameters();
}
else if (ui_parameter_group.SelectedIndex == 1)
{
ui_parameter_name.ItemsSource = hru_parameters();
}
else if (ui_parameter_group.SelectedIndex == 2)
{
ui_parameter_name.ItemsSource = aqu_parameters();
}
else if (ui_parameter_group.SelectedIndex == 3)
{
ui_parameter_name.ItemsSource = rte_parameters();
}
else if (ui_parameter_group.SelectedIndex == 4)
{
ui_parameter_name.ItemsSource = hlt_parameters();
}
else if (ui_parameter_group.SelectedIndex == 5)
{
ui_parameter_name.ItemsSource = sol_parameters();
}
else if (ui_parameter_group.SelectedIndex == 6)
{
ui_parameter_name.ItemsSource = res_parameters();
}
else if (ui_parameter_group.SelectedIndex == 7)
{
ui_parameter_name.ItemsSource = swq_parameters();
}
else
{
}
}
private void pick_observation_file(object sender, RoutedEventArgs e)
{
string file_name = pick_file("Comma Separated|*.csv", "Select and Observation File", "Select");
if (file_name == "")
{
ui_parameters_file_selection_path.Text = "Click to select an observation file";
return;
}
else
{
ui_parameters_file_selection_path.Text = file_name;
}
}
private void clear_observation_file(object sender, RoutedEventArgs e)
{
ui_parameters_file_selection_path.Text = "Click to select an observation file";
}
private void add_observation(object sender, RoutedEventArgs e)
{
// check user input for errors
// block other observed variables
if (ui_observations_observed_variable.SelectedIndex > 2)
{
//select the observed model component
System.Windows.MessageBox.Show($@"{ui_observations_observed_variable.Items[ui_observations_observed_variable.SelectedIndex].ToString().Split(':').Last()} is not yet supported in SWAT+ Toolbox");
return;
}
if (ui_observations_object_type.SelectedIndex < 0)
{
//select the observed model component
System.Windows.MessageBox.Show("Select observation Object Type");
return;
}
int obj_number = 0; // initialising
try
{
obj_number = int.Parse(ui_observations_object_number.Text);
// to do: check if the number really exists in the model
}
catch (Exception)
{
//not valid object number
System.Windows.MessageBox.Show("Object Number is not valid");
return;
}
if (ui_parameters_file_selection_path.Text == "Click to select an observation file")
{
// please chose a file
System.Windows.MessageBox.Show("An Observation File is required");
return;
}
if (ui_observations_timestep.SelectedIndex < 0)
{
//select a timestep
System.Windows.MessageBox.Show("Please, select the timestep for the observations");
return;
}
if (ui_observations_observed_variable.SelectedIndex < 0)
{
//select an observation variable
System.Windows.MessageBox.Show("You must specify the observed variable");
return;
}
//get objects values from user
string obj_type = get_observation_object_type(ui_observations_object_type.SelectedIndex + 1);
string obs_variable = get_observation_variable(ui_observations_observed_variable.SelectedIndex + 1);
string timestep = ui_observations_timestep.SelectionBoxItem.ToString();
selected_observations.Add(new observation()
{
file = ui_parameters_file_selection_path.Text,
id = 1,
number = obj_number,
object_type = obj_type,
observed_variable = obs_variable,
timestep = timestep,
chart_name = $@"{obj_type} {obj_number} {timestep} {obs_variable}",
nse = 0,
pbias = 0,
r2 = 0
});
ui_observations_object_type.Text = null;
ui_observations_observed_variable.Text = null;
ui_observations_object_number.Text = null;
ui_observations_timestep.Text = null;
ui_observations_object_number.Text = null;
ui_parameters_file_selection_path.Text = "Click to select an observation file";
current_project.current_observations = selected_observations;
update_project();
}
private void add_parameter(object sender, RoutedEventArgs e)
{
string par_obj = (string)ui_parameter_name.SelectedValue;
if (par_obj == null)
{
System.Windows.MessageBox.Show($@"You must chose a parameter first{Environment.NewLine}Chose a Parameter Group and select{Environment.NewLine}Parameter Name");
return;
}
if (ui_parameter_change_type.SelectedIndex < 0)
{
System.Windows.MessageBox.Show($@"Please select the change type{Environment.NewLine}for {par_obj}");
return;
}
foreach (var par_item in all_parameters())
{
if (par_obj == par_item.name)
{
parameter additional_parameter = par_item;
additional_parameter.change_type = get_parameter_change_type(ui_parameter_change_type.SelectedIndex + 1);
try
{
additional_parameter.minimum = double.Parse(ui_parameter_minimum.Text);
}
catch (Exception)
{
System.Windows.MessageBox.Show($@"Minimum value for {par_obj}{Environment.NewLine}is not valid");
return;
}
try
{
additional_parameter.maximum = double.Parse(ui_parameter_maximum.Text);
}
catch (Exception)
{
System.Windows.MessageBox.Show($@"Maximum value for {par_obj}{Environment.NewLine}is not valid");
return;
}
selected_parameters.Add(additional_parameter);
}
else
{
}
}
ui_parameter_maximum.Text = null;
ui_parameter_minimum.Text = null;
ui_parameter_name.Text = null;
ui_parameter_change_type.Text = null;
current_project.current_parameters = selected_parameters;
try
{
ui_sensitivity_number_of_samples.Text = $@"Number of Samples: {current_project.sensivity_settings.seed * ((2 * current_project.current_parameters.Count) + 2)}";
} catch (Exception){}
update_project();
}
string public_project_path_name;
string public_selected_txt_path;
private void home_set_project(object sender, RoutedEventArgs e)
{
if (is_swat_running == true)
{
System.Windows.MessageBox.Show($@"Cannot create a new project because{Environment.NewLine}SWAT+ is running. Please, Wait.");
return;
}
if (is_calibration_run == true)
{
System.Windows.MessageBox.Show($@"Cannot create a new project because{Environment.NewLine}calibration is running. Please, Wait.");
return;
}
if (is_sensitivity_run == true)
{
System.Windows.MessageBox.Show($@"Cannot create a new project, sensitivity{Environment.NewLine}analysis is running. Please, Wait.");
return;
}
try
{
public_selected_txt_path = pick_folder("Choose a TxtInOut Directory", "OK");
bool is_valid_txt_in_out;
if (File.Exists($@"{public_selected_txt_path}/time.sim"))
{
is_valid_txt_in_out = true;
} else
{
is_valid_txt_in_out = false;
}
if (File.Exists($@"{public_selected_txt_path}/file.cio") == false)
{
is_valid_txt_in_out = false;
}
// if the txtinout is not valid, cancel save process
if (is_valid_txt_in_out == false)
{
System.Windows.MessageBox.Show($@"The Selected TxtInOut directory {Environment.NewLine}does not contain valid SWAT+{Environment.NewLine}input files");
return;
}
public_project_path_name = pick_save_path("SWAT+ Toolbox Project|*.spt", "Choose Project Save Location", "OK");
home_new_project_save(public_selected_txt_path, public_project_path_name);
}
catch (Exception)
{
current_project = new project();
}
}
private void home_new_project_save(string sel_txt_path, string sel_proj_path)
{
current_project = new project();
selected_parameters = new ObservableCollection<parameter>();
selected_observations = new ObservableCollection<observation>();
current_project.txtinout = sel_txt_path;
current_project.project_name = Path.GetFileName(sel_proj_path);
current_project.project_path = sel_proj_path;
current_project.current_observations = selected_observations;
current_project.current_parameters = selected_parameters;
current_project.created = DateTime.Now;
current_project.last_modified = DateTime.Now;
// get run period from time.sim
string[] periodline = split_string_by_space(read_from($@"{sel_txt_path}/time.sim")[2]);
// set values GUI elements
current_project.run_date_start = new DateTime(int.Parse(periodline[1]), 1, 1);
current_project.run_date_end = new DateTime(int.Parse(periodline[3]), 12, 31);
current_project.run_warmup = 2;
current_project.print_csv = false;
current_project.print_settings = default_print_settings();
current_project.water_balance = default_water_balance();
current_project.nutrient_balance = default_nutrient_balance();
current_project.plant_summary = default_plant_results();
current_project.run_options = default_run_options();
current_project.sensivity_settings = default_sensitivity_analysis_settings();
// saving the project
save_project(current_project, recent_projects);
ui_selected_parameters.ItemsSource = selected_parameters;
ui_selected_observations.ItemsSource = selected_observations;
ui_calibration_manual_performance.ItemsSource = current_project.current_observations;
ui_calibration_automatic_performance.ItemsSource = current_project.current_observations;
//binding to ui elements
set_binding_contexts();
//run_model settings
ui_run_model_start_date.SelectedDate = current_project.run_date_start;
ui_run_model_end_date.SelectedDate = current_project.run_date_end;
ui_run_model_warmup_period.Text = current_project.run_warmup.ToString();
try
{
ui_calibration_automatic_observation_selection.SelectedIndex = current_project.selected_eval_index;
ui_sensitivity_observation_selection.SelectedIndex = current_project.selected_eval_index;
}
catch (Exception)
{
}
bool add_new_recent = true;
for (int celray = 0; celray < recent_projects.Count; celray++)
{
if (recent_projects[celray].project_name == current_project.project_name)
{
recent_projects[celray].project_path = current_project.project_path;
recent_projects[celray].txtinout = current_project.txtinout;
recent_projects[celray].last_modified = current_project.last_modified;
add_new_recent = false;
}
}
if (add_new_recent == true)
{
recent_projects.Add(new recent_project()
{
project_name = current_project.project_name,
project_path = current_project.project_path,
txtinout = current_project.txtinout,
last_modified = current_project.last_modified
});
}
}
private void home_open_project(object sender, RoutedEventArgs e)
{
if (is_swat_running == true)
{
System.Windows.MessageBox.Show($@"Cannot open a new project because{Environment.NewLine}SWAT+ is running. Please, Wait.");
return;
}
if (is_calibration_run == true)
{
System.Windows.MessageBox.Show($@"Cannot open a new project because{Environment.NewLine}calibration is running. Please, Wait.");
return;
}
if (is_sensitivity_run == true)
{
System.Windows.MessageBox.Show($@"Cannot open a new project, sensitivity{Environment.NewLine}analysis is running. Please, Wait.");
return;
}
try
{
open_project();
ui_sensitivity_seed_box.Text = current_project.sensivity_settings.seed.ToString();
ui_calibration_automatic_seed_box.Text = current_project.sensivity_settings.seed.ToString();
Console.WriteLine(current_project.sensivity_settings.sensitivity_method);
Console.WriteLine($@"System.Windows.Controls.ComboBoxItem: {current_project.sensivity_settings.sensitivity_method}");
Console.WriteLine(ui_sensitivity_analysis_method.Items[0].ToString());
for (int celray = 0; celray < ui_sensitivity_analysis_method.Items.Count; celray++)
{
Console.WriteLine(celray);
if (ui_sensitivity_analysis_method.Items[celray].ToString() == $@"System.Windows.Controls.ComboBoxItem: {current_project.sensivity_settings.sensitivity_method}")
{
ui_sensitivity_analysis_method.SelectedIndex = celray;
}
}
}
catch (Exception)
{
System.Windows.MessageBox.Show("No SWAT+ Toolbox project was" + Environment.NewLine + "was opened");
}
}
public void open_project()
{
current_project = new project();
try
{
ui_calibration_automatic_observation_selection.SelectedIndex = current_project.selected_eval_index;
ui_sensitivity_observation_selection.SelectedIndex = current_project.selected_eval_index;
}
catch (Exception)
{
}
// try to open an existing project, ignore if cancelled
using (StreamReader streamReader = new StreamReader(pick_file("SWAT+ Toolbox Project|*.spt", "Choose Existing SWAT+ Toolbox Project", "OK")))
{
var deserializer = new DeserializerBuilder()
.Build();
//Deserializer deserializer = new Deserializer();
current_project = (project)deserializer.Deserialize(streamReader, typeof(project));
}
selected_parameters = current_project.current_parameters;
selected_observations = current_project.current_observations;
ui_selected_parameters.ItemsSource = selected_parameters;
ui_selected_observations.ItemsSource = selected_observations;
ui_calibration_manual_performance.ItemsSource = current_project.current_observations;
ui_calibration_automatic_performance.ItemsSource = current_project.current_observations;
//update ui
//home
//run_model
ui_run_model_start_date.SelectedDate = current_project.run_date_start;
ui_run_model_end_date.SelectedDate = current_project.run_date_end;
ui_run_model_warmup_period.Text = current_project.run_warmup.ToString();
//binding checkboxes for printing
set_binding_contexts();
//
}
// run model functions
private void update_project_start_date(object sender, RoutedEventArgs e)
{
current_project.run_date_start = (DateTime)ui_run_model_start_date.SelectedDate;
update_project();
}
private bool cant_change_run_settings(bool sensitivity_run, bool swat_running, bool calibration_run)
{
if (swat_running == true)
{
System.Windows.MessageBox.Show($@"Cannot change project settings because{Environment.NewLine}SWAT+ is running. Please, Wait.");
return true;
}
if (calibration_run == true)
{
System.Windows.MessageBox.Show($@"Cannot change project settings because{Environment.NewLine}calibration is running. Please, Wait.");
return true;
}
if (sensitivity_run == true)
{
System.Windows.MessageBox.Show($@"Cannot change project settings because{Environment.NewLine}analysis is running. Please, Wait.");
return true;
}
return false;
}
private void update_project_end_date(object sender, RoutedEventArgs e)
{
current_project.run_date_end = (DateTime)ui_run_model_end_date.SelectedDate;
update_project();
}
private void update_project_warmup(object sender, RoutedEventArgs e)
{
try
{
current_project.run_warmup = int.Parse(ui_run_model_warmup_period.Text);
}
catch (Exception)
{
System.Windows.MessageBox.Show("Warmup period is not valid");
return;
}
update_project();
}
private void update_print_settings(object sender, RoutedEventArgs e)
{
update_project();
}
private void update_parameter_value(object sender, DataGridCellEditEndingEventArgs e)
{
update_project();
}
private void update_project()
{
recent_projects = sort_recents(recent_projects, current_project);
current_project.last_modified = DateTime.Now;
save_project(current_project, recent_projects);
}
private void set_binding_contexts()
{
ui_home_current_project_name.Visibility = Visibility.Visible;
ui_home_current_project_name.Text = $@"{current_project.project_name}";
ui_home_current_project_name_titlebar.Visibility = Visibility.Visible;
ui_home_current_project_name_titlebar.Text = $@" - {current_project.project_name}";
ui_home_current_project_txtinout.Visibility = Visibility.Visible;
ui_home_current_project_txtinout.Text = $@"{current_project.txtinout}";
ui_calibration_manual_performance.ItemsSource = current_project.current_observations;
ui_calibration_automatic_performance.ItemsSource = current_project.current_observations;
ui_home_recent_listdatagrid.ItemsSource = recent_projects;
ui_home_recent_listdatagrid.DataContext = recent_projects;
ui_sensitivity_number_of_samples.Text = "Number of Samples: - ";
ui_sensitivity_observation_selection.ItemsSource = current_project.current_observations;
ui_sensitivity_observation_selection.DisplayMemberPath = "chart_name";
ui_sensitivity_observation_selection.SelectedValuePath = "chart_name";
ui_calibration_automatic_observation_selection.ItemsSource = current_project.current_observations;
ui_calibration_automatic_observation_selection.DisplayMemberPath = "chart_name";
ui_calibration_automatic_observation_selection.SelectedValuePath = "chart_name";
ui_calibration_manual_performance.DataContext = current_project.current_observations;