Exporting data as fits#

This example shows how to submit a data export request using the ‘fits’ protocol and how to download the requested files.

Note that the ‘as-is’ protocol should be used instead of ‘fits’, if record keywords in the FITS headers are not needed, as it greatly reduces the server load.

import os
from pathlib import Path

import drms

First we will create a drms.Client, using the JSOC baseurl.

client = drms.Client()

# This example requires a registered export email address. You can register
# JSOC exports at: http://jsoc.stanford.edu/ajax/register_email.html
# You must supply your own email.
email = os.environ["JSOC_EMAIL"]

# Use 'as-is' instead of 'fits', if record keywords are not needed in the
# FITS header. This greatly reduces the server load!
export_protocol = "fits"

# Create download directory if it does not exist yet.
out_dir = Path("downloads")
if not out_dir.exists():
    Path(out_dir).mkdir(parents=True)

Construct the DRMS query string: “Series[harpnum][timespan]{data segments}”

qstr = "hmi.sharp_720s[4864][2014.11.30_00:00:00_TAI/1d@8h]{continuum, magnetogram, field}"
print(f"Data export query:\n  {qstr}\n")

# Submit export request using the 'fits' protocol
print("Submitting export request...")
result = client.export(qstr, method="url", protocol=export_protocol, email=email)

# Print request URL.
print(f"\nRequest URL: {result.request_url}")
print(f"{int(len(result.urls))} file(s) available for download.\n")

# Download selected files.
result.download(out_dir)
print("Download finished.")
print(f'\nDownload directory:\n  "{out_dir.absolute()}"\n')
Traceback (most recent call last):
  File "/home/docs/checkouts/readthedocs.org/user_builds/drms/checkouts/stable/examples/export_fits.py", line 46, in <module>
    result = client.export(qstr, method="url", protocol=export_protocol, email=email)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/docs/checkouts/readthedocs.org/user_builds/drms/envs/stable/lib/python3.11/site-packages/drms/client.py", line 1176, in export
    d = self._json.exp_request(
        ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/docs/checkouts/readthedocs.org/user_builds/drms/envs/stable/lib/python3.11/site-packages/drms/json.py", line 301, in exp_request
    req = self._json_request(self._exp_request_url(*args, **kwargs))
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/docs/checkouts/readthedocs.org/user_builds/drms/envs/stable/lib/python3.11/site-packages/drms/json.py", line 92, in _json_request
    return HttpJsonRequest(url, self._server.encoding)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/docs/checkouts/readthedocs.org/user_builds/drms/envs/stable/lib/python3.11/site-packages/drms/json.py", line 43, in __init__
    self._http = urlopen(url)
                 ^^^^^^^^^^^^
  File "/home/docs/.asdf/installs/python/3.11.10/lib/python3.11/urllib/request.py", line 216, in urlopen
    return opener.open(url, data, timeout)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/docs/.asdf/installs/python/3.11.10/lib/python3.11/urllib/request.py", line 525, in open
    response = meth(req, response)
               ^^^^^^^^^^^^^^^^^^^
  File "/home/docs/.asdf/installs/python/3.11.10/lib/python3.11/urllib/request.py", line 634, in http_response
    response = self.parent.error(
               ^^^^^^^^^^^^^^^^^^
  File "/home/docs/.asdf/installs/python/3.11.10/lib/python3.11/urllib/request.py", line 557, in error
    result = self._call_chain(*args)
             ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/docs/.asdf/installs/python/3.11.10/lib/python3.11/urllib/request.py", line 496, in _call_chain
    result = func(*args)
             ^^^^^^^^^^^
  File "/home/docs/.asdf/installs/python/3.11.10/lib/python3.11/urllib/request.py", line 749, in http_error_302
    return self.parent.open(new, timeout=req.timeout)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/docs/.asdf/installs/python/3.11.10/lib/python3.11/urllib/request.py", line 519, in open
    response = self._open(req, data)
               ^^^^^^^^^^^^^^^^^^^^^
  File "/home/docs/.asdf/installs/python/3.11.10/lib/python3.11/urllib/request.py", line 536, in _open
    result = self._call_chain(self.handle_open, protocol, protocol +
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/docs/.asdf/installs/python/3.11.10/lib/python3.11/urllib/request.py", line 496, in _call_chain
    result = func(*args)
             ^^^^^^^^^^^
  File "/home/docs/.asdf/installs/python/3.11.10/lib/python3.11/urllib/request.py", line 1391, in https_open
    return self.do_open(http.client.HTTPSConnection, req,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/docs/.asdf/installs/python/3.11.10/lib/python3.11/urllib/request.py", line 1351, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error [Errno -2] Name or service not known>

Total running time of the script: (0 minutes 0.214 seconds)

Gallery generated by Sphinx-Gallery