Coverage for webgeodyn/inout/midpath.py: 0%

42 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-12-18 09:33 +0000

1import numpy as np 

2import scipy.io 

3 

4 

5def load(dataDirectory, dataModel, keepRealisations=False): 

6 """ Loading function for mid-path model. Also adds the data to the dataModel. 

7 

8 :param dataDirectory: directory where the data is located 

9 :type dataDirectory: str (path) 

10 :param dataModel: Model to which the data should be added 

11 :type dataModel: Model 

12 :param keepRealisations: indicating if realisations should be kept or averaged (not used) 

13 :type keepRealisations: bool (default: False) 

14 :return: 0 if everything went well, -1 otherwise 

15 :rtype: int 

16 """ 

17 # load times 

18 print("Loading mid-path data !!") 

19 

20 str_dir_file = dataDirectory + "/time_midpath.mat" 

21 print(str_dir_file) 

22 tmp = scipy.io.loadmat(str_dir_file, squeeze_me=True, struct_as_record=False) # read matlab 6 binary file into dictionary 

23 

24 times = tmp["time"].T 

25 times = np.ascontiguousarray(times) 

26 

27 nt = times.shape[0] 

28 

29 print("read MF mid-path data...") 

30 # Reading MF @ CMB in NS units 

31 str_dir_file = dataDirectory + "/gnm_midpath.mat" 

32 tmp = scipy.io.loadmat(str_dir_file, squeeze_me=True, 

33 struct_as_record=False) # read matlab 6 binary file into dictionary 

34 gnmE = tmp["gnm"] 

35 gnmE = np.ascontiguousarray(gnmE) 

36 # gnmE = np.loadtxt(os.path.join(dataDirectory,"gnm_midpath.dat")) 

37 

38 print("... mid-path gnm data read !!") 

39 

40 # detect lmax 

41 lmax = (-2 + np.sqrt(4 + 4 * gnmE.shape[1])) / 2 

42 print("lmax=", lmax) 

43 if int(lmax) != lmax: 

44 raise ValueError("Data length %i does not correspond to lmax*(lmax+2)" % gnmE.shape[1]) 

45 else: 

46 lmax = int(lmax) 

47 

48 n = lmax * (lmax + 2) 

49 

50 data = gnmE * 10 ** 9 # scale to nT ?? 

51 dataModel.addMeasure("MF", "MF", lmax, "nT", data, times=times) 

52 

53 # now deals with tnm, snm 

54 str_dir_file = dataDirectory + "/snm_midpath.mat" 

55 tmp = scipy.io.loadmat(str_dir_file, squeeze_me=True, struct_as_record=False) # read matlab 6 binary file into dictionary 

56 snm = tmp["snm"] 

57 snm = np.ascontiguousarray(snm) 

58 

59 str_dir_file = dataDirectory + "/tnm_midpath.mat" 

60 tmp = scipy.io.loadmat(str_dir_file, squeeze_me=True, struct_as_record=False) # read matlab 6 binary file into dictionary 

61 tnm = tmp["tnm"] 

62 tnm = np.ascontiguousarray(tnm) 

63 

64 print("... mid-path tnm+snm data read !!") 

65 

66 data = np.concatenate((tnm, snm), axis=1) 

67 print(data.shape) 

68 

69 # detect lmax 

70 lmax = (-2 + np.sqrt(4 + 4 * (data.shape[1]) / 2)) / 2 

71 if int(lmax) != lmax: 

72 raise ValueError("Data length %i does not correspond to 2*lmax*(lmax+2)" % data.shape[1]) 

73 else: 

74 lmax = int(lmax) 

75 

76 data = data * 10 ** 0 # scale to km/yr ?? 

77 dataModel.addMeasure("U", "U", lmax, "km/yr", data, times=times) 

78 

79 return 0