##// END OF EJS Templates
Added POC AMDA python impl and CDAWEB bits...
jeandet -
r1430:ecc8b7b09c3d
parent child
Show More
@@ -0,0 +1,44
1 import sys
2 sys.path.append("/home/jeandet/Documents/prog/build-SciQLop-Desktop-Debug/core")
3 import os
4 import datetime
5 import PythonProviders
6 import pysciqlopcore
7 import numpy as np
8 import pandas as pds
9 import requests
10 from spwc.cdaweb import cdaweb
11
12 cd = cdaweb()
13
14 def get_sample(name,start,stop):
15 try:
16 tstart=datetime.datetime.fromtimestamp(start).strftime('%Y%m%dT%H%M%SZ')
17 tend=datetime.datetime.fromtimestamp(stop).strftime('%Y%m%dT%H%M%SZ')
18 req_url=f"https://cdaweb.gsfc.nasa.gov/WS/cdasr/1/dataviews/sp_phys/datasets/MMS4_SCM_SRVY_L2_SCSRVY/data/{tstart},{tend}/mms4_scm_acb_gse_scsrvy_srvy_l2?format=csv"
19 resp = requests.get(req_url,headers={"Accept":"application/json"})
20 csv_url = resp.json()['FileDescription'][0]['Name']
21 df = pds.read_csv(csv_url,comment='#',index_col=0, infer_datetime_format=True,parse_dates=True)
22 t = np.array([d.timestamp()-7200 for d in df.index])
23 values = df.values
24 return pysciqlopcore.VectorTimeSerie(t,values)
25 except Exception as e:
26 print("fuck ",str(e))
27 return pysciqlopcore.VectorTimeSerie(1)
28
29 def get_sample(name,start,stop):
30 try:
31 tstart=datetime.datetime.fromtimestamp(start)
32 tend=datetime.datetime.fromtimestamp(stop)
33 df = cd.get_variable(dataset="MMS2_SCM_SRVY_L2_SCSRVY",variable="mms2_scm_acb_gse_scsrvy_srvy_l2",tstart=tstart,tend=tend)
34 t = np.array([d.timestamp()-7200 for d in df.index])
35 values = df.values
36 return pysciqlopcore.VectorTimeSerie(t,values)
37 except Exception as e:
38 print("fuck ",str(e))
39 return pysciqlopcore.VectorTimeSerie(1)
40
41
42 PythonProviders.register_product([("/CDA/mms4_scm_acb_gse_scsrvy_srvy_l2",[],[("type","vector")])],get_sample)
43
44
@@ -1,21 +1,24
1 #include <Data/DateTimeRange.h>
1 #include <Data/DateTimeRange.h>
2 #include <TimeSeries.h>
2 #include <TimeSeries.h>
3 #include <functional>
3 #include <functional>
4 #include <iostream>
4 #include <iostream>
5 #include <memory>
5 #include <memory>
6
6
7
7
8 class PythonInterpreter
8 class PythonInterpreter
9 {
9 {
10 public:
10 public:
11 using provider_funct_t = std::function<std::shared_ptr<TimeSeries::ITimeSerie>(std::string&, double, double)>;
11 using provider_funct_t = std::function<std::shared_ptr<TimeSeries::ITimeSerie>(
12 std::vector<std::tuple<std::string, std::string>>&, double, double)>;
13 using product_t = std::tuple<std::string, std::vector<std::string>,
14 std::vector<std::pair<std::string, std::string>>>;
15
12 PythonInterpreter();
16 PythonInterpreter();
13 void add_register_callback(std::function<void(const std::vector<std::pair<std::string,std::vector<std::pair<std::string,std::string>>>>&,
17 void add_register_callback(
14 provider_funct_t)>
18 std::function<void(const std::vector<product_t>&, provider_funct_t)> callback);
15 callback);
16 ~PythonInterpreter();
19 ~PythonInterpreter();
17 void eval(const std::string& file);
20 void eval(const std::string& file);
18 void release();
21 void release();
19
22
20 private:
23 private:
21 };
24 };
@@ -1,33 +1,32
1 #ifndef PYTHON_PROVIDERS_H
1 #ifndef PYTHON_PROVIDERS_H
2 #define PYTHON_PROVIDERS_H
2 #define PYTHON_PROVIDERS_H
3
3
4 #include <Plugin/IPlugin.h>
4 #include <Plugin/IPlugin.h>
5 #include <QUuid>
5 #include <QUuid>
6
6
7 #include <memory>
7 #include <memory>
8 #include <python_interpreter.h>
8 #include <python_interpreter.h>
9
9
10 #ifndef SCIQLOP_PLUGIN_JSON_FILE_PATH
10 #ifndef SCIQLOP_PLUGIN_JSON_FILE_PATH
11 #define SCIQLOP_PLUGIN_JSON_FILE_PATH "python_providers.json"
11 #define SCIQLOP_PLUGIN_JSON_FILE_PATH "python_providers.json"
12 #endif
12 #endif
13
13
14 class DataSourceItem;
14 class DataSourceItem;
15
15
16 class PythonProviders : public QObject, public IPlugin
16 class PythonProviders : public QObject, public IPlugin
17 {
17 {
18 Q_OBJECT
18 Q_OBJECT
19 Q_INTERFACES(IPlugin)
19 Q_INTERFACES(IPlugin)
20 Q_PLUGIN_METADATA(IID "sciqlop.plugin.IPlugin" FILE SCIQLOP_PLUGIN_JSON_FILE_PATH)
20 Q_PLUGIN_METADATA(IID "sciqlop.plugin.IPlugin" FILE SCIQLOP_PLUGIN_JSON_FILE_PATH)
21 public:
21 public:
22 /// @sa IPlugin::initialize()
22 /// @sa IPlugin::initialize()
23 void initialize() override;
23 void initialize() override;
24 ~PythonProviders();
24 ~PythonProviders();
25
25
26 private:
26 private:
27 void register_product(const std::vector<std::pair<std::string,std::vector<std::pair<std::string,std::string>>>>& product_list,
27 void register_product(const std::vector<PythonInterpreter::product_t>& product_list,
28 PythonInterpreter::provider_funct_t
28 PythonInterpreter::provider_funct_t f);
29 f);
30 PythonInterpreter _interpreter;
29 PythonInterpreter _interpreter;
31 };
30 };
32
31
33 #endif // PYTHON_PROVIDERS_H
32 #endif // PYTHON_PROVIDERS_H
@@ -1,28 +1,60
1 import sys
1 import sys
2 sys.path.append("/home/jeandet/Documents/prog/build-SciQLop-Desktop-Debug/core")
2 sys.path.append("/home/jeandet/Documents/prog/build-SciQLop-Desktop-Debug/core")
3 import os
3 import os
4 import datetime
4 import datetime
5 import PythonProviders
5 import PythonProviders
6 import pysciqlopcore
6 import pysciqlopcore
7 import numpy as np
7 import numpy as np
8 import pandas as pds
8 import pandas as pds
9 import requests
9 import requests
10 from spwc.amda import AMDA
10
11
11 def get_sample(name,start,stop):
12 amda = AMDA()
13
14 def get_sample(metadata,start,stop):
15 ts_type = pysciqlopcore.ScalarTimeSerie
12 try:
16 try:
13 tstart=datetime.datetime.fromtimestamp(start).strftime('%Y%m%dT%H%M%SZ')
17 param_id = None
14 tend=datetime.datetime.fromtimestamp(stop).strftime('%Y%m%dT%H%M%SZ')
18 for key,value in metadata:
15 req_url=f"https://cdaweb.gsfc.nasa.gov/WS/cdasr/1/dataviews/sp_phys/datasets/MMS4_SCM_SRVY_L2_SCSRVY/data/{tstart},{tend}/mms4_scm_acb_gse_scsrvy_srvy_l2?format=csv"
19 if key == 'xml:id':
16 resp = requests.get(req_url,headers={"Accept":"application/json"})
20 param_id = value
17 csv_url = resp.json()['FileDescription'][0]['Name']
21 elif key == 'type':
18 df = pds.read_csv(csv_url,comment='#',index_col=0, infer_datetime_format=True,parse_dates=True)
22 if value == 'vector':
23 ts_type = pysciqlopcore.VectorTimeSerie
24 tstart=datetime.datetime.fromtimestamp(start)
25 tend=datetime.datetime.fromtimestamp(stop)
26 df = amda.get_parameter(start_time=tstart, stop_time=tend, parameter_id=param_id)
19 t = np.array([d.timestamp()-7200 for d in df.index])
27 t = np.array([d.timestamp()-7200 for d in df.index])
20 values = df.values
28 values = df.values
21 return pysciqlopcore.VectorTimeSerie(t,values)
29 print(len(t))
30 print(len(values))
31 return ts_type(t,values)
32 return ts_type(1)
22 except Exception as e:
33 except Exception as e:
23 print("fuck ",str(e))
34 print("Error in amda.py ",str(e))
24 return pysciqlopcore.VectorTimeSerie(1)
35 return ts_type(1)
36
37
38 if len(amda.component) is 0:
39 amda.update_inventory()
40 parameters = amda.parameter.copy()
41 for name,component in amda.component.items():
42 if 'components' in parameters[component['parameter']]:
43 parameters[component['parameter']]['components'].append(component)
44 else:
45 parameters[component['parameter']]['components']=[component]
46
47 products = []
48 for key,parameter in parameters.items():
49 path = f"/AMDA/{parameter['mission']}/{parameter['instrument']}/{parameter['dataset']}/{parameter['name']}"
50 components = [component['name'] for component in parameter.get('components',[])]
51 metadata = [ (key,item) for key,item in parameter.items() if key is not 'components' ]
52 if parameter.get('size',0) is '3':
53 metadata.append(("type","vector"))
54 else:
55 metadata.append(("type","scalar"))
56 products.append( (path, components, metadata))
25
57
26 PythonProviders.register_product([("/CDA/mms4_scm_acb_gse_scsrvy_srvy_l2",[("type","vector")])],get_sample)
58 PythonProviders.register_product(products, get_sample)
27
59
28
60
@@ -1,51 +1,50
1 #include "python_interpreter.h"
1 #include "python_interpreter.h"
2 #include <Data/DateTimeRange.h>
2 #include <Data/DateTimeRange.h>
3 #include <TimeSeries.h>
3 #include <TimeSeries.h>
4 #include <functional>
4 #include <functional>
5 #include <iostream>
5 #include <iostream>
6 #include <pybind11/embed.h>
6 #include <pybind11/embed.h>
7 #include <pybind11/functional.h>
7 #include <pybind11/functional.h>
8 #include <pybind11/stl.h>
8 #include <pybind11/stl.h>
9
9
10 namespace py = pybind11;
10 namespace py = pybind11;
11
11
12
12
13 PYBIND11_EMBEDDED_MODULE(PythonProviders, m) {}
13 PYBIND11_EMBEDDED_MODULE(PythonProviders, m) {}
14 static pybind11::gil_scoped_release* _rel = nullptr;
14 static pybind11::gil_scoped_release* _rel = nullptr;
15
15
16 PythonInterpreter::PythonInterpreter()
16 PythonInterpreter::PythonInterpreter()
17 {
17 {
18 py::initialize_interpreter(false);
18 py::initialize_interpreter(false);
19 }
19 }
20
20
21 void PythonInterpreter::add_register_callback(std::function<void(const std::vector<std::pair<std::string,std::vector<std::pair<std::string,std::string>>>>&,
21 void PythonInterpreter::add_register_callback(
22 provider_funct_t)>
22 std::function<void(const std::vector<product_t>&, provider_funct_t)> callback)
23 callback)
24 {
23 {
25 py::module PythonProviders = py::module::import("PythonProviders");
24 py::module PythonProviders = py::module::import("PythonProviders");
26 PythonProviders.attr("register_product") = callback;
25 PythonProviders.attr("register_product") = callback;
27 }
26 }
28
27
29 PythonInterpreter::~PythonInterpreter()
28 PythonInterpreter::~PythonInterpreter()
30 {
29 {
31 if (_rel)
30 if (_rel)
32 delete _rel;
31 delete _rel;
33 py::finalize_interpreter();
32 py::finalize_interpreter();
34 }
33 }
35
34
36 void PythonInterpreter::eval(const std::string& file)
35 void PythonInterpreter::eval(const std::string& file)
37 {
36 {
38 try
37 try
39 {
38 {
40 py::eval_file(file);
39 py::eval_file(file);
41 }
40 }
42 catch (py::error_already_set const& pythonErr)
41 catch (py::error_already_set const& pythonErr)
43 {
42 {
44 std::cout << pythonErr.what();
43 std::cout << pythonErr.what();
45 }
44 }
46 }
45 }
47
46
48 void PythonInterpreter::release()
47 void PythonInterpreter::release()
49 {
48 {
50 _rel = new py::gil_scoped_release();
49 _rel = new py::gil_scoped_release();
51 }
50 }
@@ -1,151 +1,151
1 #include "python_providers.h"
1 #include "python_providers.h"
2 #include <Data/DataProviderParameters.h>
2 #include <Data/DataProviderParameters.h>
3 #include <Data/DateTimeRange.h>
3 #include <Data/DateTimeRange.h>
4 #include <Data/IDataProvider.h>
4 #include <Data/IDataProvider.h>
5 #include <Data/ScalarTimeSerie.h>
5 #include <Data/ScalarTimeSerie.h>
6 #include <Data/SpectrogramTimeSerie.h>
6 #include <Data/SpectrogramTimeSerie.h>
7 #include <Data/TimeSeriesUtils.h>
7 #include <Data/TimeSeriesUtils.h>
8 #include <Data/VectorTimeSerie.h>
8 #include <Data/VectorTimeSerie.h>
9 #include <DataSource/DataSourceController.h>
9 #include <DataSource/DataSourceController.h>
10 #include <DataSource/DataSourceItem.h>
10 #include <DataSource/DataSourceItem.h>
11 #include <DataSource/DataSourceItemAction.h>
11 #include <DataSource/DataSourceItemAction.h>
12 #include <QDir>
12 #include <QDir>
13 #include <QStandardPaths>
13 #include <QStandardPaths>
14 #include <QStringList>
14 #include <QStringList>
15 #include <SqpApplication.h>
15 #include <SqpApplication.h>
16 #include <TimeSeries.h>
16 #include <TimeSeries.h>
17 #include <functional>
17 #include <functional>
18 #include <iostream>
18 #include <iostream>
19
19
20
20
21 const auto DATA_SOURCE_NAME = QStringLiteral("PythonProviders");
21 const auto DATA_SOURCE_NAME = QStringLiteral("PythonProviders");
22
22
23 struct noop_deleter
24 {
25 void operator()(TimeSeries::ITimeSerie*) {}
26 };
27
28 class PythonProvider : public IDataProvider
23 class PythonProvider : public IDataProvider
29 {
24 {
30 public:
25 public:
31 PythonProvider(PythonInterpreter::provider_funct_t f) : _pythonFunction { f } {}
26 PythonProvider(PythonInterpreter::provider_funct_t f) : _pythonFunction { f } {}
32
27
33 PythonProvider(const PythonProvider& other) : _pythonFunction { other._pythonFunction } {}
28 PythonProvider(const PythonProvider& other) : _pythonFunction { other._pythonFunction } {}
34
29
35 std::shared_ptr<IDataProvider> clone() const override
30 std::shared_ptr<IDataProvider> clone() const override
36 {
31 {
37 return std::make_shared<PythonProvider>(*this);
32 return std::make_shared<PythonProvider>(*this);
38 }
33 }
39 virtual TimeSeries::ITimeSerie* getData(const DataProviderParameters& parameters) override
34 virtual TimeSeries::ITimeSerie* getData(const DataProviderParameters& parameters) override
40 {
35 {
41 auto product = parameters.m_Data.value("PRODUCT", "").toString().toStdString();
36 auto product = parameters.m_Data.value("PRODUCT", "").toString().toStdString();
42 auto range = parameters.m_Range;
37 auto range = parameters.m_Range;
43 auto result = _pythonFunction(product, range.m_TStart, range.m_TEnd);
38 std::vector<std::tuple<std::string, std::string>> metadata;
39 std::transform(parameters.m_Data.constKeyValueBegin(), parameters.m_Data.constKeyValueEnd(),
40 std::back_inserter(metadata), [](const auto& item) {
41 return std::tuple<std::string, std::string> { item.first.toStdString(),
42 item.second.toString().toStdString() };
43 });
44 auto result = _pythonFunction(metadata, range.m_TStart, range.m_TEnd);
44 return TimeSeriesUtils::copy(result);
45 return TimeSeriesUtils::copy(result);
45 }
46 }
46
47
47 private:
48 private:
48 PythonInterpreter::provider_funct_t _pythonFunction;
49 PythonInterpreter::provider_funct_t _pythonFunction;
49 };
50 };
50
51
51
52
52 void PythonProviders::initialize()
53 void PythonProviders::initialize()
53 {
54 {
54 _interpreter.add_register_callback(
55 _interpreter.add_register_callback(
55 [this](const std::vector<std::pair<std::string,
56 [this](const std::vector<PythonInterpreter::product_t>& product_list,
56 std::vector<std::pair<std::string, std::string>>>>& product_list,
57 PythonInterpreter::provider_funct_t f) { this->register_product(product_list, f); });
57 PythonInterpreter::provider_funct_t f) { this->register_product(product_list, f); });
58
58
59 for (const auto& path : QStandardPaths::standardLocations(QStandardPaths::AppLocalDataLocation))
59 for (const auto& path : QStandardPaths::standardLocations(QStandardPaths::AppLocalDataLocation))
60 {
60 {
61 auto dir = QDir(path + "/python");
61 auto dir = QDir(path + "/python");
62 if (dir.exists())
62 if (dir.exists())
63 {
63 {
64 for (const auto& entry :
64 for (const auto& entry :
65 dir.entryInfoList(QDir::Files | QDir::NoDotAndDotDot, QDir::Name))
65 dir.entryInfoList(QDir::Files | QDir::NoDotAndDotDot, QDir::Name))
66 {
66 {
67 if (entry.isFile() && entry.suffix() == "py")
67 if (entry.isFile() && entry.suffix() == "py")
68 {
68 {
69 _interpreter.eval(entry.absoluteFilePath().toStdString());
69 _interpreter.eval(entry.absoluteFilePath().toStdString());
70 }
70 }
71 }
71 }
72 }
72 }
73 }
73 }
74 _interpreter.release();
74 _interpreter.release();
75 }
75 }
76
76
77 PythonProviders::~PythonProviders() {}
77 PythonProviders::~PythonProviders() {}
78
78
79 std::unique_ptr<DataSourceItem> make_folder_item(const QString& name)
79 std::unique_ptr<DataSourceItem> make_folder_item(const QString& name)
80 {
80 {
81 return std::make_unique<DataSourceItem>(DataSourceItemType::NODE, name);
81 return std::make_unique<DataSourceItem>(DataSourceItemType::NODE, name);
82 }
82 }
83
83
84 template <typename T>
84 template <typename T>
85 DataSourceItem* make_path_items(
85 DataSourceItem* make_path_items(
86 const T& path_list_begin, const T& path_list_end, DataSourceItem* root)
86 const T& path_list_begin, const T& path_list_end, DataSourceItem* root)
87 {
87 {
88 std::for_each(path_list_begin, path_list_end, [&root](const auto& folder_name) mutable {
88 std::for_each(path_list_begin, path_list_end, [&root](const auto& folder_name) mutable {
89 auto folder_ptr = root->findItem(folder_name);
89 auto folder_ptr = root->findItem(folder_name);
90 if (folder_ptr == nullptr)
90 if (folder_ptr == nullptr)
91 {
91 {
92 auto folder = make_folder_item(folder_name);
92 auto folder = make_folder_item(folder_name);
93 folder_ptr = folder.get();
93 folder_ptr = folder.get();
94 root->appendChild(std::move(folder));
94 root->appendChild(std::move(folder));
95 }
95 }
96 root = folder_ptr;
96 root = folder_ptr;
97 });
97 });
98 return root;
98 return root;
99 }
99 }
100
100
101 std::unique_ptr<DataSourceItem> make_product_item(
101 std::unique_ptr<DataSourceItem> make_product_item(
102 const QVariantHash& metaData, const QUuid& dataSourceUid)
102 const QVariantHash& metaData, const QUuid& dataSourceUid)
103 {
103 {
104 auto result = std::make_unique<DataSourceItem>(DataSourceItemType::PRODUCT, metaData);
104 auto result = std::make_unique<DataSourceItem>(DataSourceItemType::PRODUCT, metaData);
105
105
106 // Adds plugin name to product metadata
106 // Adds plugin name to product metadata
107 result->setData(DataSourceItem::PLUGIN_DATA_KEY, DATA_SOURCE_NAME);
107 result->setData(DataSourceItem::PLUGIN_DATA_KEY, DATA_SOURCE_NAME);
108 result->setData(DataSourceItem::ID_DATA_KEY, metaData.value(DataSourceItem::NAME_DATA_KEY));
108 result->setData(DataSourceItem::ID_DATA_KEY, metaData.value(DataSourceItem::NAME_DATA_KEY));
109
109
110 auto productName = metaData.value(DataSourceItem::NAME_DATA_KEY).toString();
110 auto productName = metaData.value(DataSourceItem::NAME_DATA_KEY).toString();
111
111
112 // Add action to load product from DataSourceController
112 // Add action to load product from DataSourceController
113 result->addAction(
113 result->addAction(
114 std::make_unique<DataSourceItemAction>(QObject::tr("Load %1 product").arg(productName),
114 std::make_unique<DataSourceItemAction>(QObject::tr("Load %1 product").arg(productName),
115 [productName, dataSourceUid](DataSourceItem& item) {
115 [productName, dataSourceUid](DataSourceItem& item) {
116 if (auto app = sqpApp)
116 if (auto app = sqpApp)
117 {
117 {
118 app->dataSourceController().loadProductItem(dataSourceUid, item);
118 app->dataSourceController().loadProductItem(dataSourceUid, item);
119 }
119 }
120 }));
120 }));
121
121
122 return result;
122 return result;
123 }
123 }
124
124
125 void PythonProviders::register_product(
125 void PythonProviders::register_product(
126 const std::vector<std::pair<std::string, std::vector<std::pair<std::string, std::string>>>>&
126 const std::vector<PythonInterpreter::product_t>& product_list,
127 product_list,
128 PythonInterpreter::provider_funct_t f)
127 PythonInterpreter::provider_funct_t f)
129 {
128 {
130 auto& dataSourceController = sqpApp->dataSourceController();
129 auto& dataSourceController = sqpApp->dataSourceController();
131 auto id = dataSourceController.registerDataSource(DATA_SOURCE_NAME);
130 QString test = DATA_SOURCE_NAME + QUuid::createUuid().toString();
132 auto root = make_folder_item(DATA_SOURCE_NAME);
131 auto id = dataSourceController.registerDataSource(test);
132 auto root = make_folder_item(test);
133 std::for_each(std::cbegin(product_list), std::cend(product_list),
133 std::for_each(std::cbegin(product_list), std::cend(product_list),
134 [id, f, root = root.get()](const auto& product) {
134 [id, f, root = root.get()](const auto& product) {
135 const auto& path = product.first;
135 const auto& path = std::get<0>(product);
136 auto path_list = QString::fromStdString(path).split('/');
136 auto path_list = QString::fromStdString(path).split('/');
137 auto name = *(std::cend(path_list) - 1);
137 auto name = *(std::cend(path_list) - 1);
138 auto path_item
138 auto path_item
139 = make_path_items(std::cbegin(path_list), std::cend(path_list) - 1, root);
139 = make_path_items(std::cbegin(path_list), std::cend(path_list) - 1, root);
140 QVariantHash metaData { { DataSourceItem::NAME_DATA_KEY, name } };
140 QVariantHash metaData { { DataSourceItem::NAME_DATA_KEY, name } };
141 std::for_each(std::cbegin(product.second), std::cend(product.second),
141 std::for_each(std::cbegin(std::get<2>(product)), std::cend(std::get<2>(product)),
142 [&metaData](const auto& mdata) {
142 [&metaData](const auto& mdata) {
143 metaData[QString::fromStdString(mdata.first)]
143 metaData[QString::fromStdString(mdata.first)]
144 = QString::fromStdString(mdata.second);
144 = QString::fromStdString(mdata.second);
145 });
145 });
146 path_item->appendChild(make_product_item(metaData, id));
146 path_item->appendChild(make_product_item(metaData, id));
147 });
147 });
148 dataSourceController.setDataSourceItem(id, std::move(root));
148 dataSourceController.setDataSourceItem(id, std::move(root));
149 dataSourceController.setDataProvider(id, std::make_unique<PythonProvider>(f));
149 dataSourceController.setDataProvider(id, std::make_unique<PythonProvider>(f));
150 std::cout << "Gone there" << std::endl;
150 std::cout << "Gone there" << std::endl;
151 }
151 }
General Comments 0
You need to be logged in to leave comments. Login now