#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Thu Jul 30 09:56:19 2020 @author: anekallu """ import sys import json import xml.etree.ElementTree as ET import datetime import os def main(): planned_stp = int(sys.argv[1]) # STP number to make the plan for xml_in = sys.argv[2] # EFECS file for this LTP json_in = sys.argv[3] # SWA observations JSON file for this LTP output_file = 'log_input_STP' + sys.argv[1] if os.path.exists(output_file): os.remove(output_file) #else: # print("creating outputfile: {0}".format(output_file)) # initialize stp_start, stp_end and stp_number as global variables stp_start = datetime.datetime.strptime("2000-001T00:00:00Z","%Y-%jT%H:%M:%SZ") #start of STP stp_end = datetime.datetime.strptime("2000-002T00:00:00Z","%Y-%jT%H:%M:%SZ") # end of STP stp_end1 = datetime.datetime.strptime("2000-002T00:00:00Z","%Y-%jT%H:%M:%SZ") # End of STP + 1 day to capture WOL/TCM stp_number = 0 process_EFECS(xml_in, json_in, planned_stp, stp_start, stp_end, stp_end1, stp_number, output_file) ################################# End of main() ####################################### ############################# Start of PAS_CALIB times ################################################# def add_PAS_CALIB_times(stp_start_time, stp_end_time, outfile): t_value = stp_start_time + datetime.timedelta(0, 43200) f = open(outfile, "a") print(t_value.strftime("%Y-%m-%dT%H:%M:%S"), "PAS_CALIB_RUN") f.write("{0} PAS_CALIB_RUN\n".format(t_value.strftime("%Y-%m-%dT%H:%M:%S"))) i = 0 while i < 6: t_value = t_value + datetime.timedelta(0, 86400) print(t_value.strftime("%Y-%m-%dT%H:%M:%S"), "PAS_CALIB_RUN") f.write("{0} PAS_CALIB_RUN\n".format(t_value.strftime("%Y-%m-%dT%H:%M:%S"))) i+=1 ############################# End of PAS_CALIB times ################################################# ############################# Start of EFECS file processing ################################################# def process_EFECS(xml_in, json_in, planned_stp, stp_start, stp_end, stp_end1, stp_number, outfile): tree = ET.parse(xml_in) root = tree.getroot() f = open(outfile, "a") for node in root.findall('.//*'): Elem = node.tag.split('}', 1)[1] if Elem == "STP_START": x_value = datetime.datetime.strptime(node.attrib.get("time"),"%Y-%jT%H:%M:%SZ") stp_number = int(node.attrib.get("stp_number")) if stp_number == planned_stp: stp_start = x_value elif stp_number == (planned_stp + 1): x_value = datetime.datetime.strptime(node.attrib.get("time"),"%Y-%jT%H:%M:%SZ") stp_end = x_value x_value = x_value + datetime.timedelta(0, 86400) stp_end1 = x_value today = datetime.date.today() f.write("{0} This plan is for STP-{1} and covers {2} - {3}\n".format(today.strftime("%Y-%m-%dT%H:%M:%S"), planned_stp, stp_start.strftime("%Y-%m-%dT%H:%M:%SZ"), stp_end.strftime("%Y-%m-%dT%H:%M:%SZ"))) print("{0} This plan is for STP-{1} and covers {2} - {3}".format(today.strftime("%Y-%m-%dT%H:%M:%S"), planned_stp, stp_start.strftime("%Y-%m-%dT%H:%M:%SZ"), stp_end.strftime("%Y-%m-%dT%H:%M:%SZ"))) #print(stp_end1.strftime("%Y-%m-%dT%H:%M:%S")) f.close() process_JSON(json_in, stp_start, stp_end, outfile) add_PAS_CALIB_times(stp_start, stp_end, outfile) f = open(outfile, "a") for node in root.findall('.//*'): Elem = node.tag.split('}', 1)[1] if Elem == "WOL": x_value = datetime.datetime.strptime(node.attrib.get("time"),"%Y-%jT%H:%M:%SZ") duration = int(node.attrib.get("duration")) Added = x_value + datetime.timedelta(0, duration) if x_value >= stp_start and x_value <= stp_end1: print(x_value.strftime("%Y-%m-%dT%H:%M:%S"), "WOL_start") print(Added.strftime("%Y-%m-%dT%H:%M:%S"), "WOL_end") f.write("{0} WOL_start\n".format(x_value.strftime("%Y-%m-%dT%H:%M:%S"))) f.write("{0} WOL_end\n".format(Added.strftime("%Y-%m-%dT%H:%M:%S"))) elif Elem == "EMC_MAND_QUIET": x_value = datetime.datetime.strptime(node.attrib.get("time"),"%Y-%jT%H:%M:%SZ") duration = (int(node.attrib.get("duration")) / 2) - 150 Added = x_value + datetime.timedelta(0, duration) if x_value >= stp_start and x_value <= stp_end1: print(Added.strftime("%Y-%m-%dT%H:%M:%S"), "ALL_SENS_OPT_BURST") f.write("{0} ALL_SENS_OPT_BURST\n".format(Added.strftime("%Y-%m-%dT%H:%M:%S"))) elif Elem == "ROLL": x_value = datetime.datetime.strptime(node.attrib.get("time"),"%Y-%jT%H:%M:%SZ") duration = int(node.attrib.get("duration")) Added = x_value + datetime.timedelta(0, duration) st_roll_deg = int(node.attrib.get("start_roll_deg")) en_roll_deg = int(node.attrib.get("end_roll_deg")) if x_value >= stp_start and x_value <= stp_end1: print(x_value.strftime("%Y-%m-%dT%H:%M:%S"), "ROLL_start start_angle_deg:", st_roll_deg) print(Added.strftime("%Y-%m-%dT%H:%M:%S"), "ROLL_end end_angle_deg:", en_roll_deg) f.write("{0} ROLL_start start_angle_deg:{1}\n".format(x_value.strftime("%Y-%m-%dT%H:%M:%S"), st_roll_deg)) f.write("{0} ROLL_end end_angle_deg:{1}\n".format(Added.strftime("%Y-%m-%dT%H:%M:%S"), en_roll_deg)) elif Elem == "SA_ROT": x_value = datetime.datetime.strptime(node.attrib.get("time"),"%Y-%jT%H:%M:%SZ") duration = int(node.attrib.get("duration")) Added = x_value + datetime.timedelta(0, duration) end_angle_deg = int(node.attrib.get("end_angle_deg")) st_angle_deg = int(node.attrib.get("start_angle_deg")) if x_value >= stp_start and x_value <= stp_end1: print(x_value.strftime("%Y-%m-%dT%H:%M:%S"), "SA_ROT_start start_angle_deg:", st_angle_deg) print(Added.strftime("%Y-%m-%dT%H:%M:%S"), "SA_ROT_end end_angle_deg:", end_angle_deg) f.write("{0} SA_ROT_start start_angle_deg:{1}\n".format(x_value.strftime("%Y-%m-%dT%H:%M:%S"),st_angle_deg)) f.write("{0} SA_ROT_end end_angle_deg:{1}\n".format(Added.strftime("%Y-%m-%dT%H:%M:%S"),end_angle_deg)) elif Elem == "IS_COORD_BURST": x_value = datetime.datetime.strptime(node.attrib.get("time"),"%Y-%jT%H:%M:%SZ") Added = x_value + datetime.timedelta(0, -150) if x_value >= stp_start and x_value <= stp_end1: print(Added.strftime("%Y-%m-%dT%H:%M:%S"), "ALL_SENS_CORE_BURST") f.write("{0} ALL_SENS_CORE_BURST\n".format(Added.strftime("%Y-%m-%dT%H:%M:%S"))) elif Elem == "TCM": x_value = datetime.datetime.strptime(node.attrib.get("time"),"%Y-%jT%H:%M:%SZ") duration = int(node.attrib.get("duration")) Added = x_value + datetime.timedelta(0, duration) tcm_type = node.attrib.get("type") if x_value >= stp_start and x_value <= stp_end1: print(x_value.strftime("%Y-%m-%dT%H:%M:%S"), "TCM_start type:",tcm_type) print(Added.strftime("%Y-%m-%dT%H:%M:%S"), "TCM_end type:",tcm_type) f.write("{0} TCM_start type:{1}\n".format(x_value.strftime("%Y-%m-%dT%H:%M:%S"),tcm_type)) f.write("{0} TCM_end type:{1}\n".format(Added.strftime("%Y-%m-%dT%H:%M:%S"),tcm_type)) elif Elem == "NAV": x_value = datetime.datetime.strptime(node.attrib.get("time"),"%Y-%jT%H:%M:%SZ") duration = int(node.attrib.get("duration")) Added = x_value + datetime.timedelta(0, duration) if x_value >= stp_start and x_value <= stp_end1: print(x_value.strftime("%Y-%m-%dT%H:%M:%S"), "NAV_start") print(Added.strftime("%Y-%m-%dT%H:%M:%S"), "NAV_end") f.write("{0} NAV_start\n".format(x_value.strftime("%Y-%m-%dT%H:%M:%S"))) f.write("{0} NAV_end\n".format(Added.strftime("%Y-%m-%dT%H:%M:%S"))) elif Elem == "CALIB_ROLL_MAG": x_value = datetime.datetime.strptime(node.attrib.get("time"),"%Y-%jT%H:%M:%SZ") duration = int(node.attrib.get("duration")) Added = x_value + datetime.timedelta(0, duration) if x_value >= stp_start and x_value <= stp_end1: print(x_value.strftime("%Y-%m-%dT%H:%M:%S"), "CALIB_ROLL_MAG_start") print(Added.strftime("%Y-%m-%dT%H:%M:%S"), "CALIB_ROLL_MAG_end") f.write("{0} CALIB_ROLL_MAG_start\n".format(x_value.strftime("%Y-%m-%dT%H:%M:%S"))) f.write("{0} CALIB_ROLL_MAG_end\n".format(Added.strftime("%Y-%m-%dT%H:%M:%S"))) elif Elem == "MAINT": x_value = datetime.datetime.strptime(node.attrib.get("time"),"%Y-%jT%H:%M:%SZ") duration = int(node.attrib.get("duration")) maint_type = node.attrib.get("type") Added = x_value + datetime.timedelta(0, duration) if x_value >= stp_start and x_value <= stp_end1: print(x_value.strftime("%Y-%m-%dT%H:%M:%S"), "MAINT_start type:", maint_type) print(Added.strftime("%Y-%m-%dT%H:%M:%S"), "MAINT_end type:", maint_type) f.write("{0} MAINT_start type:{1}\n".format(x_value.strftime("%Y-%m-%dT%H:%M:%S"), maint_type)) f.write("{0} MAINT_end type:{1}\n".format(Added.strftime("%Y-%m-%dT%H:%M:%S"), maint_type)) ############################# End of EFECS file processing ################################################# ############################# Start of JSON file processing ################################################# def process_JSON(json_in, stp_start, stp_end, outfile): # Process SWA Observations JSON file #import re with open(json_in,'r') as f: swa_obs = json.load(f) # Check if the json file format is old or new if isinstance(swa_obs, dict): obs_dict = swa_obs.get('observations') elif isinstance(swa_obs, list): obs_dict = swa_obs f = open(outfile, "a") #print(obs_dict[0]['name']) #print('Size of the list is: ', len(obs_dict)) i = 0 while i < len(obs_dict): Ins_name = obs_dict[i]['module'] stDate = datetime.datetime.strptime(obs_dict[i]['startDate'],"%Y-%m-%dT%H:%M:%SZ") enDate = datetime.datetime.strptime(obs_dict[i]['endDate'],"%Y-%m-%dT%H:%M:%SZ") socID = obs_dict[i]['socIds'] #print(stDate.strftime("%Y-%m-%dT%H:%M:%SZ"), Ins_name, socID[0], enDate.strftime("%Y-%m-%dT%H:%M:%SZ")) if(stp_start.strftime("%Y-%m-%dT%H:%M:%SZ") >= stDate.strftime("%Y-%m-%dT%H:%M:%SZ") and stp_end.strftime("%Y-%m-%dT%H:%M:%SZ") <= enDate.strftime("%Y-%m-%dT%H:%M:%SZ")): f.write("{0} {1} {2}\n".format(stDate.strftime("%Y-%m-%dT%H:%M:%S"), Ins_name, socID[0])) print(stDate.strftime("%Y-%m-%dT%H:%M:%S"), Ins_name, socID[0]) i += 1 f.close() ############################# End of JSON file processing ################################################# ################################## Start the Program here ################################################## if __name__ == "__main__": #start the program main() ################################## End the Program here ##################################################