Coverage for C: \ Users \ peaco \ OneDrive \ Documents \ GitHub \ mth5 \ mth5 \ clients \ lemi424.py: 100%

31 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-01-27 20:09 -0800

1# -*- coding: utf-8 -*- 

2""" 

3Created on Fri Oct 11 10:57:54 2024 

4 

5@author: jpeacock 

6""" 

7 

8from pathlib import Path 

9from typing import Any, Optional, Union 

10 

11from mth5 import read_file 

12from mth5.clients.base import ClientBase 

13from mth5.io.lemi import LEMICollection 

14 

15# ============================================================================= 

16# Imports 

17# ============================================================================= 

18from mth5.mth5 import MTH5 

19 

20 

21# ============================================================================= 

22 

23 

24class LEMI424Client(ClientBase): 

25 def __init__( 

26 self, 

27 data_path: Union[str, Path], 

28 save_path: Optional[Union[str, Path]] = None, 

29 mth5_filename: str = "from_lemi424.h5", 

30 **kwargs: Any, 

31 ) -> None: 

32 """ 

33 LEMI 424 client for converting long period data to MTH5. 

34 

35 Parameters 

36 ---------- 

37 data_path : str or Path 

38 Directory where LEMI 424 data files are located. 

39 save_path : str or Path, optional 

40 Directory to save the mth5 file. If None, uses data_path. 

41 mth5_filename : str, optional 

42 Name of the mth5 file to create. Default is 'from_lemi424.h5'. 

43 **kwargs : Any 

44 Additional keyword arguments for h5 parameters. 

45 

46 Examples 

47 -------- 

48 >>> client = LEMI424Client(data_path="./data", save_path="./output") 

49 >>> client.save_path 

50 PosixPath('output/from_lemi424.h5') 

51 """ 

52 super().__init__( 

53 data_path, 

54 save_path=save_path, 

55 sample_rates=[1], 

56 mth5_filename=mth5_filename, 

57 **kwargs, 

58 ) 

59 self.collection = LEMICollection(self.data_path) 

60 

61 def make_mth5_from_lemi424( 

62 self, 

63 survey_id: str, 

64 station_id: str, 

65 **kwargs: Any, 

66 ) -> Path: 

67 """ 

68 Create an MTH5 file from LEMI 424 long period data. 

69 

70 Parameters 

71 ---------- 

72 survey_id : str 

73 Survey identifier. 

74 station_id : str 

75 Station identifier. 

76 **kwargs : Any 

77 Additional keyword arguments to set as attributes. 

78 

79 Returns 

80 ------- 

81 Path 

82 Path to the created mth5 file. 

83 

84 Examples 

85 -------- 

86 >>> client = LEMI424Client(data_path="./data") 

87 >>> client.make_mth5_from_lemi424("SURVEY1", "ST01") 

88 PosixPath('data/from_lemi424.h5') 

89 """ 

90 for key, value in kwargs.items(): 

91 if value is not None: 

92 setattr(self, key, value) 

93 

94 self.collection.survey_id = survey_id 

95 self.collection.station_id = station_id 

96 

97 runs = self.get_run_dict() 

98 

99 with MTH5(**self.h5_kwargs) as m: 

100 m.open_mth5(self.save_path, self.mth5_file_mode) 

101 survey_group = m.add_survey(self.collection.survey_id) 

102 

103 for station_id in runs.keys(): 

104 station_group = survey_group.stations_group.add_station(station_id) 

105 for run_id, run_df in runs[station_id].items(): 

106 run_group = station_group.add_run(run_id) 

107 run_ts = read_file( 

108 run_df.fn.to_list(), 

109 calibration_dict=self.collection.calibration_dict, 

110 ) 

111 run_ts.run_metadata.id = run_id 

112 run_group.from_runts(run_ts) 

113 station_group.metadata.update(run_ts.station_metadata) 

114 station_group.write_metadata() 

115 

116 # update survey metadata from input station 

117 survey_group.update_metadata() 

118 

119 return self.save_path