def makeObservingPlots(nslots, simNumber, best_slot, data_dir, 
        mapDirectory, camera, allSky = False) :
    print "================ >>>>>>>>>>>>>>>>>>>>> =================== "
    print "makeObservingPlots(",nslots, simNumber, best_slot,data_dir," )"
    print "================ >>>>>>>>>>>>>>>>>>>>> =================== "
    import matplotlib
    matplotlib.use("Agg"); # matplotlib.use("TkAgg") important for off line image generation
    import matplotlib.pyplot as plt
    figure = plt.figure(1,figsize=(8.5*1.618,8.5))

    # if the number of slots is zero, nothing to observe or plot
    if nslots == 0 : return 0

    # first, make the probability versus something plot
    ra,dec,id,prob,slotMjd,slotNumbers = obsSlots.readObservingRecord(
        simNumber, mapDirectory)

    probabilityPlot(figure, prob, slotNumbers, simNumber, data_dir) 

    # now make the hex observation plots
    counter = 1   ;# already made one
    for i in np.unique(slotNumbers) :
        obsTime = ""
        i = np.int(i)
        ix = slotNumbers == i
        if np.any(ix) : 
            ix = np.nonzero(ix)
            if np.nonzero(ix)[0].size > 1 :
                obsTime = slotMjd[ix[0]].mean()
            else :
                obsTime = slotMjd
            #print "\t making observingPlot-{}.png".format(i)
            observingPlot(figure,simNumber,i,mapDirectory, nslots, camera, extraTitle=obsTime, allSky=allSky)
            name = str(simNumber)+"-observingPlot-{}.png".format(i)
            plt.savefig(os.path.join(mapDirectory,name))
            counter += 1
            counter+= equalAreaPlot(figure,i,simNumber,data_dir,mapDirectory)

    #counter+= equalAreaPlot(figure,best_slot,simNumber,data_dir,mapDirectory)

    # return the number of plots made
    return counter
def nothingToObserveShowSomething(simNumber, data_dir, mapDir) :
    import matplotlib.pyplot as plt
    figure = plt.figure(1,figsize=(8.5*1.618,8.5))
    ra,dec,id,prob,mjd,slotNum=obsSlots.readObservingRecord(simNumber, data_dir)
    ix = np.argmax(prob)
    try:
        slot = np.int(slotNum[ix])
    except:
        print "ra",ra
        print "dec",dec
        print "id",id
        print "prob",prob
        print "mjd",mjd
        print "slotNum", slotNum
        print "ix",ix
        print "ix index",np.nonzero(ix)
        raise Exception("this failed once, but ran fine when I did it in the directory. NSF thing?")
    title = "slot {} hex maxProb {:.6f}, nothing to observe".format(slot, prob[ix])
    counter = equalAreaPlot(figure,slot,simNumber,data_dir,mapDir,title) 
    return counter
def observingPlot(figure, simNumber, slot, data_dir, nslots, camera, extraTitle="", allSky=False) :
    import plotMapAndHex

    # get the planned observations
    ra,dec,id,prob,mjd,slotNumbers = obsSlots.readObservingRecord(simNumber, data_dir)
    ix = slotNumbers == slot
    the_mjd = mjd[ix][0]
    
    #title = "i-band limiting magnitude"
    title = "" ;# as the color bar is labeled
    if extraTitle != "" :
        #extraTitle = " mjd {:.2f}: ".format(extraTitle)
        extraTitle = " Slot {}    {} ".format(slot, utcFromMjd(the_mjd))
        title = extraTitle+title
    #title = title + "      LIGO countours at max/[1.1, 3, 10, 30]"
    title = title + "      {}".format(simNumber)


    print "making plotMapAndHex.mapAndHex(figure, ", simNumber, ",", slot, ",", data_dir, ",", nslots, ",ra,dec,", title,") "
    d=plotMapAndHex.mapAndHex(figure, simNumber, slot, data_dir, nslots, ra, dec, camera, title, slots=slotNumbers, allSky=allSky) 
    return d
Exemple #4
0
    def mapMaker(self, trigger_id, skymap, config):
        import os
        import yaml
        import getHexObservations


        overhead = config["overhead"]
        #nvisits = config["nvisits"]
        area_per_hex = config["area_per_hex"]
        start_of_season = config["start_of_season"]
        end_of_season = config["end_of_season"]
        events_observed = config["events_observed"]
        skipAll = config["skipAll"]
        mjd = self.mjd
        #print mjd, self.event_params['MJD']
        # if self.event_params['MJD'] == 'NAN':
        #     self.event_params['MJD'] = str(mjd)
        #raw_input()
        outputDir = self.outfolder
        mapDir = self.mapspath
        recycler_mjd = self.recycler_mjd

        start_days_since_burst = self.recycler_mjd - self.mjd
        start_days_since_burst = 1.


        #recycler_mjd = 57773

        if self.skymap is None:
            self.skymap = os.path.join(outputDir,'lalinference.fits.gz')

        # If distance is not set in config use xml distance
        # if config["force_distance"]:
        #     distance = config["distance"]
        # else:
        #     if self.weHaveParamFile:
        #         distance = self.event_params["MaxDistance"]
        #     else:
        #         print 'THERE IS NO PARAMFILE, HARDCODING THE DISTANCE TO THE CONFIG DIST.'
        #         distance = config["distance"]

        eventtype = self.event_params['boc']
        try:
            probhasns = self.event_params['probhasns']
        except:
            probhasns = 0. #for old maps...
        gethexobstype = None


        if eventtype == 'Burst':
            gethexobstype = 'BH'
            self.distance = 1.
        elif eventtype == 'CBC':
            if probhasns > config['probHasNS_threshold']:
                gethexobstype = 'NS'
                self.distance = -999
            else:
                gethexobstype = 'BH'
                self.distance = 1.
        else: #we dont know what we're looking at... do default obs for lightcurve
            print 'WE DONT KNOW WHAT WERE LOOKING AT!'*5
            gethexobstype = 'BH'
            self.distance = 1.



        if gethexobstype == 'BH':
            filter_list = config["exposure_filter_BH"]
            maxHexesPerSlot = config["maxHexesPerSlot_BH"]
            exposure_length = config["exposure_length_BH"]
            hoursAvailable = config["time_budget_for_BH"]

        else:
            filter_list = config["exposure_filter_NS"]
            maxHexesPerSlot = config["maxHexesPerSlot_NS"]
            exposure_length = config["exposure_length_NS"]
            hoursAvailable = config["time_budget_for_NS"]

        exposure_length = np.array(exposure_length)
        self.exposure_length = exposure_length
        self.time_budget = hoursAvailable

        if config["force_distance"]:
            self.distance = config["distance"]

        #self.distance = distance

        if not os.path.exists(outputDir):
            os.makedirs(outputDir)


        self.gethexobstype = gethexobstype
        # make the maps
        #try:
        #where = 'getHexObservations'
        #line = '103'
            #try:
            #    probs, times, slotDuration, hoursPerNight = getHexObservations.prepare(
            #        skymap, mjd, trigger_id, outputDir, mapDir, distance=distance,
            #        exposure_list=exposure_length, filter_list=filter_list,
            #        overhead=overhead, maxHexesPerSlot=maxHexesPerSlot, skipAll=skipAll)
            #except ValueError:

        # print 'skymap',self.skymap
        #
        #
        # print 'skymap',self.skymap
        # print 'distance',self.distance
        # print 'gethexobstype',gethexobstype
        # print 'start_days_since_burst',start_days_since_burst
        # print 'exposure_length',exposure_length
        # print 'filter_list',filter_list
        # print 'resolution',config['resolution']
        # print 'halfnight',config['ishalfnight']
        # print 'firsthalf', config['isfirsthalf']
        # print 'overhead',overhead
        # print 'maxHexesPerSlot',maxHexesPerSlot
        # print 'skipAll',skipAll

        #raw_input()
        probs, times, slotDuration, hoursPerNight = getHexObservations.prepare(
                    self.skymap, trigger_id, outputDir, mapDir, distance=self.distance,
                    trigger_type=gethexobstype,start_days_since_burst=start_days_since_burst,
                    exposure_list=exposure_length, filter_list=filter_list,resolution=config['resolution'],
                    halfNight=config['ishalfnight'], firstHalf=config['isfirsthalf'],
                    overhead=overhead, maxHexesPerSlot=maxHexesPerSlot, skipAll=skipAll)
            # figure out how to divide the night
            # where = 'getHexObservations.contemplateTheDivisionsOfTime()'
            # line = '102'

        # print 'probs',probs
        # print 'times', times
        # print 'slotDuration', slotDuration
        # print 'hoursPerNight', hoursPerNight
        #raw_input()
        n_slots, first_slot = getHexObservations.contemplateTheDivisionsOfTime(
                probs, times, hoursPerNight=hoursPerNight,
                hoursAvailable=hoursAvailable)

            # compute the best observations
            # where = 'getHexObservations.now()'
            # line = '109'
        best_slot = getHexObservations.now(
                n_slots, mapDirectory=mapDir, simNumber=trigger_id,
                maxHexesPerSlot=maxHexesPerSlot, mapZero=first_slot,
                exposure_list=exposure_length, filter_list=filter_list,
                trigger_type=gethexobstype, skipJson=config['skipjson'])
        # except:
        #     try:
        #         print 'skymap', self.skymap
        #         self.skymap = os.path.join(outputDir,'bayestar.fits.gz')
        #
        #         probs, times, slotDuration, hoursPerNight = getHexObservations.prepare(
        #             self.skymap, mjd, trigger_id, outputDir, mapDir, distance=distance,
        #             exposure_list=exposure_length, filter_list=filter_list,
        #             overhead=overhead, maxHexesPerSlot=maxHexesPerSlot, skipAll=skipAll)
        #         # figure out how to divide the night
        #         # where = 'getHexObservations.contemplateTheDivisionsOfTime()'
        #         # line = '102'
        #         n_slots, first_slot = getHexObservations.contemplateTheDivisionsOfTime(
        #             probs, times, hoursPerNight=hoursPerNight,
        #             hoursAvailable=hoursAvailable)
        #
        #         # compute the best observations
        #         # where = 'getHexObservations.now()'
        #         # line = '109'
        #         best_slot = getHexObservations.now(
        #             n_slots, mapDirectory=mapDir, simNumber=trigger_id,
        #             maxHexesPerSlot=maxHexesPerSlot, mapZero=first_slot,
        #             exposure_list=exposure_length, filter_list=filter_list,
        #             skipJson=True)
        #     except:
        #         try:
        #             print 'skymap', self.skymap
        #             self.skymap = os.path.join(outputDir, 'lalinference.fits.gz')
        #
        #             probs, times, slotDuration, hoursPerNight = getHexObservations.prepare(
        #                 self.skymap, mjd, trigger_id, outputDir, mapDir, distance=distance,
        #                 exposure_list=exposure_length, filter_list=filter_list,
        #                 overhead=overhead, maxHexesPerSlot=maxHexesPerSlot, skipAll=skipAll)
        #             # figure out how to divide the night
        #             # where = 'getHexObservations.contemplateTheDivisionsOfTime()'
        #             # line = '102'
        #             n_slots, first_slot = getHexObservations.contemplateTheDivisionsOfTime(
        #                 probs, times, hoursPerNight=hoursPerNight,
        #                 hoursAvailable=hoursAvailable)
        #
        #             # compute the best observations
        #             # where = 'getHexObservations.now()'
        #             # line = '109'
        #             best_slot = getHexObservations.now(
        #                 n_slots, mapDirectory=mapDir, simNumber=trigger_id,
        #                 maxHexesPerSlot=maxHexesPerSlot, mapZero=first_slot,
        #                 exposure_list=exposure_length, filter_list=filter_list,
        #                 skipJson=True)
        #         except:
        #             e = sys.exc_info()
        #             trace = traceback.format_exc(sys.exc_info())
        #             print trace
        #             self.send_processing_error(e, where, line, trace)
        #             sys.exit()

        skipecon = True


        if not skipecon:
            if n_slots > 0:
                print "================ N_SLOTS > 0 =================== "
                #   area_left is th enumber of hexes we have left to observe this season
                #   T_left is the number of days left in the season
                #   rate is the effective rate of triggers
                #
                # in seconds
                time_cost_per_hex = nvisits * np.sum(overhead + exposure_length)
                area_left = area_per_hex * \
                            (hoursAvailable * 3600) / (time_cost_per_hex)
                time_left = end_of_season - start_of_season
                rate = len(events_observed) / (recycler_mjd - start_of_season)

                # do Hsun-yu Chen's
                try:
                    where = 'getHexObservations.economics()'
                    line = '136'
                    econ_prob, econ_area, need_area, quality = \
                        getHexObservations.economics(trigger_id,
                                                     best_slot, mapDirectory=mapDir,
                                                     area_left=area_left, days_left=time_left,
                                                     rate=rate)

                    hoursOnTarget = (econ_area / area_per_hex) * (time_cost_per_hex / 3600.)

                    # figure out how to divide the night,
                    # given the new advice on how much time to spend
                    where = 'getHexObservations.contemplateTheDivisionsOfTime()'
                    line = '148'
                    n_slots, first_slot = \
                        getHexObservations.contemplateTheDivisionsOfTime(
                            probs, times, hoursPerNight=hoursPerNight,
                            hoursAvailable=hoursOnTarget)

                    where = 'getHexObservations.now()'
                    line = '156'
                    best_slot = getHexObservations.now(
                        n_slots, mapDirectory=mapDir, simNumber=trigger_id,
                        maxHexesPerSlot=maxHexesPerSlot, mapZero=first_slot,
                        exposure_list=exposure_length, filter_list=filter_list,
                        skipJson=False)
                except:
                    e = sys.exc_info()
                    trace = traceback.format_exc(sys.exc_info())
                    print trace
                    self.send_processing_error(e, where, line, trace)
                    sys.exit()
        else:
            econ_prob = 0
            econ_area = 0
            best_slot = 0
            need_area = 11734.0
            quality = 1.0

        # make observation plots
        # try:
        #     where = 'getHexObservations.makeObservingPlots()'
        #     line = '176'
        #     print '888' * 20
        #     print n_slots, trigger_id, best_slot, outputDir, mapDir
        #     print '888' * 20
        #     if not config['skipPlots']:
        #         n_plots = getHexObservations.makeObservingPlots(
        #             n_slots, trigger_id, best_slot, outputDir, mapDir, allSky=True )
        #     #string = "$(ls -v {}-observingPlot*)"
        # except:
        #     e = sys.exc_info()
        #     trace = traceback.format_exc(sys.exc_info())
        #     print trace
        #     self.send_processing_error(e, where, line, trace)
        #     sys.exit()


        try:
            where = 'getHexObservations.how_well_did_we_do()'
            line = '306'
            self.sumligoprob = getHexObservations.how_well_did_we_do(
                self.skymap, trigger_id, mapDir)
        except:
            e = sys.exc_info()
            trace = traceback.format_exc(sys.exc_info())
            print trace
            self.send_processing_error(e, where, line, trace)
            sys.exit()

        self.best_slot = best_slot
        self.n_slots = n_slots
        self.first_slot = first_slot
        self.econ_prob = econ_prob
        self.econ_area = econ_area
        self.need_area = need_area
        self.quality = quality


        np.savez(os.path.join(self.outfolder, 'mapmaker_results.npz')
                 , best_slot=best_slot
                 , n_slots=n_slots
                 , first_slot=first_slot
                 , econ_prob=econ_prob
                 , econ_area=econ_area
                 , need_area=need_area
                 , quality=quality
                 )

        ra, dec, id, self.prob, mjd, slotNum = \
            obsSlots.readObservingRecord(self.trigger_id, mapDir)

        integrated_prob = np.sum(self.prob)
        print '-'*20+'>','LIGO PROB: %.3f \tLIGO X DES PROB: %.3f' % (self.sumligoprob,integrated_prob)
        #raw_input('checking comparison of probs!!!!'*10)


        self.outputDir = outputDir
        self.mapDir = mapDir

        self.weHaveParamFile = True

        if self.weHaveParamFile:
            np.savez(self.event_paramfile,
                     MJD=self.mjd,
                     ETA=self.event_params['ETA'],
                     FAR=self.event_params['FAR'],
                     ChirpMass=self.event_params['ChirpMass'],
                     MaxDistance=self.event_params['MaxDistance'],
                     DESXLIGO_prob=integrated_prob,
                     LIGO_prob=self.sumligoprob,
                     M1=self.event_params['M1'],
                     M2=self.event_params['M2'],
                     nHexes=self.prob.size,
                     time_processed=self.now.strftime("%H:%M %B %d, %Y "),
                     boc=self.event_params['boc'],
                     CentralFreq=self.event_params['CentralFreq'],
                     best_slot=self.best_slot,
                     n_slots=self.n_slots,
                     first_slot=self.first_slot,
                     econ_prob=self.econ_prob,
                     econ_area=self.econ_area,
                     need_area=self.need_area,
                     quality=self.quality,
                     codeDistance=self.distance,
                     exposure_times=exposure_length,
                     exposure_filter=filter_list,
                     hours=self.time_budget,
                     nvisits=-999,#config['nvisits'],
                     mapname='NAN',
                     filename=self.skymap,
                     gethexobstype=self.gethexobstype
                     )
        else:
            np.savez(self.event_paramfile,
                     MJD='NAN',
                     ETA='NAN',
                     FAR='NAN',
                     ChirpMass='NAN',
                     MaxDistance='NAN',
                     DESXLIGO_prob=integrated_prob,
                     LIGO_prob=self.sumligoprob,
                     M1='NAN',
                     M2='NAN',
                     nHexes=self.prob.size,
                     time_processed=self.now.strftime("%H:%M %B %d, %Y "),
                     boc='NAN',
                     CentralFreq='NAN',
                     best_slot=self.best_slot,
                     n_slots=self.n_slots,
                     first_slot=self.first_slot,
                     econ_prob=self.econ_prob,
                     econ_area=self.econ_area,
                     need_area=self.need_area,
                     quality=self.quality,
                     codeDistance=self.distance,
                     exposure_times=exposure_length,
                     exposure_filter=filter_list,
                     hours=self.time_budget,
                     nvisits=-999,#config['nvisits'],
                     mapname='NAN',
                     filename=self.skymap,
                     gethexobstype=self.gethexobstype
                     )
Exemple #5
0
    def makeJSON(self, config):

        mapmakerresults = np.load(os.path.join(self.outfolder, 'mapmaker_results.npz'))

        self.best_slot = mapmakerresults['best_slot']
        self.n_slots = mapmakerresults['n_slots']
        self.first_slot = mapmakerresults['first_slot']
        self.econ_prob = mapmakerresults['econ_prob']
        self.econ_area = mapmakerresults['econ_area']
        self.need_area = mapmakerresults['need_area']
        self.quality = mapmakerresults['quality']

        # DESGW json file (to be files once that is done)
        json_dir = self.website_jsonpath
        map_dir = self.mapspath
        jsonname = self.trigger_id + "_JSON.zip"
        jsonFile = os.path.join(map_dir, jsonname)
        jsonfilelistld = os.listdir(map_dir)
        jsonfilelist = []
        for f in jsonfilelistld:
            if '-tmp' in f:
                os.remove(os.path.join(map_dir, f))
            elif '.json' in f:
                jsonfilelist.append(f)

        if self.n_slots > 0:
            # get statistics
            ra, dec, id, self.prob, mjd, slotNum = \
                obsSlots.readObservingRecord(self.trigger_id, map_dir)

            # adding integrated probability to paramfile
            integrated_prob = np.sum(self.prob)
            nHexes = str(self.prob.size)
        else:
            integrated_prob = 0
            nHexes = str(0)

        from time import gmtime, strftime
        timeprocessed = strftime("%H:%M:%S GMT \t %b %d, %Y", gmtime())

        #exptimes = ', '.join(map(str, config['exposure_length']))
        #expf = ', '.join(map(str, config['exposure_filter']))

        try:
            boc = self.event_params['boc']
        except:
            boc = 'NA'

        # Copy json file to web server for public download
        if not os.path.exists(jsonFile):
            if integrated_prob == 0:
                print "zero probability, thus no jsonFile at ", jsonFile
            else:
                # try:
                os.chmod(self.mapspath, 0o777)
                for js in os.listdir(self.mapspath):
                    os.chmod(os.path.join(self.mapspath,js), 0o777)

                os.system('zip -j ' + jsonFile + ' ' + self.mapspath + '/*0.json')
                # except:
                #    print "no jsonFiles at ", jsonFile
        else:
            os.remove(jsonFile)
            os.system('zip -j ' + jsonFile + ' ' + self.mapspath + '/*0.json')
            os.system('cp ' + jsonFile + ' ' + self.website_jsonpath)
        return jsonfilelist
Exemple #6
0
    def mapMaker(self, trigger_id, skymap, config):
        import os
        import yaml
        import getHexObservations

        overhead = config["overhead"]
        #nvisits = config["nvisits"]
        area_per_hex = config["area_per_hex"]
        start_of_season = config["start_of_season"]
        end_of_season = config["end_of_season"]
        events_observed = config["events_observed"]
        skipAll = config["skipAll"]
        mjd = self.mjd
        #print mjd, self.event_params['MJD']
        # if self.event_params['MJD'] == 'NAN':
        #     self.event_params['MJD'] = str(mjd)
        #raw_input()
        outputDir = self.outfolder
        mapDir = self.mapspath
        recycler_mjd = self.recycler_mjd

        start_days_since_burst = self.recycler_mjd - self.mjd
        start_days_since_burst = 1.

        #recycler_mjd = 57773

        if self.skymap is None:
            self.skymap = os.path.join(outputDir, 'lalinference.fits.gz')

        # If distance is not set in config use xml distance
        # if config["force_distance"]:
        #     distance = config["distance"]
        # else:
        #     if self.weHaveParamFile:
        #         distance = self.event_params["MaxDistance"]
        #     else:
        #         print 'THERE IS NO PARAMFILE, HARDCODING THE DISTANCE TO THE CONFIG DIST.'
        #         distance = config["distance"]

        eventtype = self.event_params['boc']
        try:
            probhasns = self.event_params['probhasns']
        except:
            probhasns = 0.  #for old maps...
        gethexobstype = None

        if eventtype == 'Burst':
            gethexobstype = 'BH'
            self.distance = 1.
        elif eventtype == 'CBC':
            if probhasns > config['probHasNS_threshold']:
                gethexobstype = 'NS'
                self.distance = -999
            else:
                gethexobstype = 'BH'
                self.distance = 1.
        else:  #we dont know what we're looking at... do default obs for lightcurve
            print 'WE DONT KNOW WHAT WERE LOOKING AT!' * 5
            gethexobstype = 'BH'
            self.distance = 1.

        if gethexobstype == 'BH':
            filter_list = config["exposure_filter_BH"]
            maxHexesPerSlot = config["maxHexesPerSlot_BH"]
            exposure_length = config["exposure_length_BH"]
            hoursAvailable = config["time_budget_for_BH"]

        else:
            filter_list = config["exposure_filter_NS"]
            maxHexesPerSlot = config["maxHexesPerSlot_NS"]
            exposure_length = config["exposure_length_NS"]
            hoursAvailable = config["time_budget_for_NS"]

        exposure_length = np.array(exposure_length)
        self.exposure_length = exposure_length
        self.time_budget = hoursAvailable

        if config["force_distance"]:
            self.distance = config["distance"]

        #self.distance = distance

        if not os.path.exists(outputDir):
            os.makedirs(outputDir)

        self.gethexobstype = gethexobstype
        # make the maps
        #try:
        #where = 'getHexObservations'
        #line = '103'
        #try:
        #    probs, times, slotDuration, hoursPerNight = getHexObservations.prepare(
        #        skymap, mjd, trigger_id, outputDir, mapDir, distance=distance,
        #        exposure_list=exposure_length, filter_list=filter_list,
        #        overhead=overhead, maxHexesPerSlot=maxHexesPerSlot, skipAll=skipAll)
        #except ValueError:

        # print 'skymap',self.skymap
        #
        #
        # print 'skymap',self.skymap
        # print 'distance',self.distance
        # print 'gethexobstype',gethexobstype
        # print 'start_days_since_burst',start_days_since_burst
        # print 'exposure_length',exposure_length
        # print 'filter_list',filter_list
        # print 'resolution',config['resolution']
        # print 'halfnight',config['ishalfnight']
        # print 'firsthalf', config['isfirsthalf']
        # print 'overhead',overhead
        # print 'maxHexesPerSlot',maxHexesPerSlot
        # print 'skipAll',skipAll

        #raw_input()
        probs, times, slotDuration, hoursPerNight = getHexObservations.prepare(
            self.skymap,
            trigger_id,
            outputDir,
            mapDir,
            distance=self.distance,
            trigger_type=gethexobstype,
            start_days_since_burst=start_days_since_burst,
            exposure_list=exposure_length,
            filter_list=filter_list,
            resolution=config['resolution'],
            halfNight=config['ishalfnight'],
            firstHalf=config['isfirsthalf'],
            overhead=overhead,
            maxHexesPerSlot=maxHexesPerSlot,
            skipAll=skipAll)
        # figure out how to divide the night
        # where = 'getHexObservations.contemplateTheDivisionsOfTime()'
        # line = '102'

        # print 'probs',probs
        # print 'times', times
        # print 'slotDuration', slotDuration
        # print 'hoursPerNight', hoursPerNight
        #raw_input()
        n_slots, first_slot = getHexObservations.contemplateTheDivisionsOfTime(
            probs,
            times,
            hoursPerNight=hoursPerNight,
            hoursAvailable=hoursAvailable)

        # compute the best observations
        # where = 'getHexObservations.now()'
        # line = '109'
        best_slot = getHexObservations.now(n_slots,
                                           mapDirectory=mapDir,
                                           simNumber=trigger_id,
                                           maxHexesPerSlot=maxHexesPerSlot,
                                           mapZero=first_slot,
                                           exposure_list=exposure_length,
                                           filter_list=filter_list,
                                           trigger_type=gethexobstype,
                                           skipJson=config['skipjson'])
        # except:
        #     try:
        #         print 'skymap', self.skymap
        #         self.skymap = os.path.join(outputDir,'bayestar.fits.gz')
        #
        #         probs, times, slotDuration, hoursPerNight = getHexObservations.prepare(
        #             self.skymap, mjd, trigger_id, outputDir, mapDir, distance=distance,
        #             exposure_list=exposure_length, filter_list=filter_list,
        #             overhead=overhead, maxHexesPerSlot=maxHexesPerSlot, skipAll=skipAll)
        #         # figure out how to divide the night
        #         # where = 'getHexObservations.contemplateTheDivisionsOfTime()'
        #         # line = '102'
        #         n_slots, first_slot = getHexObservations.contemplateTheDivisionsOfTime(
        #             probs, times, hoursPerNight=hoursPerNight,
        #             hoursAvailable=hoursAvailable)
        #
        #         # compute the best observations
        #         # where = 'getHexObservations.now()'
        #         # line = '109'
        #         best_slot = getHexObservations.now(
        #             n_slots, mapDirectory=mapDir, simNumber=trigger_id,
        #             maxHexesPerSlot=maxHexesPerSlot, mapZero=first_slot,
        #             exposure_list=exposure_length, filter_list=filter_list,
        #             skipJson=True)
        #     except:
        #         try:
        #             print 'skymap', self.skymap
        #             self.skymap = os.path.join(outputDir, 'lalinference.fits.gz')
        #
        #             probs, times, slotDuration, hoursPerNight = getHexObservations.prepare(
        #                 self.skymap, mjd, trigger_id, outputDir, mapDir, distance=distance,
        #                 exposure_list=exposure_length, filter_list=filter_list,
        #                 overhead=overhead, maxHexesPerSlot=maxHexesPerSlot, skipAll=skipAll)
        #             # figure out how to divide the night
        #             # where = 'getHexObservations.contemplateTheDivisionsOfTime()'
        #             # line = '102'
        #             n_slots, first_slot = getHexObservations.contemplateTheDivisionsOfTime(
        #                 probs, times, hoursPerNight=hoursPerNight,
        #                 hoursAvailable=hoursAvailable)
        #
        #             # compute the best observations
        #             # where = 'getHexObservations.now()'
        #             # line = '109'
        #             best_slot = getHexObservations.now(
        #                 n_slots, mapDirectory=mapDir, simNumber=trigger_id,
        #                 maxHexesPerSlot=maxHexesPerSlot, mapZero=first_slot,
        #                 exposure_list=exposure_length, filter_list=filter_list,
        #                 skipJson=True)
        #         except:
        #             e = sys.exc_info()
        #             trace = traceback.format_exc(sys.exc_info())
        #             print trace
        #             self.send_processing_error(e, where, line, trace)
        #             sys.exit()

        skipecon = True

        if not skipecon:
            if n_slots > 0:
                print "================ N_SLOTS > 0 =================== "
                #   area_left is th enumber of hexes we have left to observe this season
                #   T_left is the number of days left in the season
                #   rate is the effective rate of triggers
                #
                # in seconds
                time_cost_per_hex = nvisits * np.sum(overhead +
                                                     exposure_length)
                area_left = area_per_hex * \
                            (hoursAvailable * 3600) / (time_cost_per_hex)
                time_left = end_of_season - start_of_season
                rate = len(events_observed) / (recycler_mjd - start_of_season)

                # do Hsun-yu Chen's
                try:
                    where = 'getHexObservations.economics()'
                    line = '136'
                    econ_prob, econ_area, need_area, quality = \
                        getHexObservations.economics(trigger_id,
                                                     best_slot, mapDirectory=mapDir,
                                                     area_left=area_left, days_left=time_left,
                                                     rate=rate)

                    hoursOnTarget = (econ_area / area_per_hex) * (
                        time_cost_per_hex / 3600.)

                    # figure out how to divide the night,
                    # given the new advice on how much time to spend
                    where = 'getHexObservations.contemplateTheDivisionsOfTime()'
                    line = '148'
                    n_slots, first_slot = \
                        getHexObservations.contemplateTheDivisionsOfTime(
                            probs, times, hoursPerNight=hoursPerNight,
                            hoursAvailable=hoursOnTarget)

                    where = 'getHexObservations.now()'
                    line = '156'
                    best_slot = getHexObservations.now(
                        n_slots,
                        mapDirectory=mapDir,
                        simNumber=trigger_id,
                        maxHexesPerSlot=maxHexesPerSlot,
                        mapZero=first_slot,
                        exposure_list=exposure_length,
                        filter_list=filter_list,
                        skipJson=False)
                except:
                    e = sys.exc_info()
                    trace = traceback.format_exc(sys.exc_info())
                    print trace
                    self.send_processing_error(e, where, line, trace)
                    sys.exit()
        else:
            econ_prob = 0
            econ_area = 0
            best_slot = 0
            need_area = 11734.0
            quality = 1.0

        # make observation plots
        # try:
        #     where = 'getHexObservations.makeObservingPlots()'
        #     line = '176'
        #     print '888' * 20
        #     print n_slots, trigger_id, best_slot, outputDir, mapDir
        #     print '888' * 20
        #     if not config['skipPlots']:
        #         n_plots = getHexObservations.makeObservingPlots(
        #             n_slots, trigger_id, best_slot, outputDir, mapDir, allSky=True )
        #     #string = "$(ls -v {}-observingPlot*)"
        # except:
        #     e = sys.exc_info()
        #     trace = traceback.format_exc(sys.exc_info())
        #     print trace
        #     self.send_processing_error(e, where, line, trace)
        #     sys.exit()

        try:
            where = 'getHexObservations.how_well_did_we_do()'
            line = '306'
            self.sumligoprob = getHexObservations.how_well_did_we_do(
                self.skymap, trigger_id, mapDir)
        except:
            e = sys.exc_info()
            trace = traceback.format_exc(sys.exc_info())
            print trace
            self.send_processing_error(e, where, line, trace)
            sys.exit()

        self.best_slot = best_slot
        self.n_slots = n_slots
        self.first_slot = first_slot
        self.econ_prob = econ_prob
        self.econ_area = econ_area
        self.need_area = need_area
        self.quality = quality

        np.savez(os.path.join(self.outfolder, 'mapmaker_results.npz'),
                 best_slot=best_slot,
                 n_slots=n_slots,
                 first_slot=first_slot,
                 econ_prob=econ_prob,
                 econ_area=econ_area,
                 need_area=need_area,
                 quality=quality)

        ra, dec, id, self.prob, mjd, slotNum = \
            obsSlots.readObservingRecord(self.trigger_id, mapDir)

        integrated_prob = np.sum(self.prob)
        print '-' * 20 + '>', 'LIGO PROB: %.3f \tLIGO X DES PROB: %.3f' % (
            self.sumligoprob, integrated_prob)
        #raw_input('checking comparison of probs!!!!'*10)

        self.outputDir = outputDir
        self.mapDir = mapDir

        self.weHaveParamFile = True

        if self.weHaveParamFile:
            np.savez(
                self.event_paramfile,
                MJD=self.mjd,
                ETA=self.event_params['ETA'],
                FAR=self.event_params['FAR'],
                ChirpMass=self.event_params['ChirpMass'],
                MaxDistance=self.event_params['MaxDistance'],
                DESXLIGO_prob=integrated_prob,
                LIGO_prob=self.sumligoprob,
                M1=self.event_params['M1'],
                M2=self.event_params['M2'],
                nHexes=self.prob.size,
                time_processed=self.now.strftime("%H:%M %B %d, %Y "),
                boc=self.event_params['boc'],
                CentralFreq=self.event_params['CentralFreq'],
                best_slot=self.best_slot,
                n_slots=self.n_slots,
                first_slot=self.first_slot,
                econ_prob=self.econ_prob,
                econ_area=self.econ_area,
                need_area=self.need_area,
                quality=self.quality,
                codeDistance=self.distance,
                exposure_times=exposure_length,
                exposure_filter=filter_list,
                hours=self.time_budget,
                nvisits=-999,  #config['nvisits'],
                mapname='NAN',
                filename=self.skymap,
                gethexobstype=self.gethexobstype)
        else:
            np.savez(
                self.event_paramfile,
                MJD='NAN',
                ETA='NAN',
                FAR='NAN',
                ChirpMass='NAN',
                MaxDistance='NAN',
                DESXLIGO_prob=integrated_prob,
                LIGO_prob=self.sumligoprob,
                M1='NAN',
                M2='NAN',
                nHexes=self.prob.size,
                time_processed=self.now.strftime("%H:%M %B %d, %Y "),
                boc='NAN',
                CentralFreq='NAN',
                best_slot=self.best_slot,
                n_slots=self.n_slots,
                first_slot=self.first_slot,
                econ_prob=self.econ_prob,
                econ_area=self.econ_area,
                need_area=self.need_area,
                quality=self.quality,
                codeDistance=self.distance,
                exposure_times=exposure_length,
                exposure_filter=filter_list,
                hours=self.time_budget,
                nvisits=-999,  #config['nvisits'],
                mapname='NAN',
                filename=self.skymap,
                gethexobstype=self.gethexobstype)
Exemple #7
0
    def makeJSON(self, config):

        mapmakerresults = np.load(
            os.path.join(self.outfolder, 'mapmaker_results.npz'))

        self.best_slot = mapmakerresults['best_slot']
        self.n_slots = mapmakerresults['n_slots']
        self.first_slot = mapmakerresults['first_slot']
        self.econ_prob = mapmakerresults['econ_prob']
        self.econ_area = mapmakerresults['econ_area']
        self.need_area = mapmakerresults['need_area']
        self.quality = mapmakerresults['quality']

        # DESGW json file (to be files once that is done)
        json_dir = self.website_jsonpath
        map_dir = self.mapspath
        jsonname = self.trigger_id + "_JSON.zip"
        jsonFile = os.path.join(map_dir, jsonname)
        jsonfilelistld = os.listdir(map_dir)
        jsonfilelist = []
        for f in jsonfilelistld:
            if '-tmp' in f:
                os.remove(os.path.join(map_dir, f))
            elif '.json' in f:
                jsonfilelist.append(f)

        if self.n_slots > 0:
            # get statistics
            ra, dec, id, self.prob, mjd, slotNum = \
                obsSlots.readObservingRecord(self.trigger_id, map_dir)

            # adding integrated probability to paramfile
            integrated_prob = np.sum(self.prob)
            nHexes = str(self.prob.size)
        else:
            integrated_prob = 0
            nHexes = str(0)

        from time import gmtime, strftime
        timeprocessed = strftime("%H:%M:%S GMT \t %b %d, %Y", gmtime())

        #exptimes = ', '.join(map(str, config['exposure_length']))
        #expf = ', '.join(map(str, config['exposure_filter']))

        try:
            boc = self.event_params['boc']
        except:
            boc = 'NA'

        # Copy json file to web server for public download
        if not os.path.exists(jsonFile):
            if integrated_prob == 0:
                print "zero probability, thus no jsonFile at ", jsonFile
            else:
                # try:
                os.chmod(self.mapspath, 0o777)
                for js in os.listdir(self.mapspath):
                    os.chmod(os.path.join(self.mapspath, js), 0o777)

                os.system('zip -j ' + jsonFile + ' ' + self.mapspath +
                          '/*0.json')
                # except:
                #    print "no jsonFiles at ", jsonFile
        else:
            os.remove(jsonFile)
            os.system('zip -j ' + jsonFile + ' ' + self.mapspath + '/*0.json')
            os.system('cp ' + jsonFile + ' ' + self.website_jsonpath)
        return jsonfilelist