Source code for ecpi.database.interface_MdbSim

'''

Created on 15 mai 2018

@author: Colley Jean-Marc, APC/IN2P3/CNRS
'''

import os
import logging
import requests
import subprocess
import tarfile

from ecpi.simu.lib.database import DataBaseSVOMSimV1
    
logger = logging.getLogger(__name__) 

[docs]class DataBaseSVOMMdbSim(DataBaseSVOMSimV1): """ A revoir en se basant sur DataBaseSVOMMdbDC0 """
[docs] def get_files_evt_between_time(self, p_tstart, p_tstop, p_id): """ p_tstart and p_tstop in s from mjdref simu evt files and save them into dir_in evt files names are ECL_EVT_CAL + s from mjdref """ dir_in = self._get_name_input(p_id) mdb_address = 'http://' + os.environ['MDB_SERVER_HOST'] + ':' + os.environ['MDB_SERVER_PORT'] + '/' r = requests.post(mdb_address + 'obs', data = {'p_id':p_id, 'tb':p_tstart, 'te':p_tstop}) if r.status_code == 200: tar_filename = p_id + '.tar.gz' tar_abspath_filename = os.path.join(self.dir_proc, tar_filename) r = requests.get(mdb_address + 'processing/' + tar_filename) with open(tar_abspath_filename, 'wb') as f: f.write(r.content) #with open(os.path.join(self.dir_proc, tar_filename), 'wb') as fd: # for chunk in r.iter_content(chunk_size=128): # fd.write(chunk) tar = tarfile.open(tar_abspath_filename) tar.extractall(self.dir_proc) tar.close() os.system('rm ' + tar_abspath_filename) return dir_in else: logger.info("OH MY GOD ! something's wrong on the MDB side !") return
[docs] def insert_product(self, p_dir, p_id): """ insert a product file in database copy the file from input in dir_science_products directory of the database :param p_dir: output dir :type p_product_fits: string """ dir_out = self._get_name_output(p_id) cmd = 'cp %s/* %s'%(p_dir, dir_out) subprocess.run(cmd, shell=True) tarfile= self.get_product(p_id) file = {'file': open(tarfile, 'rb')} mdb_address = 'http://' + os.environ['MDB_SERVER_HOST'] + ':' + os.environ['MDB_SERVER_PORT'] + '/' r = requests.post(mdb_address, files=file) if r.status_code != 200 : logger.info("OH MY GOD ! something's wrong on the MDB side !") logger.info("The MDB could not properly get the product")
[docs]class DataBaseSVOMMdbDC0(DataBaseSVOMSimV1):
[docs] def get_files_from_names(self, evt_filenames, att_filenames, p_id, cfg): """ p_tstart and p_tstop in s from mjdref download evt files and save them into dir_in evt files names are ECL_EVT_CAL + s from mjdref """ try: mdb_address = 'http://' + str(cfg['mdb']['HOST']) + ':' + str(cfg['mdb']['PORT']) + '/' + cfg['mdb']['PATH'] + '/' except: mdb_address = 'http://' + str(cfg['mdb']['HOST']) + ':' + str(cfg['mdb']['PORT']) + '/' dir_in = self._get_name_input(p_id) for evt_filename in evt_filenames: abspath_filename = os.path.join(self.dir_proc, evt_filename) r = requests.get(mdb_address + evt_filename) if r.status_code == 200: with open(abspath_filename, 'wb') as f: f.write(r.content) os.system('mv {} {}'.format(abspath_filename, dir_in)) else: logger.info("OH MY GOD ! something's wrong on the MDB side !") logger.info("The MDB could not provide the asked EVT file") return for att_filename in att_filenames: abspath_filename = os.path.join(self.dir_proc, att_filename) r = requests.get(mdb_address + att_filename) if r.status_code == 200: with open(abspath_filename, 'wb') as f: f.write(r.content) os.system('mv {} {}'.format(abspath_filename, dir_in)) else: logger.info("OH MY GOD ! something's wrong on the MDB side !") logger.info("The MDB could not provide the asked ATT file") return return dir_in
[docs] def insert_product(self, p_dir, p_id): """ insert a product file in database copy the file from input in dir_science_products directory of the database :param p_dir: products dir :type p_product_fits: string """ # copy of product files dir_out = self._get_name_output(p_id) cmd = 'cp %s/* %s'%(p_dir, dir_out) subprocess.run(cmd, shell=True) os.system('rm -rf {}'.format(p_dir)) tarfile= self.get_product(p_id) if tarfile == "_NOK": logger.info("OH MY GOD ! something's wrong on the MDB side !") logger.info("Impossible to copy the product")
# pas de mdb #file = {'file': open(tarfile, 'rb')} #MDB_address = 'http://' + os.environ['MDB_SERVER_HOST'] + ':' + os.environ['MDB_SERVER_PORT'] + '/' # r = requests.post(MDB_address, files=file)