def get_sources(formdata,request,imageheaders): ra = formdata['ra'] dec = formdata['dec'] radius = formdata['radius'] units = formdata['units'] n_bands = formdata['n_bands'] if not imageheaders: raise NoCoverageError(radius=radius,units='arcseconds') Qs = [Q(user=request.user), Q(user__username='******')] q = reduce(operator.or_, Qs) results = AstroSource.objects.filter(q).annotate(nbands=Count('photometry__BAND',distinct=True)).positionFilter(ra,dec,radius=radius) results = [r for r in results if r.nbands >= n_bands or r.user==request.user] if formdata['forcedetect']: if 0.0 in [r.distance for r in results if r.user==request.user]: #Should let the user know there is already a source there! pass else: fields = {} sexRa,sexDec = deg2sex.main(ra,dec) fields['sourceID'] = 'uGROND_J%s%s' % (sexRa,sexDec) fields['RA'] = ra fields['DEC'] = dec fields['user'] = request.user s = AstroSource(**fields) s.save() s.distance = 0.0 results.append(s) if not results: #raise NoCoverageError(radius=radius) return {'no_results':True} distances = dict((i.sourceID,i.distance*3600) for i in results) #Keep distance for later ownership = dict((i.sourceID,i.user.username) for i in results) #Filter based on sourceID (chaining Q functions) # if results: # Qs = [Q(astrosource__sourceID=i.sourceID) for i in results] # q = reduce(operator.or_, Qs) # results = list(Photometry.objects.filter(q)) #group results by sourceID sources = [] for r in results: sources.append( {'name':r.sourceID,'distance':distances[r.sourceID],'ownership':ownership[r.sourceID]} ) sources = sorted(sources,key=lambda k: k['distance']) return {'sources':sources}
def _make_AstroSource(self): all_sources = self.resultfile.objects[:] print " (detected %s sources in this resultfile)" % len(all_sources) #Compare the sources detected in this resultfile the database. If sourceID already exists, we shouldn't re-write it! self.old_sources = [] sources_to_remove = [] for source in all_sources: r = sorted(AstroSource.objects.positionFilter(source['RA'],source['DEC'],self.args['match_tolerance']),key=lambda k: k.distance) if r: #Even if this source already exists, this may be a new observation of it #Therefore, we need to check also the ImageHeader(s) of this source, and #Add this one in if it doesnt exist. this_source = r[0] if self.imageheader not in this_source.imageheader.all(): this_source.imageheader.add(self.imageheader) this_source.save() self.old_sources.append(this_source) sources_to_remove.append(source) all_sources = [i for i in all_sources if i not in sources_to_remove] print " (after removal of sources already in the database, %s new sources remain)" % len(all_sources) #Finally, make the Django models and save to DB self.new_sources = [] for source in all_sources: fields = {} fields['user'] = self.user fields['RA'] = source['RA'] fields['DEC'] = source['DEC'] sexRa,sexDec = deg2sex.main(source['RA'],source['DEC']) fields['sourceID'] = 'GROND_J%s%s' % (sexRa,sexDec) d = AstroSource(**fields) d.save() d.imageheader.add(self.imageheader) self.new_sources.append(d) [i.save() for i in self.new_sources] self.sources = [] self.sources.extend(self.old_sources) self.sources.extend(self.new_sources)