コード例 #1
0
ファイル: gz_classes.py プロジェクト: willettk/gz_nodes_flask
 def get_nearest_obj(self,ra_in,dec_in,size=2):
     a=self.BC.assets
     #find a bounding box to speed up search (size depended on search regon)
     if (ra_in<60) or (ra_in>308.5):
         if (dec_in+size)<-1:
             size=abs(dec_in+1)
         elif (dec_in-size)>1:
             size=abs(dec_in-1)
     else:
         if (dec_in+size)<-3:
             size=abs(dec_in+3)
         elif (dec_in-size)>76:
             size=abs(dec_in-76)
     box=geo_bounding_box(ra_in,dec_in,size=size)
     #this distance is correct up to multiplications factors (order is correct)
     dis=func.asin(func.sqrt(func.power(func.sin(0.5*func.radians(dec_in-a.dec)),2) + func.cos(func.radians(dec_in))*func.cos(func.radians(a.dec))*func.power(func.sin(.5*func.radians(ra_in-a.ra)),2)))
     if isinstance(box[0],list):
         #the search wraps around 360
        result=self.session.query(a.name,a.id,a.ra,a.dec,a.location).filter(between(a.dec,box[1][0],box[1][1])).filter((between(a.ra,box[0][0][0],box[0][0][1]))|(between(a.ra,box[0][1][0],box[0][1][1]))).order_by(dis.asc()).first()
     else:
         result=self.session.query(a.name,a.id,a.ra,a.dec,a.location).filter(between(a.ra,box[0][0],box[0][1])).filter(between(a.dec,box[1][0],box[1][1])).order_by(dis.asc()).first()
     #result=self.session.query(a.name,a.id,a.ra,a.dec,a.location).order_by(dis.asc()).first()
     if result is None:
         result=self.get_nearest_obj(ra_in,dec_in,size=size+20)
     return result
コード例 #2
0
def average_composition(queryset, **kwargs):
    """
    The average oxide composition of a queryset, calculated in
    one of several ways (either wt%, molar%, or normalized wt% abundances).
    Returns the same result as the partial functions above, but is a bit
    more descriptive.

    :param type: Options
        - weight              Oxide weight % (default)
        - normalized_weight   Oxide weight % (normalized to 100% abundances)
        - molar               Oxide molar %
    :param uncertainties:     Whether to include uncertainties (default FALSE)
    """
    mapping = dict(weight=ProbeDatum.weight_percent,
                   normalized_weight=ProbeDatum.weight_percent * 100 /
                   ProbeMeasurement.oxide_total,
                   molar=ProbeDatum.molar_percent)

    type = kwargs.pop("type", "weight")

    try:
        quantity = mapping[type]
    except KeyError:
        raise ArgumentError("Parameter `type` must be either"
                            "`weight`,`molar`, or `normalized_weight`")

    try:
        q = queryset.with_entities(ProbeMeasurement.id)
        filt = ProbeDatum.measurement_id.in_(q)
    except AttributeError:
        filt = ProbeDatum.measurement_id == queryset.id

    average = func.avg(quantity)
    qvars = [ProbeDatum._oxide, average.label('avg')]

    uncertainties = kwargs.pop("uncertainties", False)
    if uncertainties:
        e = ProbeDatum.error
        sum_ = func.sum(func.pow(e / 100 * quantity, 2))
        std = func.sqrt(sum_) / func.count("*")
        qvars.append(std.label("analytic_std"))

        std2 = func.stddev(quantity)

        qvars.append(std2.label("sample_std"))

    data = db.session.query(ProbeDatum)\
        .join(ProbeMeasurement)\
        .filter(filt)\
        .with_entities(*tuple(qvars))\
        .group_by(ProbeDatum._oxide)\
        .all()

    if uncertainties:
        try:
            return {o: ufloat(n, a + s) for o, n, a, s in data}
        except TypeError:
            return {o: ufloat(n, a) for o, n, a, s in data}
    else:
        return {o: n for o, n in data}
コード例 #3
0
conn = engine.connect()

c = [

    ##  date/time functions  ##

    #func.timeofday(), # for postgresql
    func.localtime(),
    func.current_timestamp(),
    #func.date_part("month", func.now()),        # for postgresql
    func.now(),

    ##  mathematical functions  ##
    func.pow(4, 2),
    func.sqrt(441),
    func.pi(),
    func.floor(func.pi()),
    func.ceil(func.pi()),

    ##  string functions  ##
    func.lower("ABC"),
    func.upper("abc"),
    func.length("abc"),
    func.trim("  ab c  "),
    #func.chr(65),        # for postgresql
]

s = select(c)
rs = conn.execute(s)
print(rs.keys())
コード例 #4
0
ファイル: gz_classes.py プロジェクト: willettk/gz_nodes_flask
 def get_nearest_obj(self,ra_in,dec_in,size=5):
     a=self.BC.assets
     #this distance is correct up to multiplications factors (order is correct)
     dis=func.asin(func.sqrt(func.power(func.sin(0.5*func.radians(dec_in-a.dec)),2) + func.cos(func.radians(dec_in))*func.cos(func.radians(a.dec))*func.power(func.sin(.5*func.radians(ra_in-a.ra)),2)))
     result=self.session.query(a.name,a.id,a.ra,a.dec,a.location).order_by(dis.asc()).first()
     return result