key = opts.key run = opts.run try: int(run) except: if run != "ave" and run != "all": print "Invalid parameter for run, needs to be an integer, all or ave" sys.exit(1) fred_run = FRED_RUN(fred, key) numberDays = int(fred_run.get_param("days")) outputsAve = fred_run.outputsAve apolloDB = apollo.ApolloDB() apolloDB.connect() #for day in outputsAve: # print "Day = %d I = %d"%(day['Day'],day['I']) #ApolloConn = MySQLdb.connect(host="warhol-fred.psc.edu", # user="******", # db="apollo") #ApolloCursor = ApolloConn.cursor() ## Fill in the run table SQLString = 'INSERT INTO run set label = "' + key + '"' apolloDB.query(SQLString) runInsertID = apolloDB.insertID() # print "Run ID = " + str(runInsertID)
""") parser.add_option("-d","--debug",action="store_true") parser.add_option("-i","--runId",type="string", help="The Apollo RunId for this call") parser.add_option("-H", "--dbHost", type="string", default="warhol-fred.psc.edu") parser.add_option("-D", "--dbName", type="string", default="test") parser.add_option("-U", "--dbUser", type="string", default="apolloext") parser.add_option("-P", "--dbPword", type="string") parser.add_option("-n", "--no_db", action="store_true", default=False) opts,args=parser.parse_args() try: apolloDB = apollo.ApolloDB(host_=opts.dbHost, dbname_=opts.dbName, user_=opts.dbUser, password_=opts.dbPword) apolloDB.connect() except Exception as e: error_exit(str(e)) try: stateToDataFileDict = {'S':'susceptible.txt','E':'exposed.txt','I':'infectious.txt','T':'temp_immuned.txt', 'R':'recovered.txt','M':'immuned.txt','C':'newly_exposed.txt','MS':'mosquito_susceptible.txt', 'ME':'mosquito_exposed.txt','MI':'mosquito_infectious.txt','MC':'mosquito_newly_exposed.txt'} stateMap = {'Day':0,'S':2,'E':3,'I':4,'T':5,'R':7,'M':6,'C':8,'MS':9,'ME':10,'MI':11,'MC':12} location = "00000" fileList = [] with open('Apollo/out-1.seir','rb') as f:
statusFile = "./starttime" if simulationRunId is None: error_exit( "Need to specify an Apollo SimulationRunId to use this program") try: fred_run = FRED_RUN(fred, key) numberDays = int(fred_run.get_param("days")) outputsAve = fred_run.outputsAve except Exception as e: error_exit(str(e)) try: apolloDBs = [apollo.ApolloDB(host_=x) for x in DBHosts] for apolloDB in apolloDBs: print "working on DB: " + str(apolloDB._host) apolloDB.connect() ### Get the runId from the database locationList = [fred_run.get_param('fips')] for state, fileName in apolloDB.stateToDataFileDict.items(): stateStringList = [] if state == "C": continue for location in locationList: for day in outputsAve: state_dis = "%s_0" % state if state_dis in day.keys():
type="string", help="The status to send to the database") parser.add_option("-m", "--message", type="string", help="The message to send to the database") parser.add_option("-H", "--dbHost", type="string", default="warhol-fred.psc.edu") parser.add_option("-D", "--dbName", type="string", default="test") parser.add_option("-U", "--dbUser", type="string", default="apolloext") parser.add_option("-P", "--pword", type="string") opts, args = parser.parse_args() try: DBHosts = opts.dbHost apolloDB = apollo.ApolloDB(host_=DBHosts, dbname_=opts.dbName, user_=opts.dbUser, password_=opts.pword) apolloDB.connect() # ## Add timestamp to this. d = datetime.datetime.now() thisMessage = "%s: %s" % (d.strftime('%B %d, %Y %I:%M%p'), opts.message) apolloDB.setRunStatus(opts.runId, opts.status, opts.message) except Exception as e: sys.stderr.write(str(e)) sys.exit(1)
def main(): parser = optparse.OptionParser(usage=""" %prog [--help][-k key][-r run_number][-t vis_type] -k or --key Fred Key -r or --run The run number of vizualize ave = produce an average result all = produce an input for all runs -w or --time Turn on profiling -d or --debug Turn on debug printing """) # parser.add_option("-k","--key",type="string", # help="The FRED key for the run you wish to visualize") parser.add_option("-o", "--outputdir", type="string", default="OUT") parser.add_option("-i", "--runId", type="string", help="The Apollo RunId for this call") # parser.add_option("-r","--run",type="string", # help="The number of the run you would like to visualize (number,ave, or all)", # default=1) parser.add_option("-w", "--time", action="store_true", default=False) parser.add_option("-d", "--debug", action="store_true", default=False) parser.add_option("-H", "--dbHost", type="string", default="warhol-fred.psc.edu") parser.add_option("-D", "--dbName", type="string", default="test") parser.add_option("-U", "--dbUser", type="string", default="apolloext") parser.add_option("-P", "--dbPword", type="string") parser.add_option("-n", "--no_db", action="store_true", default=False) opts, args = parser.parse_args() DBHosts = [opts.dbHost] simulationRunId = opts.runId statusFile = "./starttime" if simulationRunId is None: error_exit( "Need to specify an Apollo SimulationRunId to use this program") try: numberDays, outputKeys, averageOuts = FREDAverageOuts(opts.outputdir) #averageOutputs = FREDAverageOuts(opts.outputdir) #sys.exit() # numberDays = int(fred_run.get_param("days")) # outputsAve = fred_run.outputsAve except Exception as e: error_exit(str(e)) try: apolloDBs = [ apollo.ApolloDB(host_=x, dbname_=opts.dbName, user_=opts.dbUser, password_=opts.dbPword) for x in DBHosts ] for apolloDB in apolloDBs: apolloDB.connect() for state, filename in apolloDB.stateToDataFileDict.items(): sio = StringIO() csvsio = StringIO() csvsio.write("BlockGroup,Day,{0}".format(state)) if state in outputKeys: for fips, days in averageOuts.items(): for i in range(len(days)): #print "US{0} {1} {2}:1\n".format(fips,days[i][outputKeys.index(state)],i) sio.write("US{0} {1} {2}:1\n".format( fips, int(round(days[i][outputKeys.index(state)], 0)), i)) m = hashlib.md5() m.update(sio.getvalue()) m5hash = m.hexdigest() runDataContentId = -1 hashvar = apolloDB.checkMD5HashExistence(m5hash) if hashvar > -1: runDataContentId = hashvar else: SQLString = 'INSERT INTO run_data_content (text_content, md5_hash_of_content) values ("%s","%s")' % ( sio.getvalue(), m5hash) if (opts.no_db is False): apolloDB.query(SQLString) runDataContentId = apolloDB.insertID() runDataDescriptionIdTS = apolloDB.getRunDataDescriptionId( label_=filename) SQLString = 'INSERT INTO run_data (run_id, description_id, content_id) values '\ '("%s","%s","%s")' % (simulationRunId, runDataDescriptionIdTS, runDataContentId) if (opts.no_db is False): apolloDB.query(SQLString) if state == "C": runDataDescriptionIdGAIA = apolloDB.getRunDataDescriptionId( label_="newly_exposed.txt", destination_software_=5) SQLString = "INSERT INTO run_data(run_Id,description_id,content_id) values ('%s','%d','%d')" % ( simulationRunId, runDataDescriptionIdGAIA, runDataContentId) SQLString = "INSERT INTO run_data(run_Id,description_id,content_id) values ('%s','%d','%d')" % ( simulationRunId, runDataDescriptionIdGAIA, runDataContentId) if (opts.no_db is False): apolloDB.query(SQLString) with open(filename, "wb") as f: f.write('{0}'.format(sio.getvalue())) apolloDB.close() statusUpdate( "LOG_FILES_WRITTEN", "%s" % datetime.datetime.now().strftime("%a %b %d %H:%M:%S EDT %Y")) except Exception as e: error_exit(str(e))