@@ -1,94 +1,93 | |||
|
1 | 1 | import traceback |
|
2 | 2 | from SciQLopBindings import PyDataProvider, Product, VectorTimeSerie, ScalarTimeSerie, DataSeriesType |
|
3 | 3 | import numpy as np |
|
4 | 4 | import math |
|
5 | 5 | from spwc.cache import _cache |
|
6 | 6 | from spwc.common.datetime_range import DateTimeRange |
|
7 | 7 | from functools import partial |
|
8 | 8 | from datetime import datetime, timedelta, timezone |
|
9 | 9 | from spwc.common.variable import SpwcVariable |
|
10 | 10 | |
|
11 | 11 | |
|
12 | 12 | def make_scalar(x): |
|
13 | 13 | y = np.cos(x/10.) |
|
14 | 14 | return SpwcVariable(time=x, data=y) |
|
15 | 15 | |
|
16 | 16 | def make_vector(x): |
|
17 | 17 | v=np.ones((len(x),3)) |
|
18 | 18 | for i in range(3): |
|
19 | 19 | v.transpose()[:][i] = np.cos(x/10. + float(i)) + (100. * np.cos(x/10000. + float(i))) |
|
20 | 20 | return SpwcVariable(time=x, data=v) |
|
21 | 21 | |
|
22 | 22 | |
|
23 | 23 | def make_multicomponent(x): |
|
24 | 24 | v=np.ones((len(x),4)) |
|
25 | 25 | for i in range(4): |
|
26 | 26 | v.transpose()[:][i] = float(i+1) * np.cos(x/10. + float(i)) |
|
27 | 27 | return SpwcVariable(time=x, data=v) |
|
28 | 28 | |
|
29 | 29 | def make_spectrogram(x): |
|
30 | 30 | v=np.ones((len(x),32)) |
|
31 | 31 | for i in range(32): |
|
32 | 32 | v.transpose()[:][i] = 100.*(2.+ float(i+1) * np.cos(x/1024. + float(i))) |
|
33 | 33 | return SpwcVariable(time=x, data=v) |
|
34 | 34 | |
|
35 | 35 | |
|
36 | 36 | def _get_data(p_type, start, stop): |
|
37 | 37 | if type(start) is datetime: |
|
38 | 38 | start = start.timestamp() |
|
39 | 39 | stop = stop.timestamp() |
|
40 | 40 | x = np.arange(math.ceil(start), math.floor(stop))*1. |
|
41 | 41 | if p_type == 'scalar': |
|
42 | 42 | return make_scalar(x) |
|
43 | 43 | if p_type == 'vector': |
|
44 | 44 | return make_vector(x) |
|
45 | 45 | if p_type == 'multicomponent': |
|
46 | 46 | return make_multicomponent(x) |
|
47 | 47 | if p_type == 'spectrogram': |
|
48 | 48 | return make_spectrogram(np.arange(math.ceil(start), math.floor(stop),15.)) |
|
49 | 49 | return None |
|
50 | 50 | |
|
51 | 51 | class MyProvider(PyDataProvider): |
|
52 | 52 | def __init__(self): |
|
53 | 53 | super(MyProvider,self).__init__() |
|
54 | 54 | self.register_products([Product("/tests/without_cache/scalar",[],{"type":"scalar"}), |
|
55 | 55 | Product("/tests/without_cache/vector",[],{"type":"vector"}), |
|
56 | 56 | Product("/tests/without_cache/multicomponent",[],{"type":"multicomponent",'size':'4'}), |
|
57 | 57 | Product("/tests/without_cache/spectrogram",[],{"type":"spectrogram",'size':'32'}), |
|
58 | 58 | Product("/tests/with_cache/scalar",[],{"type":"scalar", "cache":"true"}), |
|
59 | 59 | Product("/tests/with_cache/vector",[],{"type":"vector", "cache":"true"}), |
|
60 | 60 | Product("/tests/with_cache/multicomponent",[],{"type":"multicomponent",'size':'4', "cache":"true"}) |
|
61 | 61 | ]) |
|
62 | 62 | |
|
63 | 63 | def get_data(self,metadata,start,stop): |
|
64 | 64 | ts_type = DataSeriesType.SCALAR |
|
65 | 65 | default_ctor_args = 1 |
|
66 | 66 | use_cache = False |
|
67 | 67 | p_type = 'scalar' |
|
68 | 68 | try: |
|
69 | 69 | for key,value in metadata.items(): |
|
70 | 70 | if key == 'type': |
|
71 | 71 | p_type = value |
|
72 | 72 | if value == 'vector': |
|
73 | 73 | ts_type = DataSeriesType.VECTOR |
|
74 | 74 | elif value == 'multicomponent': |
|
75 | 75 | ts_type = DataSeriesType.MULTICOMPONENT |
|
76 | 76 | elif value == 'spectrogram': |
|
77 | 77 | ts_type = DataSeriesType.SPECTROGRAM |
|
78 | 78 | if key == 'cache' and value == 'true': |
|
79 | 79 | use_cache = True |
|
80 | 80 | if use_cache: |
|
81 | 81 | cache_product = f"tests/{p_type}" |
|
82 | 82 | var = _cache.get_data(cache_product, DateTimeRange(datetime.fromtimestamp(start, tz=timezone.utc), datetime.fromtimestamp(stop, tz=timezone.utc)), partial(_get_data, p_type), fragment_hours=24) |
|
83 | 83 | else: |
|
84 | print("No Cache") | |
|
85 | 84 | var = _get_data(p_type, start, stop) |
|
86 | 85 | return ((var.time,var.data), ts_type) |
|
87 | 86 | except Exception as e: |
|
88 | 87 | print(traceback.format_exc()) |
|
89 | 88 | print("Error in test.py ",str(e)) |
|
90 | 89 | return ((np.array(), np.array()), ts_type) |
|
91 | 90 | |
|
92 | 91 | |
|
93 | 92 | t=MyProvider() |
|
94 | 93 |
General Comments 0
You need to be logged in to leave comments.
Login now