Beispiel #1
0
def downloadFile(remotepathname, localpathname):
    try:
        urllib.urlretrieve("%s/%s" % (remoteUrl, remotepathname),
                           localpathname)
    except socket.timeout:
        raise GtBurstException(
            11,
            "Time out. Check your internet connection, and that you can access %s, then retry"
            % (remoteUrl))
    except:
        raise GtBurstException(
            1,
            "Problems with the download. Check your connection, and that you can reach %s, then retry"
            % (remoteUrl))
    pass
Beispiel #2
0
 def computeCovarianceMatrix(self, statisticGradient):
     self.covMatrix = computeCovarianceMatrix(statisticGradient,
                                              self.params)
     #Check that the covariance matrix is positive-defined
     negativeElements = (numpy.matrix.diagonal(self.covMatrix) < 0)
     if (len(negativeElements.nonzero()[0]) > 0):
         raise GtBurstException(
             8,
             "The background fit has failed. Select one interval before the transient and one after, as close as possible "
             +
             "to respectively the beginning and the end of the transient, and few hundreds seconds in duration."
         )
Beispiel #3
0
    def makeLikelihoodTemplate(self, start, stop, chatter=1):
        #Transform in relative time (the BKGE wants that!)
        start = float(start) - int(
            float(start) > 231292801.000) * self.triggertime
        stop = float(stop) - int(
            float(stop) > 231292801.000) * self.triggertime
        outdir = self.outdir + '/Bkg_Estimates/%.2f_%.2f/' % (start, stop)

        try:
            _ = BKGE_interface.allowedIRFS
        except AttributeError:
            if (self.irf.upper().find("P8") >= 0):
                raise RuntimeError(
                    "Looks like you are using the old Background Estimator," +
                    " and Pass 8 IRFS. You cannot do that. You have to update"
                    + " your Science Tools.")
            else:
                #It is fine, this is the old BKGE with P7
                pass
            pass

        else:
            #Using the new version of the BKGE
            if (not self.irf.upper() in BKGE_interface.allowedIRFS):
                raise GtBurstException(
                    7, "IRF %s is not supported by the BKGE" % (self.irf))

            else:
                BKGE_interface.setResponseFunction(self.irf.upper())

        try:
            BKGE_interface.MakeGtLikeTemplate(start,
                                              stop,
                                              self.triggertime,
                                              self.ra,
                                              self.dec,
                                              self.ft1,
                                              self.ft2,
                                              OUTPUT_DIR=self.outdir,
                                              chatter=10,
                                              ROI_Radius=self.roi,
                                              GRB_NAME=self.grbname)
        except:
            print("ERROR executing the BKGE")

        bkgetemplate = os.path.abspath(
            os.path.join(outdir, '%s_gtlike_%s_CR_EGAL.txt' %
                         (self.irf, self.grbname)))
        statisticalError = 0

        return bkgetemplate, statisticalError, systematicError
Beispiel #4
0
def update(debug=False):

    print("Searching updates at %s..." % (remoteUrl))

    #Download file_list file

    try:

        os.remove("__file_list")

    except:

        pass

    urllib.urlcleanup()

    try:

        urllib.urlretrieve("%s/__file_list" % remoteUrl, "__file_list")

    except socket.timeout:

        raise GtBurstException(
            11,
            "Time out when connecting to %s. Check your internet connection, then retry"
            % (remoteUrl))

    except:

        raise GtBurstException(
            1,
            "Problems with the download. Check your connection, and that you can reach %s"
            % (remoteUrl))

    pass

    #Read the list of files
    f = open('__file_list')
    files = f.readlines()
    f.close()
    os.remove("__file_list")

    #Get the path of the gtburst installation
    path = GtBurst.__file__
    installationPath = os.path.join(
        os.path.sep.join(path.split(os.path.sep)[0:-3]))

    nUpdates = 0
    for ff in files:
        atoms = ff.split()
        pathname = atoms[-1].replace('*', '')
        if (ff.find("__file_list") >= 0):
            if (debug):
                print("Skipping %s..." % (ff))
        else:
            remoteMD5 = atoms[0]
            if (debug):
                print("File %s has remote MD5 checksum %s" %
                      (pathname, remoteMD5))

            #Get the MD5 of the same file in the GtBurst package path
            pathnameThisSys = pathname.replace("/", os.path.sep)
            localPath = os.path.join(installationPath, pathnameThisSys)
            if (not os.path.exists(localPath)):
                print(
                    "File %s does not exist in the current installation. Creating it..."
                    % (localPath))
                #If the new file is in a new directory, the directory needs to be created
                try:
                    os.makedirs(os.path.dirname(localPath))
                except:
                    #This will fail if the directory already exists
                    pass
                downloadFile(pathname, localPath)
                nUpdates += 1
            else:
                #File exists. Check the size
                localMD5 = md5.md5(open(localPath, 'rb').read()).hexdigest()
                if (localMD5 != remoteMD5):
                    print("Updating %s..." % (localPath))
                    downloadFile(pathname, localPath)
                    nUpdates += 1
                else:
                    if (debug):
                        print(
                            "NOT updating %s (local MD5: %s, remote MD5: %s)..."
                            % (localPath, localMD5, remoteMD5))
                    pass
                pass
                if (debug):
                    print("\n\n")
        pass
    pass

    return nUpdates
Beispiel #5
0
def run(**kwargs):
    if (len(kwargs.keys()) == 0):
        #Nothing specified, the user needs just help!
        thisCommand.getHelp()
        return
    pass

    #Get parameters values
    thisCommand.setParValuesFromDictionary(kwargs)
    try:
        eventfile = thisCommand.getParValue('eventfile')
        rspfile = thisCommand.getParValue('rspfile')
        ft2file = thisCommand.getParValue('ft2file')
        ra = thisCommand.getParValue('ra')
        dec = thisCommand.getParValue('dec')
        rad = thisCommand.getParValue('rad')
        irf = thisCommand.getParValue('irf')
        zmax = thisCommand.getParValue('zmax')
        tstart = thisCommand.getParValue('tstart')
        tstop = thisCommand.getParValue('tstop')
        emin = thisCommand.getParValue('emin')
        emax = thisCommand.getParValue('emax')
        skybinsize = thisCommand.getParValue('skybinsize')
        outfile = thisCommand.getParValue('skymap')
        strategy = thisCommand.getParValue('strategy')
        thetamax = float(thisCommand.getParValue('thetamax'))
        allowEmpty = _yesOrNoToBool(thisCommand.getParValue('allowEmpty'))
        clobber = _yesOrNoToBool(thisCommand.getParValue('clobber'))
        verbose = _yesOrNoToBool(thisCommand.getParValue('verbose'))
        figure = thisCommand.getParValue('figure')
    except KeyError as err:
        print("\n\nERROR: Parameter %s not found or incorrect! \n\n" %
              (err.args[0]))

        #Print help
        print thisCommand.getHelp()
        return
    pass

    from GtBurst import dataHandling
    global lastDisplay

    LATdata = dataHandling.LATData(eventfile, rspfile, ft2file)

    if (strategy.lower() == "time"):
        #gtmktime cut
        filteredFile, nEvents = LATdata.performStandardCut(ra,
                                                           dec,
                                                           rad,
                                                           irf,
                                                           tstart,
                                                           tstop,
                                                           emin,
                                                           emax,
                                                           zmax,
                                                           thetamax,
                                                           True,
                                                           strategy='time')
    elif (strategy.lower() == "events"):
        #no gtmktime cut, Zenith cut applied directly to the events
        filteredFile, nEvents = LATdata.performStandardCut(ra,
                                                           dec,
                                                           rad,
                                                           irf,
                                                           tstart,
                                                           tstop,
                                                           emin,
                                                           emax,
                                                           zmax,
                                                           thetamax,
                                                           True,
                                                           strategy='events')
    pass

    LATdata.doSkyMap(outfile, skybinsize)

    #Now open the output file and get exposure and total number of events
    skymap = pyfits.open(outfile)
    totalNumberOfEvents = numpy.sum(skymap[0].data)
    totalTime = numpy.sum(skymap['GTI'].data.field('STOP') -
                          skymap['GTI'].data.field('START'))
    skymap.close()
    print("\nTotal number of events in the counts map: %s" %
          (totalNumberOfEvents))
    print("Total time in Good Time Intervals:        %s" % (totalTime))
    if ((totalTime == 0) and allowEmpty == False):
        raise GtBurstException(
            2, "Your filter resulted in zero exposure. \n\n" +
            " Loose your cuts, or enlarge the time interval. You might want to "
            +
            " check also the navigation plots (in the Tools menu) to make sure "
            +
            " that the ROI is within the LAT FOV in the desired time interval."
        )

    displ = None

    if (figure != None):
        if (lastDisplay != None):
            lastDisplay.unbind()
        pass
        from GtBurst.InteractiveFt1Display import InteractiveFt1Display
        lastDisplay = InteractiveFt1Display(filteredFile, outfile, figure, ra,
                                            dec)
    pass

    return 'skymap', outfile, 'filteredeventfile', filteredFile, 'irf', irf, 'eventDisplay', lastDisplay
Beispiel #6
0
def run(**kwargs):
    if (len(kwargs.keys()) == 0):
        #Nothing specified, the user needs just help!
        thisCommand.getHelp()
        return
    pass

    #Get parameters values
    thisCommand.setParValuesFromDictionary(kwargs)
    try:
        ra = thisCommand.getParValue('ra')
        dec = thisCommand.getParValue('dec')
        particlemodel = thisCommand.getParValue('particle_model')
        galacticmodel = thisCommand.getParValue('galactic_model')
        sourcemodel = thisCommand.getParValue('source_model')
        filteredeventfile = thisCommand.getParValue('filteredeventfile')
        xmlmodel = thisCommand.getParValue('xmlmodel')
        triggername = thisCommand.getParValue('triggername')
        ft2file = thisCommand.getParValue('ft2file')
        fglmode = thisCommand.getParValue('fgl_mode')
        clobber = _yesOrNoToBool(thisCommand.getParValue('clobber'))
        verbose = _yesOrNoToBool(thisCommand.getParValue('verbose'))
    except KeyError as err:
        print("\n\nERROR: Parameter %s not found or incorrect! \n\n" %
              (err.args[0]))

        #Print help
        print thisCommand.getHelp()
        return
    pass

    #Get the IRF from the event file
    try:
        f = pyfits.open(filteredeventfile)
    except:
        raise GtBurstException(
            31, "Could not open filtered event file %s" % (filteredeventfile))

    tstart = float(f[0].header['_TMIN'])
    tstop = float(f[0].header['_TMAX'])
    irf = str(f[0].header['_IRF'])
    ra = float(f[0].header['_ROI_RA'])
    dec = float(f[0].header['_ROI_DEC'])
    roi = float(f[0].header['_ROI_RAD'])

    #Lookup table for the models
    models = {}
    if (particlemodel == 'isotr with pow spectrum'):
        models[
            'isotr with pow spectrum'] = LikelihoodComponent.IsotropicPowerlaw(
            )
    elif (particlemodel == 'isotr template'):
        models['isotr template'] = LikelihoodComponent.IsotropicTemplate(irf)
    pass

    if (galacticmodel == 'template'):
        models['template'] = LikelihoodComponent.GalaxyAndExtragalacticDiffuse(
            irf, ra, dec, 2.5 * roi)
    elif (galacticmodel == 'template (fixed norm.)'):
        models[
            'template (fixed norm.)'] = LikelihoodComponent.GalaxyAndExtragalacticDiffuse(
                irf, ra, dec, 2.5 * roi)
        models['template (fixed norm.)'].fixNormalization()
    pass

    deltat = numpy.sum(f['GTI'].data.field('STOP') -
                       f['GTI'].data.field('START'))
    f.close()
    triggertime = dataHandling.getTriggerTime(filteredeventfile)

    if (irf.lower().find('source') >= 0 and particlemodel != 'isotr template'):
        raise GtBurstException(
            6,
            "Do not use '%s' as model for the particle background in SOURCE class. Use '%s' instead."
            % (particlemodel, 'isotropic template'))

    modelsToUse = [
        LikelihoodComponent.PointSource(ra, dec, triggername, sourcemodel)
    ]
    if (particlemodel != 'none'):
        if (particlemodel == 'bkge'):
            if (ft2file is None or ft2file == ''):
                raise ValueError(
                    "If you want to use the BKGE, you have to provide an FT2 file!"
                )

            modelsToUse.append(
                LikelihoodComponent.BKGETemplate(filteredeventfile, ft2file,
                                                 tstart, tstop, triggername,
                                                 triggertime))
        else:
            modelsToUse.append(models[particlemodel])
    if (galacticmodel != 'none'):
        modelsToUse.append(models[galacticmodel])

    xml = LikelihoodComponent.LikelihoodModel()
    xml.addSources(*modelsToUse)
    xml.writeXML(xmlmodel)

    if (fglmode == 'complete'):

        #Use a very large delta T so that
        #all FGL sources are included in the
        #xml model
        deltat = 1e12

    xml.add2FGLsources(ra, dec, float(roi) + 8.0, xmlmodel, deltat)

    dataHandling._writeParamIntoXML(xmlmodel,
                                    IRF=irf,
                                    OBJECT=triggername,
                                    RA=ra,
                                    DEC=dec)

    return 'xmlmodel', xmlmodel
Beispiel #7
0
def run(**kwargs):
    if (len(kwargs.keys()) == 0):
        #Nothing specified, the user needs just help!
        thisCommand.getHelp()
        return
    pass

    #Get parameters values
    thisCommand.setParValuesFromDictionary(kwargs)
    try:
        eventfile = thisCommand.getParValue('filteredeventfile')
        rspfile = thisCommand.getParValue('rspfile')
        ft2file = thisCommand.getParValue('ft2file')
        expomap = thisCommand.getParValue('expomap')
        ltcube = thisCommand.getParValue('ltcube')
        xmlmodel = thisCommand.getParValue('xmlmodel')
        showmodelimage = thisCommand.getParValue('showmodelimage')
        optimize = thisCommand.getParValue('optimizeposition')
        spectralfiles = thisCommand.getParValue('spectralfiles')
        tsmin = float(thisCommand.getParValue('tsmin'))
        skymap = thisCommand.getParValue('skymap')
        liketype = thisCommand.getParValue('liketype')
        clobber = _yesOrNoToBool(thisCommand.getParValue('clobber'))
        verbose = _yesOrNoToBool(thisCommand.getParValue('verbose'))

        figure = thisCommand.getParValue('figure')
    except KeyError as err:
        print("\n\nERROR: Parameter %s not found or incorrect! \n\n" %
              (err.args[0]))

        #Print help
        print thisCommand.getHelp()
        return
    pass

    from GtBurst import dataHandling
    from GtBurst.angularDistance import getAngularDistance

    LATdata = dataHandling.LATData(eventfile, rspfile, ft2file)
    try:
        if (liketype == 'unbinned'):
            outfilelike, sources = LATdata.doUnbinnedLikelihoodAnalysis(
                xmlmodel, tsmin, expomap=expomap, ltcube=ltcube)
        else:
            #Generation of spectral files and optimization of the position is
            #not supported yet for binned analysis

            if (spectralfiles == 'yes'):

                print(
                    "\nWARNING: you specified spectralfiles=yes, but the generation of spectral files is not supported for binned analysis\n"
                )
                spectralfiles = 'no'

            if (optimize == 'yes'):

                print(
                    "\nWARNING: you specified optimize=yes, but position optimization is not supported for binned analysis\n"
                )
                optimize = 'no'

            outfilelike, sources = LATdata.doBinnedLikelihoodAnalysis(
                xmlmodel, tsmin, expomap=expomap, ltcube=ltcube)
    except GtBurstException as gt:
        raise gt
    except:
        raise

    #Transfer information on the source from the input to the output XML
    irf = dataHandling._getParamFromXML(xmlmodel, 'IRF')
    ra = dataHandling._getParamFromXML(xmlmodel, 'RA')
    dec = dataHandling._getParamFromXML(xmlmodel, 'DEC')
    name = dataHandling._getParamFromXML(xmlmodel, 'OBJECT')

    try:
        grb = filter(lambda x: x.name.lower().find(name.lower()) >= 0,
                     sources)[0]
        grb_TS = grb.TS
    except:
        #A model without GRB
        print("\nWarning: no GRB in the model!")
        grb_TS = -1
    pass

    if (irf == None):
        print(
            "\n\nWARNING: could not read IRF from XML file. Be sure you know what you are doing..."
        )
    else:
        dataHandling._writeParamIntoXML(outfilelike,
                                        IRF=irf,
                                        OBJECT=name,
                                        RA=ra,
                                        DEC=dec)
    pass

    if (spectralfiles == 'yes'):
        phafile, rspfile, bakfile = LATdata.doSpectralFiles(outfilelike)
    pass

    localizationMessage = ''
    bestra = ''
    bestdec = ''
    poserr = ''
    distance = ''
    if (optimize == 'yes'):
        sourceName = name

        #If the TS for the source is above tsmin, optimize its position
        #grb                       = filter(lambda x:x.name.lower().find(sourceName.lower())>=0,sources)[0]
        if (math.ceil(grb_TS) >= tsmin):
            try:
                bestra, bestdec, poserr = LATdata.optimizeSourcePosition(
                    outfilelike, sourceName)
            except:
                raise GtBurstException(
                    207,
                    "gtfindsrc execution failed. Were the source detected in the likelihood step?"
                )
            else:
                localizationMessage += "\nNew localization from gtfindsrc:\n\n"
                localizationMessage += "(R.A., Dec)                     = (%6.3f, %6.3f)\n" % (
                    bestra, bestdec)
                localizationMessage += "68 %s containment radius        = %6.3f\n" % (
                    '%', poserr)
                localizationMessage += "90 %s containment radius        = %6.3f\n" % (
                    '%', 1.41 * poserr)
                distance = getAngularDistance(float(ra), float(dec),
                                              float(bestra), float(bestdec))
                localizationMessage += "Distance from initial position  = %6.3f\n\n" % (
                    distance)
                localizationMessage += "NOTE: this new localization WILL NOT be used by default. If you judge"
                localizationMessage += " it is a better localization than the one you started with, update the"
                localizationMessage += " coordinates yourself and re-run the likelihood\n"
        pass
    pass

    if (figure != None and skymap != None and showmodelimage == 'yes'):

        #Now produce the binned exposure map (needed in order to display the fitted model as an image)
        modelmapfile = LATdata.makeModelSkyMap(outfilelike)

        #Display point sources in the image, and report in the table
        #all 2FGL sources with TS > 9 + always the GRB, independently of its TS
        detectedSources = []
        grbFlux = 1e-13
        for src in sources:
            weight = 'bold'

            if (src.type == 'PointSource'):
                if (src.TS > 4):
                    detectedSources.append(src)
                    if (src.name.find('2FGL') < 0):
                        #GRB
                        grbFlux = src.flux
                pass
            pass
        pass

        #Display the counts map
        from GtBurst import aplpy
        import matplotlib.pyplot as plt

        figure.clear()
        orig = aplpy.FITSFigure(skymap,
                                convention='calabretta',
                                figure=figure,
                                subplot=[0.1, 0.1, 0.45, 0.7])
        vmax = max(pyfits.open(skymap)[0].data.flatten())
        nEvents = numpy.sum(pyfits.open(skymap)[0].data)
        telapsed = pyfits.open(eventfile)[0].header['TSTOP'] - pyfits.open(
            eventfile)[0].header['TSTART']
        orig.set_tick_labels_font(size='small')
        orig.set_axis_labels_font(size='small')
        orig.show_colorscale(cmap='gist_heat',
                             vmin=0.1,
                             vmax=max(vmax, 0.11),
                             stretch='log',
                             aspect='auto')
        # Modify the tick labels for precision and format
        orig.tick_labels.set_xformat('ddd.dd')
        orig.tick_labels.set_yformat('ddd.dd')

        # Display a grid and tweak the properties
        orig.show_grid()

        #Renormalize the modelmapfile to the flux of the grb
        f = pyfits.open(modelmapfile, 'update')
        f[0].data = f[0].data / numpy.max(f[0].data) * nEvents / telapsed
        print(numpy.max(f[0].data))
        f.close()

        img = aplpy.FITSFigure(modelmapfile,
                               convention='calabretta',
                               figure=figure,
                               subplot=[0.55, 0.1, 0.4, 0.7])
        img.set_tick_labels_font(size='small')
        img.set_axis_labels_font(size='small')
        #vmax                     = max(pyfits.open(modelmapfile)[0].data.flatten())
        img.show_colorscale(cmap='gist_heat', aspect='auto', stretch='log')

        for src in detectedSources:
            img.add_label(float(src.ra),
                          float(src.dec),
                          "%s\n(ts = %i)" % (src.name, int(math.ceil(src.TS))),
                          relative=False,
                          weight=weight,
                          color='green',
                          size='small')
        pass

        # Modify the tick labels for precision and format
        img.tick_labels.set_xformat('ddd.dd')
        img.tick_labels.set_yformat('ddd.dd')

        # Display a grid and tweak the properties
        img.show_grid()

        #ax                        = figure.add_axes([0.1,0.72,0.85,0.25],frame_on=False)
        #ax.xaxis.set_visible(False)
        #ax.yaxis.set_visible(False)
        #col_labels                =['Source Name','TS','Energy flux','Photon index']
        #table_vals                = map(lambda x:[x.name,"%i" %(int(math.ceil(x.TS))),
        #                                          "%s +/- %s" % (x.flux,x.fluxError),
        #                                          "%s +/- %s" % (x.photonIndex,x.photonIndexError)],detectedSources)
        #
        #if(len(table_vals)>0):
        #  the_table                 = ax.table(cellText=table_vals,
        #                                        colLabels=col_labels,
        #                                       loc='upper center')

        figure.canvas.draw()
        figure.savefig("likelihood_results.png")
    pass

    if (figure != None):

        #Assume we have an X server running
        #Now display the results
        likemsg = "Log(likelihood) = %s" % (LATdata.logL)
        displayResults(
            figure.canvas._tkcanvas, LATdata.resultsStrings + "\n" + likemsg +
            "\n" + localizationMessage)

    print(localizationMessage)

    return 'likexmlresults', outfilelike, 'TS', grb_TS, 'bestra', bestra, 'bestdec', bestdec, 'poserr', poserr, 'distance', distance, 'sources', sources
Beispiel #8
0
    def downloadDirectoryWithFTP(self,
                                 address,
                                 filenames=None,
                                 namefilter=None):
        #Connect to the server
        if (address.find("ftp://") == 0):
            serverAddress = address.split("/")[2]
            directory = "/" + "/".join(address.split("/")[3:])
        else:
            serverAddress = address.split("/")[0]
            directory = "/" + "/".join(address.split("/")[1:])
        pass

        #Open FTP session
        try:
            ftp = ftplib.FTP(serverAddress, "anonymous", '', '', timeout=60)
        except socket.error as socketerror:
            raise GtBurstException(
                11,
                "Error when connecting: %s" % os.strerror(socketerror.errno))

        print("Loggin in to %s..." % serverAddress),
        try:
            ftp.login()
        except:
            #Maybe we are already logged in
            try:
                ftp.cwd('/')
            except:
                #nope! don't know what is happening
                raise
            pass
        pass

        print("done")
        self.makeLocalDir()
        try:
            ftp.cwd(directory)
        except:
            #Remove the empty directory just created
            try:
                os.rmdir(self.localRepository)
            except:
                pass
            raise GtBurstException(
                5,
                "The remote directory %s is not accessible. This kind of data is probably not available for trigger %s, or the server is offline."
                % (serverAddress + directory, self.trigName))
        pass

        if (filenames is None):
            filenames = []
            ftp.retrlines('NLST', filenames.append)
        pass

        maxTrials = 10

        #Build the window for the progress
        if (self.parent is None):
            #Do not use any graphical output
            root = None
            m1 = None
        else:
            #make a transient window
            root = Toplevel()
            root.transient(self.parent)
            root.grab_set()
            l = Label(root, text='Downloading...')
            l.grid(row=0, column=0)
            m1 = Meter(root,
                       500,
                       20,
                       'grey',
                       'blue',
                       0,
                       None,
                       None,
                       'white',
                       relief='ridge',
                       bd=3)
            m1.grid(row=1, column=0)
            m1.set(0.0, 'Download started...')
            l2 = Label(root, text='Total progress:')
            l2.grid(row=2, column=0)
            m2 = Meter(root,
                       500,
                       20,
                       'grey',
                       'blue',
                       0,
                       None,
                       None,
                       'white',
                       relief='ridge',
                       bd=3)
            m2.grid(row=3, column=0)
            m2.set(0.0, 'Download started...')
        pass

        for i, filename in enumerate(filenames):
            if (namefilter is not None and filename.find(namefilter) < 0):
                #Filename does not match, do not download it
                continue

            if (root is not None):
                m2.set((float(i)) / len(filenames))
            skip = False
            if (not self.getCSPEC):
                if (filename.find("cspec") >= 0):
                    skip = True
            if (not self.getTTE):
                if (filename.find("tte") >= 0):
                    skip = True
            if (not self.getRSP):
                if (filename.find("rsp") >= 0):
                    skip = True
            if (not self.getCTIME):
                if (filename.find("ctime") >= 0):
                    skip = True
            pass
            #if(filename.find(".pdf")>0 or filename.find("gif") >0 or filename.find(".png")>0):
            #  skip                  = (not self.minimal)
            if (skip):
                print("Skipping %s ..." % (filename))
                continue
            else:
                print("Retrieving %s ..." % (filename)),

            if (root is not None):
                l['text'] = "Downloading %s..." % filename

            done = False
            local_filename = os.path.join(self.localRepository, filename)

            while (done == False):
                try:
                    f = open(local_filename, 'wb')
                except:
                    raise IOError(
                        "Could not open file %s for writing. Do you have write permission on %s?"
                        % (local_filename, self.localRepository))

                g = downloadCallback.get_size()
                try:
                    ftp.dir(filename, g)
                    totalsize = g.size  #*1024
                    printer = downloadCallback.Callback(totalsize, f, m1)
                    ftp.retrbinary('RETR ' + filename, printer)
                    localSize = f.tell()
                    f.close()
                    if (localSize != totalsize):
                        #This will be catched by the next "except", which will retry
                        raise
                    done = True
                except:
                    print("\nConnection lost! Trying to reconnect...")
                    #Reconnect
                    f.close()
                    try:
                        ftp.close()
                    except:
                        pass
                    ftp = ftplib.FTP(serverAddress,
                                     "anonymous",
                                     '',
                                     '',
                                     timeout=60)
                    try:
                        ftp.login()
                    except:
                        pass
                    ftp.cwd(directory)
                    done = False
                    continue
                pass
            print(" done")
        pass

        ftp.close()
        print("\nDownload files done!")
        if (root is not None):
            m2.set(1.0)
            root.destroy()
        pass
Beispiel #9
0
def update(debug=False):

    print("Searching updates at %s..." % (remoteUrl))

    #Download file_list file

    try:

        os.remove("__file_list")

    except:

        pass

    #now check if we have write permission in this directory
    try:

        with open("_write_test", "w+") as f:

            f.write("TEST")

    except:

        raise GtBurstException(
            1,
            "The updater cannot write in directory %s. Do you have write permission there?"
            % (os.getcwd()))

    else:

        os.remove("_write_test")

    urllib.urlcleanup()

    try:

        urllib.urlretrieve("%s/__file_list" % remoteUrl, "__file_list")

    except socket.timeout:

        raise GtBurstException(
            11,
            "Time out when connecting to %s. Check your internet connection, then retry"
            % (remoteUrl))

    except:

        raise GtBurstException(
            1,
            "Problems with the download. Check your connection, and that you can reach %s"
            % (remoteUrl))

    pass

    #Read the list of files
    f = open('__file_list')
    files = f.readlines()
    f.close()
    os.remove("__file_list")

    #Check if there is a HTML tag in the __file_list. This can
    #happen if there is a proxy in between which is forwarding to
    #a page different than the one intended
    if (" ".join(files).lower().find("<html>") >= 0):
        raise GtBurstException(
            1,
            "Download was redirected. This happens usually when you are behind a proxy."
            + " If you are behind a proxy, make sure to log in and " +
            "that executables from the command line can reach the internet directly."
        )

    # First check if we need to update the updater script itself

    self_path = os.path.abspath(__file__)
    updater_line = filter(lambda x: x.find("GtBurst/updater.py") >= 0,
                          files)[0]

    remote_self_md5 = updater_line.split()[0]
    local_self_md5 = md5.md5(open(self_path, 'rb').read()).hexdigest()

    if remote_self_md5 != local_self_md5:

        # Need to update the updater
        downloadFile(updater_line.split()[-1].replace('*', ''), self_path)

        print("Bootstrapping new updater...")

        # Execute the new copy of myself :-)
        os.system("python %s" % self_path)

        print("done")

        return 999

    #Get the path of the gtburst installation
    path = GtBurst.__file__
    installationPath = os.path.join(
        os.path.sep.join(path.split(os.path.sep)[0:-3]))

    nUpdates = 0
    for ff in files:
        atoms = ff.split()
        pathname = atoms[-1].replace('*', '')
        if (ff.find("__file_list") >= 0):
            if (debug):
                print("Skipping %s..." % (ff))
        else:
            remoteMD5 = atoms[0]
            if (debug):
                print("File %s has remote MD5 checksum %s" %
                      (pathname, remoteMD5))

            #Check if the file exists in the local installation
            pathnameThisSys = pathname.replace("/", os.path.sep)

            if (pathname.find("data") == 0):

                #This is a data file
                localPath = os.path.join(
                    data_path,
                    pathnameThisSys.replace("data%s" % (os.path.sep), ""))

            else:

                #This is some other file
                localPath = os.path.join(
                    python_path,
                    pathnameThisSys.replace("python%s" % (os.path.sep), ""))

            pass

            if (not os.path.exists(localPath)):
                print(
                    "File %s does not exist in the current installation. Creating it..."
                    % (localPath))
                #If the new file is in a new directory, the directory needs to be created
                try:
                    os.makedirs(os.path.dirname(localPath))
                except:
                    #This will fail if the directory already exists
                    pass
                downloadFile(pathname, localPath)
                nUpdates += 1
            else:
                #File exists. Check the size
                localMD5 = md5.md5(open(localPath, 'rb').read()).hexdigest()
                if (localMD5 != remoteMD5):
                    print("Updating %s..." % (localPath))
                    downloadFile(pathname, localPath)
                    nUpdates += 1
                else:
                    if (debug):
                        print(
                            "NOT updating %s (local MD5: %s, remote MD5: %s)..."
                            % (localPath, localMD5, remoteMD5))
                    pass
                pass
                if (debug):
                    print("\n\n")
        pass
    pass

    return nUpdates