db_file_reader.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import datetime
  2. from collections import defaultdict
  3. from file_reader.file_reader import FileReader
  4. import pandas as pd
  5. import pymongo
  6. class DbFileReader(FileReader):
  7. # __DB_URL = "mongodb://localhost:27017/"
  8. __amp_n_cols = []
  9. for i in range(1, 17):
  10. __amp_n_cols.append(f'amp{i}')
  11. __amp_n_cols.append(f'n{i}')
  12. def __init__(self, cluster, single_date, db_url):
  13. self.cluster = cluster
  14. self.single_date = single_date
  15. self.__db_url = db_url
  16. def reading_db(self) -> pd.DataFrame():
  17. """Метод, прочитывающий noSQL БД ПРИЗМА-32 с помощью DB_URL"""
  18. data_cl = pd.DataFrame.from_records(
  19. pymongo.MongoClient(self.__db_url)["prisma-32_db"][f'{str(self.single_date.date())}_12d'].find(
  20. {'cluster': self.cluster}))
  21. # print(self.single_date)
  22. # print(data_cl)
  23. if data_cl.empty:
  24. raise FileNotFoundError
  25. amp_dict = defaultdict(list)
  26. n_dict = defaultdict(list)
  27. for item in data_cl['detectors']:
  28. for j in [f'det_{i:02}' for i in range(1, 17)]:
  29. amp_dict[j].append(item[j]['amplitude'])
  30. n_dict[j].append(item[j]['neutrons'])
  31. for i in range(1, 17):
  32. data_cl[f'amp{i}'] = amp_dict[f'det_{i:02}']
  33. data_cl[f'n{i}'] = n_dict[f'det_{i:02}']
  34. data_cl['time'] = [round(item / 1e9, 2) for item in data_cl['time_ns']]
  35. data_cl['Date'] = [datetime.date(int(item[0:4]), int(item[5:7]), int(item[8:10])) for item in data_cl['_id']]
  36. return data_cl
  37. def concat_n_data(self, concat_n_df):
  38. data_cl = self.reading_db()
  39. # noinspection PyUnresolvedReferences
  40. concat_n_df = pd.concat([concat_n_df, data_cl[['Date', 'time', 'trigger'] + DbFileReader.__amp_n_cols]],
  41. ignore_index=True)
  42. return concat_n_df
  43. @staticmethod
  44. def db_preparing_data(start_date, end_date, path_to_db):
  45. concat_n_df_1 = pd.DataFrame(columns=['Date', 'time', 'trigger'] + DbFileReader.__amp_n_cols)
  46. concat_n_df_2 = pd.DataFrame(columns=['Date', 'time', 'trigger'] + DbFileReader.__amp_n_cols)
  47. for single_date in pd.date_range(start_date, end_date):
  48. try:
  49. db_file_reader_1 = DbFileReader(cluster=1, single_date=single_date, db_url=path_to_db)
  50. concat_n_df_1 = db_file_reader_1.concat_n_data(concat_n_df=concat_n_df_1)
  51. except FileNotFoundError:
  52. print(
  53. f"File n_{single_date.month:02}-" +
  54. f"{single_date.day:02}.{single_date.year - 2000:02}', does not exist")
  55. try:
  56. db_file_reader_1 = DbFileReader(cluster=2, single_date=single_date, db_url=path_to_db)
  57. concat_n_df_2 = db_file_reader_1.concat_n_data(concat_n_df=concat_n_df_2)
  58. except FileNotFoundError:
  59. print(
  60. f"File 2n_{single_date.month:02}-" +
  61. f"{single_date.day:02}.{single_date.year - 2000:02}', does not exist")
  62. return concat_n_df_1, concat_n_df_2