1
# Licensed under a 3-clause BSD style license - see LICENSE.rst
2 1
from __future__ import print_function
3

4 1
import os.path
5 1
import warnings
6

7 1
from ..utils.process_asyncs import async_to_sync
8 1
from ..query import BaseQuery
9 1
from . import conf
10 1
from . load_species_table import species_lookuptable
11

12 1
__doctest_skip__ = ['VamdcClass.*']
13

14

15 1
@async_to_sync
16 1
class VamdcClass(BaseQuery):
17

18 1
    TIMEOUT = conf.timeout
19 1
    CACHE_LOCATION = conf.cache_location
20

21 1
    def __init__(self, doimport=True):
22 1
        super(VamdcClass, self).__init__()
23

24 1
        if not doimport:
25
            # this is a hack to allow the docstrings to be produced without
26
            # importing the necessary modules
27 1
            return
28

29 1
        from vamdclib import nodes as vnodes
30 0
        from vamdclib import request as vrequest
31 0
        from vamdclib import results as vresults
32 0
        from vamdclib import specmodel
33

34 0
        self._vnodes = vnodes
35 0
        self._vrequest = vrequest
36 0
        self._vresults = vresults
37

38 0
        self._nl = vnodes.Nodelist()
39 0
        self._cdms = self._nl.findnode('cdms')
40

41 0
        self.specmodel = specmodel
42

43 1
    @property
44 1
    def species_lookuptable(self, cache=True):
45
        """
46
        As a property, you can't turn off caching....
47
        """
48 0
        if not hasattr(self, '_lut'):
49 0
            self._lut = species_lookuptable(cache=cache)
50

51 0
        return self._lut
52

53 1
    def query_molecule(self, molecule_name, chem_re_flags=0, cache=True):
54
        """
55
        Query for the VAMDC data for a specific molecule
56

57
        Parameters
58
        ----------
59
        molecule_name: str
60
            The common name (including unicode characters) or the ordinary
61
            molecular formula (e.g., CH3OH for Methanol) of the molecule.
62
        chem_re_flags: int
63
            The re (regular expression) flags for comparison of the molecule
64
            name with the lookuptable keys
65
        cache: bool
66
            Use the astroquery cache to store/recover the result
67

68
        Returns
69
        -------
70
        result: ``vamdclib.request.Result``
71
            A vamdclib Result object that has a data attribute.  The result
72
            object has dictionary-like entries but has more functionality built
73
            on top of that
74
        """
75

76 0
        myhash = "{0}_re{1}".format(molecule_name, chem_re_flags)
77 0
        myhashpath = os.path.join(self.CACHE_LOCATION,
78
                                  myhash)
79 0
        if os.path.exists(myhashpath) and cache:
80 0
            with open(myhashpath, 'rb') as fh:
81 0
                xml = fh.read()
82 0
            result = self._vresults.Result(xml=xml)
83 0
            result.populate_model()
84
        else:
85 0
            species_id_dict = self.species_lookuptable.find(molecule_name,
86
                                                            flags=chem_re_flags)
87 0
            if len(species_id_dict) == 1:
88 0
                species_id = list(species_id_dict.values())[0]
89
            else:
90 0
                raise ValueError("Too many species matched: {0}"
91
                                 .format(species_id_dict))
92

93 0
            request = self._vrequest.Request(node=self._cdms)
94 0
            query_string = "SELECT ALL WHERE VAMDCSpeciesID='%s'" % species_id
95 0
            request.setquery(query_string)
96 0
            result = request.dorequest()
97

98 0
            if cache:
99 0
                with open(myhashpath, 'wb') as fh:
100 0
                    xml = fh.write(result.Xml)
101

102 0
        return result
103

104
        # example use of specmodel; return to this later...
105
        # Q = self.specmodel.calculate_partitionfunction(result.data['States'],
106
        #                                  temperature=tex)[species_id]
107

108

109 1
try:
110 1
    Vamdc = VamdcClass()
111 1
except ImportError:
112 1
    warnings.warn("vamdclib could not be imported; the vamdc astroquery module "
113
                  "will not work")
114 1
    Vamdc = VamdcClass(doimport=False)

Read our documentation on viewing source code .

Loading