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

32 statements  

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

1import os 

2import re 

3import numpy as np 

4 

5 

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

7 """ Loading function for the COV-OBS model. Also adds the data to the dataModel. 

8 

9 :param dataDirectory: directory where the data is located 

10 :type dataDirectory: str (path) 

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

12 :type dataModel: Model 

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

14 :type keepRealisations: bool (default: False) 

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

16 :rtype: int 

17 """ 

18 

19 # Reading measure BR and SV from covobs 

20 # List realisation 

21 for measureName in ["MF", "SV"]: 

22 

23 realisations = [] 

24 for file in os.listdir(dataDirectory): 

25 matchfile = re.match('^real(.*)_' + measureName.lower() + '_int_coefs.dat$', file) 

26 if matchfile is not None: 

27 realisations.append(os.path.join(dataDirectory, file)) 

28 

29 nb_reals = len(realisations) 

30 if nb_reals == 0: 

31 print('No realisations were found while loading COV-OBS ! Aborting loading...') 

32 return -1 

33 

34 g = None 

35 # Storing realisation data in g 

36 for irealisation, file in enumerate(realisations): 

37 sourceData = np.loadtxt(file) 

38 times = sourceData[:, 0] 

39 sourceData = sourceData[:, 1:] 

40 

41 # Initialisation of g (first iteration of the loop) 

42 if g is None: 

43 g = np.zeros((sourceData.shape[0], sourceData.shape[1], nb_reals)) 

44 g[:, :, irealisation] = sourceData 

45 

46 # Compute lmax from the size of the data previously read 

47 sizeData = sourceData.shape[1] 

48 lmax = (-2+np.sqrt(4+4*sizeData))//2 

49 

50 if measureName == "SV": 

51 units = "nT/yr" 

52 elif measureName == "MF": 

53 units = "nT" 

54 

55 if keepRealisations: 

56 # If realisation must be kept, add the data to the model as read 

57 dataModel.addMeasure(measureName, measureName, 

58 lmax, units, g, times=times) 

59 else: 

60 # Else, average the data read on realisations 

61 dataModel.addMeasure(measureName, measureName, 

62 lmax, units, 

63 np.mean(g, axis=2), np.std(g, axis=2), times) 

64 return 0