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

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