Exemple #1
0
def setupResults(sql):
    q = sql2Q(sql)

    states = models.State.objects.select_related().filter(q)
    species = None

    return {'Atoms':species,
           }
Exemple #2
0
def setupResults(sql):
    """
    This function is always called by the NodeSoftware.
    """
    # log the incoming query
    log.debug(sql)

    # convert the incoming sql to a correct django query syntax object
    # (sql2Q is a helper function to do this for us).
    q = sql2Q(sql)
    import sys
    log.debug( "QQQQQ"+str(q) )
    # We build a queryset of database matches on the Transision model
    # since through this model (in our example) we are be able to
    # reach all other models. Note that a queryset is actually not yet
    # hitting the database, making it very efficient.
    species = models.VamdcSpecies.objects.filter(q)

    # count the number of matches, make a simple trunkation if there are
    # too many (record the coverage in the returned header)
##    ntranss=transs.count()
##    if LIMIT < ntranss :
##        transs = transs[:limit]
##        percentage='%.1f' % (float(limit) / ntranss * 100)
##    else:
##        percentage=None
    
    # Through the transition-matches, use our helper functions to extract
    # all the relevant database data for our query.
#    sources = getRefs(transs)
#    nsources = sources.count()
#    species, nspecies, nstates = getSpeciesWithStates(transs)
#    methods = getLifetimeMethods()

    percentage = 100
    nsources = 0
    nspecies = species.count()
    nstates = 0
    ntranss = 0
    molecules = species.filter(species_type=models.SpeciesType.MOLECULE)
    atoms = species.filter(species_type=models.SpeciesType.ATOM)
    
    # Create the header with some useful info. The key names here are
    # standardized and shouldn't be changed.
    headerinfo = {'TRUNCATED':percentage,
                  'COUNT-SOURCES':nsources,
                  'COUNT-SPECIES':nspecies,
                  'COUNT-STATES':nstates,
                  'COUNT_RADIATIVE':ntranss,
                  'APPROX-SIZE':nspecies*0.001 }

    # Return the data. The keynames are standardized.
    return {'Atoms':atoms,
            'Molecules':molecules,
            #'Sources':sources,
            'HeaderInfo':headerinfo,
            #'Methods':methods
            }
Exemple #3
0
def query(sql, limit):

    # log the incoming query
    logging.debug(sql)

    # convert the incoming sql to a correct django query syntax object
    # based on the RESTRICTABLES dictionary in dictionaries.py
    q = sql2Q(sql)

    if constraintsPresent(sql) or transitionsRequired(sql) or statesRequired(
            sql):
        species, nstates, transs, percentage = genericQuery(sql, q, limit)
        nspecies = species.count()
        ntranss = transs.count()
    else:
        species = allSpeciesQuery(sql, q, limit)
        nspecies = species.count()
        nstates = 0
        ntranss = 0
        transs = {}
        percentage = None

    #sources = getSources(species)
    #sources = models.Sources.objects.all()
    sources = None

    # Adjust the counts of things returned according to the requestables.
    # The caller will choose what actually to return, but we have to set
    # the counts in the header ourselves.
    if not transitionsRequired(sql):
        ntranss = 0
    if not statesRequired(sql):
        nstates = 0

    # Create the header with some useful info. The key names here are
    # standardized and shouldn't be changed.
    headerinfo={\
            'Truncated':percentage,
            'COUNT-species':nspecies,
            'count-states':nstates,
            'count-radiative':ntranss
            }
    logging.debug(headerinfo)

    # Return the data. The keynames are standardized.
    if (nspecies > 0 or nstates > 0 or ntranss > 0):
        return {
            'RadTrans': transs,
            'Atoms': species,
            'HeaderInfo': headerinfo,
            'Methods': getMethods(),
            'Sources': sources
        }

    # As a special case, if there are no data, return an empty structure.
    # This causes the node software to send a 204 "No content" response.
    else:
        return {}
Exemple #4
0
def setupVssRequest(sql, limit=10000):
    """		
        Execute a vss request
        @type  sql: string
        @param sql: vss request
        @rtype:   util_models.Result
        @return:  Result object		
    """
    # convert the incoming sql to a correct django query syntax object 
    # based on the RESTRICTABLES dictionary in dictionaries.py
    q = sql2Q(sql)  
    transs = django_models.Collisionaltransition.objects.filter(q)
    # count the number of matches, make a simple trunkation if there are
    # too many (record the coverage in the returned header)
    ncoll=transs.count()
    if limit < ncoll :
        transs, percentage = truncateTransitions(transs, q, limit)
    else:
        percentage=None

    species, nstates, sourceids = getSpeciesWithStates(transs)
    # electron collider
    particles = getParticles()

    # cross sections
    states = []
    for specie in species:
        states.extend(specie.States)
    nspecies = species.count()

    nspecies = species.count()
    sources = getSources(sourceids)
    nsources = sources.count()

    # Create the result object
    result = util_models.Result()
    result.addHeaderField('TRUNCATED', percentage)
    result.addHeaderField('COUNT-STATES',nstates)
    result.addHeaderField('COUNT-COLLISIONS',ncoll)
    result.addHeaderField('COUNT-SPECIES',nspecies)
    result.addHeaderField('COUNT-SOURCES',nsources)
    
    if LAST_MODIFIED is not None : 
      result.addHeaderField('LAST-MODIFIED',LAST_MODIFIED)
    

    if(nstates == 0 and nspecies == 0):
        result.addHeaderField('APPROX-SIZE', 0)
    
    if ncoll > 0 :
        result.addDataField('CollTrans',transs)
        result.addDataField('Particles',particles)
    result.addDataField('Atoms',species)
    result.addDataField('Sources',sources)
    

    return result
Exemple #5
0
def query(sql, limit):

    # log the incoming query
    LOG(sql)

    # convert the incoming sql to a correct django query syntax object 
    # based on the RESTRICTABLES dictionary in dictionaries.py
    q = sql2Q(sql)

    if constraintsPresent(sql) or transitionsRequired(sql) or statesRequired(sql):
        species, nstates, transs, percentage = genericQuery(sql, q, limit)
        nspecies = species.count()
        ntranss = transs.count()
    else:
        species = allSpeciesQuery(sql, q, limit)
        nspecies = species.count()
        nstates = 0
        ntranss = 0
        transs = {}
        percentage = None
    
    sources = getSources(species)
    #sources = models.Sources.objects.all()
    #sources = None;

    # Adjust the counts of things returned according to the requestables.
    # The caller will choose what actually to return, but we have to set
    # the counts in the header ourselves.
    if not transitionsRequired(sql):
        ntranss = 0
    if not statesRequired(sql):
        nstates = 0


    # Create the header with some useful info. The key names here are
    # standardized and shouldn't be changed.
    headerinfo={\
            'Truncated':percentage,
            'COUNT-species':nspecies,
            'count-states':nstates,
            'count-radiative':ntranss
            }
    LOG(headerinfo)
            
    # Return the data. The keynames are standardized.
    if (nspecies > 0 or nstates > 0 or ntranss > 0):
        return {'RadTrans'  : transs,
                'Atoms'     : species,
                'HeaderInfo': headerinfo,
                'Methods'   : getMethods(),
                'Sources'   : sources
               }

    # As a special case, if there are no data, return an empty structure.
    # This causes the node software to send a 204 "No content" response.
    else:
        return {}
Exemple #6
0
def setupResults(sql):
    q = sql2Q(sql)

    states = models.State.objects.select_related().filter(q)
    species = None

    return {
        'Atoms': species,
    }
Exemple #7
0
def setupVssRequest(sql, limit=500):
    """		
        Execute a vss request
        @type  sql: string
        @param sql: vss request
        @rtype:   util_models.Result
        @return:  Result object		
    """

    result = util_models.Result()
    q = sql2Q(sql)    

    #select transitions : combination of density/temperature
    transs = django_models.Transition.objects.filter(q)
    ntranss=transs.count()

    if limit < ntranss :
        transs, percentage = truncateTransitions(transs, q, limit)
    else:
        percentage=None 
        
    # Through the transition-matches, use our helper functions to extract 
    # all the relevant database data for our query. 
    if ntranss > 0 :	
        species, nspecies, nstates = getSpeciesWithStates(transs)      
        transitions, environments = getTransitionsData(transs)    
        particles = getParticles(ntranss) 
        functions = getFunctions()
        sources =  getSources(transs)
        nsources = len(sources)
        

        # Create the header with some useful info. The key names here are
        # standardized and shouldn't be changed.
        result.addHeaderField('TRUNCATED',percentage)
        result.addHeaderField('COUNT-SPECIES',nspecies)
        result.addHeaderField('COUNT-STATES',nstates)
        result.addHeaderField('COUNT-RADIATIVE',len(transitions))	
        
        if LAST_MODIFIED is not None : 
          result.addHeaderField('LAST-MODIFIED',LAST_MODIFIED)
       
        result.addDataField('RadTrans',transitions)        
        result.addDataField('Atoms',species)
        result.addDataField('Environments',environments)
        result.addDataField('Particles',particles)
        #~ result.addDataField('Functions',functions)
        result.addDataField('Sources',sources)
        
    else : # only fill header
        result.addHeaderField('APPROX-SIZE', 0)    
        result.addHeaderField('TRUNCATED',percentage)
        result.addHeaderField('COUNT-STATES',0)
        result.addHeaderField('COUNT-RADIATIVE',0)
    return result	
Exemple #8
0
def setupVssRequest(sql, limit=10000):
    """		
        Execute a vss request
        @type  sql: string
        @param sql: vss request
        @rtype:   util_models.Result
        @return:  Result object		
    """
    # convert the incoming sql to a correct django query syntax object
    # based on the RESTRICTABLES dictionary in dictionaries.py
    q = sql2Q(sql)
    transs = django_models.Collisionaltransition.objects.filter(q)
    # count the number of matches, make a simple trunkation if there are
    # too many (record the coverage in the returned header)
    ncoll = transs.count()
    if limit < ncoll:
        transs, percentage = truncateTransitions(transs, q, limit)
    else:
        percentage = None

    species, nstates, sourceids = getSpeciesWithStates(transs)
    # electron collider
    particles = getParticles()

    # cross sections
    states = []
    for specie in species:
        states.extend(specie.States)
    nspecies = species.count()

    nspecies = species.count()
    sources = getSources(sourceids)
    nsources = sources.count()

    # Create the result object
    result = util_models.Result()
    result.addHeaderField('TRUNCATED', percentage)
    result.addHeaderField('COUNT-STATES', nstates)
    result.addHeaderField('COUNT-COLLISIONS', ncoll)
    result.addHeaderField('COUNT-SPECIES', nspecies)
    result.addHeaderField('COUNT-SOURCES', nsources)

    if LAST_MODIFIED is not None:
        result.addHeaderField('LAST-MODIFIED', LAST_MODIFIED)

    if (nstates == 0 and nspecies == 0):
        result.addHeaderField('APPROX-SIZE', 0)

    if ncoll > 0:
        result.addDataField('CollTrans', transs)
        result.addDataField('Particles', particles)
    result.addDataField('Atoms', species)
    result.addDataField('Sources', sources)

    return result
Exemple #9
0
def setupVssRequest(sql, limit=2000):
    """		
        Execute a vss request
        @type  sql: string
        @param sql: vss request
        @rtype:   util_models.Result
        @return:  Result object		
    """

    result = util_models.Result()
    q = sql2Q(sql)    
    #log.debug(q)
    #select transitions : combination of density/temperature
    transs = models.Radiativetransition.objects.filter(q)
    ntranss=transs.count()
    methods = util_models.Methods()

    if limit < ntranss :
        transs, percentage = truncateTransitions(transs, q, limit)
    else:
        percentage=None 
        
    #log.debug("number of transitions : "+str(ntranss))
    # Through the transition-matches, use our helper functions to extract 
    # all the relevant database data for our query. 
    if ntranss > 0 :	
        species, nspecies, nstates = getSpeciesWithStates(transs)      
        transitions = transs        
        sources =  getSources(transs)  
        nsources = len(sources)

        # Create the header with some useful info. The key names here are
        # standardized and shouldn't be changed.
        result.addHeaderField('TRUNCATED',percentage)
        result.addHeaderField('COUNT-SPECIES',nspecies)
        result.addHeaderField('COUNT-STATES',nstates)
        result.addHeaderField('COUNT-SOURCES',nsources)	
        result.addHeaderField('COUNT-RADIATIVE',len(transitions))	
        
        if LAST_MODIFIED is not None : 
          result.addHeaderField('LAST-MODIFIED',LAST_MODIFIED)
       
        result.addDataField('RadTrans',transitions)        
        result.addDataField('Molecules',species)
        result.addDataField('Methods',methods.getMethodsAsList())
        result.addDataField('Sources',sources)
        
    else : # only fill header
        result.addHeaderField('APPROX-SIZE', 0)    
        result.addHeaderField('TRUNCATED',percentage)
        result.addHeaderField('COUNT-STATES',0)
        result.addHeaderField('COUNT-RADIATIVE',0)
       
    return result	
Exemple #10
0
def setupVssRequest(sql, limit=3000):
    """
    This function is always called by the software.
    """
    # log the incoming query
    log.debug(sql)

    # convert the incoming sql to a correct django query syntax object
    q = sql2Q(sql)

    start = time.clock()
    transs = django_models.Radiativetransition.objects.filter(q)
    ntranss=transs.count()

    if limit < ntranss :
        transs, percentage = truncateTransitions(transs, q, limit)
    else:
        percentage=None

    species, nstates, sourceids = getSpeciesWithStates(transs)

    # cross sections
    states = []
    
    nspecies = len(species)
    for specie in species:        
        for state in specie.States : 
            if state.xdata is not None : # do not add state without xdata/ydata
                states.append(state)
                
    #transs = toLowerUpperStates(transs)
    sources = getSources(sourceids)
    nsources = sources.count()

    # Create the result object
    result = util_models.Result()

    result.addHeaderField('TRUNCATED', percentage)
    result.addHeaderField('COUNT-STATES',nstates)
    result.addHeaderField('COUNT-RADIATIVE',ntranss)
    result.addHeaderField('COUNT-SPECIES',nspecies)
    result.addHeaderField('COUNT-SOURCES',nsources)  
    
    if(nstates == 0 and nspecies == 0):
        result.addHeaderField('APPROX-SIZE', 0)

    result.addDataField('RadTrans',transs)
    result.addDataField('Atoms',species)
    result.addDataField('RadCross',states)
    result.addDataField('Sources', sources)
    
    end = time.clock()-start
    log.debug("1 "+str(end)+  "s")
    return result
def setupVssRequest(sql, limit=500):
    """		
        Execute a vss request
        @type  sql: string
        @param sql: vss request
        @rtype:   util_models.Result
        @return:  Result object		
    """

    result = util_models.Result()
    q = sql2Q(sql)

    #select transitions : combination of density/temperature
    transs = django_models.Transition.objects.filter(q)
    ntranss = transs.count()

    if limit < ntranss:
        transs, percentage = truncateTransitions(transs, q, limit)
    else:
        percentage = None

    # Through the transition-matches, use our helper functions to extract
    # all the relevant database data for our query.
    if ntranss > 0:
        species, nspecies, nstates = getSpeciesWithStates(transs)
        transitions, environments = getTransitionsData(transs)
        particles = getParticles(ntranss)
        functions = getFunctions()
        sources = getSources(transs)
        nsources = len(sources)

        # Create the header with some useful info. The key names here are
        # standardized and shouldn't be changed.
        result.addHeaderField('TRUNCATED', percentage)
        result.addHeaderField('COUNT-SPECIES', nspecies)
        result.addHeaderField('COUNT-STATES', nstates)
        result.addHeaderField('COUNT-RADIATIVE', len(transitions))

        if LAST_MODIFIED is not None:
            result.addHeaderField('LAST-MODIFIED', LAST_MODIFIED)

        result.addDataField('RadTrans', transitions)
        result.addDataField('Atoms', species)
        result.addDataField('Environments', environments)
        result.addDataField('Particles', particles)
        #~ result.addDataField('Functions',functions)
        result.addDataField('Sources', sources)

    else:  # only fill header
        result.addHeaderField('APPROX-SIZE', 0)
        result.addHeaderField('TRUNCATED', percentage)
        result.addHeaderField('COUNT-STATES', 0)
        result.addHeaderField('COUNT-RADIATIVE', 0)
    return result
Exemple #12
0
def setupVssRequest(sql, limit=3000):
    """
    This function is always called by the software.
    """
    # log the incoming query
    log.debug(sql)

    # convert the incoming sql to a correct django query syntax object
    q = sql2Q(sql)

    start = time.clock()
    transs = django_models.Radiativetransition.objects.filter(q)
    ntranss = transs.count()

    if limit < ntranss:
        transs, percentage = truncateTransitions(transs, q, limit)
    else:
        percentage = None

    species, nstates, sourceids = getSpeciesWithStates(transs)

    # cross sections
    states = []

    nspecies = len(species)
    for specie in species:
        for state in specie.States:
            if state.xdata is not None:  # do not add state without xdata/ydata
                states.append(state)

    #transs = toLowerUpperStates(transs)
    sources = getSources(sourceids)
    nsources = sources.count()

    # Create the result object
    result = util_models.Result()

    result.addHeaderField('TRUNCATED', percentage)
    result.addHeaderField('COUNT-STATES', nstates)
    result.addHeaderField('COUNT-RADIATIVE', ntranss)
    result.addHeaderField('COUNT-SPECIES', nspecies)
    result.addHeaderField('COUNT-SOURCES', nsources)

    if (nstates == 0 and nspecies == 0):
        result.addHeaderField('APPROX-SIZE', 0)

    result.addDataField('RadTrans', transs)
    result.addDataField('Atoms', species)
    result.addDataField('RadCross', states)
    result.addDataField('Sources', sources)

    end = time.clock() - start
    log.debug("1 " + str(end) + "s")
    return result
Exemple #13
0
def setupResults(sql):
    """
    This function is always called by the NodeSoftware.
    """
    # log the incoming query
    log.debug(sql)

    # convert the incoming sql to a correct django query syntax object
    # (sql2Q is a helper function to do this for us).
    q = sql2Q(sql)

    # We build a queryset of database matches on the Transision model
    # since through this model (in our example) we are be able to
    # reach all other models. Note that a queryset is actually not yet
    # hitting the database, making it very efficient.
    transs = models.Transition.objects.select_related(depth=3).filter(q)

    # count the number of matches, make a simple trunkation if there are
    # too many (record the coverage in the returned header)
    ntranss = transs.count()
    if LIMIT < ntranss:
        transs = transs[:LIMIT]
        percentage = '%.1f' % (float(LIMIT) / ntranss * 100)
    else:
        percentage = None

    # Through the transition-matches, use our helper functions to extract
    # all the relevant database data for our query.
    sources = []  #    sources = getRefs(transs)
    nsources = 0  # nsources = sources.count()
    species, nspecies, nstates = getSpeciesWithStates(transs)
    methods = getLifetimeMethods()

    # Create the header with some useful info. The key names here are
    # standardized and shouldn't be changed.
    headerinfo = {
        'TRUNCATED': percentage,
        'COUNT-SOURCES': nsources,
        'COUNT-SPECIES': nspecies,
        'COUNT-STATES': nstates,
        'COUNT-RADIATIVE': ntranss,
        'APPROX-SIZE': ntranss * 0.001
    }

    # Return the data. The keynames are standardized.
    return {
        'RadTrans': transs,
        'Atoms': species,
        'Sources': sources,
        'HeaderInfo': headerinfo,
        'Methods': methods
    }
def setupResults(sql, limit=1000):
    """
    This function is always called by the software.
    """
    # log the incoming query
    log.debug(sql)

    # convert the incoming sql to a correct django query syntax object 
    # based on the RESTRICTABLES dictionary in dictionaries.py
    q = sql2Q(sql)   
    
    transs = models.Collisionaltransition.objects.filter(q)
    # count the number of matches, make a simple trunkation if there are
    # too many (record the coverage in the returned header)
    ncoll=transs.count()
    if limit < ncoll :
        transs, percentage = truncateTransitions(transs, q, limit)
    else:
        percentage=None

    # Through the transition-matches, use our helper functions to extract
    # all the relevant database data for our query.
    #sources = getRefs(transs)
    #nsources = sources.count()
    species, nstates = getSpeciesWithStates(transs)
    # electron collider
    particles = getParticles()

    # cross sections
    states = []
    for specie in species:
        states.extend(specie.States)
    
    # Create the header with some useful info. The key names here are
    # standardized and shouldn't be changed.
    headerinfo={\
            'Truncated':percentage,
            #'COUNT-SOURCES':nsources,
            'count-states':nstates,
            'count-collisions':ncoll
            }

    # Return the data. The keynames are standardized.
    return {'CollTrans':transs,
            'Atoms':species,
            'Particles' : particles,
            #'Sources':sources,
            'HeaderInfo':headerinfo,
            #'Methods':methods
            #'Functions':functions
           }
Exemple #15
0
def setupVssRequest(sql, limit=2000):
    """		
        Execute a vss request
        @type  sql: string
        @param sql: vss request
        @rtype:   util_models.Result
        @return:  Result object		
    """

    result = util_models.Result()
    q = sql2Q(sql)
    log.debug(q)
    #select transitions : combination of density/temperature
    transs = models.Radiativetransition.objects.filter(q)
    ntranss = transs.count()
    methods = util_models.Methods()

    if limit < ntranss:
        transs, percentage = truncateTransitions(transs, q, limit)
    else:
        percentage = None

    log.debug("number of transitions : " + str(ntranss))
    # Through the transition-matches, use our helper functions to extract
    # all the relevant database data for our query.
    if ntranss > 0:
        species, nspecies, nstates = getSpeciesWithStates(transs)
        transitions = transs
        sources = getSources(transs)
        nsources = len(sources)

        # Create the header with some useful info. The key names here are
        # standardized and shouldn't be changed.
        result.addHeaderField('TRUNCATED', percentage)
        result.addHeaderField('COUNT-SPECIES', nspecies)
        result.addHeaderField('COUNT-STATES', nstates)
        result.addHeaderField('COUNT-SOURCES', nsources)
        result.addHeaderField('COUNT-RADIATIVE', len(transitions))

        result.addDataField('RadTrans', transitions)
        result.addDataField('Molecules', species)
        result.addDataField('Methods', methods.getMethodsAsList())
        result.addDataField('Sources', sources)

    else:  # only fill header
        result.addHeaderField('APPROX-SIZE', 0)
        result.addHeaderField('TRUNCATED', percentage)
        result.addHeaderField('COUNT-STATES', 0)
        result.addHeaderField('COUNT-RADIATIVE', 0)
    return result
Exemple #16
0
def setupResults(sql):
    q = sql2Q(sql)

    states = models.State.objects.select_related(depth=2).filter(q)
    species = None

    headerinfo = {'COUNT-SOURCES':nsources,
                  'COUNT-SPECIES':nspecies,
                  'COUNT-STATES':nstates,
                  'APPROX-SIZE':states.count()*0.001 }

    return {'Atoms':species,
            'HeaderInfo':headerinfo,
           }
def setupResults(sql, limit=1000):
    """
    This function is always called by the software.
    """
    # log the incoming query
    log.debug(sql)

    # convert the incoming sql to a correct django query syntax object
    q = sql2Q(sql)
    
    transs = models.Radiativetransition.objects.filter(q)
    ntranss=transs.count()

    if limit < ntranss :
        transs, percentage = truncateTransitions(transs, q, limit)
    else:
        percentage=None

    #sources = getRefs(transs)
    #nsources = sources.count()
    species, nstates = getSpeciesWithStates(transs)


    # cross sections
    states = []
    for specie in species:
        states.extend(specie.States)

    # Create the header with some useful info. The key names here are
    # standardized and shouldn't be changed.
    headerinfo={\
            'Truncated':percentage,
            #'COUNT-SOURCES':nsources,
            'count-states':nstates,
            'count-radiative':ntranss
            }

    # Return the data. The keynames are standardized.
    return {'RadTrans':transs,
            'Atoms':species,
            'RadCross' : states,
            #'Sources':sources,
            'HeaderInfo':headerinfo,
            #'Methods':methods
            #'Functions':functions
           }
Exemple #18
0
def setupResults(sql):
    """
    This function is always called by the NodeSoftware.
    """
    # log the incoming query
    log.debug(sql)

    # convert the incoming sql to a correct django query syntax object
    # (sql2Q is a helper function to do this for us).
    q = sql2Q(sql)

    # collisions = models.Collision.objects.filter(q)

    # reactantids = set(collisions.values_list('reactant', flat=True))
    # productids  = set(collisions.values_list('product',  flat=True))
    # molids = chain(reactantids, productids)
    # molecules = models.Species.objects.filter(pk__in=molids)
    molecules = models.Molspecies.objects.filter(q)
    nspecies = molecules.count
    nstates = 0

    # for coll in collisions:
    #    coll.Reactants = models.Species.objects.filter(pk=coll.reactant.id)
    #    coll.Products  = models.Species.objects.filter(pk=coll.product.id)
    #    coll.DataSets  = models.DataSet.objects.filter(pk=coll.id)
    #    for dataset in coll.DataSets:
    #       dataset.TabData = models.TabData.objects.filter(dataset_id=coll.id)
    # ncoll = collisions.count
    ntrans = 0

    nsources = 0

    # standardized and shouldn't be changed.
    headerinfo = {
        "COUNT-SOURCES": nsources,
        "COUNT-SPECIES": nspecies,
        "COUNT-STATES": nstates,
        "COUNT_RADIATIVE": ntrans,
    }

    # Return the data. The keynames are standardized.
    return {"Molecules": molecules}  #'CollTrans'  : collisions,
Exemple #19
0
def setupResults(sql):
    """
    This function is always called by the NodeSoftware.
    """
    # log the incoming query
    log.debug(sql)

    # convert the incoming sql to a correct django query syntax object
    # (sql2Q is a helper function to do this for us).
    q = sql2Q(sql)

    collisions = models.Collision.objects.filter(q)

    reactantids = set(collisions.values_list('reactant', flat=True))
    productids = set(collisions.values_list('product', flat=True))
    molids = chain(reactantids, productids)
    molecules = models.Species.objects.filter(pk__in=molids)
    nspecies = molecules.count
    nstates = 0

    for coll in collisions:
        coll.Reactants = models.Species.objects.filter(pk=coll.reactant.id)
        coll.Products = models.Species.objects.filter(pk=coll.product.id)
        coll.DataSets = models.DataSet.objects.filter(pk=coll.id)
        for dataset in coll.DataSets:
            dataset.TabData = models.TabData.objects.filter(dataset_id=coll.id)
    ncoll = collisions.count
    ntrans = 0

    nsources = 0

    # standardized and shouldn't be changed.
    headerinfo = {
        'COUNT-SOURCES': nsources,
        'COUNT-SPECIES': nspecies,
        'COUNT-STATES': nstates,
        'COUNT_RADIATIVE': ntrans
    }

    # Return the data. The keynames are standardized.
    return {'CollTrans': collisions, 'Molecules': molecules}
Exemple #20
0
def setupResults(sql):
    # log the incoming query
    log.debug(sql)
    


    # convert the incoming sql to a correct django query syntax object
    # (sql2Q is a helper function to do this for us).
    q = sql2Q(sql)
    
    print (sql)
    # print (q)
    
    if constraintsPresent(sql) == False:
        # SELECT SPECIES special case
        # return all species ( only with inchikey ?)
        species = Species.objects.all().filter(inchi_key__isnull=False,application="Astro")
        customKida = ['CR', 'CRP', 'Photon', 'e-']
        kidaParticles = Species.objects.all().filter(common_name__in=customKida)
        species = union (species, kidaParticles)
        atoms, molecules, particles = splitSpecies(species)
        natoms = len(atoms)
        nmolecules = len(molecules)
        nparticles = len(particles)
        headerinfo={\
            'COUNT-ATOMS':natoms,
            'COUNT-MOLECULES':nmolecules,
            'COUNT-SPECIES':natoms+nmolecules+nparticles,
            'COUNT-COLLISIONS':0
            }

    
        return {'Atoms':atoms, 
            'Molecules':molecules,
            'Particles':particles,
            'HeaderInfo':headerinfo
           }
        
    else:
        return parseComplexQuery(q)
def setupResults(sql):
    """		
    Return results for request
    @type  sql: string
    @param sql: vss request
    @type  limit: int
    @param limit: maximum number of results
    @rtype:   dict
    @return:  dictionnary containig data		
  """
    result = None
    # return all species
    if str(sql).strip().lower() == 'select species':
        return setupSpecies()

    # all other requests
    else:
        q = sql2Q(sql)
        transs = django_models.Radiativetransition.objects.filter(q)

        if sql.HTTPmethod == 'HEAD':
            return {'HeaderInfo': returnHeaders(transs)}
        else:
            return setupVssRequest(transs, q)
Exemple #22
0
def setupResults(sql):
  """		
    Return results for request
    @type  sql: string
    @param sql: vss request
    @type  limit: int
    @param limit: maximum number of results
    @rtype:   dict
    @return:  dictionnary containig data		
  """
  result = None  
  # return all species
  if str(sql).strip().lower() == 'select species': 
    return setupSpecies()
    
  # all other requests
  else : 
    q = sql2Q(sql)
    transs = django_models.Radiativetransition.objects.filter(q)

    if sql.HTTPmethod == 'HEAD':
      return {'HeaderInfo':returnHeaders(transs)}
    else : 
      return setupVssRequest(transs, q)			
def setupResults(sql, limit=1000):
    """
    This function is always called by the software.
    """
    # log the incoming query
    LOG(sql)

    # convert the incoming sql to a correct django query syntax object 
    # based on the RESTRICTABLES dictionary in dictionaries.py
    q = sql2Q(sql)

    # We build a queryset of database matches on the Transision model
    # since through this model (in our example) we are be able to
    # reach all other models. Note that a queryset is actually not yet
    # hitting the database, making it very efficient.
    LOG("getting transitions")
    transs = models.Transitions.objects.select_related(depth=2).filter(q)
    LOG(transs.count())

    # count the number of matches, make a simple truncation if there are
    # too many (record the coverage in the returned header)
    # If we are constraining by transitions but not returning them,
    # do not truncate.
    ntranss=transs.count()    
    if limit < ntranss and transitionsRequired(sql):
        transs = transs[:limit]
        percentage='%.1f' % (float(limit) / ntranss * 100)
    else: 
        percentage=None

    # Through the transition-matches, use our helper functions to extract 
    # all the relevant database data for our query. 
    #sources = getRefs(transs)
    sources = {}
    nsources = 0
    LOG("Getting species")
    species, nspecies, nstates = getSpeciesWithStates(transs)
    methods = getLifetimeMethods()
    functions = {}
    LOG(species)

    # Adjust the counts of things returned according to the requestables.
    # The caller will choose what actually to return, but we have to set
    # the counts in the header ourselves.
    if not transitionsRequired(sql):
        ntranss = 0
    if not statesRequired(sql):
        nstates = 0

    # Create the header with some useful info. The key names here are
    # standardized and shouldn't be changed.
    headerinfo={\
            'Truncated':percentage,
            'COUNT-SOURCES':nsources,
            'COUNT-species':nspecies,
            'count-states':nstates,
            'count-radiative':ntranss
            }
    LOG(headerinfo)
            
    # Return the data. The keynames are standardized. 
    return {'RadTrans':transs,
            'Atoms':species,
            'Sources':sources,
            'HeaderInfo':headerinfo,
            'Methods':methods
           }
Exemple #24
0
def setupResults(tap):
    """
		This function is always called by the software.
		"""
    # log the incoming query
    LOG(tap)
    if not tap.where:
        return {}
    q = sql2Q(tap)

    database = getDatabase(q, None)
    if database is None:
        database = "saga2"

    table = getTable(q, None)
    if table is None:
        table = "transition"
    rows = getRows(database, table, q)
    rowCount = rows.count()

    if settings.ROW_LIMIT < rowCount:
        rows = rows[:settings.ROW_LIMIT]
        percentage = '%.1f' % (float(settings.ROW_LIMIT) / rowCount * 100)
        rowCount = settings.ROW_LIMIT
    else:
        percentage = '100'

    if table == 'energy':
        transitions = []
        transitionCount = 0
    else:
        transitions = rows
        transitionCount = rowCount

    sources = getSources(rows)
    sourceCount = 0
    if sources is not None:
        sourceCount = len(sources)

    molecules = getMolecules(rows)
    stateCount = 0
    moleculeCount = 0
    if molecules is not None:
        moleculeCount = len(molecules)
        for substance in molecules:
            substance.States = sorted(substance.States.values(),
                                      key=lambda x: x.id)
            stateCount += len(substance.States)

    size = 0.0011 + sourceCount * 0.0015 + moleculeCount * 0.00065 + stateCount * 0.0004 + transitionCount * 0.0003

    # Create the header with some useful info. The key names here are
    # standardized and shouldn't be changed.
    headerInfo = {  #
        #see vamdctap->views->addHeaders()->HEADS
        'COUNT-SOURCES':
        sourceCount,  #the count of the corresponding blocks in the XSAMS schema
        #'COUNT-ATOMS',
        'COUNT-MOLECULES': moleculeCount,
        'COUNT-SPECIES': moleculeCount,
        'COUNT-STATES': stateCount,
        #'COUNT-COLLISIONS',
        'COUNT-RADIATIVE': transitionCount,
        #'COUNT-NONRADIATIVE',
        'TRUNCATED':
        percentage,  #the percentage that the returned data represent with respect to the total amount available for that query
        'APPROX-SIZE':
        '%.3f' % size,  #estimate uncompressed document size in megabytes
    }

    LOG(headerInfo)
    # Return the data. The keynames are standardized.
    # see vamdctap->generators->Xsams()
    return {  #
        'HeaderInfo': headerInfo,
        'Sources': sources,
        'Methods': models.methods,
        #'Functions':,
        #'Environments':,
        #'Atoms':,
        'Molecules': molecules,
        #'Solids':,
        #'Particles':,
        #'CollTrans':,
        'RadTrans': transitions,
        #'RadCross':,
        #'NonRadTrans':,
    }
Exemple #25
0
def setupResults(tap_query):
	"""
		This function is always called by the software.
	"""

	# log the incoming query
	LOG(tap_query)

	# SELECT SPECIES
	if (len(tap_query.parsedSQL.columns) == 1) and not tap_query.where:
		#empty NormalModes XML-element remove
		tap_query.requestables.add('moleculestates')

		self_source = atmos.Biblios()
		self_source.biblioid = 0
		self_source.biblioname = settings.NODENAME.upper()
		self_source.biblioauthors = 'Fazliev Alexander, etc'
		self_source.biblioTypeName = 'vamdc node'
		self_source.biblioyear = date.today().year
		sources = [self_source]
		sourceCount = 0
		if sources is not None:
			sourceCount = len(sources)

		molecules = getAllAvailableMolecules()
		moleculeCount = 0
		if molecules is not None:
			moleculeCount = len(molecules)
		return {
			'Sources': sources,
			'Molecules': molecules,
			'HeaderInfo':{
				'COUNT-SOURCES': sourceCount,
				'COUNT-MOLECULES': moleculeCount,
				'COUNT-SPECIES': moleculeCount,
			}
		}

	if not tap_query.where:
		return {}
	q = sql2Q(tap_query)

	database = getDatabase(q, None)
	if database is None:
		database = "saga2"

	table = getTable(q, None)
	if table is None:
		table = "transition"
	rows = getRows(database, table, q)
	rowCount = rows.count()

	if settings.ROW_LIMIT < rowCount:
		rows = rows[:settings.ROW_LIMIT]
		percentage = '%.1f' % (float(settings.ROW_LIMIT) / rowCount * 100)
		rowCount = settings.ROW_LIMIT
	else:
		percentage = '100'

	if table == 'energy':
		transitions = []
		transitionCount = 0
	else:
		transitions = rows
		transitionCount = rowCount

	sources = getSources(rows)
	sourceCount = 0
	if sources is not None:
		sourceCount = len(sources)

	molecules = getMolecules(rows)
	stateCount = 0
	moleculeCount = 0
	if molecules is not None:
		moleculeCount = len(molecules)
		for substance in molecules:
			substance.States = sorted(substance.States.values(), key = lambda x: x.id)
			stateCount += len(substance.States)

	size = sourceCount*0.0015 + moleculeCount*0.00065 + stateCount*0.0004 + transitionCount*0.0003
	size += 0.0011 if size > 0 else 0.0

	# Create the header with some useful info. The key names here are
	# standardized and shouldn't be changed.
	headerInfo = {#
		#see vamdctap->views->addHeaders()->HEADS
		'COUNT-SOURCES': sourceCount, #the count of the corresponding blocks in the XSAMS schema
		#'COUNT-ATOMS',
		'COUNT-MOLECULES': moleculeCount,
		'COUNT-SPECIES': moleculeCount,
		'COUNT-STATES' : stateCount,
		#'COUNT-COLLISIONS',
		'COUNT-RADIATIVE': transitionCount,
		#'COUNT-NONRADIATIVE',
		'TRUNCATED': percentage, #the percentage that the returned data represent with respect to the total amount available for that query
		'APPROX-SIZE': '%.3f' % size, #estimate uncompressed document size in megabytes
	}
	LOG(headerInfo)

	# Return the data. The keynames are standardized.
	# see vamdctap->generators->Xsams()
	return {#
		'HeaderInfo': headerInfo,
		'Sources': sources,
		'Methods': models.methods,
		#'Functions':,
		#'Environments':,
		#'Atoms':,
		'Molecules': molecules,
		#'Solids':,
		#'Particles':,
		#'CollTrans':,
		'RadTrans': transitions,
		#'RadCross':,
		#'NonRadTrans':,
	}
def query(sql, limit):
    """
    Do the query.  Pass to the appropriate function depending on the type of query.
    """

    # log the incoming query
    log.debug('SQL input: %s' % sql)

    q = sql2Q(sql)

    #log.debug('Q: %s' % q)

    if len(sql.where) > 0:
        # It's a constrained query
        react_ds, atoms, molecules, particles, sources, functions, methods = constrainedResults(sql, q, limit)
        nsources = sources.count()
        nreacts = react_ds.count()
        nmolecules = molecules.count()
        natoms = atoms.count()
        nparticles = particles.count()
    elif ('%s' % sql).upper() == 'SELECT SPECIES':
        # Assume it's a species only query
        atoms, molecules, particles = speciesOnlyResults(sql, q, limit)
        methods = {}
        react_ds = {}
        sources = {}
        functions = {}
        methods = {}
        nreacts = 0
        nmolecules = molecules.count()
        natoms = atoms.count()
        nparticles = particles.count()
    else:
        return {}


    log.debug('Done setting up the QuerySets')
    # Create the header with some useful info. The key names here are
    # standardized and shouldn't be changed.
    headerinfo={\
            'COUNT-ATOMS':natoms,
            'COUNT-MOLECULES':nmolecules,
            'COUNT-SPECIES':nmolecules+natoms+nparticles,
            }

    # Return the data. The keynames are standardized. 
    # 2012-02-14 KWS As per Guy's message - return an empty dict if there is no data.
    if nreacts > 0:
        headerinfo['COUNT-COLLISIONS'] = nreacts
        return {\
                'HeaderInfo':headerinfo,
                'CollTrans':react_ds,
                'Atoms':atoms,
                'Molecules':molecules,
                'Particles':particles,
                'Sources':sources,
                'Functions':functions,
                'Methods':methods
               }
    elif nreacts == 0 and (nmolecules > 0 or natoms > 0 or nparticles > 0):
        return {\
                'HeaderInfo':headerinfo,
                'Atoms':atoms,
                'Molecules':molecules,
                'Particles':particles,
               }
    else:
        return {}
def setupResults(sql):
    """
    This function is always called by the NodeSoftware.
    """
    # log the incoming query
    log.debug(sql)

    # convert the incoming sql to a correct django query syntax object
    # (sql2Q is a helper function to do this for us).
    q = sql2Q(sql)
    import sys
    log.debug("QQQQQ" + str(q))
    # We build a queryset of database matches on the Transision model
    # since through this model (in our example) we are be able to
    # reach all other models. Note that a queryset is actually not yet
    # hitting the database, making it very efficient.
    species = models.VamdcSpecies.objects.filter(q)

    # count the number of matches, make a simple trunkation if there are
    # too many (record the coverage in the returned header)
    ##    ntranss=transs.count()
    ##    if LIMIT < ntranss :
    ##        transs = transs[:limit]
    ##        percentage='%.1f' % (float(limit) / ntranss * 100)
    ##    else:
    ##        percentage=None

    # Through the transition-matches, use our helper functions to extract
    # all the relevant database data for our query.
    #    sources = getRefs(transs)
    #    nsources = sources.count()
    #    species, nspecies, nstates = getSpeciesWithStates(transs)
    #    methods = getLifetimeMethods()

    percentage = 100
    nsources = 0
    nspecies = species.count()
    nstates = 0
    ntranss = 0
    molecules = species.filter(species_type__name='Molecule')
    atoms = species.filter(species_type__name='Atom')

    # Create the header with some useful info. The key names here are
    # standardized and shouldn't be changed.
    headerinfo = {
        'TRUNCATED': percentage,
        'COUNT-SOURCES': nsources,
        'COUNT-SPECIES': nspecies,
        'COUNT-STATES': nstates,
        'COUNT_RADIATIVE': ntranss,
        'APPROX-SIZE': nspecies * 0.001
    }

    # Return the data. The keynames are standardized.
    return {
        'Atoms': atoms,
        'Molecules': molecules,
        #'Sources':sources,
        'HeaderInfo': headerinfo,
        #'Methods':methods
    }
Exemple #28
0
def setupResults(sql, limit=1000, es_id = None):
    """
    This function is always called by the software.
    """
    # log the incoming query
    LOG(sql)

    #x_internal is the list for the iteration over one search result, x the overall list (which is deduplicated in the end)

    molecules = []
    molecules_internal = []
    atoms = []
    atoms_internal = []
    sources = []
    sources_internal = []
    particles = []
    electron_particle = Particle('electron')

    inchiconvertedsearch = False

    #define the last modified header with an old date. we will compare all timestamps to this and take the most recent one
    lastmodifiedheader = datetime.datetime(1970, 1, 1, 1, 1)

    #use the function sql2Q provided by vamdctap to translate from query to Q-object
    q = sql2Q(sql)

    #create queryset for energyscans according to query
    energyscans = models.Energyscan.objects.filter(q)

    # count the number of matches
    nenergyscans = energyscans.count()

    #in case somebody is searching for a InchiKey and it didn't bring up any results:
    #convert the inchikey to an inchi, extract the sum formula and try again
    if nenergyscans == 0:
        if re.search('InchiKey', str(sql)) is not None:
            strsql = str(sql)
            match = re.findall('[A-Z]{14}-[A-Z]{10}-[A-Z]', strsql)

            #for each inchikey found we extract the chemical formula from the inchi
            #then we replace it in the original sql string
            for matchitem in match:
                chemical_formula = inchikey2chemicalformula(matchitem)
                if chemical_formula is not None:
                    strsql = strsql.replace(matchitem, chemical_formula)

            #if we had found one, we now replace the query
            if match is not None:
                strsql = strsql.replace('InchiKey','MoleculeStoichiometricFormula')
                #we now inject the new query to the request and call validate() to parse the SQL
                sql.request['QUERY'] = strsql
                sql.validate()

                #try again as usual
                energyscans = models.Energyscan.objects.filter(q)
                nenergyscans = energyscans.count()
                inchiconvertedsearch = True

    #append electron if there are results:
    if nenergyscans != 0:
        particles.append(electron_particle)

    #loop over energyscans that came back

    for energyscan in energyscans:
        #compare if lastmodified is newer than then newest we have already included
        if energyscan.lastmodified > lastmodifiedheader:
            lastmodifiedheader = energyscan.lastmodified

        #our reactants are always molecules. here we check if the product is a molecule.
        if energyscan.species.molecule:
            molecules_internal = models.Species.objects.filter(Q(id__exact=energyscan.species.id)|Q(id__exact=energyscan.origin_species.id))
        else:
            atoms_internal = models.Species.objects.filter(Q(id__exact=energyscan.species.id))
            molecules_internal = models.Species.objects.filter(Q(id__exact=energyscan.origin_species.id))

        energyscan.Products = models.Species.objects.filter(id__exact=energyscan.species.id)
        energyscan.Reactants = models.Species.objects.filter(id__exact=energyscan.origin_species.id)

        # check if we have two process codes. if so, we should spit them out as a list:
        if energyscan.process_code_2 is not None:
            energyscan.process_codes = [energyscan.process_code, energyscan.process_code_2]

        # adjust IAEA process codes
        if 'elat' in energyscan.process_codes:
            energyscan.IAEA_codes = 'EDA'
        elif 'ioni' in energyscan.process_codes:
            energyscan.IAEA_codes = 'EIN'

        #this part is a bit tricky: we make a new species-object which we give the ID 'electron'. otherwise it is empty
        #then we use list on the queryset energyscan.Reactants to force it to be executed.
        #afterwards, we append the newly created electron instance of the class species

        #keep in mind, that we actually defined the particle electron further up in the Particle() class. it was instanciated in the beginning of this function under the object electron_particle

        electron = models.Species('electron', '', '', '', '')
        energyscan.Reactants = list(energyscan.Reactants.all())
        energyscan.Reactants.append(electron)

        # adjust product charges
        for product in energyscan.Products:
            for molecule in molecules_internal:
                if product.id is molecule.id:
                    if energyscan.product_charge is models.Energyscan.NEUTRAL:
                        molecule.ioncharge = 0
                        molecule.inchikey = product.inchikey_neutral
                    elif energyscan.product_charge is models.Energyscan.ANIONIC:
                        molecule.ioncharge = -1
                        molecule.inchikey = product.inchikey_negative
                    elif energyscan.product_charge is models.Energyscan.CATIONIC:
                        molecule.ioncharge = 1
                        molecule.inchikey = product.inchikey_positive
                # reactant, neutral
                else:
                    molecule.ioncharge = 0
                    molecule.inchikey = product.inchikey_neutral
            for atom in atoms_internal:
                if product.id is atom.id:
                    if energyscan.product_charge is models.Energyscan.NEUTRAL:
                        atom.ioncharge = 0
                        atom.inchikey = product.inchikey_neutral
                    elif energyscan.product_charge is models.Energyscan.ANIONIC:
                        atom.ioncharge = -1
                        atom.inchikey = product.inchikey_negative
                    elif energyscan.product_charge is models.Energyscan.CATIONIC:
                        atom.ioncharge = 1
                        atom.inchikey = product.inchikey_positive
                # reactant, neutral
                else:
                    atom.ioncharge = 0
                    atom.inchikey = product.inchikey_neutral

        #calculate exact / nominal masses
        for atom in atoms_internal:
            if molecule.isotope is True:
                atom.exactmass = chemlib.chemicalformula2exactmass(atom.chemical_formula)

        for molecule in molecules_internal:
            if molecule.isotope is True:
                molecule.mass = chemlib.chemicalformula2exactmass(molecule.chemical_formula)

        #treat sources
        sources_internal = models.Source.objects.filter(id__exact=energyscan.source.id)
        for source in sources_internal:
            authorlist = []
            for author in source.authors.all():
                authorlist.append(u'%s, %s'%(author.lastname, author.firstname))

            source.author = authorlist

            #unescape and escape the URL again. this prevents us from delivering ampersands and therefore creating invalid XSAMS documents
            source.url = escape(unescape(source.url))

        #insert the standard-comment in addition to a possibly existing user-specific comment
        standardcomment = 'X-Values are measured with an energy resolution of %s eV. Therefore every shown peak is the original peak shape convoluted with our resolution. Energy scans are calibrated. Therefore we estimate an error of 0.1 eV' % energyscan.energyresolution 

        if energyscan.comment != '':
            usercomment = energyscan.comment
            energyscan.comment = 'Comment of the Producer: ' + usercomment + ' Additional Comment: ' + standardcomment
        else:
            energyscan.comment = standardcomment 

        #give warning when we converted inchikey to chemical formula for searching
        if inchiconvertedsearch is True:
            inchiwarning = 'WARNING: For this query, an InChI-Key was converted to a stoichiometric formula, because otherwise no results were obtained. '
            energyscan.comment = inchiwarning + energyscan.comment

        #prepare the origin data
        ES_list = energyscan.energyscan_data.split()
        k = 0
        x = []
        y = []
        for datapoint in ES_list:
            datapoint = datapoint.replace(',','.')
            #even -> x-value
            if k % 2 == 0:
                x.append(float(datapoint))
            #odd -> y-value
            else:
                y.append(float(datapoint))
            k = k + 1

        if len(x) != len(y):
            LOG('WARNING - number of x and y values is not equal')

        #create datasets
        energyscan.DataSets = []
        dataset = DataSet(x, y, energyscan.productiondate, energyscan.y_units)
        dataset.description = 'crossSection'
        dataset.accuracytype = 'systematic'
        energyscan.DataSets.append(dataset)

        #here we combine the list for molecules, atoms and sources from this particular energyscan with the query-wide list and remove all duplicates
        #see http://stackoverflow.com/questions/1319338/combining-two-lists-and-removing-duplicates-without-removing-duplicates-in-orig
        molecules = molecules + list(set(molecules_internal) - set(molecules))
        atoms = atoms + list(set(atoms_internal) - set(atoms))
        sources = sources + list(set(sources_internal) - set(sources))

    #count species and sources in order to return it to the headerinfo

    nsources = len(sources)
    nmolecules = len(molecules)
    natoms = len(atoms)
    nspecies = natoms + nmolecules

    #Create the Last Modified header
    #the header must not be newer than now!
    if lastmodifiedheader > datetime.datetime.now():
        lastmodifiedheader = datetime.datetime.now()

    # Create the header with some useful info. The key names here are
    # standardized and shouldn't be changed.
    headerinfo = {\
            'COUNT-SOURCES':nsources,
            'COUNT-SPECIES':nspecies,
            'COUNT-ATOMS':natoms,
            'COUNT-MOLECULES':nmolecules,
            'COUNT-COLLISIONS':nenergyscans,
            'COUNT-STATES':0,
            'COUNT-RADIATIVE':0,
            'COUNT-NONRADIATIVE':0,
            'LAST-MODIFIED':lastmodifiedheader,
            }

    # Return the data if it is not empty... The keynames are standardized. 
    if nenergyscans > 0:
        return {'CollTrans':energyscans,
                'Sources':sources,
                'Atoms':atoms,
                'Molecules':molecules,
                'Particles':particles,
                'HeaderInfo':headerinfo,
               }
    else:
        return {}
Exemple #29
0
def query(sql, limit):
    """
    Do the query.  Pass to the appropriate function depending on the type of query.
    """

    # log the incoming query
    log.debug('SQL input: %s' % sql)

    q = sql2Q(sql)

    #log.debug('Q: %s' % q)

    if len(sql.where) > 0:
        # It's a constrained query
        react_ds, atoms, molecules, particles, sources, functions, methods = constrainedResults(
            sql, q, limit)
        nsources = sources.count()
        nreacts = react_ds.count()
        nmolecules = molecules.count()
        natoms = atoms.count()
        nparticles = particles.count()
    elif ('%s' % sql).upper() == 'SELECT SPECIES':
        # Assume it's a species only query
        atoms, molecules, particles = speciesOnlyResults(sql, q, limit)
        methods = {}
        react_ds = {}
        sources = {}
        functions = {}
        methods = {}
        nreacts = 0
        nmolecules = molecules.count()
        natoms = atoms.count()
        nparticles = particles.count()
    else:
        return {}

    log.debug('Done setting up the QuerySets')
    # Create the header with some useful info. The key names here are
    # standardized and shouldn't be changed.
    headerinfo={\
            'COUNT-ATOMS':natoms,
            'COUNT-MOLECULES':nmolecules,
            'COUNT-SPECIES':nmolecules+natoms+nparticles,
            }

    # Return the data. The keynames are standardized.
    # 2012-02-14 KWS As per Guy's message - return an empty dict if there is no data.
    if nreacts > 0:
        headerinfo['COUNT-COLLISIONS'] = nreacts
        return {\
                'HeaderInfo':headerinfo,
                'CollTrans':react_ds,
                'Atoms':atoms,
                'Molecules':molecules,
                'Particles':particles,
                'Sources':sources,
                'Functions':functions,
                'Methods':methods
               }
    elif nreacts == 0 and (nmolecules > 0 or natoms > 0 or nparticles > 0):
        return {\
                'HeaderInfo':headerinfo,
                'Atoms':atoms,
                'Molecules':molecules,
                'Particles':particles,
               }
    else:
        return {}
Exemple #30
0
def setupResults(sql, limit=1000, es_id = None):
    """
    This function is always called by the software.
    """
    # log the incoming query
    LOG(sql)

    #x_internal is the list for the iteration over one search result, x the overall list (which is deduplicated in the end)

    molecules = []
    molecules_internal = []
    atoms = []
    atoms_internal = []
    sources = []
    sources_internal = []
    particles = []
    electron_particle = Particle('electron')

    inchiconvertedsearch = False

    #define the last modified header with an old date. we will compare all timestamps to this and take the most recent one
    lastmodifiedheader = datetime.datetime(1970, 1, 1, 1, 1)

    #use the function sql2Q provided by vamdctap to translate from query to Q-object
    q = sql2Q(sql)

    #create queryset for energyscans according to query
    energyscans = models.Energyscan.objects.filter(q)

    # count the number of matches
    nenergyscans = energyscans.count()

    #in case somebody is searching for a InchiKey and it didn't bring up any results:
    #convert the inchikey to an inchi, extract the sum formula and try again
    if nenergyscans == 0:
        if re.search('InchiKey', str(sql)) is not None:
            strsql = str(sql)
            match = re.findall('[A-Z]{14}-[A-Z]{10}-[A-Z]', strsql)

            #for each inchikey found we extract the chemical formula from the inchi
            #then we replace it in the original sql string
            for matchitem in match:
                chemical_formula = inchikey2chemicalformula(matchitem)
                if chemical_formula is not None:
                    strsql = strsql.replace(matchitem, chemical_formula)

            #if we had found one, we now replace the query
            if match is not None:
                strsql = strsql.replace('InchiKey','MoleculeStoichiometricFormula')
                #we now inject the new query to the request and call validate() to parse the SQL
                sql.request['QUERY'] = strsql
                sql.validate()

                #try again as usual
                energyscans = models.Energyscan.objects.filter(q)
                nenergyscans = energyscans.count()
                inchiconvertedsearch = True

    #append electron if there are results:
    if nenergyscans != 0:
        particles.append(electron_particle)

    #loop over energyscans that came back

    for energyscan in energyscans:
        #compare if lastmodified is newer than then newest we have already included
        if energyscan.lastmodified > lastmodifiedheader:
            lastmodifiedheader = energyscan.lastmodified

        #our reactants are always molecules. here we check if the product is a molecule.
        if energyscan.species.molecule:
            molecules_internal = models.Species.objects.filter(Q(id__exact=energyscan.species.id)|Q(id__exact=energyscan.origin_species.id))
        else:
            atoms_internal = models.Species.objects.filter(Q(id__exact=energyscan.species.id))
            molecules_internal = models.Species.objects.filter(Q(id__exact=energyscan.origin_species.id))

        energyscan.Products = models.Species.objects.filter(id__exact=energyscan.species.id)
        energyscan.Reactants = models.Species.objects.filter(id__exact=energyscan.origin_species.id)

        # check if we have two process codes. if so, we should spit them out as a list:
        if energyscan.process_code_2 != '':
            energyscan.process_codes = [energyscan.process_code, energyscan.process_code_2]
        else:
            energyscan.process_codes = [energyscan.process_code]

        # adjust IAEA process codes
        if 'elat' in energyscan.process_codes:
            energyscan.IAEA_codes = 'EDA'
        elif 'ioni' in energyscan.process_codes:
            energyscan.IAEA_codes = 'EIN'

        #this part is a bit tricky: we make a new species-object which we give the ID 'electron'. otherwise it is empty
        #then we use list on the queryset energyscan.Reactants to force it to be executed.
        #afterwards, we append the newly created electron instance of the class species

        #keep in mind, that we actually defined the particle electron further up in the Particle() class. it was instanciated in the beginning of this function under the object electron_particle

        electron = models.Species('electron', '', '', '', '')
        energyscan.Reactants = list(energyscan.Reactants.all())
        energyscan.Reactants.append(electron)

        # adjust product charges
        for product in energyscan.Products:
            for molecule in molecules_internal:
                if product.id is molecule.id:
                    if energyscan.product_charge is models.Energyscan.NEUTRAL:
                        molecule.ioncharge = 0
                        molecule.inchikey = product.inchikey_neutral
                        molecule.inchi = product.inchi_neutral
                    elif energyscan.product_charge is models.Energyscan.ANIONIC:
                        molecule.ioncharge = -1
                        molecule.inchikey = product.inchikey_negative
                        molecule.inchi = product.inchi_negative
                    elif energyscan.product_charge is models.Energyscan.CATIONIC:
                        molecule.ioncharge = 1
                        molecule.inchikey = product.inchikey_positive
                        molecule.inchi = product.inchi_positive
                # reactant, neutral
                else:
                    molecule.ioncharge = 0
                    molecule.inchikey = product.inchikey_neutral
                    molecule.inchi = product.inchi_neutral
            for atom in atoms_internal:
                if product.id is atom.id:
                    if energyscan.product_charge is models.Energyscan.NEUTRAL:
                        atom.ioncharge = 0
                        atom.inchikey = product.inchikey_neutral
                        atom.inchi = product.inchi_neutral
                    elif energyscan.product_charge is models.Energyscan.ANIONIC:
                        atom.ioncharge = -1
                        atom.inchikey = product.inchikey_negative
                        atom.inchi = product.inchi_negative
                    elif energyscan.product_charge is models.Energyscan.CATIONIC:
                        atom.ioncharge = 1
                        atom.inchikey = product.inchikey_positive
                        atom.inchi = product.inchi_positive
                # reactant, neutral
                else:
                    atom.ioncharge = 0
                    atom.inchikey = product.inchikey_neutral
                    atom.inchi = product.inchi_neutral

        #calculate exact / nominal masses
        for atom in atoms_internal:
            if molecule.isotope is True:
                atom.exactmass = chemlib.chemicalformula2exactmass(atom.chemical_formula)

        for molecule in molecules_internal:
            if molecule.isotope is True:
                molecule.mass = chemlib.chemicalformula2exactmass(molecule.chemical_formula)

        #treat sources
        sources_internal = models.Source.objects.filter(id__exact=energyscan.source.id)
        for source in sources_internal:
            authorlist = []
            for author in source.authors.all():
                authorlist.append(u'%s, %s'%(author.lastname, author.firstname))

            source.author = authorlist

            #unescape and escape the URL again. this prevents us from delivering ampersands and therefore creating invalid XSAMS documents
            source.url = escape(unescape(source.url))

        #insert the standard-comment in addition to a possibly existing user-specific comment
        standardcomment = 'X-Values are measured with an energy resolution of %s eV. Therefore every shown peak is the original peak shape convoluted with our resolution. Energy scans are calibrated. Therefore we estimate an error of 0.1 eV' % energyscan.energyresolution 

        if energyscan.comment != '':
            usercomment = energyscan.comment
            energyscan.comment = 'Comment of the Producer: ' + usercomment + ' Additional Comment: ' + standardcomment
        else:
            energyscan.comment = standardcomment 

        #give warning when we converted inchikey to chemical formula for searching
        if inchiconvertedsearch is True:
            inchiwarning = 'WARNING: For this query, an InChI-Key was converted to a stoichiometric formula, because otherwise no results were obtained. '
            energyscan.comment = inchiwarning + energyscan.comment

        #prepare the origin data
        ES_list = energyscan.energyscan_data.split()
        k = 0
        x = []
        y = []
        for datapoint in ES_list:
            datapoint = datapoint.replace(',','.')
            #even -> x-value
            if k % 2 == 0:
                x.append(float(datapoint))
            #odd -> y-value
            else:
                y.append(float(datapoint))
            k = k + 1

        if len(x) != len(y):
            LOG('WARNING - number of x and y values is not equal')

        #create datasets
        energyscan.DataSets = []
        dataset = DataSet(x, y, energyscan.productiondate, energyscan.y_units)
        dataset.description = 'crossSection'
        dataset.accuracytype = 'systematic'
        energyscan.DataSets.append(dataset)

        #here we combine the list for molecules, atoms and sources from this particular energyscan with the query-wide list and remove all duplicates
        #see http://stackoverflow.com/questions/1319338/combining-two-lists-and-removing-duplicates-without-removing-duplicates-in-orig
        molecules = molecules + list(set(molecules_internal) - set(molecules))
        atoms = atoms + list(set(atoms_internal) - set(atoms))
        sources = sources + list(set(sources_internal) - set(sources))

    #count species and sources in order to return it to the headerinfo

    nsources = len(sources)
    nmolecules = len(molecules)
    natoms = len(atoms)
    nspecies = natoms + nmolecules

    #Create the Last Modified header
    #the header must not be newer than now!
    if lastmodifiedheader > datetime.datetime.now():
        lastmodifiedheader = datetime.datetime.now()

    # Create the header with some useful info. The key names here are
    # standardized and shouldn't be changed.
    headerinfo = {\
            'COUNT-SOURCES':nsources,
            'COUNT-SPECIES':nspecies,
            'COUNT-ATOMS':natoms,
            'COUNT-MOLECULES':nmolecules,
            'COUNT-COLLISIONS':nenergyscans,
            'COUNT-STATES':0,
            'COUNT-RADIATIVE':0,
            'COUNT-NONRADIATIVE':0,
            'LAST-MODIFIED':lastmodifiedheader,
            }

    # Return the data if it is not empty... The keynames are standardized. 
    if nenergyscans > 0:
        return {'CollTrans':energyscans,
                'Sources':sources,
                'Atoms':atoms,
                'Molecules':molecules,
                'Particles':particles,
                'HeaderInfo':headerinfo,
               }
    else:
        return {}
Exemple #31
0
def setupResults(tap):
	"""
		This function is always called by the software.
		"""
	# log the incoming query
	LOG(tap)
	if not tap.where:
		return {}
	q = sql2Q(tap)

	database = getDatabase(q, None)
	if database is None:
		database = "saga2"

	table = getTable(q, None)
	if table is None:
		table = "transition"
	rows = getRows(database, table, q)

	if table == 'energy':
		transitions = []
	else:
		transitions = rows

	transitionCount = len(transitions)
	if LIMIT < transitionCount:
		transitions = transitions[:LIMIT]
		percentage = '%.1f' % (float(LIMIT) / transitionCount * 100)
	else:
		percentage = '100'

	sources = getSources(rows)
	sourceCount = 0
	if sources is not None:
		sourceCount = len(sources)

	molecules = getMolecules(rows)
	stateCount = 0
	moleculeCount = 0
	if molecules is not None:
		moleculeCount = len(molecules)
		for substance in molecules:
			substance.States = sorted(substance.States, key = lambda x: x.id)
			stateCount += len(substance.States)

	size = 0.0011 + sourceCount*0.0014 + moleculeCount*0.00065 + stateCount*0.0006 + transitionCount*0.00045

	# Create the header with some useful info. The key names here are
	# standardized and shouldn't be changed.
	headerInfo = {#
		#see vamdctap->views->addHeaders()->HEADS
		'COUNT-SOURCES': sourceCount, #the count of the corresponding blocks in the XSAMS schema
		#'COUNT-ATOMS',
		'COUNT-MOLECULES': moleculeCount,
		'COUNT-SPECIES': moleculeCount,
		'COUNT-STATES' : stateCount,
		#'COUNT-COLLISIONS',
		'COUNT-RADIATIVE': transitionCount,
		#'COUNT-NONRADIATIVE',
		'TRUNCATED': percentage, #the percentage that the returned data represent with respect to the total amount available for that query
		'APPROX-SIZE': '%.3f' % size, #estimate uncompressed document size in megabytes
	}

	LOG(headerInfo)
	# Return the data. The keynames are standardized.
	# see vamdctap->generators->Xsams()
	return {#
		'HeaderInfo': headerInfo,
		'Sources': sources,
		'Methods': models.methods,
		#'Functions':,
		#'Environments':,
		#'Atoms':,
		'Molecules': molecules,
		#'Solids':,
		#'Particles':,
		#'CollTrans':,
		'RadTrans': transitions,
		#'RadCross':,
		#'NonRadTrans':,
	}