Commit ff1aaab0aa27803e6bf7c1664c14435fbd17bb76
1 parent
381fb883
Reorder code in documentation (first C++, then Python)
Showing
7 changed files
with
126 additions
and
137 deletions
doc/source/users/user_manual/modules/fits/create.rst
| ... | ... | @@ -6,27 +6,6 @@ one image and one table (see ``examples/cpp/createfits/createfits.cpp`` for the |
| 6 | 6 | source code; the line numbers are for reference and are not part of |
| 7 | 7 | the source code) |
| 8 | 8 | |
| 9 | -**Python** | |
| 10 | - | |
| 11 | -.. code-block:: python | |
| 12 | - :linenos: | |
| 13 | - | |
| 14 | - fits = gammalib.GFits() | |
| 15 | - image = gammalib.GFitsImageDouble(20,10) | |
| 16 | - for x in range(20): | |
| 17 | - for y in range(10): | |
| 18 | - image[x,y] = x+100.0*y | |
| 19 | - fits.append(image) | |
| 20 | - table = gammalib.GFitsBinTable() | |
| 21 | - column = gammalib.GFitsTableDoubleCol('ENERGY', 10, 3) | |
| 22 | - for row in range(10): | |
| 23 | - for index in range(3): | |
| 24 | - column[row, index] = row*100.0+index | |
| 25 | - table.append(column) | |
| 26 | - fits.append(table) | |
| 27 | - fits.saveto('my_fits_file.fits', True) | |
| 28 | - fits.close() | |
| 29 | - | |
| 30 | 9 | **C++** |
| 31 | 10 | |
| 32 | 11 | .. code-block:: cpp |
| ... | ... | @@ -52,6 +31,27 @@ the source code) |
| 52 | 31 | fits.saveto("my_fits_file.fits", true); |
| 53 | 32 | fits.close(); |
| 54 | 33 | |
| 34 | +**Python** | |
| 35 | + | |
| 36 | +.. code-block:: python | |
| 37 | + :linenos: | |
| 38 | + | |
| 39 | + fits = gammalib.GFits() | |
| 40 | + image = gammalib.GFitsImageDouble(20,10) | |
| 41 | + for x in range(20): | |
| 42 | + for y in range(10): | |
| 43 | + image[x,y] = x+100.0*y | |
| 44 | + fits.append(image) | |
| 45 | + table = gammalib.GFitsBinTable() | |
| 46 | + column = gammalib.GFitsTableDoubleCol('ENERGY', 10, 3) | |
| 47 | + for row in range(10): | |
| 48 | + for index in range(3): | |
| 49 | + column[row, index] = row*100.0+index | |
| 50 | + table.append(column) | |
| 51 | + fits.append(table) | |
| 52 | + fits.saveto('my_fits_file.fits', True) | |
| 53 | + fits.close() | |
| 54 | + | |
| 55 | 55 | In line 1, a FITS object is allocated in memory. |
| 56 | 56 | An image is created in line 2, comprising 20 pixels in the x-direction |
| 57 | 57 | and 10 pixels in the y-direction, each pixel being a double precision | ... | ... |
doc/source/users/user_manual/modules/obs/caldb.rst
| ... | ... | @@ -11,37 +11,28 @@ The database is the open by specifying a mission and an instrument name. |
| 11 | 11 | The examples below show two variants of opening the ``prod2`` database for |
| 12 | 12 | ``CTA``. |
| 13 | 13 | |
| 14 | -**Python** | |
| 15 | - | |
| 16 | -.. code-block:: python | |
| 17 | - :linenos: | |
| 18 | - | |
| 19 | - import gammalib # Make GammaLib available | |
| 20 | - caldb1 = gammalib.GCaldb('cta','prod2') # Using opening constructor | |
| 21 | - caldb2 = gammalib.GCaldb() | |
| 22 | - caldb2.open('cta','prod2') # Using open method | |
| 23 | - | |
| 24 | 14 | **C++** |
| 25 | 15 | |
| 26 | 16 | .. code-block:: cpp |
| 27 | 17 | :linenos: |
| 28 | 18 | |
| 29 | - #include "GammaLib.hpp" // Make GammaLib available | |
| 30 | 19 | GCaldb caldb1("cta","prod2"); // Using opening constructor |
| 31 | 20 | GCaldb caldb2; |
| 32 | 21 | caldb2.open("cta","prod2"); // Using open method |
| 33 | 22 | |
| 34 | -Once opened, the database can be used to locate for example the filename | |
| 35 | -of a response component. The examples below show how the effective area | |
| 36 | -filename for the response ``South_5h`` is accessed via the calibration | |
| 37 | -database. | |
| 38 | - | |
| 39 | 23 | **Python** |
| 40 | 24 | |
| 41 | 25 | .. code-block:: python |
| 42 | 26 | :linenos: |
| 43 | 27 | |
| 44 | - filename = caldb1.filename('','','EFF_AREA','','','NAME(South_5h)') | |
| 28 | + caldb1 = gammalib.GCaldb('cta','prod2') # Using opening constructor | |
| 29 | + caldb2 = gammalib.GCaldb() | |
| 30 | + caldb2.open('cta','prod2') # Using open method | |
| 31 | + | |
| 32 | +Once opened, the database can be used to locate for example the filename | |
| 33 | +of a response component. The examples below show how the effective area | |
| 34 | +filename for the response ``South_5h`` is accessed via the calibration | |
| 35 | +database. | |
| 45 | 36 | |
| 46 | 37 | **C++** |
| 47 | 38 | |
| ... | ... | @@ -49,3 +40,10 @@ database. |
| 49 | 40 | :linenos: |
| 50 | 41 | |
| 51 | 42 | GFilename filename = caldb1.filename("","","EFF_AREA","","","NAME(South_5h)"); |
| 43 | + | |
| 44 | +**Python** | |
| 45 | + | |
| 46 | +.. code-block:: python | |
| 47 | + :linenos: | |
| 48 | + | |
| 49 | + filename = caldb1.filename('','','EFF_AREA','','','NAME(South_5h)') | ... | ... |
doc/source/users/user_manual/modules/obs/energy.rst
| ... | ... | @@ -12,30 +12,28 @@ The code threads below illustrate how an energy of 511 keV can be set using |
| 12 | 12 | the energy constructor or a dedicated method, and how that energy can be |
| 13 | 13 | retrieved in units of MeV. |
| 14 | 14 | |
| 15 | -**Python** | |
| 16 | - | |
| 17 | -.. code-block:: python | |
| 18 | - :linenos: | |
| 19 | - | |
| 20 | - import gammalib # Make GammaLib available | |
| 21 | - energy1 = gammalib.GEnergy(511.0,'keV') # Using energy constructor | |
| 22 | - energy2 = gammalib.GEnergy() | |
| 23 | - energy2.keV(511.0) # Using keV method | |
| 24 | - E_MeV1 = energy2('MeV') # Get energy in MeV | |
| 25 | - E_MeV2 = energy2.MeV() # Another variant to get energy in MeV | |
| 26 | - | |
| 27 | 15 | **C++** |
| 28 | 16 | |
| 29 | 17 | .. code-block:: cpp |
| 30 | 18 | :linenos: |
| 31 | 19 | |
| 32 | - #include "GammaLib.hpp" // Make GammaLib available | |
| 33 | 20 | GEnergy energy1(511.0,"keV"); // Using energy constructor |
| 34 | 21 | GEnergy energy2; |
| 35 | 22 | energy2.keV(511.0); // Using keV method |
| 36 | 23 | double E_MeV1 = energy2("MeV"); // Get energy in MeV |
| 37 | 24 | double E_MeV2 = energy2.MeV(); // Another variant to get energy in MeV |
| 38 | 25 | |
| 26 | +**Python** | |
| 27 | + | |
| 28 | +.. code-block:: python | |
| 29 | + :linenos: | |
| 30 | + | |
| 31 | + energy1 = gammalib.GEnergy(511.0,'keV') # Using energy constructor | |
| 32 | + energy2 = gammalib.GEnergy() | |
| 33 | + energy2.keV(511.0) # Using keV method | |
| 34 | + E_MeV1 = energy2('MeV') # Get energy in MeV | |
| 35 | + E_MeV2 = energy2.MeV() # Another variant to get energy in MeV | |
| 36 | + | |
| 39 | 37 | Energies can be added and subtracted, or multiplied or divided by a floating |
| 40 | 38 | point value. |
| 41 | 39 | In addition, two energies can be divided, resulting in a floating point value. |
| ... | ... | @@ -45,30 +43,28 @@ Energies can be collected by the :doxy:`GEnergies` container class. The threads |
| 45 | 43 | below illustrate how energies can be appended or inserted into the |
| 46 | 44 | :doxy:`GEnergies` container class. |
| 47 | 45 | |
| 48 | -**Python** | |
| 49 | - | |
| 50 | -.. code-block:: python | |
| 51 | - :linenos: | |
| 52 | - | |
| 53 | - import gammalib # Make GammaLib available | |
| 54 | - energy1 = gammalib.GEnergy(511.0,'keV') # Set first energy | |
| 55 | - energy2 = gammalib.GEnergy(1024.0,'keV') # Set second energy | |
| 56 | - energies = gammalib.GEnergies() # Create energy container instance | |
| 57 | - energies.append(energy1) # Append first energy | |
| 58 | - energies.insert(0,energy2) # Insert second energy before first energy | |
| 59 | - | |
| 60 | 46 | **C++** |
| 61 | 47 | |
| 62 | 48 | .. code-block:: cpp |
| 63 | 49 | :linenos: |
| 64 | 50 | |
| 65 | - #include "GammaLib.hpp" // Make GammaLib available | |
| 66 | 51 | GEnergy energy1(511.0,"keV"); // Set first energy |
| 67 | 52 | GEnergy energy2(1024.0,"keV"); // Set second energy |
| 68 | 53 | GEnergies energies; // Create energy container instance |
| 69 | 54 | energies.append(energy1); // Append first energy |
| 70 | 55 | energies.insert(0,energy2); // Insert second energy before first energy |
| 71 | 56 | |
| 57 | +**Python** | |
| 58 | + | |
| 59 | +.. code-block:: python | |
| 60 | + :linenos: | |
| 61 | + | |
| 62 | + energy1 = gammalib.GEnergy(511.0,'keV') # Set first energy | |
| 63 | + energy2 = gammalib.GEnergy(1024.0,'keV') # Set second energy | |
| 64 | + energies = gammalib.GEnergies() # Create energy container instance | |
| 65 | + energies.append(energy1) # Append first energy | |
| 66 | + energies.insert(0,energy2) # Insert second energy before first energy | |
| 67 | + | |
| 72 | 68 | The energy container can be saved into a file using the :doxy:`GEnergies::save()` |
| 73 | 69 | method, and loaded from a file using either the load constructor or the |
| 74 | 70 | :doxy:`GEnergies::load()` method. | ... | ... |
doc/source/users/user_manual/modules/obs/xml.rst
| ... | ... | @@ -58,47 +58,17 @@ Similar definitions exist for the other instruments. |
| 58 | 58 | The code threads below show different variants of how observations can be |
| 59 | 59 | loaded from or saved into an XML file. |
| 60 | 60 | |
| 61 | -**Python** | |
| 62 | - | |
| 63 | -.. code-block:: python | |
| 64 | - :linenos: | |
| 65 | - | |
| 66 | - import gammalib # Make GammaLib available | |
| 67 | - obs = gammalib.GObservations('my_observations.xml') # Construct observations from XML file | |
| 68 | - obs.save('my_copied_observations.xml') # Save observations into XML file | |
| 69 | - | |
| 70 | -.. code-block:: python | |
| 71 | - :linenos: | |
| 72 | - | |
| 73 | - import gammalib # Make GammaLib available | |
| 74 | - obs = gammalib.GObservations() # Construct empty observations | |
| 75 | - obs.load('my_observations.xml') # Load observations from XML file | |
| 76 | - obs.save('my_copied_observations.xml') # Save observations into XML file | |
| 77 | - | |
| 78 | -.. code-block:: python | |
| 79 | - :linenos: | |
| 80 | - | |
| 81 | - import gammalib # Make GammaLib available | |
| 82 | - obs = gammalib.GObservations() # Construct empty observations | |
| 83 | - xmlin = gammalib.GXml('my_observations.xml') # Construct GXml instance from XML file | |
| 84 | - xmlout = gammalib.GXml() # Construct empty GXml instance | |
| 85 | - obs.read(xmlin) # Read observations from GXml instance | |
| 86 | - obs.write(xmlout) # Write observations into GXml instance | |
| 87 | - xmlout.save('my_copied_observations.xml') # Save GXml instance in XML file | |
| 88 | - | |
| 89 | 61 | **C++** |
| 90 | 62 | |
| 91 | 63 | .. code-block:: cpp |
| 92 | 64 | :linenos: |
| 93 | 65 | |
| 94 | - #include "GammaLib.hpp" // Make GammaLib available | |
| 95 | 66 | GObservations obs("my_observations.xml"); // Construct observations from XML file |
| 96 | 67 | obs.save("my_copied_observations.xml"); // Save observations into XML file |
| 97 | 68 | |
| 98 | 69 | .. code-block:: cpp |
| 99 | 70 | :linenos: |
| 100 | 71 | |
| 101 | - #include "GammaLib.hpp" // Make GammaLib available | |
| 102 | 72 | GObservations obs; // Construct empty observations |
| 103 | 73 | obs.load("my_observations.xml"); // Load observations from XML file |
| 104 | 74 | obs.save("my_copied_observations.xml"); // Save observations into XML file |
| ... | ... | @@ -106,10 +76,34 @@ loaded from or saved into an XML file. |
| 106 | 76 | .. code-block:: cpp |
| 107 | 77 | :linenos: |
| 108 | 78 | |
| 109 | - #include "GammaLib.hpp" // Make GammaLib available | |
| 110 | 79 | GObservations obs; // Construct empty observations |
| 111 | 80 | GXml xmlin("my_observations.xml"); // Construct GXml instance from XML file |
| 112 | 81 | GXml xmlout; // Construct empty GXml instance |
| 113 | 82 | obs.read(xmlin); // Read observations from GXml instance |
| 114 | 83 | obs.write(xmlout); // Write observations into GXml instance |
| 115 | 84 | xmlout.save("my_copied_observations.xml"); // Save GXml instance in XML file |
| 85 | + | |
| 86 | +**Python** | |
| 87 | + | |
| 88 | +.. code-block:: python | |
| 89 | + :linenos: | |
| 90 | + | |
| 91 | + obs = gammalib.GObservations('my_observations.xml') # Construct observations from XML file | |
| 92 | + obs.save('my_copied_observations.xml') # Save observations into XML file | |
| 93 | + | |
| 94 | +.. code-block:: python | |
| 95 | + :linenos: | |
| 96 | + | |
| 97 | + obs = gammalib.GObservations() # Construct empty observations | |
| 98 | + obs.load('my_observations.xml') # Load observations from XML file | |
| 99 | + obs.save('my_copied_observations.xml') # Save observations into XML file | |
| 100 | + | |
| 101 | +.. code-block:: python | |
| 102 | + :linenos: | |
| 103 | + | |
| 104 | + obs = gammalib.GObservations() # Construct empty observations | |
| 105 | + xmlin = gammalib.GXml('my_observations.xml') # Construct GXml instance from XML file | |
| 106 | + xmlout = gammalib.GXml() # Construct empty GXml instance | |
| 107 | + obs.read(xmlin) # Read observations from GXml instance | |
| 108 | + obs.write(xmlout) # Write observations into GXml instance | |
| 109 | + xmlout.save('my_copied_observations.xml') # Save GXml instance in XML file | ... | ... |
doc/source/users/user_manual/modules/support/interpolation.rst
| ... | ... | @@ -7,19 +7,6 @@ used to describe a functional relation :math:`y_i=f(x_i)`. The following |
| 7 | 7 | code illustrates how the :doxy:`GNodeArray` class is used |
| 8 | 8 | (see ``examples/cpp/interpolate/interpolate.cpp`` for the source code): |
| 9 | 9 | |
| 10 | -**Python** | |
| 11 | - | |
| 12 | -.. code-block:: python | |
| 13 | - :linenos: | |
| 14 | - | |
| 15 | - x_i = [1.0, 4.0, 6.0] | |
| 16 | - y_i = [8.0, 7.0, 2.0] | |
| 17 | - nodes = gammalib.GNodeArray(x_i) | |
| 18 | - for x in range(20): | |
| 19 | - nodes.set_value(x*0.5) | |
| 20 | - y = y_i[nodes.inx_left()] * nodes.wgt_left() + y_i[nodes.inx_right()] * nodes.wgt_right() | |
| 21 | - print('x=%f : y=%f' % (x, y)) | |
| 22 | - | |
| 23 | 10 | **C++** |
| 24 | 11 | |
| 25 | 12 | .. code-block:: cpp |
| ... | ... | @@ -34,6 +21,20 @@ code illustrates how the :doxy:`GNodeArray` class is used |
| 34 | 21 | std::cout << "x=" << x << " : y=" << y << std::endl; |
| 35 | 22 | } |
| 36 | 23 | |
| 24 | +**Python** | |
| 25 | + | |
| 26 | +.. code-block:: python | |
| 27 | + :linenos: | |
| 28 | + | |
| 29 | + x_i = [1.0, 4.0, 6.0] | |
| 30 | + y_i = [8.0, 7.0, 2.0] | |
| 31 | + nodes = gammalib.GNodeArray(x_i) | |
| 32 | + for x in range(20): | |
| 33 | + nodes.set_value(x*0.5) | |
| 34 | + y = y_i[nodes.inx_left()] * nodes.wgt_left() + y_i[nodes.inx_right()] * nodes.wgt_right() | |
| 35 | + print('x=%f : y=%f' % (x, y)) | |
| 36 | + | |
| 37 | + | |
| 37 | 38 | In line 1, the nodes :math:`x_i` at which the function values :math:`y_i` |
| 38 | 39 | are given are declared, the actual function values :math:`y_i` are |
| 39 | 40 | declared in line 2. In line 3, a node array is constructed from the | ... | ... |
doc/source/users/user_manual/modules/support/media.rst
| ... | ... | @@ -14,18 +14,6 @@ media is implemented by the :doxy:`GUrlString` class. |
| 14 | 14 | An example of a class making using of :doxy:`GUrl` is the :doxy:`GXml` class. Look |
| 15 | 15 | at the following code: |
| 16 | 16 | |
| 17 | -**Python** | |
| 18 | - | |
| 19 | -.. code-block:: python | |
| 20 | - :linenos: | |
| 21 | - | |
| 22 | - xml = gammalib.GXml() | |
| 23 | - xml.append(gammalib.GXmlElement('dummy')) | |
| 24 | - file = gammalib.GUrlFile('my_file.xml', 'w') | |
| 25 | - chain = gammalib.GUrlString() | |
| 26 | - xml.write(file) | |
| 27 | - xml.write(chain) | |
| 28 | - | |
| 29 | 17 | **C++** |
| 30 | 18 | |
| 31 | 19 | .. code-block:: cpp |
| ... | ... | @@ -38,6 +26,18 @@ at the following code: |
| 38 | 26 | xml.write(file); |
| 39 | 27 | xml.write(chain); |
| 40 | 28 | |
| 29 | +**Python** | |
| 30 | + | |
| 31 | +.. code-block:: python | |
| 32 | + :linenos: | |
| 33 | + | |
| 34 | + xml = gammalib.GXml() | |
| 35 | + xml.append(gammalib.GXmlElement('dummy')) | |
| 36 | + file = gammalib.GUrlFile('my_file.xml', 'w') | |
| 37 | + chain = gammalib.GUrlString() | |
| 38 | + xml.write(file) | |
| 39 | + xml.write(chain) | |
| 40 | + | |
| 41 | 41 | Line 1 declares a XML object and in line 2 we append a dummy element |
| 42 | 42 | to it. In line 3 we now create a file named ``my_file.xml`` for which |
| 43 | 43 | we allow write access. In line 4 we allocate a string media. We then | ... | ... |
doc/source/users/user_manual/modules/support/random.rst
| ... | ... | @@ -4,17 +4,6 @@ Random number generation |
| 4 | 4 | Random number generation is widely used within GammaLib for drawing event |
| 5 | 5 | distributions from functions. |
| 6 | 6 | |
| 7 | -**Python** | |
| 8 | - | |
| 9 | -.. code-block:: python | |
| 10 | - :linenos: | |
| 11 | - | |
| 12 | - ran = gammalib.GRan() # Construct random number generate instance | |
| 13 | - uniform = ran.uniform() # Uniform random number | |
| 14 | - exponential = ran.exp(3.7) # Exponential random number | |
| 15 | - poisson = ran.poisson(2.4) # Poisson random number | |
| 16 | - chi2 = ran.chisq2() # Chi2 random number | |
| 17 | - | |
| 18 | 7 | **C++** |
| 19 | 8 | |
| 20 | 9 | .. code-block:: cpp |
| ... | ... | @@ -26,17 +15,20 @@ distributions from functions. |
| 26 | 15 | double poisson = ran.poisson(2.4); // Poisson random number |
| 27 | 16 | double chi2 = ran.chisq2(); // Chi2 random number |
| 28 | 17 | |
| 29 | -In line 1 a random number generator is allocated. If control over the | |
| 30 | -seed value of the random number generator is needed (for example to draw | |
| 31 | -different samples), you may specify the seed value upon construction: | |
| 32 | - | |
| 33 | 18 | **Python** |
| 34 | 19 | |
| 35 | 20 | .. code-block:: python |
| 36 | 21 | :linenos: |
| 37 | 22 | |
| 38 | - seed = 123456 | |
| 39 | - ran = gammalib.GRan(seed) | |
| 23 | + ran = gammalib.GRan() # Construct random number generate instance | |
| 24 | + uniform = ran.uniform() # Uniform random number | |
| 25 | + exponential = ran.exp(3.7) # Exponential random number | |
| 26 | + poisson = ran.poisson(2.4) # Poisson random number | |
| 27 | + chi2 = ran.chisq2() # Chi2 random number | |
| 28 | + | |
| 29 | +In line 1 a random number generator is allocated. If control over the | |
| 30 | +seed value of the random number generator is needed (for example to draw | |
| 31 | +different samples), you may specify the seed value upon construction: | |
| 40 | 32 | |
| 41 | 33 | **C++** |
| 42 | 34 | |
| ... | ... | @@ -46,6 +38,14 @@ different samples), you may specify the seed value upon construction: |
| 46 | 38 | unsigned long long int seed = 123456; |
| 47 | 39 | GRan ran(seed); |
| 48 | 40 | |
| 41 | +**Python** | |
| 42 | + | |
| 43 | +.. code-block:: python | |
| 44 | + :linenos: | |
| 45 | + | |
| 46 | + seed = 123456 | |
| 47 | + ran = gammalib.GRan(seed) | |
| 48 | + | |
| 49 | 49 | The :doxy:`GRan::uniform` method returns a random number between 0 and 1. The |
| 50 | 50 | :doxy:`GRan::exp` method returns a random number of the exponential law |
| 51 | 51 | ... | ... |