Beispiel #1
0
def realtime_objects_list(request,
                          night='',
                          state='all',
                          preview=False,
                          singlepage=False,
                          unanalyzed=False,
                          unclassified=False,
                          analyzed=False,
                          laser=False,
                          min_length=0,
                          channel=0):
    if request.method == 'POST':
        assert_permission(request, 'fweb.modify_data')
        message = ""
        ids = [int(id) for id in request.POST.getlist('object_ids[]')]
        # Make ids unique
        ids = [id for id in set(ids)]
        action = request.POST.get('action')
        r = None

        if action == 'process':
            r = [
                favor2_celery.processObjects.apply_async(kwargs={
                    'id': ids,
                    'reprocess': True
                },
                                                         queue="channel%d" %
                                                         id)
                for id in xrange(1, settings.NCHANNELS + 1)
            ]
        elif action == 'process_all':
            r = [
                favor2_celery.processObjects.apply_async(kwargs={
                    'id': 0,
                    'state': state,
                    'night': night,
                    'reprocess': False
                },
                                                         queue="channel%d" %
                                                         id)
                for id in xrange(1, settings.NCHANNELS + 1)
            ]
        elif action == 'reprocess_all':
            r = [
                favor2_celery.processObjects.apply_async(kwargs={
                    'id': 0,
                    'state': state,
                    'night': night,
                    'reprocess': True
                },
                                                         queue="channel%d" %
                                                         id)
                for id in xrange(1, settings.NCHANNELS + 1)
            ]
        elif action == 'preview':
            r = [
                favor2_celery.processObjects.apply_async(kwargs={
                    'id': ids,
                    'reprocess': True,
                    'preview': True
                },
                                                         queue="channel%d" %
                                                         id)
                for id in xrange(1, settings.NCHANNELS + 1)
            ]
        elif action == 'preview_all':
            r = [
                favor2_celery.processObjects.apply_async(kwargs={
                    'id': 0,
                    'state': state,
                    'night': night,
                    'reprocess': False,
                    'preview': True
                },
                                                         queue="channel%d" %
                                                         id)
                for id in xrange(1, settings.NCHANNELS + 1)
            ]
        elif action == 'merge_meteors':
            r = favor2_celery.mergeMeteors.apply_async(kwargs={'night': night},
                                                       queue="beholder")
        elif action == 'meteors_night_processed' and night and night != 'all':
            n = MeteorsNightsProcessed(night=night)
            n.save()
        elif action == 'filter_laser':
            r = favor2_celery.filterLaserEvents.apply_async(
                kwargs={'night': night}, queue="beholder")
        elif action == 'filter_satellites':
            r = favor2_celery.filterSatellites.apply_async(
                kwargs={'night': night}, queue="beholder")
        elif action == 'filter_flashes':
            r = favor2_celery.filterFlashes.apply_async(
                kwargs={'night': night}, queue="beholder")
        elif action == 'delete':
            for id in ids:
                realtime_object_delete(id=id)

        if r and type(r) != list:
            # Redirect to the task page
            return redirect('tasks_task', id=r.id)
        elif r:
            # Redirect to the task page
            return redirect('tasks_list')
        else:
            # Redirect to the same view, but with no POST args. We can't display messages with it!
            if not state or state == '':
                state = 'all'

            if night and night != 'all':
                return redirect('/realtime/night/%s/%s' % (night, state))
            else:
                return redirect('/realtime/%s' % (state))

    if request.method == 'GET':
        if request.GET.get('preview'):
            preview = True

        if request.GET.get('singlepage'):
            singlepage = True

        if request.GET.get('laser'):
            laser = True

        if request.GET.get('unanalyzed'):
            unanalyzed = True

        if request.GET.get('unclassified'):
            unclassified = True

        if request.GET.get('analyzed'):
            analyzed = True

        if request.GET.get('min_length'):
            min_length = int(request.GET.get('min_length'))

        if request.GET.get('channel'):
            channel = int(request.GET.get('channel'))

    objects = RealtimeObjects.objects.extra(
        select={
            'nrecords':
            'SELECT count(*) FROM realtime_records WHERE object_id=realtime_objects.id',
            'nimages':
            'SELECT count(*) FROM realtime_images WHERE object_id=realtime_objects.id'
        })

    if request.method == 'GET' and request.GET.get('nframes'):
        nframes = int(request.GET.get('nframes'))
        objects = objects.filter(state=0).extra(
            where=["(params->'nframes')::int > %s"], params=[nframes])

    if state == 'meteor' and request.method == 'GET' and request.GET.get(
            'ang_vel'):
        ang_vel = float(request.GET.get('ang_vel'))
        objects = objects.filter(state=0).extra(where=[
            "((params->'ang_vel')::float < %s and (params->'ang_vel')::float > 0)"
        ],
                                                params=[ang_vel])

    if night and night != 'all':
        objects = objects.filter(night=night)

    if channel:
        objects = objects.filter(channel_id=channel)

    objects = objects.order_by('-time_start')

    if not state or state == 'all':
        state = 'all'
    else:
        state_id = RealtimeObjectState.objects.get(name=state).id
        objects = objects.filter(state=state_id)

    if request.method == 'GET' and request.GET.get('after'):
        objects = objects.filter(night__gt=request.GET.get('after'))
    if request.method == 'GET' and request.GET.get('before'):
        objects = objects.filter(night__lt=request.GET.get('before'))
    if request.method == 'GET' and request.GET.get('duration'):
        objects = objects.extra(
            where=['extract(epoch from time_end-time_start) > %s'],
            params=[float(request.GET.get('duration'))])

    if request.method == 'GET' and request.GET.get('sparse'):
        objects = objects.extra(where=[
            '10.0*extract(epoch from time_end-time_start) > %s*(params->\'length\')::int'
        ],
                                params=[float(request.GET.get('sparse'))])

    #states = [s['name'] for s in RealtimeObjectState.objects.values('name')]
    states = ['single', 'meteor', 'moving', 'flash', 'particle', 'misc']
    states.append('all')

    channels = [str(x) for x in range(1, settings.NCHANNELS + 1)]

    if min_length:
        objects = objects.extra(where=['(params->\'length\')::int>%s'],
                                params=[min_length])

    if unclassified:
        objects = objects.extra(where=['(params->\'classification\') = \'\''])

    if unanalyzed or analyzed or laser:  # unclassified or laser:
        objects_filtered = []

        for object in objects:
            if ((unanalyzed and (not object.params.has_key('analyzed')
                                 or object.params['analyzed'] != '1')) or
                (unclassified and object.params.has_key('classification')
                 and object.params['classification'] == '')
                    or (analyzed and object.params.has_key('analyzed')
                        and object.params['analyzed'] == '1')
                    or (laser and (object.params.has_key('laser')
                                   and object.params['laser'] == '1'))):
                objects_filtered.append(object)
    else:
        objects_filtered = objects

    # if min_length:
    #     objects = objects_filtered
    #     objects_filtered = []

    #     for object in objects:
    #         if object.nrecords > min_length:
    #             objects_filtered.append(object)

    context = {
        'objects': objects_filtered,
        'night': night,
        'state': state,
        'states': states,
        'channels': channels
    }

    if state == 'meteor':
        for obj in objects:
            r = obj.realtimerecords_set.order_by('time').first()
            if r:
                obj.filter_id = r.filter_id
    if preview:
        for obj in objects:
            # This step should be optimized somehow
            if state == 'flash':
                r = obj.realtimerecords_set.order_by('mag').first()
            else:
                r = obj.realtimerecords_set.order_by('time').first()
            if r:
                i = obj.realtimeimages_set.filter(time=r.time).first()
                if i:
                    obj.firstimage = i

                if state == 'flash':
                    obj.firstimage0 = obj.realtimeimages_set.order_by(
                        'time').first()
                    obj.firstimage1 = obj.realtimeimages_set.order_by(
                        'time').last()

    if state == 'flash':
        for obj in objects:
            obj.alert = Alerts.objects.filter(
                channel_id=obj.channel_id).filter(time=obj.time_start).first()
            if obj.alert:
                if not Images.objects.filter(type='alert').filter(
                        night=obj.night
                ).extra(where=["keywords->'TARGET'='alert_%s'" %
                               obj.alert.id]).exists():
                    obj.alert = None

            rec = RealtimeRecords.objects.filter(
                object_id=obj.id).order_by('time').first()
            if rec:
                obj.ra0, obj.dec0 = rec.ra, rec.dec

    if night and night != 'all':
        context['night_prev'] = RealtimeObjects.objects.distinct(
            'night').filter(
                night__lt=night).order_by('-night').values('night').first()
        context['night_next'] = RealtimeObjects.objects.distinct(
            'night').filter(
                night__gt=night).order_by('night').values('night').first()

    return TemplateResponse(request, 'realtime_objects.html', context=context)
Beispiel #2
0
def satellite_download(request,
                       id=0,
                       track_id=0,
                       param=None,
                       show_records=False):
    response = HttpResponse(request, content_type='text/plain')

    if track_id:
        try:
            track = SatelliteTracks.objects.get(id=track_id)
        except:
            raise Http404('No such track')

        satellite = track.satellite
        #records = SatelliteRecordsSimple.objects.filter(track_id=track_id).order_by('time')
        records = track.satelliterecords_set.order_by('time')

        response[
            'Content-Disposition'] = 'attachment; filename=track_%s.txt' % track_id

        print >> response, "# satellite: %d %s / %s" % (
            satellite.catalogue_id, satellite.name, satellite.country)
        print >> response, "# catalogue:", satellite.catalogue, satellite_catalogue(
            satellite.catalogue)
        print >> response, "# track:", track.id
        print >> response, "# variability:", track.variability, {
            0: 'Not variable',
            1: 'Aperiodic',
            2: 'Periodic'
        }.get(track.variability)
        print >> response, "# variability_period:", track.variability_period
        print >> response, "# npoints:", len(records)

        print >> response, "#"

        print >> response, "# Date Time StdMag Mag Filter Penumbra Distance Phase Channel"

        for record in records:
            print >> response, record.time, record.stdmag, record.mag, filter_name(
                record.filter_id
            ), record.penumbra, record.distance, record.phase, record.channel_id

    elif id and param is not None:
        try:
            satellite = Satellites.objects.get(id=id)
        except:
            raise Http404('No such satellite')

        tracks = satellite.satellitetracks_set.order_by('id')

        response[
            'Content-Disposition'] = 'attachment; filename=satellite_%s_%s.txt' % (
                param, id)

        print >> response, "# satellite: %d %s / %s" % (
            satellite.catalogue_id, satellite.name, satellite.country)
        print >> response, "# catalogue:", satellite.catalogue, satellite_catalogue(
            satellite.catalogue)
        print >> response, "# variability:", satellite.variability, {
            0: 'Not variable',
            1: 'Aperiodic',
            2: 'Periodic'
        }.get(satellite.variability)
        print >> response, "# variability_period:", satellite.variability_period
        print >> response, "# ntracks:", len(tracks)

        print >> response, "#"

        print >> response, "# Date Time Track MJD %s" % param.capitalize()

        for track in tracks:
            if param == 'period':
                if track.variability == 2:
                    time = track.satelliterecords_set.order_by(
                        'time').first().time
                    mjd = astropy.time.Time(time).mjd
                    print >> response, time, track.id, mjd, track.variability_period

    elif id == 'all':
        # records = SatelliteRecords.objects.order_by('track').order_by('time')

        # if not has_permission(request, 'fweb.access_satellites_all'):
        #     records = records.exclude(satellite__catalogue=2)
        #     records = records.exclude(satellite__country='CIS',satellite__country__isnull=False)

        # if request.method == 'GET' and request.GET.get('after'):
        #     records = records.filter(track__gt=request.GET.get('after'))

        # if request.method == 'GET' and request.GET.get('before'):
        #     records = records.filter(track__lt=request.GET.get('before'))

        # print >>response, "# Date Time StdMag Mag Filter Penumbra Distance Phase Channel Track Satellite"

        # for record in records:
        #     print >>response, record.time, record.stdmag, record.mag, filter_name(record.filter_id), record.penumbra, record.distance, record.phase, record.channel_id, record.track_id, record.satellite.catalogue_id

        tracks = SatelliteTracks.objects.order_by('id')
        if not has_permission(request, 'fweb.access_satellites_all'):
            tracks = tracks.exclude(satellite__catalogue=2)
            tracks = tracks.exclude(satellite__country='CIS',
                                    satellite__country__isnull=False)

        if request.method == 'GET' and request.GET.get('after'):
            tracks = tracks.filter(id__gt=request.GET.get('after'))

        if request.method == 'GET' and request.GET.get('before'):
            tracks = tracks.filter(id__lt=request.GET.get('before'))

        response[
            'Content-Disposition'] = 'attachment; filename=satellites_all.txt'

        print >> response, "# Date Time StdMag Mag Filter Penumbra Distance Phase Channel Track Satellite"

        for i, track in enumerate(tracks):
            #print i,'/',len(tracks)

            #records = SatelliteRecords.get(track__id=track).order_by('track').order_by('time')
            records = track.satelliterecords_set.order_by('time')

            for record in records:
                print >> response, record.time, record.stdmag, record.mag, filter_name(
                    record.filter_id
                ), record.penumbra, record.distance, record.phase, record.channel_id, record.track_id, track.satellite.catalogue_id

    elif id:
        try:
            satellite = Satellites.objects.get(id=id)
        except:
            raise Http404('No such satellite')

        records = satellite.satelliterecords_set.order_by('time')

        if satellite.catalogue == 2 or satellite.country == 'CIS':
            assert_permission(request, 'fweb.access_satellites_all')

        if request.method == 'GET' and request.GET.get('after'):
            records = records.filter(track__gt=request.GET.get('after'))

        if request.method == 'GET' and request.GET.get('before'):
            records = records.filter(track__lt=request.GET.get('before'))

        response[
            'Content-Disposition'] = 'attachment; filename=satellite_%s.txt' % id

        print >> response, "# satellite: %d %s / %s" % (
            satellite.catalogue_id, satellite.name, satellite.country)
        print >> response, "# catalogue:", satellite.catalogue, satellite_catalogue(
            satellite.catalogue)
        print >> response, "# variability:", satellite.variability, {
            0: 'Not variable',
            1: 'Aperiodic',
            2: 'Periodic'
        }.get(satellite.variability)
        print >> response, "# variability_period:", satellite.variability_period
        print >> response, "# npoints:", len(records)

        print >> response, "#"

        print >> response, "# Date Time StdMag Mag Filter Penumbra Distance Phase Channel Track"

        for record in records:
            print >> response, record.time, record.stdmag, record.mag, filter_name(
                record.filter_id
            ), record.penumbra, record.distance, record.phase, record.channel_id, record.track_id

    else:
        satellites = SatellitesView.objects.order_by('catalogue_id')

        if not has_permission(request, 'fweb.access_satellites_all'):
            satellites = satellites.exclude(catalogue=2)
            satellites = satellites.exclude(country='CIS',
                                            country__isnull=False)

        response[
            'Content-Disposition'] = 'attachment; filename=satellites_list.txt'

        print >> response, "# nsatellites:", len(satellites)

        print >> response, "#"

        print >> response, "# Catalogue Id Name Country Variability Period StdMag_Clear StdMag_Clear_Sigma StdMag_B StdMag_B_Sigma StdMag_V StdMag_V_Sigma StdMag_R StdMag_R_Sigma StdMag_Clear_Median StdMag_Clear_Min StdMag_Clear_Max"

        for satellite in satellites:
            satellite.country = satellite.country or None
            satellite.variability_period = satellite.variability_period or 0

            satellite.mean_clear = satellite.mean_clear or np.nan
            satellite.mean_b = satellite.mean_b or np.nan
            satellite.mean_v = satellite.mean_v or np.nan
            satellite.mean_r = satellite.mean_r or np.nan

            satellite.sigma_clear = satellite.sigma_clear or np.nan
            satellite.sigma_b = satellite.sigma_b or np.nan
            satellite.sigma_v = satellite.sigma_v or np.nan
            satellite.sigma_r = satellite.sigma_r or np.nan

            satellite.median_clear = satellite.median_clear or np.nan
            satellite.min_clear = satellite.min_clear or np.nan
            satellite.max_clear = satellite.max_clear or np.nan

            print >> response, satellite_catalogue(
                satellite.catalogue
            ), satellite.catalogue_id, '"%s"' % satellite.name, satellite.country, satellite.variability, satellite.variability_period, "%.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f" % (
                satellite.mean_clear, satellite.sigma_clear, satellite.mean_b,
                satellite.sigma_b, satellite.mean_v, satellite.sigma_v,
                satellite.mean_r, satellite.sigma_r, satellite.median_clear,
                satellite.min_clear, satellite.max_clear)

    return response
Beispiel #3
0
def realtime_object_details(request, object_id=0):
    if request.method == 'POST':
        assert_permission(request, 'fweb.modify_data')
        message = ""
        action = request.POST.get('action')
        r = None

        if action in [
                'process', 'process_1s', 'process_5s', 'process_10s', 'preview'
        ]:
            object = RealtimeObjects.objects.get(id=object_id)

            if action == 'process_1s':
                deltat = 1
            elif action == 'process_5s':
                deltat = 5
            elif action == 'process_10s':
                deltat = 10
            else:
                deltat = 0

            if action == 'preview':
                preview = True
            else:
                preview = False

            r = favor2_celery.processObjects.apply_async(kwargs={
                'id': object_id,
                'deltat': deltat,
                'reprocess': True,
                'preview': preview
            },
                                                         queue="channel%d" %
                                                         object.channel_id)

        elif action == 'delete' or action == 'delete_and_next':
            object = RealtimeObjects.objects.get(id=object_id)
            night = object.night
            state = object.state.name
            realtime_object_delete(id=object_id)
            next_state = request.POST.get('next_state')
            if action == 'delete' or not next_state:
                return redirect('/realtime/night/%s/%s' % (night, state))
            else:
                return redirect('realtime_object', object_id=int(next_state))

        elif action == 'comment-delete':
            for c in RealtimeComments.objects.filter(object_id=object_id):
                c.delete()

        elif action == 'comment':
            comment = request.POST.get('comment').strip()
            c, _ = RealtimeComments.objects.get_or_create(object_id=object_id)
            if comment:
                c.comment = comment
                c.save()
            else:
                c.delete()

        elif action == 'change_state':
            new_state = request.POST.get('new_state').strip()
            if new_state:
                realtime_object_change_state(object_id, new_state)
        elif action == 'change_state_particle':
            realtime_object_change_state(object_id, 'particle')
        elif action == 'change_state_misc':
            realtime_object_change_state(object_id, 'misc')

        if r:
            # Redirect to the task page
            return redirect('tasks_task', id=r.id)
        else:
            # Redirect to the same view, but with no POST args. We can't display messages with it!
            return redirect('realtime_object', object_id=object_id)

    object = RealtimeObjects.objects.annotate(
        nrecords=Count('realtimerecords')).get(id=object_id)

    context = {'object': object}

    next = RealtimeObjects.objects.filter(night=object.night).filter(
        id__lt=object.id).order_by('-time_start').order_by('-id').first()
    next_state = RealtimeObjects.objects.filter(
        night=object.night, state=object.state,
        id__lt=object.id).order_by('-time_start').order_by('-id').first()
    if next:
        context['next'] = next.id
    if next_state:
        context['next_state'] = next_state.id

    prev = RealtimeObjects.objects.filter(night=object.night).filter(
        id__gt=object.id).order_by('time_start').order_by('id').first()
    prev_state = RealtimeObjects.objects.filter(
        night=object.night, state=object.state,
        id__gt=object.id).order_by('time_start').order_by('id').first()
    if prev:
        context['prev'] = prev.id
    if prev_state:
        context['prev_state'] = prev_state.id

    images = RealtimeImages.objects.filter(
        object_id=object_id).order_by('time')
    if not images:
        records = RealtimeRecords.objects.filter(
            object_id=object_id).order_by('time')
        images = [{'record': r, 'time': r.time} for r in records]
        context['processed'] = False
    else:
        context['processed'] = True

    comments = RealtimeComments.objects.filter(object_id=object_id)
    if comments:
        context['comment'] = comments[0]

    if object.params.has_key('related'):
        related = object.params['related'].split()
        context['related'] = related

    avg = Images.objects.filter(type='avg').filter(
        channel_id=object.channel_id).filter(
            time__gt=object.time_start).order_by('time').first()
    if avg:
        context['avg_id'] = avg.id
    #context['avg_id'] = find_image_id(object.time_start, 'avg', object.channel_id)

    if object.channel_id:
        # Try to get direct links to RAW files
        raw_base = fix_remote_path(settings.BASE_RAW,
                                   channel_id=object.channel_id)
        raw_dirs = [
            d for d in os.listdir(raw_base)
            if os.path.isdir(posixpath.join(raw_base, d))
            and re.match('^\d{8}_\d{6}$', d)
        ]
        raw_dirs.sort()

        i0 = max(
            0,
            bisect.bisect_right(
                raw_dirs,
                get_dirname(object.time_start - datetime.timedelta(seconds=1)))
            - 1)
        i1 = bisect.bisect_right(
            raw_dirs,
            get_dirname(object.time_end + datetime.timedelta(seconds=1))) - 1
        raw_dirs = raw_dirs[i0:i1 + 1]

        for image in images:
            raw = None

            if raw_dirs:
                time = image['time'] if type(image) == dict else image.time
                filename = get_filename(time)

                for dir in raw_dirs:
                    idx = get_index(posixpath.join(raw_base, dir))

                    for i in idx:
                        if i['path'] == filename:
                            raw = posixpath.join(dir, i['path'])
                            break

                    if raw:
                        break

            if type(image) == dict:
                image['raw'] = raw
            else:
                image.raw = raw

        context['images'] = images

    return TemplateResponse(request, 'realtime_object.html', context=context)
Beispiel #4
0
def satellite_track_variability_plot(request,
                                     track_id=0,
                                     width=800,
                                     type='fold'):
    track = SatelliteTracks.objects.get(id=track_id)
    records = track.satelliterecords_set.order_by('time')

    if track.satellite.catalogue == 2:
        assert_permission(request, 'fweb.access_satellites_all')

    fig = Figure(facecolor='white',
                 figsize=(width / 72, width * 0.5 / 72),
                 tight_layout=True)
    ax = fig.add_subplot(111)
    ax.autoscale()

    time_start = satellite_track_start(track)
    time_end = satellite_track_end(track)
    T = (time_end - time_start).total_seconds()
    if track.variability == 2:
        P = track.variability_period
    else:
        P = None

    xs = []
    ys = []
    fs = []

    for r in records:
        x = (r.time - time_start).total_seconds()

        y = r.stdmag

        color = favor2_filters[
            r.filter_id] if r.filter_id else favor2_filters[0]
        if r.penumbra:
            f = color + [0.3]
        else:
            f = color + [1.0]

        xs.append(x)
        ys.append(y)
        fs.append(f)

    x = np.array(xs)
    y = np.array(ys)

    fit = np.polyfit(x, y, 2)
    y0 = np.polyval(fit, x)

    # ax.plot((0,0), (min(ys), max(ys)), c='lightgray')
    # ax.plot((track.variability_period,track.variability_period), (min(ys), max(ys)), c='lightgray')

    #ax.scatter(xs, ys, c=fs, marker='.', edgecolors=f)

    if type == 'fold' and P:
        #P = refine_period(x, y-y0, P, detrend=False)

        if int(request.GET.get('refine', 0)):
            P = refine_track_period(track)

        px = np.mod(x, P)

        ax.scatter(px, y - y0, c=fs, marker='.', edgecolors=fs)
        ax.scatter(px - P, y - y0, c=fs, marker='.', edgecolors=fs)
        ax.scatter(px + P, y - y0, c=fs, marker='.', edgecolors=fs)

        ax.axvline(0, c='gray', alpha=0.5)
        ax.axvline(P, c='gray', alpha=0.5)

        if P != track.variability_period:
            ax.set_xlabel(
                "Time, seconds, folded to period %g (original %g) s" %
                (P, track.variability_period))
        else:
            ax.set_xlabel("Time, seconds, folded to period %g s" % (P))

        ax.set_ylabel("Standard Magnitude")
        ax.invert_yaxis()
    elif type == 'ls':
        freq = np.arange(1.0 / T, 5.0, 1.0 / T)
        ls = signal.lombscargle(x, y, 2 * np.pi * freq)

        #ax.semilogx(freq, ls)
        ax.loglog(freq, ls, drawstyle='steps')

        if P:
            ax.axvline(1.0 / P, c='gray', alpha=0.5)
        ax.set_xlabel("Frequency, Hz")
        ax.set_ylabel("Lomb-Scargle periodogram")
    elif type == 'pdm':
        pp = np.arange(P * 0.9, P * 1.1, P * 0.001)
        pdm = PDM(x, y - y0, 1.0 / pp)

        ax.plot(pp, pdm, drawstyle='steps')
        ax.set_xlabel("Period, seconds")
        ax.set_ylabel("Phase Dispersion Minimization")

    ax.xaxis.set_major_formatter(ScalarFormatter(useOffset=False))
    ax.yaxis.set_major_formatter(ScalarFormatter(useOffset=False))

    #fig.autofmt_xdate()

    # 10% margins on both axes
    ax.margins(0.0, 0.1)

    time_start_str = time_start.strftime(
        '%Y-%m-%d %H:%M:%S') + '.%03d' % np.round(
            1e-3 * time_start.microsecond)

    ax.set_title(
        "%s %d / %s, track at %s" %
        (satellite_catalogue(track.satellite.catalogue),
         track.satellite.catalogue_id, track.satellite.name, time_start_str))

    canvas = FigureCanvas(fig)
    response = HttpResponse(content_type='image/png')
    canvas.print_png(response)

    return response
Beispiel #5
0
def satellite_plot(request, id=0, width=800, type='stdmag'):
    satellite = Satellites.objects.get(id=id)

    if satellite.catalogue == 2:
        assert_permission(request, 'fweb.access_satellites_all')

    x = []
    y = []
    f = []

    if type == 'period':
        tracks = satellite.satellitetracks_set.all()
        for t in tracks:
            if t.variability == 2:
                x.append(t.satelliterecords_set.all()[0].time)
                y.append(t.variability_period)
    else:
        records = satellite.satelliterecords_set.all()
        for r in records:
            if type == 'phase_stdmag':
                if r.penumbra:
                    continue
                x.append(r.phase)
            else:
                x.append(r.time)

            if type == 'stdmag' or type == 'phase_stdmag':
                y.append(r.stdmag)
            elif type == 'mag':
                y.append(r.mag)
            elif type == 'distance':
                y.append(r.distance)
            elif type == 'phase':
                y.append(r.phase)
            else:
                y.append(0)

            color = favor2_filters[
                r.filter_id] if r.filter_id else favor2_filters[0]
            if r.penumbra:
                f.append(color + [0.3])
            else:
                f.append(color + [1.0])

    fig = Figure(facecolor='white',
                 figsize=(width / 72, width * 0.5 / 72),
                 tight_layout=True)
    ax = fig.add_subplot(111)
    ax.autoscale()

    if type == 'period':
        ax.scatter(x, y, c='r', marker='o')
    else:
        ax.scatter(x, y, c=f, marker='.', edgecolors=f, s=1)

    if type == 'phase_stdmag':
        ax.xaxis.set_major_formatter(ScalarFormatter(useOffset=False))
        ax.set_xlabel("Phase angle, degrees")
    else:
        ax.xaxis.set_major_formatter(DateFormatter('%Y.%m.%d'))
        ax.set_xlabel("Time, UT")

    ax.yaxis.set_major_formatter(ScalarFormatter(useOffset=False))

    if type == 'stdmag' or type == 'phase_stdmag':
        ax.set_ylabel("Standard Magnitude")
        ax.invert_yaxis()
    elif type == 'mag':
        ax.set_ylabel("Apparent Magnitude")
        ax.invert_yaxis()
    elif type == 'distance':
        ax.set_ylabel("Distance, km")
    elif type == 'phase':
        ax.set_ylabel("Phase angle, degrees")
    elif type == 'period':
        ax.set_ylabel("Light curve period, s")

    fig.autofmt_xdate()

    # 10% margins on both axes
    ax.margins(0.1, 0.1)

    ax.set_title("%s %d / %s" % (satellite_catalogue(
        satellite.catalogue), satellite.catalogue_id, satellite.name))

    canvas = FigureCanvas(fig)
    response = HttpResponse(content_type='image/png')
    canvas.print_png(response)

    return response
Beispiel #6
0
def satellite_track_plot(request, track_id=0, width=800, type='stdmag'):
    if type in ['fold', 'ls', 'pdm']:
        return satellite_track_variability_plot(request, track_id, width, type)

    track = SatelliteTracks.objects.get(id=track_id)
    records = track.satelliterecords_set.order_by('time')

    if track.satellite.catalogue == 2:
        assert_permission(request, 'fweb.access_satellites_all')

    fig = Figure(facecolor='white',
                 figsize=(width / 72, width * 0.5 / 72),
                 tight_layout=True)
    ax = fig.add_subplot(111)
    ax.autoscale()

    #time_start = track.satelliterecords_set.aggregate(Min('record__time')).get('record__time__min')
    time_start = satellite_track_start(track)

    for channel_id in [
            r.channel_id
            for r in track.satelliterecords_set.distinct('channel_id')
    ]:
        records = track.satelliterecords_set.filter(
            channel_id=channel_id).order_by('time')

        xs = []
        ys = []
        x_prev = None
        f_prev = None
        channel_prev = None

        for r in records:
            x = (r.time - time_start).total_seconds()

            if type == 'stdmag':
                y = r.stdmag
            elif type == 'mag':
                y = r.mag
            elif type == 'distance':
                y = r.distance
            elif type == 'phase':
                y = r.phase
            else:
                y = 0

            color = favor2_filters[
                r.filter_id] if r.filter_id else favor2_filters[0]
            if r.penumbra:
                f = color + [0.3]
            else:
                f = color + [1.0]

            channel = r.channel_id

            if (x_prev and x - x_prev > 0.5) or (f_prev and f != f_prev) or (
                    channel_prev and channel != channel_prev):
                ax.plot(xs, ys, marker='.', color=f_prev, lw=0.1, ms=3)
                xs = []
                ys = []

            xs.append(x)
            ys.append(y)

            x_prev = x
            f_prev = f
            channel_prev = channel

        # Finalize
        if x and y and f_prev:
            ax.plot(xs, ys, marker='.', color=f_prev, lw=0.1, ms=3)

    #ax.scatter(x, y, c=f, marker='.', edgecolors=f)
    #ax.plot(x, y, marker='.')

    #ax.xaxis.set_major_formatter(DateFormatter('%Y.%m.%d %H:%M:%S'))
    #ax.xaxis.set_major_formatter(DateFormatter('%H:%M:%S'))
    ax.xaxis.set_major_formatter(ScalarFormatter(useOffset=False))
    ax.yaxis.set_major_formatter(ScalarFormatter(useOffset=False))

    ax.set_xlabel("Time since track start, seconds")
    if type == 'stdmag':
        ax.set_ylabel("Standard Magnitude")
        ax.invert_yaxis()
    elif type == 'mag':
        ax.set_ylabel("Apparent Magnitude")
        ax.invert_yaxis()
    elif type == 'distance':
        ax.set_ylabel("Distance, km")
    elif type == 'phase':
        ax.set_ylabel("Phase angle, degrees")

    #fig.autofmt_xdate()

    # 10% margins on both axes
    ax.margins(0.1, 0.1)

    time_start_str = time_start.strftime(
        '%Y-%m-%d %H:%M:%S') + '.%03d' % np.round(
            1e-3 * time_start.microsecond)

    ax.set_title(
        "%s %d / %s, track at %s" %
        (satellite_catalogue(track.satellite.catalogue),
         track.satellite.catalogue_id, track.satellite.name, time_start_str))

    canvas = FigureCanvas(fig)
    if request.GET.get('format', 'png') == 'pdf':
        response = HttpResponse(content_type='application/pdf')
        canvas.print_figure(response, format='pdf')
    elif request.GET.get('format', 'png') == 'eps':
        response = HttpResponse(content_type='application/postscript')
        canvas.print_figure(response, format='eps')
    else:
        response = HttpResponse(content_type='image/png')
        canvas.print_png(response)

    return response
Beispiel #7
0
def satellite_details(request, id=0, track_id=0, show_records=False):
    context = {}

    if request.method == 'GET':
        if request.GET.get('show_records'):
            show_records = request.GET.get('show_records')

    if request.method == 'POST':
        assert_permission(request, 'fweb.modify_satellites')
        if id:
            # Deleting satellite or its tracks
            action = request.POST.get('action')
            tracks = [
                int(track_id)
                for track_id in request.POST.getlist('track_ids[]')
            ]
            comments = request.POST.get('comments')

            if action == 'delete_tracks':
                print "Deleting tracks %s" % tracks
                for track in SatelliteTracks.objects.filter(id__in=tracks):
                    track.delete()

                update_satellite_variability(id)

                return redirect('satellite_details', id=id)

            elif action == 'delete_satellite':
                print "Deleting satellite %s" % id

                satellite = Satellites.objects.get(id=id)

                if satellite:
                    satellite.delete()

                return redirect('satellites_list')

            elif action == 'rename_satellite':
                new_name = request.POST.get('new_name')
                print "Renaming satellite"

                satellite = Satellites.objects.get(id=id)
                satellite.name = new_name
                satellite.save()

                return redirect('satellite_details', id=id)

            elif action == 'merge_satellite':

                new_cat = float(request.POST.get('new_cat'))

                if request.POST.get('new_catid') == '':
                    satellite = Satellites.objects.get(id=id)
                    print "Changing satellite %s catid to %d" % (satellite.id,
                                                                 new_cat)
                    satellite.catalogue = new_cat
                    satellite.save()
                else:
                    new_catid = float(request.POST.get('new_catid'))
                    new_satellite = Satellites.objects.get(
                        catalogue_id=new_catid, catalogue=new_cat)
                    satellite = Satellites.objects.get(id=id)

                    print "Merging satellite %s into %s" % (satellite.id,
                                                            new_satellite.id)

                    if satellite.id != new_satellite.id:
                        db_query(
                            "UPDATE satellite_tracks SET satellite_id=%s WHERE satellite_id=%s",
                            (new_satellite.id, satellite.id))
                        db_query(
                            "UPDATE satellite_records SET satellite_id=%s WHERE satellite_id=%s",
                            (new_satellite.id, satellite.id))

                        id = new_satellite.id
                        satellite.delete()
                        return redirect('satellite_details', id=id)

            elif action == 'change_satellite_type':
                new_type = int(request.POST.get('new_type'))
                satellite = Satellites.objects.get(id=id)
                satellite.type = new_type
                satellite.save()

            elif action == 'comments-update':
                satellite = Satellites.objects.get(id=id)
                satellite.comments = comments
                satellite.save()

                return redirect('satellite_details', id=id)

            elif action == 'comments-delete':
                satellite = Satellites.objects.get(id=id)
                satellite.comments = None
                satellite.save()

                return redirect('satellite_details', id=id)

        elif track_id:
            action = request.POST.get('action')
            new_variability = request.POST.get('new_variability')
            new_period = request.POST.get('new_period')
            comments = request.POST.get('comments')

            track = SatelliteTracks.objects.get(id=track_id)
            id = track.satellite_id

            if action == 'update_track':
                track.variability = int(new_variability)
                track.variability_period = float(
                    new_period) if new_period else 0.0
                track.save()

                update_satellite_variability(id)

                return redirect('satellite_track', track_id=track_id)
            if action == 'refine_track_period':
                track.variability_period = refine_track_period(track)
                track.save()

                update_satellite_variability(id)

                return redirect('satellite_track', track_id=track_id)
            elif action == 'delete_track':
                track.delete()
                update_satellite_variability(id)

                return redirect('satellite_details', id=id)

            elif action == 'comments-update':
                satellite = Satellites.objects.get(id=id)
                satellite.comments = comments
                satellite.save()

                return redirect('satellite_track', track_id=track_id)

            elif action == 'comments-delete':
                satellite = Satellites.objects.get(id=id)
                satellite.comments = None
                satellite.save()

                return redirect('satellite_track', track_id=track_id)

    context['show_records'] = show_records

    context['nsatellites'] = Satellites.objects.count()
    context['ntracks'] = SatelliteTracks.objects.count()
    context['time_min'] = SatelliteRecords.objects.aggregate(
        Min('time'))['time__min']
    context['time_max'] = SatelliteRecords.objects.aggregate(
        Max('time'))['time__max']

    if track_id:
        track = SatelliteTracks.objects.get(id=track_id)
        satellite = track.satellite
        context['track'] = track
        if show_records:
            records = track.satelliterecords_set.order_by('time')
            context['records'] = records

        context['track_filters'] = [
            x.filter.name
            for x in track.satelliterecords_set.distinct('filter')
        ]
        context['track_objects'] = [
            x.object_id for x in track.satelliterecords_set.distinct('object')
        ]
        context['track_channels'] = [
            x.channel_id
            for x in track.satelliterecords_set.distinct('channel_id')
        ]

        context['tle'] = parse_tle(track.tle)
    else:
        satellite = Satellites.objects.get(id=id)
        tracks = satellite.satellitetracks_set.order_by('id')
        tracks = tracks.annotate(time_min=Min('satelliterecords__time'),
                                 time_max=Max('satelliterecords__time'))
        #tracks = tracks.extra(select={'stdmag_mean': 'SELECT avg(stdmag) FROM satellite_records WHERE track_id=satellite_tracks.id AND penumbra = 0'})
        context['tracks'] = tracks

    context['norad'] = satellite.id
    context['name'] = satellite.name

    context['satellite'] = satellite

    if satellite.catalogue == 2 or satellite.country == 'CIS':
        assert_permission(request, 'fweb.access_satellites_all')

    return TemplateResponse(request, 'satellites.html', context=context)
Beispiel #8
0
def tasks_view(request, id=''):
    context = {}

    if request.method == 'POST':
        assert_permission(request, 'fweb.manage_tasks')

        action = request.POST.get('action')

        if action == 'terminatealltasks':
            management.call_command('terminatealltasks')
            return redirect('tasks_list')

        if action == 'processall':
            management.call_command('processall')
            return redirect('tasks_list')

        if action == 'extractstars':
            management.call_command('extractstars')
            return redirect('tasks_list')

        if action == 'findtransients':
            management.call_command('findtransients')
            return redirect('tasks_list')

        if action == 'terminatetask' and id:
            revoke(id, terminate=True, signal='SIGKILL')
            return redirect('tasks_task', id=id)

        if action == 'uploadsatellites':
            management.call_command('uploadsatellites')
            return redirect('tasks_list')

    if id:
        task = app.AsyncResult(id)
        meta = app.backend.get_task_meta(id)
        formatted = None

        if type(task.result) == list or type(task.result) == tuple:
            is_first = True
            formatted = "<table class='table table-striped table-condensed'>"

            for row in task.result:
                formatted = formatted + "<tr>"

                if isinstance(row, psycopg2.extras.DictRow):
                    if is_first:
                        is_first = False

                        for col in row.iteritems():
                            formatted = formatted + "<th>%s</th>" % col[0]

                        formatted = formatted + "</tr><tr>"

                    for col in row.iteritems():
                        formatted = formatted + "<td>%s</td>" % col[1]

                elif type(row) == list or type(row) == tuple:
                    for col in row:
                        formatted = formatted + "<td>%s</td>" % col
                else:
                    formatted = formatted + "<td>%s</td>" % row

                formatted = formatted + "</tr>"

            formatted = formatted + "</table>"

        context = {'task': task, 'meta': meta, 'formatted': formatted}
    else:
        tasks = []

        inspect = app.control.inspect()

        for w in inspect.active().values():
            for t in w:
                t['state'] = 'active'
                if t.has_key('name'):
                    t['shortname'] = t['name'].split('.')[-1]
                tasks.append(t)

        for w in inspect.reserved().values():
            for t in w:
                t['state'] = 'active'
                if t.has_key('name'):
                    t['shortname'] = t['name'].split('.')[-1]
                tasks.append(t)

        for w in inspect.scheduled().values():
            for t0 in w:
                t = t0['request']
                t['state'] = 'scheduled'
                if t.has_key('name'):
                    t['shortname'] = t['name'].split('.')[-1]
                    if t['name'] == 'celery.chord_unlock':
                        t['args'] = ''
                        t['kwargs'] = ''

                tasks.append(t)

        context = {'tasks': tasks}

    return TemplateResponse(request, 'tasks.html', context=context)