prisma_7d_db_copier.py 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import pymongo
  2. import pandas as pd
  3. import datetime
  4. from config_info.config import *
  5. db_client = pymongo.MongoClient(DB_URL)
  6. prisma_db = db_client["prisma-32_db"]
  7. def prisma_7d_past_data_copier(date, cluster):
  8. if cluster == 1:
  9. n7_file_template = f"n7_{date.month:02}-{date.day:02}.{date.year - 2000:02}"
  10. n7_file = pd.read_csv(PATH_TO_PRISMA_1_7d_DATA + n7_file_template, sep=' ', skipinitialspace=True, header=None)
  11. n7_file = n7_file.dropna(axis=1, how='all')
  12. print("Data file: {}".format(PATH_TO_PRISMA_1_7d_DATA + n7_file_template))
  13. else:
  14. n7_file_template = f"2n7_{date.month:02}-{date.day:02}.{date.year - 2000:02}"
  15. n7_file = pd.read_csv(PATH_TO_PRISMA_2_7d_DATA + n7_file_template, sep=' ', skipinitialspace=True, header=None)
  16. n7_file = n7_file.dropna(axis=1, how='all')
  17. print("Data file: {}".format(PATH_TO_PRISMA_2_7d_DATA + n7_file_template))
  18. for index in range(len(n7_file.index)):
  19. params = list(n7_file.iloc[index])
  20. if type(params[0]) is str:
  21. params[0] = float('.'.join(params[0].split(',')))
  22. event_time = str(datetime.timedelta(seconds=params[0])) # перевод в utc-формат
  23. # event_date = (datetime.timedelta(seconds=params[0]) + datetime.timedelta(hours=3)).date()
  24. event_datetime = datetime.datetime(date.year, date.month, date.day, int(event_time.split(':')[0]),
  25. int(event_time.split(':')[1]), int(float(event_time.split(':')[2])),
  26. int(round(
  27. float(event_time.split(':')[2]) - int(float(event_time.split(':')[2])),
  28. 2) * 10 ** 6)) - datetime.timedelta(hours=3)
  29. trigger = params[2]
  30. amp = [int(params[j]) for j in range(3, 19)]
  31. det_params = {}
  32. for i in range(1, 17):
  33. det_params[f'det_{i:02}'] = {
  34. 'amplitude': amp[i - 1]
  35. }
  36. try:
  37. new_record = {
  38. '_id': f'{event_datetime.date()}_{cluster:02}_07d_{int(event_datetime.hour):02}:' +
  39. f'{int(event_datetime.minute):02}:{int(event_datetime.second):02}.' +
  40. f'{str(event_datetime.microsecond)[:3]}.000.000',
  41. 'time_ns': int((int(event_datetime.hour) * 1440 + int(event_datetime.minute) * 60 + int(
  42. event_datetime.second)) * 10e8 + int(event_datetime.microsecond) * 1000),
  43. 'cluster': cluster,
  44. 'trigger': int(trigger),
  45. 'detectors': det_params
  46. }
  47. collection_prisma = prisma_db[f'{str(event_datetime.date())}_7d']
  48. ins_result = collection_prisma.insert_one(new_record)
  49. print(f'Copied - {ins_result.inserted_id}')
  50. except pymongo.errors.DuplicateKeyError:
  51. print(f'Ошибка - {event_datetime.date()}-{event_time}')
  52. # Press the green button in the gutter to run the script.
  53. if __name__ == '__main__':
  54. cluster_1 = 1
  55. cluster_2 = 2
  56. date_time_start = datetime.date(2021, 12, 1) # посмотреть почему не собирается конец дня 2018-04-22
  57. date_time_stop = datetime.date(2021, 12, 31)
  58. LIST_OF_DATES = [(date_time_start + datetime.timedelta(days=i)) for i in
  59. range((date_time_stop - date_time_start).days + 1)]
  60. for date in LIST_OF_DATES:
  61. try:
  62. prisma_7d_past_data_copier(date, cluster_1)
  63. except FileNotFoundError:
  64. print(f'файла {cluster_1}-го кластера от {date} не существует')
  65. try:
  66. prisma_7d_past_data_copier(date, cluster_2)
  67. except FileNotFoundError:
  68. print(f'файла {cluster_2}-го кластера от {date} не существует')
  69. print('test')