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

35 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 Gillet_gnm_2015 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 modelname = "mod013" 

19 

20 # Reading measure U from ens flow models 

21 # List realisation 

22 realisations = [] 

23 for file in os.listdir(dataDirectory): 

24 matchfile = re.match('^'+modelname+'_real(.*[^.zip])$', file) 

25 if matchfile is not None: 

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

27 

28 if len(realisations) == 0: 

29 print('No realisations were found while loading ENS-CORE ! Aborting loading...') 

30 return -1 

31 

32 g = None 

33 irealisation = 0 

34 for file in realisations: 

35 with open(file) as f: 

36 # Read parameters 

37 ntimes, lmax = [int(x) for x in f.readline().split()] 

38 times = np.zeros((ntimes,)) 

39 if g is None: 

40 g = np.zeros((len(realisations), ntimes, 2*lmax*(lmax+2))) 

41 

42 for i in range(ntimes): 

43 times[i] = float(f.readline()) 

44 k = 0 

45 while k < 2*lmax*(lmax+2): 

46 line = f.readline() 

47 temp = [float(x) for x in line.split()] 

48 g[irealisation, i, k:k+len(temp)] = temp 

49 k += len(temp) 

50 irealisation += 1 

51 

52 if keepRealisations: 

53 # Move realisation axis to last place to have data of form [ntimes, ncoef, nreal] (originally [nreal, ntimes, ncoef]) 

54 formatted_g = np.moveaxis(g, 0, -1) 

55 dataModel.addMeasure("U", "U", lmax, "km/yr", formatted_g, times=times) 

56 else: 

57 dataModel.addMeasure("U", "U", lmax, "km/yr", np.mean(g, axis=0), np.std(g, axis=0), times) 

58 return 0