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

36 statements  

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

1import os 

2import numpy as np 

3import glob 

4 

5 

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

7 """ 

8 Loading function for the archeomagnetic models (COVARCH et COVLAKE). Also adds the data to the dataModel. 

9 

10 :param dataDirectory: directory where the data is located 

11 :type dataDirectory: str (path) 

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

13 :type dataModel: Model 

14 :param keepRealisations: indicating if realisations should be kept or averaged 

15 :type keepRealisations: bool (default: False) 

16 :return: 0 if everything went well, -1 if inconsistency in dates 

17 :rtype: int 

18 """ 

19 mean_file_name = glob.glob(os.path.join(dataDirectory, 'Mean_*'))[0] 

20 rms_file_name = glob.glob(os.path.join(dataDirectory, 'RMS_*'))[0] 

21 

22 # Read metadata from mean file 

23 mean_file = open(mean_file_name) 

24 # Skip first line 

25 mean_file.readline() 

26 # Read second line that contain metadata 

27 meta_data = mean_file.readline().split() 

28 mean_file.close() 

29 

30 Lb = int(meta_data[0]) 

31 nb_dates = int(meta_data[1]) 

32 dates = np.array(meta_data[2:], dtype=float) 

33 

34 # Check consistency 

35 if len(dates) != nb_dates: 

36 print('Got a number of dates ({}) different than what was announced in the header ({}) !'.format(len(dates), nb_dates)) 

37 return -1 

38 

39 # Create magnetic field quantities 

40 measure_name = "MF" 

41 units = "nT" 

42 Nb = Lb * (Lb + 2) 

43 

44 # Raw data is succession of coeffs grouped 4 by line 

45 # Everything is read and then reshaped to get the proper coef by date 

46 if keepRealisations: 

47 real_folder = os.path.join(dataDirectory, 'ensemble') 

48 real_files = glob.glob(os.path.join(real_folder, 'real_*')) 

49 nb_reals = len(real_files) 

50 

51 # Storing array for realisation data 

52 real_data = np.zeros((nb_dates, Nb, nb_reals)) 

53 

54 for file in real_files: 

55 i_real = int(file.split('_')[-1]) - 1 

56 raw_data = np.genfromtxt(file, skip_header=2, dtype=np.float64) 

57 reshaped_data = np.reshape(raw_data, (nb_dates, Nb), order='C') 

58 real_data[:, :, i_real] = reshaped_data 

59 

60 dataModel.addMeasure(measure_name, measure_name, Lb, units, real_data, times=dates) 

61 

62 else: 

63 raw_mean_data = np.genfromtxt(mean_file_name, skip_header=2) 

64 mean_data = np.reshape(raw_mean_data, (nb_dates, Nb), order='C') 

65 

66 raw_rms_data = np.genfromtxt(rms_file_name, skip_header=2) 

67 rms_data = np.reshape(raw_rms_data, (nb_dates, Nb), order='C') 

68 

69 dataModel.addMeasure(measure_name, measure_name, Lb, units, mean_data, rms_data, dates) 

70 

71 return 0