makeRequest.py 3.5 KB

# coding: utf-8

# In[1]:

from datetime import datetime
from lxml import etree
import sys, os


# In[2]:

def checkArgs(nbArgs, message):
    if len(sys.argv) != nbArgs:
        print(message)
        sys.exit(2)


# In[3]:

# CHECK ARGS
checkArgs(5, 'Usage : python makeRequest.py (tgt_vi) (sw_vi) (TaoStopTime) (SwStopTime)')

prefix = sys.argv[1].split("_")[0] 
tgtVI = sys.argv[1]
plasmaVI = sys.argv[2] 

# In[4]:
omniStopTime = datetime.strptime(sys.argv[4], '%Y-%m-%dT%H:%M:%S.000Z')
print  omniStopTime

# In[5]:
missionStopTime = datetime.strptime(sys.argv[3], '%Y-%m-%dT%H:%M:%S.000Z')
print  missionStopTime

# In[6]:

def writeXMLMission(XMLfilename,startTime,stopTime,plasmaVI,tgtVI,tgtRParam,tgtLonParam):
    if not os.path.exists(os.getenv('REQ')):
	 os.makedirs(os.getenv('REQ'))
		    
    with open(os.getenv('REQ')+'/'+XMLfilename, "w") as xml:
        xml.write('<?xml version="1.0" ?>\n')
        xml.write('<MISSION>\n')
        xml.write('\t<START>'+startTime+'</START>\n')
        xml.write('\t<STOP>'+stopTime+'</STOP>\n')
        xml.write('\t<PLASMA_VI>'+plasmaVI+'</PLASMA_VI>\n')
        xml.write('\t<SOURCE_VI>earth_orb_all</SOURCE_VI>\n')
        xml.write('\t<SOURCE_R_PARAM>R</SOURCE_R_PARAM>\n')
        xml.write('\t<SOURCE_LON_PARAM>LON_HCI</SOURCE_LON_PARAM>\n')
        xml.write('\t<TARGET_VI>'+tgtVI+'</TARGET_VI>\n')
        xml.write('\t<TARGET_R_PARAM>'+tgtRParam+'</TARGET_R_PARAM>\n')
        xml.write('\t<TARGET_LON_PARAM>'+tgtLonParam+'</TARGET_LON_PARAM>\n')
        xml.write('</MISSION>')
    xml.close()


# In[8]:

tgtRParam = 'R_HCI'
if tgtVI == 'p67_orb_all':
	tgtRParam = 'R'
	
tgtLonParam = 'LON_HCI'
newMissionStart = missionStopTime
newMissionStop = omniStopTime
print "New mission start : " + newMissionStart.isoformat()
print "New mission stop  : " + newMissionStop.isoformat()

if newMissionStop <= newMissionStart:
    print "Database is on date"
    exit(0)
else:
    print "Database has to be updated"
    
if newMissionStart.year != newMissionStop.year and plasmaVI != 'ace_swepam_real':
    print "2 files will be producted"
    newMissionxml1Start = datetime(newMissionStart.year,1,1,0,0)
    newMissionxml1Stop = datetime(newMissionStop.year,1,1,0,0)
    newMissionxml2Start = datetime(newMissionStop.year,1,1,0,0)
    newMissionxml2Stop = newMissionStop
    print 'XML 1 Start : ' + newMissionxml1Start.isoformat()
    print 'XML 1 Stop : ' + newMissionxml1Stop.isoformat()
    print 'XML 2 Start : ' + newMissionxml2Start.isoformat()
    print 'XML 2 Stop : ' + newMissionxml2Stop.isoformat()
    XMLfilename = prefix+'_'+str(newMissionxml1Start.year)+'.xml'
    writeXMLMission(XMLfilename,newMissionxml1Start.isoformat(),newMissionxml1Stop.isoformat(),plasmaVI,tgtVI,tgtRParam,tgtLonParam)
    XMLfilename = prefix+'_'+str(newMissionxml2Start.year)+'.xml'
    writeXMLMission(XMLfilename,newMissionxml2Start.isoformat(),newMissionxml2Stop.isoformat(),plasmaVI,tgtVI,tgtRParam,tgtLonParam)
else:
    print "1 file will be producted"
    if plasmaVI == "ace_swepam_real":
	   newMissionxmlStart = datetime(newMissionStart.year,newMissionStart.month,newMissionStart.day,0,0)  	    
    else:	    
	   newMissionxmlStart = datetime(newMissionStart.year,1,1,0,0)
    newMissionxmlStop  = newMissionStop
    print 'XML Start : ' + newMissionxmlStart.isoformat()
    print 'XML Stop : ' + newMissionxmlStop.isoformat()
    XMLfilename = prefix+'_'+str(newMissionxmlStart.year)+'.xml'
    writeXMLMission(XMLfilename,newMissionxmlStart.isoformat(),newMissionxmlStop.isoformat(),plasmaVI,tgtVI,tgtRParam,tgtLonParam)