makeRequest.py 4.21 KB

# coding: utf-8

# In[1]:

from datetime import datetime, timedelta
from lxml import etree
import sys, os
import numpy as np

# In[2]:

def checkArgs(nbArgs, message):
    if len(sys.argv) != nbArgs:
        print(message)
        sys.exit(2)
        
def getMarginDays(tgtName):
	if tgtName == 'mercury':
		distAU = 0.61
	elif tgtName == 'venus':
		distAU = 0.28
	elif tgtName == 'mars':
		distAU = 0.52
	elif tgtName == 'jupiter':
		distAU = 4.21
	elif tgtName == 'saturn':
		distAU = 8.54
	elif tgtName == 'p67':
		distAU = 4.0
	elif tgtName == 'juno':
		distAU = 6.0
	else:
		distAU = 9.0

	return np.ceil( (distAU*150000000)/(200*24*3600)) + 10

# 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>'+srcVI+'</SOURCE_VI>\n')
        xml.write('\t<SOURCE_R_PARAM>'+srcRParam+'</SOURCE_R_PARAM>\n')
        xml.write('\t<SOURCE_LON_PARAM>'+srcLonParam+'</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]:
if plasmaVI == 'sta_l2_pla' :
	srcVI = 'sta_l2_orb'
elif plasmaVI == 'stb_l2_pla' :	
	srcVI = 'stb_l2_orb'
else :	
	srcVI = 'earth_orb_all'
	
srcRParam = 'R_HCI'
srcLonParam = 'LON_HCI'
tgtRParam = 'R_HCI'
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 produced"
    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 produced"
    if plasmaVI == "ace_swepam_real":
	   newMissionxmlStart = datetime(newMissionStart.year,newMissionStart.month,newMissionStart.day,0,0)
	   # Future !!!
	   tgt = tgtVI.split("_")[0]	   
	   newMissionxmlStop = newMissionStop + timedelta(getMarginDays(tgt))
    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)