.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/correlated_examples/plot_2_astronomy.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_correlated_examples_plot_2_astronomy.py: Astronomy, 2D{1,1,1} dataset (Creating image composition) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 6-15 More often, the images in astronomy are a composition of datasets measured at different wavelengths over an area of the sky. In this example, we illustrate the use of the CSDM file-format, and `csdmpy` module, beyond just reading a CSDM-compliant file. We'll use these datasets, and compose an image, using Numpy arrays. The following example is the data from the `Eagle Nebula` acquired at three different wavelengths and serialized as a CSDM compliant file. Import the `csdmpy` model and load the dataset. .. GENERATED FROM PYTHON SOURCE LINES 15-21 .. code-block:: Python import csdmpy as cp domain = "https://www.ssnmr.org/sites/default/files/CSDM" filename = f"{domain}/EagleNebula/eagleNebula_base64.csdf" eagle_nebula = cp.load(filename) .. GENERATED FROM PYTHON SOURCE LINES 23-25 Let's get the tuple of dimension and dependent variable objects from the ``eagle_nebula`` instance. .. GENERATED FROM PYTHON SOURCE LINES 25-28 .. code-block:: Python x = eagle_nebula.dimensions y = eagle_nebula.dependent_variables .. GENERATED FROM PYTHON SOURCE LINES 29-35 Before we compose an image, let's take a look at the individual dependent variables from the dataset. The three dependent variables correspond to signal acquisition at 502 nm, 656 nm, and 673 nm, respectively. This information is also listed in the :attr:`~csdmpy.DependentVariable.name` attribute of the respective dependent variable instances, .. GENERATED FROM PYTHON SOURCE LINES 35-37 .. code-block:: Python print(y[0].name) .. rst-class:: sphx-glr-script-out .. code-block:: none Eagle Nebula acquired @ 502 nm .. GENERATED FROM PYTHON SOURCE LINES 38-40 .. code-block:: Python print(y[1].name) .. rst-class:: sphx-glr-script-out .. code-block:: none Eagle Nebula acquired @ 656 nm .. GENERATED FROM PYTHON SOURCE LINES 41-43 .. code-block:: Python print(y[2].name) .. rst-class:: sphx-glr-script-out .. code-block:: none Eagle Nebula acquired @ 673 nm .. GENERATED FROM PYTHON SOURCE LINES 44-49 Data Visualization ------------------ For convince, let’s view this CSDM object with three dependent-variables as three CSDM objects, each with a single dependent variable. We use the split() method. .. GENERATED FROM PYTHON SOURCE LINES 49-51 .. code-block:: Python data0, data1, data2 = eagle_nebula.split() .. GENERATED FROM PYTHON SOURCE LINES 52-55 Here, ``data0``, ``data1``, and ``data2`` contain the dependent-variable at index 0, 1, 2 of the ``eagle_nebula`` object. Let's plot the data from these dependent variables. .. GENERATED FROM PYTHON SOURCE LINES 55-66 .. code-block:: Python import matplotlib.pyplot as plt _, ax = plt.subplots(3, 1, figsize=(6, 14), subplot_kw={"projection": "csdm"}) ax[0].imshow(data0 / data0.max(), cmap="bone", vmax=0.1, aspect="auto") ax[1].imshow(data1 / data1.max(), cmap="bone", vmax=0.1, aspect="auto") ax[2].imshow(data2 / data1.max(), cmap="bone", vmax=0.1, aspect="auto") plt.tight_layout() plt.show() .. image-sg:: /auto_examples/correlated_examples/images/sphx_glr_plot_2_astronomy_001.png :alt: Eagle Nebula acquired @ 502 nm, Eagle Nebula acquired @ 656 nm, Eagle Nebula acquired @ 673 nm :srcset: /auto_examples/correlated_examples/images/sphx_glr_plot_2_astronomy_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 67-69 Image composition ----------------- .. GENERATED FROM PYTHON SOURCE LINES 69-71 .. code-block:: Python import numpy as np .. GENERATED FROM PYTHON SOURCE LINES 72-76 For the image composition, we assign the dependent variable at index zero as the blue channel, index one as the green channel, and index two as the red channel of an RGB image. Start with creating an empty array to hold the RGB dataset. .. GENERATED FROM PYTHON SOURCE LINES 76-79 .. code-block:: Python shape = y[0].components[0].shape + (3,) image = np.empty(shape, dtype=np.float64) .. GENERATED FROM PYTHON SOURCE LINES 80-83 Here, ``image`` is the variable we use for storing the composition. Add the respective dependent variables to the designated color channel in the ``image`` array, .. GENERATED FROM PYTHON SOURCE LINES 83-87 .. code-block:: Python image[..., 0] = y[2].components[0] / y[2].components[0].max() # red channel image[..., 1] = y[1].components[0] / y[1].components[0].max() # green channel image[..., 2] = y[0].components[0] / y[0].components[0].max() # blue channel .. GENERATED FROM PYTHON SOURCE LINES 88-95 Following the intensity plot of the individual dependent variables, see the above figures, it is evident that the component intensity from ``y[1]`` and, therefore, the green channel dominates the other two. If we plot the ``image`` data, the image will be saturated with green intensity. To attain a color-balanced image, we arbitrarily scale the intensities from the three channels. You may choose any scaling factor. Each scaling factor will produce a different composition. In this example, we use the following, .. GENERATED FROM PYTHON SOURCE LINES 95-99 .. code-block:: Python image[..., 0] = np.clip(image[..., 0] * 65.0, 0, 1) # red channel image[..., 1] = np.clip(image[..., 1] * 7.50, 0, 1) # green channel image[..., 2] = np.clip(image[..., 2] * 75.0, 0, 1) # blue channel .. GENERATED FROM PYTHON SOURCE LINES 100-101 Now to plot this composition. .. GENERATED FROM PYTHON SOURCE LINES 101-119 .. code-block:: Python # Set the extents of the image plot. extent = [ x[0].coordinates[0].value, x[0].coordinates[-1].value, x[1].coordinates[0].value, x[1].coordinates[-1].value, ] # add figure plt.imshow(image, origin="lower", extent=extent) plt.xlabel(x[0].axis_label) plt.ylabel(x[1].axis_label) plt.title("composition") plt.tight_layout() plt.show() .. image-sg:: /auto_examples/correlated_examples/images/sphx_glr_plot_2_astronomy_002.png :alt: composition :srcset: /auto_examples/correlated_examples/images/sphx_glr_plot_2_astronomy_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 3.284 seconds) .. _sphx_glr_download_auto_examples_correlated_examples_plot_2_astronomy.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_2_astronomy.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_2_astronomy.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_