MPAS-JEDI
AnalyzeStats.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 
3 from AnalyzeStatsArgs import args
4 
5 from Analyses import Analyses
6 import analyze_config as anconf
7 import config as conf
8 from copy import deepcopy
9 import diag_utils as du
10 import logging
11 import logsetup
12 import multiprocessing as mp
13 import os
14 import StatisticsDatabase as sdb
15 
16 _logger = logging.getLogger(__name__)
17 
18 depends_on = [
19  'AnalyzeStatsArgs',
20  'analyze_config',
21  'Analyses',
22  'basic_plot_functions',
23  'predefined_configs',
24  'binning_params',
25  'binning_utils',
26  'config',
27  'diag_utils',
28  'plot_utils',
29  'stat_utils',
30  'StatisticsDatabase',
31  'var_utils',
32 ]
33 
34 def main():
35  '''
36  Main function that sequentially
37  () collates command-line arguments from AnalyzeStatsArgs with static
38  configuration modules (config and analysis_config)
39  () loops over selected DiagSpaces, and for each
40  - initializes StatisticsDatabase object (internal multiprocessing)
41  - analyzes the statistics for all selected anconf.analysisTypes (internal multiprocessing)
42  See analysis_config for more information
43  '''
44  _logger.info('Starting main()')
45 
46  DiagSpaceConfig = deepcopy(conf.DiagSpaceConfig)
47  for key in sorted(DiagSpaceConfig):
48  if not DiagSpaceConfig[key]['process']: del DiagSpaceConfig[key]
49 
50  ## process processor selection
51  # note: these scripts only work on a single node
52  if args.npan:
53  npan = min(int(args.npan), mp.cpu_count())
54  else:
55  npan = 1
56  if args.npread:
57  npread = min(int(args.npread), mp.cpu_count())
58  else:
59  npread = npan
60 
61  ## process DiagSpace command-line selections
62  anGrp = None
63  selectDiagSpace = None
64  if args.diagSpace:
65  selectDiagSpace = args.diagSpace
66  elif args.anGrp:
67  anGrp = args.anGrp
68 
69  ## remove DiagSpaces that are not selected
70  for key in sorted(DiagSpaceConfig):
71  if ((selectDiagSpace is not None and selectDiagSpace not in key) or
72  (anGrp is not None and DiagSpaceConfig[key]['anGrp'] != anGrp)):
73  del DiagSpaceConfig[key]
74 
75  ## process analysisType command-line selection or use defaults from analyze_config
76  if args.analysisType:
77  analysisTypes = [args.analysisType]
78  else:
79  analysisTypes = anconf.analysisTypes
80 
81  ## loop over selected DiagSpaces
82  for DiagSpaceName in sorted(DiagSpaceConfig):
83  ## setup DiagSpaceName configuration
84  myDBConf = deepcopy(anconf.dbConf)
85  myDBConf['DiagSpaceName'] = DiagSpaceName
86 
87  myDBConf['diagnosticConfigs'] = du.diagnosticConfigs(
88  DiagSpaceConfig[DiagSpaceName]['diagNames'], DiagSpaceName,
89  analysisStatistics = anconf.analysisStatistics)
90  for diag in list(myDBConf['diagnosticConfigs'].keys()):
91  if not myDBConf['diagnosticConfigs'][diag]['analyze']:
92  del myDBConf['diagnosticConfigs'][diag]
93 
94  # Construct statistical database for each DiagSpace
95  db = sdb.StatsDB(myDBConf)
96 
97  if db.available:
98  _logger.info('')
99  _logger.info('Analyzing StatsDB for '+DiagSpaceName)
100 
101  ## Initialize the database
102  db.read(npread)
103 
104  analyses = Analyses(db, analysisTypes, anconf.diagnosticGroupings, npan)
105 
106  analyses.analyze()
107 
108  _logger.info('Finished main() successfully')
109 
110 if __name__ == '__main__': main()