This commit is contained in:
Jianshun Gao 2023-11-02 11:03:19 +01:00
parent f1e547b1be
commit 3e4edd8b0c
4 changed files with 231 additions and 1254 deletions

View File

@ -12,6 +12,7 @@ import xarray as xr
# #sys.path.insert(0, 'C:/Users/Fabrizio Klassen/PycharmProjects/DyLabDataViewer/src/bin/Analyser/AnalyserScript')
# import sys
# sys.path.append('../')
# from bin.Analyser.AnalyserScript.ToolFunction.ToolFunction import get_date
from ToolFunction.ToolFunction import get_date
@ -49,6 +50,10 @@ class MongoDB:
:return: The converted data
:rtype: normal python data type
"""
if data is None:
return None
for key in data:
typeKey = type(data[key])
if typeKey in npTypeDict:
@ -93,7 +98,7 @@ class MongoDB:
self.month = int(date[1])
self.day = int(date[2])
def create_global(self, shotNum, dataSet=None, date=None):
def create_global(self, shotNum, dataSet=None, date=None, overwrite=True, runNum=None):
"""Creat a the global document in MongoDB
:param shotNum: The shot number
@ -102,32 +107,58 @@ class MongoDB:
:type dataSet: xarray DataSet, optional
:param date: the date of the data, defaults to None
:type date: str, optional
:param overwrite: If overwrite the exist global document, defaults to True
:type overwrite: bool, optional
"""
if not date is None:
self.set_date(date)
data = {
'year': self.year,
'month': self.month,
'day': self.day,
'shotNum': shotNum,
}
self.mongoDB['global'].delete_many(data)
if runNum is None:
data = {
'year': self.year,
'month': self.month,
'day': self.day,
'shotNum': shotNum,
}
runNum = 0
else:
data = {
'year': self.year,
'month': self.month,
'day': self.day,
'shotNum': shotNum,
'runNum': runNum,
}
if overwrite:
self.mongoDB['global'].delete_many(data)
else:
res = self.mongoDB['global'].find_one(data)
if not res is None:
if not len(res)==0:
return
data = {
'year': self.year,
'month': self.month,
'day': self.day,
'shotNum': shotNum,
'runNum': 0,
'runNum': runNum,
'global_parameters' : {},
}
if dataSet is None:
self.mongoDB['global'].insert_one(data)
return
if ('scanAxis' in dataSet.attrs) and len(dataSet.attrs['scanAxis'])==0:
del dataSet.attrs['scanAxis']
del dataSet.attrs['scanAxisLength']
global_parameters = self._convert_numpy_type(dataSet.attrs)
if not dataSet is None:
data['global_parameters'].update(global_parameters)
data['global_parameters'].update(global_parameters)
data = self._convert_numpy_type(data)
@ -201,7 +232,7 @@ class MongoDB:
self.mongoDB['global'].update_one(filter, {"$set": data}, upsert=False)
def _add_data_xarray_dataArray(self, shotNum, dataArray, name=None, scanAxis=None):
def _add_data_xarray_dataArray(self, shotNum, dataArray, name=None, scanAxis=None, runNum=None):
"""Write the data in a type of xarray DataArray to the MongoDb.
:param shotNum: The shot number
@ -213,22 +244,54 @@ class MongoDB:
:param scanAxis: The scan axes of the data, defaults to None
:type scanAxis: array like, optional
"""
if scanAxis is None:
scanAxis = list(dataArray.coords)
if name is None:
name = dataArray.name
dataArray.attrs = self._convert_numpy_type(dataArray.attrs)
if scanAxis is None or len(scanAxis) == 0:
if runNum is None:
return
filter = {
'year': self.year,
'month': self.month,
'day': self.day,
'shotNum': shotNum,
'runNum': runNum,
}
mongoID, _ = self.xdb.put(dataArray)
data_label = {
name:
{
'name': name,
'mongoID': mongoID,
'engine': 'xarray',
'dtype': 'dataArray',
}
}
self.mongoDB['global'].update_one(filter, {"$set": data_label}, upsert=False)
return
stackedDataArray = dataArray.stack(_scanAxis=tuple(scanAxis))
stackedDataArray = stackedDataArray.groupby('_scanAxis')
filter = {
'year': self.year,
'month': self.month,
'day': self.day,
'shotNum': shotNum,
}
for i in stackedDataArray:
stackedDataArray_single = i[1].drop('_scanAxis')
@ -243,9 +306,9 @@ class MongoDB:
mongoID, _ = self.xdb.put(stackedDataArray_single)
data_label = {
dataArray.name:
name:
{
'name': dataArray.name,
'name': name,
'mongoID': mongoID,
'engine': 'xarray',
'dtype': 'dataArray',
@ -254,7 +317,7 @@ class MongoDB:
self.mongoDB['global'].update_one(filter, {"$set": data_label}, upsert=False)
def _add_data_xarray_dataSet(self, shotNum, dataSet, name, scanAxis=None):
def _add_data_xarray_dataSet(self, shotNum, dataSet, name, scanAxis=None, runNum=None):
"""Write the data in a type of xarray DataSet to the MongoDb.
:param shotNum: The shot number
@ -274,6 +337,35 @@ class MongoDB:
for key in list(dataSet.data_vars):
dataSet[key].attrs = self._convert_numpy_type(dataSet[key].attrs)
if scanAxis is None or len(scanAxis) == 0:
if runNum is None:
return
filter = {
'year': self.year,
'month': self.month,
'day': self.day,
'shotNum': shotNum,
'runNum': runNum,
}
mongoID, _ = self.xdb.put(dataSet)
data_label = {
name:
{
'name': name,
'mongoID': mongoID,
'engine': 'xarray',
'dtype': 'dataSet',
}
}
self.mongoDB['global'].update_one(filter, {"$set": data_label}, upsert=False)
return
stackedDataSet = dataSet.stack(_scanAxis=tuple(scanAxis))
stackedDataSet = stackedDataSet.groupby('_scanAxis')
@ -371,9 +463,9 @@ class MongoDB:
self._add_data_normal(shotNum=shotNum, runNum=runNum, data=data)
elif engine == 'xarray':
if isinstance(data, type(xr.Dataset())):
self._add_data_xarray_dataSet(shotNum=shotNum, dataSet=data, name=name)
self._add_data_xarray_dataSet(shotNum=shotNum, dataSet=data, runNum=runNum, name=name)
else:
self._add_data_xarray_dataArray(shotNum=shotNum, dataArray=data, name=name)
self._add_data_xarray_dataArray(shotNum=shotNum, dataArray=data, runNum=runNum, name=name)
elif engine == 'additional':
self._add_data_additional(shotNum=shotNum, runNum=runNum, data=data, name=name)
@ -641,14 +733,18 @@ class MongoDB:
docs = self.mongoDB['global'].find(filter).sort('runNum')
data = []
i = 0
for doc in docs:
key=data_key
if isinstance(doc[key], dict) and ('mongoID' in doc[key]):
mongoID = doc[key]['mongoID']
engine = doc[key]['engine']
single_data = self._load_data_single(mongoID=mongoID, engine=engine)
print(single_data)
for axis in globalDict['scanAxis']:
if not axis in single_data.coords:
single_data = single_data.assign_coords({axis:globalDict[axis][i]})
if not axis in single_data.dims:
single_data = single_data.expand_dims(axis)
else:
@ -656,6 +752,8 @@ class MongoDB:
single_data = doc[key]
data.append(single_data)
i = i + 1
# combine data along coordinate axes
try:

BIN
droplet.pdf Normal file

Binary file not shown.

BIN
droplet1.pdf Normal file

Binary file not shown.

File diff suppressed because one or more lines are too long