Exemplo n.º 1
0
def setupResults(sql):
    # rather than use the sql2Q method:
    #q = sqlparse.sql2Q(sql)

    start_time = time.time()

    # ... we parse the query into its restrictables and logic:
    if not sql.where:
        return {}
    logic, rs, count = sqlparse.splitWhere(sql.where)
    # and replace any restrictions on ChemicalName or StoichiometricFormula
    # with the equivalent on MoleculeInchiKey. Also convert wavelength
    # (in Angstroms) to wavenumber:
    for i in rs:
        r, op, foo = rs[i][0], rs[i][1], rs[i][2:]
        if r == 'MoleculeChemicalName':
            rs[i] = ChemicalName2MoleculeInchiKey(op, foo)
        if r == 'MoleculeStoichiometricFormula':
            rs[i] = StoichiometricFormula2MoleculeInchiKey(op, foo)
        if r == 'RadTransWavelength':
            rs[i] = Wavelength2Wavenumber(op, foo)
        if r == 'Pressure':
            rs[i] = Pascals2Torr(op, foo)
    # now rebuild the query as a dictionary of QTypes ...
    qdict = {}
    for i, r in rs.items():
        q = sqlparse.restriction2Q(r)
        qdict[i] = q
    # ... and merge them to a single query object
    q = sqlparse.mergeQwithLogic(qdict, logic)

    if 'radiativecrosssections' in sql.requestables:
        return setupXsecResults(q)
    
    transitions = Trans.objects.filter(q).order_by('nu')
    ntrans = transitions.count()
    if settings.TRANSLIM is not None and ntrans > settings.TRANSLIM:
        # we need to filter transitions again later, so can't take a slice
        #transitions = transitions[:settings.TRANSLIM]
        # so do this:
        numax = transitions[settings.TRANSLIM].nu
        transitions = Trans.objects.filter(q, Q(nu__lte=numax))
        percentage = '%.1f' % (float(settings.TRANSLIM)/ntrans * 100)
        ntrans = transitions.count()
        print 'Results truncated to %s %%' % percentage
    else:
        percentage = '100'
        print 'Results not truncated'
    print 'TRANSLIM is', settings.TRANSLIM
    print 'ntrans =',ntrans

    ts = time.time()
    ref_ids = attach_prms(transitions)
    te = time.time()
    print 'time to attach parameters = %.1f secs' % (te-ts)

    ts = time.time()
    sources = get_sources(ref_ids)
    te = time.time()
    print 'time to get sources = %.1f secs' % (te-ts)
    # extract the state quantum numbers in a form that generators.py can use:
    ts = time.time()
    species, nspecies, nstates = get_species(transitions)
    te = time.time()
    print 'time to get species = %.1f secs' % (te-ts)
    LOG('%s transitions retrieved from HITRAN database' % ntrans)
    LOG('%s states retrieved from HITRAN database' % nstates)
    LOG('%s species retrieved from HITRAN database' % nspecies)

    nsources = 0
    if sources is not None:
        # sources is a Python list, not a queryset, so we can't use count()
        nsources = len(sources)
    print 'nsources =', nsources
    print 'nspecies =', nspecies
    print 'nstates =', nstates
    print 'ntrans =', ntrans

    headerinfo = {
        'Truncated': '%s %%' % percentage,
        'count-sources': nsources,
        'count-species': nspecies,
        'count-molecules': nspecies,
        'count-states': nstates,
        'count-radiative': ntrans
    }

    methods = [Method('MEXP', 'experiment', 'experiment'),
               Method('MTHEORY', 'theory', 'theory')]

    end_time = time.time()
    print 'timed at %.1f secs' % (end_time - start_time)

   # return the dictionary as described above
    return {'HeaderInfo': headerinfo,
            'Methods': methods,
            'RadTrans': transitions,
            'Sources': sources,
            'Molecules': species,
            'Environments': HITRANenvs,
            'Functions': HITRANfuncs}
Exemplo n.º 2
0
 LOG(sql)
 result_sources = []  # Sources related to results
 methods = []  # Methods related to results
 nmMethod = None
 # convert the incoming sql to a correct django query syntax object
 # based on the RESTRICTABLES dictionary in dictionaries.py
 # (where2q is a helper function to do this for us).
 q = where2q(sql.where, dictionaries.RESTRICTABLES)
 try:
     q = eval(q)  # test queryset syntax validity
 except Exception, e:
     return {}
 # ... we parse the query into its restrictables and logic:
 if not sql.where:
     return {}
 logic, rs, count = splitWhere(sql.where)
 # Convert MoleculeNormalModeHarmonicFrequency
 # from MHz to cm-1:
 for i in rs:
     r, op, foo = rs[i][0], rs[i][1], rs[i][2:]
     if r == "MoleculeNormalModeHarmonicFrequency":
         rs[i] = MoleculeNormalModeHarmonicFrequency_MHz2cm(op, foo)
 # now rebuild the query as a dictionary of QTypes ...
 qdict = {}
 for i, r in rs.items():
     q = restriction2Q(r)
     qdict[i] = q
 # ... and merge them to a single query object
 q = mergeQwithLogic(qdict, logic)
 # We build a queryset of database matches on the Transision model
 # since through this model (in our example) we are be able to
Exemplo n.º 3
0
def setupResults(sql):
    # rather than use the sql2Q method:
    #q = sqlparse.sql2Q(sql)

    start_time = time.time()

    # ... we parse the query into its restrictables and logic:
    if not sql.where:
        return {}
    logic, rs, count = sqlparse.splitWhere(sql.where)
    # and replace any restrictions on ChemicalName or StoichiometricFormula
    # with the equivalent on MoleculeInchiKey. Also convert wavelength
    # (in Angstroms) to wavenumber:
    for i in rs:
        r, op, foo = rs[i][0], rs[i][1], rs[i][2:]
        if r == 'MoleculeChemicalName':
            rs[i] = ChemicalName2MoleculeInchiKey(op, foo)
        if r == 'MoleculeStoichiometricFormula':
            rs[i] = StoichiometricFormula2MoleculeInchiKey(op, foo)
        if r == 'RadTransWavelength':
            rs[i] = Wavelength2Wavenumber(op, foo)
        if r == 'Pressure':
            rs[i] = Pascals2Torr(op, foo)
    # now rebuild the query as a dictionary of QTypes ...
    qdict = {}
    for i, r in rs.items():
        q = sqlparse.restriction2Q(r)
        qdict[i] = q
    # ... and merge them to a single query object
    q = sqlparse.mergeQwithLogic(qdict, logic)

    if 'radiativecrosssections' in sql.requestables:
        return setupXsecResults(q)

    transitions = Trans.objects.filter(q).order_by('nu')
    ntrans = transitions.count()
    if settings.TRANSLIM is not None and ntrans > settings.TRANSLIM:
        # we need to filter transitions again later, so can't take a slice
        #transitions = transitions[:settings.TRANSLIM]
        # so do this:
        numax = transitions[settings.TRANSLIM].nu
        transitions = Trans.objects.filter(q, Q(nu__lte=numax))
        percentage = '%.1f' % (float(settings.TRANSLIM) / ntrans * 100)
        ntrans = transitions.count()
        print 'Results truncated to %s %%' % percentage
    else:
        percentage = '100'
        print 'Results not truncated'
    print 'TRANSLIM is', settings.TRANSLIM
    print 'ntrans =', ntrans

    ts = time.time()
    ref_ids = attach_prms(transitions)
    te = time.time()
    print 'time to attach parameters = %.1f secs' % (te - ts)

    ts = time.time()
    sources = get_sources(ref_ids)
    te = time.time()
    print 'time to get sources = %.1f secs' % (te - ts)
    # extract the state quantum numbers in a form that generators.py can use:
    ts = time.time()
    species, nspecies, nstates = get_species(transitions)
    te = time.time()
    print 'time to get species = %.1f secs' % (te - ts)
    LOG('%s transitions retrieved from HITRAN database' % ntrans)
    LOG('%s states retrieved from HITRAN database' % nstates)
    LOG('%s species retrieved from HITRAN database' % nspecies)

    nsources = 0
    if sources is not None:
        # sources is a Python list, not a queryset, so we can't use count()
        nsources = len(sources)
    print 'nsources =', nsources
    print 'nspecies =', nspecies
    print 'nstates =', nstates
    print 'ntrans =', ntrans

    headerinfo = {
        'Truncated': '%s %%' % percentage,
        'count-sources': nsources,
        'count-species': nspecies,
        'count-molecules': nspecies,
        'count-states': nstates,
        'count-radiative': ntrans
    }

    methods = [
        Method('MEXP', 'experiment', 'experiment'),
        Method('MTHEORY', 'theory', 'theory')
    ]

    end_time = time.time()
    print 'timed at %.1f secs' % (end_time - start_time)

    # return the dictionary as described above
    if (ntrans > 0 or nstates > 0 or nspecies > 0):
        return {
            'HeaderInfo': headerinfo,
            'Methods': methods,
            'RadTrans': transitions,
            'Sources': sources,
            'Molecules': species,
            'Environments': HITRANenvs,
            'Functions': HITRANfuncs
        }
    # return an empty dictionary to trigger a 204 if there's no data
    return {}
Exemplo n.º 4
0
 LOG(sql)
 result_sources = []  #Sources related to results
 methods = []  #Methods related to results
 nmMethod = None
 # convert the incoming sql to a correct django query syntax object
 # based on the RESTRICTABLES dictionary in dictionaries.py
 # (where2q is a helper function to do this for us).
 q = where2q(sql.where, dictionaries.RESTRICTABLES)
 try:
     q = eval(q)  # test queryset syntax validity
 except Exception, e:
     return {}
 # ... we parse the query into its restrictables and logic:
 if not sql.where:
     return {}
 logic, rs, count = splitWhere(sql.where)
 #Convert MoleculeNormalModeHarmonicFrequency
 # from MHz to cm-1:
 for i in rs:
     r, op, foo = rs[i][0], rs[i][1], rs[i][2:]
     if r == 'MoleculeNormalModeHarmonicFrequency':
         rs[i] = MoleculeNormalModeHarmonicFrequency_MHz2cm(op, foo)
 # now rebuild the query as a dictionary of QTypes ...
 qdict = {}
 for i, r in rs.items():
     q = restriction2Q(r)
     qdict[i] = q
 # ... and merge them to a single query object
 q = mergeQwithLogic(qdict, logic)
 # We build a queryset of database matches on the Transision model
 # since through this model (in our example) we are be able to