Exemplo n.º 1
0
    def __init__(self,FILE, templatefilename=None, NCHAN=16, save=False, disp = False, minutes = False, ext = None):
        warnings.filterwarnings("ignore") #probably should remove later
        start = time.time()
        self.templatefilename = templatefilename
        self.save = save
        self.disp = disp
        self.minutes = minutes
        self.ext = ext
        self.handler = ArchiveHandler(FILE,templatefilename = templatefilename, NCHAN=NCHAN)
        
        self.fig = figure(figsize= (15,9.5))
        self.fig.subplots_adjust(top=0.93)
        gs1 = gridspec.GridSpec(13,2)
        gs1.update(left = .05, right = .33, wspace = .05, hspace = .2)
        
        
        ax1 = subplot(gs1[2:,0])
        self.handler.plotProfiles(ax = ax1, labels = True)
        
        #ax2 = subplot(gs1[2:,1], sharey = ax1)
        ax2 = subplot(gs1[2:,1])
        self.handler.plotProfiles(ax = ax2, difference = True, labels = False)
        ax2.set_ylim(ax1.get_ylim())
        
        ax3 = subplot(gs1[0:2,0])
        self.handler.plotAverageProfile(ax = ax3)
        
        ax4 = subplot(gs1[0:2,1])
        self.handler.plotTemplate(ax = ax4)
        
        ax3.axes.get_xaxis().set_visible(False)
        ax4.axes.get_xaxis().set_visible(False)
        
        gs2 = gridspec.GridSpec(13,2)
        #gs2.update(left = .37, right = .61)
        gs2.update(left = .34, right = .58)
        
        #ax5 = subplot(gs2[2:,:], sharey = ax1)
        ax5 = subplot(gs2[2:,:])
        self.handler.plotDynamicSpectrum(ax = ax5, minutes = minutes, labels = True)
        ax5.set_ylim(ax1.get_ylim())
        
        
        gs3 = gridspec.GridSpec(26,12)
        #gs3.update(left = .65, right = .99, wspace = .02, hspace = .02)
        gs3.update(left = .63, right = .99, wspace = .02, hspace = .02)
        #gs3.update(left = .62, right = .99, wspace = .02, hspace = .02)
        
        ax7 = subplot(gs3[2:9,1:11])
        self.handler.plotAcf2d(ax = ax7, minutes = minutes, labels = True)
        
        ax8 = subplot(gs3[11:20,1:11])
        self.handler.plotSecondarySpectrum(ax = ax8, minutes = minutes, labels = True)
        
        ax9 = subplot(gs3[22:26,0:5])
        if not args.nodm:
            print 'Now calculating SNR vs DM. This is slow; to skip this step use quicklook.py -nodm'
            self.handler.DMCalculator(ax9, initial=.99, end = 1.01, iters = np.abs(args.iters), max_depth = np.abs(args.depth))
        
        ax10 = subplot(gs3[22:26,7:12])
        self.handler.plotBasicHistogram(ax = ax10, bins = 48, labels = True)
    
        self.plotHeader()
        
        if self.handler.templatefilename is None:
            textstr = 'NONE'
        else:
            textstr = 'TMPL'
        
        props = dict(boxstyle='round', facecolor= 'none', edgecolor = 'black', alpha=0.5)
        self.fig.text(0.3, 0.92, textstr, fontsize=8, verticalalignment='top', bbox=props)
        
        #hide stuff
        ax3.axes.get_xaxis().set_visible(False)
        ax4.axes.get_xaxis().set_visible(False)
        ax4.axes.get_yaxis().set_visible(False)
        ax2.axes.get_xaxis().set_visible(False)
        ax2.axes.get_yaxis().set_visible(False)
        ax5.axes.get_yaxis().set_visible(False)
        
        
        end = time.time()
        print 'Run time: %0.2f s'%(end - start) 
            
        if ext is not None:
            self.save_to_pdf(ext=ext)
        
        if save:
            self.handler.save_to_npz(name = None)

        if disp:
            show()
Exemplo n.º 2
0
    def run(self, finder):
        """Finder object organize content in directories in 4 categories:
         - video
         - subtitle
         - archive
         - subdirectories
        First Finder looks for related archive and video file, than extracts
        subtitles from archives.
        After that Finder looks for related archive and subdirectories and move
        archives to subdirectories.
        When archives are completly handle, Finder looks for related videos and
        subtitles and rename subtitles with related video.
        """

        archives = finder.get_archives()
        videos = finder.get_videos()

        # extract archives that match with video files
        archive_handler = ArchiveHandler()
        correlator = Correlator()
        correlation = correlator.get_related(archives, videos)
        for t in correlation:
            archive_handler.extract(t[0].get_filename())

        dir_path = finder.get_folder_path()

        # move archives to directories if they match
        subdirs = finder.get_subdirs()
        correlation = correlator.get_related(subdirs, archives)
        for t in correlation:
            try:
                src = os.path.join(dir_path, t[1].get_filename())
                dst = os.path.join(dir_path,
                                   t[0].get_filename(),
                                   t[1].get_filename())
                if src != dst:
                    os.rename(src, dst)
                    print(('moving: ' + src + '\nto:     ' + dst + '\n'))
            except OSError as err:
                print((str(err) + " " + src), file=sys.stderr)

        # update directory content
        finder.update_content()

        videos = finder.get_videos()
        subtitles = finder.get_subtitles()
        correlation = correlator.get_related(videos, subtitles)

        # rename subtitles with video filename
        for t in correlation:
            try:
                src = os.path.join(dir_path, t[1].get_filename())
                dst = os.path.join(dir_path,
                                   t[0].get_filename_no_ext() + '.srt')
                # rename file only if it doesn't already exists
                if src != dst and not os.path.exists(dst):
                    os.rename(src, dst)
                    print(('renaming: ' + src + '\ninto:     ' + dst + '\n'))
            except OSError as err:
                print((str(err) + " " + src), file=sys.stderr)


        """ Opensubtitle subtitle search """
        # update directory content
        finder.update_content()

        videos = finder.get_videos()
        subtitles = finder.get_subtitles()
        unrelated_videos = correlator.get_unrelated(videos, subtitles)

        # get OpenSubtitle user account info from config.json file
        with open(constants.USER_CONFIG_FILE, 'r') as config:
            try:
                decoder = json.JSONDecoder()
                userconfig = decoder.decode(config.read())
                username = userconfig['username']
                password = userconfig['password']
                language = userconfig['language']
                useragent = userconfig['useragent']
            except json.JSONDecodeError as jsderr:
                print(jsderr, file=sys.stderr)

        opensubtitle = OpenSubtitle()
        opensubtitle.login(username, password, language, useragent)
        for video in videos:
            subtitle_file = os.path.join(dir_path, video.get_filename_no_ext() + ".srt")
            if not os.path.exists(subtitle_file):
                subtitle = opensubtitle.download_subtitle(video.get_filename())
                if subtitle:
                    with open(subtitle_file, 'w') as f:
                        f.write(subtitle)
                        print("Subtitle file downloaded: " + subtitle_file)
        opensubtitle.logout(opensubtitle.get_token())
Exemplo n.º 3
0
class Quicklook:
    def __init__(self,FILE, templatefilename=None, NCHAN=16, save=False, disp = False, minutes = False, ext = None):
        warnings.filterwarnings("ignore") #probably should remove later
        start = time.time()
        self.templatefilename = templatefilename
        self.save = save
        self.disp = disp
        self.minutes = minutes
        self.ext = ext
        self.handler = ArchiveHandler(FILE,templatefilename = templatefilename, NCHAN=NCHAN)
        
        self.fig = figure(figsize= (15,9.5))
        self.fig.subplots_adjust(top=0.93)
        gs1 = gridspec.GridSpec(13,2)
        gs1.update(left = .05, right = .33, wspace = .05, hspace = .2)
        
        
        ax1 = subplot(gs1[2:,0])
        self.handler.plotProfiles(ax = ax1, labels = True)
        
        #ax2 = subplot(gs1[2:,1], sharey = ax1)
        ax2 = subplot(gs1[2:,1])
        self.handler.plotProfiles(ax = ax2, difference = True, labels = False)
        ax2.set_ylim(ax1.get_ylim())
        
        ax3 = subplot(gs1[0:2,0])
        self.handler.plotAverageProfile(ax = ax3)
        
        ax4 = subplot(gs1[0:2,1])
        self.handler.plotTemplate(ax = ax4)
        
        ax3.axes.get_xaxis().set_visible(False)
        ax4.axes.get_xaxis().set_visible(False)
        
        gs2 = gridspec.GridSpec(13,2)
        #gs2.update(left = .37, right = .61)
        gs2.update(left = .34, right = .58)
        
        #ax5 = subplot(gs2[2:,:], sharey = ax1)
        ax5 = subplot(gs2[2:,:])
        self.handler.plotDynamicSpectrum(ax = ax5, minutes = minutes, labels = True)
        ax5.set_ylim(ax1.get_ylim())
        
        
        gs3 = gridspec.GridSpec(26,12)
        #gs3.update(left = .65, right = .99, wspace = .02, hspace = .02)
        gs3.update(left = .63, right = .99, wspace = .02, hspace = .02)
        #gs3.update(left = .62, right = .99, wspace = .02, hspace = .02)
        
        ax7 = subplot(gs3[2:9,1:11])
        self.handler.plotAcf2d(ax = ax7, minutes = minutes, labels = True)
        
        ax8 = subplot(gs3[11:20,1:11])
        self.handler.plotSecondarySpectrum(ax = ax8, minutes = minutes, labels = True)
        
        ax9 = subplot(gs3[22:26,0:5])
        if not args.nodm:
            print 'Now calculating SNR vs DM. This is slow; to skip this step use quicklook.py -nodm'
            self.handler.DMCalculator(ax9, initial=.99, end = 1.01, iters = np.abs(args.iters), max_depth = np.abs(args.depth))
        
        ax10 = subplot(gs3[22:26,7:12])
        self.handler.plotBasicHistogram(ax = ax10, bins = 48, labels = True)
    
        self.plotHeader()
        
        if self.handler.templatefilename is None:
            textstr = 'NONE'
        else:
            textstr = 'TMPL'
        
        props = dict(boxstyle='round', facecolor= 'none', edgecolor = 'black', alpha=0.5)
        self.fig.text(0.3, 0.92, textstr, fontsize=8, verticalalignment='top', bbox=props)
        
        #hide stuff
        ax3.axes.get_xaxis().set_visible(False)
        ax4.axes.get_xaxis().set_visible(False)
        ax4.axes.get_yaxis().set_visible(False)
        ax2.axes.get_xaxis().set_visible(False)
        ax2.axes.get_yaxis().set_visible(False)
        ax5.axes.get_yaxis().set_visible(False)
        
        
        end = time.time()
        print 'Run time: %0.2f s'%(end - start) 
            
        if ext is not None:
            self.save_to_pdf(ext=ext)
        
        if save:
            self.handler.save_to_npz(name = None)

        if disp:
            show()

            
    def plotHeader(self):
        """Plot the header for Quicklook"""
        calcDM = self.handler.calculated_DM
        if calcDM is None:
            calcDM = "Not calculated"
        else:
            calcDM = "%0.6f"%calcDM
        duration = self.handler.getDuration()
        if self.minutes:
            duration = "%0.2f min"%(duration/60)
        else:
            duration = "%0.2f s"%(duration)
        header = ('Name:  %s    Telescope:  %s    MJD:  %i\n'%(self.handler.getName(full=True),self.handler.getTelescope(),self.handler.getMJD())+
                  'PSR:  %s    Period:  %0.3f ms    Nbins:  %i    DM:  %0.6f    Calc DM:  %s\n'%(self.handler.ar.getName(),self.handler.getPeriod()*1e3,self.handler.getNbin(),self.handler.getDM(),calcDM)+
                  'BW:  %0.2f MHz    Nchan:  %i    Duration  %s\n'%(abs(self.handler.getBandwidth()),np.size(self.handler.F),duration)+
                  'Time/Bin:  %0.2f us'%(self.handler.getPeriod()/self.handler.getNbin()*1e6))
                
        props = dict(boxstyle='round', facecolor= 'none', edgecolor = 'black', alpha=0.5)
        self.fig.text(0.35, 0.95, header, fontsize=13, verticalalignment='top')
            
    
    def save_to_pdf(self, name=None, ext = None):
        """Print the Quicklook plot to a file with extension ext"""
        if name is None:
            name = self.handler.name
        name = name + '.' + ext
        self.fig.savefig(name)