-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinterfaces.py
2088 lines (1710 loc) · 91.1 KB
/
interfaces.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import re
import numpy as np
from collections import OrderedDict
from datetime import datetime as dt
from os.path import join, dirname
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from Hime import log
from Hime.vic_execer import vic_exec
from Hime.routing import confluence, write_runoff_data, gather_to_month
from Hime.uh_creater import write_rout_data, load_rout_data, create_rout
from Hime.calibrater import calibrate
from Hime.param_creater import create_params_file
from Hime.forcing_creater import read_stn_data, create_forcing
from Hime.utils import set_nc_value
group_ss = "QGroupBox{border-radius: 5px; border: 2px groove lightgrey; margin-top: 1.2ex;font-family:serif}" \
"QGroupBox::title {subcontrol-origin: margin;subcontrol-position: top left; left:15px;}"
arc_info = True
########################################################################################################################
#
# The first panel of main interface of VIC Hime.
# Mainly for global setting of VIC model.
#
########################################################################################################################
class GlobalConfig(QWidget):
def __init__(self, parent=None):
super(GlobalConfig, self).__init__(parent)
self.parent = parent
#######################################################################
# Global config group
#######################################################################
self.start_time_de = QDateTimeEdit()
self.start_time_de.setDateTime(QDateTime(1949, 1, 1, 0, 0, 0))
self.start_time_de.setDisplayFormat("yyyy-MM-dd")
self.end_time_de = QDateTimeEdit()
self.end_time_de.setDateTime(QDateTime(1950, 12, 31, 0, 0, 0))
self.end_time_de.setDisplayFormat("yyyy-MM-dd")
self.calendars = ["standard", "gregorian", "proleptic_gregorian","noleap",
"365_day", "360_day", "julian", "all_leap", "366_day"]
self.calendar_co = QComboBox()
self.calendar_co.addItems(self.calendars)
self.model_steps_le = QLineEdit()
self.snow_steps_le = QLineEdit()
self.runoff_steps_le = QLineEdit()
self.model_steps_le.setText("1")
self.snow_steps_le.setText("4")
self.runoff_steps_le.setText("4")
self.model_steps_le.setFixedWidth(36)
self.snow_steps_le.setFixedWidth(36)
self.runoff_steps_le.setFixedWidth(36)
self.param_path_le = QLineEdit()
self.param_path_btn = QPushButton("...")
self.param_path_btn.setFixedWidth(36)
self.domain_path_le = QLineEdit()
self.domain_path_btn = QPushButton("...")
self.domain_path_btn.setFixedWidth(36)
self.nodes_le = QLineEdit()
self.nodes_le.setFixedWidth(36)
self.layers_le = QLineEdit()
self.layers_le.setFixedWidth(36)
self.full_energy_cb = QCheckBox("Full energy")
self.close_energy_cb = QCheckBox("Close energy")
self.frozen_soil_cb = QCheckBox("Frozen soil")
self.quick_flux_cb = QCheckBox("Quick flux")
self.snow_bands_cb = QCheckBox("Snow bands")
self.organic_cb = QCheckBox("Organic")
self.organic_fract_cb = QCheckBox("Organic fraction")
self.july_tavg_supplied_cb = QCheckBox("JULY_TAVG supplied")
self.compute_treeline_cb = QCheckBox("Compute treeline")
global_group = QGroupBox()
global_group.setStyleSheet(group_ss)
global_group.setTitle("Global")
global_layout = QVBoxLayout()
global_group.setLayout(global_layout)
sub_top_layout = QGridLayout()
sub_top_layout.addWidget(QLabel("Start time:"), 0, 0, 1, 1)
sub_top_layout.addWidget(self.start_time_de, 0, 1, 1, 1)
sub_top_layout.addWidget(QLabel("End time:"), 1, 0, 1, 1)
sub_top_layout.addWidget(self.end_time_de, 1, 1, 1, 1)
sub_top_layout.addWidget(QLabel("Calendar:"), 2, 0, 1, 1)
sub_top_layout.addWidget(self.calendar_co, 2, 1, 1, 1)
sub_top_layout.addWidget(QLabel("Model steps"), 0, 2, 1, 1)
sub_top_layout.addWidget(QLabel("Snow steps"), 1, 2, 1, 1)
sub_top_layout.addWidget(QLabel("Runoff steps"), 2, 2, 1, 1)
sub_top_layout.addWidget(self.model_steps_le, 0, 3, 1, 1)
sub_top_layout.addWidget(self.snow_steps_le, 1, 3, 1, 1)
sub_top_layout.addWidget(self.runoff_steps_le, 2, 3, 1, 1)
sub_median_layout = QGridLayout()
sub_median_layout.addWidget(QLabel("Parameters file:"), 1, 0, 1, 2)
sub_median_layout.addWidget(self.param_path_le, 1, 2, 1, 4)
sub_median_layout.addWidget(self.param_path_btn, 1, 6, 1, 1)
sub_median_layout.addWidget(QLabel("Domain file:"), 2, 0, 1, 2)
sub_median_layout.addWidget(self.domain_path_le, 2, 2, 1, 4)
sub_median_layout.addWidget(self.domain_path_btn, 2, 6, 1, 1)
sub_median_layout.addWidget(QLabel("Layers:"), 0, 0, 1, 1)
sub_median_layout.addWidget(self.layers_le, 0, 1, 1, 1)
sub_median_layout.addWidget(QLabel("Nodes:"), 0, 3, 1, 1)
sub_median_layout.addWidget(self.nodes_le, 0, 4, 1, 1)
sub_bottom_layout = QGridLayout()
sub_bottom_layout.addWidget(self.full_energy_cb, 0, 0, 1, 2)
sub_bottom_layout.addWidget(self.close_energy_cb, 0, 2, 1, 2)
sub_bottom_layout.addWidget(self.frozen_soil_cb, 1, 0, 1, 2)
sub_bottom_layout.addWidget(self.quick_flux_cb, 1, 2, 1, 2)
sub_bottom_layout.addWidget(self.organic_cb, 2, 0, 1, 2)
sub_bottom_layout.addWidget(self.organic_fract_cb, 2, 2, 1, 2)
sub_bottom_layout.addWidget(self.snow_bands_cb, 3, 0, 1, 2)
sub_bottom_layout.addWidget(self.july_tavg_supplied_cb, 3, 2, 1, 2)
sub_bottom_layout.addWidget(self.compute_treeline_cb, 4, 2, 1, 2)
global_layout.addLayout(sub_top_layout)
global_layout.addLayout(sub_median_layout)
global_layout.addLayout(sub_bottom_layout)
#######################################################################
# Forcings interface setting
#######################################################################
self.forcing_table = QTableWidget()
self.forcing_table.setMinimumWidth(192)
self.forcing_table.setColumnCount(2)
forcing_header = self.forcing_table.horizontalHeader()
forcing_header.setResizeMode(0, QHeaderView.ResizeToContents)
forcing_header.setResizeMode(1, QHeaderView.Stretch)
self.forcing_table.setHorizontalHeaderLabels(["Force type", "nc variable name"])
self.add_forcing_btn = QPushButton("Add item")
self.remove_forcing_btn = QPushButton("Remove item")
self.forcing_path_le = QLineEdit()
self.forcing_path_btn = QPushButton("...")
self.forcing_path_btn.setFixedWidth(36)
forcings_group = QGroupBox()
forcings_group.setStyleSheet(group_ss)
forcings_group.setTitle("Forcing files")
forcings_layout = QGridLayout()
forcings_group.setLayout(forcings_layout)
forcings_layout.addWidget(self.forcing_table, 0, 0, 5, 6)
forcings_layout.addWidget(self.add_forcing_btn, 5, 3, 1, 1)
forcings_layout.addWidget(self.remove_forcing_btn, 5, 4, 1, 2)
forcings_layout.addWidget(QLabel("Path:"), 6, 0, 1, 1)
forcings_layout.addWidget(self.forcing_path_le, 6, 1, 1, 4)
forcings_layout.addWidget(self.forcing_path_btn, 6, 5, 1, 1)
#######################################################################
# Outputs interface setting
#######################################################################
self.output_table = QTableWidget()
self.output_table.setMinimumWidth(192)
self.output_table.setColumnCount(4)
output_header = self.output_table.horizontalHeader()
for i in range(7):
output_header.setResizeMode(i, QHeaderView.ResizeToContents)
self.output_table.setHorizontalHeaderLabels(["Out file", "Out variable", "Out format", "Agg freq"])
self.add_outputs_btn = QPushButton("Add item")
self.remove_outputs_btn = QPushButton("Remove item")
self.output_path_le = QLineEdit()
self.output_path_btn = QPushButton("...")
self.output_path_btn.setFixedWidth(36)
outputs_group = QGroupBox()
outputs_group.setStyleSheet(group_ss)
outputs_group.setTitle("Output files")
outputs_layout = QGridLayout()
outputs_group.setLayout(outputs_layout)
outputs_layout.addWidget(self.output_table, 0, 0, 5, 6)
outputs_layout.addWidget(self.add_outputs_btn, 5, 3, 1, 1)
outputs_layout.addWidget(self.remove_outputs_btn, 5, 4, 1, 2)
outputs_layout.addWidget(QLabel("Path:"), 6, 0, 1, 1)
outputs_layout.addWidget(self.output_path_le, 6, 1, 1, 4)
outputs_layout.addWidget(self.output_path_btn, 6, 5, 1, 1)
#######################################################################
# Bottom buttons
#######################################################################
self.global_path_le = QLineEdit()
self.global_path_le.setMinimumWidth(360)
self.global_path_btn = QPushButton("...")
self.global_path_btn.setFixedWidth(36)
self.apply_configs_btn = QPushButton("&Apply configs")
self.create_global_btn = QPushButton("&Create global file")
button_layout = QHBoxLayout()
button_layout.addStretch(1)
button_layout.addWidget(QLabel("Global file path:"))
button_layout.addWidget(self.global_path_le)
button_layout.addWidget(self.global_path_btn)
button_layout.addStretch(1)
button_layout.addWidget(self.apply_configs_btn)
button_layout.addWidget(self.create_global_btn)
#######################################################################
# Main layout
#######################################################################
main_layout = QVBoxLayout()
top_layout = QHBoxLayout()
left_layout = QVBoxLayout()
right_layout = QVBoxLayout()
left_layout.addWidget(global_group)
left_layout.addStretch(1)
right_layout.addWidget(forcings_group)
right_layout.addWidget(outputs_group)
top_layout.addLayout(left_layout)
top_layout.addLayout(right_layout)
main_layout.addLayout(top_layout)
main_layout.addLayout(button_layout)
self.setLayout(main_layout)
#######################################################################
# Actions of file dialogs (Setting path).
#######################################################################
self.connect(self.param_path_btn, SIGNAL("clicked()"),
lambda: self.set_file_by_dialog(line_edit=self.param_path_le, disc="Set parameters file path"))
self.connect(self.domain_path_btn, SIGNAL("clicked()"),
lambda: self.set_file_by_dialog(line_edit=self.domain_path_le, disc="Set domain file path"))
self.connect(self.global_path_btn, SIGNAL("clicked()"),
lambda: self.set_file_by_dialog(line_edit=self.global_path_le, disc="Set global file path"))
self.connect(self.output_path_btn, SIGNAL("clicked()"),
lambda: self.set_dir_by_dialog(line_edit=self.output_path_le, disc="Set VIC output path"))
self.connect(self.forcing_path_btn, SIGNAL("clicked()"), self.set_forcing_path)
#######################################################################
# Saving, writing global file, and others.
#######################################################################
self.connect(self.apply_configs_btn, SIGNAL("clicked()"), self.apply_configs)
self.connect(self.create_global_btn, SIGNAL("clicked()"), self.write_global_file)
self.connect(self.add_forcing_btn, SIGNAL("clicked()"), lambda: self.add_item(table=self.forcing_table))
self.connect(self.remove_forcing_btn, SIGNAL("clicked()"), lambda: self.remove_item(table=self.forcing_table))
self.connect(self.add_outputs_btn, SIGNAL("clicked()"), lambda: self.add_item(table=self.output_table))
self.connect(self.remove_outputs_btn, SIGNAL("clicked()"), lambda: self.remove_item(table=self.output_table))
#######################################################################
# Parameters
#######################################################################
def load_configs(self):
proj_params = self.parent.proj.proj_params
global_params = self.parent.proj.global_params
self.global_path_le.setText(unicode(proj_params["global_file"]))
self.start_time_de.setDateTime(global_params["start_time"])
self.end_time_de.setDateTime(global_params["end_time"])
calendar = global_params["calendar"]
for i in range(self.calendar_co.count()):
if self.calendar_co.itemText(i) == calendar:
self.calendar_co.setCurrentIndex(i)
self.model_steps_le.setText(unicode(global_params["model_steps_per_day"]))
self.snow_steps_le.setText(unicode(global_params["snow_steps_per_day"]))
self.runoff_steps_le.setText(unicode(global_params["runoff_steps_per_day"]))
self.nodes_le.setText(unicode(global_params["nodes"]))
self.layers_le.setText(unicode(global_params["nlayer"]))
self.param_path_le.setText(unicode(global_params["param_file"]))
self.domain_path_le.setText(unicode(global_params["domain_file"]))
if global_params.get("full_energy") == "TRUE":
self.full_energy_cb.setCheckState(Qt.Checked)
else:
self.full_energy_cb.setCheckState(Qt.Unchecked)
if global_params.get("close_energy") == "TRUE":
self.close_energy_cb.setCheckState(Qt.Checked)
else:
self.close_energy_cb.setCheckState(Qt.Unchecked)
if global_params.get("frozen_soil") == "TRUE":
self.frozen_soil_cb.setCheckState(Qt.Checked)
else:
self.frozen_soil_cb.setCheckState(Qt.Unchecked)
if global_params.get("quick_flux") == "TRUE":
self.quick_flux_cb.setCheckState(Qt.Checked)
else:
self.quick_flux_cb.setCheckState(Qt.Unchecked)
if global_params.get("organic") == "TRUE":
self.organic_cb.setCheckState(Qt.Checked)
else:
self.organic_cb.setCheckState(Qt.Unchecked)
if global_params.get("organic_fract") == "TRUE":
self.organic_fract_cb.setCheckState(Qt.Checked)
else:
self.organic_fract_cb.setCheckState(Qt.Unchecked)
if global_params.get("snow_band") == "TRUE":
self.snow_bands_cb.setCheckState(Qt.Checked)
else:
self.snow_bands_cb.setCheckState(Qt.Unchecked)
if global_params.get("july_tavg") == "TRUE":
self.july_tavg_supplied_cb.setCheckState(Qt.Checked)
else:
self.july_tavg_supplied_cb.setCheckState(Qt.Unchecked)
if global_params.get("compute_treeline") == "TRUE":
self.compute_treeline_cb.setCheckState(Qt.Checked)
else:
self.compute_treeline_cb.setCheckState(Qt.Unchecked)
# Forcings.
self.forcing_path_le.setText(unicode(global_params["forcing1"]["file_path"]))
forcing1_types = global_params["forcing1"]["force_type"]
self.forcing_table.setRowCount(len(forcing1_types))
for i in range(len(forcing1_types)):
key = forcing1_types.keys()[i]
self.forcing_table.setItem(i, 0, QTableWidgetItem(key))
self.forcing_table.setItem(i, 1, QTableWidgetItem(forcing1_types[key]))
# Outputs.
self.output_path_le.setText(unicode(global_params["out_path"]))
r = 0
for f in global_params["out_file"]:
for type in f["out_var"]:
self.output_table.setRowCount(r + 1)
self.output_table.setItem(r, 0, QTableWidgetItem(f["out_file"]))
self.output_table.setItem(r, 1, QTableWidgetItem(type))
self.output_table.setItem(r, 2, QTableWidgetItem(f["out_format"]))
self.output_table.setItem(r, 3, QTableWidgetItem(f["aggfreq"]))
r += 1
def apply_configs(self):
proj_params = self.parent.proj.proj_params
global_params = self.parent.proj.global_params
proj_params["global_file"] = unicode(self.global_path_le.text())
global_params["start_time"] = self.start_time_de.dateTime().toPyDateTime()
global_params["end_time"] = self.end_time_de.dateTime().toPyDateTime()
global_params["calendar"] = unicode(self.calendar_co.currentText())
global_params["model_steps_per_day"] = int(self.model_steps_le.text())
global_params["snow_steps_per_day"] = int(self.model_steps_le.text())
global_params["runoff_steps_per_day"] = int(self.model_steps_le.text())
global_params["nodes"] = int(self.nodes_le.text())
global_params["nlayer"] = int(self.layers_le.text())
global_params["param_file"] = unicode(self.param_path_le.text())
global_params["domain_file"] = unicode(self.domain_path_le.text())
global_params["full_energy"] = "TRUE" if self.full_energy_cb.isChecked() else "False"
global_params["close_energy"] = "TRUE" if self.close_energy_cb.isChecked() else "False"
global_params["frozen_soil"] = "TRUE" if self.frozen_soil_cb.isChecked() else "False"
global_params["quick_flux"] = "TRUE" if self.quick_flux_cb.isChecked() else "False"
global_params["organic"] = "TRUE" if self.organic_cb.isChecked() else "False"
global_params["organic_fract"] = "TRUE" if self.organic_fract_cb.isChecked() else "False"
global_params["snow_band"] = "TRUE" if self.snow_bands_cb.isChecked() else "False"
global_params["july_tavg"] = "TRUE" if self.july_tavg_supplied_cb.isChecked() else "False"
global_params["compute_treeline"] = "TRUE" if self.compute_treeline_cb.isChecked() else "False"
# Forcings.
global_params["forcing1"]["file_path"] = unicode(self.forcing_path_le.text())
global_params["forcing1"]["force_type"] = OrderedDict()
forcing1_types = global_params["forcing1"]["force_type"]
for i in range(self.forcing_table.rowCount()):
key = unicode(self.forcing_table.item(i, 0).text())
value = unicode(self.forcing_table.item(i, 1).text())
forcing1_types[key] = value
# Outputs.
global_params["out_path"] = unicode(self.output_path_le.text())
global_params["out_file"] = []
current_file = None
for i in range(self.output_table.rowCount()):
out_file = unicode(self.output_table.item(i, 0).text())
out_var = unicode(self.output_table.item(i, 1).text())
out_format = unicode(self.output_table.item(i, 2).text())
agg_freq = unicode(self.output_table.item(i, 3).text())
if current_file != out_file:
current_file = out_file
current_info = OrderedDict()
current_info["out_file"] = current_file
current_info["out_format"] = out_format
current_info["compress"] = "FALSE"
current_info["aggfreq"] = agg_freq
current_info["out_var"] = []
global_params["out_file"].append(current_info)
current_info["out_var"].append(out_var)
self.parent.dirty = True
log.info("Configs has been applied.")
def set_file_by_dialog(self, line_edit, disc):
ddir = os.path.expanduser('~')
if self.parent.proj is not None:
ddir = self.parent.proj.proj_params["proj_path"]
file = QFileDialog.getOpenFileName(self, disc, ddir)
log.debug("Get file: %s" % file)
if file == "":
return
line_edit.setText(file)
def set_dir_by_dialog(self, line_edit, disc):
ddir = os.path.expanduser('~')
if self.parent.proj is not None:
ddir = self.parent.proj.proj_params["proj_path"]
dir = QFileDialog.getExistingDirectory(self, disc, ddir)
log.debug("Get dir: %s" % dir)
if dir == "":
return
line_edit.setText(dir)
def set_forcing_path(self):
ddir = os.path.expanduser('~')
if self.parent.proj is not None:
ddir = self.parent.proj.proj_params["proj_path"]
forcing_file = QFileDialog.getOpenFileName(self, "Set forcing files", ddir)
log.debug("Get file: %s" % forcing_file)
if forcing_file == "":
return
forcing_file = re.sub(r"\d\d\d\d\.nc", "", unicode(forcing_file))
self.forcing_path_le.setText(forcing_file)
def add_item(self, table):
nrow_o = table.rowCount()
table.setRowCount(nrow_o + 1)
if nrow_o < 1:
return
for i in range(table.columnCount()):
table.setItem(nrow_o, i, QTableWidgetItem(table.item(nrow_o-1, i)))
def remove_item(self, table):
rs = [ind.row() for ind in table.selectedIndexes()]
rs = list(set(rs))
rs.sort(reverse=True)
[table.removeRow(r) for r in rs]
log.debug("Remove row %s" % rs)
def write_global_file(self):
self.parent.proj.write_global_file()
########################################################################################################################
#
# The second panel of main interface of VIC Hime.
# Mainly to run VIC model.
#
########################################################################################################################
class VicRun(QWidget):
def __init__(self, parent=None):
super(VicRun, self).__init__(parent)
self.parent = parent
#######################################################################
# Driver config group
#######################################################################
self.vic_driver_le = QLineEdit()
self.vic_driver_le.setMinimumWidth(128)
self.cores_le = QLineEdit()
self.cores_le.setFixedWidth(36)
self.vic_driver_btn = QPushButton("...")
self.vic_driver_btn.setFixedWidth(36)
driver_group = QGroupBox()
driver_group.setStyleSheet(group_ss)
driver_group.setTitle("VIC driver")
driver_group.setMinimumWidth(420)
driver_layout = QHBoxLayout()
driver_group.setLayout(driver_layout)
driver_layout.addWidget(QLabel("VIC driver path:"))
driver_layout.addWidget(self.vic_driver_le)
driver_layout.addWidget(self.vic_driver_btn)
driver_layout.addStretch(1)
driver_layout.addWidget(QLabel("Cores:"))
driver_layout.addWidget(self.cores_le)
#######################################################################
# Input file config group
#######################################################################
self.global_file_le = QLineEdit()
self.global_file_btn = QPushButton("...")
self.global_file_btn.setFixedWidth(36)
self.rout_cb = QCheckBox("With routing")
self.rout_data_le = QLineEdit()
self.rout_data_btn = QPushButton("...")
self.rout_data_btn.setFixedWidth(36)
self.rout_out_path_le = QLineEdit()
self.rout_out_path_btn = QPushButton("...")
self.rout_out_path_btn.setFixedWidth(36)
self.vic_output_le = QLineEdit()
self.vic_output_btn = QPushButton("...")
self.vic_output_btn.setFixedWidth(36)
self.apply_configs_btn = QPushButton("&Apply configs")
self.mpi_cb = QCheckBox("Run with MPI")
self.run_range_le = QLineEdit()
self.apply_configs_btn = QPushButton("&Apply configs")
self.run_btn = QPushButton("&Run VIC")
self.vic_run_console = QTextBrowser()
input_file_group = QGroupBox()
input_file_group.setStyleSheet(group_ss)
input_file_group.setTitle("Input file")
input_file_group.setMinimumWidth(420)
input_file_layout = QGridLayout()
input_file_layout.addWidget(QLabel("Global file:"), 0, 0)
input_file_layout.addWidget(self.global_file_le, 0, 1, 1, 3)
input_file_layout.addWidget(self.global_file_btn, 0, 4, 1, 1)
input_file_layout.addWidget(self.rout_cb, 1, 0, 1, 1)
input_file_layout.addWidget(self.mpi_cb, 1, 2, 1, 1)
input_file_layout.addWidget(self.apply_configs_btn, 5, 5, 1, 1)
input_file_layout.addWidget(self.run_btn, 5, 7, 1, 1)
input_file_group.setLayout(input_file_layout)
main_layout = QVBoxLayout()
main_layout.addWidget(driver_group)
main_layout.addWidget(input_file_group)
# main_layout.addStretch(1)
main_layout.addWidget(self.vic_run_console)
self.setLayout(main_layout)
#######################################################################
# Connections.
#######################################################################
self.connect(self.vic_driver_btn, SIGNAL("clicked()"),
lambda: self.set_file_by_dialog(line_edit=self.vic_driver_le, disc="Set VIC driver file path"))
self.connect(self.global_file_btn, SIGNAL("clicked()"),
lambda: self.set_file_by_dialog(line_edit=self.global_file_le, disc="Set global file path"))
self.connect(self.vic_output_btn, SIGNAL("clicked()"),
lambda: self.set_file_by_dialog(line_edit=self.vic_output_le, disc="Set VIC output file path."))
self.connect(self.rout_data_btn, SIGNAL("clicked()"),
lambda: self.set_file_by_dialog(line_edit=self.rout_data_le, disc="Set routing data path."))
self.connect(self.rout_out_path_btn, SIGNAL("clicked()"),
lambda: self.set_dir_by_dialog(line_edit=self.rout_out_path_le, disc="Set routing output path."))
self.connect(self.run_btn, SIGNAL("clicked()"), self.start_vic)
self.connect(self.apply_configs_btn, SIGNAL("clicked()"), self.apply_configs)
#######################################################################
# Business part
#######################################################################
self.configs = None
self.vic_running = False
self.vic_run_thread = VICRunThread(self)
def set_file_by_dialog(self, line_edit, disc):
ddir = os.path.expanduser('~')
if self.parent.proj is not None:
ddir = self.parent.proj.proj_params["proj_path"]
file = QFileDialog.getOpenFileName(self, disc, ddir)
log.debug("Open file: %s" % file)
if file == "":
return
line_edit.setText(file)
def set_dir_by_dialog(self, line_edit, disc):
ddir = os.path.expanduser('~')
if self.parent.proj is not None:
ddir = self.parent.proj.proj_params["proj_path"]
dir = QFileDialog.getExistingDirectory(self, disc, ddir)
log.debug("Open directory: %s" % dir)
if dir == "":
return
line_edit.setText(dir)
def output_writen(self, text):
cursor = self.parent.vic_run_console.textCursor()
cursor.movePosition(QTextCursor.End)
cursor.insertText(text)
self.parent.vic_run_console.setTextCursor(cursor)
self.parent.vic_run_console.ensureCursorVisible()
def run_vic(self):
# If you want to routing after run vic you should configure the routing options.
routing_configs = None
rout_data = None
if self.rout_cb.isChecked():
if self.parent.proj.proj_params.get("routing_config") is None:
log.error("Routing configs was not set. Can not run with routing.")
return
routing_configs = self.parent.proj.proj_params["routing_config"]
rout_data = load_rout_data(routing_configs["rout_data_file"])
run_range = rout_data["basin"]
domain_file = self.parent.proj.global_params["domain_file"]
set_nc_value(domain_file, "mask", 0)
set_nc_value(domain_file, "mask", 1, mask=run_range)
log.info("VIC start to run...")
self.vic_running = True
vic_path = unicode(self.vic_driver_le.text())
n_cores = unicode(self.cores_le.text())
use_mpi = True if self.mpi_cb.isChecked() else False
# Execute VIC image driver.
global_file = unicode(self.global_file_le.text())
status, logs_out, logs_err = vic_exec(vic_path, global_file, mpi=use_mpi, n_cores=n_cores)
# vic_logs = []
# for line in logs_out[-1024:]:
# vic_logs.append(line)
# for line in logs_err:
# vic_logs.append(line)
# cursor = self.vic_run_console.textCursor()
# [cursor.insertText(line) for line in vic_logs]
self.parent.vic_running = False
if status != 0:
log.error("Error in VIC running.")
return
log.info("VIC running complete.")
if self.rout_cb.isChecked():
vic_out_file = routing_configs["vic_out_file"]
domain_file = routing_configs["domain_file"]
symd = routing_configs["start_date"]
eymd = routing_configs["end_date"]
out_dir = routing_configs["rout_output_dir"]
runoffs = confluence(vic_out_file, rout_data, domain_file,
dt(symd[0], symd[1], symd[2]), dt(eymd[0], eymd[1], eymd[2]))
runoffs_m = gather_to_month(runoffs)
try:
os.makedirs(out_dir)
except Exception:
pass
stn_name = rout_data["name"]
write_runoff_data(runoffs, join(out_dir, stn_name + "_daily.txt"))
write_runoff_data(runoffs_m, join(out_dir, stn_name + "_monthly.txt"))
log.info("Routing complete.")
def start_vic(self):
self.vic_run_thread.start()
def apply_configs(self):
self.configs["vic_driver_path"] = unicode(self.vic_driver_le.text())
self.configs["n_cores"] = unicode(self.cores_le.text())
self.configs["global_file"] = unicode(self.global_file_le.text())
if self.mpi_cb.isChecked():
self.configs["with_mpi"] = True
else:
self.configs["with_mpi"] = False
if self.rout_cb.isChecked():
self.configs["with_routing"] = True
else:
self.configs["with_routing"] = False
self.parent.proj.proj_params["vic_run_config"] = self.configs
self.parent.dirty = True
log.info("Configs has been applied.")
def load_configs(self):
if self.parent.proj.proj_params.get("vic_run_config") is None:
self.configs = OrderedDict()
self.configs["vic_driver_path"] = "None"
self.configs["n_cores"] = "4"
self.configs["global_file"] = self.parent.proj.proj_params["global_file"]
self.configs["with_mpi"] = False
self.configs["with_routing"] = False
else:
self.configs = self.parent.proj.proj_params["vic_run_config"]
self.vic_driver_le.setText(self.configs["vic_driver_path"])
self.cores_le.setText(self.configs["n_cores"])
self.global_file_le.setText(self.configs["global_file"])
if self.configs["with_mpi"]:
self.mpi_cb.setCheckState(Qt.Checked)
else:
self.mpi_cb.setCheckState(Qt.Unchecked)
if self.configs["with_routing"]:
self.rout_cb.setCheckState(Qt.Checked)
else:
self.rout_cb.setCheckState(Qt.Unchecked)
###########################################################################
# Read in run range of VIC gridcells, first try to read from rout data file,
# if fail read as simple ascii data file.
###########################################################################
def get_run_range(self, run_range_file):
try:
may_rout_data = load_rout_data(run_range_file)
run_range = may_rout_data["basin"]
except:
run_range = np.loadtxt(run_range_file)
return run_range
class VICRunThread(QThread):
def __init__(self, parent=None):
super(VICRunThread, self).__init__(parent)
self.parent = parent
def run(self):
self.parent.run_vic()
class StreamEmitter(QObject):
def __init__(self, parent=None, text_written=None):
super(StreamEmitter, self).__init__(parent)
self.parent = parent
if text_written is None:
self.text_written = pyqtSignal(str)
def write(self, text):
self.text_written.emit(str(text))
########################################################################################################################
#
# The third panel of main interface of VIC Hime.
# Mainly to run Routing model for VIC.
#
########################################################################################################################
class Routing(QWidget):
def __init__(self, parent=None):
super(Routing, self).__init__(parent)
self.parent = parent
#######################################################################
# Rout data creating group
#######################################################################
self.direc_file_le = QLineEdit()
self.direc_file_btn = QPushButton("...")
self.direc_file_btn.setFixedWidth(36)
self.veloc_file_le = QLineEdit()
self.veloc_file_btn = QPushButton("...")
self.veloc_file_btn.setFixedWidth(36)
self.diffu_file_le = QLineEdit()
self.diffu_file_btn = QPushButton("...")
self.diffu_file_btn.setFixedWidth(36)
self.uh_slope_data_le = QLineEdit()
self.uh_slope_data_btn = QPushButton("...")
self.uh_slope_data_btn.setFixedWidth(36)
self.out_rout_data_le = QLineEdit()
self.out_rout_data_btn = QPushButton("...")
self.out_rout_data_btn.setFixedWidth(36)
self.stn_name_le = QLineEdit()
self.stn_name_le.setFixedWidth(128)
self.stn_x_le = QLineEdit()
self.stn_x_le.setFixedWidth(36)
self.stn_y_le = QLineEdit()
self.stn_y_le.setFixedWidth(36)
self.create_rout_data_btn = QPushButton("&Create rout data")
rout_data_group = QGroupBox()
rout_data_group.setStyleSheet(group_ss)
rout_data_layout = QGridLayout()
rout_data_group.setLayout(rout_data_layout)
rout_data_group.setTitle("Rout data create")
rout_data_layout.addWidget(QLabel("Direction file:"), 0, 0)
rout_data_layout.addWidget(self.direc_file_le, 0, 1, 1, 10)
rout_data_layout.addWidget(self.direc_file_btn, 0, 11, 1, 1)
rout_data_layout.addWidget(QLabel("Velocity file:"), 1, 0)
rout_data_layout.addWidget(self.veloc_file_le, 1, 1, 1, 10)
rout_data_layout.addWidget(self.veloc_file_btn, 1, 11, 1, 1)
rout_data_layout.addWidget(QLabel("Diffusion file:"), 2, 0)
rout_data_layout.addWidget(self.diffu_file_le, 2, 1, 1, 10)
rout_data_layout.addWidget(self.diffu_file_btn, 2, 11, 1, 1)
rout_data_layout.addWidget(QLabel("Slope UH file:"), 3, 0)
rout_data_layout.addWidget(self.uh_slope_data_le, 3, 1, 1, 10)
rout_data_layout.addWidget(self.uh_slope_data_btn, 3, 11, 1, 1)
rout_data_layout.addWidget(QLabel("Station name:"), 4, 0)
rout_data_layout.addWidget(self.stn_name_le, 4, 1, 1, 3)
rout_data_layout.addWidget(QLabel("Station location Column:"), 5, 0, 1, 2)
rout_data_layout.addWidget(self.stn_x_le, 5, 2)
rout_data_layout.addWidget(QLabel("Row:"), 5, 3)
rout_data_layout.addWidget(self.stn_y_le, 5, 4)
rout_data_layout.addWidget(QLabel("Rout data output path:"), 6, 0, 1, 2)
rout_data_layout.addWidget(self.out_rout_data_le, 6, 2, 1, 9)
rout_data_layout.addWidget(self.out_rout_data_btn, 6, 11, 1, 1)
rout_data_layout.addWidget(self.create_rout_data_btn, 7, 10, 1, 2)
#######################################################################
# Routing group
#######################################################################
self.vic_out_file_le = QLineEdit()
self.vic_out_file_btn = QPushButton("...")
self.vic_out_file_btn.setFixedWidth(36)
self.domain_file_le = QLineEdit()
self.domain_file_btn = QPushButton("...")
self.domain_file_btn.setFixedWidth(36)
self.rout_data_file_le = QLineEdit()
self.rout_data_file_btn = QPushButton("...")
self.rout_data_file_btn.setFixedWidth(36)
self.rout_out_dir_le = QLineEdit()
self.rout_out_dir_btn = QPushButton("...")
self.rout_out_dir_btn.setFixedWidth(36)
self.start_date_de = QDateTimeEdit()
self.start_date_de.setDisplayFormat("yyyy-MM-dd")
self.end_date_de = QDateTimeEdit()
self.end_date_de.setDisplayFormat("yyyy-MM-dd")
self.apply_configs_btn = QPushButton("&Apply configs")
self.routing_btn = QPushButton("&Routing")
routing_group = QGroupBox()
routing_group.setStyleSheet(group_ss)
routing_group.setTitle("Routing")
routing_layout = QGridLayout()
routing_group.setLayout(routing_layout)
routing_layout.addWidget(QLabel("VIC output file:"), 0, 0)
routing_layout.addWidget(self.vic_out_file_le, 0, 1, 1, 8)
routing_layout.addWidget(self.vic_out_file_btn, 0, 9)
routing_layout.addWidget(QLabel("Domain file:"), 1, 0)
routing_layout.addWidget(self.domain_file_le, 1, 1, 1, 8)
routing_layout.addWidget(self.domain_file_btn, 1, 9)
routing_layout.addWidget(QLabel("Rout data file:"), 2, 0)
routing_layout.addWidget(self.rout_data_file_le, 2, 1, 1, 8)
routing_layout.addWidget(self.rout_data_file_btn, 2, 9)
routing_layout.addWidget(QLabel("Start date:"), 3, 0)
routing_layout.addWidget(self.start_date_de, 3, 1, 1, 3)
routing_layout.addWidget(QLabel("End date:"), 4, 0)
routing_layout.addWidget(self.end_date_de, 4, 1, 1, 3)
routing_layout.addWidget(QLabel("Routing output path:"), 5, 0, 1, 2)
routing_layout.addWidget(self.rout_out_dir_le, 5, 2, 1, 7)
routing_layout.addWidget(self.rout_out_dir_btn, 5, 9)
routing_layout.addWidget(self.apply_configs_btn, 6, 6, 1, 2)
routing_layout.addWidget(self.routing_btn, 6, 8, 1, 2)
main_layout = QVBoxLayout()
main_layout.addWidget(rout_data_group)
main_layout.addWidget(routing_group)
self.setLayout(main_layout)
self.connect(self.direc_file_btn, SIGNAL("clicked()"),
lambda: self.set_file_by_dialog(line_edit=self.direc_file_le, disc="Set flow direction file path"))
self.connect(self.veloc_file_btn, SIGNAL("clicked()"),
lambda: self.set_file_by_dialog(line_edit=self.veloc_file_le, disc="Set velocity file path"))
self.connect(self.diffu_file_btn, SIGNAL("clicked()"),
lambda: self.set_file_by_dialog(line_edit=self.direc_file_le, disc="Set diffusion file path"))
self.connect(self.uh_slope_data_btn, SIGNAL("clicked()"),
lambda: self.set_file_by_dialog(line_edit=self.uh_slope_data_le, disc="Set Slope UH file path"))
self.connect(self.out_rout_data_btn, SIGNAL("clicked()"),
lambda: self.set_file_by_dialog(line_edit=self.out_rout_data_le, disc="Set Slope UH file path"))
self.connect(self.vic_out_file_btn, SIGNAL("clicked()"),
lambda: self.set_file_by_dialog(line_edit=self.vic_out_file_le, disc="Set Slope UH file path"))
self.connect(self.domain_file_btn, SIGNAL("clicked()"),
lambda: self.set_file_by_dialog(line_edit=self.domain_file_le, disc="Set Slope UH file path"))
self.connect(self.rout_data_file_btn, SIGNAL("clicked()"),
lambda: self.set_file_by_dialog(line_edit=self.rout_data_file_le, disc="Set Slope UH file path"))
self.connect(self.rout_out_dir_btn, SIGNAL("clicked()"),
lambda: self.set_dir_by_dialog(line_edit=self.rout_out_dir_le, disc="Set Slope UH file path"))
self.connect(self.create_rout_data_btn, SIGNAL("clicked()"), self.create_rout_data)
self.connect(self.apply_configs_btn, SIGNAL("clicked()"), self.apply_configs)
self.connect(self.routing_btn, SIGNAL("clicked()"), self.routing)
#######################################################################
# Business part
#######################################################################
self.configs = None
def set_file_by_dialog(self, line_edit, disc):
ddir = os.path.expanduser('~')
if self.parent.proj is not None:
ddir = self.parent.proj.proj_params["proj_path"]
file = QFileDialog.getOpenFileName(self, disc, ddir)
log.debug("Open file: %s" % file)
if file == "":
return
line_edit.setText(file)
def set_dir_by_dialog(self, line_edit, disc):
ddir = os.path.expanduser('~')
if self.parent.proj is not None:
ddir = self.parent.proj.proj_params["proj_path"]
dir = QFileDialog.getExistingDirectory(self, disc, ddir)
log.debug("Open directory: %s" % dir)
if dir == "":
return
line_edit.setText(dir)
def apply_configs(self):
self.configs["direc_file"] = unicode(self.direc_file_le.text())
self.configs["veloc_file"] = unicode(self.veloc_file_le.text())
self.configs["diffu_file"] = unicode(self.diffu_file_le.text())
self.configs["uh_slope"] = unicode(self.uh_slope_data_le.text())
self.configs["station_name"] = unicode(self.stn_name_le.text())
self.configs["station_row"] = unicode(self.stn_x_le.text())
self.configs["station_col"] = unicode(self.stn_y_le.text())
self.configs["out_rout_data"] = unicode(self.out_rout_data_le.text())
self.configs["vic_out_file"] = unicode(self.vic_out_file_le.text())
self.configs["domain_file"] = unicode(self.domain_file_le.text())
self.configs["rout_data_file"] = unicode(self.rout_data_file_le.text())
self.configs["rout_output_dir"] = unicode(self.rout_out_dir_le.text())
self.configs["start_date"] = list(self.start_date_de.date().getDate())
self.configs["end_date"] = list(self.end_date_de.date().getDate())
self.parent.proj.proj_params["routing_config"] = self.configs
self.parent.dirty = True
log.info("Configs has been applied.")
def load_configs(self):
if self.parent.proj.proj_params.get("routing_config") is None:
self.configs = OrderedDict()
self.configs["direc_file"] = "None"
self.configs["veloc_file"] = "1.5"
self.configs["diffu_file"] = "800"
self.configs["uh_slope"] = "from_template"
self.configs["station_name"] = "NONE"
self.configs["station_row"] = 0
self.configs["station_col"] = 0
self.configs["out_rout_data"] = "None"
self.configs["vic_out_file"] = "None"
self.configs["domain_file"] = "None"
self.configs["rout_data_file"] = "None"
self.configs["rout_output_dir"] = "None"
self.configs["start_date"] = [1960, 1, 1]
self.configs["end_date"] = [1970, 12, 31]
else:
self.configs = self.parent.proj.proj_params["routing_config"]
self.direc_file_le.setText(self.configs["direc_file"])
self.veloc_file_le.setText(self.configs["veloc_file"])
self.diffu_file_le.setText(self.configs["diffu_file"])
self.uh_slope_data_le.setText(self.configs["uh_slope"])
self.stn_name_le.setText(self.configs["station_name"])
self.stn_x_le.setText(unicode(self.configs["station_row"]))