This repository has been archived by the owner on May 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 112
/
cli_app.py
1477 lines (1258 loc) · 69.3 KB
/
cli_app.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
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import curses
import sys
import os
import numpy as np
import getpass
import urllib2
import json
from glob import glob
from netCDF4 import Dataset
from datetime import datetime, timedelta
import ocw.metrics as metrics
import ocw.plotter as plotter
import ocw.dataset_processor as dsp
import ocw.evaluation as evaluation
import ocw.data_source.rcmed as rcmed
from ocw.dataset import Bounds
from ocw.data_source.local import load_file
import ocw.utils as utils
import ocw.data_source.esgf as esgf
from ocw_config_runner.configuration_writer import export_evaluation_to_config
import ssl
if hasattr(ssl, '_create_unverified_context'):
ssl._create_default_https_context = ssl._create_unverified_context
def ready_screen(page, note=""):
''' Generates page borders, header, footer and notification center.
:param page: Name of current page
:type page: string
:param note: Notification that system returns and will be shown
at the bottom of page
:type note: string
:returns: y and x as location of text on screen
:rtype: integer
'''
screen.clear()
y, x = screen.getmaxyx()
screen.border(0)
screen.addstr(0, x/2-len(TITLE)/2, TITLE)
screen.addstr(y-1, x/2-len(ORGANIZATION)/2, ORGANIZATION)
screen.addstr(y-3, 1, "Notification:")
for each in range(1, x-1):
screen.addstr(y-4, each, "-")
if page == "main_menu":
screen.addstr(y-3, x-21, "(NC) = Not complete")
screen.addstr(y-2, x-21, "(C) = Complete")
if page == "settings_screen":
for i in range(y-5):
screen.addstr(i+1, x/2-2, ".")
screen.addstr(y-2, 1, note)
return y, x
def get_esgf_netCDF_file_name(esgf_dataset_id, esgf_variable):
dataset_info = esgf._get_file_download_data(esgf_dataset_id, esgf_variable)
netCDF_name = dataset_info[0][0].split("/")[-1]
return netCDF_name
##############################################################
# Manage Model Screen
##############################################################
def load_local_model_screen(header):
'''Generates screen to be able to load local model file.
Path to model file (netCDF) and variable name is required.
:param header: Header of page
:type header: string
:returns: Notification
:rtype: string
'''
ready_screen("load_local_model_screen")
screen.addstr(1, 1, header + " > Load Local Model File ")
screen.addstr(4, 3, "[0]: Enter model full path")
screen.addstr(5, 3, "[1]: Select from existing model file/s")
screen.addstr(3, 2, "Select Option: ")
model_selection_option = screen.getstr()
if model_selection_option == '0':
screen.addstr(7, 2, "Enter model (netCDF) full path: ")
model_full_path = screen.getstr()
model_full_path = model_full_path.replace(" ", "")
try:
netCDF_file = Dataset(model_full_path, 'r')
all_netcdf_variables = [variable.encode() for variable in netCDF_file.variables.keys()]
screen.addstr(9, 2, "Enter model variable name {0}: ".format(all_netcdf_variables))
variable_name = screen.getstr()
try:
variable_info = format(netCDF_file.variables[variable_name]).splitlines()
for i, info in enumerate(variable_info):
screen.addstr(11 + i, 2, info)
screen.addstr(22, 2, "Confirm:")
screen.addstr(23, 4, "0- No")
screen.addstr(24, 4, "1- Yes")
screen.addstr(25, 3, "Would you take this variable:")
answer = screen.getstr()
if answer == "0":
note = "WARNING: Model file cannot be added."
elif answer == "1":
model_dataset = load_file(model_full_path, variable_name)
model_datasets.append(dsp.normalize_dataset_datetimes(model_dataset, model_dataset.temporal_resolution()))
models_info.append({'directory': model_full_path, 'variable_name': variable_name})
note = "Model file successfully added."
else:
note = "WARNING: Model file cannot be added."
except:
note = "WARNING: Model file cannot be added. The variable [{0}] is not accepted. Please try again.".format(variable_name)
except:
note = "WARNING: Model file cannot be read. Please check the file directory or format. Only netCDF format is accepted."
elif model_selection_option == '1':
model_path = glob('./data/*.nc')
for imodel, model in enumerate(model_path):
screen.addstr(8 + imodel, 14, '[%d]: ' %imodel + model[7:])
screen.addstr(7, 2, "Select Model (Model-ID): ")
model_id = screen.getstr()
try:
model_id = int(model_id)
netCDF_file = Dataset(model_path[model_id], 'r')
all_netcdf_variables = [variable.encode() for variable in netCDF_file.variables.keys()]
screen.addstr(10 + imodel, 2, "Enter model variable name {0}: ".format(all_netcdf_variables))
variable_name = screen.getstr()
try:
variable_info = format(netCDF_file.variables[variable_name]).splitlines()
for ii, info in enumerate(variable_info):
screen.addstr(11 + imodel + ii, 2, info)
screen.addstr(17 + ii, 2, "Confirm:")
screen.addstr(18 + ii, 4, "0- No")
screen.addstr(19 + ii, 4, "1- Yes")
screen.addstr(20 + ii, 3, "Would you take this variable:")
answer = screen.getstr()
if answer == "0":
note = "WARNING: Model file cannot be added."
elif answer == "1":
model_dataset = load_file(model_path[model_id], variable_name)
model_datasets.append(dsp.normalize_dataset_datetimes(model_dataset, model_dataset.temporal_resolution()))
models_info.append({'directory': model_path, 'variable_name': variable_name})
note = "Model file successfully added."
else:
note = "WARNING: Model file cannot be added."
except:
note = "WARNING: Model file cannot be added. The variable [{0}] is not accepted. Please try again.".format(variable_name)
netCDF_file.close()
except:
note = "WARNING: Model file cannot be read. Please check the file directory or format. Only netCDF format is accepted."
else:
note = "WARNING: Model file cannot be added."
return note
def load_esgf_model_screen(header):
'''Generates screen to be able to load ESGF model file.
:param header: Header of page
:type header: string
:returns: Notification
:rtype: string
'''
ready_screen("load_esgf_model_screen")
screen.addstr(1, 1, header + " > Download ESGF Dataset ")
screen.addstr(6, 1, "Enter Dataset ID:")
esgf_dataset_id = screen.getstr()
screen.addstr(7, 1, "Enter Variable:")
esgf_variable = screen.getstr()
screen.addstr(8, 1, "Enter Username:")
esgf_username = screen.getstr()
screen.addstr(9, 1, "Enter Password:")
esgf_password = screen.getstr()
if esgf_dataset_id and esgf_variable and esgf_username and esgf_password:
try:
solr_url = "http://esg-datanode.jpl.nasa.gov/esg-search/search?id={0}&variable={1}&format=application%2Fsolr%2Bjson".format(esgf_dataset_id, esgf_variable)
metadata_json = json.load(urllib2.urlopen(solr_url))
if metadata_json['response']['docs'][0]["product"][0] != "observations":
screen.addstr(11, 4, "Title: {0}".format(metadata_json['response']['docs'][0]['title']))
screen.addstr(12, 4, "Start Date: {0}".format(metadata_json['response']['docs'][0]['datetime_start']))
screen.addstr(13, 4, "End Date: {0}".format(metadata_json['response']['docs'][0]['datetime_stop']))
screen.addstr(15, 2, "Confirm:")
screen.addstr(16, 4, "0- No")
screen.addstr(17, 4, "1- Yes")
screen.addstr(18, 3, "Would you take this dataset:")
answer = screen.getstr()
if answer == "0":
note = "WARNING: ESGF model file cannot be added."
elif answer == "1":
try:
screen.addstr(20, 4, "Downloading dataset.....")
screen.refresh()
datasets = esgf.load_dataset(esgf_dataset_id,
esgf_variable,
esgf_username,
esgf_password)
netCDF_name = get_esgf_netCDF_file_name(esgf_dataset_id, esgf_variable)
netCDF_path = "/tmp/{0}".format(netCDF_name)
model_dataset = load_file(netCDF_path, esgf_variable)
model_datasets.append(model_dataset)
models_info.append({'directory': netCDF_path, 'variable_name': esgf_variable})
note = "Dataset successfully downloaded."
except:
note = "WARNING: Dataset has not been downloaded. Check your ESGF permission."
else:
note = "The selected dataset is Observation, please enter model dataset."
except:
note = "WARNING: Something went wrong in downloading model dataset from ESGF."
else:
note = "WARNING: No information given and no ESGF dataset has been downloaded."
return note
def unload_model_screen(header):
'''Generates screen to be able to unload model file.
It lists all loaded model with index for each.
Selection of model with index will remove model from list of models.
:param header: Header of page
:type header: string
:returns: Notification
:rtype: string
'''
ready_screen("unload_model_screen")
screen.addstr(1, 1, header + " > Unload Model File")
screen.addstr(6, 1, "List of Model:")
for i, model in enumerate(models_info):
screen.addstr(8 + i, 10, "Model Number:[{0}] - Model path:[{1}] - Variables:[{2}]".format(str(i), model['directory'], model['variable_name']))
screen.addstr(3, 2, "Select the model number to remove (press enter to go back): ")
try:
model_remove_index = screen.getstr()
models_info.pop(int(model_remove_index))
model_datasets.pop(int(model_remove_index))
note = "Model file unloaded successfully"
except:
note = "WARNING: Model file not unloaded successfully."
return note
def list_model_screen(header):
'''Generates screen to list all model files.
:param header: Header of page
:type header: string
'''
ready_screen("list_model_screen")
screen.addstr(1, 1, header + " > List Model File ")
screen.addstr(6, 6, "List of model(s): ")
for i, model in enumerate(models_info):
screen.addstr(8 + i, 10, "Model Number:[{0}] - Model path:[{1}] - Variables:[{2}]".format(str(i), model['directory'], model['variable_name']))
screen.addstr(4, 4, "Return to Manage Model (press Enter) :")
screen.getstr()
def manage_model_screen(header, note=""):
'''Generates Manage Model screen.
:param header: Header of page
:type header: string
:param note: Notification, defult to empty string.
:type note: string
'''
option = ''
while option != '0':
ready_screen("manage_model_screen", note)
screen.addstr(1, 1, header)
screen.addstr(4, 4, "1 - Load Local Model File")
screen.addstr(6, 4, "2 - Load ESGF Model File")
screen.addstr(8, 4, "3 - Unload Model File")
screen.addstr(10, 4, "4 - List Model File")
screen.addstr(12, 4, "0 - Return to Main Menu")
screen.addstr(14, 2, "Select an option: ")
screen.refresh()
option = screen.getstr()
if option == '1':
note = load_local_model_screen(header)
if option == '2':
note = load_esgf_model_screen(header)
if option == '3':
note = unload_model_screen(header)
if option == '4':
note = list_model_screen(header)
note = " "
##############################################################
# Manage Observation Screen
##############################################################
def select_obs_screen(header): #TODO: if the observation is already selected, don't select again.
'''Generates screen to select observation.
It reterives list of observations from database and make a table from that.
User has to select observation with dataset_id, parameter_id.
If the size of terminal screen is small to show whole table, a notification with link to parameter table on website will show up instead.
:param header: Header of page
:type header: string
:returns: Notification
:rtype: string
'''
ready_screen("select_obs_screen")
screen.addstr(1, 1, header + " > Select Observation ")
screen.addstr(7, 1, "Observations Table: ")
screen.addstr(8, 2, "|D-ID| - |P-ID| - |Database")
screen.addstr(9, 2, "|----| - |----| - |--------")
all_obs_info = rcmed.get_parameters_metadata()
new_all_obs_info = []
for each in all_obs_info:
if not each['parameter_id'] in ['72', '73', '74', '75', '80', '42', '81', '84', '85', '86', '89', '90', '91', '94', '95', '96', '97', '98', '99', '100', '101', '103', '106']:
new_all_obs_info.append(each)
all_obs_info = new_all_obs_info
del new_all_obs_info
try:
for position, obs_info in enumerate(all_obs_info):
dataset_id = obs_info['dataset_id']
parameter_id = obs_info['parameter_id']
database = obs_info['database']
line = "|{0:>4}| - |{1:>4}| - |{2}".format(dataset_id, parameter_id, database)
if position <= 25:
screen.addstr(10 + position, 2, line)
elif position > 25 and position <= 50:
screen.addstr(8, 50, "|D-ID| - |P-ID| - |Database")
screen.addstr(9, 50, "|----| - |----| - |--------")
screen.addstr(10 + position - 26, 50, line)
else:
screen.addstr(8, 100, "|D-ID| - |P-ID| - |Database")
screen.addstr(9, 100, "|----| - |----| - |--------")
screen.addstr(10 + position - 51, 100, line)
except:
ready_screen("select_obs_screen")
screen.addstr(1, 1, header + " > Select Observation ")
screen.addstr(10, 1, "Observation table cannot be shown due to small screen size. ")
screen.addstr(11, 1, "Please enlarge your screen and try again or refer to 'https://rcmes.jpl.nasa.gov/content/data-rcmes-database'. ")
try:
screen.addstr(2, 1, "More info for observation: https://rcmes.jpl.nasa.gov/content/data-rcmes-database")
screen.addstr(4, 2, "Enter Dataset ID (D-ID): ")
dataset_id = screen.getstr()
screen.addstr(5, 2, "Enter Parameter ID (P-ID): ")
parameter_id = screen.getstr()
for obs in all_obs_info:
if obs['dataset_id'] == dataset_id and obs['parameter_id'] == parameter_id:
observations_info.append({
'database':obs['database'],
'dataset_id':dataset_id,
'parameter_id':parameter_id,
'start_date':obs['start_date'],
'end_date':obs['end_date'],
'bounding_box':obs['bounding_box'],
'timestep':obs['timestep'],
'min_lat':float(eval(obs['bounding_box'].encode())[2][0]) if obs['bounding_box'] else None,
'max_lat':float(eval(obs['bounding_box'].encode())[0][0]) if obs['bounding_box'] else None,
'min_lon':float(eval(obs['bounding_box'].encode())[2][1]) if obs['bounding_box'] else None,
'max_lon':float(eval(obs['bounding_box'].encode())[0][1]) if obs['bounding_box'] else None,
'lat_res':float(obs['lat_res'].encode()),
'lon_res':float(obs['lon_res'].encode()),
'unit':obs['units']
})
note = "Observation sucessfully selected."
break
else:
note = "WARNING: Observation cannot be selected. There is no observation with given info."
except:
note = "WARNING: Observation cannot be selected, dataset or parameter id is wrong."
return note
def load_esgf_obs_screen(header):
'''Generates screen to be able to load ESGF observation file.
:param header: Header of page
:type header: string
:returns: Notification
:rtype: string
'''
ready_screen("load_esgf_obs_screen")
screen.addstr(1, 1, header + " > Download ESGF Dataset ")
screen.addstr(6, 1, "Enter Dataset ID:")
esgf_dataset_id = screen.getstr()
screen.addstr(7, 1, "Enter Variable:")
esgf_variable = screen.getstr()
screen.addstr(8, 1, "Enter Username:")
esgf_username = screen.getstr()
screen.addstr(9, 1, "Enter Password:")
esgf_password = screen.getstr()
if esgf_dataset_id and esgf_variable and esgf_username and esgf_password:
try:
solr_url = "http://esg-datanode.jpl.nasa.gov/esg-search/search?id={0}&variable={1}&format=application%2Fsolr%2Bjson".format(esgf_dataset_id, esgf_variable)
metadata_json = json.load(urllib2.urlopen(solr_url))
all_variables = metadata_json['response']['docs'][0]['variable']
variable_index = all_variables.index(esgf_variable)
if metadata_json['response']['docs'][0]["product"][0] == "observations":
screen.addstr(11, 4, "Variable Long Name: {0}".format(metadata_json['response']['docs'][0]['variable_long_name'][variable_index]))
screen.addstr(12, 4, "Start Date: {0}".format(metadata_json['response']['docs'][0]['datetime_start']))
screen.addstr(13, 4, "End Stop: {0}".format(metadata_json['response']['docs'][0]['datetime_stop']))
screen.addstr(14, 4, "Time Frequency: {0}".format(metadata_json['response']['docs'][0]['time_frequency']))
screen.addstr(15, 4, "Variable Units: {0}".format(metadata_json['response']['docs'][0]['variable_units'][variable_index]))
screen.addstr(16, 4, "East Degrees: {0}".format(metadata_json['response']['docs'][0]['east_degrees']))
screen.addstr(17, 4, "North Degrees: {0}".format(metadata_json['response']['docs'][0]['north_degrees']))
screen.addstr(18, 4, "South Degrees: {0}".format(metadata_json['response']['docs'][0]['south_degrees']))
screen.addstr(19, 4, "West Degrees: {0}".format(metadata_json['response']['docs'][0]['west_degrees']))
screen.addstr(22, 2, "Confirm:")
screen.addstr(23, 4, "0- No")
screen.addstr(24, 4, "1- Yes")
screen.addstr(25, 3, "Would you take this dataset:")
answer = screen.getstr()
if answer == "0":
note = "WARNING: ESGF observation file cannot be added."
elif answer == "1":
try:
screen.addstr(27, 4, "Downloading dataset.....")
screen.refresh()
datasets = esgf.load_dataset(esgf_dataset_id,
esgf_variable,
esgf_username,
esgf_password)
netCDF_name = get_esgf_netCDF_file_name(esgf_dataset_id, esgf_variable)
netCDF_path = "/tmp/{0}".format(netCDF_name)
obs_dataset = load_file(netCDF_path, esgf_variable)
observations_info.append({
'database':"{0}".format(netCDF_path),
'dataset_id':"esgf".format(esgf_variable),
'parameter_id':"{0}".format(esgf_variable),
'start_date': obs_dataset.temporal_boundaries()[0].strftime("%Y-%m-%d"),
'end_date':obs_dataset.temporal_boundaries()[1].strftime("%Y-%m-%d"),
#'bounding_box':obs['bounding_box'],
'timestep':"monthly",
'min_lat':obs_dataset.spatial_boundaries()[0],
'max_lat':obs_dataset.spatial_boundaries()[1],
'min_lon':obs_dataset.spatial_boundaries()[2],
'max_lon':obs_dataset.spatial_boundaries()[3],
'lat_res':obs_dataset.spatial_resolution()[0],
'lon_res':obs_dataset.spatial_resolution()[1],
'unit':"{0}".format(metadata_json['response']['docs'][0]['variable_units'][1])
})
note = "Dataset successfully downloaded."
except:
note = "WARNING: Dataset has not been downloaded."
else:
note = "The selected dataset is not Observation, please enter observation dataset."
except:
note = "WARNING: Something went wrong in downloading observation dataset from ESGF."
else:
note = "WARNING: No information given and no ESGF dataset has been downloaded."
return note
def unselect_obs_screen(header):
'''Generates screen to be able to unselect observations.
Observations can be unselected by entering index allocated to them.
:param header: Header of page
:type header: string
:returns: Notification
:rtype: string
'''
ready_screen("unselect_obs_screen")
screen.addstr(1, 1, header + " > Unselect Observation ")
screen.addstr(6, 1, "List Observation(s):")
for i, obs_info in enumerate(observations_info):
screen.addstr(8 + i, 10, " [" + str(i) + "] : " + " Dataset ID: " + obs_info['dataset_id'] + " - Parameter ID: "+ obs_info['parameter_id'] + " - Database: "+ obs_info['database'])
screen.addstr(3, 2, "Select the observation to remove (press enter to go back): ")
try:
obs_remove_index = screen.getstr()
observations_info.pop(int(obs_remove_index))
note = "Observation sucessfully unselected."
except:
note = "WARNING: Unselecting model was not successful."
return note
def list_obs_screen(header):
'''Generates screen to list observations.
:param header: Header of page
:type header: string
'''
ready_screen("list_obs_screen")
screen.addstr(1, 1, header + " > List Observation ")
screen.addstr(6, 6, "List of observation(s): ")
for i, obs_info in enumerate(observations_info):
screen.addstr(8 + i, 10, " [" + str(i) + "] : " + " Dataset ID: " + obs_info['dataset_id'] + " - Parameter ID: "+ obs_info['parameter_id'] + " - Database: "+ obs_info['database'])
screen.addstr(4, 4, "Return to Manage Observation (press Enter) :")
screen.getstr()
def manage_obs_screen(header, note=""):
'''Generates Manage Observation screen.
:param header: Header of page
:type header: string
:param note: Notification, defult to empty string.
:type note: string
'''
option = ''
while option != '0':
ready_screen("manage_obs_screen", note)
screen.addstr(1, 1, header)
screen.addstr(4, 4, "1 - Select Observation")
screen.addstr(6, 4, "2 - Load ESGF Observation")
screen.addstr(8, 4, "3 - Unselect Observation")
screen.addstr(10, 4, "4 - List Observation")
screen.addstr(12, 4, "0 - Return to Main Menu")
screen.addstr(14, 2, "Select an option: ")
screen.refresh()
option = screen.getstr()
if option == '1':
note = select_obs_screen(header)
if option == '2':
note = load_esgf_obs_screen(header)
if option == '3':
note = unselect_obs_screen(header)
if option == '4':
list_obs_screen(header)
note = " "
##############################################################
# Run Evaluation Screen
##############################################################
def run_screen(model_datasets, models_info, observations_info,
overlap_start_time, overlap_end_time, overlap_min_lat,
overlap_max_lat, overlap_min_lon, overlap_max_lon,
temp_grid_setting, spatial_grid_option, spatial_grid_setting_lat, spatial_grid_setting_lon, reference_dataset, target_datasets, metric, working_directory, plot_title):
'''Generates screen to show running evaluation process.
:param model_datasets: list of model dataset objects
:type model_datasets: list
:param models_info: list of dictionaries that contain information for each model
:type models_info: list
:param observations_info: list of dictionaries that contain information for each observation
:type observations_info: list
:param overlap_start_time: overlap start time between model and obs start time
:type overlap_start_time: datetime
:param overlap_end_time: overlap end time between model and obs end time
:type overlap_end_time: float
:param overlap_min_lat: overlap minimum lat between model and obs minimum lat
:type overlap_min_lat: float
:param overlap_max_lat: overlap maximum lat between model and obs maximum lat
:type overlap_max_lat: float
:param overlap_min_lon: overlap minimum lon between model and obs minimum lon
:type overlap_min_lon: float
:param overlap_max_lon: overlap maximum lon between model and obs maximum lon
:type overlap_max_lon: float
:param temp_grid_setting: temporal grid option such as hourly, daily, monthly and annually
:type temp_grid_setting: string
:param spatial_grid_setting:
:type spatial_grid_setting: string
:param reference_dataset: dictionary of reference dataset
:type reference_dataset: dictionary
:param target_datasets: dictionary of all target datasets
:type target_datasets: dictionary
:param metric: name of selected metric
:type metric: string
:param working_directory: path to a directory for storring outputs
:type working_directory: string
:param plot_title: Title for plot
:type plot_title: string
'''
try:
target_datasets_ensemble = []
new_model_datasets = model_datasets[:]
option = None
if option != "0":
ready_screen("run_evaluation_screen")
y = screen.getmaxyx()[0]
screen.addstr(2, 2, "Evaluation started....")
screen.refresh()
screen.addstr(4, 4, "Retrieving data...")
screen.refresh()
obs_dataset = []
for i in range(len(observations_info)):
if observations_info[i]['dataset_id'] == "esgf":
obs_dataset.append(load_file(observations_info[i]['database'], observations_info[i]['parameter_id']))
else:
dataset_id = int(observations_info[i]['dataset_id'])
parameter_id = int(observations_info[i]['parameter_id'])
obs_dataset.append(rcmed.parameter_dataset(
dataset_id,
parameter_id,
overlap_min_lat,
overlap_max_lat,
overlap_min_lon,
overlap_max_lon,
overlap_start_time,
overlap_end_time))
screen.addstr(4, 4, "--> Data retrieved.")
screen.refresh()
EVAL_BOUNDS = Bounds(lat_min=overlap_min_lat, lat_max=overlap_max_lat, lon_min=overlap_min_lon, lon_max=overlap_max_lon, start=overlap_start_time, end=overlap_end_time)
screen.addstr(5, 4, "Temporally regridding...")
screen.refresh()
for i in range(len(obs_dataset)):
obs_dataset[i] = dsp.temporal_rebin(obs_dataset[i], temp_grid_setting.lower())
for member, each_target_dataset in enumerate(new_model_datasets):
new_model_datasets[member] = dsp.temporal_rebin(new_model_datasets[member], temp_grid_setting.lower())
new_model_datasets[member] = dsp.normalize_dataset_datetimes(new_model_datasets[member], temp_grid_setting.lower())
start_index = np.where(new_model_datasets[member].times >= overlap_start_time)[0][0]
end_index = np.where(new_model_datasets[member].times <= overlap_end_time)[0][-1]
new_model_datasets[member].values=each_target_dataset.values[start_index:end_index+1,:]
new_model_datasets[member].times=each_target_dataset.times[start_index:end_index+1]
screen.addstr(5, 4, "--> Temporally regridded.")
screen.refresh()
screen.addstr(6, 4, "Spatially regridding...")
screen.refresh()
if spatial_grid_option == 'Observation':
new_lats = obs_dataset[0].lats
new_lons = obs_dataset[0].lons
elif spatial_grid_option == 'Model':
new_lats = new_model_datasets[0].lats
new_lons = new_model_datasets[0].lons
else:
new_lats = np.arange(overlap_min_lat, overlap_max_lat, spatial_grid_setting_lat)
new_lons = np.arange(overlap_min_lon, overlap_max_lon, spatial_grid_setting_lon)
if spatial_grid_option != 'Observation':
for i in range(len(obs_dataset)):
obs_dataset[i] = dsp.spatial_regrid(obs_dataset[i], new_lats, new_lons)
obs_dataset[i] = dsp.variable_unit_conversion(obs_dataset[i])
if spatial_grid_option != 'Model':
for member, each_target_dataset in enumerate(new_model_datasets):
new_model_datasets[member] = dsp.spatial_regrid(new_model_datasets[member], new_lats, new_lons)
new_model_datasets[member] = dsp.variable_unit_conversion(new_model_datasets[member])
screen.addstr(6, 4, "--> Spatially regridded.")
screen.refresh()
obs_dataset = dsp.mask_missing_data(obs_dataset+new_model_datasets)[0:len(obs_dataset)]
new_model_datasets = dsp.mask_missing_data(obs_dataset+new_model_datasets)[len(obs_dataset):]
if metric == 'bias_of_multiyear_climatology':
allNames = []
for model in new_model_datasets:
allNames.append(model.name)
screen.addstr(7, 4, "Setting up metrics...")
screen.refresh()
mean_bias = metrics.TemporalMeanBias()
pattern_correlation = metrics.PatternCorrelation()
spatial_std_dev_ratio = metrics.StdDevRatio()
screen.addstr(7, 4, "--> Metrics setting done.")
screen.refresh()
screen.addstr(8, 4, "Running evaluation.....")
screen.refresh()
if reference_dataset[:3] == 'obs':
reference = obs_dataset[int(reference_dataset[-1])]
if reference_dataset[:3] == 'mod':
reference = obs_dataset[int(new_model_datasets[-1])]
targets = []
for target in target_datasets:
if target[:3] == 'obs':
targets.append(obs_dataset[int(target[-1])])
if target[:3] == 'mod':
targets.append(new_model_datasets[int(target[-1])])
evaluation_result = evaluation.Evaluation(reference, targets, [mean_bias])
#export_evaluation_to_config(evaluation_result)
evaluation_result.run()
screen.addstr(8, 4, "--> Evaluation Finished.")
screen.refresh()
screen.addstr(9, 4, "Generating plots....")
screen.refresh()
new_rcm_bias = evaluation_result.results[0]
if not os.path.exists(working_directory):
os.makedirs(working_directory)
fname = working_directory + 'Bias_contour'
fname2= working_directory + 'Obs_contour'
fname3= working_directory + 'Model_contour'
plotter.draw_contour_map(new_rcm_bias, new_lats, new_lons, gridshape=(2, 5), fname=fname, subtitles=allNames, cmap='coolwarm')
plotter.draw_contour_map(utils.calc_temporal_mean(reference), new_lats, new_lons, gridshape=(2, 5), fname=fname2, subtitles=allNames, cmap='rainbow')
plotter.draw_contour_map(utils.calc_temporal_mean(targets[0]), new_lats, new_lons, gridshape=(2, 5), fname=fname3, subtitles=allNames, cmap='rainbow')
screen.addstr(9, 4, "--> Plots generated.")
screen.refresh()
screen.addstr(y-2, 1, "Press 'enter' to Exit: ")
option = screen.getstr()
if metric == 'std':
for i in range(len(obs_dataset)):
_, obs_dataset[i].values = utils.calc_climatology_year(obs_dataset[i])
obs_dataset[i].values = np.expand_dims(obs_dataset[i].values, axis=0)
target_datasets_ensemble = dsp.ensemble(new_model_datasets)
target_datasets_ensemble.name = "ENS"
new_model_datasets.append(target_datasets_ensemble)
for member, each_target_dataset in enumerate(new_model_datasets):
_, new_model_datasets[member].values = utils.calc_climatology_year(new_model_datasets[member])
new_model_datasets[member].values = np.expand_dims(new_model_datasets[member].values, axis=0)
allNames = []
for model in new_model_datasets:
allNames.append(model.name)
pattern_correlation = metrics.PatternCorrelation()
spatial_std_dev = metrics.StdDevRatio()
if reference_dataset[:3] == 'obs':
reference = obs_dataset[int(reference_dataset[-1])]
if reference_dataset[:3] == 'mod':
reference = obs_dataset[int(new_model_datasets[-1])]
targets = []
for target in target_datasets:
if target[:3] == 'obs':
targets.append(obs_dataset[int(target[-1])])
if target[:3] == 'mod':
targets.append(new_model_datasets[int(target[-1])])
evaluation_result = evaluation.Evaluation(reference, targets, [spatial_std_dev])
export_evaluation_to_config(evaluation_result)
evaluation_result.run()
rcm_std_dev = evaluation_result.results
evaluation_result = evaluation.Evaluation(reference, targets, [pattern_correlation])
evaluation_result.run()
rcm_pat_cor = evaluation_result.results
taylor_data = np.array([rcm_std_dev, rcm_pat_cor]).transpose()
new_taylor_data = np.squeeze(np.array(taylor_data))
if not os.path.exists(working_directory):
os.makedirs(working_directory)
fname = working_directory + 'taylor_plot'
plotter.draw_taylor_diagram(new_taylor_data, allNames, "CRU31", fname=fname, fmt='png', frameon=False)
del new_model_datasets
del obs_dataset
return "No error"
except Exception, error:
return "Error: {0}".format(error[0][:200])
##############################################################
# Settings Screen
##############################################################
def get_models_temp_bound():
'''Get models temporal bound.
:returns: model start and end time
:rtypes: (datatime, datetime)
'''
models_start_time = []
models_end_time = []
for model in model_datasets:
models_start_time.append(model.temporal_boundaries()[0])
models_end_time.append(model.temporal_boundaries()[1])
return models_start_time, models_end_time
def get_obs_temp_bound():
'''Get observation temporal bound.
:returns: observation start and end time
:rtype: (datetime, datetime)
'''
observations_start_time = []
observations_end_time = []
for obs in observations_info:
obs_start_time = datetime.strptime(obs['start_date'], "%Y-%m-%d")
observations_start_time.append(obs_start_time)
obs_end_time = datetime.strptime(obs['end_date'], "%Y-%m-%d")
observations_end_time.append(obs_end_time)
return observations_start_time, observations_end_time
def get_models_temp_overlap(models_start_time, models_end_time):
'''Calculate temporal overlap between all the models
:param models_start_time: models start time
:type models_start_time: list of datetimes
:param models_end_time: models end time
:type models_end_time: list of datetime
:returns: overlap start and end time between all the models
:rtype: (datetime, datetime)
'''
models_overlap_start_time = max(models_start_time)
models_overlap_end_time = min(models_end_time)
#Need to check if all models have temporal overlap, otherwise return
# to main menu and print a warning as notification.
if models_overlap_end_time <= models_overlap_start_time:
main_menu(model_datasets, models_info, observation_datasets, observations_info, note="WARNING: One or more model does not have temporal overlap with others.")
return models_overlap_start_time, models_overlap_end_time
def get_obs_temp_overlap(observations_start_time, observations_end_time):
'''Calculate temporal overlap between all the observations
:param observations_start_time: observations start time
:type observations_start_time: list of datetimes
:param observations_end_time: observations end time
:type observations_end_time: list of datetime
:returns: overlap start and end time between all the observations
:rtype: (datetime, datetime)
'''
obs_overlap_start_time = max(observations_start_time)
obs_overlap_end_time = min(observations_end_time)
#Need to check if all observations have temporal overlap, otherwise return
# to main menu and print a warning as notification.
if obs_overlap_end_time <= obs_overlap_start_time:
main_menu(model_datasets, models_info, observation_datasets, observations_info, note="WARNING: One or more observation does not have temporal overlap with others.")
return obs_overlap_start_time, obs_overlap_end_time
def get_all_temp_overlap(models_overlap_start_time, models_overlap_end_time, obs_overlap_start_time, obs_overlap_end_time):
'''Calculate temporal overlap between given datasets.
:param models_overlap_start_time: models overlap start time
:type models_overlap_start_time: list of datetimes
:param models_overlap_end_time: models overlap end time
:type models_overlap_end_time: list of datetime
:param obs_overlap_start_time: obs overlap start time
:type obs_overlap_start_time: list of datetimes
:param obs_overlap_end_time: obs overlap end time
:type obs_overlap_end_time: list of datetimes
:returns: overlap start and end time between models and observations
:rtype: (datetime, datetime)
'''
all_overlap_start_time = max([models_overlap_start_time, obs_overlap_start_time])
all_overlap_end_time = min([models_overlap_end_time, obs_overlap_end_time])
#Need to check if all datasets have temporal overlap, otherwise return
# to main menu and print a warning as notification.
if all_overlap_end_time <= all_overlap_start_time:
main_menu(model_datasets, models_info, observation_datasets, observations_info, note="WARNING: One or more dataset does not have temporal overlap with others.")
return all_overlap_start_time, all_overlap_end_time
def get_models_spatial_bound(): #TODO: convert longitudes to -180, 180 to match with observation data
'''Get all models spatial bound.
:returns: all models spatial boundaries
:rtype: list
'''
models_bound = []
for model in model_datasets:
models_bound.append(model.spatial_boundaries())
return models_bound
def get_models_spatial_overlap(models_bound):
'''Calculate spatial overlap between all models.
:param models_bound: all models spatial boundaries information
:type models_bound: list
:returns: spatial boundaries overlap between all models
:rtype: (float, float, float, float)
'''
models_overlap_min_lat = max(each[0] for each in models_bound)
models_overlap_max_lat = min(each[1] for each in models_bound)
models_overlap_min_lon = max(each[2] for each in models_bound)
models_overlap_max_lon = min(each[3] for each in models_bound)
#Need to check if all models have spatial overlap, otherwise return
# to main menu and print a warning as notification.
if models_overlap_max_lat <= models_overlap_min_lat or models_overlap_max_lon <= models_overlap_min_lon:
main_menu(model_datasets, models_info, observation_datasets, observations_info, note="WARNING: One or more model does not have spatial overlap with others.")
return models_overlap_min_lat, models_overlap_max_lat, models_overlap_min_lon, models_overlap_max_lon
def get_obs_spatial_bound():
'''Get all observations spatial bound.
:returns: all observations spatial boundaries
:rtype: list
'''
observations_bound = []
for obs in observations_info:
observations_bound.append([obs['min_lat'], obs['max_lat'], obs['min_lon'], obs['max_lon']])
return observations_bound
def get_obs_spatial_overlap(observations_bound):
'''Calculate spatial overlap between all observations.
:param observations_bound: all observations spatial boundaries information
:type observations_bound: list
:returns: spatial boundaries overlap between all observations
:rtype: (float, float, float, float)
'''
obs_overlap_min_lat = max(each[0] for each in observations_bound)
obs_overlap_max_lat = min(each[1] for each in observations_bound)
obs_overlap_min_lon = max(each[2] for each in observations_bound)
obs_overlap_max_lon = min(each[3] for each in observations_bound)
#Need to check if all observations have spatial overlap, otherwise return
# to main menu and print a warning as notification.
if obs_overlap_max_lat <= obs_overlap_min_lat or obs_overlap_max_lon <= obs_overlap_min_lon:
main_menu(model_datasets, models_info, observation_datasets, observations_info, note="WARNING: One or more observation does not have spatial overlap with others.")
return obs_overlap_min_lat, obs_overlap_max_lat, obs_overlap_min_lon, obs_overlap_max_lon
def get_all_spatial_overlap(models_overlap_min_lat, models_overlap_max_lat, models_overlap_min_lon, models_overlap_max_lon, obs_overlap_min_lat, obs_overlap_max_lat, obs_overlap_min_lon, obs_overlap_max_lon):
'''Calculate spatial overlap between all models and observations
:param models_overlap_min_lat: min latitude between all models
:type models_overlap_min_lat: float
:param models_overlap_max_lat: max latitude between all models
:type models_overlap_max_lat: float
:param models_overlap_min_lon: min longitude between all models
:type models_overlap_min_lon: float
:param models_overlap_max_lon: max longitude between all models
:type models_overlap_max_lon: float
:param obs_overlap_min_lat: min latitude between all onservations
:type obs_overlap_min_lat: float
:param obs_overlap_max_lat: max latitude between all onservations
:type obs_overlap_max_lat: float
:param obs_overlap_min_lon: min longitude between all onservations
:type obs_overlap_min_lon: float
:param obs_overlap_max_lon: max longitude between all onservations
:type obs_overlap_max_lon: float
:returns: spatial boundaries overlap between all models and observations
:rtype: (float, float, float, float)
'''
all_overlap_min_lat = max([models_overlap_min_lat, obs_overlap_min_lat])
all_overlap_max_lat = min([models_overlap_max_lat, obs_overlap_max_lat])
all_overlap_min_lon = max([models_overlap_min_lon, obs_overlap_min_lon])
all_overlap_max_lon = min([models_overlap_max_lon, obs_overlap_max_lon])