<p>I have no experience with astroquery, but this type of problem can commonly be solved with <a href="https://docs.python.org/3/tutorial/errors.html#handling-exceptions" rel="nofollow noreferrer">exception handling</a>. My Python is rather rusty, but something like this should work:</p>
<pre><code>table = None
try:
    table = Nist.query(lambda_min[i] * u.AA, lambda_max[i] * u.AA, linename=species[j], wavelength_type=&quot;vac+air&quot;)[[&quot;Observed&quot;,&quot;Ritz&quot;]]
except Exception:
    print(species[j], 2*&quot;\n&quot;, &quot;Not Present&quot;, 2*&quot;\n&quot;)
    continue
</code></pre>
<p>followed by what currently is in your <code>else</code> block.</p>

CC BY-SA 4.0
2021-10-25T16:33:27.520Z
Hey @Glorfindel, thanks for the solution! It was dumb of me to not think of exception handling. Don't worry, your Python is not rusty :)
ShaunakP
CC BY-SA 4.0
2021-10-25T11:23:58.900Z
2021-10-25T11:23:58.900Z
Glorfindel
<p>I'm writing a code to extract wavelengths of certain species of atoms/ions in a given wavelength range using the <code>Nist.query()</code> command of the Python package <code>astroquery</code>. I wish to print the output of the query for each species in given wavelength range to a text file. Here's my code:</p>
<pre><code>from astroquery.nist import Nist
import astropy.units as u
from astropy.io import ascii
import sys

lambda_min = np.array([4245, 4270, 4295, 4325, 4350, 4380, 4400, 4430, 4460, 4515, 4545, 4570, 4605, 4630, 4665,
                       4695, 4755, 4790, 4830, 4880, 4950, 5020, 5095, 5170, 5245, 5325, 5405, 5485, 5570, 5660, 
                       5750, 5845, 5945, 6045, 6150, 6255, 6370, 6485, 6605, 6730])

lambda_max = np.array([4260, 4280, 4310, 4330, 4360, 4385, 4415, 4440, 4470, 4530, 4550, 4585, 4610, 4645, 4670, 
                       4705, 4770, 4795, 4835, 4900, 4970, 5040, 5110, 5185, 5260, 5340, 5420, 5500, 5590, 5675,
                       5770, 5865, 5960, 6060, 6165, 6275, 6385, 6500, 6620, 6750])

species = np.array([&quot;C I&quot;, &quot;C II&quot;, &quot;N I&quot;, &quot;N II&quot;, &quot;O I&quot;, &quot;Ne I&quot;, &quot;Na I&quot;, &quot;Mg I&quot;, &quot;Mg II&quot;, &quot;Al II&quot;, &quot;Al III&quot;, &quot;Si I&quot;,
           &quot;Si II&quot;, &quot;P II&quot;, &quot;S I&quot;, &quot;S II&quot;, &quot;Ca I&quot;, &quot;Ca II&quot;, &quot;Sc II&quot;, &quot;Ti II&quot;, &quot;Cr II&quot;, &quot;Mn II&quot;, &quot;Fe I&quot;, &quot;Fe II&quot;, 
           &quot;Ni I&quot;, &quot;Sr II&quot;, &quot;Y II&quot;, &quot;Zr II&quot;, &quot;Ba II&quot;])

for i in range(len(lambda_min)):
    for j in range(len(species)): 
        table = Nist.query(lambda_min[i] * u.AA, lambda_max[i] * u.AA, linename=species[j], wavelength_type=&quot;vac+air&quot;)[[&quot;Observed&quot;,&quot;Ritz&quot;]]    
        if table is None:
            print(species[j], 2*&quot;\n&quot;, &quot;Not Present&quot;, 2*&quot;\n&quot;)
        else:
            orig_stdout = sys.stdout
            f = open(fr'/path/{lambda_min[i]-lambda_max[i]}.txt', 'a')
            sys.stdout = f
            print(species[j], 2*&quot;\n&quot;, table, 2*&quot;\n&quot;)
            sys.stdout = orig_stdout
            f.close()
</code></pre>
<p>Where <code>lambda_min</code> and <code>lambda_max</code> are lower and upper bounds of the wavelength regions, <code>species</code> is an array containing the species whose wavelengths I want to fetch. I figured out that if certain wavelength range doesn't contain wavelengths of certain species, then <code>Nist.query()</code> throws an <code>Exception: Result did not contain a table</code>. In such cases, I wish to print wavelengths are &quot;Not present&quot;, but I am not able to handle empty table returned by <code>Nist.query()</code>. How to solve this issue? There's no help regarding this issue in <code>astroquery</code> documentation.</p>
<p>Here's the error that I'm getting:</p>
<p><a href="https://i.sstatic.net/jadRl.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/jadRl.jpg" alt="enter image description here" /></a></p>

<data-analysis><spectra><python><astropy>
How to handle empty tables returned by astroquery Nist.query() in Python?
ShaunakP
# How to handle empty tables returned by astroquery Nist.query() in Python?

(Asked by: username_0 on 2021-10-24)

I'm writing a code to extract wavelengths of certain species of atoms/ions in a given wavelength range using the `Nist.query()` command of the Python package `astroquery`. I wish to print the output of the query for each species in given wavelength range to a text file. Here's my code:

```
from astroquery.nist import Nist
import astropy.units as u
from astropy.io import ascii
import sys

lambda_min = np.array([4245, 4270, 4295, 4325, 4350, 4380, 4400, 4430, 4460, 4515, 4545, 4570, 4605, 4630, 4665,
                       4695, 4755, 4790, 4830, 4880, 4950, 5020, 5095, 5170, 5245, 5325, 5405, 5485, 5570, 5660, 
                       5750, 5845, 5945, 6045, 6150, 6255, 6370, 6485, 6605, 6730])

lambda_max = np.array([4260, 4280, 4310, 4330, 4360, 4385, 4415, 4440, 4470, 4530, 4550, 4585, 4610, 4645, 4670, 
                       4705, 4770, 4795, 4835, 4900, 4970, 5040, 5110, 5185, 5260, 5340, 5420, 5500, 5590, 5675,
                       5770, 5865, 5960, 6060, 6165, 6275, 6385, 6500, 6620, 6750])

species = np.array(["C I", "C II", "N I", "N II", "O I", "Ne I", "Na I", "Mg I", "Mg II", "Al II", "Al III", "Si I",
           "Si II", "P II", "S I", "S II", "Ca I", "Ca II", "Sc II", "Ti II", "Cr II", "Mn II", "Fe I", "Fe II", 
           "Ni I", "Sr II", "Y II", "Zr II", "Ba II"])

for i in range(len(lambda_min)):
    for j in range(len(species)): 
        table = Nist.query(lambda_min[i] * u.AA, lambda_max[i] * u.AA, linename=species[j], wavelength_type="vac+air")[["Observed","Ritz"]]    
        if table is None:
            print(species[j], 2*"\n", "Not Present", 2*"\n")
        else:
            orig_stdout = sys.stdout
            f = open(fr'/path/{lambda_min[i]-lambda_max[i]}.txt', 'a')
            sys.stdout = f
            print(species[j], 2*"\n", table, 2*"\n")
            sys.stdout = orig_stdout
            f.close()

```

Where `lambda_min` and `lambda_max` are lower and upper bounds of the wavelength regions, `species` is an array containing the species whose wavelengths I want to fetch. I figured out that if certain wavelength range doesn't contain wavelengths of certain species, then `Nist.query()` throws an `Exception: Result did not contain a table`. In such cases, I wish to print wavelengths are "Not present", but I am not able to handle empty table returned by `Nist.query()`. How to solve this issue? There's no help regarding this issue in `astroquery` documentation.

Here's the error that I'm getting:

[![enter image description here](https://i.sstatic.net/jadRl.jpg)](https://i.sstatic.net/jadRl.jpg)

**Tags:** data-analysis, spectra, python, astropy


---

## 1 answers

### Answer by username_1 on 2021-10-25. Score: 0 | Accepted answer

I have no experience with astroquery, but this type of problem can commonly be solved with [exception handling](https://docs.python.org/3/tutorial/errors.html#handling-exceptions). My Python is rather rusty, but something like this should work:

```
table = None
try:
    table = Nist.query(lambda_min[i] * u.AA, lambda_max[i] * u.AA, linename=species[j], wavelength_type="vac+air")[["Observed","Ritz"]]
except Exception:
    print(species[j], 2*"\n", "Not Present", 2*"\n")
    continue

```

followed by what currently is in your `else` block.

> **Comments:**
> **username_0**: Hey @username_1, thanks for the solution! It was dumb of me to not think of exception handling. Don't worry, your Python is not rusty :)  

---

