This repository was archived by the owner on Nov 27, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathNetCDFClient.cs
More file actions
1740 lines (1545 loc) · 74.2 KB
/
Copy pathNetCDFClient.cs
File metadata and controls
1740 lines (1545 loc) · 74.2 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.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using ucar.nc2;
using ucar.nc2.dataset;
using ucar.nc2.ncml;
using ucar.util;
using ucar;
using java;
using java.io;
using DHI.Generic.NetCDF.MIKE.Commands;
using DHI.Generic.MikeZero;
using System.Globalization;
namespace DHI.Generic.NetCDF.MIKE
{
public partial class NetCDFClient : Form
{
private EngineSettings _saveSettings = null;
private NetCdfUtilities _netcdfUtil = null;
private DfsUtilities _dfsUtil = null;
private List<string> _variableNames;
private List<bool> _isVariableSelected;
private List<int> _selectedEUMIndex;
private List<bool> _hasSelectedEUM;
private List<List<DHICFEntry>> _variableMappings;
private bool _isNCFile;
private bool _canPlot;
private bool _hasImportSettings = false;
public NetCDFClient()
{
InitializeComponent();
_createCommands();
}
#region Step 1 Select Command
private void _createCommands()
{
listBoxCommand.Items.Clear();
List<Type> commandList = _getClasses(typeof(iCommand));
foreach (Type commandType in commandList)
{
if (!commandType.IsInterface)
{
object command = Activator.CreateInstance(commandType);
listBoxCommand.Items.Add(commandType.Name);
}
}
listBoxCommand.Sorted = true;
}
private void buttonS1Next_Click(object sender, EventArgs e)
{
_gotoStep2(true);
}
private void listBoxCommand_DoubleClick(object sender, EventArgs e)
{
_gotoStep2(true);
}
private void listBoxCommand_SelectedIndexChanged(object sender, EventArgs e)
{
List<Type> commandList = _getClasses(typeof(iCommand));
bool hasDisplayed = false;
foreach (Type commandType in commandList)
{
if (!commandType.IsInterface)
{
if (listBoxCommand.SelectedItem.ToString() == commandType.Name)
{
object command = Activator.CreateInstance(commandType);
object commandDescription = commandType.InvokeMember("CommandDescription", System.Reflection.BindingFlags.InvokeMethod,
null, command, null);
richTextBoxCommDesc.Text = commandDescription.ToString();
hasDisplayed = true;
}
else if (hasDisplayed == false)
richTextBoxCommDesc.Text = "";
}
}
}
private void listBoxCommand_MouseDown(object sender, MouseEventArgs e)
{
if (_hasImportSettings && e.Button == System.Windows.Forms.MouseButtons.Left)
{
DialogResult result = MessageBox.Show("Do you want to discard saved changes in the settings file?", "Confirmation", MessageBoxButtons.YesNo);
if (result == DialogResult.No)
{
_hasImportSettings = true;
for (int i = 0; i < listBoxCommand.Items.Count; i++)
{
if (listBoxCommand.Items[i].ToString() == _saveSettings.Commands[0].CommandName)
{
listBoxCommand.SelectedIndex = i;
}
}
}
else
_hasImportSettings = false;
}
}
private void _gotoStep2(bool goToNextStep)
{
try
{
if (listBoxCommand.SelectedItem == null)
{
throw new Exception("No command chosen.");
}
if (!_hasImportSettings)
{
_saveSettings = new EngineSettings();
_saveSettings.Commands = new List<CommandSettings>();
CommandSettings newCommand = new CommandSettings();
newCommand.CommandName = listBoxCommand.SelectedItem.ToString().Split('-')[0].Trim();
List<Type> commandList = _getClasses(typeof(iCommand));
foreach (Type commandType in commandList)
{
if (!commandType.IsInterface)
{
if (commandType.Name == newCommand.CommandName)
{
object command = Activator.CreateInstance(commandType);
object commandInputFileExtension = commandType.InvokeMember("CommandInputFileExtension", System.Reflection.BindingFlags.InvokeMethod,
null, command, null);
object commandOutputFileExtension = commandType.InvokeMember("CommandOutputFileExtension", System.Reflection.BindingFlags.InvokeMethod,
null, command, null);
newCommand.InputFileExtension = commandInputFileExtension.ToString();
newCommand.OutputFileExtension = commandOutputFileExtension.ToString();
_canPlot = (bool)commandType.InvokeMember("CanPlot", System.Reflection.BindingFlags.InvokeMethod,
null, command, null);
}
}
}
_saveSettings.Commands.Add(newCommand);
openFileDialog1.Filter = newCommand.InputFileExtension + " files|*" + newCommand.InputFileExtension;
saveFileDialog1.Filter = newCommand.OutputFileExtension + " files|*" + newCommand.OutputFileExtension;
}
if (_saveSettings.Commands[0].CommandName.EndsWith("ToDfs2") || _saveSettings.Commands[0].CommandName.EndsWith("ToDfs3"))
{
groupBoxOverwriteOri.Enabled = true;
groupBoxGrid.Enabled = true;
}
else
{
groupBoxOverwriteOri.Enabled = false;
groupBoxGrid.Enabled = false;
}
if (goToNextStep) _nextStep();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void _enableDXDY()
{
textBoxDX.Enabled = true;
textBoxDX.Text = "Auto";
textBoxDY.Enabled = true;
textBoxDY.Text = "Auto";
textBoxNumXCells.Enabled = true;
textBoxNumXCells.Text = "Auto";
textBoxNumYCells.Enabled = true;
textBoxNumYCells.Text = "Auto";
textBoxNumZCells.Enabled = true;
textBoxNumZCells.Text = "Auto";
textBoxDZ.Enabled = true;
textBoxMemBlock.Enabled = true;
//groupBoxSubset.Enabled = false;
}
private void _disableDXDY()
{
textBoxDX.Enabled = false;
textBoxDX.Text = "Auto";
textBoxDY.Enabled = false;
textBoxDY.Text = "Auto";
textBoxNumXCells.Enabled = false;
textBoxNumXCells.Text = "Auto";
textBoxNumYCells.Enabled = false;
textBoxNumYCells.Text = "Auto";
textBoxNumZCells.Enabled = false;
textBoxNumZCells.Text = "Auto";
textBoxDZ.Enabled = false;
textBoxMemBlock.Enabled = false;
//groupBoxSubset.Enabled = true;
}
/// <summary>
/// Get list of types in the calling assembly that inherits
/// from a certain base type.
/// </summary>
/// <param name="baseType">The base type to check for.</param>
/// <returns>A list of types that inherits from baseType.</returns>
private static List<Type> _getClasses(Type baseType)
{
return System.Reflection.Assembly.GetCallingAssembly().GetTypes().Where(type => baseType.IsAssignableFrom(type)).ToList();
}
#endregion //Step 1
#region Step 2 Select File
private void buttonBrowse_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog();
textBoxInputFile.Text = openFileDialog1.FileName;
_hasImportSettings = false;
_readInputFile(_hasImportSettings);
}
private void _readInputFile(bool hasImportSettings)
{
if (!String.IsNullOrEmpty(textBoxInputFile.Text))
{
if (!hasImportSettings) _initSaveSettings();
if (System.IO.Path.GetExtension(textBoxInputFile.Text).StartsWith(".dfs"))
{
_isNCFile = false;
bool readOk = _readDfsFile(textBoxInputFile.Text);
_isFile(readOk);
if (readOk)
{
_createDFSTreeView();
//_createVariablesCheckBoxList();
if (!hasImportSettings)
{
_saveSettings.Commands[0].TimeAxisName = "time";
_saveSettings.Commands[0].XAxisName = "lon";
_saveSettings.Commands[0].YAxisName = "lat";
_saveSettings.Commands[0].ZAxisName = "depth";
}
}
}
else //try to read as netcdf datasets (should work with thredds, opendab, etc)
{
_isNCFile = true;
bool readOk = _readNCFile(textBoxInputFile.Text);
_isFile(readOk);
if (readOk)
{
_createNCTreeView();
}
}
}
}
private bool _readNCFile(string fileName)
{
try
{
_netcdfUtil = new NetCdfUtilities(textBoxInputFile.Text, checkBoxNotFile.Checked);
if (_saveSettings.Commands[0].CommandName.Contains("Grib"))
_saveSettings.Commands[0].UseDataSet = true;
else
_saveSettings.Commands[0].UseDataSet = checkBoxNotFile.Checked;
return true;
}
catch (Exception ex)
{
MessageBox.Show("Read NetCDF file error. " + ex.Message);
return false;
}
}
private bool _readDfsFile(string fileName)
{
try
{
_dfsUtil = new DfsUtilities();
_dfsUtil.ReadDfsFile(fileName);
return true;
}
catch (Exception ex)
{
MessageBox.Show("Read Dfs file error. " + ex.Message);
return false;
}
}
private void _isFile(bool yesOrNo)
{
label8.Visible = yesOrNo;
treeViewNCFileInfo.Visible = yesOrNo;
}
private void _createNCTreeView()
{
treeViewNCFileInfo.Nodes.Clear();
object[] ncDimensions = _netcdfUtil.GetDimensions();
TreeNode[] dimTreeArr = new TreeNode[ncDimensions.Length];
for (int i = 0; i < ncDimensions.Length; i++)
{
TreeNode newDimTreeNode = new TreeNode(((Dimension)ncDimensions[i]).toString());
dimTreeArr[i] = newDimTreeNode;
}
TreeNode treeNode = new TreeNode("Dimensions [" + ncDimensions.Length.ToString() + "]", dimTreeArr);
treeViewNCFileInfo.Nodes.Add(treeNode);
object[] ncVariables = _netcdfUtil.GetVariables();
TreeNode[] varTreeArr = new TreeNode[ncVariables.Length];
for (int i = 0; i < ncVariables.Length; i++)
{
TreeNode newVarTreeNode = new TreeNode(((Variable)ncVariables[i]).toString());
varTreeArr[i] = newVarTreeNode;
}
treeNode = new TreeNode("Variables [" + ncVariables.Length.ToString() + "]", varTreeArr);
treeViewNCFileInfo.Nodes.Add(treeNode);
object[] ncGlobalAtt = _netcdfUtil.GetGlobalAttributes();
TreeNode[] gAttTreeArr = new TreeNode[ncGlobalAtt.Length];
for (int i = 0; i < ncGlobalAtt.Length; i++)
{
TreeNode newgAttTreeNode = new TreeNode(((ucar.nc2.Attribute)ncGlobalAtt[i]).toString());
gAttTreeArr[i] = newgAttTreeNode;
}
treeNode = new TreeNode("Global Attributes [" + ncGlobalAtt.Length.ToString() + "]", gAttTreeArr);
treeViewNCFileInfo.Nodes.Add(treeNode);
}
private void _createDFSTreeView()
{
treeViewNCFileInfo.Nodes.Clear();
TreeNode treeNode;
List<TreeNode> gAttTreeArr = new List<TreeNode>();
gAttTreeArr.Add(new TreeNode("File Name = " + _dfsUtil.FileName));
gAttTreeArr.Add(new TreeNode("DfsFileType = " + _dfsUtil.dfsFileType));
gAttTreeArr.Add(new TreeNode("Compressed = " + _dfsUtil.compressed));
gAttTreeArr.Add(new TreeNode("Delete Value = " + _dfsUtil.delVal));
gAttTreeArr.Add(new TreeNode("File Title = " + _dfsUtil.FileTitle));
gAttTreeArr.Add(new TreeNode("Stat Type = " + _dfsUtil.statType));
gAttTreeArr.Add(new TreeNode("Projection Type = " + _dfsUtil.Projection_type));
gAttTreeArr.Add(new TreeNode("Projection = " + _dfsUtil.Projection));
gAttTreeArr.Add(new TreeNode("Orientation = " + _dfsUtil.Orientation));
gAttTreeArr.Add(new TreeNode("Start DateTime = " + _dfsUtil.StartDateTime.ToString("yyyy-MM-dd HH:mm:ss")));
gAttTreeArr.Add(new TreeNode("Time Steps Count = " + _dfsUtil.tAxis_nTSteps));
treeNode = new TreeNode("Headers [" + gAttTreeArr.Count() + "]", gAttTreeArr.ToArray());
treeViewNCFileInfo.Nodes.Add(treeNode);
TreeNode[] varTreeArr = new TreeNode[_dfsUtil.Items.Count()];
for (int i = 0; i < _dfsUtil.Items.Count(); i++)
{
TreeNode newVarTreeNode = new TreeNode(_dfsUtil.Items[i].Name);
varTreeArr[i] = newVarTreeNode;
}
treeNode = new TreeNode("Items [" + _dfsUtil.Items.Count() + "]", varTreeArr);
treeViewNCFileInfo.Nodes.Add(treeNode);
}
private void buttonS2Next_Click(object sender, EventArgs e)
{
if (System.IO.File.Exists(textBoxInputFile.Text))
{
if (_isNCFile)
{
if (!_hasImportSettings)
{
_populateComboBoxDimensions();
_populateSubsets();
}
_setTab3ToValid();
_nextStep();
}
else
{
_setTab3ToInValid();
if (!_hasImportSettings)
{
_createVariablesCheckBoxList();
}
tabControl1.SelectedTab = tabPageS4;
}
}
else
MessageBox.Show("Input file does not exist.");
}
private void _setTab3ToInValid()
{
groupBoxDimensions.Enabled = false;
groupBoxSubset.Enabled = false;
}
private void _setTab3ToValid()
{
groupBoxDimensions.Enabled = true;
groupBoxSubset.Enabled = true;
}
private void buttonS2Back_Click(object sender, EventArgs e)
{
_previousStep();
}
private void textBoxInputFile_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (int)Keys.Return)
{
if (!String.IsNullOrEmpty(textBoxInputFile.Text))
{
_initSaveSettings();
if (System.IO.Path.GetExtension(textBoxInputFile.Text).StartsWith(".dfs"))
{
_isNCFile = false;
bool readOk = _readDfsFile(textBoxInputFile.Text);
_isFile(readOk);
if (readOk)
{
_createDFSTreeView();
//_createVariablesCheckBoxList();
_saveSettings.Commands[0].TimeAxisName = "time";
_saveSettings.Commands[0].XAxisName = "lon";
_saveSettings.Commands[0].YAxisName = "lat";
_saveSettings.Commands[0].ZAxisName = "depth";
}
}
else //try to read as netcdf datasets
{
_isNCFile = true;
bool readOk = _readNCFile(textBoxInputFile.Text);
_isFile(readOk);
if (readOk)
{
_createNCTreeView();
}
}
}
}
}
#endregion //Step 2
#region Step 3 Set Dimensions
private void _populateComboBoxDimensions()
{
object[] ncDims = _netcdfUtil.GetDimensions();
object[] ncVars = _netcdfUtil.GetVariables();
comboBoxX.Items.Clear();
comboBoxY.Items.Clear();
comboBoxZ.Items.Clear();
comboBoxTime.Items.Clear();
comboBoxX.Items.Add("None");
comboBoxY.Items.Add("None");
comboBoxZ.Items.Add("None");
comboBoxTime.Items.Add("None");
//remove dimensions and use only variables. dimensions will be automatically added
/*foreach (object ncDim in ncDims)
{
ucar.nc2.Dimension dim = (ucar.nc2.Dimension)ncDim;
comboBoxX.Items.Add(dim.getName());
comboBoxY.Items.Add(dim.getName());
comboBoxZ.Items.Add(dim.getName());
comboBoxTime.Items.Add(dim.getName());
}*/
foreach (object ncVar in ncVars)
{
ucar.nc2.Variable var = (ucar.nc2.Variable)ncVar;
foreach (object ncDim in ncDims)
{
ucar.nc2.Dimension dim = (ucar.nc2.Dimension)ncDim;
if (dim.getName() != var.getFullName())
{
bool doesExist = false;
foreach (string existingName in comboBoxX.Items)
{
if (var.getFullName() == existingName) doesExist = true;
}
if (!doesExist) comboBoxX.Items.Add(var.getFullName());
}
}
foreach (object ncDim in ncDims)
{
ucar.nc2.Dimension dim = (ucar.nc2.Dimension)ncDim;
if (dim.getName() != var.getFullName())
{
bool doesExist = false;
foreach (string existingName in comboBoxY.Items)
{
if (var.getFullName() == existingName) doesExist = true;
}
if (!doesExist) comboBoxY.Items.Add(var.getFullName());
}
}
foreach (object ncDim in ncDims)
{
ucar.nc2.Dimension dim = (ucar.nc2.Dimension)ncDim;
if (dim.getName() != var.getFullName())
{
bool doesExist = false;
foreach (string existingName in comboBoxZ.Items)
{
if (var.getFullName() == existingName) doesExist = true;
}
if (!doesExist) comboBoxZ.Items.Add(var.getFullName());
}
}
foreach (object ncDim in ncDims)
{
ucar.nc2.Dimension dim = (ucar.nc2.Dimension)ncDim;
if (dim.getName() != var.getFullName())
{
bool doesExist = false;
foreach (string existingName in comboBoxTime.Items)
{
if (var.getFullName() == existingName) doesExist = true;
}
if (!doesExist) comboBoxTime.Items.Add(var.getFullName());
}
}
}
//find lon
CFMapping cfmap = new CFMapping();
List<string> searchWords = new List<string>();
string foundWord;
for (int i = 0; i < comboBoxX.Items.Count; i++)
{
searchWords.Add(comboBoxX.Items[i].ToString());
}
int index = cfmap.Search("longitude", searchWords, 0.7, out foundWord);
if (foundWord == "None") index = cfmap.Search("lon", searchWords, 0.7, out foundWord);
if (foundWord == "None") index = cfmap.Search("x", searchWords, 0.7, out foundWord);
for (int i = 0; i < comboBoxX.Items.Count; i++)
{
if (foundWord == comboBoxX.Items[i].ToString())
comboBoxX.SelectedIndex = i;
}
//find lat
searchWords = new List<string>();
for (int i = 0; i < comboBoxY.Items.Count; i++)
{
searchWords.Add(comboBoxY.Items[i].ToString());
}
index = cfmap.Search("latitude", searchWords, 0.7, out foundWord);
if (foundWord == "None") index = cfmap.Search("lat", searchWords, 0.7, out foundWord);
if (foundWord == "None") index = cfmap.Search("y", searchWords, 0.7, out foundWord);
for (int i = 0; i < comboBoxY.Items.Count; i++)
{
if (foundWord == comboBoxY.Items[i].ToString())
comboBoxY.SelectedIndex = i;
}
//find depth
searchWords = new List<string>();
for (int i = 0; i < comboBoxZ.Items.Count; i++)
{
searchWords.Add(comboBoxZ.Items[i].ToString());
}
index = cfmap.Search("depth", searchWords, 0.7, out foundWord);
if (foundWord == "None") index = cfmap.Search("dep", searchWords, 0.7, out foundWord);
if (foundWord == "None") index = cfmap.Search("z", searchWords, 0.7, out foundWord);
//added: 20-09-2016 filter for dfs1 option to set depth to "none"
if (_saveSettings.Commands[0].CommandName.ToLower().Contains("dfs1"))
index = cfmap.Search("None", searchWords, 0.7, out foundWord);
for (int i = 0; i < comboBoxZ.Items.Count; i++)
{
if (foundWord == comboBoxZ.Items[i].ToString())
comboBoxZ.SelectedIndex = i;
}
//find time
searchWords = new List<string>();
for (int i = 0; i < comboBoxTime.Items.Count; i++)
{
searchWords.Add(comboBoxTime.Items[i].ToString());
}
index = cfmap.Search("time", searchWords, 0.7, out foundWord);
if (foundWord == "None") index = cfmap.Search("dt", searchWords, 0.7, out foundWord);
for (int i = 0; i < comboBoxTime.Items.Count; i++)
{
if (foundWord == comboBoxTime.Items[i].ToString())
comboBoxTime.SelectedIndex = i;
}
}
private void _populateSubsets()
{
object[] ncDims = _netcdfUtil.GetDimensions();
bool hasFoundDataSet = false;
foreach (object ncDim in ncDims)
{
ucar.nc2.Dimension dim = (ucar.nc2.Dimension)ncDim;
if (dim.getName() == comboBoxX.Text)
{
textBoxSubX.Text = "0:" + (dim.getLength() - 1).ToString();
hasFoundDataSet = true;
}
else if (!hasFoundDataSet)
textBoxSubX.Text = "Not an axis dataset";
if (dim.getName() == comboBoxY.Text)
{
textBoxSubY.Text = "0:" + (dim.getLength()-1).ToString();
hasFoundDataSet = true;
}
else if (!hasFoundDataSet)
textBoxSubY.Text = "Not an axis dataset";
}
}
private void _setDimensions()
{
try
{
if (comboBoxX.Text == "None" || comboBoxX.Text == "")
_saveSettings.Commands[0].XAxisName = null;
else
{
_saveSettings.Commands[0].XAxisName = comboBoxX.Text;
_saveSettings.Commands[0].XAxisDimensionName = comboBoxX.Text;
}
if (comboBoxY.Text == "None" || comboBoxY.Text == "")
_saveSettings.Commands[0].YAxisName = null;
else
{
_saveSettings.Commands[0].YAxisName = comboBoxY.Text;
_saveSettings.Commands[0].YAxisDimensionName = comboBoxY.Text;
}
if (comboBoxZ.Text == "None" || comboBoxZ.Text == "")
_saveSettings.Commands[0].ZAxisName = null;
else
{
_saveSettings.Commands[0].ZAxisName = comboBoxZ.Text;
_saveSettings.Commands[0].ZAxisDimensionName = comboBoxZ.Text;
}
if (comboBoxTime.Text == "None" || comboBoxTime.Text == "")
_saveSettings.Commands[0].TimeAxisName = null;
else
{
_saveSettings.Commands[0].TimeAxisName = comboBoxTime.Text;
_saveSettings.Commands[0].TimeAxisDimensionName = comboBoxTime.Text;
}
_saveSettings.Commands[0].MZMapProjectionString = textBoxProj.Text;
_saveSettings.Commands[0].ProjectionEastNorthMultiplier = textBoxENMultiplier.Text;
_saveSettings.Commands[0].XLayer = textBoxSubX.Text;
_saveSettings.Commands[0].YLayer = textBoxSubY.Text;
if (textBoxOriX.Text != "" && textBoxOriY.Text != "" && textBoxRotation.Text != "")
{
_saveSettings.Commands[0].OverwriteOriginX = Convert.ToDouble(textBoxOriX.Text, CultureInfo.InvariantCulture);
_saveSettings.Commands[0].OverwriteOriginY = Convert.ToDouble(textBoxOriY.Text, CultureInfo.InvariantCulture);
_saveSettings.Commands[0].OverwriteRotation = Convert.ToDouble(textBoxRotation.Text, CultureInfo.InvariantCulture);
}
else
{
_saveSettings.Commands[0].OverwriteOriginX = -999;
_saveSettings.Commands[0].OverwriteOriginY = -999;
_saveSettings.Commands[0].OverwriteRotation = -999;
}
_saveSettings.Commands[0].MaxBlockSizeMB = Convert.ToInt32(textBoxMemBlock.Text, CultureInfo.InvariantCulture);
_saveSettings.Commands[0].TimeStepSeconds = Convert.ToInt32(textBoxTimeStepSec.Text, CultureInfo.InvariantCulture);
if (textBoxDZ.Text.ToLower() != "auto")
_saveSettings.Commands[0].DZ = (float)Convert.ToDouble(textBoxDZ.Text, CultureInfo.InvariantCulture);
else
_saveSettings.Commands[0].DZ = 0;
//adding DX and DY to settings
if (textBoxDX.Text.ToLower() != "auto")
_saveSettings.Commands[0].DX = (float)Convert.ToDouble(textBoxDX.Text, CultureInfo.InvariantCulture);
else
_saveSettings.Commands[0].DX = 0;
if (textBoxDY.Text.ToLower() != "auto")
_saveSettings.Commands[0].DY = (float)Convert.ToDouble(textBoxDY.Text, CultureInfo.InvariantCulture);
else
_saveSettings.Commands[0].DY = 0;
if (textBoxNumXCells.Text.ToLower() != "auto")
_saveSettings.Commands[0].NumberXCells = Convert.ToInt32(textBoxNumXCells.Text, CultureInfo.InvariantCulture);
else
_saveSettings.Commands[0].NumberXCells = 0;
if (textBoxNumYCells.Text.ToLower() != "auto")
_saveSettings.Commands[0].NumberYCells = Convert.ToInt32(textBoxNumYCells.Text, CultureInfo.InvariantCulture);
else
_saveSettings.Commands[0].NumberYCells = 0;
if (textBoxNumZCells.Text.ToLower() != "auto")
_saveSettings.Commands[0].NumberZCells = Convert.ToInt32(textBoxNumZCells.Text, CultureInfo.InvariantCulture);
else
_saveSettings.Commands[0].NumberZCells = 0;
}
catch (Exception ex)
{
throw new Exception ("_setDimensions Error: " + ex.Message);
}
}
private void _createVariablesCheckBoxList()
{
checkedListBoxFileItems.Items.Clear();
_variableMappings = new List<List<DHICFEntry>>();
_variableNames = new List<string>();
_isVariableSelected = new List<bool>();
_selectedEUMIndex = new List<int>();
_hasSelectedEUM = new List<bool>();
if (!_hasImportSettings)
{
//if (_saveSettings.Commands[0].VariablesMappings == null)
_initSaveSettings();
}
if (_isNCFile)
{
object[] ncVariables = _netcdfUtil.GetVariables();
//check how many variables are selected as axes
int varFilterCount = 0;
if (_saveSettings.Commands[0].XAxisDimensionName != null) varFilterCount++;
if (_saveSettings.Commands[0].YAxisDimensionName != null) varFilterCount++;
if (_saveSettings.Commands[0].ZAxisDimensionName != null) varFilterCount++;
if (_saveSettings.Commands[0].TimeAxisDimensionName != null) varFilterCount++;
for (int i = 0; i < ncVariables.Length; i++)
{
java.util.List ncDimensions = ((Variable)ncVariables[i]).getDimensions();
int dimCount = 0;
for (int j = 0; j < ncDimensions.size(); j++)
{
if (((Dimension)ncDimensions.get(j)).getName()== _saveSettings.Commands[0].XAxisDimensionName) dimCount++;
if (((Dimension)ncDimensions.get(j)).getName() == _saveSettings.Commands[0].YAxisDimensionName) dimCount++;
if (((Dimension)ncDimensions.get(j)).getName() == _saveSettings.Commands[0].ZAxisDimensionName) dimCount++;
if (((Dimension)ncDimensions.get(j)).getName() == _saveSettings.Commands[0].TimeAxisDimensionName) dimCount++;
}
if (dimCount == varFilterCount && dimCount == ncDimensions.size())
{
checkedListBoxFileItems.Items.Add(((Variable)ncVariables[i]).getFullName());
_variableNames.Add(((Variable)ncVariables[i]).getFullName());
if (!_hasImportSettings)
{
_saveSettings.Commands[0].VariablesMappings.Add(new DHICFEntry());
}
CFMapping cfMap = new CFMapping();
List<DHICFEntry> closestDHIEUMs = cfMap.FindDHIEums(((Variable)ncVariables[i]).getFullName(), 0.7);
if (closestDHIEUMs.Count > 0)
_variableMappings.Add(closestDHIEUMs);
else
{
_variableMappings.Add(new List<DHICFEntry>());
}
_isVariableSelected.Add(false);
_selectedEUMIndex.Add(0);
_hasSelectedEUM.Add(false);
}
}
}
else
{
CFMapping cfMap = new CFMapping();
for (int i = 0; i < _dfsUtil.Items.Count(); i++)
{
checkedListBoxFileItems.Items.Add(_dfsUtil.Items[i].Name);
_variableNames.Add(_dfsUtil.Items[i].Name);
if (!_hasImportSettings)
{
_saveSettings.Commands[0].VariablesMappings.Add(new DHICFEntry());
}
List<DHICFEntry> closestDHIEUMs = new List<DHICFEntry>();
closestDHIEUMs = cfMap.FindDHIEums(_dfsUtil.Items[i].Name, 0.3);
if (closestDHIEUMs.Count > 0)
{
List<DHICFEntry> foundEntries = new List<DHICFEntry>();
for (int j = 0; j < closestDHIEUMs.Count; j++)
{
List<DHICFEntry> currentfoundEntries = cfMap.FindClosestCFStandards(closestDHIEUMs[j].EUMItemDesc, 0.3);
for (int k = 0; k < currentfoundEntries.Count; k++)
{
if (checkExists(currentfoundEntries[k], foundEntries))
foundEntries.Add(currentfoundEntries[k]);
}
}
_variableMappings.Add(foundEntries);
}
else
{ _variableMappings.Add(new List<DHICFEntry>()); }
_isVariableSelected.Add(false);
_selectedEUMIndex.Add(0);
_hasSelectedEUM.Add(false);
}
}
}
private bool checkExists(DHICFEntry currentFoundEntry, List<DHICFEntry> foundEntries)
{
for (int i = 0; i < foundEntries.Count; i++)
{
if (foundEntries[i].CFStandardName == currentFoundEntry.CFStandardName)
return true;
}
return false;
}
private void _initSaveSettings()
{
try
{
_saveSettings.WorkingDirectory = System.IO.Path.GetDirectoryName(textBoxInputFile.Text);
_saveSettings.InputFileExtension = System.IO.Path.GetExtension(textBoxInputFile.Text);
_saveSettings.InputFilePrefix = System.IO.Path.GetFileNameWithoutExtension(textBoxInputFile.Text);
if (_saveSettings.InputFileExtension.StartsWith(".dfs") || _saveSettings.Commands[0].CommandName.Contains("Trans") || _saveSettings.Commands[0].CommandName.StartsWith("Mercator_ConvertNc"))
groupBoxSubset.Enabled = false;
else
groupBoxSubset.Enabled = true;
_saveSettings.Commands[0].InputFileName = textBoxInputFile.Text;
_saveSettings.Commands[0].VariablesMappings = new List<DHICFEntry>();
_saveSettings.Commands[0].Variables = new List<string>();
_saveSettings.Commands[0].IsVariablesSelected = new List<bool>();
}
catch (Exception ex)
{
//throw new Exception("Init. save settings error. " + ex.Message);
}
}
private void buttonS3Next_Click(object sender, EventArgs e)
{
if (System.IO.File.Exists(textBoxInputFile.Text))
{
if (_isNCFile)
{
if (!_hasImportSettings)
{
_createVariablesCheckBoxList();
}
_setDimensions();
_nextStep();
if (_saveSettings.Commands[0].CommandName.Contains("ConvertNcToDfs2Trans"))// || _saveSettings.Commands[0].CommandName == "ARPAL_ConvertNcToDfs2Trans" || _saveSettings.Commands[0].CommandName == "Hycom_ConvertNcToDfs2Trans")
_createTransectTable();
else
{
groupBoxTransect.Enabled = false;
dataGridViewTP.DataSource = null;
}
if (checkedListBoxFileItems.Items.Count > 0)
{
if (!_hasImportSettings)
{
checkedListBoxFileItems.SetItemChecked(0, true);
checkedListBoxFileItems.SelectedIndex = 0;
}
}
else
MessageBox.Show("No variables are found with the user defined axes (Step 3).");
}
}
else
MessageBox.Show("Input file does not exist.");
}
private void buttonS3Back_Click(object sender, EventArgs e)
{
if (_isNCFile)
{
_previousStep();
}
}
private void checkBoxProj_CheckedChanged(object sender, EventArgs e)
{
textBoxProj.Enabled = !checkBoxProj.Checked;
textBoxENMultiplier.Enabled = !checkBoxProj.Checked;
textBoxOriX.Enabled = !checkBoxProj.Checked;
textBoxOriY.Enabled = !checkBoxProj.Checked;
textBoxRotation.Enabled = !checkBoxProj.Checked;
if (checkBoxProj.Checked)
{
textBoxProj.Text = "LONG/LAT";
textBoxENMultiplier.Text = "";
textBoxOriX.Text = "";
textBoxOriY.Text = "";
textBoxRotation.Text = "";
}
}
private void checkBoxTimeStep_CheckedChanged(object sender, EventArgs e)
{
textBoxTimeStepSec.Enabled = !checkBoxTimeStep.Checked;
if (checkBoxTimeStep.Checked)
textBoxTimeStepSec.Text = "86400";
}
private void comboBoxX_TextChanged(object sender, EventArgs e)
{
_populateSubsets();
}
private void comboBoxY_TextChanged(object sender, EventArgs e)
{
_populateSubsets();
}
#endregion //Step 3
#region Step 4 Select Variables
private void _setVariableMappings(int varIndex, int eumIndex, int eumUnitIndex)
{
try
{
_saveSettings.Commands[0].Variables = _variableNames;
_saveSettings.Commands[0].IsVariablesSelected = _isVariableSelected;
if (_isNCFile)
{
_variableMappings[varIndex][eumIndex].EUMMappedItemUnitKey = _variableMappings[varIndex][eumIndex].EUMItemUnitKeys[eumUnitIndex];
_variableMappings[varIndex][eumIndex].EUMMappedItemUnitDesc = _variableMappings[varIndex][eumIndex].EUMItemUnitDesc[eumUnitIndex];
_saveSettings.Commands[0].VariablesMappings[varIndex] = _variableMappings[varIndex][eumIndex];
}
else
{
_saveSettings.Commands[0].VariablesMappings[varIndex] = _variableMappings[varIndex][eumIndex];
}
try
{
_saveSettings.Commands[0].TransectSpaceStepsNumber = Convert.ToInt32(textBoxTSSize.Text);
}
catch (Exception ex)
{
MessageBox.Show("Cannot convert " + textBoxTSSize.Text + " to an integer." + ex.Message);
}
}
catch
{
}
}
private void _createEums(int varIndex)
{
try
{
_checkMike();
CFMapping cfMap = new CFMapping();
if (_variableMappings[varIndex].Count > 0) //found mapped entry
{
comboBoxEUM.DataSource = null;