Coverage for pygeodyn/inout/writes.py: 92%

75 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-12-22 13:43 +0000

1import numpy as np 

2import os 

3import time 

4import logging 

5from pygeodyn.corestates import with_core_state_of_dimensions 

6from pygeodyn.utilities import date_array_to_decimal 

7import pygeodyn 

8 

9class Hdf5GroupHandler: 

10 def __init__(self, dates, nb_realisations, core_state, hdf5_file, group_name, format, exclude = ['Z']): 

11 self.mother_file = hdf5_file 

12 self.group = self.mother_file.create_group(group_name) 

13 self.dates = dates 

14 # Convert unsupported type datetime64 in strings 

15 if self.dates.dtype == 'datetime64[M]': 

16 self.dates = date_array_to_decimal(self.dates, 2) 

17 self.group.create_dataset('times', data=self.dates, dtype=format) 

18 

19 self.handlers = {} 

20 self.exclude = exclude 

21 for measure in core_state.measures: 

22 if measure not in exclude: 

23 self.handlers[measure] = self.group.create_dataset(measure, data = np.zeros((nb_realisations,core_state._measures[measure].shape[1],core_state._measures[measure].shape[2])), dtype=format) 

24 

25 @with_core_state_of_dimensions(3) 

26 def update_all(self, core_state, attributed_models): 

27 for measure in self.handlers: 

28 for i_idx, i_real in enumerate(attributed_models): 

29 self.handlers[measure][i_real] = core_state._measures[measure][i_idx] 

30 

31 def close(self): 

32 self.mother_file.close() 

33 

34 

35def saveReadme(readme_path, config, algo_name, nb_realisations, git_info, elapsed_time, parallel, seed): 

36 try: 

37 readme = open(os.path.join(readme_path, 'README.md'), 'w') 

38 except IOError: 

39 logging.error('Problem in creating README at {} ! Other files will be saved but no README will be generated.'.format(readme_path)) 

40 return -1 

41 

42 if parallel > 1: 

43 readme.write('Parallel algorithm with {} process'.format(parallel)) 

44 else: 

45 readme.write('Sequential algorithm ') 

46 

47 readme.write('{} (pygeodyn v{}) finished at {} in {:.2f} s\n'.format(algo_name.upper(), pygeodyn.__version__, time.asctime(), elapsed_time)) 

48 readme.write('---\n') 

49 readme.write('Git branch: {}, last message: "{}", version and sha1: {}'.format(*git_info)) 

50 readme.write('---\n') 

51 readme.write('{} realisations were considered with seed {}'.format(nb_realisations, seed)) 

52 

53 wo = 'with' if config.compute_e else 'without' 

54 readme.write(' {} computation of sub-grid errors\n'.format(wo)) 

55 

56 readme.write('-------Config file----------\n\n') 

57 write_conf(readme, config) 

58 

59 readme.write('----------------------------\n') 

60 

61 readme.close() 

62 return 0 

63 

64def conf_dict(config): 

65 """ 

66 Rewrite the config.__dict__ in a form that is ready to be printed in a text file. 

67 """ 

68 

69 file_attrs = config.__dict__ 

70 

71 keys_without_conversion = ['dt_f', 'dt_a_f_ratio', 'dt_smoothing', 't_start_analysis', 't_end_analysis', 'compute_e', 'N_pca_u', 'AR_type', 'combined_U_ER_forecast', 'core_state_init', 

72 'Lb', 'Lu', 'Lsv', 'TauU', 'TauE', 

73 'prior_dir', 'prior_type', 'obs_dir', 'obs_type', 

74 'Nth_legendre'] 

75 optional_keys = ['remove_spurious', 'pca_norm', 'init_file', 'obs_select', 'discard_high_lat'] 

76 for key in optional_keys: 

77 if key in file_attrs: 

78 keys_without_conversion.append(key) 

79 extracted_dict = {k: file_attrs[k] for k in keys_without_conversion} 

80 

81 # Converting dt stored as fractions of years in timedelta64 months. 

82 dt_to_convert = ['TauU', 'TauE'] 

83 for dt in dt_to_convert: 

84 # Add decimal to the dt name except for Tau 

85 key = dt 

86 try: 

87 extracted_dict[dt] = np.timedelta64(int(file_attrs[key]*12), 'M') 

88 except: 

89 raise KeyError('Key not found') 

90 return extracted_dict 

91 

92def write_conf(readme, config): 

93 extracted_dict = conf_dict(config) 

94 

95 for k, v in extracted_dict.items(): 

96 if type(v).__module__ == 'numpy': 

97 

98 if k[:3] == 'Tau': 

99 v_type = 'years' 

100 else: 

101 # convert the int64,float32... to native python type, int, float... 

102 v_type = type(v.item()).__name__ 

103 else: 

104 if k == 'obs_select': 

105 v_type = 'dict' 

106 else: 

107 v_type = type(v).__name__ 

108 

109 readme.write('{} {} {} \n'.format(k, v_type, v))