1This is gnuastro.info, produced by makeinfo version 6.8 from
2gnuastro.texi.
3
4This book documents version 0.16 of the GNU Astronomy Utilities
5(Gnuastro).  Gnuastro provides various programs and libraries for
6astronomical data manipulation and analysis.
7
8   Copyright © 2015-2021, Free Software Foundation, Inc.
9
10     Permission is granted to copy, distribute and/or modify this
11     document under the terms of the GNU Free Documentation License,
12     Version 1.3 or any later version published by the Free Software
13     Foundation; with no Invariant Sections, no Front-Cover Texts, and
14     no Back-Cover Texts.  A copy of the license is included in the
15     section entitled “GNU Free Documentation License”.
16INFO-DIR-SECTION Astronomy
17START-INFO-DIR-ENTRY
18* Gnuastro: (gnuastro).       GNU Astronomy Utilities.
19* libgnuastro: (gnuastro)Gnuastro library. Full Gnuastro library doc.
20
21* help-gnuastro: (gnuastro)help-gnuastro mailing list. Getting help.
22
23* bug-gnuastro: (gnuastro)Report a bug. How to report bugs
24
25* Arithmetic: (gnuastro)Arithmetic. Arithmetic operations on pixels.
26* astarithmetic: (gnuastro)Invoking astarithmetic. Options to Arithmetic.
27
28* BuildProgram: (gnuastro)BuildProgram. Compile and run programs using Gnuastro’s library.
29* astbuildprog: (gnuastro)Invoking astbuildprog. Options to BuildProgram.
30
31* ConvertType: (gnuastro)ConvertType. Convert different file types.
32* astconvertt: (gnuastro)Invoking astconvertt. Options to ConvertType.
33
34* Convolve: (gnuastro)Convolve. Convolve an input file with kernel.
35* astconvolve: (gnuastro)Invoking astconvolve. Options to Convolve.
36
37* CosmicCalculator: (gnuastro)CosmicCalculator. For cosmological params.
38* astcosmiccal: (gnuastro)Invoking astcosmiccal. Options to CosmicCalculator.
39
40* Crop: (gnuastro)Crop. Crop region(s) from image(s).
41* astcrop: (gnuastro)Invoking astcrop. Options to Crop.
42
43* Fits: (gnuastro)Fits. View and manipulate FITS extensions and keywords.
44* astfits: (gnuastro)Invoking astfits. Options to Fits.
45
46* MakeCatalog: (gnuastro)MakeCatalog. Make a catalog from labeled image.
47* astmkcatalog: (gnuastro)Invoking astmkcatalog. Options to MakeCatalog.
48
49* MakeNoise: (gnuastro)MakeNoise. Make (add) noise to an image.
50* astmknoise: (gnuastro)Invoking astmknoise. Options to MakeNoise.
51
52* MakeProfiles: (gnuastro)MakeProfiles. Make mock profiles.
53* astmkprof: (gnuastro)Invoking astmkprof. Options to MakeProfiles.
54
55* Match: (gnuastro)Match. Match two separate catalogs.
56* astmatch: (gnuastro)Invoking astmatch. Options to Match.
57
58* NoiseChisel: (gnuastro)NoiseChisel. Detect signal in noise.
59* astnoisechisel: (gnuastro)Invoking astnoisechisel. Options to NoiseChisel.
60
61* Segment: (gnuastro)Segment. Segment detections based on signal structure.
62* astsegment: (gnuastro)Invoking astsegment. Options to Segment.
63
64* Query: (gnuastro)Query. Access remote databases for downloading data.
65* astquery: (gnuastro)Invoking astquery. Options to Query.
66
67* Statistics: (gnuastro)Statistics. Get image Statistics.
68* aststatistics: (gnuastro)Invoking aststatistics. Options to Statistics.
69
70* Table: (gnuastro)Table. Read and write FITS binary or ASCII tables.
71* asttable: (gnuastro)Invoking asttable. Options to Table.
72
73* Warp: (gnuastro)Warp. Warp a dataset to a new grid.
74* astwarp: (gnuastro)Invoking astwarp. Options to Warp.
75
76* astscript: (gnuastro)Installed scripts. Gnuastro’s installed scripts.
77* astscript-sort-by-night: (gnuastro)Invoking astscript-sort-by-night. Options to this script
78* astscript-radial-profile: (gnuastro)Invoking astscript-radial-profile. Options to this script
79* astscript-ds9-region: (gnuastro)Invoking astscript-ds9-region. Options to this script
80
81END-INFO-DIR-ENTRY
82
83
84File: gnuastro.info,  Node: Top,  Next: Introduction,  Prev: (dir),  Up: (dir)
85
86GNU Astronomy Utilities
87***********************
88
89This book documents version 0.16 of the GNU Astronomy Utilities
90(Gnuastro).  Gnuastro provides various programs and libraries for
91astronomical data manipulation and analysis.
92
93   Copyright © 2015-2021, Free Software Foundation, Inc.
94
95     Permission is granted to copy, distribute and/or modify this
96     document under the terms of the GNU Free Documentation License,
97     Version 1.3 or any later version published by the Free Software
98     Foundation; with no Invariant Sections, no Front-Cover Texts, and
99     no Back-Cover Texts.  A copy of the license is included in the
100     section entitled “GNU Free Documentation License”.
101
102* Menu:
103
104* Introduction::                General introduction.
105* Tutorials::                   Tutorials or Cookbooks.
106* Installation::                Requirements and installation.
107* Common program behavior::     Common behavior in all programs.
108* Data containers::             Tools to operate on extensions and tables.
109* Data manipulation::           Tools for basic image manipulation.
110* Data analysis::               Analyze images.
111* Modeling and fittings::       Make and fit models.
112* High-level calculations::     Physical calculations.
113* Installed scripts::           Installed scripts that operate like programs.
114* Library::                     Gnuastro’s library of useful functions.
115* Developing::                  The development environment.
116* Gnuastro programs list::      List and short summary of Gnuastro.
117* Other useful software::       Installing other useful software.
118* GNU Free Doc. License::       Full text of the GNU Free documentation license.
119* GNU General Public License::  Full text of the GNU General public license.
120* Index::                       Index of terms
121
122 — The Detailed Node Listing —
123
124Introduction
125
126* Quick start::                 A quick start to installation.
127* Science and its tools::       Some philosophy and history.
128* Your rights::                 User rights.
129* Naming convention::           About names of programs in Gnuastro.
130* Version numbering::           Understanding version numbers.
131* New to GNU/Linux?::           Suggested GNU/Linux distribution.
132* Report a bug::                Search and report the bug you found.
133* Suggest new feature::         How to suggest a new feature.
134* Announcements::               How to stay up to date with Gnuastro.
135* Conventions::                 Conventions used in this book.
136* Acknowledgments::             People who helped in the production.
137
138Version numbering
139
140* GNU Astronomy Utilities 1.0::  Plans for version 1.0 release
141
142New to GNU/Linux?
143
144* Command-line interface::      Introduction to the command-line
145
146Tutorials
147
148* Sufi simulates a detection::  Simulating a detection.
149* General program usage tutorial::  Tutorial on many programs in generic scenario.
150* Detecting large extended targets::  NoiseChisel for huge extended targets.
151
152Sufi simulates a detection
153
154* General program usage tutorial::
155
156General program usage tutorial
157
158* Calling Gnuastro's programs::  Easy way to find Gnuastro’s programs.
159* Accessing documentation::     Access to manual of programs you are running.
160* Setup and data download::     Setup this template and download datasets.
161* Dataset inspection and cropping::  Crop the flat region to use in next steps.
162* Angular coverage on the sky::  Measure the field size on the sky.
163* Cosmological coverage::       Measure the field size at different redshifts.
164* Building custom programs with the library::  Easy way to build new programs.
165* Option management and configuration files::  Dealing with options and configuring them.
166* Warping to a new pixel grid::  Transforming/warping the dataset.
167* NoiseChisel and Multiextension FITS files::  Running NoiseChisel and having multiple HDUs.
168* NoiseChisel optimization for detection::  Check NoiseChisel’s operation and improve it.
169* NoiseChisel optimization for storage::  Dramatically decrease output’s volume.
170* Segmentation and making a catalog::  Finding true peaks and creating a catalog.
171* Working with catalogs estimating colors::  Estimating colors using the catalogs.
172* Column statistics color-magnitude diagram::  Visualizing column correlations.
173* Aperture photometry::         Doing photometry on a fixed aperture.
174* Matching catalogs::           Easily find corresponding rows from two catalogs.
175* Finding reddest clumps and visual inspection::  Selecting some targets and inspecting them.
176* Writing scripts to automate the steps::  Scripts will greatly help in re-doing things fast.
177* Citing and acknowledging Gnuastro::  How to cite and acknowledge Gnuastro in your papers.
178
179Detecting large extended targets
180
181* Downloading and validating input data::  How to get and check the input data.
182* NoiseChisel optimization::    Detect the extended and diffuse wings.
183* Image surface brightness limit::  Standards to quantify the noise level.
184* Achieved surface brightness level::  Calculate the outer surface brightness.
185* Extract clumps and objects::  Find sub-structure over the detections.
186
187Installation
188
189* Dependencies::                Necessary packages for Gnuastro.
190* Downloading the source::      Ways to download the source code.
191* Build and install::           Configure, build and install Gnuastro.
192
193Dependencies
194
195* Mandatory dependencies::      Gnuastro will not install without these.
196* Optional dependencies::       Adding more functionality.
197* Bootstrapping dependencies::  If you have the version controlled source.
198* Dependencies from package managers::  Installing from OS package managers.
199
200Mandatory dependencies
201
202* GNU Scientific Library::      Installing GSL.
203* CFITSIO::                     C interface to the FITS standard.
204* WCSLIB::                      C interface to the WCS standard of FITS.
205
206Downloading the source
207
208* Release tarball::             Download a stable official release.
209* Version controlled source::   Get and use the version controlled source.
210
211Version controlled source
212
213* Bootstrapping::               Adding all the automatically generated files.
214* Synchronizing::               Keep your local clone up to date.
215
216Build and install
217
218* Configuring::                 Configure Gnuastro
219* Separate build and source directories::  Keeping derivate/build files separate.
220* Tests::                       Run tests to see if it is working.
221* A4 print book::               Customize the print book.
222* Known issues::                Issues you might encounter.
223
224Configuring
225
226* Gnuastro configure options::  Configure options particular to Gnuastro.
227* Installation directory::      Specify the directory to install.
228* Executable names::            Changing executable names.
229* Configure and build in RAM::  For minimal use of HDD or SSD, and clean source.
230
231Common program behavior
232
233* Command-line::                How to use the command-line.
234* Configuration files::         Values for unspecified variables.
235* Getting help::                Getting more information on the go.
236* Multi-threaded operations::   How threads are managed in Gnuastro.
237* Numeric data types::          Different types and how to specify them.
238* Memory management::           How memory is allocated (in RAM or HDD/SSD).
239* Tables::                      Recognized table formats.
240* Tessellation::                Tile the dataset into non-overlapping bins.
241* Automatic output::            About automatic output names.
242* Output FITS files::           Common properties when outputs are FITS.
243
244Command-line
245
246* Arguments and options::       Different ways to specify inputs and configuration.
247* Common options::              Options that are shared between all programs.
248* Shell TAB completion::        Customized TAB completion in Gnuastro.
249* Standard input::              Using output of another program as input.
250
251Arguments and options
252
253* Arguments::                   For specifying the main input files/operations.
254* Options::                     For configuring the behavior of the program.
255
256Common options
257
258* Input output options::        Common input/output options.
259* Processing options::          Options for common processing steps.
260* Operating mode options::      Common operating mode options.
261
262Configuration files
263
264* Configuration file format::   ASCII format of configuration file.
265* Configuration file precedence::  Precedence of configuration files.
266* Current directory and User wide::  Local and user configuration files.
267* System wide::                 System wide configuration files.
268
269Getting help
270
271* --usage::                     View option names and value formats.
272* --help::                      List all options with description.
273* Man pages::                   Man pages generated from –help.
274* Info::                        View complete book in terminal.
275* help-gnuastro mailing list::  Contacting experienced users.
276
277Multi-threaded operations
278
279* A note on threads::           Caution and suggestion on using threads.
280* How to run simultaneous operations::  How to run things simultaneously.
281
282Tables
283
284* Recognized table formats::    Table formats that are recognized in Gnuastro.
285* Gnuastro text table format::  Gnuastro’s convention plain text tables.
286* Selecting table columns::     Identify/select certain columns from a table
287
288Recognized table formats
289
290* Gnuastro text table format::  Reading plain text tables
291
292Data containers
293
294* Fits::                        View and manipulate extensions and keywords.
295* ConvertType::                 Convert data to various formats.
296* Table::                       Read and Write FITS tables to plain text.
297* Query::                       Import data from external databases.
298
299Fits
300
301* Invoking astfits::            Arguments and options to Header.
302
303Invoking Fits
304
305* HDU information and manipulation::  Learn about the HDUs and move them.
306* Keyword inspection and manipulation::  Manipulate metadata keywords in a HDU
307
308ConvertType
309
310* Recognized file formats::     Recognized file formats
311* Color::                       Some explanations on color.
312* Aligning images with small WCS offsets::  When the WCS slightly differs.
313* Annotations for figure in paper::  Adding coordinates or physical scale.
314* Invoking astconvertt::        Options and arguments to ConvertType.
315
316Annotations for figure in paper
317
318* Full script of annotations on figure::  All the steps in one script
319
320Table
321
322* Column arithmetic::           How to do operations on table columns.
323* Operation precedence in Table::  Order of running options in Table.
324* Invoking asttable::           Options and arguments to Table.
325
326Query
327
328* Available databases::         List of available databases to Query.
329* Invoking astquery::           Inputs, outputs and configuration of Query.
330
331Data manipulation
332
333* Crop::                        Crop region(s) from a dataset.
334* Arithmetic::                  Arithmetic on input data.
335* Convolve::                    Convolve an image with a kernel.
336* Warp::                        Warp/Transform an image to a different grid.
337
338Crop
339
340* Crop modes::                  Basic modes to define crop region.
341* Crop section syntax::         How to define a section to crop.
342* Blank pixels::                Pixels with no value.
343* Invoking astcrop::            Calling Crop on the command-line
344
345Invoking Crop
346
347* Crop options::                A list of all the options with explanation.
348* Crop output::                 The outputs of Crop.
349* Crop known issues::           Known issues in running Crop.
350
351Arithmetic
352
353* Reverse polish notation::     The current notation style for Arithmetic
354* Arithmetic operators::        List of operators known to Arithmetic
355* Invoking astarithmetic::      How to run Arithmetic: options and output
356
357Arithmetic operators
358
359* Basic mathematical operators::  For example +, -, /, log, pow, and etc.
360* Trigonometric and hyperbolic operators::  sin, cos, atan, asinh, and etc
361* Unit conversion operators::   magnitudes to counts, or parsecs to AUs, and etc.
362* Statistical operators::       Statistics of a single dataset (for example mean).
363* Stacking operators::          Coadding or combining multiple datasets into one.
364* Filtering operators::         Smoothing a dataset through mixing pixel with neighbors.
365* Interpolation operators::     Giving blank pixels a value.
366* Dimensionality changing operators::  Collapse or expand a dataset.
367* Conditional operators::       Select certain pixels within the dataset.
368* Mathematical morphology operators::  Work on binary images, for example erode.
369* Bitwise operators::           Work on bits within one pixel.
370* Numerical type conversion operators::  Convert the numeric datatype of a dataset.
371* Adding noise operators::      Add noise to a dataset.
372* Elliptical shape operators::  Operations that are focused on an ellipse.
373* Building new dataset::        How to construct an empty dataset from scratch.
374* Operand storage in memory or a file::  Tools for complex operations in one command.
375
376Convolve
377
378* Spatial domain convolution::  Only using the input image values.
379* Frequency domain and Fourier operations::  Using frequencies in input.
380* Spatial vs. Frequency domain::  When to use which?
381* Convolution kernel::          How to specify the convolution kernel.
382* Invoking astconvolve::        Options and argument to Convolve.
383
384Spatial domain convolution
385
386* Convolution process::         More basic explanations.
387* Edges in the spatial domain::  Dealing with the edges of an image.
388
389Frequency domain and Fourier operations
390
391* Fourier series historical background::  Historical background.
392* Circles and the complex plane::  Interpreting complex numbers.
393* Fourier series::              Fourier Series definition.
394* Fourier transform::           Fourier Transform definition.
395* Dirac delta and comb::        Dirac delta and Dirac comb.
396* Convolution theorem::         Derivation of Convolution theorem.
397* Sampling theorem::            Sampling theorem (Nyquist frequency).
398* Discrete Fourier transform::  Derivation and explanation of DFT.
399* Fourier operations in two dimensions::  Extend to 2D images.
400* Edges in the frequency domain::  Interpretation of edge effects.
401
402Warp
403
404* Warping basics::              Basics of coordinate transformation.
405* Merging multiple warpings::   How to merge multiple matrices.
406* Resampling::                  Warping an image is re-sampling it.
407* Invoking astwarp::            Arguments and options for Warp.
408
409Data analysis
410
411* Statistics::                  Calculate dataset statistics.
412* NoiseChisel::                 Detect objects in an image.
413* Segment::                     Segment detections based on signal structure.
414* MakeCatalog::                 Catalog from input and labeled images.
415* Match::                       Match two datasets.
416
417Statistics
418
419* Histogram and Cumulative Frequency Plot::  Basic definitions.
420* 2D Histograms::               Plotting the distribution of two variables.
421* Sigma clipping::              Definition of $\sigma$-clipping.
422* Sky value::                   Definition and derivation of the Sky value.
423* Invoking aststatistics::      Arguments and options to Statistics.
424
4252D Histograms
426
427* 2D histogram as a table::     Format and usage in table format.
428* 2D histogram as an image::    Format and usage in image format
429
430Sky value
431
432* Sky value definition::        Definition of the Sky/reference value.
433* Sky value misconceptions::    Wrong methods to estimate the Sky value.
434* Quantifying signal in a tile::  Method to estimate the presence of signal.
435
436NoiseChisel
437
438* NoiseChisel changes after publication::  Updates since published papers.
439* Invoking astnoisechisel::     Options and arguments for NoiseChisel.
440
441Invoking NoiseChisel
442
443* NoiseChisel input::           NoiseChisel’s input options.
444* Detection options::           Configure detection in NoiseChisel.
445* NoiseChisel output::          NoiseChisel’s output options and format.
446
447Segment
448
449* Invoking astsegment::         Inputs, outputs and options to Segment
450
451Invoking Segment
452
453* Segment input::               Input files and options.
454* Segmentation options::        Parameters of the segmentation process.
455* Segment output::              Outputs of Segment
456
457MakeCatalog
458
459* Detection and catalog production::  Discussing why/how to treat these separately
460* Brightness flux magnitude::   More on Magnitudes, surface brightness and etc.
461* Quantifying measurement limits::  For comparing different catalogs.
462* Measuring elliptical parameters::  Estimating elliptical parameters.
463* Adding new columns to MakeCatalog::  How to add new columns.
464* Invoking astmkcatalog::       Options and arguments to MakeCatalog.
465
466Quantifying measurement limits
467
468* Magnitude measurement error of each detection::  Derivation of mag error equation
469* Surface brightness error of each detection::  Error in measuring the Surface brightness.
470* Completeness limit of each detection::  Possibility of detecting similar objects?
471* Upper limit magnitude of each detection::  How reliable is your magnitude?
472* Surface brightness limit of image::  How deep is your data?
473* Upper limit magnitude of image::  How deep is your data for certain footprint?
474
475Invoking MakeCatalog
476
477* MakeCatalog inputs and basic settings::  Input files and basic settings.
478* Upper-limit settings::        Settings for upper-limit measurements.
479* MakeCatalog measurements::    Available measurements in MakeCatalog.
480* MakeCatalog output::          File names of MakeCatalog’s output table.
481
482Match
483
484* Invoking astmatch::           Inputs, outputs and options of Match
485
486Modeling and fitting
487
488* MakeProfiles::                Making mock galaxies and stars.
489* MakeNoise::                   Make (add) noise to an image.
490
491MakeProfiles
492
493* Modeling basics::             Astronomical modeling basics.
494* If convolving afterwards::    Considerations for convolving later.
495* Profile magnitude::           Definition of total profile magnitude.
496* Invoking astmkprof::          Inputs and Options for MakeProfiles.
497
498Modeling basics
499
500* Defining an ellipse and ellipsoid::  Definition of these important shapes.
501* PSF::                         Radial profiles for the PSF.
502* Stars::                       Making mock star profiles.
503* Galaxies::                    Radial profiles for galaxies.
504* Sampling from a function::    Sample a function on a pixelated canvas.
505* Oversampling::                Oversampling the model.
506
507Invoking MakeProfiles
508
509* MakeProfiles catalog::        Required catalog properties.
510* MakeProfiles profile settings::  Configuration parameters for all profiles.
511* MakeProfiles output dataset::  The canvas/dataset to build profiles over.
512* MakeProfiles log file::       A description of the optional log file.
513
514MakeNoise
515
516* Noise basics::                Noise concepts and definitions.
517* Invoking astmknoise::         Options and arguments to MakeNoise.
518
519Noise basics
520
521* Photon counting noise::       Poisson noise
522* Instrumental noise::          Readout, dark current and other sources.
523* Final noised pixel value::    How the final noised value is calculated.
524* Generating random numbers::   How random numbers are generated.
525
526High-level calculations
527
528* CosmicCalculator::            Calculate cosmological variables
529
530CosmicCalculator
531
532* Distance on a 2D curved space::  Distances in 2D for simplicity
533* Extending distance concepts to 3D::  Going to 3D (our real universe).
534* Invoking astcosmiccal::       How to run CosmicCalculator
535
536Invoking CosmicCalculator
537
538* CosmicCalculator input options::  Options to specify input conditions.
539* CosmicCalculator basic cosmology calculations::  Like distance modulus, distances and etc.
540* CosmicCalculator spectral line calculations::  How they get affected by redshift.
541
542Installed scripts
543
544* Sort FITS files by night::    Sort many files by date.
545* Generate radial profile::     Radial profile of an object in an image.
546* SAO DS9 region files from table::  Create ds9 region file from a table.
547
548Sort FITS files by night
549
550* Invoking astscript-sort-by-night::  Inputs and outputs to this script.
551
552Generate radial profile
553
554* Invoking astscript-radial-profile::  How to call astscript-radial-profile
555
556SAO DS9 region files from table
557
558* Invoking astscript-ds9-region::  How to call astscript-ds9-region
559
560Library
561
562* Review of library fundamentals::  Guide on libraries and linking.
563* BuildProgram::                Link and run source files with this library.
564* Gnuastro library::            Description of all library functions.
565* Library demo programs::       Demonstration for using libraries.
566
567Review of library fundamentals
568
569* Headers::                     Header files included in source.
570* Linking::                     Linking the compiled source files into one.
571* Summary and example on libraries::  A summary and example on using libraries.
572
573BuildProgram
574
575* Invoking astbuildprog::       Options and examples for using this program.
576
577Gnuastro library
578
579* Configuration information::   General information about library config.
580* Multithreaded programming::   Tools for easy multi-threaded operations.
581* Library data types::          Definitions and functions for types.
582* Pointers::                    Wrappers for easy working with pointers.**
583* Library blank values::        Blank values and functions to deal with them.
584* Library data container::      General data container in Gnuastro.
585* Dimensions::                  Dealing with coordinates and dimensions.
586* Linked lists::                Various types of linked lists.
587* Array input output::          Reading and writing images or cubes.
588* Table input output::          Reading and writing table columns.
589* FITS files::                  Working with FITS data.
590* File input output::           Reading and writing to various file formats.
591* World Coordinate System::     Dealing with the world coordinate system.
592* Arithmetic on datasets::      Arithmetic operations on a dataset.
593* Tessellation library::        Functions for working on tiles.
594* Bounding box::                Finding the bounding box.
595* Polygons::                    Working with the vertices of a polygon.
596* Qsort functions::             Helper functions for Qsort.
597* K-d tree::                    Space partitioning in K dimensions.
598* Permutations::                Re-order (or permute) the values in a dataset.
599* Matching::                    Matching catalogs based on position.
600* Statistical operations::      Functions for basic statistics.
601* Binary datasets::             Datasets that can only have values of 0 or 1.
602* Labeled datasets::            Working with Segmented/labeled datasets.
603* Convolution functions::       Library functions to do convolution.
604* Interpolation::               Interpolate (over blank values possibly).
605* Git wrappers::                Wrappers for functions in libgit2.
606* Unit conversion library::     Converting between recognized units.
607* Spectral lines library::      Functions for operating on Spectral lines.
608* Cosmology library::           Cosmological calculations.
609* SAO DS9 library::             Take inputs from files generated by SAO DS9.
610
611Multithreaded programming (‘threads.h’)
612
613* Implementation of pthread_barrier::  Some systems don’t have pthread_barrier
614* Gnuastro's thread related functions::  Functions for managing threads.
615
616Data container (‘data.h’)
617
618* Generic data container::      Definition of Gnuastro’s generic container.
619* Dataset allocation::          Allocate, initialize and free a dataset.
620* Arrays of datasets::          Functions to help with array of datasets.
621* Copying datasets::            Functions to copy a dataset to a new one.
622
623Linked lists (‘list.h’)
624
625* List of strings::             Simply linked list of strings.
626* List of int32_t::             Simply linked list of int32_ts.
627* List of size_t::              Simply linked list of size_ts.
628* List of float::               Simply linked list of floats.
629* List of double::              Simply linked list of doubles
630* List of void::                Simply linked list of void * pointers.
631* Ordered list of size_t::      Simply linked, ordered list of size_t.
632* Doubly linked ordered list of size_t::  Definition and functions.
633* List of gal_data_t::          Simply linked list Gnuastro’s generic datatype.
634
635FITS files (‘fits.h’)
636
637* FITS macros errors filenames::  General macros, errors and checking names.
638* CFITSIO and Gnuastro types::  Conversion between FITS and Gnuastro types.
639* FITS HDUs::                   Opening and getting information about HDUs.
640* FITS header keywords::        Reading and writing FITS header keywords.
641* FITS arrays::                 Reading and writing FITS images/arrays.
642* FITS tables::                 Reading and writing FITS tables.
643
644File input output
645
646* Text files::                  Reading and writing from/to plain text files.
647* TIFF files::                  Reading and writing from/to TIFF files.
648* JPEG files::                  Reading and writing from/to JPEG files.
649* EPS files::                   Writing to EPS files.
650* PDF files::                   Writing to PDF files.
651
652Tessellation library (‘tile.h’)
653
654* Independent tiles::           Work on or check independent tiles.
655* Tile grid::                   Cover a full dataset with non-overlapping tiles.
656
657Library demo programs
658
659* Library demo - reading a image::  Read a FITS image into memory.
660* Library demo - inspecting neighbors::  Inspect the neighbors of a pixel.
661* Library demo - multi-threaded operation::  Doing an operation on threads.
662* Library demo - reading and writing table columns::  Simple Column I/O.
663
664Developing
665
666* Why C::                       Why Gnuastro is designed in C.
667* Program design philosophy::   General ideas behind the package structure.
668* Coding conventions::          Gnuastro coding conventions.
669* Program source::              Conventions for the code.
670* Documentation::               Documentation is an integral part of Gnuastro.
671* Building and debugging::      Build and possibly debug during development.
672* Test scripts::                Understanding the test scripts.
673* Bash programmable completion::  Auto-completions for better user experience.
674* Developer's checklist::       Checklist to finalize your changes.
675* Gnuastro project webpage::    Central hub for Gnuastro activities.
676* Developing mailing lists::    Stay up to date with Gnuastro’s development.
677* Contributing to Gnuastro::    Share your changes with all users.
678
679Program source
680
681* Mandatory source code files::  Description of files common to all programs.
682* The TEMPLATE program::        Template for easy creation of a new program.
683
684Bash programmable completion
685
686* Bash TAB completion tutorial::  Fast tutorial to get you started on concepts.
687* Implementing TAB completion in Gnuastro::  How Gnuastro uses Bash auto-completion features.
688
689Contributing to Gnuastro
690
691* Copyright assignment::        Copyright has to be assigned to the FSF.
692* Commit guidelines::           Guidelines for commit messages.
693* Production workflow::         Submitting your commits (work) for inclusion.
694* Forking tutorial::            Tutorial on workflow steps with Git.
695
696Other useful software
697
698* SAO DS9::                     Viewing FITS images.
699* PGPLOT::                      Plotting directly in C
700
701SAO DS9
702
703* Viewing multiextension FITS images::  Configure SAO DS9 for multiextension images.
704
705
706
707File: gnuastro.info,  Node: Introduction,  Next: Tutorials,  Prev: Top,  Up: Top
708
7091 Introduction
710**************
711
712GNU Astronomy Utilities (Gnuastro) is an official GNU package consisting
713of separate programs and libraries for the manipulation and analysis of
714astronomical data.  All the programs share the same basic command-line
715user interface for the comfort of both the users and developers.
716Gnuastro is written to comply fully with the GNU coding standards so it
717integrates finely with the GNU/Linux operating system.  This also
718enables astronomers to expect a fully familiar experience in the source
719code, building, installing and command-line user interaction that they
720have seen in all the other GNU software that they use.  The official and
721always up to date version of this book (or manual) is freely available
722under *note GNU Free Doc. License:: in various formats (PDF, HTML, plain
723text, info, and as its Texinfo source) at
724<http://www.gnu.org/software/gnuastro/manual/>.
725
726   For users who are new to the GNU/Linux environment, unless otherwise
727specified most of the topics in *note Installation:: and *note Common
728program behavior:: are common to all GNU software, for example
729installation, managing command-line options or getting help (also see
730*note New to GNU/Linux?::).  So if you are new to this empowering
731environment, we encourage you to go through these chapters carefully.
732They can be a starting point from which you can continue to learn more
733from each program’s own manual and fully benefit from and enjoy this
734wonderful environment.  Gnuastro also comes with a large set of
735libraries, so you can write your own programs using Gnuastro’s building
736blocks, see *note Review of library fundamentals:: for an introduction.
737
738   In Gnuastro, no change to any program or library will be committed to
739its history, before it has been fully documented here first.  As
740discussed in *note Science and its tools:: this is a founding principle
741of the Gnuastro.
742
743* Menu:
744
745* Quick start::                 A quick start to installation.
746* Science and its tools::       Some philosophy and history.
747* Your rights::                 User rights.
748* Naming convention::           About names of programs in Gnuastro.
749* Version numbering::           Understanding version numbers.
750* New to GNU/Linux?::           Suggested GNU/Linux distribution.
751* Report a bug::                Search and report the bug you found.
752* Suggest new feature::         How to suggest a new feature.
753* Announcements::               How to stay up to date with Gnuastro.
754* Conventions::                 Conventions used in this book.
755* Acknowledgments::             People who helped in the production.
756
757
758File: gnuastro.info,  Node: Quick start,  Next: Science and its tools,  Prev: Introduction,  Up: Introduction
759
7601.1 Quick start
761===============
762
763The latest official release tarball is always available as
764gnuastro-latest.tar.gz765(http://ftp.gnu.org/gnu/gnuastro/gnuastro-latest.tar.gz).  For better
766compression (faster download), and robust archival features, an Lzip
767(http://www.nongnu.org/lzip/lzip.html) compressed tarball is also
768available at ‘gnuastro-latest.tar.lz769(http://ftp.gnu.org/gnu/gnuastro/gnuastro-latest.tar.lz), see *note
770Release tarball:: for more details on the tarball release(1).
771
772   Let’s assume the downloaded tarball is in the ‘TOPGNUASTRO’
773directory.  The first two commands below can be used to decompress the
774source.  If you download ‘tar.lz’ and your Tar implementation doesn’t
775recognize Lzip (the second command fails), run the third and fourth
776lines(2).  Note that lines starting with ‘##’ don’t need to be typed.
777
778     ## Go into the download directory.
779     $ cd TOPGNUASTRO
780
781     ## Also works on `tar.gz'. GNU Tar recognizes both formats.
782     $ tar xf gnuastro-latest.tar.lz
783
784     ## Only when the previous command fails.
785     $ lzip -d gnuastro-latest.tar.lz
786     $ tar xf gnuastro-latest.tar
787
788   Gnuastro has three mandatory dependencies and some optional
789dependencies for extra functionality, see *note Dependencies:: for the
790full list.  In *note Dependencies from package managers:: we have
791prepared the command to easily install Gnuastro’s dependencies using the
792package manager of some operating systems.  When the mandatory
793dependencies are ready, you can configure, compile, check and install
794Gnuastro on your system with the following commands.  See *note Known
795issues:: if you confront any complications.
796
797     $ cd gnuastro-X.X                  # Replace X.X with version number.
798     $ ./configure
799     $ make -j8                         # Replace 8 with no. CPU threads.
800     $ make check -j8                   # Replace 8 with no. CPU threads.
801     $ sudo make install
802     $ echo "source /usr/local/share/gnuastro/completion.bash" >> ~/.bashrc
803
804The last command is to enable Gnuastro’s custom TAB completion in Bash.
805For more on this useful feature, see *note Shell TAB completion::).
806
807   For each program there is an ‘Invoke ProgramName’ sub-section in this
808book which explains how the programs should be run on the command-line
809(for example *note Invoking asttable::).
810
811   Some complete Tutorials are also available in this book with common
812Gnuastro usage scenarios in astronomical research.  They even contain
813links to download the necessary data, and thoroughly describe every step
814of the process (the science, statistics and optimal usage of the
815command-line).  We therefore strongly recommend to follow the tutorials
816before starting to use Gnuastro, see *note Tutorials::.
817
818   ---------- Footnotes ----------
819
820   (1) The Gzip library and program are commonly available on most
821systems.  However, Gnuastro recommends Lzip as described above and the
822beta-releases are also only distributed in ‘tar.lz’.  You can download
823and install Lzip’s source (in ‘.tar.gz’ format) from its web page and
824follow the same process as below: Lzip has no dependencies, so simply
825decompress, then run ‘./configure’, ‘make’, ‘sudo make install’.
826
827   (2) In case Tar doesn’t directly uncompress your ‘.tar.lz’ tarball,
828you can merge the separate calls to Lzip and Tar (shown in the main body
829of text) into one command by directly piping the output of Lzip into Tar
830with a command like this: ‘$ lzip -cd gnuastro-0.5.tar.lz | tar -xf -’
831
832
833File: gnuastro.info,  Node: Science and its tools,  Next: Your rights,  Prev: Quick start,  Up: Introduction
834
8351.2 Gnuastro manifesto: Science and its tools
836=============================================
837
838History of science indicates that there are always inevitably unseen
839faults, hidden assumptions, simplifications and approximations in all
840our theoretical models, data acquisition and analysis techniques.  It is
841precisely these that will ultimately allow future generations to advance
842the existing experimental and theoretical knowledge through their new
843solutions and corrections.
844
845   In the past, scientists would gather data and process them
846individually to achieve an analysis thus having a much more intricate
847knowledge of the data and analysis.  The theoretical models also
848required little (if any) simulations to compare with the data.  Today
849both methods are becoming increasingly more dependent on pre-written
850software.  Scientists are dissociating themselves from the intricacies
851of reducing raw observational data in experimentation or from bringing
852the theoretical models to life in simulations.  These ‘intricacies’ are
853precisely those unseen faults, hidden assumptions, simplifications and
854approximations that define scientific progress.
855
856     Unfortunately, most persons who have recourse to a computer for
857     statistical analysis of data are not much interested either in
858     computer programming or in statistical method, being primarily
859     concerned with their own proper business.  Hence the common use of
860     library programs and various statistical packages.  ...  It’s time
861     that was changed.
862_F.J. Anscombe. The American Statistician, Vol. 27, No. 1. 1973_
863
864   Anscombe’s quartet
865(http://en.wikipedia.org/wiki/Anscombe%27s_quartet) demonstrates how
866four data sets with widely different shapes (when plotted) give nearly
867identical output from standard regression techniques.  Anscombe uses
868this (now famous) quartet, which was introduced in the paper quoted
869above, to argue that “_Good statistical analysis is not a purely routine
870matter, and generally calls for more than one pass through the
871computer_”.  Echoing Anscombe’s concern after 44 years, some of the
872highly recognized statisticians of our time (Leek, McShane, Gelman,
873Colquhoun, Nuijten and Goodman), wrote in Nature that:
874
875     We need to appreciate that data analysis is not purely
876     computational and algorithmic – it is a human
877     behaviour....Researchers who hunt hard enough will turn up a result
878     that fits statistical criteria – but their discovery will probably
879     be a false positive.
880        — _Five ways to fix statistics, Nature, 551, Nov 2017._
881
882   Users of statistical (scientific) methods (software) are therefore
883not passive (objective) agents in their result.  Therefore, it is
884necessary to actually understand the method, not just use it as a black
885box.  The subjective experience gained by frequently using a
886method/software is not sufficient to claim an understanding of how the
887tool/method works and how relevant it is to the data and analysis.  This
888kind of subjective experience is prone to serious misunderstandings
889about the data, what the software/statistical-method really does
890(especially as it gets more complicated), and thus the scientific
891interpretation of the result.  This attitude is further encouraged
892through non-free software(1), poorly written (or non-existent)
893scientific software manuals, and non-reproducible papers(2).  This
894approach to scientific software and methods only helps in producing
895dogmas and an “_obscurantist faith in the expert’s special skill, and in
896his personal knowledge and authority_”(3).
897
898     Program or be programmed.  Choose the former, and you gain access
899     to the control panel of civilization.  Choose the latter, and it
900     could be the last real choice you get to make.
901   — _Douglas Rushkoff. Program or be programmed, O/R Books (2010)._
902
903   It is obviously impractical for any one human being to gain the
904intricate knowledge explained above for every step of an analysis.  On
905the other hand, scientific data can be large and numerous, for example
906images produced by telescopes in astronomy.  This requires efficient
907algorithms.  To make things worse, natural scientists have generally not
908been trained in the advanced software techniques, paradigms and
909architecture that are taught in computer science or engineering courses
910and thus used in most software.  The GNU Astronomy Utilities are an
911effort to tackle this issue.
912
913   Gnuastro is not just a software, this book is as important to the
914idea behind Gnuastro as the source code (software).  This book has tried
915to learn from the success of the “Numerical Recipes” book in educating
916those who are not software engineers and computer scientists but still
917heavy users of computational algorithms, like astronomers.  There are
918two major differences.
919
920   The first difference is that Gnuastro’s code and the background
921information are segregated: the code is moved within the actual Gnuastro
922software source code and the underlying explanations are given here in
923this book.  In the source code, every non-trivial step is heavily
924commented and correlated with this book, it follows the same logic of
925this book, and all the programs follow a similar internal data, function
926and file structure, see *note Program source::.  Complementing the code,
927this book focuses on thoroughly explaining the concepts behind those
928codes (history, mathematics, science, software and usage advise when
929necessary) along with detailed instructions on how to run the programs.
930At the expense of frustrating “professionals” or “experts”, this book
931and the comments in the code also intentionally avoid jargon and
932abbreviations.  The source code and this book are thus intimately
933linked, and when considered as a single entity can be thought of as a
934real (an actual software accompanying the algorithms) “Numerical
935Recipes” for astronomy.
936
937   The second major, and arguably more important, difference is that
938“Numerical Recipes” does not allow you to distribute any code that you
939have learned from it.  In other words, it does not allow you to release
940your software’s source code if you have used their codes, you can only
941publicly release binaries (a black box) to the community.  Therefore,
942while it empowers the privileged individual who has access to it, it
943exacerbates social ignorance.  Exactly at the opposite end of the
944spectrum, Gnuastro’s source code is released under the GNU general
945public license (GPL) and this book is released under the GNU free
946documentation license.  You are therefore free to distribute any
947software you create using parts of Gnuastro’s source code or text, or
948figures from this book, see *note Your rights::.
949
950   With these principles in mind, Gnuastro’s developers aim to impose
951the minimum requirements on you (in computer science, engineering and
952even the mathematics behind the tools) to understand and modify any step
953of Gnuastro if you feel the need to do so, see *note Why C:: and *note
954Program design philosophy::.
955
956   Without prior familiarity and experience with optics, it is hard to
957imagine how, Galileo could have come up with the idea of modifying the
958Dutch military telescope optics to use in astronomy.  Astronomical
959objects could not be seen with the Dutch military design of the
960telescope.  In other words, it is unlikely that Galileo could have asked
961a random optician to make modifications (not understood by Galileo) to
962the Dutch design, to do something no astronomer of the time took
963seriously.  In the paradigm of the day, what could be the purpose of
964enlarging geometric spheres (planets) or points (stars)?  In that
965paradigm only the position and movement of the heavenly bodies was
966important, and that had already been accurately studied (recently by
967Tycho Brahe).
968
969   In the beginning of his “The Sidereal Messenger” (published in 1610)
970he cautions the readers on this issue and _before_ describing his
971results/observations, Galileo instructs us on how to build a suitable
972instrument.  Without a detailed description of _how_ he made his tools
973and done his observations, no reasonable person would believe his
974results.  Before he actually saw the moons of Jupiter, the mountains on
975the Moon or the crescent of Venus, Galileo was “evasive”(4) to Kepler.
976Science is defined by its tools/methods, _not_ its raw results(5).
977
978   The same is true today: science cannot progress with a black box, or
979poorly released code.  The source code of a research is the new
980(abstractified) communication language in science, understandable by
981humans _and_ computers.  Source code (in any programming language) is a
982language/notation designed to express all the details that would be too
983tedious/long/frustrating to report in spoken languages like English,
984similar to mathematic notation.
985
986     An article about computational science [almost all sciences today]
987     ...  is not the scholarship itself, it is merely advertising of the
988     scholarship.  The Actual Scholarship is the complete software
989     development environment and the complete set of instructions which
990     generated the figures.
991   — _Buckheit & Donoho, Lecture Notes in Statistics, Vol 103, 1996_
992
993   Today, the quality of the source code that goes into a scientific
994result (and the distribution of that code) is as critical to scientific
995vitality and integrity, as the quality of its written language/English
996used in publishing/distributing its paper.  A scientific paper will not
997even be reviewed by any respectable journal if its written in a poor
998language/English.  A similar level of quality assessment is thus
999increasingly becoming necessary regarding the codes/methods used to
1000derive the results of a scientific paper.  For more on this, please see
1001Akhlaghi et al.  (2021) at arXiv:2006.03018
1002(https://arxiv.org/abs/2006.03018)).
1003
1004   Bjarne Stroustrup (creator of the C++ language) says: “_Without
1005understanding software, you are reduced to believing in magic_”.  Ken
1006Thomson (the designer or the Unix operating system) says “_I abhor a
1007system designed for the ‘user’ if that word is a coded pejorative
1008meaning ‘stupid and unsophisticated’_.” Certainly no scientist (user of
1009a scientific software) would want to be considered a believer in magic,
1010or stupid and unsophisticated.
1011
1012   This can happen when scientists get too distant from the raw data and
1013methods, and are mainly discussing results.  In other words, when they
1014feel they have tamed Nature into their own high-level (abstract) models
1015(creations), and are mainly concerned with scaling up, or
1016industrializing those results.  Roughly five years before special
1017relativity, and about two decades before quantum mechanics fundamentally
1018changed Physics, Lord Kelvin is quoted as saying:
1019
1020     There is nothing new to be discovered in physics now.  All that
1021     remains is more and more precise measurement.
1022                — _William Thomson (Lord Kelvin), 1900_
1023
1024A few years earlier Albert.  A. Michelson made the following statement:
1025
1026     The more important fundamental laws and facts of physical science
1027     have all been discovered, and these are now so firmly established
1028     that the possibility of their ever being supplanted in consequence
1029     of new discoveries is exceedingly remote....  Our future
1030     discoveries must be looked for in the sixth place of decimals.
1031— _Albert. A. Michelson, dedication of Ryerson Physics Lab, U. Chicago 1894_
1032
1033   If scientists are considered to be more than mere puzzle solvers(6)
1034(simply adding to the decimals of existing values or observing a feature
1035in 10, 100, or 100000 more galaxies or stars, as Kelvin and Michelson
1036clearly believed), they cannot just passively sit back and uncritically
1037repeat the previous (observational or theoretical) methods/tools on new
1038data.  Today there is a wealth of raw telescope images ready (mostly for
1039free) at the finger tips of anyone who is interested with a fast enough
1040internet connection to download them.  The only thing lacking is new
1041ways to analyze this data and dig out the treasure that is lying hidden
1042in them to existing methods and techniques.
1043
1044     New data that we insist on analyzing in terms of old ideas (that
1045     is, old models which are not questioned) cannot lead us out of the
1046     old ideas.  However many data we record and analyze, we may just
1047     keep repeating the same old errors, missing the same crucially
1048     important things that the experiment was competent to find.
1049— _Jaynes, Probability theory, the logic of science. Cambridge U. Press (2003)._
1050
1051   ---------- Footnotes ----------
1052
1053   (1) <https://www.gnu.org/philosophy/free-sw.html>
1054
1055   (2) Where the authors omit many of the analysis/processing “details”
1056from the paper by arguing that they would make the paper too
1057long/unreadable.  However, software engineers have been dealing with
1058such issues for a long time.  There are thus software management
1059solutions that allow us to supplement papers with all the details
1060necessary to exactly reproduce the result.  For example see Akhlaghi et
1061al.  (2021, arXiv:2006.03018 (https://arxiv.org/abs/2006.03018)).
1062
1063   (3) Karl Popper.  The logic of scientific discovery.  1959.  Larger
1064quote is given at the start of the PDF (for print) version of this book.
1065
1066   (4) Galileo G. (Translated by Maurice A. Finocchiaro).  _The
1067essential Galileo_.Hackett publishing company, first edition, 2008.
1068
1069   (5) For example, take the following two results on the age of the
1070universe: roughly 14 billion years (suggested by the current consensus
1071of the standard model of cosmology) and less than 10,000 years
1072(suggested from some interpretations of the Bible).  Both these numbers
1073are _results_.  What distinguishes these two results, is the
1074tools/methods that were used to derive them.  Therefore, as the term
1075“Scientific method” also signifies, a scientific statement it defined by
1076its _method_, not its result.
1077
1078   (6) Thomas S. Kuhn.  _The Structure of Scientific Revolutions_,
1079University of Chicago Press, 1962.
1080
1081
1082File: gnuastro.info,  Node: Your rights,  Next: Naming convention,  Prev: Science and its tools,  Up: Introduction
1083
10841.3 Your rights
1085===============
1086
1087The paragraphs below, in this section, belong to the GNU Texinfo(1)
1088manual and are not written by us!  The name “Texinfo” is just changed to
1089“GNU Astronomy Utilities” or “Gnuastro” because they are released under
1090the same licenses and it is beautifully written to inform you of your
1091rights.
1092
1093   GNU Astronomy Utilities is “free software”; this means that everyone
1094is free to use it and free to redistribute it on certain conditions.
1095Gnuastro is not in the public domain; it is copyrighted and there are
1096restrictions on its distribution, but these restrictions are designed to
1097permit everything that a good cooperating citizen would want to do.
1098What is not allowed is to try to prevent others from further sharing any
1099version of Gnuastro that they might get from you.
1100
1101   Specifically, we want to make sure that you have the right to give
1102away copies of the programs that relate to Gnuastro, that you receive
1103the source code or else can get it if you want it, that you can change
1104these programs or use pieces of them in new free programs, and that you
1105know you can do these things.
1106
1107   To make sure that everyone has such rights, we have to forbid you to
1108deprive anyone else of these rights.  For example, if you distribute
1109copies of the Gnuastro related programs, you must give the recipients
1110all the rights that you have.  You must make sure that they, too,
1111receive or can get the source code.  And you must tell them their
1112rights.
1113
1114   Also, for our own protection, we must make certain that everyone
1115finds out that there is no warranty for the programs that relate to
1116Gnuastro.  If these programs are modified by someone else and passed on,
1117we want their recipients to know that what they have is not what we
1118distributed, so that any problems introduced by others will not reflect
1119on our reputation.
1120
1121   The full text of the licenses for the Gnuastro book and software can
1122be respectively found in *note GNU General Public License::(2) and *note
1123GNU Free Doc. License::(3).
1124
1125   ---------- Footnotes ----------
1126
1127   (1) Texinfo is the GNU documentation system.  It is used to create
1128this book in all the various formats.
1129
1130   (2) Also available in <http://www.gnu.org/copyleft/gpl.html>
1131
1132   (3) Also available in <http://www.gnu.org/copyleft/fdl.html>
1133
1134
1135File: gnuastro.info,  Node: Naming convention,  Next: Version numbering,  Prev: Your rights,  Up: Introduction
1136
11371.4 Naming convention
1138=====================
1139
1140Gnuastro is a package of independent programs and a collection of
1141libraries, here we are mainly concerned with the programs.  Each program
1142has an official name which consists of one or two words, describing what
1143they do.  The latter are printed with no space, for example NoiseChisel
1144or Crop.  On the command-line, you can run them with their executable
1145names which start with an ‘ast’ and might be an abbreviation of the
1146official name, for example ‘astnoisechisel’ or ‘astcrop’, see *note
1147Executable names::.
1148
1149   We will use “ProgramName” for a generic official program name and
1150‘astprogname’ for a generic executable name.  In this book, the programs
1151are classified based on what they do and thoroughly explained.  An
1152alphabetical list of the programs that are installed on your system with
1153this installation are given in *note Gnuastro programs list::.  That
1154list also contains the executable names and version numbers along with a
1155one line description.
1156
1157
1158File: gnuastro.info,  Node: Version numbering,  Next: New to GNU/Linux?,  Prev: Naming convention,  Up: Introduction
1159
11601.5 Version numbering
1161=====================
1162
1163Gnuastro can have two formats of version numbers, for official and
1164unofficial releases.  Official Gnuastro releases are announced on the
1165‘info-gnuastro’ mailing list, they have a version control tag in
1166Gnuastro’s development history, and their version numbers are formatted
1167like “‘A.B’”.  ‘A’ is a major version number, marking a significant
1168planned achievement (for example see *note GNU Astronomy Utilities
11691.0::), while ‘B’ is a minor version number, see below for more on the
1170distinction.  Note that the numbers are not decimals, so version 2.34 is
1171much more recent than version 2.5, which is not equal to 2.50.
1172
1173   Gnuastro also allows a unique version number for unofficial releases.
1174Unofficial releases can mark any point in Gnuastro’s development
1175history.  This is done to allow astronomers to easily use any point in
1176the version controlled history for their data-analysis and research
1177publication.  See *note Version controlled source:: for a complete
1178introduction.  This section is not just for developers and is intended
1179to straightforward and easy to read, so please have a look if you are
1180interested in the cutting-edge.  This unofficial version number is a
1181meaningful and easy to read string of characters, unique to that
1182particular point of history.  With this feature, users can easily stay
1183up to date with the most recent bug fixes and additions that are
1184committed between official releases.
1185
1186   The unofficial version number is formatted like: ‘A.B.C-D’.  ‘A’ and
1187‘B’ are the most recent official version number.  ‘C’ is the number of
1188commits that have been made after version ‘A.B’.  ‘D’ is the first 4 or
11895 characters of the commit hash number(1).  Therefore, the unofficial
1190version number ‘‘3.92.8-29c8’’, corresponds to the 8th commit after the
1191official version ‘3.92’ and its commit hash begins with ‘29c8’.  The
1192unofficial version number is sort-able (unlike the raw hash) and as
1193shown above is descriptive of the state of the unofficial release.  Of
1194course an official release is preferred for publication (since its
1195tarballs are easily available and it has gone through more tests, making
1196it more stable), so if an official release is announced prior to your
1197publication’s final review, please consider updating to the official
1198release.
1199
1200   The major version number is set by a major goal which is defined by
1201the developers and user community before hand, for example see *note GNU
1202Astronomy Utilities 1.0::.  The incremental work done in minor releases
1203are commonly small steps in achieving the major goal.  Therefore, there
1204is no limit on the number of minor releases and the difference between
1205the (hypothetical) versions 2.927 and 3.0 can be a small (negligible to
1206the user) improvement that finalizes the defined goals.
1207
1208* Menu:
1209
1210* GNU Astronomy Utilities 1.0::  Plans for version 1.0 release
1211
1212   ---------- Footnotes ----------
1213
1214   (1) Each point in Gnuastro’s history is uniquely identified with a 40
1215character long hash which is created from its contents and previous
1216history for example: ‘5b17501d8f29ba3cd610673261e6e2229c846d35’.  So the
1217string ‘D’ in the version for this commit could be ‘5b17’, or ‘5b175’.
1218
1219
1220File: gnuastro.info,  Node: GNU Astronomy Utilities 1.0,  Prev: Version numbering,  Up: Version numbering
1221
12221.5.1 GNU Astronomy Utilities 1.0
1223---------------------------------
1224
1225Currently (prior to Gnuastro 1.0), the aim of Gnuastro is to have a
1226complete system for data manipulation and analysis at least similar to
1227IRAF(1). So an astronomer can take all the standard data analysis steps
1228(starting from raw data to the final reduced product and standard
1229post-reduction tools) with the various programs in Gnuastro.
1230
1231   The maintainers of each camera or detector on a telescope can provide
1232a completely transparent shell script or Makefile to the observer for
1233data analysis.  This script can set configuration files for all the
1234required programs to work with that particular camera.  The script can
1235then run the proper programs in the proper sequence.  The user/observer
1236can easily follow the standard shell script to understand (and modify)
1237each step and the parameters used easily.  Bash (or other modern
1238GNU/Linux shell scripts) is powerful and made for this gluing job.  This
1239will simultaneously improve performance and transparency.  Shell
1240scripting (or Makefiles) are also basic constructs that are easy to
1241learn and readily available as part of the Unix-like operating systems.
1242If there is no program to do a desired step, Gnuastro’s libraries can be
1243used to build specific programs.
1244
1245   The main factor is that all observatories or projects can freely
1246contribute to Gnuastro and all simultaneously benefit from it (since it
1247doesn’t belong to any particular one of them), much like how for-profit
1248organizations (for example RedHat, or Intel and many others) are major
1249contributors to free and open source software for their shared benefit.
1250Gnuastro’s copyright has been fully awarded to GNU, so it doesn’t belong
1251to any particular astronomer or astronomical facility or project.
1252
1253   ---------- Footnotes ----------
1254
1255   (1) <http://iraf.noao.edu/>
1256
1257
1258File: gnuastro.info,  Node: New to GNU/Linux?,  Next: Report a bug,  Prev: Version numbering,  Up: Introduction
1259
12601.6 New to GNU/Linux?
1261=====================
1262
1263Some astronomers initially install and use a GNU/Linux operating system
1264because their necessary tools can only be installed in this environment.
1265However, the transition is not necessarily easy.  To encourage you in
1266investing the patience and time to make this transition, and actually
1267enjoy it, we will first start with a basic introduction to GNU/Linux
1268operating systems.  Afterwards, in *note Command-line interface:: we’ll
1269discuss the wonderful benefits of the command-line interface, how it
1270beautifully complements the graphic user interface, and why it is worth
1271the (apparently steep) learning curve.  Finally a complete chapter
1272(*note Tutorials::) is devoted to real world scenarios of using Gnuastro
1273(on the command-line).  Therefore if you don’t yet feel comfortable with
1274the command-line we strongly recommend going through that chapter after
1275finishing this section.
1276
1277   You might have already noticed that we are not using the name
1278“Linux”, but “GNU/Linux”.  Please take the time to have a look at the
1279following essays and FAQs for a complete understanding of this very
1280important distinction.
1281
1282   • <https://www.gnu.org/gnu/gnu-users-never-heard-of-gnu.html>
1283
1284   • <https://www.gnu.org/gnu/linux-and-gnu.html>
1285
1286   • <https://www.gnu.org/gnu/why-gnu-linux.html>
1287
1288   • <https://www.gnu.org/gnu/gnu-linux-faq.html>
1289
1290   In short, the Linux kernel(1) is built using the GNU C library
1291(glibc) and GNU compiler collection (gcc).  The Linux kernel software
1292alone is just a means for other software to access the hardware
1293resources, it is useless alone: to say “running Linux”, is like saying
1294“driving your carburetor”.
1295
1296   To have an operating system, you need lower-level (to build the
1297kernel), and higher-level (to use it) software packages.  The majority
1298of such software in most Unix-like operating systems are GNU software:
1299“the whole system is basically GNU with Linux loaded”.  Therefore to
1300acknowledge GNU’s instrumental role in the creation and usage of the
1301Linux kernel and the operating systems that use it, we should call these
1302operating systems “GNU/Linux”.
1303
1304* Menu:
1305
1306* Command-line interface::      Introduction to the command-line
1307
1308   ---------- Footnotes ----------
1309
1310   (1) In Unix-like operating systems, the kernel connects software and
1311hardware worlds.
1312
1313
1314File: gnuastro.info,  Node: Command-line interface,  Prev: New to GNU/Linux?,  Up: New to GNU/Linux?
1315
13161.6.1 Command-line interface
1317----------------------------
1318
1319One aspect of Gnuastro that might be a little troubling to new GNU/Linux
1320users is that (at least for the time being) it only has a command-line
1321user interface (CLI). This might be contrary to the mostly graphical
1322user interface (GUI) experience with proprietary operating systems.
1323Since the various actions available aren’t always on the screen, the
1324command-line interface can be complicated, intimidating, and frustrating
1325for a first-time user.  This is understandable and also experienced by
1326anyone who started using the computer (from childhood) in a graphical
1327user interface (this includes most of Gnuastro’s authors).  Here we hope
1328to convince you of the unique benefits of this interface which can
1329greatly enhance your productivity while complementing your GUI
1330experience.
1331
1332   Through GNOME 3(1), most GNU/Linux based operating systems now have
1333an advanced and useful GUI. Since the GUI was created long after the
1334command-line, some wrongly consider the command line to be obsolete.
1335Both interfaces are useful for different tasks.  For example you can’t
1336view an image, video, pdf document or web page on the command-line.  On
1337the other hand you can’t reproduce your results easily in the GUI.
1338Therefore they should not be regarded as rivals but as complementary
1339user interfaces, here we will outline how the CLI can be useful in
1340scientific programs.
1341
1342   You can think of the GUI as a veneer over the CLI to facilitate a
1343small subset of all the possible CLI operations.  Each click you do on
1344the GUI, can be thought of as internally running a different CLI
1345command.  So asymptotically (if a good designer can design a GUI which
1346is able to show you all the possibilities to click on) the GUI is only
1347as powerful as the command-line.  In practice, such graphical designers
1348are very hard to find for every program, so the GUI operations are
1349always a subset of the internal CLI commands.  For programs that are
1350only made for the GUI, this results in not including lots of potentially
1351useful operations.  It also results in ‘interface design’ to be a
1352crucially important part of any GUI program.  Scientists don’t usually
1353have enough resources to hire a graphical designer, also the complexity
1354of the GUI code is far more than CLI code, which is harmful for a
1355scientific software, see *note Science and its tools::.
1356
1357   For programs that have a GUI, one action on the GUI (moving and
1358clicking a mouse, or tapping a touchscreen) might be more efficient and
1359easier than its CLI counterpart (typing the program name and your
1360desired configuration).  However, if you have to repeat that same action
1361more than once, the GUI will soon become frustrating and prone to
1362errors.  Unless the designers of a particular program decided to design
1363such a system for a particular GUI action, there is no general way to
1364run any possible series of actions automatically on the GUI.
1365
1366   On the command-line, you can run any series of of actions which can
1367come from various CLI capable programs you have decided your self in any
1368possible permutation with one command(2).  This allows for much more
1369creativity and exact reproducibility that is not possible to a GUI user.
1370For technical and scientific operations, where the same operation (using
1371various programs) has to be done on a large set of data files, this is
1372crucially important.  It also allows exact reproducibility which is a
1373foundation principle for scientific results.  The most common CLI (which
1374is also known as a shell) in GNU/Linux is GNU Bash, we strongly
1375encourage you to put aside several hours and go through this beautifully
1376explained web page: <https://flossmanuals.net/command-line/>.  You don’t
1377need to read or even fully understand the whole thing, only a general
1378knowledge of the first few chapters are enough to get you going.
1379
1380   Since the operations in the GUI are limited and they are visible,
1381reading a manual is not that important in the GUI (most programs don’t
1382even have any!).  However, to give you the creative power explained
1383above, with a CLI program, it is best if you first read the manual of
1384any program you are using.  You don’t need to memorize any details, only
1385an understanding of the generalities is needed.  Once you start working,
1386there are more easier ways to remember a particular option or operation
1387detail, see *note Getting help::.
1388
1389   To experience the command-line in its full glory and not in the GUI
1390terminal emulator, press the following keys together: <CTRL+ALT+F4>(3)
1391to access the virtual console.  To return back to your GUI, press the
1392same keys above replacing <F4> with <F7> (or <F1>, or <F2>, depending on
1393your GNU/Linux distribution).  In the virtual console, the GUI, with all
1394its distracting colors and information, is gone.  Enabling you to focus
1395entirely on your actual work.
1396
1397   For operations that use a lot of your system’s resources (processing
1398a large number of large astronomical images for example), the virtual
1399console is the place to run them.  This is because the GUI is not
1400competing with your research work for your system’s RAM and CPU. Since
1401the virtual consoles are completely independent, you can even log out of
1402your GUI environment to give even more of your hardware resources to the
1403programs you are running and thus reduce the operating time.
1404
1405   Since it uses far less system resources, the CLI is also convenient
1406for remote access to your computer.  Using secure shell (SSH) you can
1407log in securely to your system (similar to the virtual console) from
1408anywhere even if the connection speeds are low.  There are apps for
1409smart phones and tablets which allow you to do this.
1410
1411   ---------- Footnotes ----------
1412
1413   (1) <http://www.gnome.org/>
1414
1415   (2) By writing a shell script and running it, for example see the
1416tutorials in *note Tutorials::.
1417
1418   (3) Instead of <F4>, you can use any of the keys from <F1> to <F6>
1419for different virtual consoles depending on your GNU/Linux distribution,
1420try them all out.  You can also run a separate GUI from within this
1421console if you want to.
1422
1423
1424File: gnuastro.info,  Node: Report a bug,  Next: Suggest new feature,  Prev: New to GNU/Linux?,  Up: Introduction
1425
14261.7 Report a bug
1427================
1428
1429According to Wikipedia “a software bug is an error, flaw, failure, or
1430fault in a computer program or system that causes it to produce an
1431incorrect or unexpected result, or to behave in unintended ways”.  So
1432when you see that a program is crashing, not reading your input
1433correctly, giving the wrong results, or not writing your output
1434correctly, you have found a bug.  In such cases, it is best if you
1435report the bug to the developers.  The programs will also inform you if
1436known impossible situations occur (which are caused by something
1437unexpected) and will ask the users to report the bug issue.
1438
1439   Prior to actually filing a bug report, it is best to search previous
1440reports.  The issue might have already been found and even solved.  The
1441best place to check if your bug has already been discussed is the bugs
1442tracker on *note Gnuastro project webpage:: at
1443<https://savannah.gnu.org/bugs/?group=gnuastro>.  In the top search
1444fields (under “Display Criteria”) set the “Open/Closed” drop-down menu
1445to “Any” and choose the respective program or general category of the
1446bug in “Category” and click the “Apply” button.  The results colored
1447green have already been solved and the status of those colored in red is
1448shown in the table.
1449
1450   Recently corrected bugs are probably not yet publicly released
1451because they are scheduled for the next Gnuastro stable release.  If the
1452bug is solved but not yet released and it is an urgent issue for you,
1453you can get the version controlled source and compile that, see *note
1454Version controlled source::.
1455
1456   To solve the issue as readily as possible, please follow the
1457following to guidelines in your bug report.  The How to Report Bugs
1458Effectively (http://www.chiark.greenend.org.uk/~sgtatham/bugs.html) and
1459How To Ask Questions The Smart Way
1460(http://catb.org/~esr/faqs/smart-questions.html) essays also provide
1461some good generic advice for all software (don’t contact their authors
1462for Gnuastro’s problems).  Mastering the art of giving good bug reports
1463(like asking good questions) can greatly enhance your experience with
1464any free and open source software.  So investing the time to read
1465through these essays will greatly reduce your frustration after you see
1466something doesn’t work the way you feel it is supposed to for a large
1467range of software, not just Gnuastro.
1468
1469*Be descriptive*
1470     Please provide as many details as possible and be very descriptive.
1471     Explain what you expected and what the output was: it might be that
1472     your expectation was wrong.  Also please clearly state which
1473     sections of the Gnuastro book (this book), or other references you
1474     have studied to understand the problem.  This can be useful in
1475     correcting the book (adding links to likely places where users will
1476     check).  But more importantly, it will be encouraging for the
1477     developers, since you are showing how serious you are about the
1478     problem and that you have actually put some thought into it.  “To
1479     be able to ask a question clearly is two-thirds of the way to
1480     getting it answered.” – John Ruskin (1819-1900).
1481
1482*Individual and independent bug reports*
1483     If you have found multiple bugs, please send them as separate (and
1484     independent) bugs (as much as possible).  This will significantly
1485     help us in managing and resolving them sooner.
1486
1487*Reproducible bug reports*
1488     If we cannot exactly reproduce your bug, then it is very hard to
1489     resolve it.  So please send us a Minimal working example(1) along
1490     with the description.  For example in running a program, please
1491     send us the full command-line text and the output with the ‘-P’
1492     option, see *note Operating mode options::.  If it is caused only
1493     for a certain input, also send us that input file.  In case the
1494     input FITS is large, please use Crop to only crop the problematic
1495     section and make it as small as possible so it can easily be
1496     uploaded and downloaded and not waste the archive’s storage, see
1497     *note Crop::.
1498
1499There are generally two ways to inform us of bugs:
1500
1501   • Send a mail to ‘bug-gnuastro@gnu.org’.  Any mail you send to this
1502     address will be distributed through the bug-gnuastro mailing
1503     list(2).  This is the simplest way to send us bug reports.  The
1504     developers will then register the bug into the project web page
1505     (next choice) for you.
1506
1507   • Use the Gnuastro project web page at
1508     <https://savannah.gnu.org/projects/gnuastro/>: There are two ways
1509     to get to the submission page as listed below.  Fill in the form as
1510     described below and submit it (see *note Gnuastro project webpage::
1511     for more on the project web page).
1512
1513        • Using the top horizontal menu items, immediately under the top
1514          page title.  Hovering your mouse on “Support” will open a
1515          drop-down list.  Select “Submit new”.
1516
1517        • In the main body of the page, under the “Communication tools”
1518          section, click on “Submit new item”.
1519
1520   Once the items have been registered in the mailing list or web page,
1521the developers will add it to either the “Bug Tracker” or “Task Manager”
1522trackers of the Gnuastro project web page.  These two trackers can only
1523be edited by the Gnuastro project developers, but they can be browsed by
1524anyone, so you can follow the progress on your bug.  You are most
1525welcome to join us in developing Gnuastro and fixing the bug you have
1526found maybe a good starting point.  Gnuastro is designed to be easy for
1527anyone to develop (see *note Science and its tools::) and there is a
1528full chapter devoted to developing it: *note Developing::.
1529
1530   ---------- Footnotes ----------
1531
1532   (1) <http://en.wikipedia.org/wiki/Minimal_Working_Example>
1533
1534   (2) <https://lists.gnu.org/mailman/listinfo/bug-gnuastro>
1535
1536
1537File: gnuastro.info,  Node: Suggest new feature,  Next: Announcements,  Prev: Report a bug,  Up: Introduction
1538
15391.8 Suggest new feature
1540=======================
1541
1542We would always be happy to hear of suggested new features.  For every
1543program there are already lists of features that we are planning to add.
1544You can see the current list of plans from the Gnuastro project web page
1545at <https://savannah.gnu.org/projects/gnuastro/> and following
1546“Tasks”→“Browse” on the horizontal menu at the top of the page
1547immediately under the title, see *note Gnuastro project webpage::.  If
1548you want to request a feature to an existing program, click on the
1549“Display Criteria” above the list and under “Category”, choose that
1550particular program.  Under “Category” you can also see the existing
1551suggestions for new programs or other cases like installation,
1552documentation or libraries.  Also be sure to set the “Open/Closed” value
1553to “Any”.
1554
1555   If the feature you want to suggest is not already listed in the task
1556manager, then follow the steps that are fully described in *note Report
1557a bug::.  Please have in mind that the developers are all busy with
1558their own astronomical research, and implementing existing “task”s to
1559add or resolving bugs.  Gnuastro is a volunteer effort and none of the
1560developers are paid for their hard work.  So, although we will try our
1561best, please don’t not expect that your suggested feature be immediately
1562included (with the next release of Gnuastro).
1563
1564   The best person to apply the exciting new feature you have in mind is
1565you, since you have the motivation and need.  In fact Gnuastro is
1566designed for making it as easy as possible for you to hack into it (add
1567new features, change existing ones and so on), see *note Science and its
1568tools::.  Please have a look at the chapter devoted to developing (*note
1569Developing::) and start applying your desired feature.  Once you have
1570added it, you can use it for your own work and if you feel you want
1571others to benefit from your work, you can request for it to become part
1572of Gnuastro.  You can then join the developers and start maintaining
1573your own part of Gnuastro.  If you choose to take this path of action
1574please contact us before hand (*note Report a bug::) so we can avoid
1575possible duplicate activities and get interested people in contact.
1576
1577*Gnuastro is a collection of low level programs:* As described in *note
1578Program design philosophy::, a founding principle of Gnuastro is that
1579each library or program should be basic and low-level.  High level jobs
1580should be done by running the separate programs or using separate
1581functions in succession through a shell script or calling the libraries
1582by higher level functions, see the examples in *note Tutorials::.  So
1583when making the suggestions please consider how your desired job can
1584best be broken into separate steps and modularized.
1585
1586
1587File: gnuastro.info,  Node: Announcements,  Next: Conventions,  Prev: Suggest new feature,  Up: Introduction
1588
15891.9 Announcements
1590=================
1591
1592Gnuastro has a dedicated mailing list for making announcements
1593(‘info-gnuastro’).  Anyone can subscribe to this mailing list.  Anytime
1594there is a new stable or test release, an email will be circulated
1595there.  The email contains a summary of the overall changes along with a
1596detailed list (from the ‘NEWS’ file).  This mailing list is thus the
1597best way to stay up to date with new releases, easily learn about the
1598updated/new features, or dependencies (see *note Dependencies::).
1599
1600   To subscribe to this list, please visit
1601<https://lists.gnu.org/mailman/listinfo/info-gnuastro>.  Traffic (number
1602of mails per unit time) in this list is designed to be low: only a
1603handful of mails per year.  Previous announcements are available on its
1604archive (http://lists.gnu.org/archive/html/info-gnuastro/).
1605
1606
1607File: gnuastro.info,  Node: Conventions,  Next: Acknowledgments,  Prev: Announcements,  Up: Introduction
1608
16091.10 Conventions
1610================
1611
1612In this book we have the following conventions:
1613
1614   • All commands that are to be run on the shell (command-line) prompt
1615     as the user start with a ‘$’.  In case they must be run as a
1616     super-user or system administrator, they will start with a single
1617     ‘#’.  If the command is in a separate line and next line ‘is also
1618     in the code type face’, but doesn’t have any of the ‘$’ or ‘#’
1619     signs, then it is the output of the command after it is run.  As a
1620     user, you don’t need to type those lines.  A line that starts with
1621     ‘##’ is just a comment for explaining the command to a human reader
1622     and must not be typed.
1623
1624   • If the command becomes larger than the page width a <\> is inserted
1625     in the code.  If you are typing the code by hand on the
1626     command-line, you don’t need to use multiple lines or add the extra
1627     space characters, so you can omit them.  If you want to copy and
1628     paste these examples (highly discouraged!)  then the <\> should
1629     stay.
1630
1631     The <\> character is a shell escape character which is used
1632     commonly to make characters which have special meaning for the
1633     shell loose that special place (the shell will not treat them
1634     specially if there is a <\> behind them).  When it is a last
1635     character in a line (the next character is a new-line character)
1636     the new-line character looses its meaning an the shell sees it as a
1637     simple white-space character, enabling you to use multiple lines to
1638     write your commands.
1639
1640   This is not a convention, but a bi-product of the PDF building
1641process of the manual: In the PDF version of this manual, a single quote
1642(or apostrophe) character in the commands or codes is shown like this:
1643‘'’.  Single quotes are sometimes necessary in combination with commands
1644like ‘awk’ or ‘sed’, or when using Column arithmetic in Gnuastro’s own
1645Table (see *note Column arithmetic::).  Therefore when typing
1646(recommended) or copy-pasting (not recommended) the commands that have a
1647‘'’, please correct it to the single-quote (or apostrophe) character,
1648otherwise the command will fail.
1649
1650
1651File: gnuastro.info,  Node: Acknowledgments,  Prev: Conventions,  Up: Introduction
1652
16531.11 Acknowledgments
1654====================
1655
1656Gnuastro would not have been possible without scholarships and grants
1657from several funding institutions.  We thus ask that if you used
1658Gnuastro in any of your papers/reports, please add the proper citation
1659and acknowledge the funding agencies/projects.  For details of which
1660papers to cite (may be different for different programs) and get the
1661acknowledgment statement to include in your paper, please run the
1662relevant programs with the common ‘--cite’ option like the example
1663commands below (for more on ‘--cite’, please see *note Operating mode
1664options::).
1665
1666     $ astnoisechisel --cite
1667     $ astmkcatalog --cite
1668
1669   Here, we’ll acknowledge all the institutions (and their grants) along
1670with the people who helped make Gnuastro possible.  The full list of
1671Gnuastro authors is available at the start of this book and the
1672‘AUTHORS’ file in the source code (both are generated automatically from
1673the version controlled history).  The plain text file ‘THANKS’, which is
1674also distributed along with the source code, contains the list of people
1675and institutions who played an indirect role in Gnuastro (not committed
1676any code in the Gnuastro version controlled history).
1677
1678   The Japanese Ministry of Education, Culture, Sports, Science, and
1679Technology (MEXT) scholarship for Mohammad Akhlaghi’s Masters and PhD
1680degree in Tohoku University Astronomical Institute had an instrumental
1681role in the long term learning and planning that made the idea of
1682Gnuastro possible.  The very critical view points of Professor Takashi
1683Ichikawa (Mohammad’s adviser) were also instrumental in the initial
1684ideas and creation of Gnuastro.  Afterwards, the European Research
1685Council (ERC) advanced grant 339659-MUSICOS (Principal investigator:
1686Roland Bacon) was vital in the growth and expansion of Gnuastro.
1687Working with Roland at the Centre de Recherche Astrophysique de Lyon
1688(CRAL), enabled a thorough re-write of the core functionality of all
1689libraries and programs, turning Gnuastro into the large collection of
1690generic programs and libraries it is today.  Work on improving Gnuastro
1691and making it mature is now continuing primarily in the Instituto de
1692Astrofisica de Canarias (IAC) and in particular in collaboration with
1693Johan Knapen and Ignacio Trujillo.
1694
1695   In general, we would like to gratefully thank the following people
1696for their useful and constructive comments and suggestions (in
1697alphabetical order by family name): Valentina Abril-melgarejo, Marjan
1698Akbari, Carlos Allende Prieto, Hamed Altafi, Roland Bacon, Roberto Baena
1699Gallé, Zahra Bagheri, Karl Berry, Leindert Boogaard, Nicolas Bouché,
1700Stefan Brüns, Fernando Buitrago, Adrian Bunk, Rosa Calvi, Mark
1701Calabretta Nushkia Chamba, Benjamin Clement, Nima Dehdilani, Antonio
1702Diaz Diaz, Alexey Dokuchaev, Pierre-Alain Duc, Elham Eftekhari, Paul
1703Eggert, Sepideh Eskandarlou, Gaspar Galaz, Andrés García-Serra Romero,
1704Zohre Ghaffari, Thérèse Godefroy, Giulia Golini, Madusha Gunawardhana,
1705Bruno Haible, Stephen Hamer, Leslie Hunt, Takashi Ichikawa, Raúl Infante
1706Sainz, Brandon Invergo, Oryna Ivashtenko, Aurélien Jarno, Lee Kelvin,
1707Brandon Kelly, Mohammad-Reza Khellat, Johan Knapen, Geoffry Krouchi,
1708Martin Kuemmel, Clotilde Laigle, Floriane Leclercq, Alan Lefor, Javier
1709Licandro, Sebastián Luna Valero, Alberto Madrigal, Guillaume Mahler,
1710Juan Miro, Alireza Molaeinezhad, Javier Moldon, Juan Molina Tobar,
1711Francesco Montanari, Raphael Morales, Carlos Morales Socorro, Sylvain
1712Mottet, Dmitrii Oparin, François Ochsenbein, Bertrand Pain, William
1713Pence, Mamta Pommier, Marcel Popescu, Bob Proulx, Joseph Putko, Samane
1714Raji, Teymoor Saifollahi, Joanna Sakowska, Elham Saremi, Markus Schaney,
1715Yahya Sefidbakht, Alejandro Serrano Borlaff, Zahra Sharbaf, David Shupe,
1716Leigh Smith, Jenny Sorce, Lee Spitler, Richard Stallman, Michael Stein,
1717Ole Streicher, Alfred M. Szmidt, Michel Tallon, Juan C. Tello, Vincenzo
1718Testa, Éric Thiébaut, Ignacio Trujillo, David Valls-Gabaud, Aaron
1719Watkins, Richard Wilbur, Michael H.F. Wilkinson, Christopher Willmer,
1720Xiuqin Wu, Sara Yousefi Taemeh, Johannes Zabl.  The GNU French
1721Translation Team is also managing the French version of the top Gnuastro
1722web page which we highly appreciate.  Finally we should thank all the
1723(sometimes anonymous) people in various online forums which patiently
1724answered all our small (but imporant) technical questions.
1725
1726   All work on Gnuastro has been voluntary, but the authors are most
1727grateful to the following institutions (in chronological order) for
1728hosting/supporting us in our research.  Where necessary, these
1729institutions have disclaimed any ownership of the parts of Gnuastro that
1730were developed there, thus insuring the freedom of Gnuastro for the
1731future (see *note Copyright assignment::).  We highly appreciate their
1732support for free software, and thus free science, and therefore a free
1733society.
1734
1735     Tohoku University Astronomical Institute, Sendai, Japan.
1736     University of Salento, Lecce, Italy.
1737     Centre de Recherche Astrophysique de Lyon (CRAL), Lyon, France.
1738     Instituto de Astrofisica de Canarias (IAC), Tenerife, Spain.
1739     Google Summer of Code 2020
1740
1741
1742File: gnuastro.info,  Node: Tutorials,  Next: Installation,  Prev: Introduction,  Up: Top
1743
17442 Tutorials
1745***********
1746
1747To help new users have a smooth and easy start with Gnuastro, in this
1748chapter several thoroughly elaborated tutorials, or cookbooks, are
1749provided.  These tutorials demonstrate the capabilities of different
1750Gnuastro programs and libraries, along with tips and guidelines for the
1751best practices of using them in various realistic situations.
1752
1753   We strongly recommend going through these tutorials to get a good
1754feeling of how the programs are related (built in a modular design to be
1755used together in a pipeline), very similar to the core Unix-based
1756programs that they were modeled on.  Therefore these tutorials will
1757greatly help in optimally using Gnuastro’s programs (and generally, the
1758Unix-like command-line environment) effectively for your research.
1759
1760   In *note Sufi simulates a detection::, we’ll start with a
1761fictional(1) tutorial explaining how Abd al-rahman Sufi (903 – 986 A.D.,
1762the first recorded description of “nebulous” objects in the heavens is
1763attributed to him) could have used some of Gnuastro’s programs for a
1764realistic simulation of his observations and see if his detection of
1765nebulous objects was trust-able.  Because all conditions are under
1766control in a simulated/mock environment/dataset, mock datasets can be a
1767valuable tool to inspect the limitations of your data analysis and
1768processing.  But they need to be as realistic as possible, so the first
1769tutorial is dedicated to this important step of an analysis.
1770
1771   The next two tutorials (*note General program usage tutorial:: and
1772*note Detecting large extended targets::) use real input datasets from
1773some of the deep Hubble Space Telescope (HST) images and the Sloan
1774Digital Sky Survey (SDSS) respectively.  Their aim is to demonstrate
1775some real-world problems that many astronomers often face and how they
1776can be be solved with Gnuastro’s programs.
1777
1778   The ultimate aim of *note General program usage tutorial:: is to
1779detect galaxies in a deep HST image, measure their positions and
1780brightness and select those with the strongest colors.  In the process,
1781it takes many detours to introduce you to the useful capabilities of
1782many of the programs.  So please be patient in reading it.  If you don’t
1783have much time and can only try one of the tutorials, we recommend this
1784one.
1785
1786   *note Detecting large extended targets:: deals with a major problem
1787in astronomy: effectively detecting the faint outer wings of bright (and
1788large) nearby galaxies to extremely low surface brightness levels
1789(roughly one quarter of the local noise level in the example discussed).
1790Besides the interesting scientific questions in these low-surface
1791brightness features, failure to properly detect them will bias the
1792measurements of the background objects and the survey’s noise estimates.
1793This is an important issue, especially in wide surveys.  Because
1794bright/large galaxies and stars(2), cover a significant fraction of the
1795survey area.
1796
1797   In these tutorials, we have intentionally avoided too many cross
1798references to make it more easy to read.  For more information about a
1799particular program, you can visit the section with the same name as the
1800program in this book.  Each program section in the subsequent chapters
1801starts by explaining the general concepts behind what it does, for
1802example see *note Convolve::.  If you only want practical information on
1803running a program, for example its options/configuration, input(s) and
1804output(s), please consult the subsection titled “Invoking ProgramName”,
1805for example see *note Invoking astnoisechisel::.  For an explanation of
1806the conventions we use in the example codes through the book, please see
1807*note Conventions::.
1808
1809* Menu:
1810
1811* Sufi simulates a detection::  Simulating a detection.
1812* General program usage tutorial::  Tutorial on many programs in generic scenario.
1813* Detecting large extended targets::  NoiseChisel for huge extended targets.
1814
1815   ---------- Footnotes ----------
1816
1817   (1) The two historically motivated tutorials (*note Sufi simulates a
1818detection:: is not intended to be a historical reference (the historical
1819facts of this fictional tutorial used Wikipedia as a reference).  This
1820form of presenting a tutorial was influenced by the PGF/TikZ and Beamer
1821manuals.  They are both packages in in TeX and LaTeX, the first is a
1822high-level vector graphic programming environment, while with the second
1823you can make presentation slides.  On a similar topic, there are also
1824some nice words of wisdom for Unix-like systems called Rootless Root
1825(http://catb.org/esr/writings/unix-koans).  These also have a similar
1826style but they use a mythical figure named Master Foo.  If you already
1827have some experience in Unix-like systems, you will definitely find
1828these Unix Koans entertaining/educative.
1829
1830   (2) Stars also have similarly large and extended wings due to the
1831point spread function, see *note PSF::.
1832
1833
1834File: gnuastro.info,  Node: Sufi simulates a detection,  Next: General program usage tutorial,  Prev: Tutorials,  Up: Tutorials
1835
18362.1 Sufi simulates a detection
1837==============================
1838
1839It is the year 953 A.D. and Abd al-rahman Sufi (903 – 986 A.D.)(1) is in
1840Shiraz as a guest astronomer.  He had come there to use the advanced 123
1841centimeter astrolabe for his studies on the Ecliptic.  However,
1842something was bothering him for a long time.  While mapping the
1843constellations, there were several non-stellar objects that he had
1844detected in the sky, one of them was in the Andromeda constellation.
1845During a trip he had to Yemen, Sufi had seen another such object in the
1846southern skies looking over the Indian ocean.  He wasn’t sure if such
1847cloud-like non-stellar objects (which he was the first to call ‘Sahābi’
1848in Arabic or ‘nebulous’) were real astronomical objects or if they were
1849only the result of some bias in his observations.  Could such diffuse
1850objects actually be detected at all with his detection technique?
1851
1852   He still had a few hours left until nightfall (when he would continue
1853his studies on the ecliptic) so he decided to find an answer to this
1854question.  He had thoroughly studied Claudius Ptolemy’s (90 – 168 A.D)
1855Almagest and had made lots of corrections to it, in particular in
1856measuring the brightness.  Using his same experience, he was able to
1857measure a magnitude for the objects and wanted to simulate his
1858observation to see if a simulated object with the same brightness and
1859size could be detected in a simulated noise with the same detection
1860technique.  The general outline of the steps he wants to take are:
1861
1862  1. Make some mock profiles in an over-sampled image.  The initial mock
1863     image has to be over-sampled prior to convolution or other forms of
1864     transformation in the image.  Through his experiences, Sufi knew
1865     that this is because the image of heavenly bodies is actually
1866     transformed by the atmosphere or other sources outside the
1867     atmosphere (for example gravitational lenses) prior to being
1868     sampled on an image.  Since that transformation occurs on a
1869     continuous grid, to best approximate it, he should do all the work
1870     on a finer pixel grid.  In the end he can re-sample the result to
1871     the initially desired grid size.
1872
1873  2. Convolve the image with a point spread function (PSF, see *note
1874     PSF::) that is over-sampled to the same resolution as the mock
1875     image.  Since he wants to finish in a reasonable time and the PSF
1876     kernel will be very large due to oversampling, he has to use
1877     frequency domain convolution which has the side effect of dimming
1878     the edges of the image.  So in the first step above he also has to
1879     build the image to be larger by at least half the width of the PSF
1880     convolution kernel on each edge.
1881
1882  3. With all the transformations complete, the image should be
1883     re-sampled to the same size of the pixels in his detector.
1884
1885  4. He should remove those extra pixels on all edges to remove
1886     frequency domain convolution artifacts in the final product.
1887
1888  5. He should add noise to the (until now, noise-less) mock image.
1889     After all, all observations have noise associated with them.
1890
1891   Fortunately Sufi had heard of GNU Astronomy Utilities from a
1892colleague in Isfahan (where he worked) and had installed it on his
1893computer a year before.  It had tools to do all the steps above.  He had
1894used MakeProfiles before, but wasn’t sure which columns he had chosen in
1895his user or system wide configuration files for which parameters, see
1896*note Configuration files::.  So to start his simulation, Sufi runs
1897MakeProfiles with the ‘-P’ option to make sure what columns in a catalog
1898MakeProfiles currently recognizes and the output image parameters.  In
1899particular, Sufi is interested in the recognized columns (shown below).
1900
1901     $ astmkprof -P
1902
1903     [[[ ... Truncated lines ... ]]]
1904
1905     # Output:
1906      type         float32     # Type of output: e.g., int16, float32, etc...
1907      mergedsize   1000,1000   # Number of pixels along first FITS axis.
1908      oversample   5           # Scale of oversampling (>0 and odd).
1909
1910     [[[ ... Truncated lines ... ]]]
1911
1912     # Columns, by info (see `--searchin'), or number (starting from 1):
1913      ccol         2           # Coord. columns (one call for each dim.).
1914      ccol         3           # Coord. columns (one call for each dim.).
1915      fcol         4           # sersic (1), moffat (2), gaussian (3),
1916                               # point (4), flat (5), circumference (6),
1917                               # distance (7), radial-table (8).
1918      rcol         5           # Effective radius or FWHM in pixels.
1919      ncol         6           # Sersic index or Moffat beta.
1920      pcol         7           # Position angle.
1921      qcol         8           # Axis ratio.
1922      mcol         9           # Magnitude.
1923      tcol         10          # Truncation in units of radius or pixels.
1924
1925     [[[ ... Truncated lines ... ]]]
1926
1927
1928In Gnuastro, column counting starts from 1, so the columns are ordered
1929such that the first column (number 1) can be an ID he specifies for each
1930object (and MakeProfiles ignores), each subsequent column is used for
1931another property of the profile.  It is also possible to use column
1932names for the values of these options and change these defaults, but
1933Sufi preferred to stick to the defaults.  Fortunately MakeProfiles has
1934the capability to also make the PSF which is to be used on the mock
1935image and using the ‘--prepforconv’ option, he can also make the mock
1936image to be larger by the correct amount and all the sources to be
1937shifted by the correct amount.
1938
1939   For his initial check he decides to simulate the nebula in the
1940Andromeda constellation.  The night he was observing, the PSF had
1941roughly a FWHM of about 5 pixels, so as the first row (profile), he
1942defines the PSF parameters and sets the radius column (‘rcol’ above,
1943fifth column) to ‘5.000’, he also chooses a Moffat function for its
1944functional form.  Remembering how diffuse the nebula in the Andromeda
1945constellation was, he decides to simulate it with a mock Sérsic index
19461.0 profile.  He wants the output to be 499 pixels by 499 pixels, so he
1947can put the center of the mock profile in the central pixel of the image
1948(note that an even number doesn’t have a central element).
1949
1950   Looking at his drawings of it, he decides a reasonable effective
1951radius for it would be 40 pixels on this image pixel scale, he sets the
1952axis ratio and position angle to approximately correct values too and
1953finally he sets the total magnitude of the profile to 3.44 which he had
1954accurately measured.  Sufi also decides to truncate both the mock
1955profile and PSF at 5 times the respective radius parameters.  In the end
1956he decides to put four stars on the four corners of the image at very
1957low magnitudes as a visual scale.  While he was preparing the catalog,
1958one of his students approached him and was also following the steps.
1959
1960   Using all the information above, he creates the catalog of mock
1961profiles he wants in a file named ‘cat.txt’ (short for catalog) using
1962his favorite text editor and stores it in a directory named
1963‘simulationtest’ in his home directory.  [The ‘cat’ command prints the
1964contents of a file, short for “concatenation”.  So please copy-paste the
1965lines after “‘cat cat.txt’” into ‘cat.txt’ when the editor opens in the
1966steps above it, note that there are 7 lines, first one starting with
1967<#>.  Also be careful when copying from the PDF format, the Info, web,
1968or text formats shouldn’t have any problem]:
1969
1970     $ mkdir ~/simulationtest
1971     $ cd ~/simulationtest
1972     $ pwd
1973     /home/rahman/simulationtest
1974     $ emacs cat.txt
1975     $ ls
1976     cat.txt
1977     $ cat cat.txt
1978     # Column 4: PROFILE_NAME [,str6] Radial profile's functional name
1979      1  0.0000   0.0000  moffat  5.000  4.765  0.0000  1.000  30.000  5.000
1980      2  250.00   250.00  sersic  40.00  1.000  -25.00  0.400  3.4400  5.000
1981      3  50.000   50.000  point   0.000  0.000  0.0000  0.000  6.0000  0.000
1982      4  450.00   50.000  point   0.000  0.000  0.0000  0.000  6.5000  0.000
1983      5  50.000   450.00  point   0.000  0.000  0.0000  0.000  7.0000  0.000
1984      6  450.00   450.00  point   0.000  0.000  0.0000  0.000  7.5000  0.000
1985
1986The zero point magnitude for his observation was 18.  Now he has all the
1987necessary parameters and runs MakeProfiles with the following command:
1988
1989
1990     $ astmkprof --prepforconv --mergedsize=499,499 --zeropoint=18.0 cat.txt
1991     MakeProfiles started on Sat Oct  6 16:26:56 953
1992       - 6 profiles read from cat.txt
1993       - Random number generator (RNG) type: mt19937
1994       - Using 8 threads.
1995       ---- row 2 complete, 5 left to go
1996       ---- row 3 complete, 4 left to go
1997       ---- row 4 complete, 3 left to go
1998       ---- row 5 complete, 2 left to go
1999       ---- ./0_cat.fits created.
2000       ---- row 0 complete, 1 left to go
2001       ---- row 1 complete, 0 left to go
2002       - ./cat_profiles.fits created.                       0.041651 seconds
2003     MakeProfiles finished in 0.267234 seconds
2004
2005     $ls
2006     0_cat.fits  cat_profiles.fits  cat.txt
2007
2008The file ‘0_cat.fits’ is the PSF Sufi had asked for, and
2009cat_profiles.fits’ is the image containing the main objects in the
2010catalog.  The size of ‘cat_profiles.fits’ was surprising for the
2011student, instead of 499 by 499 (as we had requested), it was 2615 by
20122615 pixels (from the command below):
2013
2014     $ astfits cat_profiles.fits -h1 | grep NAXIS
2015
2016So Sufi explained why oversampling is important in modeling, especially
2017for parts of the image where the flux change is significant over a
2018pixel.  Recall that when you oversample the model (for example by 5
2019times), for every desired pixel, you get 25 pixels ($5\times5$).  Sufi
2020then explained that after convolving (next step below) we will
2021down-sample the image to get our originally desired size/resolution.
2022
2023   Sufi then opened ‘cat_profiles.fits’ [you can use any FITS viewer,
2024for example, ‘ds9’].  After seeing the image, the student complained
2025that only the large elliptical model for the Andromeda nebula can be
2026seen in the center.  He couldn’t see the four stars that we had also
2027requested in the catalog.  So Sufi had to explain that the stars are
2028there in the image, but the reason that they aren’t visible when looking
2029at the whole image at once, is that they only cover a single pixel!  To
2030prove it, he centered the image around the coordinates 2308 and 2308,
2031where one of the stars is located in the over-sampled image [you can do
2032this in ‘ds9’ by selecting “Pan” in the “Edit” menu, then clicking
2033around that position].  Sufi then zoomed in to that region and soon, the
2034star’s non-zero pixel could be clearly seen.
2035
2036   Sufi explained that the stars will take the shape of the PSF (cover
2037an area of more than one pixel) after convolution.  If we didn’t have an
2038atmosphere and we didn’t need an aperture, then stars would only cover a
2039single pixel with normal CCD resolutions.  So Sufi convolved the image
2040with this command:
2041
2042     $ astconvolve --kernel=0_cat.fits cat_profiles.fits \
2043                   --output=cat_convolved.fits
2044     Convolve started on Sat Oct  6 16:35:32 953
2045       - Using 8 CPU threads.
2046       - Input: cat_profiles.fits (hdu: 1)
2047       - Kernel: 0_cat.fits (hdu: 1)
2048       - Input and Kernel images padded.                    0.075541 seconds
2049       - Images converted to frequency domain.              6.728407 seconds
2050       - Multiplied in the frequency domain.                0.040659 seconds
2051       - Converted back to the spatial domain.              3.465344 seconds
2052       - Padded parts removed.                              0.016767 seconds
2053       - Output: cat_convolved.fits
2054     Convolve finished in:  10.422161 seconds
2055
2056     $ls
2057     0_cat.fits  cat_convolved.fits  cat_profiles.fits  cat.txt
2058
2059When convolution finished, Sufi opened ‘cat_convolved.fits’ and the four
2060stars could be easily seen now.  It was interesting for the student that
2061all the flux in that single pixel is now distributed over so many pixels
2062(the sum of all the pixels in each convolved star is actually equal to
2063the value of the single pixel before convolution).  Sufi explained how a
2064PSF with a larger FWHM would make the points even wider than this
2065(distributing their flux in a larger area).  With the convolved image
2066ready, they were prepared to re-sample it to the original pixel scale
2067Sufi had planned [from the ‘$ astmkprof -P’ command above, recall that
2068MakeProfiles had over-sampled the image by 5 times].  Sufi explained the
2069basic concepts of warping the image to his student and ran Warp with the
2070following command:
2071
2072     $ astwarp --scale=1/5 --centeroncorner cat_convolved.fits
2073     Warp started on Sat Oct  6 16:51:59 953
2074      Using 8 CPU threads.
2075      Input: cat_convolved.fits (hdu: 1)
2076      matrix:
2077             0.2000   0.0000   0.4000
2078             0.0000   0.2000   0.4000
2079             0.0000   0.0000   1.0000
2080
2081     $ ls
2082     0_cat.fits          cat_convolved_scaled.fits     cat.txt
2083     cat_convolved.fits  cat_profiles.fits
2084
2085     $ astfits -p cat_convolved_scaled.fits | grep NAXIS
2086     NAXIS   =                    2 / number of data axes
2087     NAXIS1  =                  523 / length of data axis 1
2088     NAXIS2  =                  523 / length of data axis 2
2089
2090cat_convolved_scaled.fits’ now has the correct pixel scale.  However,
2091the image is still larger than what we had wanted, it is 523
2092($499+12+12$) by 523 pixels.  The student is slightly confused, so Sufi
2093also re-samples the PSF with the same scale by running
2094
2095     $ astwarp --scale=1/5 --centeroncorner 0_cat.fits
2096     $ astfits -p 0_cat_scaled.fits | grep NAXIS
2097     NAXIS   =                    2 / number of data axes
2098     NAXIS1  =                   25 / length of data axis 1
2099     NAXIS2  =                   25 / length of data axis 2
2100
2101Sufi notes that $25=(2\times12)+1$ and goes on to explain how frequency
2102space convolution will dim the edges and that is why he added the
2103‘--prepforconv’ option to MakeProfiles, see *note If convolving
2104afterwards::.  Now that convolution is done, Sufi can remove those extra
2105pixels using Crop with the command below.  Crop’s ‘--section’ option
2106accepts coordinates inclusively and counting from 1 (according to the
2107FITS standard), so the crop region’s first pixel has to be 13, not 12.
2108
2109     $ astcrop cat_convolved_scaled.fits --section=13:*-12,13:*-12    \
2110               --mode=img --zeroisnotblank
2111     Crop started on Sat Oct  6 17:03:24 953
2112       - Read metadata of 1 image.                          0.001304 seconds
2113       ---- ...nvolved_scaled_cropped.fits created: 1 input.
2114     Crop finished in:  0.027204 seconds
2115
2116     $ls
2117     0_cat.fits          cat_convolved_scaled_cropped.fits  cat_profiles.fits
2118     cat_convolved.fits  cat_convolved_scaled.fits          cat.txt
2119
2120Finally, ‘cat_convolved_scaled_cropped.fits’ is $499\times499$ pixels
2121and the mock Andromeda galaxy is centered on the central pixel (open the
2122image in a FITS viewer and confirm this by zooming into the center, note
2123that an even-width image wouldn’t have a central pixel).  This is the
2124same dimensions as Sufi had desired in the beginning.  All this trouble
2125was certainly worth it because now there is no dimming on the edges of
2126the image and the profile centers are more accurately sampled.
2127
2128   The final step to simulate a real observation would be to add noise
2129to the image.  Sufi set the zero point magnitude to the same value that
2130he set when making the mock profiles and looking again at his
2131observation log, he had measured the background flux near the nebula had
2132a magnitude of 7 that night.  So using these values he ran MakeNoise:
2133
2134     $ astmknoise --zeropoint=18 --background=7 --output=out.fits    \
2135                  cat_convolved_scaled_cropped.fits
2136     MakeNoise started on Sat Oct  6 17:05:06 953
2137       - Generator type: ranlxs1
2138       - Generator seed: 1428318100
2139     MakeNoise finished in:  0.033491 (seconds)
2140
2141     $ls
2142     0_cat.fits         cat_convolved_scaled_cropped.fits cat_profiles.fits
2143     cat_convolved.fits cat_convolved_scaled.fits         cat.txt  out.fits
2144
2145The ‘out.fits’ file now contains the noised image of the mock catalog
2146Sufi had asked for.  Seeing how the ‘--output’ option allows the user to
2147specify the name of the output file, the student was confused and wanted
2148to know why Sufi hadn’t used it more regularly before?  Sufi then
2149explained to him that for intermediate steps, you can rely on the
2150automatic output of the programs, see *note Automatic output::.  Doing
2151so will give all the intermediate files a similar basic name structure,
2152so in the end you can simply remove them all with the Shell’s
2153capabilities, and it will be familiar for other users.  So Sufi decided
2154to show this to the student by making a shell script from the commands
2155he had used before.
2156
2157   The command-line shell has the capability to read all the separate
2158input commands from a file.  This is useful when you want to do the same
2159thing multiple times, with only the names of the files or minor
2160parameters changing between the different instances.  Using the shell’s
2161history (by pressing the up keyboard key) Sufi reviewed all the commands
2162and then he retrieved the last 5 commands with the ‘$ history 5’
2163command.  He selected all those lines he had input and put them in a
2164text file named ‘mymock.sh’.  Then he defined the ‘edge’ and ‘base’
2165shell variables for easier customization later.  Finally, before every
2166command, he added some comments (lines starting with <#>) for future
2167readability.
2168
2169     edge=12
2170     base=cat
2171
2172     # Stop running next commands if one fails.
2173     set -e
2174
2175     # Remove any (possibly) existing output (from previous runs)
2176     # before starting.
2177     rm -f out.fits
2178
2179     # Run MakeProfiles to create an oversampled FITS image.
2180     astmkprof --prepforconv --mergedsize=499,499 --zeropoint=18.0 \
2181               "$base".txt
2182
2183     # Convolve the created image with the kernel.
2184     astconvolve --kernel=0_"$base".fits "$base"_profiles.fits \
2185                 --output="$base"_convolved.fits
2186
2187     # Scale the image back to the intended resolution.
2188     astwarp --scale=1/5 --centeroncorner "$base"_convolved.fits
2189
2190     # Crop the edges out (dimmed during convolution). ‘--section’
2191     # accepts inclusive coordinates, so the start of the section
2192     # must be one pixel larger than its end.
2193     st_edge=$(( edge + 1 ))
2194     astcrop "$base"_convolved_scaled.fits --zeroisnotblank \
2195             --mode=img --section=$st_edge:*-$edge,$st_edge:*-$edge
2196
2197     # Add noise to the image.
2198     astmknoise --zeropoint=18 --background=7 --output=out.fits \
2199                "$base"_convolved_scaled_cropped.fits
2200
2201     # Remove all the temporary files.
2202     rm 0*.fits "$base"*.fits
2203
2204   He used this chance to remind the student of the importance of
2205comments in code or shell scripts: when writing the code, you have a
2206good mental picture of what you are doing, so writing comments might
2207seem superfluous and excessive.  However, in one month when you want to
2208re-use the script, you have lost that mental picture and remembering it
2209can be time-consuming and frustrating.  The importance of comments is
2210further amplified when you want to share the script with a
2211friend/colleague.  So it is good to accompany any script/code with
2212useful comments while you are writing it (create a good mental picture
2213of what/why you are doing something).
2214
2215   Sufi then explained to the eager student that you define a variable
2216by giving it a name, followed by an ‘=’ sign and the value you want.
2217Then you can reference that variable from anywhere in the script by
2218calling its name with a ‘$’ prefix.  So in the script whenever you see
2219‘$base’, the value we defined for it above is used.  If you use advanced
2220editors like GNU Emacs or even simpler ones like Gedit (part of the
2221GNOME graphical user interface) the variables will become a different
2222color which can really help in understanding the script.  We have put
2223all the ‘$base’ variables in double quotation marks (‘"’) so the
2224variable name and the following text do not get mixed, the shell is
2225going to ignore the ‘"’ after replacing the variable value.  To make the
2226script executable, Sufi ran the following command:
2227
2228     $ chmod +x mymock.sh
2229
2230Then finally, Sufi ran the script, simply by calling its file name:
2231
2232     $ ./mymock.sh
2233
2234   After the script finished, the only file remaining is the ‘out.fits2235file that Sufi had wanted in the beginning.  Sufi then explained to the
2236student how he could run this script anywhere that he has a catalog if
2237the script is in the same directory.  The only thing the student had to
2238modify in the script was the name of the catalog (the value of the
2239‘base’ variable in the start of the script) and the value to the ‘edge’
2240variable if he changed the PSF size.  The student was also happy to hear
2241that he won’t need to make it executable again when he makes changes
2242later, it will remain executable unless he explicitly changes the
2243executable flag with ‘chmod’.
2244
2245   The student was really excited, since now, through simple shell
2246scripting, he could really speed up his work and run any command in any
2247fashion he likes allowing him to be much more creative in his works.
2248Until now he was using the graphical user interface which doesn’t have
2249such a facility and doing repetitive things on it was really frustrating
2250and some times he would make mistakes.  So he left to go and try
2251scripting on his own computer.
2252
2253   Sufi could now get back to his own work and see if the simulated
2254nebula which resembled the one in the Andromeda constellation could be
2255detected or not.  Although it was extremely faint(2), fortunately it
2256passed his detection tests and he wrote it in the draft manuscript that
2257would later become “Book of fixed stars”.  He still had to check the
2258other nebula he saw from Yemen and several other such objects, but they
2259could wait until tomorrow (thanks to the shell script, he only has to
2260define a new catalog).  It was nearly sunset and they had to begin
2261preparing for the night’s measurements on the ecliptic.
2262
2263* Menu:
2264
2265* General program usage tutorial::
2266
2267   ---------- Footnotes ----------
2268
2269   (1) In Latin Sufi is known as Azophi.  He was an Iranian astronomer.
2270His manuscript “Book of fixed stars” contains the first recorded
2271observations of the Andromeda galaxy, the Large Magellanic Cloud and
2272seven other non-stellar or ‘nebulous’ objects.
2273
2274   (2) The brightness of a diffuse object is added over all its pixels
2275to give its final magnitude, see *note Brightness flux magnitude::.  So
2276although the magnitude 3.44 (of the mock nebula) is orders of magnitude
2277brighter than 6 (of the stars), the central galaxy is much fainter.  Put
2278another way, the brightness is distributed over a large area in the case
2279of a nebula.
2280
2281
2282File: gnuastro.info,  Node: General program usage tutorial,  Next: Detecting large extended targets,  Prev: Sufi simulates a detection,  Up: Tutorials
2283
22842.2 General program usage tutorial
2285==================================
2286
2287Measuring colors of astronomical objects in broad-band or narrow-band
2288images is one of the most basic and common steps in astronomical
2289analysis.  Here, we will use Gnuastro’s programs to get a physical scale
2290(area at certain redshifts) of the field we are studying, detect objects
2291in a Hubble Space Telescope (HST) image, measure their colors and
2292identify the ones with the strongest colors, do a visual inspection of
2293these objects and inspect spatial position in the image.  After this
2294tutorial, you can also try the *note Detecting large extended targets::
2295tutorial which goes into a little more detail on detecting very low
2296surface brightness signal.
2297
2298   During the tutorial, we will take many detours to explain, and
2299practically demonstrate, the many capabilities of Gnuastro’s programs.
2300In the end you will see that the things you learned during this tutorial
2301are much more generic than this particular problem and can be used in
2302solving a wide variety of problems involving the analysis of data
2303(images or tables).  So please don’t rush, and go through the steps
2304patiently to optimally master Gnuastro.
2305
2306   In this tutorial, we’ll use the HSTeXtreme Deep Field
2307(https://archive.stsci.edu/prepds/xdf) dataset.  Like almost all
2308astronomical surveys, this dataset is free for download and usable by
2309the public.  You will need the following tools in this tutorial:
2310Gnuastro, SAO DS9 (1), GNU Wget(2), and AWK (most common implementation
2311is GNU AWK(3)).
2312
2313   This tutorial was first prepared for the “Exploring the Ultra-Low
2314Surface Brightness Universe” workshop (November 2017) at the ISSI in
2315Bern, Switzerland.  It was further extended in the “4th Indo-French
2316Astronomy School” (July 2018) organized by LIO, CRAL CNRS UMR5574, UCBL,
2317and IUCAA in Lyon, France.  We are very grateful to the organizers of
2318these workshops and the attendees for the very fruitful discussions and
2319suggestions that made this tutorial possible.
2320
2321*Write the example commands manually:* Try to type the example commands
2322on your terminal manually and use the history feature of your
2323command-line (by pressing the “up” button to retrieve previous
2324commands).  Don’t simply copy and paste the commands shown here.  This
2325will help simulate future situations when you are processing your own
2326datasets.
2327
2328* Menu:
2329
2330* Calling Gnuastro's programs::  Easy way to find Gnuastro’s programs.
2331* Accessing documentation::     Access to manual of programs you are running.
2332* Setup and data download::     Setup this template and download datasets.
2333* Dataset inspection and cropping::  Crop the flat region to use in next steps.
2334* Angular coverage on the sky::  Measure the field size on the sky.
2335* Cosmological coverage::       Measure the field size at different redshifts.
2336* Building custom programs with the library::  Easy way to build new programs.
2337* Option management and configuration files::  Dealing with options and configuring them.
2338* Warping to a new pixel grid::  Transforming/warping the dataset.
2339* NoiseChisel and Multiextension FITS files::  Running NoiseChisel and having multiple HDUs.
2340* NoiseChisel optimization for detection::  Check NoiseChisel’s operation and improve it.
2341* NoiseChisel optimization for storage::  Dramatically decrease output’s volume.
2342* Segmentation and making a catalog::  Finding true peaks and creating a catalog.
2343* Working with catalogs estimating colors::  Estimating colors using the catalogs.
2344* Column statistics color-magnitude diagram::  Visualizing column correlations.
2345* Aperture photometry::         Doing photometry on a fixed aperture.
2346* Matching catalogs::           Easily find corresponding rows from two catalogs.
2347* Finding reddest clumps and visual inspection::  Selecting some targets and inspecting them.
2348* Writing scripts to automate the steps::  Scripts will greatly help in re-doing things fast.
2349* Citing and acknowledging Gnuastro::  How to cite and acknowledge Gnuastro in your papers.
2350
2351   ---------- Footnotes ----------
2352
2353   (1) See *note SAO DS9::, available at
2354<http://ds9.si.edu/site/Home.html>
2355
2356   (2) <https://www.gnu.org/software/wget>
2357
2358   (3) <https://www.gnu.org/software/gawk>
2359
2360
2361File: gnuastro.info,  Node: Calling Gnuastro's programs,  Next: Accessing documentation,  Prev: General program usage tutorial,  Up: General program usage tutorial
2362
23632.2.1 Calling Gnuastro’s programs
2364---------------------------------
2365
2366A handy feature of Gnuastro is that all program names start with ‘ast’.
2367This will allow your command-line processor to easily list and
2368auto-complete Gnuastro’s programs for you.  Try typing the following
2369command (press <TAB> key when you see ‘<TAB>’) to see the list:
2370
2371     $ ast<TAB><TAB>
2372
2373Any program that starts with ‘ast’ (including all Gnuastro programs)
2374will be shown.  By choosing the subsequent characters of your desired
2375program and pressing <<TAB><TAB>> again, the list will narrow down and
2376the program name will auto-complete once your input characters are
2377unambiguous.  In short, you often don’t need to type the full name of
2378the program you want to run.
2379
2380
2381File: gnuastro.info,  Node: Accessing documentation,  Next: Setup and data download,  Prev: Calling Gnuastro's programs,  Up: General program usage tutorial
2382
23832.2.2 Accessing documentation
2384-----------------------------
2385
2386Gnuastro contains a large number of programs and it is natural to forget
2387the details of each program’s options or inputs and outputs.  Therefore,
2388before starting the analysis steps of this tutorial, let’s review how
2389you can access this book to refresh your memory any time you want,
2390without having to take your hands off the keyboard.
2391
2392   When you install Gnuastro, this book is also installed on your system
2393along with all the programs and libraries, so you don’t need an internet
2394connection to to access/read it.  Also, by accessing this book as
2395described below, you can be sure that it corresponds to your installed
2396version of Gnuastro.
2397
2398   GNU Info(1) is the program in charge of displaying the manual on the
2399command-line (for more, see *note Info::).  To see this whole book on
2400your command-line, please run the following command and press subsequent
2401keys.  Info has its own mini-environment, therefore we’ll show the keys
2402that must be pressed in the mini-environment after a ‘->’ sign.  You can
2403also ignore anything after the ‘#’ sign in the middle of the line, they
2404are only for your information.
2405
2406     $ info gnuastro                # Open the top of the manual.
2407     -> <SPACE>                     # All the book chapters.
2408     -> <SPACE>                     # Continue down: show sections.
2409     -> <SPACE> ...                 # Keep pressing space to go down.
2410     -> q                           # Quit Info, return to the command-line.
2411
2412   The thing that greatly simplifies navigation in Info is the links
2413(regions with an underline).  You can immediately go to the next link in
2414the page with the <<TAB>> key and press <<ENTER>> on it to go into that
2415part of the manual.  Try the commands above again, but this time also
2416use <<TAB>> to go to the links and press <<ENTER>> on them to go to the
2417respective section of the book.  Then follow a few more links and go
2418deeper into the book.  To return to the previous page, press <l> (small
2419L). If you are searching for a specific phrase in the whole book (for
2420example an option name), press <s> and type your search phrase and end
2421it with an <<ENTER>>.
2422
2423   You don’t need to start from the top of the manual every time.  For
2424example, to get to *note Invoking astnoisechisel::, run the following
2425command.  In general, all programs have such an “Invoking ProgramName”
2426section in this book.  These sections are specifically for the
2427description of inputs, outputs and configuration options of each
2428program.  You can access them directly for each program by giving its
2429executable name to Info.
2430
2431     $ info astnoisechisel
2432
2433   The other sections don’t have such shortcuts.  To directly access
2434them from the command-line, you need to tell Info to look into
2435Gnuastro’s manual, then look for the specific section (an unambiguous
2436title is necessary).  For example, if you only want to review/remember
2437NoiseChisel’s *note Detection options::), just run the following
2438command.  Note how case is irrelevant for Info when calling a title in
2439this manner.
2440
2441     $ info gnuastro "Detection options"
2442
2443   In general, Info is a powerful and convenient way to access this
2444whole book with detailed information about the programs you are running.
2445If you are not already familiar with it, please run the following
2446command and just read along and do what it says to learn it.  Don’t stop
2447until you feel sufficiently fluent in it.  Please invest the half an
2448hour’s time necessary to start using Info comfortably.  It will greatly
2449improve your productivity and you will start reaping the rewards of this
2450investment very soon.
2451
2452     $ info info
2453
2454   As a good scientist you need to feel comfortable to play with the
2455features/options and avoid (be critical to) using default values as much
2456as possible.  On the other hand, our human memory is limited, so it is
2457important to be able to easily access any part of this book fast and
2458remember the option names, what they do and their acceptable values.
2459
2460   If you just want the option names and a short description, calling
2461the program with the ‘--help’ option might also be a good solution like
2462the first example below.  If you know a few characters of the option
2463name, you can feed the output to ‘grep’ like the second or third example
2464commands.
2465
2466     $ astnoisechisel --help
2467     $ astnoisechisel --help | grep quant
2468     $ astnoisechisel --help | grep check
2469
2470   ---------- Footnotes ----------
2471
2472   (1) GNU Info is already available on almost all Unix-like operating
2473systems.
2474
2475
2476File: gnuastro.info,  Node: Setup and data download,  Next: Dataset inspection and cropping,  Prev: Accessing documentation,  Up: General program usage tutorial
2477
24782.2.3 Setup and data download
2479-----------------------------
2480
2481The first step in the analysis of the tutorial is to download the
2482necessary input datasets.  First, to keep things clean, let’s create a
2483‘gnuastro-tutorial’ directory and continue all future steps in it:
2484
2485     $ mkdir gnuastro-tutorial
2486     $ cd gnuastro-tutorial
2487
2488   We will be using the near infra-red Wide Field Camera
2489(http://www.stsci.edu/hst/wfc3) dataset.  If you already have them in
2490another directory (for example ‘XDFDIR’, with the same FITS file names),
2491you can set the ‘download’ directory to be a symbolic link to ‘XDFDIR’
2492with a command like this:
2493
2494     $ ln -s XDFDIR download
2495
2496Otherwise, when the following images aren’t already present on your
2497system, you can make a ‘download’ directory and download them there.
2498
2499     $ mkdir download
2500     $ cd download
2501     $ xdfurl=http://archive.stsci.edu/pub/hlsp/xdf
2502     $ wget $xdfurl/hlsp_xdf_hst_wfc3ir-60mas_hudf_f105w_v1_sci.fits
2503     $ wget $xdfurl/hlsp_xdf_hst_wfc3ir-60mas_hudf_f125w_v1_sci.fits
2504     $ wget $xdfurl/hlsp_xdf_hst_wfc3ir-60mas_hudf_f160w_v1_sci.fits
2505     $ cd ..
2506
2507In this tutorial, we’ll just use these three filters.  Later, you may
2508need to download more filters.  To do that, you can use the shell’s
2509‘for’ loop to download them all in series (one after the other(1)) with
2510one command like the one below for the WFC3 filters.  Put this command
2511instead of the three ‘wget’ commands above.  Recall that all the extra
2512spaces, back-slashes (‘\’), and new lines can be ignored if you are
2513typing on the lines on the terminal.
2514
2515     $ for f in f105w f125w f140w f160w; do \
2516         wget $xdfurl/hlsp_xdf_hst_wfc3ir-60mas_hudf_"$f"_v1_sci.fits; \
2517       done
2518
2519   ---------- Footnotes ----------
2520
2521   (1) Note that you only have one port to the internet, so downloading
2522in parallel will actually be slower than downloading in series.
2523
2524
2525File: gnuastro.info,  Node: Dataset inspection and cropping,  Next: Angular coverage on the sky,  Prev: Setup and data download,  Up: General program usage tutorial
2526
25272.2.4 Dataset inspection and cropping
2528-------------------------------------
2529
2530First, let’s visually inspect the datasets we downloaded in *note Setup
2531and data download::.  Let’s take F160W image as an example.  Do the
2532steps below with the other image(s) too (and later with any dataset that
2533you want to work on).  It is very important to get a good visual feeling
2534of the dataset you intend to use.  Also, note how SAO DS9 (used here for
2535visual inspection of FITS images) doesn’t follow the GNU style of
2536options where “long” and “short” options are preceded by ‘--’ and ‘-’
2537respectively (for example ‘--width’ and ‘-w’, see *note Options::).
2538
2539   Run the command below to see the F160W image with DS9.  Ds9’s
2540‘-zscale’ scaling is good to visually highlight the low surface
2541brightness regions, and as the name suggests, ‘-zoom to fit’ will fit
2542the whole dataset in the window.  If the window is too small, expand it
2543with your mouse, then press the “zoom” button on the top row of buttons
2544above the image.  Afterwards, in the bottom row of buttons, press “zoom
2545fit”.  You can also zoom in and out by scrolling your mouse or the
2546respective operation on your touch-pad when your cursor/pointer is over
2547the image.
2548
2549     $ ds9 download/hlsp_xdf_hst_wfc3ir-60mas_hudf_f160w_v1_sci.fits     \
2550           -zscale -zoom to fit
2551
2552   As you hover your mouse over the image, notice how the “Value” and
2553positional fields on the top of the ds9 window get updated.  The first
2554thing you might notice is that when you hover the mouse over the regions
2555with no data, they have a value of zero.  The next thing might be that
2556the dataset actually has two “depth”s (see *note Quantifying measurement
2557limits::).  Recall that this is a combined/reduced image of many
2558exposures, and the parts that have more exposures are deeper.  In
2559particular, the exposure time of the deep inner region is larger than 4
2560times of the outer (more shallower) parts.
2561
2562   To simplify the analysis in this tutorial, we’ll only be working on
2563the deep field, so let’s crop it out of the full dataset.  Fortunately
2564the XDF survey web page (above) contains the vertices of the deep flat
2565WFC3-IR field.  With Gnuastro’s Crop program(1), you can use those
2566vertices to cutout this deep region from the larger image.  But before
2567that, to keep things organized, let’s make a directory called ‘flat-ir’
2568and keep the flat (single-depth) regions in that directory (with a
2569‘‘xdf-’’ suffix for a shorter and easier filename).
2570
2571     $ mkdir flat-ir
2572     $ astcrop --mode=wcs -h0 --output=flat-ir/xdf-f105w.fits \
2573               --polygon="53.187414,-27.779152 : 53.159507,-27.759633 : \
2574                          53.134517,-27.787144 : 53.161906,-27.807208" \
2575               download/hlsp_xdf_hst_wfc3ir-60mas_hudf_f105w_v1_sci.fits
2576
2577     $ astcrop --mode=wcs -h0 --output=flat-ir/xdf-f125w.fits \
2578               --polygon="53.187414,-27.779152 : 53.159507,-27.759633 : \
2579                          53.134517,-27.787144 : 53.161906,-27.807208" \
2580               download/hlsp_xdf_hst_wfc3ir-60mas_hudf_f125w_v1_sci.fits
2581
2582     $ astcrop --mode=wcs -h0 --output=flat-ir/xdf-f160w.fits \
2583               --polygon="53.187414,-27.779152 : 53.159507,-27.759633 : \
2584                          53.134517,-27.787144 : 53.161906,-27.807208" \
2585               download/hlsp_xdf_hst_wfc3ir-60mas_hudf_f160w_v1_sci.fits
2586
2587   The only thing varying in the three calls to Gnuastro’s Crop program
2588is the filter name!  Note how everything else is the same.  In such
2589cases, you should generally avoid repeating a command manually, it is
2590prone to many bugs, and as you see, it is very hard to read (didn’t you
2591suddenly write a ‘7’ as an ‘8’?).  To simplify the command, and later
2592allow work on more filters, we can use the shell’s ‘for’ loop as shown
2593below.  Notice how the place where the filter names (‘f105w’, ‘f125w’
2594and ‘f160w’) are used above, have been replaced with ‘$f’ (the shell
2595variable that ‘for’ will update in every loop) below.
2596
2597     $ rm flat-ir/*.fits
2598     $ for f in f105w f125w f160w; do \
2599         astcrop --mode=wcs -h0 --output=flat-ir/xdf-$f.fits \
2600                 --polygon="53.187414,-27.779152 : 53.159507,-27.759633 : \
2601                            53.134517,-27.787144 : 53.161906,-27.807208" \
2602                 download/hlsp_xdf_hst_wfc3ir-60mas_hudf_"$f"_v1_sci.fits; \
2603       done
2604
2605   Please open these images and inspect them with the same ‘ds9’ command
2606you used above.  You will see how it is nicely flat now and doesn’t have
2607varying depths.  Another important result of this crop is that regions
2608with no data now have a NaN (Not-a-Number, or a blank value) value.  In
2609the downloaded files, such regions had a value of zero.  However, zero
2610is a number, and is thus meaningful, especially when you later want to
2611NoiseChisel(2).  Generally, when you want to ignore some pixels in a
2612dataset, and avoid higher-level ambiguities or complications, it is
2613always best to give them blank values (not zero, or some other absurdly
2614large or small number).  Gnuastro has the Arithmetic program for such
2615cases, and we’ll introduce it later in this tutorial.
2616
2617   ---------- Footnotes ----------
2618
2619   (1) To learn more about the crop program see *note Crop::.
2620
2621   (2) As you will see below, unlike most other detection algorithms,
2622NoiseChisel detects the objects from their faintest parts, it doesn’t
2623start with their high signal-to-noise ratio peaks.  Since the Sky is
2624already subtracted in many images and noise fluctuates around zero, zero
2625is commonly higher than the initial threshold applied.  Therefore not
2626ignoring zero-valued pixels in this image, will cause them to part of
2627the detections!
2628
2629
2630File: gnuastro.info,  Node: Angular coverage on the sky,  Next: Cosmological coverage,  Prev: Dataset inspection and cropping,  Up: General program usage tutorial
2631
26322.2.5 Angular coverage on the sky
2633---------------------------------
2634
2635This is the deepest image we currently have of the sky.  The first thing
2636that comes to mind may be this: “How large is this field on the sky?”.
2637You can get a fast and crude answer with Gnuastro’s Fits program using
2638this command:
2639
2640     astfits flat-ir/xdf-f160w.fits --skycoverage
2641
2642   It will print the sky coverage in two formats (all numbers are in
2643units of degrees for this image): 1) the image’s central RA and Dec and
2644full width around that center, 2) the range of RA and Dec covered by
2645this image.  You can use these values in various online query systems.
2646You can also use this option to automatically calculate the area covered
2647by this image.  With the ‘--quiet’ option, the printed output of
2648‘--skycoverage’ will not contain human-readable text, making it easier
2649for further processing:
2650
2651     astfits flat-ir/xdf-f160w.fits --skycoverage --quiet
2652
2653   The second row is the coverage range along RA and Dec (compare with
2654the outputs before using ‘--quiet’).  We can thus simply subtract the
2655second from the first column and multiply it with the difference of the
2656fourth and third columns to calculate the image area.  We’ll also
2657multiply each by 60 to have the area in arc-minutes squared.
2658
2659     astfits flat-ir/xdf-f160w.fits --skycoverage --quiet \
2660             | awk 'NR==2{print ($2-$1)*60*($4-$3)*60}'
2661
2662   The returned value is $9.06711$ arcmin$^2$.  *However, this method
2663ignores the fact many of the image pixels are blank!*  In other words,
2664the image does cover this area, but there is no data in more than half
2665of the pixels.  So let’s calculate the area coverage over-which we
2666actually have data.
2667
2668   The FITS world coordinate system (WCS) meta data standard contains
2669the key to answering this question.  Run the following command to see
2670all the FITS keywords (metadata) for one of the images (almost identical
2671with the other images because they were are scaled to the same region of
2672Sky):
2673
2674     astfits flat-ir/xdf-f160w.fits -h1
2675
2676   Look into the keywords grouped under the ‘‘World Coordinate System
2677(WCS)’’ title.  These keywords define how the image relates to the
2678outside world.  In particular, the ‘CDELT*’ keywords (or ‘CDELT1’ and
2679‘CDELT2’ in this 2D image) contain the “Coordinate DELTa” (or change in
2680coordinate units) with a change in one pixel.  But what is the units of
2681each “world” coordinate?  The ‘CUNIT*’ keywords (for “Coordinate UNIT”)
2682have the answer.  In this case, both ‘CUNIT1’ and ‘CUNIT1’ have a value
2683of ‘deg’, so both “world” coordinates are in units of degrees.  We can
2684thus conclude that the value of ‘CDELT*’ is in units of
2685degrees-per-pixel(1).
2686
2687   With the commands below, we’ll use ‘CDELT’ (along with the image
2688size) to find the answer of our initial question: “how much of the sky
2689does this image cover?”.  The lines starting with ‘##’ are just comments
2690for you to read and understand each command.  Don’t type them on the
2691terminal.  The commands are intentionally repetitive in some places to
2692better understand each step and also to demonstrate the beauty of
2693command-line features like history, variables, pipes and loops (which
2694you will commonly use as you master the command-line).
2695
2696*Use shell history:* Don’t forget to make effective use of your shell’s
2697history: you don’t have to re-type previous command to add something to
2698them.  This is especially convenient when you just want to make a small
2699change to your previous command.  Press the “up” key on your keyboard
2700(possibly multiple times) to see your previous command(s) and modify
2701them accordingly.
2702
2703     ## If your system language uses ',' (not '.') as decimal separator.
2704     $ export LANG=C
2705
2706     ## See the general statistics of non-blank pixel values.
2707     $ aststatistics flat-ir/xdf-f160w.fits
2708
2709     ## We only want the number of non-blank pixels.
2710     $ aststatistics flat-ir/xdf-f160w.fits --number
2711
2712     ## Keep the result of the command above in the shell variable `n'.
2713     $ n=$(aststatistics flat-ir/xdf-f160w.fits --number)
2714
2715     ## See what is stored the shell variable `n'.
2716     $ echo $n
2717
2718     ## Show all the FITS keywords of this image.
2719     $ astfits flat-ir/xdf-f160w.fits -h1
2720
2721     ## The resolution (in degrees/pixel) is in the `CDELT' keywords.
2722     ## Only show lines that contain these characters, by feeding
2723     ## the output of the previous command to the `grep' program.
2724     $ astfits flat-ir/xdf-f160w.fits -h1 | grep CDELT
2725
2726     ## Since the resolution of both dimensions is (approximately) equal,
2727     ## we'll only use one of them (CDELT1).
2728     $ astfits flat-ir/xdf-f160w.fits -h1 | grep CDELT1
2729
2730     ## To extract the value (third token in the line above), we'll
2731     ## feed the output to AWK. Note that the first two tokens are
2732     ## `CDELT1' and `='.
2733     $ astfits flat-ir/xdf-f160w.fits -h1 | grep CDELT1 | awk '{print $3}'
2734
2735     ## Save it as the shell variable `r'.
2736     $ r=$(astfits flat-ir/xdf-f160w.fits -h1 | grep CDELT1   \
2737                   | awk '{print $3}')
2738
2739     ## Print the values of `n' and `r'.
2740     $ echo $n $r
2741
2742     ## Use the number of pixels (first number passed to AWK) and
2743     ## length of each pixel's edge (second number passed to AWK)
2744     ## to estimate the area of the field in arc-minutes squared.
2745     $ echo $n $r | awk '{print $1 * ($2*60)^2}'
2746
2747   The output of the last command (area of this field) is 4.03817 (or
2748approximately 4.04) arc-minutes squared.  Just for comparison, this is
2749roughly 175 times smaller than the average moon’s angular area (with a
2750diameter of 30arc-minutes or half a degree).
2751
2752   Some FITS writers don’t use the ‘CDELT’ convention, making it hard to
2753use the steps above.  In such cases, you can extract the pixel scale
2754with the ‘--pixelscale’ option of Gnuastro’s Fits program like the
2755command below.  Like the ‘--skycoverage’ option above, you can also use
2756the ‘--quiet’ option to allow easy usage of the values in scripts.
2757
2758     $ astfits flat-ir/xdf-f160w.fits --pixelscale
2759
2760*AWK for table/value processing:* As you saw above AWK is a powerful and
2761simple tool for text processing.  You will see it often in shell
2762scripts.  GNU AWK (the most common implementation) comes with a free and
2763wonderful book (https://www.gnu.org/software/gawk/manual/) in the same
2764format as this book which will allow you to master it nicely.  Just like
2765this manual, you can also access GNU AWK’s manual on the command-line
2766whenever necessary without taking your hands off the keyboard.  Just run
2767‘info awk’.
2768
2769*Your locale doesn’t use ‘.’ as decimal separator:* the input/output of
2770some core operating system tools like ‘awk’ or ‘seq’ depend on the
2771system locale
2772(https://en.wikipedia.org/wiki/Locale_(computer_software)).  For example
2773in Spanish and some other languages the decimal separator (symbol used
2774to separate the integer and fractional part of a number), is a comma.
2775Therefore in systems that have Spanish as their default Locale, ‘seq’
2776will print half of unity as ‘‘0,5’’ (instead of ‘‘0.5’’).  This can
2777cause problems for parsing the printed numbers in other programs.  You
2778can check your current locale with the ‘locale’ command.  You can test
2779your default decimal separator with this command:
2780
2781     seq 0.5 1
2782
2783   To avoid these kinds of locale-specific problems (for example another
2784program not being able to read ‘‘0,5’’ as half of unity), you can change
2785the locale by setting the ‘LANG’ environment variable (or the
2786lower-level/generic ‘LC_ALL’).  You can do it only for a single command
2787(the first one below), or all commands within the running session (the
2788second command below):
2789
2790     ## Change the locale to the standard, only for this 'seq' command.
2791     $ LANG=C seq 0.5 1
2792
2793     ## Change the locale to the standard, for all commands after it.
2794     $ export LANG=C
2795
2796   If you want to change it generally for all future sessions, you can
2797put the second command in your shell’s startup file.  For more on
2798startup files, please see *note Installation directory::.
2799
2800   ---------- Footnotes ----------
2801
2802   (1) With the FITS ‘CDELT’ convention, rotation (‘PC’ or ‘CD’
2803keywords) and scales (‘CDELT’) are separated.  In the FITS standard the
2804‘CDELT’ keywords are optional.  When ‘CDELT’ keywords aren’t present,
2805the ‘PC’ matrix is assumed to contain _both_ the coordinate rotation and
2806scales.  Note that not all FITS writers use the ‘CDELT’ convention.  So
2807you might not find the ‘CDELT’ keywords in the WCS meta data of some
2808FITS files.  However, all Gnuastro programs (which use the default FITS
2809keyword writing format of WCSLIB) write their output WCS with the
2810‘CDELT’ convention, even if the input doesn’t have it.  If your dataset
2811doesn’t use the ‘CDELT’ convention, you can feed it to any (simple)
2812Gnuastro program (for example Arithmetic) and the output will have the
2813‘CDELT’ keyword.  See Section 8 of the FITS standard
2814(https://fits.gsfc.nasa.gov/standard40/fits_standard40aa-le.pdf) for
2815more
2816
2817
2818File: gnuastro.info,  Node: Cosmological coverage,  Next: Building custom programs with the library,  Prev: Angular coverage on the sky,  Up: General program usage tutorial
2819
28202.2.6 Cosmological coverage
2821---------------------------
2822
2823Having found the angular coverage of the dataset in *note Angular
2824coverage on the sky::, we can now use Gnuastro to answer a more
2825physically motivated question: “How large is this area at different
2826redshifts?”.  To get a feeling of the tangential area that this field
2827covers at redshift 2, you can use Gnuastro’s CosmicCalcular program
2828(*note CosmicCalculator::).  In particular, you need the tangential
2829distance covered by 1 arc-second as raw output.  Combined with the
2830field’s area that was measured before, we can calculate the tangential
2831distance in Mega Parsecs squared ($Mpc^2$).
2832
2833     ## If your system language uses ',' (not '.') as decimal separator.
2834     $ export LANG=C
2835
2836     ## Print general cosmological properties at redshift 2 (for example).
2837     $ astcosmiccal -z2
2838
2839     ## When given a "Specific calculation" option, CosmicCalculator
2840     ## will just print that particular calculation. To see all such
2841     ## calculations, add a `--help' token to the previous command
2842     ## (under the same title). Note that with `--help', no processing
2843     ## is done, so you can always simply append it to remember
2844     ## something without modifying the command you want to run.
2845     $ astcosmiccal -z2 --help
2846
2847     ## Only print the "Tangential dist. covered by 1arcsec at z (kpc)".
2848     ## in units of kpc/arc-seconds.
2849     $ astcosmiccal -z2 --arcsectandist
2850
2851     ## But its easier to use the short version of this option (which
2852     ## can be appended to other short options.
2853     $ astcosmiccal -sz2
2854
2855     ## Convert this distance to kpc^2/arcmin^2 and save in `k'.
2856     $ k=$(astcosmiccal -sz2 | awk '{print ($1*60)^2}')
2857
2858     ## Re-calculate the area of the dataset in arcmin^2.
2859     $ n=$(aststatistics flat-ir/xdf-f160w.fits --number)
2860     $ r=$(astfits flat-ir/xdf-f160w.fits -h1 | grep CDELT1   \
2861                   | awk '{print $3}')
2862     $ a=$(echo $n $r | awk '{print $1 * ($2^2) * 3600}')
2863
2864     ## Multiply `k' and `a' and divide by 10^6 for value in Mpc^2.
2865     $ echo $k $a | awk '{print $1 * $2 / 1e6}'
2866
2867At redshift 2, this field therefore covers approximately 1.07 $Mpc^2$.
2868If you would like to see how this tangential area changes with redshift,
2869you can use a shell loop like below.
2870
2871     $ for z in 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0; do        \
2872         k=$(astcosmiccal -sz$z);                                  \
2873         echo $z $k $a | awk '{print $1, ($2*60)^2 * $3 / 1e6}';   \
2874       done
2875
2876Fortunately, the shell has a useful tool/program to print a sequence of
2877numbers that is nicely called ‘seq’.  You can use it instead of typing
2878all the different redshifts in this example.  For example the loop below
2879will calculate and print the tangential coverage of this field across a
2880larger range of redshifts (0.1 to 5) and with finer increments of 0.1.
2881
2882     ## If your system language uses ',' (not '.') as decimal separator.
2883     $ export LANG=C
2884
2885     ## The loop over the redshifts
2886     $ for z in $(seq 0.1 0.1 5); do                                  \
2887         k=$(astcosmiccal -z$z --arcsectandist);                      \
2888         echo $z $k $a | awk '{print $1, ($2*60)^2 * $3 / 1e6}';   \
2889       done
2890
2891
2892File: gnuastro.info,  Node: Building custom programs with the library,  Next: Option management and configuration files,  Prev: Cosmological coverage,  Up: General program usage tutorial
2893
28942.2.7 Building custom programs with the library
2895-----------------------------------------------
2896
2897In *note Cosmological coverage::, we repeated a certain
2898calculation/output of a program multiple times using the shell’s ‘for’
2899loop.  This simple way repeating a calculation is great when it is only
2900necessary once.  However, if you commonly need this calculation and
2901possibly for a larger number of redshifts at higher precision, the
2902command above can be slow (try it out to see).
2903
2904   This slowness of the repeated calls to a generic program (like
2905CosmicCalculator), is because it can have a lot of overhead on each
2906call.  To be generic and easy to operate, it has to parse the
2907command-line and all configuration files (see *note Option management
2908and configuration files::) which contain human-readable characters and
2909need a lot of pre-processing to be ready for processing by the computer.
2910Afterwards, CosmicCalculator has to check the sanity of its inputs and
2911check which of its many options you have asked for.  All the this
2912pre-processing takes as much time as the high-level calculation you are
2913requesting, and it has to re-do all of these for every redshift in your
2914loop.
2915
2916   To greatly speed up the processing, you can directly access the core
2917work-horse of CosmicCalculator without all that overhead by designing
2918your custom program for this job.  Using Gnuastro’s library, you can
2919write your own tiny program particularly designed for this exact
2920calculation (and nothing else!).  To do that, copy and paste the
2921following C program in a file called ‘myprogram.c’.
2922
2923     #include <math.h>
2924     #include <stdio.h>
2925     #include <stdlib.h>
2926     #include <gnuastro/cosmology.h>
2927
2928     int
2929     main(void)
2930     {
2931       double area=4.03817;          /* Area of field (arcmin^2). */
2932       double z, adist, tandist;     /* Temporary variables.      */
2933
2934       /* Constants from Plank 2018 (arXiv:1807.06209, Table 2) */
2935       double H0=67.66, olambda=0.6889, omatter=0.3111, oradiation=0;
2936
2937       /* Do the same thing for all redshifts (z) between 0.1 and 5. */
2938       for(z=0.1; z<5; z+=0.1)
2939         {
2940           /* Calculate the angular diameter distance. */
2941           adist=gal_cosmology_angular_distance(z, H0, olambda,
2942                                                omatter, oradiation);
2943
2944           /* Calculate the tangential distance of one arcsecond. */
2945           tandist = adist * 1000 * M_PI / 3600 / 180;
2946
2947           /* Print the redshift and area. */
2948           printf("%-5.2f %g\n", z, pow(tandist * 60,2) * area / 1e6);
2949         }
2950
2951       /* Tell the system that everything finished successfully. */
2952       return EXIT_SUCCESS;
2953     }
2954
2955Then run the following command to compile your program and run it.
2956
2957     $ astbuildprog myprogram.c
2958
2959In the command above, you used Gnuastro’s BuildProgram program.  Its job
2960is to greatly simplify the compilation, linking and running of simple C
2961programs that use Gnuastro’s library (like this one).  BuildProgram is
2962designed to manage Gnuastro’s dependencies, compile and link your custom
2963program and then run it.
2964
2965   Did you notice how your custom program was much faster than the
2966repeated calls to CosmicCalculator in the previous section?  You might
2967have noticed that a new file called ‘myprogram’ is also created in the
2968directory.  This is the compiled program that was created and run by the
2969command above (its in binary machine code format, not human-readable any
2970more).  You can run it again to get the same results with a command like
2971this:
2972
2973     $ ./myprogram
2974
2975   The efficiency of your custom ‘myprogram’ compared to repeated calls
2976to CosmicCalculator is because in the latter, the requested processing
2977is comparable to the necessary overheads.  For other programs that take
2978large input datasets and do complicated processing on them, the overhead
2979is usually negligible compared to the processing.  In such cases, the
2980libraries are only useful if you want a different/new processing
2981compared to the functionalities in Gnuastro’s existing programs.
2982
2983   Gnuastro has a large library which is used extensively by all the
2984programs.  In other words, the library is like the skeleton of Gnuastro.
2985For the full list of available functions classified by context, please
2986see *note Gnuastro library::.  Gnuastro’s library and BuildProgram are
2987created to make it easy for you to use these powerful features as you
2988like.  This gives you a high level of creativity, while also providing
2989efficiency and robustness.  Several other complete working examples
2990(involving images and tables) of Gnuastro’s libraries can be see in
2991*note Library demo programs::.
2992
2993   But for this tutorial, let’s stop discussing the libraries at this
2994point in and get back to Gnuastro’s already built programs which don’t
2995need any programming.  But before continuing, let’s clean up the files
2996we don’t need any more:
2997
2998     $ rm myprogram*
2999
3000
3001File: gnuastro.info,  Node: Option management and configuration files,  Next: Warping to a new pixel grid,  Prev: Building custom programs with the library,  Up: General program usage tutorial
3002
30032.2.8 Option management and configuration files
3004-----------------------------------------------
3005
3006None of Gnuastro’s programs keep a default value internally within their
3007code.  However, when you ran CosmicCalculator only with the ‘-z2’ option
3008(not specifying the cosmological parameters) in *note Cosmological
3009coverage::, it completed its processing and printed results.  Where did
3010the necessary cosmological parameters (like the matter density, etc)
3011that are necessary for its calculations come from?  Fast reply: the
3012values come from a configuration file (see *note Configuration file
3013precedence::).
3014
3015   CosmicCalculator is a small program with a limited set of
3016parameters/options.  Therefore, let’s use it to discuss configuration
3017files in Gnuastro (for more, you can always see *note Configuration
3018files::).  Configuration files are an important part of all Gnuastro’s
3019programs, especially the ones with a large number of options, so its
3020important to understand this part well .
3021
3022   Once you get comfortable with configuration files here, you can make
3023good use of them in all Gnuastro programs (for example, NoiseChisel).
3024For example, to do optimal detection on various datasets, you can have
3025configuration files for different noise properties.  The configuration
3026of each program (besides its version) is vital for the reproducibility
3027of your results, so it is important to manage them properly.
3028
3029   As we saw above, the full list of the options in all Gnuastro
3030programs can be seen with the ‘--help’ option.  Try calling it with
3031CosmicCalculator as shown below.  Note how options are grouped by
3032context to make it easier to find your desired option.  However, in each
3033group, options are ordered alphabetically.
3034
3035     $ astcosmiccal --help
3036
3037The options that need a value have an <=> sign after their long version
3038and ‘FLT’, ‘INT’ or ‘STR’ for floating point numbers, integer numbers,
3039and strings (filenames for example) respectively.  All options have a
3040long format and some have a short format (a single character), for more
3041see *note Options::.
3042
3043   When you are using a program, it is often necessary to check the
3044value the option has just before the program starts its processing.  In
3045other words, after it has parsed the command-line options and all
3046configuration files.  You can see the values of all options that need
3047one with the ‘--printparams’ or ‘-P’ option.  ‘--printparams’ is common
3048to all programs (see *note Common options::).  In the command below, try
3049replacing ‘-P’ with ‘--printparams’ to see how both do the same
3050operation.
3051
3052     $ astcosmiccal -P
3053
3054   Let’s say you want a different Hubble constant.  Try running the
3055following command (just adding ‘--H0=70’ after the command above) to see
3056how the Hubble constant in the output of the command above has changed.
3057
3058     $ astcosmiccal -P --H0=70
3059
3060Afterwards, delete the ‘-P’ and add a ‘-z2’ to see the calculations with
3061the new cosmology (or configuration).
3062
3063     $ astcosmiccal --H0=70 -z2
3064
3065   From the output of the ‘--help’ option, note how the option for
3066Hubble constant has both short (‘-H’) and long (‘--H0’) formats.  One
3067final note is that the equal (<=>) sign is not mandatory.  In the short
3068format, the value can stick to the actual option (the short option name
3069is just one character after-all, thus easily identifiable) and in the
3070long format, a white-space character is also enough.
3071
3072     $ astcosmiccal -H70    -z2
3073     $ astcosmiccal --H0 70 -z2 --arcsectandist
3074
3075When an option doesn’t need a value, and has a short format (like
3076‘--arcsectandist’), you can easily append it _before_ other short
3077options.  So the last command above can also be written as:
3078
3079     $ astcosmiccal --H0 70 -sz2
3080
3081   Let’s assume that in one project, you want to only use rounded
3082cosmological parameters (H0 of 70km/s/Mpc and matter density of 0.3).
3083You should therefore run CosmicCalculator like this:
3084
3085     $ astcosmiccal --H0=70 --olambda=0.7 --omatter=0.3 -z2
3086
3087   But having to type these extra options every time you run
3088CosmicCalculator will be prone to errors (typos in particular),
3089frustrating and slow.  Therefore in Gnuastro, you can put all the
3090options and their values in a “Configuration file” and tell the programs
3091to read the option values from there.
3092
3093   Let’s create a configuration file...  With your favorite text editor,
3094make a file named ‘my-cosmology.conf’ (or ‘my-cosmology.txt’, the suffix
3095doesn’t matter, but a more descriptive suffix like ‘.conf’ is
3096recommended).  Then put the following lines inside of it.  One space
3097between the option value and name is enough, the values are just under
3098each other to help in readability.  Also note that you can only use long
3099option names in configuration files.
3100
3101     H0       70
3102     olambda  0.7
3103     omatter  0.3
3104
3105You can now tell CosmicCalculator to read this file for option values
3106immediately using the ‘--config’ option as shown below.  Do you see how
3107the output of the following command corresponds to the option values in
3108my-cosmology.conf’, and is therefore identical to the previous command?
3109
3110     $ astcosmiccal --config=my-cosmology.conf -z2
3111
3112   But still, having to type ‘--config=my-cosmology.conf’ every time is
3113annoying, isn’t it?  If you need this cosmology every time you are
3114working in a specific directory, you can use Gnuastro’s default
3115configuration file names and avoid having to type it manually.
3116
3117   The default configuration files (that are checked if they exist) must
3118be placed in the hidden ‘.gnuastro’ sub-directory (in the same directory
3119you are running the program).  Their file name (within ‘.gnuastro’) must
3120also be the same as the program’s executable name.  So in the case of
3121CosmicCalculator, the default configuration file in a given directory is
3122‘.gnuastro/astcosmiccal.conf’.
3123
3124   Let’s do this.  We’ll first make a directory for our custom
3125cosmology, then build a ‘.gnuastro’ within it.  Finally, we’ll copy the
3126custom configuration file there:
3127
3128     $ mkdir my-cosmology
3129     $ mkdir my-cosmology/.gnuastro
3130     $ mv my-cosmology.conf my-cosmology/.gnuastro/astcosmiccal.conf
3131
3132   Once you run CosmicCalculator within ‘my-cosmology’ (as shown below),
3133you will see how your custom cosmology has been implemented without
3134having to type anything extra on the command-line.
3135
3136     $ cd my-cosmology
3137     $ astcosmiccal -P
3138     $ cd ..
3139
3140   To further simplify the process, you can use the ‘--setdirconf’
3141option.  If you are already in your desired working directory, calling
3142this option with the others will automatically write the final values
3143(along with descriptions) in ‘.gnuastro/astcosmiccal.conf’.  For example
3144try the commands below:
3145
3146     $ mkdir my-cosmology2
3147     $ cd my-cosmology2
3148     $ astcosmiccal -P
3149     $ astcosmiccal --H0 70 --olambda=0.7 --omatter=0.3 --setdirconf
3150     $ astcosmiccal -P
3151     $ cd ..
3152
3153   Gnuastro’s programs also have default configuration files for a
3154specific user (when run in any directory).  This allows you to set a
3155special behavior every time a program is run by a specific user.  Only
3156the directory and filename differ from the above, the rest of the
3157process is similar to before.  Finally, there are also system-wide
3158configuration files that can be used to define the option values for all
3159users on a system.  See *note Configuration file precedence:: for a more
3160detailed discussion.
3161
3162   We’ll stop the discussion on configuration files here, but you can
3163always read about them in *note Configuration files::.  Before
3164continuing the tutorial, let’s delete the two extra directories that we
3165don’t need any more:
3166
3167     $ rm -rf my-cosmology*
3168
3169
3170File: gnuastro.info,  Node: Warping to a new pixel grid,  Next: NoiseChisel and Multiextension FITS files,  Prev: Option management and configuration files,  Up: General program usage tutorial
3171
31722.2.9 Warping to a new pixel grid
3173---------------------------------
3174
3175We are now ready to start processing the downloaded images.  The XDF
3176datasets we are using here are already aligned to the same pixel grid.
3177However, warping to a different/matched pixel grid is commonly needed
3178before higher-level analysis when you are using datasets from different
3179instruments.  So let’s have a look at Gnuastro’s features warping
3180features here.
3181
3182   Gnuastro’s Warp program should be used for warping the pixel-grid
3183(see *note Warp::).  For example, try rotating one of the images by 20
3184degrees:
3185
3186     $ astwarp flat-ir/xdf-f160w.fits --rotate=20
3187
3188Open the output (‘xdf-f160w_rotated.fits’) and see how it is rotated.
3189If your final image is already aligned with RA and Dec, you can simply
3190use the ‘--align’ option and let Warp calculate the necessary rotation
3191and apply it.  For example, try aligning the rotated image back to the
3192standard orientation (just note that because of the two rotations, the
3193NaN parts of the image are larger now):
3194
3195     $ astwarp xdf-f160w_rotated.fits --align
3196
3197   Warp can generally be used for many kinds of pixel grid manipulation
3198(warping), not just rotations.  For example the outputs of the commands
3199below will respectively have larger pixels (new resolution being one
3200quarter the original resolution), get shifted by 2.8 (by sub-pixel), get
3201a shear of 2, and be tilted (projected).  Run each of them and open the
3202output file to see the effect, they will become handy for you in the
3203future.
3204
3205     $ astwarp flat-ir/xdf-f160w.fits --scale=0.25
3206     $ astwarp flat-ir/xdf-f160w.fits --translate=2.8
3207     $ astwarp flat-ir/xdf-f160w.fits --shear=0.2
3208     $ astwarp flat-ir/xdf-f160w.fits --project=0.001,0.0005
3209
3210If you need to do multiple warps, you can combine them in one call to
3211Warp.  For example to first rotate the image, then scale it, run this
3212command:
3213
3214     $ astwarp flat-ir/xdf-f160w.fits --rotate=20 --scale=0.25
3215
3216   If you have multiple warps, do them all in one command.  Don’t warp
3217them in separate commands because the correlated noise will become too
3218strong.  As you see in the matrix that is printed when you run Warp, it
3219merges all the warps into a single warping matrix (see *note Merging
3220multiple warpings::) and simply applies that (mixes the pixel values)
3221just once.  However, if you run Warp multiple times, the pixels will be
3222mixed multiple times, creating a strong artificial blur/smoothing, or
3223stronger correlated noise.
3224
3225   Recall that the merging of multiple warps is done through matrix
3226multiplication, therefore order matters in the separate operations.  At
3227a lower level, through Warp’s ‘--matrix’ option, you can directly
3228request your desired final warp and don’t have to break it up into
3229different warps like above (see *note Invoking astwarp::).
3230
3231   Fortunately these datasets are already aligned to the same pixel
3232grid, so you don’t actually need the files that were just generated.You
3233can safely delete them all with the following command.  Here, you see
3234why we put the processed outputs that we need later into a separate
3235directory.  In this way, the top directory can be used for temporary
3236files for testing that you can simply delete with a generic command like
3237below.
3238
3239     $ rm *.fits
3240
3241
3242File: gnuastro.info,  Node: NoiseChisel and Multiextension FITS files,  Next: NoiseChisel optimization for detection,  Prev: Warping to a new pixel grid,  Up: General program usage tutorial
3243
32442.2.10 NoiseChisel and Multiextension FITS files
3245------------------------------------------------
3246
3247Having completed a review of the basics in the previous sections, we are
3248now ready to separate the signal (galaxies or stars) from the background
3249noise in the image.  We will be using the results of *note Dataset
3250inspection and cropping::, so be sure you already have them.  Gnuastro
3251has NoiseChisel for this job.  But NoiseChisel’s output is a
3252multi-extension FITS file, therefore to better understand how to use
3253NoiseChisel, let’s take a look at multi-extension FITS files and how you
3254can interact with them.
3255
3256   In the FITS format, each extension contains a separate dataset (image
3257in this case).  You can get basic information about the extensions in a
3258FITS file with Gnuastro’s Fits program (see *note Fits::).  To start
3259with, let’s run NoiseChisel without any options, then use Gnuastro’s
3260FITS program to inspect the number of extensions in this file.
3261
3262     $ astnoisechisel flat-ir/xdf-f160w.fits
3263     $ astfits xdf-f160w_detected.fits
3264
3265   From the output list, we see that NoiseChisel’s output contains 5
3266extensions and the first (counting from zero, with name
3267‘NOISECHISEL-CONFIG’) is empty: it has value of ‘0’ in the last column
3268(which shows its size).  The first extension in all the outputs of
3269Gnuastro’s programs only contains meta-data: data about/describing the
3270datasets within (all) the output’s extensions.  This is recommended by
3271the FITS standard, see *note Fits:: for more.  In the case of Gnuastro’s
3272programs, this generic zero-th/meta-data extension (for the whole file)
3273contains all the configuration options of the program that created the
3274file.
3275
3276   The second extension of NoiseChisel’s output (numbered 1, named
3277‘INPUT-NO-SKY’) is the Sky-subtracted input that you provided.  The
3278third (‘DETECTIONS’) is NoiseChisel’s main output which is a binary
3279image with only two possible values for all pixels: 0 for noise and 1
3280for signal.  Since it only has two values, to avoid taking too much
3281space on your computer, its numeric datatype an unsigned 8-bit integer
3282(or ‘uint8’)(1).  The fourth and fifth (‘SKY’ and ‘SKY_STD’) extensions,
3283have the Sky and its standard deviation values for the input on a tile
3284grid and were calculated over the undetected regions (for more on the
3285importance of the Sky value, see *note Sky value::).
3286
3287   Metadata regarding how the analysis was done (or a dataset was
3288created) is very important for higher-level analysis and
3289reproducibility.  Therefore, Let’s first take a closer look at the
3290‘NOISECHISEL-CONFIG’ extension.  If you specify a special header in the
3291FITS file, Gnuastro’s Fits program will print the header keywords
3292(metadata) of that extension.  You can either specify the HDU/extension
3293counter (starting from 0), or name.  Therefore, the two commands below
3294are identical for this file:
3295
3296     $ astfits xdf-f160w_detected.fits -h0
3297     $ astfits xdf-f160w_detected.fits -hNOISECHISEL-CONFIG
3298
3299   The first group of FITS header keywords are standard keywords
3300(containing the ‘SIMPLE’ and ‘BITPIX’ keywords the first empty line).
3301They are required by the FITS standard and must be present in any FITS
3302extension.  The second group contains the input file and all the options
3303with their values in that run of NoiseChisel.  Finally, the last group
3304contains the date and version information of Gnuastro and its
3305dependencies.  The “versions and date” group of keywords are present in
3306all Gnuastro’s FITS extension outputs, for more see *note Output FITS
3307files::.
3308
3309   Note that if a keyword name is larger than 8 characters, it is
3310preceded by a ‘HIERARCH’ keyword and that all keyword names are in
3311capital letters.  Therefore, if you want to see only one keyword’s value
3312by feeding the output to Grep, you should ask Grep to ignore case with
3313its ‘-i’ option (short name for ‘--ignore-case’).  For example, below
3314we’ll check the value to the ‘--snminarea’ option, note how we don’t
3315need Grep’s ‘-i’ option when it is fed with ‘astnoisechisel -P’ since it
3316is already in small-caps there.  The extra white spaces in the first
3317command are only to help in readability, you can ignore them when
3318typing.
3319
3320     $ astnoisechisel -P                   | grep    snminarea
3321     $ astfits xdf-f160w_detected.fits -h0 | grep -i snminarea
3322
3323The metadata (that is stored in the output) can later be used to exactly
3324reproduce/understand your result, even if you have lost/forgot the
3325command you used to create the file.  This feature is present in all of
3326Gnuastro’s programs, not just NoiseChisel.
3327
3328   Let’s continue with the extensions in NoiseChisel’s output that
3329contain a dataset by visually inspecting them (here, we’ll use SAO DS9).
3330Since the file contains multiple related extensions, the easiest way to
3331view all of them in DS9 is to open the file as a “Multi-extension data
3332cube” with the ‘-mecube’ option as shown below(2).
3333
3334     $ ds9 -mecube xdf-f160w_detected.fits -zscale -zoom to fit
3335
3336   A “cube” window opens along with DS9’s main window.  The buttons and
3337horizontal scroll bar in this small new window can be used to navigate
3338between the extensions.  In this mode, all DS9’s settings (for example
3339zoom or color-bar) will be identical between the extensions.  Try
3340zooming into to one part and flipping through the extensions to see how
3341the galaxies were detected along with the Sky and Sky standard deviation
3342values for that region.  Just have in mind that NoiseChisel’s job is
3343_only_ detection (separating signal from noise), We’ll do segmentation
3344on this result later to find the individual galaxies/peaks over the
3345detected pixels.
3346
3347   Each HDU/extension in a FITS file is an independent dataset (image or
3348table) which you can delete from the FITS file, or copy/cut to another
3349file.  For example, with the command below, you can copy NoiseChisel’s
3350‘DETECTIONS’ HDU/extension to another file:
3351
3352     $ astfits xdf-f160w_detected.fits --copy=DETECTIONS -odetections.fits
3353
3354   There are similar options to conveniently cut (‘--cut’, copy, then
3355remove from the input) or delete (‘--remove’) HDUs from a FITS file
3356also.  See *note HDU information and manipulation:: for more.
3357
3358   ---------- Footnotes ----------
3359
3360   (1) To learn more about numeric data types see *note Numeric data
3361types::.
3362
3363   (2) You can configure your graphic user interface to open DS9 in
3364multi-extension cube mode by default when using the GUI (double clicking
3365on the file).  If your graphic user interface is GNOME (another GNU
3366software, it is most common in GNU/Linux operating systems), a full
3367description is given in *note Viewing multiextension FITS images::
3368
3369
3370File: gnuastro.info,  Node: NoiseChisel optimization for detection,  Next: NoiseChisel optimization for storage,  Prev: NoiseChisel and Multiextension FITS files,  Up: General program usage tutorial
3371
33722.2.11 NoiseChisel optimization for detection
3373---------------------------------------------
3374
3375In *note NoiseChisel and Multiextension FITS files::, we ran NoiseChisel
3376and reviewed NoiseChisel’s output format.  Now that you have a better
3377feeling for multi-extension FITS files, let’s optimize NoiseChisel for
3378this particular dataset.
3379
3380   One good way to see if you have missed any signal (small galaxies, or
3381the wings of brighter galaxies) is to mask all the detected pixels and
3382inspect the noise pixels.  For this, you can use Gnuastro’s Arithmetic
3383program (in particular its ‘where’ operator, see *note Arithmetic
3384operators::).  The command below will produce ‘mask-det.fits’.  In it,
3385all the pixels in the ‘INPUT-NO-SKY’ extension that are flagged 1 in the
3386‘DETECTIONS’ extension (dominated by signal, not noise) will be set to
3387NaN.
3388
3389   Since the various extensions are in the same file, for each dataset
3390we need the file and extension name.  To make the command easier to
3391read/write/understand, let’s use shell variables: ‘‘in’’ will be used
3392for the Sky-subtracted input image and ‘‘det’’ will be used for the
3393detection map.  Recall that a shell variable’s value can be retrieved by
3394adding a ‘$’ before its name, also note that the double quotations are
3395necessary when we have white-space characters in a variable name (like
3396this case).
3397
3398     $ in="xdf-f160w_detected.fits -hINPUT-NO-SKY"
3399     $ det="xdf-f160w_detected.fits -hDETECTIONS"
3400     $ astarithmetic $in $det nan where --output=mask-det.fits
3401
3402To invert the result (only keep the detected pixels), you can flip the
3403detection map (from 0 to 1 and vice-versa) by adding a ‘‘not’’ after the
3404second ‘$det’:
3405
3406     $ astarithmetic $in $det not nan where --output=mask-sky.fits
3407
3408   Look again at the ‘DETECTIONS’ extension, in particular the long
3409worm-like structure around (1) pixel 1650 (X) and 1470 (Y). These types
3410of long wiggly structures show that we have dug too deep into the noise,
3411and are a signature of correlated noise.  Correlated noise is created
3412when we warp (for example rotate) individual exposures (that are each
3413slightly offset compared to each other) into the same pixel grid before
3414adding them into one deeper image.  During the warping, nearby pixels
3415are mixed and the effect of this mixing on the noise (which is in every
3416pixel) is called “correlated noise”.  Correlated noise is a form of
3417convolution and it slightly smooths the image.
3418
3419   In terms of the number of exposures (and thus correlated noise), the
3420XDF dataset is by no means an ordinary dataset.  Therefore the default
3421parameters need to be slightly customized.  It is the result of warping
3422and adding roughly 80 separate exposures which can create strong
3423correlated noise/smoothing.  In common surveys the number of exposures
3424is usually 10 or less.  See Figure 2 of Akhlaghi [2019]
3425(https://arxiv.org/abs/1909.11230) and the discussion on
3426‘--detgrowquant’ there for more on how NoiseChisel “grow”s the detected
3427objects and the patterns caused by correlated noise.
3428
3429   Let’s tweak NoiseChisel’s configuration a little to get a better
3430result on this dataset.  Don’t forget that “_Good statistical analysis
3431is not a purely routine matter, and generally calls for more than one
3432pass through the computer_” (Anscombe 1973, see *note Science and its
3433tools::).  A good scientist must have a good understanding of her tools
3434to make a meaningful analysis.  So don’t hesitate in playing with the
3435default configuration and reviewing the manual when you have a new
3436dataset (from a new instrument) in front of you.  Robust data analysis
3437is an art, therefore a good scientist must first be a good artist.  Once
3438you have found the good configuration for that particular noise pattern
3439(instrument) you can safely use it for all new data that have a similar
3440noise pattern.
3441
3442   NoiseChisel can produce “Check images” to help you visualize and
3443inspect how each step is done.  You can see all the check images it can
3444produce with this command.
3445
3446     $ astnoisechisel --help | grep check
3447
3448   Let’s check the overall detection process to get a better feeling of
3449what NoiseChisel is doing with the following command.  To learn the
3450details of NoiseChisel in more detail, please see *note NoiseChisel::,
3451Akhlaghi and Ichikawa [2015] (https://arxiv.org/abs/1505.01664) and
3452Akhlaghi [2019] (https://arxiv.org/abs/1909.11230).
3453
3454     $ astnoisechisel flat-ir/xdf-f160w.fits --checkdetection
3455
3456   The check images/tables are also multi-extension FITS files.  As you
3457saw from the command above, when check datasets are requested,
3458NoiseChisel won’t go to the end.  It will abort as soon as all the
3459extensions of the check image are ready.  Please list the extensions of
3460the output with ‘astfits’ and then opening it with ‘ds9’ as we done
3461above.  If you have read the paper, you will see why there are so many
3462extensions in the check image.
3463
3464     $ astfits xdf-f160w_detcheck.fits
3465     $ ds9 -mecube xdf-f160w_detcheck.fits -zscale -zoom to fit
3466
3467   In order to understand the parameters and their biases (especially as
3468you are starting to use Gnuastro, or running it a new dataset), it is
3469_strongly_ encouraged to play with the different parameters and use the
3470respective check images to see which step is affected by your changes
3471and how, for example see *note Detecting large extended targets::.
3472
3473   Let’s focus on one step: the ‘OPENED_AND_LABELED’ extension shows the
3474initial detection step of NoiseChisel.  We see the seeds of that
3475correlated noise structure with many small detections (a relatively
3476early stage in the processing).  Such connections at the lowest surface
3477brightness limits usually occur when the dataset is too smoothed, the
3478threshold is too low, or the final “growth” is too much.
3479
3480   As you see from the 2nd (‘CONVOLVED’) extension, the first operation
3481that NoiseChisel does on the data is to slightly smooth it.  However,
3482the natural correlated noise of this dataset is already one level of
3483artificial smoothing, so further smoothing it with the default kernel
3484may be the culprit.  To see the effect, let’s use a sharper kernel as a
3485first step to convolve/smooth the input.
3486
3487   By default NoiseChisel uses a Gaussian with full-width-half-maximum
3488(FWHM) of 2 pixels.  We can use Gnuastro’s MakeProfiles to build a
3489kernel with FWHM of 1.5 pixel (truncated at 5 times the FWHM, like the
3490default) using the following command.  MakeProfiles is a powerful tool
3491to build any number of mock profiles on one image or independently, to
3492learn more of its features and capabilities, see *note MakeProfiles::.
3493
3494     $ astmkprof --kernel=gaussian,1.5,5 --oversample=1
3495
3496Please open the output ‘kernel.fits’ and have a look (it is very small
3497and sharp).  We can now tell NoiseChisel to use this instead of the
3498default kernel with the following command (we’ll keep the
3499‘--checkdetection’ to continue checking the detection steps)
3500
3501     $ astnoisechisel flat-ir/xdf-f160w.fits --kernel=kernel.fits  \
3502                      --checkdetection
3503
3504   Open the output ‘xdf-f160w_detcheck.fits’ as a multi-extension FITS
3505file and go to the last extension (‘DETECTIONS-FINAL’, it is the same
3506pixels as the final NoiseChisel output without ‘--checkdetections’).
3507Look again at that position mentioned above (1650,1470), you see that
3508the long wiggly structure is gone.  This shows we are making progress
3509:-).
3510
3511   Looking at the new ‘OPENED_AND_LABELED’ extension, we see that the
3512thin connections between smaller peaks has now significantly decreased.
3513Going two extensions/steps ahead (in the first ‘HOLES-FILLED’), you can
3514see that during the process of finding false pseudo-detections, too many
3515holes have been filled: do you see how the many of the brighter galaxies
3516are connected?  At this stage all holes are filled, irrespective of
3517their size.
3518
3519   Try looking two extensions ahead (in the first ‘PSEUDOS-FOR-SN’), you
3520can see that there aren’t too many pseudo-detections because of all
3521those extended filled holes.  If you look closely, you can see the
3522number of pseudo-detections in the printed outputs of NoiseChisel
3523(around 6400).  This is another side-effect of correlated noise.  To
3524address it, we should slightly increase the pseudo-detection threshold
3525(before changing ‘--dthresh’, run with ‘-P’ to see the default value):
3526
3527     $ astnoisechisel flat-ir/xdf-f160w.fits --kernel=kernel.fits \
3528                      --dthresh=0.1 --checkdetection
3529
3530   Before visually inspecting the check image, you can already see the
3531effect of this small change in NoiseChisel’s command-line output: notice
3532how the number of pseudo-detections has increased to more than 7100!
3533Open the check image now and have a look, you can see how the
3534pseudo-detections are distributed much more evenly in the blank sky
3535regions of the ‘PSEUDOS-FOR-SN’ extension.
3536
3537*Maximize the number of pseudo-detections:* When using NoiseChisel on
3538datasets with a new noise-pattern (for example going to a Radio
3539astronomy image, or a shallow ground-based image), play with ‘--dthresh’
3540until you get a maximal number of pseudo-detections: the total number of
3541pseudo-detections is printed on the command-line when you run
3542NoiseChisel, you don’t even need to open a FITS viewer.
3543
3544   In this particular case, try ‘--dthresh=0.2’ and you will see that
3545the total printed number decreases to around 6700 (recall that with
3546‘--dthresh=0.1’, it was roughly 7100).  So for this type of very deep
3547HST images, we should set ‘--dthresh=0.1’.
3548
3549   As discussed in Section 3.1.5 of Akhlaghi and Ichikawa [2015]
3550(https://arxiv.org/abs/1505.01664), the signal-to-noise ratio of
3551pseudo-detections are critical to identifying/removing false detections.
3552For an optimal detection they are very important to get right (where you
3553want to detect the faintest and smallest objects in the image
3554successfully).  Let’s have a look at their signal-to-noise distribution
3555with ‘--checksn’.
3556
3557     $ astnoisechisel flat-ir/xdf-f160w.fits --kernel=kernel.fits  \
3558                      --dthresh=0.1 --checkdetection --checksn
3559
3560   The output (‘xdf-f160w_detsn.fits’) contains two extensions for the
3561pseudo-detections containing two-column tables over the undetected
3562(‘SKY_PSEUDODET_SN’) regions and those over detections
3563(‘DET_PSEUDODET_SN’).  With the first command below you can see the HDUs
3564of this file, and with the second you can see the information of the
3565table in the first HDU (which is the default when you don’t use
3566‘--hdu’):
3567
3568     $ astfits xdf-f160w_detsn.fits
3569     $ asttable xdf-f160w_detsn.fits -i
3570
3571You can see the table columns with the first command below and get a
3572feeling of the signal-to-noise value distribution with the second
3573command (the two Table and Statistics programs will be discussed later
3574in the tutorial):
3575
3576     $ asttable xdf-f160w_detsn.fits -hSKY_PSEUDODET_SN
3577     $ aststatistics xdf-f160w_detsn.fits -hSKY_PSEUDODET_SN -c2
3578     ... [output truncated] ...
3579     Histogram:
3580      |           *
3581      |          ***
3582      |         ******
3583      |        *********
3584      |        **********
3585      |       *************
3586      |      *****************
3587      |     ********************
3588      |    **************************
3589      |   ********************************
3590      |*******************************************************   * **       *
3591      |----------------------------------------------------------------------
3592
3593   The correlated noise is again visible in the signal-to-noise
3594distribution of sky pseudo-detections!  Do you see how skewed this
3595distribution is?  In an image with less correlated noise, this
3596distribution would be much more symmetric.  A small change in the
3597quantile will translate into a big change in the S/N value.  For example
3598see the difference between the three 0.99, 0.95 and 0.90 quantiles with
3599this command:
3600
3601     $ aststatistics xdf-f160w_detsn.fits -hSKY_PSEUDODET_SN -c2      \
3602                     --quantile=0.99 --quantile=0.95 --quantile=0.90
3603
3604   We get a change of almost 2 units (which is very significant).  If
3605you run NoiseChisel with ‘-P’, you’ll see the default signal-to-noise
3606quantile ‘--snquant’ is 0.99.  In effect with this option you specify
3607the purity level you want (contamination by false detections).  With the
3608‘aststatistics’ command above, you see that a small number of extra
3609false detections (impurity) in the final result causes a big change in
3610completeness (you can detect more lower signal-to-noise true
3611detections).  So let’s loosen-up our desired purity level, remove the
3612check-image options, and then mask the detected pixels like before to
3613see if we have missed anything.
3614
3615     $ astnoisechisel flat-ir/xdf-f160w.fits --kernel=kernel.fits  \
3616                      --dthresh=0.1 --snquant=0.95
3617     $ in="xdf-f160w_detected.fits -hINPUT-NO-SKY"
3618     $ det="xdf-f160w_detected.fits -hDETECTIONS"
3619     $ astarithmetic $in $det nan where --output=mask-det.fits
3620
3621   Overall it seems good, but if you play a little with the color-bar
3622and look closer in the noise, you’ll see a few very sharp, but faint,
3623objects that have not been detected.  For example the object around
3624pixel (456, 1662).  Despite its high valued pixels, this object was lost
3625because erosion ignores the precise pixel values.  Loosing small/sharp
3626objects like this only happens for under-sampled datasets like HST
3627(where the pixel size is larger than the point spread function FWHM). So
3628this won’t happen on ground-based images.
3629
3630   To address this problem of sharp objects, we can use NoiseChisel’s
3631‘--noerodequant’ option.  All pixels above this quantile will not be
3632eroded, thus allowing us to preserve small/sharp objects (that cover a
3633small area, but have a lot of signal in it).  Check its default value,
3634then run NoiseChisel like below and make the mask again.
3635
3636     $ astnoisechisel flat-ir/xdf-f160w.fits --kernel=kernel.fits     \
3637                      --noerodequant=0.95 --dthresh=0.1 --snquant=0.95
3638
3639   This seems to be fine and the object above is now detected.  We’ll
3640stop the configuration here, but please feel free to keep looking into
3641the data to see if you can improve it even more.
3642
3643   Once you have found the proper customization for the type of images
3644you will be using you don’t need to change them any more.  The same
3645configuration can be used for any dataset that has been similarly
3646produced (and has a similar noise pattern).  But entering all these
3647options on every call to NoiseChisel is annoying and prone to bugs
3648(mistakenly typing the wrong value for example).  To simply things,
3649we’ll make a configuration file in a visible ‘config’ directory.  Then
3650we’ll define the hidden ‘.gnuastro’ directory (that all Gnuastro’s
3651programs will look into for configuration files) as a symbolic link to
3652the ‘config’ directory.  Finally, we’ll write the finalized values of
3653the options into NoiseChisel’s standard configuration file within that
3654directory.  We’ll also put the kernel in a separate directory to keep
3655the top directory clean of any files we later need.
3656
3657     $ mkdir kernel config
3658     $ ln -s config/ .gnuastro
3659     $ mv kernel.fits kernel/noisechisel.fits
3660     $ echo "kernel kernel/noisechisel.fits" > config/astnoisechisel.conf
3661     $ echo "noerodequant 0.95"             >> config/astnoisechisel.conf
3662     $ echo "dthresh      0.1"              >> config/astnoisechisel.conf
3663     $ echo "snquant      0.95"             >> config/astnoisechisel.conf
3664
3665We are now ready to finally run NoiseChisel on the three filters and
3666keep the output in a dedicated directory (which we’ll call ‘nc’ for
3667simplicity).
3668     $ rm *.fits
3669     $ mkdir nc
3670     $ for f in f105w f125w f160w; do \
3671         astnoisechisel flat-ir/xdf-$f.fits --output=nc/xdf-$f.fits; \
3672       done
3673
3674   ---------- Footnotes ----------
3675
3676   (1) To find a particular coordiante easily in DS9, you can do this:
3677Click on the “Edit” menu, and select “Region”.  Then click on any random
3678part of the image to see a circle show up in that location (this is the
3679“region”).  Double-click on the region and a “Circle” window will open.
3680If you have celestial coordinates, keep the default “fk5” in the
3681scroll-down menu after the “Center”.  But if you have pixel/image
3682coordinates, click on the “fk5” and select “Image”.  Now you can set the
3683“Center” coordinates of the region (‘1650’ and ‘1470’ in this case) by
3684manually typing them in the two boxes in front of “Center”.  Finally,
3685when everything is ready, click on the “Apply” button and your region
3686will go over your requested coordinates.  You can zoom out (to see the
3687whole image) and visually find it.
3688
3689
3690File: gnuastro.info,  Node: NoiseChisel optimization for storage,  Next: Segmentation and making a catalog,  Prev: NoiseChisel optimization for detection,  Up: General program usage tutorial
3691
36922.2.12 NoiseChisel optimization for storage
3693-------------------------------------------
3694
3695As we showed before (in *note NoiseChisel and Multiextension FITS
3696files::), NoiseChisel’s output is a multi-extension FITS file with
3697several images the same size as the input.  As the input datasets get
3698larger this output can become hard to manage and waste a lot of storage
3699space.  Fortunately there is a solution to this problem (which is also
3700useful for Segment’s outputs).
3701
3702   In this small section we’ll take a short detour to show this feature.
3703Please note that the outputs generated here are not needed for the rest
3704of the tutorial.  But first, let’s have a look at the contents/HDUs and
3705volume of NoiseChisel’s output from *note NoiseChisel optimization for
3706detection:: (fast answer, its larger than 100 mega-bytes):
3707
3708     $ astfits nc/xdf-f160w.fits
3709     $ ls -lh nc/xdf-f160w.fits
3710
3711   Two options can drastically decrease NoiseChisel’s output file size:
37121) With the ‘--rawoutput’ option, NoiseChisel won’t create a
3713Sky-subtracted input.  After all, it is redundant: you can always
3714generate it by subtracting the ‘SKY’ extension from the input image
3715(which you have in your database) using the Arithmetic program.  2) With
3716the ‘--oneelempertile’, you can tell NoiseChisel to store its Sky and
3717Sky standard deviation results with one pixel per tile (instead of many
3718pixels per tile).  So let’s run NoiseChisel with these options, then
3719have another look at the HDUs and the over-all file size:
3720
3721     $ astnoisechisel flat-ir/xdf-f160w.fits --oneelempertile --rawoutput \
3722                      --output=nc-for-storage.fits
3723     $ astfits nc-for-storage.fits
3724     $ ls -lh nc-for-storage.fits
3725
3726See how ‘nc-for-storage.fits’ has four HDUs, while ‘nc/xdf-f160w.fits3727had five HDUs?  As explained above, the missing extension is
3728‘INPUT-NO-SKY’.  Also, look at the sizes of the ‘SKY’ and ‘SKY_STD’
3729HDUs, unlike before, they aren’t the same size as ‘DETECTIONS’, they
3730only have one pixel for each tile (group of pixels in raw input).
3731Finally, you see that ‘nc-for-storage.fits’ is just under 8 mega byes
3732(while ‘nc/xdf-f160w.fits’ was 100 mega bytes)!
3733
3734   But were are not finished!  You can even be more efficient in
3735storage, archival or transferring NoiseChisel’s output by compressing
3736this file.  Try the command below to see how NoiseChisel’s output has
3737now shrunk to about 250 kilo-byes while keeping all the necessary
3738information as the original 100 mega-byte output.
3739
3740     $ gzip --best nc-for-storage.fits
3741     $ ls -lh nc-for-storage.fits.gz
3742
3743   We can get this wonderful level of compression because NoiseChisel’s
3744output is binary with only two values: 0 and 1.  Compression algorithms
3745are highly optimized in such scenarios.
3746
3747   You can open ‘nc-for-storage.fits.gz’ directly in SAO DS9 or feed it
3748to any of Gnuastro’s programs without having to decompress it.
3749Higher-level programs that take NoiseChisel’s output (for example
3750Segment or MakeCatalog) can also deal with this compressed image where
3751the Sky and its Standard deviation are one pixel-per-tile.  You just
3752have to give the “values” image as a separate option, for more, see
3753*note Segment:: and *note MakeCatalog::.
3754
3755   Segment (the program we will introduce in the next section for
3756identifying sub-structure), also has similar features to optimize its
3757output for storage.  Since this file was only created for a fast detour
3758demonstration, let’s keep our top directory clean and move to the next
3759step:
3760
3761     rm nc-for-storage.fits.gz
3762
3763
3764File: gnuastro.info,  Node: Segmentation and making a catalog,  Next: Working with catalogs estimating colors,  Prev: NoiseChisel optimization for storage,  Up: General program usage tutorial
3765
37662.2.13 Segmentation and making a catalog
3767----------------------------------------
3768
3769The main output of NoiseChisel is the binary detection map (‘DETECTIONS’
3770extension, see *note NoiseChisel optimization for detection::).  which
3771only has two values of 1 or 0.  This is useful when studying the noise
3772or background properties, but hardly of any use when you actually want
3773to study the targets/galaxies in the image, especially in such a deep
3774field where almost everything is connected.  To find the galaxies over
3775the detections, we’ll use Gnuastro’s *note Segment:: program:
3776
3777     $ mkdir seg
3778     $ astsegment nc/xdf-f160w.fits -oseg/xdf-f160w.fits
3779     $ astsegment nc/xdf-f125w.fits -oseg/xdf-f125w.fits
3780     $ astsegment nc/xdf-f105w.fits -oseg/xdf-f105w.fits
3781
3782   Segment’s operation is very much like NoiseChisel (in fact, prior to
3783version 0.6, it was part of NoiseChisel).  For example the output is a
3784multi-extension FITS file, it has check images and uses the undetected
3785regions as a reference.  Please have a look at Segment’s multi-extension
3786output with ‘ds9’ to get a good feeling of what it has done.
3787
3788     $ ds9 -mecube seg/xdf-f160w.fits -zscale -zoom to fit
3789
3790   Like NoiseChisel, the first extension is the input.  The ‘CLUMPS’
3791extension shows the true “clumps” with values that are $\ge1$, and the
3792diffuse regions labeled as $-1$.  Please flip between the first
3793extension and the clumps extension and zoom-in on some of the clumps to
3794get a feeling of what they are.  In the ‘OBJECTS’ extension, we see that
3795the large detections of NoiseChisel (that may have contained many
3796galaxies) are now broken up into separate labels.  Play with the
3797color-bar and hover your mouse of the various detections to see their
3798different labels.
3799
3800   The clumps are not affected by the hard-to-deblend and low
3801signal-to-noise diffuse regions, they are more robust for calculating
3802the colors (compared to objects).  From this step onward, we’ll continue
3803with clumps.
3804
3805   Having localized the regions of interest in the dataset, we are ready
3806to do measurements on them with *note MakeCatalog::.  MakeCatalog is
3807specialized and optimized for doing measurements over labeled regions of
3808an image.  In other words, through MakeCatalog, you can “reduce” an
3809image to a table (catalog of certain properties of objects in the
3810image).  Each requested measurement (over each label) will be given a
3811column in the output table.  To see the full set of available
3812measurements run it with ‘--help’ like below (and scroll up), note that
3813measurements are classified by context.
3814
3815     $ astmkcatalog --help
3816
3817   So let’s select the properties we want to measure in this tutorial.
3818First of all, we need to know which measurement belongs to which object
3819or clump, so we’ll start with the ‘--ids’ (read as: IDs(1)).  We also
3820want to measure (in this order) the Right Ascension (with ‘--ra’),
3821Declination (‘--dec’), magnitude (‘--magnitude’), and signal-to-noise
3822ratio (‘--sn’) of the objects and clumps.  Furthermore, as mentioned
3823above, we also want measurements on clumps, so we also need to call
3824‘--clumpscat’.  The following command will make these measurements on
3825Segment’s F160W output and write them in a catalog for each object and
3826clump in a FITS table.
3827
3828     $ mkdir cat
3829     $ astmkcatalog seg/xdf-f160w.fits --ids --ra --dec --magnitude --sn \
3830                    --zeropoint=25.94 --clumpscat --output=cat/xdf-f160w.fits
3831
3832From the printed statements on the command-line, you see that
3833MakeCatalog read all the extensions in Segment’s output for the various
3834measurements it needed.  To calculate colors, we also need magnitude
3835measurements on the other filters.  So let’s repeat the command above on
3836them, just changing the file names and zeropoint (which we got from the
3837XDF survey web page):
3838
3839     $ astmkcatalog seg/xdf-f125w.fits --ids --ra --dec --magnitude --sn \
3840                    --zeropoint=26.23 --clumpscat --output=cat/xdf-f125w.fits
3841
3842     $ astmkcatalog seg/xdf-f105w.fits --ids --ra --dec --magnitude --sn \
3843                    --zeropoint=26.27 --clumpscat --output=cat/xdf-f105w.fits
3844
3845   However, the galaxy properties might differ between the filters
3846(which is the whole purpose behind observing in different filters!).
3847Also, the noise properties and depth of the datasets differ.  You can
3848see the effect of these factors in the resulting clump catalogs, with
3849Gnuastro’s Table program.  We’ll go deep into working with tables in the
3850next section, but in summary: the ‘-i’ option will print information
3851about the columns and number of rows.  To see the column values, just
3852remove the ‘-i’ option.  In the output of each command below, look at
3853the ‘Number of rows:’, and note that they are different.
3854
3855     $ asttable cat/xdf-f105w.fits -hCLUMPS -i
3856     $ asttable cat/xdf-f125w.fits -hCLUMPS -i
3857     $ asttable cat/xdf-f160w.fits -hCLUMPS -i
3858
3859   Matching the catalogs is possible (for example with *note Match::).
3860However, the measurements of each column are also done on different
3861pixels: the clump labels can/will differ from one filter to another for
3862one object.  Please open them and focus on one object to see for your
3863self.  This can bias the result, if you match catalogs.
3864
3865   An accurate color calculation can only be done when magnitudes are
3866measured from the same pixels on all images and this can be done easily
3867with MakeCatalog.  In fact this is one of the reasons that NoiseChisel
3868or Segment don’t generate a catalog like most other
3869detection/segmentation software.  This gives you the freedom of
3870selecting the pixels for measurement in any way you like (from other
3871filters, other software, manually, and etc).  Fortunately in these
3872images, the Point spread function (PSF) is very similar, allowing us to
3873use a single labeled image output for all filters(2).
3874
3875   The F160W image is deeper, thus providing better
3876detection/segmentation, and redder, thus observing smaller/older stars
3877and representing more of the mass in the galaxies.  We will thus use the
3878F160W filter as a reference and use its segment labels to identify which
3879pixels to use for which objects/clumps.  But we will do the measurements
3880on the sky-subtracted F105W and F125W images (using MakeCatalog’s
3881‘--valuesfile’ option) as shown below: Notice that the only difference
3882between these calls and the call to generate the raw F160W catalog
3883(excluding the zero point and the output name) is the ‘--valuesfile’.
3884
3885     $ astmkcatalog seg/xdf-f160w.fits --ids --ra --dec --magnitude --sn \
3886                    --valuesfile=nc/xdf-f125w.fits --zeropoint=26.23 \
3887                    --clumpscat --output=cat/xdf-f125w-on-f160w-lab.fits
3888
3889     $ astmkcatalog seg/xdf-f160w.fits --ids --ra --dec --magnitude --sn \
3890                    --valuesfile=nc/xdf-f105w.fits --zeropoint=26.27 \
3891                    --clumpscat --output=cat/xdf-f105w-on-f160w-lab.fits
3892
3893   After running the commands above, look into what MakeCatalog printed
3894on the command-line.  You can see that (as requested) the object and
3895clump pixel labels in both were taken from the respective extensions in
3896seg/xdf-f160w.fits’.  However, the pixel values and pixel Sky standard
3897deviation were respectively taken from ‘nc/xdf-f105w.fits’ and
3898nc/xdf-f125w.fits’.  Since we used the same labeled image on all
3899filters, the number of rows in both catalogs are now identical.  Let’s
3900have a look:
3901
3902     $ asttable cat/xdf-f105w-on-f160w-lab.fits -hCLUMPS -i
3903     $ asttable cat/xdf-f125w-on-f160w-lab.fits -hCLUMPS -i
3904     $ asttable cat/xdf-f160w.fits -hCLUMPS -i
3905
3906   Finally, MakeCatalog also does basic calculations on the full dataset
3907(independent of each labeled region but related to whole data), for
3908example pixel area or per-pixel surface brightness limit.  They are
3909stored as keywords in the FITS headers (or lines starting with ‘#’ in
3910plain text).  You can see them with this command (for more, see *note
3911Image surface brightness limit:: in the next tutorial):
3912
3913     $ astfits cat/xdf-f160w.fits -h1
3914
3915   ---------- Footnotes ----------
3916
3917   (1) This option is plural because we need two ID columns for
3918identifying “clumps” in the clumps catalog/table: the first column will
3919be the ID of the host “object”, and the second one will be the ID of the
3920clump within that object.  In the “objects” catalog/table, only a single
3921column will be returned for this option.
3922
3923   (2) When the PSFs between two images differ largely, you would have
3924to PSF-match the images before using the same pixels for measurements.
3925
3926
3927File: gnuastro.info,  Node: Working with catalogs estimating colors,  Next: Column statistics color-magnitude diagram,  Prev: Segmentation and making a catalog,  Up: General program usage tutorial
3928
39292.2.14 Working with catalogs (estimating colors)
3930------------------------------------------------
3931
3932In the previous step we generated catalogs of objects and clumps over
3933our dataset (see *note Segmentation and making a catalog::).  The
3934catalogs are available in the two extensions of the single FITS file(1).
3935Let’s see the extensions and their basic properties with the Fits
3936program:
3937
3938     $ astfits  cat/xdf-f160w.fits              # Extension information
3939
3940   Let’s inspect the table in each extension with Gnuastro’s Table
3941program (see *note Table::).  We should have used ‘-hOBJECTS’ and
3942‘-hCLUMPS’ instead of ‘-h1’ and ‘-h2’ respectively.  The numbers are
3943just used here to convey that both names or numbers are possible, in the
3944next commands, we’ll just use names.
3945
3946     $ asttable cat/xdf-f160w.fits -h1 --info   # Objects catalog info.
3947     $ asttable cat/xdf-f160w.fits -h1          # Objects catalog columns.
3948     $ asttable cat/xdf-f160w.fits -h2 -i       # Clumps catalog info.
3949     $ asttable cat/xdf-f160w.fits -h2          # Clumps catalog columns.
3950
3951As you see above, when given a specific table (file name and extension),
3952Table will print the full contents of all the columns.  To see the basic
3953metadata about each column (for example name, units and comments),
3954simply append a ‘--info’ (or ‘-i’) to the command.
3955
3956   To print the contents of special column(s), just give the column
3957number(s) (counting from ‘1’) or the column name(s) (if they have one)
3958to the ‘--column’ (or ‘-c’) option.  For example, if you just want the
3959magnitude and signal-to-noise ratio of the clumps (in the clumps
3960catalog), you can get it with any of the following commands
3961
3962     $ asttable cat/xdf-f160w.fits -hCLUMPS --column=5,6
3963     $ asttable cat/xdf-f160w.fits -hCLUMPS -c5,SN
3964     $ asttable cat/xdf-f160w.fits -hCLUMPS -c5         -c6
3965     $ asttable cat/xdf-f160w.fits -hCLUMPS -cMAGNITUDE -cSN
3966
3967Similar to HDUs, when the columns have names, always use the name: it is
3968so common to mis-write numbers or forget the order later!  Using column
3969names instead of numbers has many advantages:
3970  1. You don’t have to worry about the order of columns in the table.
3971  2. It acts as a documentation in the script.
3972  3. Column meta-data (including a name) aren’t just limited to FITS
3973     tables and can also be used in plain text tables, see *note
3974     Gnuastro text table format::.
3975
3976Table also has tools to limit the displayed rows.  For example with the
3977first command below only rows with a magnitude in the range of 29 to 30
3978will be shown.  With the second command, you can further limit the
3979displayed rows to rows with an S/N larger than 10 (a range between 10 to
3980infinity).  You can further sort the output rows, only show the top (or
3981bottom) N rows and etc, for more see *note Table::.
3982
3983     $ asttable cat/xdf-f160w.fits -hCLUMPS --range=MAGNITUDE,28:29
3984     $ asttable cat/xdf-f160w.fits -hCLUMPS \
3985                --range=MAGNITUDE,28:29 --range=SN,10:inf
3986
3987   Now that you are comfortable in viewing table columns and rows, let’s
3988look into merging columns of multiple tables into one table (which is
3989necessary for measuring the color of the clumps).  Since
3990cat/xdf-f160w.fits’ and ‘cat/xdf-f105w-on-f160w-lab.fits’ have exactly
3991the same number of rows and the rows correspond to the same clump, let’s
3992merge them to have one table with magnitudes in both filters.
3993
3994   We can merge columns with the ‘--catcolumnfile’ option like below.
3995You give this option a file name (which is assumed to be a table that
3996has the same number of rows as the main input), and all the table’s
3997columns will be concatenated/appended to the main table.  So please try
3998it out with the commands below.  We’ll first look at the metadata of the
3999first table (only the ‘CLUMPS’ extension).  With the second command,
4000we’ll concatenate the two tables and write them in, ‘two-in-one.fits4001and finally, we’ll check the new catalog’s metadata.
4002
4003     $ asttable cat/xdf-f160w.fits -i -hCLUMPS
4004     $ asttable cat/xdf-f160w.fits -hCLUMPS --output=two-in-one.fits \
4005                --catcolumnfile=cat/xdf-f125w-on-f160w-lab.fits \
4006                --catcolumnhdu=CLUMPS
4007     $ asttable two-in-one.fits -i
4008
4009   By comparing the two metadata, we see that both tables have the same
4010number of rows.  But what might have attracted your attention more, is
4011that ‘two-in-one.fits’ has double the number of columns (as expected,
4012after all, you merged both tables into one file, and didn’t ask for any
4013specific column).  In fact you can concatenate any number of other
4014tables in one command, for example:
4015
4016     $ asttable cat/xdf-f160w.fits -hCLUMPS --output=three-in-one.fits \
4017                --catcolumnfile=cat/xdf-f125w-on-f160w-lab.fits \
4018                --catcolumnfile=cat/xdf-f105w-on-f160w-lab.fits \
4019                --catcolumnhdu=CLUMPS --catcolumnhdu=CLUMPS
4020     $ asttable three-in-one.fits -i
4021
4022   As you see, to avoid confusion in column names, Table has
4023intentionally appended a ‘-1’ to the column names of the first
4024concatenated table (so for example we have the original ‘RA’ column, and
4025another one called ‘RA-1’).  Similarly a ‘-2’ has been added for the
4026columns of the second concatenated table.
4027
4028   However, this example clearly shows a problem with this full
4029concatenation: some columns are identical (for example ‘HOST_OBJ_ID’ and
4030‘HOST_OBJ_ID-1’), or not needed (for example ‘RA-1’ and ‘DEC-1’ which
4031are not necessary here).  In such cases, you can use ‘--catcolumns’ to
4032only concatenate certain columns, not the whole table.  For example this
4033command:
4034
4035     $ asttable cat/xdf-f160w.fits -hCLUMPS --output=two-in-one-2.fits \
4036                --catcolumnfile=cat/xdf-f125w-on-f160w-lab.fits \
4037                --catcolumnhdu=CLUMPS --catcolumns=MAGNITUDE
4038     $ asttable two-in-one-2.fits -i
4039
4040   You see that we have now only appended the ‘MAGNITUDE’ column of
4041cat/xdf-f125w-on-f160w-lab.fits’.  This is what we needed to be able to
4042later subtract the magnitudes.  Let’s go ahead and add the F105W
4043magnitudes also with the command below.  Note how we need to call
4044‘--catcolumnhdu’ once for every table that should be appended, but we
4045only call ‘--catcolumn’ once (assuming all the tables that should be
4046appended have this column).
4047
4048     $ asttable cat/xdf-f160w.fits -hCLUMPS --output=three-in-one-2.fits \
4049                --catcolumnfile=cat/xdf-f125w-on-f160w-lab.fits \
4050                --catcolumnfile=cat/xdf-f105w-on-f160w-lab.fits \
4051                --catcolumnhdu=CLUMPS --catcolumnhdu=CLUMPS \
4052                --catcolumns=MAGNITUDE
4053     $ asttable three-in-one-2.fits -i
4054
4055   But we aren’t finished yet!  There is a very big problem: its not
4056immediately clear which one of ‘MAGNITUDE’, ‘MAGNITUDE-1’ or
4057‘MAGNITUDE-2’ columns belong to which filter!  Right now, you know this
4058because you just ran this command.  But in one hour, you’ll start
4059doubting your self and will be forced to go through your command
4060history, trying to figure out if you added F105W first, or F125W. You
4061should never torture your future-self (or your colleagues) like this!
4062So, let’s rename these confusing columns in the matched catalog.
4063
4064   Fortunately, with the ‘--colmetadata’ option, you can correct the
4065column metadata of the final table (just before it is written).  It
4066takes four values: 1) the original column name or number, 2) the new
4067column name, 3) the column unit and 4) the column comments.  Since the
4068comments are usually human-friendly sentences and contain space
4069characters, you should put them in double quotations like below.  For
4070example by adding three calls of this option to the previous command, we
4071write the filter name in the magnitude column name and description.
4072
4073     $ asttable cat/xdf-f160w.fits -hCLUMPS --output=three-in-one-3.fits \
4074             --catcolumnfile=cat/xdf-f125w-on-f160w-lab.fits \
4075             --catcolumnfile=cat/xdf-f105w-on-f160w-lab.fits \
4076             --catcolumnhdu=CLUMPS --catcolumnhdu=CLUMPS \
4077             --catcolumns=MAGNITUDE \
4078             --colmetadata=MAGNITUDE,MAG-F160w,log,"Magnitude in F160W." \
4079             --colmetadata=MAGNITUDE-1,MAG-F125w,log,"Magnitude in F125W." \
4080             --colmetadata=MAGNITUDE-2,MAG-F105w,log,"Magnitude in F105W."
4081     $ asttable three-in-one-3.fits -i
4082
4083   We now have all three magnitudes in one table and can start doing
4084arithmetic on them (to estimate colors, which are just a subtraction of
4085magnitudes).  To use column arithmetic, simply call the column selection
4086option (‘--column’ or ‘-c’), put the value in single quotations and
4087start the value with ‘arith’ (followed by a space) like the example
4088below.  Column arithmetic uses the same “reverse polish notation” as the
4089Arithmetic program (see *note Reverse polish notation::), with almost
4090all the same operators (see *note Arithmetic operators::), and some
4091column-specific operators (that aren’t available for images).  In
4092column-arithmetic, you can identify columns by number (prefixed with a
4093‘$’) or name, for more see *note Column arithmetic::.
4094
4095   So let’s estimate one color from ‘three-in-one-3.fits’ using column
4096arithmetic.  All the commands below will produce the same output, try
4097them each and focus on the differences.  Note that column arithmetic can
4098be mixed with other ways to choose output columns (the ‘-c’ option).
4099
4100     $ asttable three-in-one-3.fits -ocolor-cat.fits \
4101                -c1,2,3,4,'arith $5 $7 -'
4102
4103     $ asttable three-in-one-3.fits -ocolor-cat.fits \
4104                -c1,2,RA,DEC,'arith MAG-F125W MAG-F160W -'
4105
4106     $ asttable three-in-one-3.fits -ocolor-cat.fits -c1,2 \
4107                -cRA,DEC --column='arith MAG-F105W MAG-F160W -'
4108
4109   This example again highlights the important point on using column
4110names: if you don’t know the commands before, you have no way of making
4111sense of the first command: what is in column 5 and 7?  why not subtract
4112columns 3 and 4 from each other?  Do you see how cryptic the first one
4113is?  Then look at the last one: even if you have no idea how this table
4114was created, you immediately understand the desired operation.  *When
4115you have column names, please use them.*  If your table doesn’t have
4116column names, give them names with the ‘--colmetadata’ (described above)
4117as you are creating them.  But how about the metadata for the column you
4118just created with column arithmetic?  Have a look at the column metadata
4119of the table produced above:
4120
4121     $ asttable color-cat.fits -i
4122
4123   The name of the column produced by arithmetic column is ‘ARITH_1’!
4124This is natural: Arithmetic has no idea what the modified column is!
4125You could have multiplied two columns, or done much more complex
4126transformations with many columns.  _Metadata can’t be set
4127automatically, your (the human) input is necessary._  To add metadata,
4128you can use ‘--colmetadata’ like before:
4129
4130     $ asttable three-in-one-3.fits -ocolor-cat.fits -c1,2,RA,DEC \
4131              --column='arith MAG-F105W MAG-F160W -' \
4132              --colmetadata=ARITH_1,F105W-F160W,log,"Magnitude difference"
4133     $ asttable color-cat.fits -i
4134
4135   We are now ready to make our final table.  We want it to have the
4136magnitudes in all three filters, as well as the three possible colors.
4137Recall that by convention in astronomy colors are defined by subtracting
4138the bluer magnitude from the redder magnitude.  In this way a larger
4139color value corresponds to a redder object.  So from the three
4140magnitudes, we can produce three colors (as shown below).  Also, because
4141this is the final table we are creating here and want to use it later,
4142we’ll store it in ‘cat/’ and we’ll also give it a clear name and use the
4143‘--range’ option to only print columns with a signal-to-noise ratio
4144(‘SN’ column, from the F160W filter) above 5.
4145
4146     $ asttable three-in-one-3.fits --range=SN,5,inf -c1,2,RA,DEC,SN \
4147              -cMAG-F160W,MAG-F125W,MAG-F105W \
4148              -c'arith MAG-F125W MAG-F160W -' \
4149              -c'arith MAG-F105W MAG-F125W -' \
4150              -c'arith MAG-F105W MAG-F160W -' \
4151              --colmetadata=SN,SN-F160W,ratio,"F160W signal to noise ratio" \
4152              --colmetadata=ARITH_1,F125W-F160W,log,"Color F125W and F160W" \
4153              --colmetadata=ARITH_2,F105W-F125W,log,"Color F105W and F125W" \
4154              --colmetadata=ARITH_3,F105W-F160W,log,"Color F105W and F160W" \
4155              --output=cat/mags-with-color.fits
4156     $ asttable cat/mags-with-color.fits -i
4157
4158   The table now has all the columns we need and it has the proper
4159metadata to let us safely use it later (without frustrating over column
4160orders!)  or passing it to colleagues.
4161
4162   Let’s finish this section of the tutorial with a useful tip on
4163modifying column metadata.  Above, updating/changing column metadata was
4164done with the ‘--colmetadata’ in the same command that produced the
4165newly created Table file.  But in many situations, the table is already
4166made and you just want to update the metadata of one column.  In such
4167cases using ‘--colmetadata’ is over-kill (wasting CPU/RAM energy or time
4168if the table is large) because it will load the full table data and
4169metadata into memory, just change the metadata and write it back into a
4170file.
4171
4172   In scenarios when the table’s data doesn’t need to be changed and you
4173just want to set or update the metadata, it is much more efficient to
4174use basic FITS keyword editing.  For example, in the FITS standard,
4175column names are stored in the ‘TTYPE’ header keywords, so let’s have a
4176look:
4177
4178     $ asttable two-in-one.fits -i
4179     $ astfits two-in-one.fits -h1 | grep TTYPE
4180
4181   Changing/updating the column names is as easy as updating the values
4182to these keywords.  You don’t need to touch the actual data!  With the
4183command below, we’ll just update the ‘MAGNITUDE’ and ‘MAGNITUDE-1’
4184columns (which are respectively stored in the ‘TTYPE5’ and ‘TTYPE11’
4185keywords) by modifying the keyword values and checking the effect by
4186listing the column metadata again:
4187
4188     $ astfits two-in-one.fits -h1 \
4189               --update=TTYPE5,MAG-F160W \
4190               --update=TTYPE11,MAG-F125W
4191     $ asttable two-in-one.fits -i
4192
4193   You can see that the column names have indeed been changed without
4194touching any of the data.  You can do the same for the column units or
4195comments by modifying the keywords starting with ‘TUNIT’ or ‘TCOMM’.
4196
4197   Generally, Gnuastro’s table is a very useful program in data analysis
4198and what you have seen so far is just the tip of the iceberg.  But to
4199avoid making the tutorial even longer, we’ll stop reviewing the features
4200here, for more, please see *note Table::.  Before continuing, let’s just
4201delete all the temporary FITS tables we placed in the top project
4202directory:
4203
4204     rm *.fits
4205
4206   ---------- Footnotes ----------
4207
4208   (1) MakeCatalog can also output plain text tables.  However, in the
4209plain text format you can only have one table per file.  Therefore, if
4210you also request measurements on clumps, two plain text tables will be
4211created (suffixed with ‘_o.txt’ and ‘_c.txt’).
4212
4213
4214File: gnuastro.info,  Node: Column statistics color-magnitude diagram,  Next: Aperture photometry,  Prev: Working with catalogs estimating colors,  Up: General program usage tutorial
4215
42162.2.15 Column statistics (color-magnitude diagram)
4217--------------------------------------------------
4218
4219In *note Working with catalogs estimating colors:: we created a single
4220catalog containing the magnitudes of our desired clumps in all three
4221filters, and their colors.  To start with, let’s inspect the
4222distribution of three colors with the Statistics program.
4223
4224     $ aststatistics cat/mags-with-color.fits -cF105W-F125W
4225     $ aststatistics cat/mags-with-color.fits -cF105W-F160W
4226     $ aststatistics cat/mags-with-color.fits -cF125W-F160W
4227
4228   This tiny and cute ASCII histogram (and the general information
4229printed above it) gives you a crude (but very useful and fast) feeling
4230on the distribution.  You can later use Gnuastro’s Statistics program
4231with the ‘--histogram’ option to build a much more fine-grained
4232histogram as a table to feed into your favorite plotting program for a
4233much more accurate/appealing plot (for example with PGFPlots in LaTeX).
4234If you just want a specific measure, for example the mean, median and
4235standard deviation, you can ask for them specifically, like below:
4236
4237     $ aststatistics cat/mags-with-color.fits -cF105W-F160W \
4238                     --mean --median --std
4239
4240   The basic statistics we measured above were just on one column.  In
4241many scenarios this is fine, but things get much more exciting if you
4242look at the correlation of two columns with each other.  For example,
4243let’s create the color-magnitude diagram for our measured targets.
4244
4245   In many papers, the color-magnitude diagram is usually plotted as a
4246scatter plot.  However, scatter plots have a major limitation when there
4247are a lot of points and they cluster together in one region of the plot:
4248the possible correlation in that dense region is lost (because the
4249points fall over each other).  In such cases, its much better to use a
42502D histogram.  In a 2D histogram, the full range in both columns is
4251divided into discrete 2D bins (or pixels!)  and we count how many
4252objects fall in that 2D bin.
4253
4254   Since a 2D histogram is a pixelated space, we can simply save it as a
4255FITS image and view it in a FITS viewer.  Let’s do this in the command
4256below.  As is common with color-magnitude plots, we’ll put the redder
4257magnitude on the horizontal axis and the color on the vertical axis.
4258We’ll set both dimensions to have 100 bins (with ‘--numbins’ for the
4259horizontal and ‘--numbins2’ for the vertical).  Also, to avoid strong
4260outliers in any of the dimensions, we’ll manually set the range of each
4261dimension with the ‘--greaterequal’, ‘--greaterequal2’, ‘--lessthan’ and
4262‘--lessthan2’ options.
4263
4264     $ aststatistics cat/mags-with-color.fits -cMAG-F160W,F105W-F160W \
4265                     --histogram2d=image --manualbinrange \
4266                     --numbins=100  --greaterequal=22  --lessthan=30 \
4267                     --numbins2=100 --greaterequal2=-1 --lessthan2=3 \
4268                     --manualbinrange --output=cmd.fits
4269
4270You can now open this FITS file as a normal FITS image, for example with
4271the command below.  Try hovering/zooming over the pixels: not only will
4272you see the number of objects in the UVUDF catalog that fall in each
4273bin/pixel, but you also see the ‘F160W’ magnitude and color of that
4274pixel also (in the same place you usually see RA and Dec when hovering
4275over an astronomical image).
4276
4277     $ ds9 cmd.fits -cmap sls -zoom to fit
4278
4279   Having a 2D histogram as a FITS image with WCS has many great
4280advantages.  For example, just like FITS images of the night sky, you
4281can “match” many 2D histograms that were created independently.  You can
4282add two histograms with each other, or you can use advanced features of
4283FITS viewers to find structure in the correlation of your columns.
4284
4285With the first command below, you can activate the grid feature of DS9
4286to actually see the coordinate grid, as well as values on each line.
4287With the second command, DS9 will even read the labels of the axises and
4288use them to generate an almost publication-ready plot.
4289
4290     $ ds9 cmd.fits -cmap sls -zoom to fit -grid yes
4291     $ ds9 cmd.fits -cmap sls -zoom to fit -grid yes -grid type publication
4292
4293   If you are happy with the grid and coloring and etc, you can also use
4294ds9 to save this as a JPEG image to directly use in your
4295documents/slides with these extra DS9 options (DS9 will write the image
4296to ‘cmd-2d.jpeg’ and quit immediately afterwards):
4297
4298     $ ds9 cmd.fits -cmap sls -zoom 4 -grid yes -grid type publication \
4299           -saveimage cmd-2d.jpeg -quit
4300
4301   This is good for a fast progress update.  But for your paper or more
4302official report, you want to show something with higher quality.  For
4303that, you can use the PGFPlots package in LaTeX to add axises in the
4304same font as your text, sharp grids and many other elegant/powerful
4305features (like over-plotting interesting points, lines and etc).  But to
4306load the 2D histogram into PGFPlots first you need to convert the FITS
4307image into a more standard format, for example PDF. We’ll use Gnuastro’s
4308*note ConvertType:: for this, and use the ‘sls-inverse’ color map (which
4309will map the pixels with a value of zero to white):
4310
4311     $ astconvertt cmd.fits --colormap=sls-inverse --borderwidth=0 -ocmd.pdf
4312
4313Below you can see a minimally working example of how to add axis
4314numbers, labels and a grid to the PDF generated above.  First, let’s
4315create a new ‘report’ directory to keep the LaTeX outputs, then put the
4316minimal report’s source in a file called ‘report.tex’.  Notice the
4317‘xmin’, ‘xmax’, ‘ymin’, ‘ymax’ values and how they are the same as the
4318range specified above.
4319
4320     $ mkdir report
4321     $ mv cmd.pdf report/
4322     $ cat report/report.tex
4323     \documentclass{article}
4324     \usepackage{pgfplots}
4325     \dimendef\prevdepth=0
4326     \begin{document}
4327
4328     You can write all you want here...\par
4329
4330     \begin{tikzpicture}
4331       \begin{axis}[
4332           enlargelimits=false,
4333           grid,
4334           axis on top,
4335           width=\linewidth,
4336           height=\linewidth,
4337           xlabel={Magnitude (F160W)},
4338           ylabel={Color (F105W-F160W)}]
4339
4340         \addplot graphics[xmin=22, xmax=30, ymin=-1, ymax=3] {cmd.pdf};
4341       \end{axis}
4342     \end{tikzpicture}
4343     \end{document}
4344
4345Run this command to build your PDF (assuming you have LaTeX and
4346PGFPlots).
4347
4348     $ cd report
4349     $ pdflatex report.tex
4350
4351   Open the newly created ‘report.pdf’ and enjoy the exquisite quality.
4352The improved quality, blending in with the text, vector-graphics
4353resolution and other features make this plot pleasing to the eye, and
4354let your readers focus on the main point of your scientific argument.
4355PGFPlots can also built the PDF of the plot separately from the rest of
4356the paper/report, see *note 2D histogram as a table:: for the necessary
4357changes in the preamble.
4358
4359   We won’t go much deeper into the Statistics program here, but there
4360is so much more you can do with it.  After finishing the tutorial, see
4361*note Statistics::.
4362
4363
4364File: gnuastro.info,  Node: Aperture photometry,  Next: Matching catalogs,  Prev: Column statistics color-magnitude diagram,  Up: General program usage tutorial
4365
43662.2.16 Aperture photometry
4367--------------------------
4368
4369The colors we calculated in *note Working with catalogs estimating
4370colors:: used a different segmentation map for each object.  This might
4371not satisfy some science cases that need the flux within a fixed
4372area/aperture.  Fortunately Gnuastro’s modular programs make it very
4373easy do this type of measurement (photometry).  To do this, we can
4374ignore the labeled images of NoiseChisel of Segment, we can just built
4375our own labeled image!  That labeled image can then be given to
4376MakeCatalog
4377
4378   To generate the apertures catalog we’ll use Gnuastro’s MakeProfiles
4379(see *note MakeProfiles::).  But first we need a list of positions
4380(aperture photometry needs a-priori knowledge of your target positions).
4381So we’ll first read the clump positions from the F160W catalog, then use
4382AWK to set the other parameters of each profile to be a fixed circle of
4383radius 5 pixels (recall that we want all apertures to have an identical
4384size/area in this scenario).
4385
4386     $ rm *.fits *.txt
4387     $ asttable cat/xdf-f160w.fits -hCLUMPS -cRA,DEC \
4388                | awk '!/^#/{print NR, $1, $2, 5, 5, 0, 0, 1, NR, 1}' \
4389                > apertures.txt
4390     $ cat apertures.txt
4391
4392   We can now feed this catalog into MakeProfiles using the command
4393below to build the apertures over the image.  The most important option
4394for this particular job is ‘--mforflatpix’, it tells MakeProfiles that
4395the values in the magnitude column should be used for each pixel of a
4396flat profile.  Without it, MakeProfiles would build the profiles such
4397that the _sum_ of the pixels of each profile would have a _magnitude_
4398(in log-scale) of the value given in that column (what you would expect
4399when simulating a galaxy for example).  See *note Invoking astmkprof::
4400for details on the options.
4401
4402     $ astmkprof apertures.txt --background=flat-ir/xdf-f160w.fits \
4403                 --clearcanvas --replace --type=int16 --mforflatpix \
4404                 --mode=wcs --output=apertures.fits
4405
4406   Open ‘apertures.fits’ with a FITS image viewer (like SAO DS9) and
4407look around at the circles placed over the targets.  Also open the input
4408image and Segment’s clumps image and compare them with the positions of
4409these circles.  Where the apertures overlap, you will notice that one
4410label has replaced the other (because of the ‘--replace’ option).  In
4411the future, MakeCatalog will be able to work with overlapping labels,
4412but currently it doesn’t.  If you are interested, please join us in
4413completing Gnuastro with added improvements like this (see task 14750
4414(1)).
4415
4416   We can now feed the ‘apertures.fits’ labeled image into MakeCatalog
4417instead of Segment’s output as shown below.  In comparison with the
4418previous MakeCatalog call, you will notice that there is no more
4419‘--clumpscat’ option, since there is no more separate “clump” image now,
4420each aperture is treated as a separate “object”.
4421
4422     $ astmkcatalog apertures.fits -h1 --zeropoint=26.27 \
4423                    --valuesfile=nc/xdf-f105w.fits \
4424                    --ids --ra --dec --magnitude --sn \
4425                    --output=cat/xdf-f105w-aper.fits
4426
4427   This catalog has the same number of rows as the catalog produced from
4428clumps in *note Working with catalogs estimating colors::.  Therefore
4429similar to how we found colors, you can compare the aperture and clump
4430magnitudes for example.
4431
4432   You can also change the filter name and zero point magnitudes and run
4433this command again to have the fixed aperture magnitude in the F160W
4434filter and measure colors on apertures.
4435
4436   ---------- Footnotes ----------
4437
4438   (1) <https://savannah.gnu.org/task/index.php?14750>
4439
4440
4441File: gnuastro.info,  Node: Matching catalogs,  Next: Finding reddest clumps and visual inspection,  Prev: Aperture photometry,  Up: General program usage tutorial
4442
44432.2.17 Matching catalogs
4444------------------------
4445
4446In the example above, we had the luxury to generate the catalogs
4447ourselves, and where thus able to generate them in a way that the rows
4448match.  But this isn’t generally the case.  In many situations, you need
4449to use catalogs from many different telescopes, or catalogs with
4450high-level calculations that you can’t simply regenerate with the same
4451pixels without spending a lot of time or using heavy computation.  In
4452such cases, when each catalog has the coordinates of its own objects,
4453you can use the coordinates to match the rows with Gnuastro’s Match
4454program (see *note Match::).
4455
4456   As the name suggests, Gnuastro’s Match program will match rows based
4457on distance (or aperture in 2D) in one, two, or three columns.  For this
4458tutorial, let’s try matching the two catalogs that weren’t created from
4459the same labeled images, recall how each has a different number of rows:
4460
4461     $ asttable cat/xdf-f105w.fits -hCLUMPS -i
4462     $ asttable cat/xdf-f160w.fits -hCLUMPS -i
4463
4464   You give Match two catalogs (from the two different filters we
4465derived above) as argument, and the HDUs containing them (if they are
4466FITS files) with the ‘--hdu’ and ‘--hdu2’ options.  The ‘--ccol1’ and
4467‘--ccol2’ options specify the coordinate-columns which should be matched
4468with which in the two catalogs.  With ‘--aperture’ you specify the
4469acceptable error (radius in 2D), in the same units as the columns.
4470
4471     $ astmatch cat/xdf-f160w.fits           cat/xdf-f105w.fits \
4472                --hdu=CLUMPS                 --hdu2=CLUMPS \
4473                --ccol1=RA,DEC               --ccol2=RA,DEC \
4474                --aperture=0.5/3600 \
4475                --output=matched.fits
4476     $ astfits matched.fits
4477
4478   From the second command, you see that the output has two extensions
4479and that both have the same number of rows.  The rows in each extension
4480are the matched rows of the respective input table: those in the first
4481HDU come from the first input and those in the second HDU come from the
4482second.  However, their order may be different from the input tables
4483because the rows match: the first row in the first HDU matches with the
4484first row in the second HDU, and etc.  You can also see which objects
4485didn’t match with the ‘--notmatched’, like below.  Note how each
4486extension of now has a different number of rows.
4487
4488     $ astmatch cat/xdf-f160w.fits           cat/xdf-f105w.fits \
4489                --hdu=CLUMPS                 --hdu2=CLUMPS \
4490                --ccol1=RA,DEC               --ccol2=RA,DEC \
4491                --aperture=0.5/3600 \
4492                --output=not-matched.fits    --notmatched
4493     $ astfits not-matched.fits
4494
4495   The ‘--outcols’ of Match is a very convenient feature: you can use it
4496to specify which columns from the two catalogs you want in the output
4497(merge two input catalogs into one).  If the first character is an
4498‘<a>’, the respective matched column (number or name, similar to Table
4499above) in the first catalog will be written in the output table.  When
4500the first character is a ‘<b>’, the respective column from the second
4501catalog will be written in the output.  Also, if the first character is
4502followed by ‘_all’, then all the columns from the respective catalog
4503will be put in the output.
4504
4505     $ astmatch cat/xdf-f160w.fits           cat/xdf-f105w.fits \
4506                --hdu=CLUMPS                 --hdu2=CLUMPS \
4507                --ccol1=RA,DEC               --ccol2=RA,DEC \
4508                --aperture=0.35/3600 \
4509                --outcols=a_all,bMAGNITUDE,bSN \
4510                --output=matched.fits
4511     $ astfits matched.fits
4512
4513
4514File: gnuastro.info,  Node: Finding reddest clumps and visual inspection,  Next: Writing scripts to automate the steps,  Prev: Matching catalogs,  Up: General program usage tutorial
4515
45162.2.18 Finding reddest clumps and visual inspection
4517---------------------------------------------------
4518
4519As a final step, let’s go back to the original clumps-based color
4520measurement we generated in *note Working with catalogs estimating
4521colors::.  We’ll find the objects with the strongest color and make a
4522cutout to inspect them visually and finally, we’ll see how they are
4523located on the image.  With the command below, we’ll select the reddest
4524objects (those with a color larger than 1.5):
4525
4526     $ asttable cat/mags-with-color.fits --range=F105W-F160W,1.5,inf
4527
4528You can see how many they are by piping it to ‘wc -l’:
4529
4530     $ asttable cat/mags-with-color.fits --range=F105W-F160W,1.5,inf | wc -l
4531
4532   Let’s crop the F160W image around each of these objects, but we first
4533need a unique identifier for them.  We’ll define this identifier using
4534the object and clump labels (with an underscore between them) and feed
4535the output of the command above to AWK to generate a catalog.  Note that
4536since we are making a plain text table, we’ll define the necessary (for
4537the string-type first column) metadata manually (see *note Gnuastro text
4538table format::).
4539
4540     $ echo "# Column 1: ID [name, str10] Object ID" > reddest.txt
4541     $ asttable cat/mags-with-color.fits --range=F105W-F160W,1.5,inf \
4542                | awk '{printf("%d_%-10d %f %f\n", $1, $2, $3, $4)}' \
4543                >> reddest.txt
4544
4545   We can now feed ‘reddest.txt’ into Gnuastro’s Crop program to see
4546what these objects look like.  To keep things clean, we’ll make a
4547directory called ‘crop-red’ and ask Crop to save the crops in this
4548directory.  We’ll also add a ‘-f160w.fits’ suffix to the crops (to
4549remind us which filter they came from).  The width of the crops will be
455015 arc-seconds (or 15/3600 degrees, which is the units of the WCS).
4551
4552     $ mkdir crop-red
4553     $ astcrop flat-ir/xdf-f160w.fits --mode=wcs --namecol=ID \
4554               --catalog=reddest.txt --width=15/3600,15/3600  \
4555               --suffix=-f160w.fits --output=crop-red
4556
4557   You can see all the cropped FITS files in the ‘crop-red’ directory.
4558Like the MakeProfiles command in *note Aperture photometry::, you might
4559notice that the crops aren’t made in order.  This is because each crop
4560is independent of the rest, therefore crops are done in parallel, and
4561parallel operations are asynchronous.  In the command above, you can
4562change ‘f160w’ to ‘f105w’ to make the crops in both filters.
4563
4564   To view the crops more easily (not having to open ds9 for each
4565image), you can convert the FITS crops into the JPEG format with a shell
4566loop like below.
4567
4568     $ cd crop-red
4569     $ for f in *.fits; do                                                  \
4570         astconvertt $f --fluxlow=-0.001 --fluxhigh=0.005 --invert -ojpg;   \
4571       done
4572     $ cd ..
4573     $ ls crop-red/
4574
4575   You can now use your general graphic user interface image viewer to
4576flip through the images more easily, or import them into your
4577papers/reports.
4578
4579   The ‘for’ loop above to convert the images will do the job in series:
4580each file is converted only after the previous one is complete.  If you
4581have GNU Parallel (https://www.gnu.org/s/parallel), you can greatly
4582speed up this conversion.  GNU Parallel will run the separate commands
4583simultaneously on different CPU threads in parallel.  For more
4584information on efficiently using your threads, see *note Multi-threaded
4585operations::.  Here is a replacement for the shell ‘for’ loop above
4586using GNU Parallel.
4587
4588     $ cd crop-red
4589     $ parallel astconvertt --fluxlow=-0.001 --fluxhigh=0.005 --invert   \
4590                -ojpg ::: *.fits
4591     $ cd ..
4592
4593Did you notice how much faster this one was?  When possible, its always
4594very helpful to do your analysis in parallel.  But the problem is that
4595many operations are not as simple as this.  For such cases, you can use
4596Make (https://en.wikipedia.org/wiki/Make_(software)) which will greatly
4597help designing workflows.  But that is beyond the topic here.
4598
4599   As the final action, let’s see how these objects are positioned over
4600the dataset.  DS9 has the “Region”s concept for this purpose.  You just
4601have to convert your catalog into a “region file” to feed into DS9.  To
4602do that, you can use AWK again as shown below.
4603
4604     $ awk 'BEGIN{print "# Region file format: DS9 version 4.1";      \
4605                  print "global color=green width=2";                 \
4606                  print "fk5";}                                       \
4607            !/^#/{printf "circle(%s,%s,1\") # text={%s}\n",$2,$3,$1;}'\
4608           reddest.txt > reddest.reg
4609
4610   This region file can be loaded into DS9 with its ‘-regions’ option to
4611display over any image (that has world coordinate system).  In the
4612example below, we’ll open Segment’s output and load the regions over all
4613the extensions (to see the image and the respective clump):
4614
4615     $ ds9 -mecube seg/xdf-f160w.fits -zscale -zoom to fit    \
4616           -regions load all reddest.reg
4617
4618
4619File: gnuastro.info,  Node: Writing scripts to automate the steps,  Next: Citing and acknowledging Gnuastro,  Prev: Finding reddest clumps and visual inspection,  Up: General program usage tutorial
4620
46212.2.19 Writing scripts to automate the steps
4622--------------------------------------------
4623
4624In the previous sub-sections, we went through a series of steps like
4625downloading the necessary datasets (in *note Setup and data download::),
4626detecting the objects in the image, and finally selecting a particular
4627subset of them to inspect visually (in *note Finding reddest clumps and
4628visual inspection::).  To benefit most effectively from this subsection,
4629please go through the previous sub-sections, and if you haven’t actually
4630done them, we recommended to do/run them before continuing here.
4631
4632   Each sub-section/step of the sub-sections above involved several
4633commands on the command-line.  Therefore, if you want to reproduce the
4634previous results (for example to only change one part, and see its
4635effect), you’ll have to go through all the sections above and read
4636through them again.  If you done the commands recently, you may also
4637have them in the history of your shell (command-line environment).  You
4638can see many of your previous commands on the shell (even if you have
4639closed the terminal) with the ‘history’ command, like this:
4640
4641     $ history
4642
4643   Try it in your teminal to see for your self.  By default in GNU Bash,
4644it shows the last 500 commands.  You can also save this “history” of
4645previous commands to a file using shell redirection (to have it after
4646your next 500 commands), with this command
4647
4648     $ history > my-previous-commands.txt
4649
4650   This is a good way to temporarily keep track of every single command
4651you ran.  But in the middle of all the useful commands, you will have
4652many extra commands, like tests that you did before/after the good
4653output of a step (that you decided to continue working on), or an
4654unrelated job you had to do in the middle of this project.  Because of
4655these impurities, after a few days (that you have forgot the context:
4656tests you didn’t end-up using, or unrelated jobs) reading this full
4657history will be very frustrating.
4658
4659   Keeping the final commands that were used in each step of an analysis
4660is a common problem for anyone who is doing something serious with the
4661computer.  But simply keeping the most important commands in a text file
4662is not enough, the small steps in the middle (like making a directory to
4663keep the outputs of one step) are also important.  In other words, the
4664only way you can be sure that you are under control of your processing
4665(and actually understand how you produced your final result) is to run
4666the commands automatically.
4667
4668   Fortunately, typing commands interactively with your fingers isn’t
4669the only way to operate the shell.  The shell can also take its
4670orders/commands from a plain-text file, which is called a _script_.
4671When given a script, the shell will read it line-by-line as if you have
4672actually typed it manually.
4673
4674   Let’s continue with an example: try typing the commands below in your
4675shell.  With these commands we are making a text file (‘a.txt’)
4676containing a simple $3\times3$ matrix, converting it to a FITS image and
4677computing its basic statistics.  After the first three commands open
4678a.txt’ with a text editor to actually see the values we wrote in it,
4679and after the fourth, open the FITS file to see the matrix as an image.
4680a.txt’ is created through the shell’s redirection feature: ‘‘>’’
4681overwrites the existing contents of a file, and ‘‘>>’’ appends the new
4682contents after the old contents.
4683
4684     $ echo "1 1 1" > a.txt
4685     $ echo "1 2 1" >> a.txt
4686     $ echo "1 1 1" >> a.txt
4687     $ astconvertt a.txt --output=a.fits
4688     $ aststatistics a.fits
4689
4690   To automate these series of commands, you should put them in a text
4691file.  But that text file must have two special features: 1) It should
4692tell the shell what program should interpret the script.  2) The
4693operating system should know that the file can be directly executed.
4694
4695   For the first, Unix-like operating systems define the _shebang_
4696concept (also known as _sha-bang_ or _hashbang_).  In the shebang
4697convention, the first two characters of a file should be ‘‘#!’’.  When
4698confronted with these characters, the script will be interpreted with
4699the program that follows them.  In this case, we want to write a shell
4700script and the most common shell program is GNU Bash which is installed
4701in ‘/bin/bash’.  So the first line of your script should be
4702‘‘#!/bin/bash’’(1).
4703
4704   It may happen (rarely) that GNU Bash is in another location on your
4705system.  In other cases, you may prefer to use a non-standard version of
4706Bash installed in another location (that has higher priority in your
4707‘PATH’, see *note Installation directory::).  In such cases, you can use
4708the ‘‘#!/usr/bin/env bash’’ shebang instead.  Through the ‘env’ program,
4709this shebang will look in your ‘PATH’ and use the first ‘bash’ it finds
4710to run your script.  But for simplicity in the rest of the tutorial,
4711we’ll continue with the ‘‘#!/bin/bash’’ shebang.
4712
4713   Using your favorite text editor, make a new empty file, let’s call it
4714my-first-script.sh’.  Write the GNU Bash shebang (above) as its first
4715line After the shebang, copy the series of commands we ran above.  Just
4716note that the ‘‘$’’ sign at the start of every line above is the prompt
4717of the interactive shell (you never actually typed it, remember?).
4718Therefore, commands in a shell script should not start with a ‘‘$’’.
4719Once you add the commands, close the text editor and run the ‘cat’
4720command to confirm its contents.  It should look like the example below.
4721Recall that you should only type the line that starts with a ‘‘$’’, the
4722lines without a ‘‘$’’, are printed automatically on the command-line
4723(they are the contents of your script).
4724
4725     $ cat my-first-script.sh
4726     #!/bin/bash
4727     echo "1 1 1" > a.txt
4728     echo "1 2 1" >> a.txt
4729     echo "1 1 1" >> a.txt
4730     astconvertt a.txt --output=a.fits
4731     aststatistics a.fits
4732
4733   The script contents are now ready, but to run it, you should activate
4734the script file’s _executable flag_.  In Unix-like operating systems,
4735every file has three types of flags: _read_ (or ‘r’), _write_ (or ‘w’)
4736and _execute_ (or ‘x’).  To toggle a file’s flags, you should use the
4737‘chmod’ (for “change mode”) command.  To activate a flag, you put a
4738‘‘+’’ before the flag character (for example ‘+x’).  To deactivate it,
4739you put a ‘‘-’’ (for example ‘-x’).  In this case, you want to activate
4740the script’s executable flag, so you should run
4741
4742     $ chmod +x my-first-script.sh
4743
4744   Your script is now ready to run/execute the series of commands.  To
4745run it, you should call it while specifying its location in the file
4746system.  Since you are currently in the same directory as the script,
4747its easiest to use relative addressing like below (where ‘‘./’’ means
4748the current directory).  But before running your script, first delete
4749the two ‘a.txt’ and ‘a.fits’ files that were created when you
4750interactively ran the commands.
4751
4752     $ rm a.txt a.fits
4753     $ ls
4754     $ ./my-first-script.sh
4755     $ ls
4756
4757The script immediately prints the statistics while doing all the
4758previous steps in the background.  With the last ‘ls’, you see that it
4759automatically re-built the ‘a.txt’ and ‘a.fits’ files, open them and
4760have a look at their contents.
4761
4762   An extremely useful feature of shell scripts is that the shell will
4763ignore anything after a ‘‘#’’ character.  You can thus add
4764descriptions/comments to the commands and make them much more useful for
4765the future.  For example, after adding comments, your script might look
4766like this:
4767
4768     $ cat my-first-script.sh
4769     #!/bin/bash
4770
4771     # This script is my first attempt at learning to write shell scripts.
4772     # As a simple series of commands, I am just building a small FITS
4773     # image, and calculating its basic statistics.
4774
4775     # Write the matrix into a file.
4776     echo "1 1 1" > a.txt
4777     echo "1 2 1" >> a.txt
4778     echo "1 1 1" >> a.txt
4779
4780     # Convert the matrix to a FITS image.
4781     astconvertt a.txt --output=a.fits
4782
4783     # Calculate the statistics of the FITS image.
4784     aststatistics a.fits
4785
4786Isn’t this much more easier to read now?  Comments help to provide
4787human-friendly context to the raw commands.  At the time you make a
4788script, comments may seem like an extra effort and slow you down.  But
4789in one year, you will forget almost everything about your script and you
4790will appreciate the effort so much!  Think of the comments as an email
4791to your future-self and always put a well-written description of the
4792context/purpose (most importantly, things that aren’t directly clear by
4793reading the commands) in your scripts.
4794
4795   The example above was very basic and mostly redundant series of
4796commands, to show the basic concepts behind scripts.  You can put any
4797(arbitrarily long and complex) series of commands in a script by
4798following the two rules: 1) add a shebang, and 2) enable the executable
4799flag.  In fact, as you continue your own research projects, you will
4800find that any time you are dealing with more than two or three commands,
4801keeping them in a script (and modifying that script, and running it) is
4802much more easier, and future-proof, then typing the commands directly on
4803the command-line and relying on things like ‘history’.  Here are some
4804tips that will come in handy when you are writing your scripts:
4805
4806   As a more realistic example, let’s have a look at a script that will
4807do the steps of *note Setup and data download:: and *note Dataset
4808inspection and cropping::.  In particular note how often we are using
4809variables to avoid repeating fixed strings of characters (usually
4810file/directory names).  This greatly helps in scaling up your project,
4811and avoiding hard-to-find bugs that are caused by typos in those fixed
4812strings.
4813
4814     $ cat gnuastro-tutorial-1.sh
4815     #!/bin/bash
4816
4817
4818     # Download the input datasets
4819     # ---------------------------
4820     #
4821     # The default file names have this format (where `FILTER' differs for
4822     # each filter):
4823     #   hlsp_xdf_hst_wfc3ir-60mas_hudf_FILTER_v1_sci.fits
4824     # To make the script easier to read, a prefix and suffix variable are
4825     # used to sandwich the filter name into one short line.
4826     downloaddir=download
4827     xdfsuffix=_v1_sci.fits
4828     xdfprefix=hlsp_xdf_hst_wfc3ir-60mas_hudf_
4829     xdfurl=http://archive.stsci.edu/pub/hlsp/xdf
4830
4831     # The file name and full URLs of the input data.
4832     f105w_in=$xdfprefix"f105w"$xdfsuffix
4833     f160w_in=$xdfprefix"f160w"$xdfsuffix
4834     f105w_full=$xdfurl/$f105w_in
4835     f160w_full=$xdfurl/$f160w_in
4836
4837     # Go into the download directory and download the images there,
4838     # then come back up to the top running directory.
4839     mkdir $downloaddir
4840     cd $downloaddir
4841     wget $f105w_full
4842     wget $f160w_full
4843     cd ..
4844
4845
4846     # Only work on the deep region
4847     # ----------------------------
4848     #
4849     # To help in readability, each vertice of the deep/flat field is stored
4850     # as a separate variable. They are then merged into one variable to
4851     # define the polygon.
4852     flatdir=flat-ir
4853     vertice1="53.187414,-27.779152"
4854     vertice2="53.159507,-27.759633"
4855     vertice3="53.134517,-27.787144"
4856     vertice4="53.161906,-27.807208"
4857     f105w_flat=$flatdir/xdf-f105w.fits
4858     f160w_flat=$flatdir/xdf-f160w.fits
4859     deep_polygon="$vertice1:$vertice2:$vertice3:$vertice4"
4860
4861     mkdir $flatdir
4862     astcrop --mode=wcs -h0 --output=$f105w_flat \
4863             --polygon=$deep_polygon $downloaddir/$f105w_in
4864     astcrop --mode=wcs -h0 --output=$f160w_flat \
4865             --polygon=$deep_polygon $downloaddir/$f160w_in
4866
4867   The first thing you may notice is that even if you already have the
4868downloaded input images, this script will always try to re-download
4869them.  Also, if you re-run the script, you will notice that ‘mkdir’
4870prints an error message that the download directory already exists.
4871Therefore, the script above isn’t too useful and some modifications are
4872necessary to make it more generally useful.  Here are some general tips
4873that are often very useful when writing scripts:
4874
4875*Stop script if a command crashes*
4876     By default, if a command in a script crashes (aborts and fails to
4877     do what it was meant to do), the script will continue onto the next
4878     command.  In GNU Bash, you can tell the shell to stop a script in
4879     the case of a crash by adding this line at the start of your
4880     script:
4881
4882          set -e
4883
4884*Check if a file/directory exists to avoid re-creating it*
4885     Conditionals are a very useful feature in scripts.  One common
4886     conditional is to check if a file exists or not.  Assuming the
4887     file’s name is ‘FILENAME’, you can check its existance (to avoid
4888     re-doing the commands that build it) like this:
4889          if [ -f FILENAME ]; then
4890            echo "FILENAME exists"
4891          else
4892            # Some commands to generate the file
4893            echo "done" > FILENAME
4894          fi
4895     To check the existance of a directory instead of a file, use ‘-d’
4896     instead of ‘-f’.  To negate a conditional, use ‘‘!’’ and note that
4897     conditionals can be written in one line also (useful for when its
4898     short).
4899
4900     One common scenario that you’ll need to check the existance of
4901     directories is when you are making them: the default ‘mkdir’
4902     command will crash if the desired directory already exists.  On
4903     some systems (including GNU/Linux distributions), ‘mkdir’ has
4904     options to deal with such cases.  But if you want your script to be
4905     portable, its best to check yourself like below:
4906
4907          if ! [ -d DIRNAME ]; then mkdir DIRNAME; fi
4908
4909Taking these tips into consideration, we can write a better version of
4910the script above that includes checks on every step to avoid repeating
4911steps/commands.  Please compare this script with the previous one
4912carefully to spot the differences.  These are very important points that
4913you will definitely encouter during your own research, and knowing them
4914can greatly help your productiveity, so pay close attention (even in the
4915comments).
4916
4917     $ cat gnuastro-tutorial-2.sh
4918     #!/bin/bash
4919     set -e
4920
4921
4922     # Download the input datasets
4923     # ---------------------------
4924     #
4925     # The default file names have this format (where `FILTER' differs for
4926     # each filter):
4927     #   hlsp_xdf_hst_wfc3ir-60mas_hudf_FILTER_v1_sci.fits
4928     # To make the script easier to read, a prefix and suffix variable are
4929     # used to sandwich the filter name into one short line.
4930     downloaddir=download
4931     xdfsuffix=_v1_sci.fits
4932     xdfprefix=hlsp_xdf_hst_wfc3ir-60mas_hudf_
4933     xdfurl=http://archive.stsci.edu/pub/hlsp/xdf
4934
4935     # The file name and full URLs of the input data.
4936     f105w_in=$xdfprefix"f105w"$xdfsuffix
4937     f160w_in=$xdfprefix"f160w"$xdfsuffix
4938     f105w_full=$xdfurl/$f105w_in
4939     f160w_full=$xdfurl/$f160w_in
4940
4941     # Go into the download directory and download the images there,
4942     # then come back up to the top running directory.
4943     if ! [ -d $downloaddir ]; then mkdir $downloaddir; fi
4944     cd $downloaddir
4945     if ! [ -f $f105w_in ]; then wget $f105w_full; fi
4946     if ! [ -f $f160w_in ]; then wget $f160w_full; fi
4947     cd ..
4948
4949
4950     # Only work on the deep region
4951     # ----------------------------
4952     #
4953     # To help in readability, each vertice of the deep/flat field is stored
4954     # as a separate variable. They are then merged into one variable to
4955     # define the polygon.
4956     flatdir=flat-ir
4957     vertice1="53.187414,-27.779152"
4958     vertice2="53.159507,-27.759633"
4959     vertice3="53.134517,-27.787144"
4960     vertice4="53.161906,-27.807208"
4961     f105w_flat=$flatdir/xdf-f105w.fits
4962     f160w_flat=$flatdir/xdf-f160w.fits
4963     deep_polygon="$vertice1:$vertice2:$vertice3:$vertice4"
4964
4965     if ! [ -d $flatdir ]; then mkdir $flatdir; fi
4966     if ! [ -f $f105w_flat ]; then
4967         astcrop --mode=wcs -h0 --output=$f105w_flat \
4968                 --polygon=$deep_polygon $downloaddir/$f105w_in
4969     fi
4970     if ! [ -f $f160w_flat ]; then
4971         astcrop --mode=wcs -h0 --output=$f160w_flat \
4972                 --polygon=$deep_polygon $downloaddir/$f160w_in
4973     fi
4974
4975   ---------- Footnotes ----------
4976
4977   (1) When the script is to be run by the same shell that is calling it
4978(like this script), the shebang is optional.  But it is still
4979recommended, because it ensures that even if the user isn’t using GNU
4980Bash, the script will be run in GNU Bash: given the differences between
4981various shells, writing truely portable shell scripts, that can be run
4982by many shell programs/implementations, isn’t easy (sometimes not
4983possible!).
4984
4985
4986File: gnuastro.info,  Node: Citing and acknowledging Gnuastro,  Prev: Writing scripts to automate the steps,  Up: General program usage tutorial
4987
49882.2.20 Citing and acknowledging Gnuastro
4989----------------------------------------
4990
4991In conclusion, we hope this extended tutorial has been a good starting
4992point to help in your exciting research.  If this book or any of the
4993programs in Gnuastro have been useful for your research, please cite the
4994respective papers, and acknowledge the funding agencies that made all of
4995this possible.  Without citations, we won’t be able to secure future
4996funding to continue working on Gnuastro or improving it, so please take
4997software citation seriously (for all the scientific software you use,
4998not just Gnuastro).
4999
5000   To help you in this aspect is well, all Gnuastro programs have a
5001‘--cite’ option to facilitate the citation and acknowledgment.  Just
5002note that it may be necessary to cite additional papers for different
5003programs, so please try it out on all the programs that you used, for
5004example:
5005
5006     $ astmkcatalog --cite
5007     $ astnoisechisel --cite
5008
5009
5010File: gnuastro.info,  Node: Detecting large extended targets,  Prev: General program usage tutorial,  Up: Tutorials
5011
50122.3 Detecting large extended targets
5013====================================
5014
5015The outer wings of large and extended objects can sink into the noise
5016very gradually and can have a large variety of shapes (for example due
5017to tidal interactions).  Therefore separating the outer boundaries of
5018the galaxies from the noise can be particularly tricky.  Besides causing
5019an under-estimation in the total estimated brightness of the target,
5020failure to detect such faint wings will also cause a bias in the noise
5021measurements, thereby hampering the accuracy of any measurement on the
5022dataset.  Therefore even if they don’t constitute a significant fraction
5023of the target’s light, or aren’t your primary target, these regions must
5024not be ignored.  In this tutorial, we’ll walk you through the strategy
5025of detecting such targets using *note NoiseChisel::.
5026
5027*Don’t start with this tutorial:* If you haven’t already completed *note
5028General program usage tutorial::, we strongly recommend going through
5029that tutorial before starting this one.  Basic features like access to
5030this book on the command-line, the configuration files of Gnuastro’s
5031programs, benefiting from the modular nature of the programs, viewing
5032multi-extension FITS files, or using NoiseChisel’s outputs are discussed
5033in more detail there.
5034
5035   We’ll try to detect the faint tidal wings of the beautiful M51
5036group(1) in this tutorial.  We’ll use a dataset/image from the public
5037Sloan Digital Sky Survey (http://www.sdss.org/), or SDSS. Due to its
5038more peculiar low surface brightness structure/features, we’ll focus on
5039the dwarf companion galaxy of the group (or NGC 5195).
5040
5041* Menu:
5042
5043* Downloading and validating input data::  How to get and check the input data.
5044* NoiseChisel optimization::    Detect the extended and diffuse wings.
5045* Image surface brightness limit::  Standards to quantify the noise level.
5046* Achieved surface brightness level::  Calculate the outer surface brightness.
5047* Extract clumps and objects::  Find sub-structure over the detections.
5048
5049   ---------- Footnotes ----------
5050
5051   (1) <https://en.wikipedia.org/wiki/M51_Group>
5052
5053
5054File: gnuastro.info,  Node: Downloading and validating input data,  Next: NoiseChisel optimization,  Prev: Detecting large extended targets,  Up: Detecting large extended targets
5055
50562.3.1 Downloading and validating input data
5057-------------------------------------------
5058
5059To get the image, you can use SDSS’s Simple field search
5060(https://dr12.sdss.org/fields) tool.  As long as it is covered by the
5061SDSS, you can find an image containing your desired target either by
5062providing a standard name (if it has one), or its coordinates.  To
5063access the dataset we will use here, write ‘NGC5195’ in the “Object
5064Name” field and press “Submit” button.
5065
5066*Type the example commands:* Try to type the example commands on your
5067terminal and use the history feature of your command-line (by pressing
5068the “up” button to retrieve previous commands).  Don’t simply copy and
5069paste the commands shown here.  This will help simulate future
5070situations when you are processing your own datasets.
5071
5072   You can see the list of available filters under the color image.  For
5073this demonstration, we’ll use the r-band filter image.  By clicking on
5074the “r-band FITS” link, you can download the image.  Alternatively, you
5075can just run the following command to download it with GNU Wget(1).  To
5076keep things clean, let’s also put it in a directory called ‘ngc5195’.
5077With the ‘-O’ option, we are asking Wget to save the downloaded file
5078with a more manageable name: ‘r.fits.bz2’ (this is an r-band image of
5079NGC 5195, which was the directory name).
5080
5081     $ mkdir ngc5195
5082     $ cd ngc5195
5083     $ topurl=https://dr12.sdss.org/sas/dr12/boss/photoObj/frames
5084     $ wget $topurl/301/3716/6/frame-r-003716-6-0117.fits.bz2 -Or.fits.bz2
5085
5086   When you want to reproduce a previous result (a known analysis, on a
5087known dataset, to get a known result: like the case here!)  it is
5088important to verify that the file is correct: that the input file hasn’t
5089changed (on the remote server, or in your own archive), or there was no
5090downloading problem.  Otherwise, if the data have changed in your
5091server/archive, and you use the same script, you will get a different
5092result, causing a lot of confusion!
5093
5094   One good way to verify the contents of a file is to store its
5095_Checksum_ in your analysis script and check it before any other
5096operation.  The _Checksum_ algorithms look into the contents of a file
5097and calculate a fixed-length string from them.  If any change (even in a
5098bit or byte) is made within the file, the resulting string will change,
5099for more see Wikipedia (https://en.wikipedia.org/wiki/Checksum).  There
5100are many common algorithms, but a simple one is the SHA-1 algorithm
5101(https://en.wikipedia.org/wiki/SHA-1) (Secure Hash Algorithm 1) that you
5102can calculate easily with the command below (the second line is the
5103output, and the checksum is the first/long string: it is independent of
5104the file name)
5105
5106     $ sha1sum r.fits.bz2
5107     5fb06a572c6107c72cbc5eb8a9329f536c7e7f65  r.fits.bz2
5108
5109   If the checksum on your computer is different from this, either the
5110file has been incorrectly downloaded (most probable), or it has changed
5111on SDSS servers (very unlikely(2)).  To get a better feeling of
5112checksums open your favorite text editor and make a test file by writing
5113something in it.  Save it and calculate the text file’s SHA-1 checksum
5114with ‘sha1sum’.  Try renaming that file, and you’ll see the checksum
5115hasn’t changed (checksums only look into the contents, not the
5116name/location of the file).  Then open the file with your text editor
5117again, make a change and re-calculate its checksum, you’ll see the
5118checksum string has changed.
5119
5120   Its always good to keep this short checksum string with your
5121project’s scripts and validate your input data before using them.  You
5122can do this with a shell conditional like this:
5123
5124     filename=r.fits.bz2
5125     expected=5fb06a572c6107c72cbc5eb8a9329f536c7e7f65
5126     sum=$(sha1sum $filename | awk '{print $1}')
5127     if [ $sum = $expected ]; then
5128       echo "$filename: validated"
5129     else
5130       echo "$filename: wrong checksum!"
5131       exit 1
5132     fi
5133
5134Now that we know you have the same data that we wrote this tutorial
5135with, let’s continue.  The SDSS server keeps the files in a Bzip2
5136compressed file format (that have a ‘.bz2’ suffix).  So we’ll first
5137decompress it with the following command to use it as a normal FITS
5138file.  By convention, compression programs delete the original file
5139(compressed when uncompressing, or uncompressed when compressing).  To
5140keep the original file, you can use the ‘--keep’ or ‘-k’ option which is
5141available in most compression programs for this job.  Here, we don’t
5142need the compressed file any more, so we’ll just let ‘bunzip’ delete it
5143for us and keep the directory clean.
5144
5145     $ bunzip2 r.fits.bz2
5146
5147   ---------- Footnotes ----------
5148
5149   (1) To make the command easier to view on screen or in a page, we
5150have defined the top URL of the image as the ‘topurl’ shell variable.
5151You can just replace the value of this variable with ‘$topurl’ in the
5152‘wget’ command.
5153
5154   (2) If your checksum is different, try uncompressing the file with
5155the ‘bunzip2’ command after this, and open the resulting FITS file.  If
5156it opens and you see the image of M51 and NGC5195, then there was no
5157download problem, and the file has indeed changed on the SDSS servers!
5158In this case, please contact us at ‘bug-gnuastro@gnu.org’.
5159
5160
5161File: gnuastro.info,  Node: NoiseChisel optimization,  Next: Image surface brightness limit,  Prev: Downloading and validating input data,  Up: Detecting large extended targets
5162
51632.3.2 NoiseChisel optimization
5164------------------------------
5165
5166In *note Detecting large extended targets:: we downloaded the single
5167exposure SDSS image.  Let’s see how NoiseChisel operates on it with its
5168default parameters:
5169
5170     $ astnoisechisel r.fits -h0
5171
5172   As described in *note NoiseChisel and Multiextension FITS files::,
5173NoiseChisel’s default output is a multi-extension FITS file.  Open the
5174output ‘r_detected.fits’ file and have a look at the extensions, the
51750-th extension is only meta-data and contains NoiseChisel’s
5176configuration parameters.  The rest are the Sky-subtracted input, the
5177detection map, Sky values and Sky standard deviation.
5178
5179     $ ds9 -mecube r_detected.fits -zscale -zoom to fit
5180
5181   Flipping through the extensions in a FITS viewer, you will see that
5182the first image (Sky-subtracted image) looks reasonable: there are no
5183major artifacts due to bad Sky subtraction compared to the input.  The
5184second extension also seems reasonable with a large detection map that
5185covers the whole of NGC5195, but also extends towards the bottom of the
5186image where we actually see faint and diffuse signal in the input image.
5187
5188   Now try flipping between the ‘DETECTIONS’ and ‘SKY’ extensions.  In
5189the ‘SKY’ extension, you’ll notice that there is still significant
5190signal beyond the detected pixels.  You can tell that this signal
5191belongs to the galaxy because the far-right side of the image (away from
5192M51) is dark (has lower values) and the brighter parts in the Sky image
5193(with larger values) are just under the detections and follow a similar
5194pattern.
5195
5196   The fact that signal from the galaxy remains in the ‘SKY’ HDU shows
5197that NoiseChisel can be optimized for a much better result.  The ‘SKY’
5198extension must not contain any light around the galaxy.  Generally, any
5199time your target is much larger than the tile size and the signal is
5200very diffuse and extended at low signal-to-noise values (like this
5201case), this _will_ happen.  Therefore, when there are large objects in
5202the dataset, *the best place* to check the accuracy of your detection is
5203the estimated Sky image.
5204
5205   When dominated by the background, noise has a symmetric distribution.
5206However, signal is not symmetric (we don’t have negative signal).
5207Therefore when non-constant(1) signal is present in a noisy dataset, the
5208distribution will be positively skewed.  For a demonstration, see Figure
52091 of Akhlaghi and Ichikawa [2015] (https://arxiv.org/abs/1505.01664).
5210This skewness is a good measure of how much faint signal we have in the
5211distribution.  The skewness can be accurately measured by the difference
5212in the mean and median (assuming no strong outliers): the more distant
5213they are, the more skewed the dataset is.  For more see *note
5214Quantifying signal in a tile::.
5215
5216   However, skewness is only a proxy for signal when the signal has
5217structure (varies per pixel).  Therefore, when it is approximately
5218constant over a whole tile, or sub-set of the image, the constant
5219signal’s effect is just to shift the symmetric center of the noise
5220distribution to the positive and there won’t be any skewness (major
5221difference between the mean and median).  This positive(2) shift that
5222preserves the symmetric distribution is the Sky value.  When there is a
5223gradient over the dataset, different tiles will have different constant
5224shifts/Sky-values, for example see Figure 11 of Akhlaghi and Ichikawa
5225[2015] (https://arxiv.org/abs/1505.01664).
5226
5227   To make this very large diffuse/flat signal detectable, you will
5228therefore need a larger tile to contain a larger change in the values
5229within it (and improve number statistics, for less scatter when
5230measuring the mean and median).  So let’s play with the tessellation a
5231little to see how it affects the result.  In Gnuastro, you can see the
5232option values (‘--tilesize’ in this case) by adding the ‘-P’ option to
5233your last command.  Try running NoiseChisel with ‘-P’ to see its default
5234tile size.
5235
5236   You can clearly see that the default tile size is indeed much smaller
5237than this (huge) galaxy and its tidal features.  As a result,
5238NoiseChisel was unable to identify the skewness within the tiles under
5239the outer parts of M51 and NGC 5159 and the threshold has been
5240over-estimated on those tiles.  To see which tiles were used for
5241estimating the quantile threshold (no skewness was measured), you can
5242use NoiseChisel’s ‘--checkqthresh’ option:
5243
5244     $ astnoisechisel r.fits -h0 --checkqthresh
5245
5246   Did you see how NoiseChisel aborted after finding and applying the
5247quantile thresholds?  When you call any of NoiseChisel’s ‘--check*’
5248options, by default, it will abort as soon as all the check steps have
5249been written in the check file (a multi-extension FITS file).  This
5250allows you to focus on the problem you wanted to check as soon as
5251possible (you can disable this feature with the ‘--continueaftercheck’
5252option).
5253
5254   To optimize the threshold-related settings for this image, let’s play
5255with this quantile threshold check image a little.  Don’t forget that
5256“_Good statistical analysis is not a purely routine matter, and
5257generally calls for more than one pass through the computer_” (Anscombe
52581973, see *note Science and its tools::).  A good scientist must have a
5259good understanding of her tools to make a meaningful analysis.  So don’t
5260hesitate in playing with the default configuration and reviewing the
5261manual when you have a new dataset (from a new instrument) in front of
5262you.  Robust data analysis is an art, therefore a good scientist must
5263first be a good artist.  So let’s open the check image as a
5264multi-extension cube:
5265
5266     $ ds9 -mecube r_qthresh.fits -zscale -cmap sls -zoom to fit
5267
5268   The first extension (called ‘CONVOLVED’) of ‘r_qthresh.fits’ is the
5269convolved input image where the threshold(s) is(are) defined (and later
5270applied to).  For more on the effect of convolution and thresholding,
5271see Sections 3.1.1 and 3.1.2 of Akhlaghi and Ichikawa [2015]
5272(https://arxiv.org/abs/1505.01664).  The second extension
5273(‘QTHRESH_ERODE’) has a blank/white value for all the pixels of any tile
5274that was identified as having significant signal.  The other tiles have
5275the measured threshold over them.  The next two extensions
5276(‘QTHRESH_NOERODE’ and ‘QTHRESH_EXPAND’) are the other two quantile
5277thresholds that are necessary in NoiseChisel’s later steps.  Every step
5278in this file is repeated on the three thresholds.
5279
5280   Play a little with the color bar of the ‘QTHRESH_ERODE’ extension,
5281you clearly see how the non-blank tiles around NGC 5195 have a gradient.
5282As one line of attack against discarding too much signal below the
5283threshold, NoiseChisel rejects outlier tiles.  Go forward by three
5284extensions to ‘VALUE1_NO_OUTLIER’ and you will see that many of the
5285tiles over the galaxy have been removed in this step.  For more on the
5286outlier rejection algorithm, see the latter half of *note Quantifying
5287signal in a tile::.
5288
5289   Even though much of the galaxy’s footprint has been rejected as
5290outliers, there are still tiles with signal remaining: play with the DS9
5291color-bar and you still see a gradient near the outer tidal feature of
5292the galaxy.  Before trying to correct this, let’s look at the other
5293extensions of this check image.  We will use a ‘*’ as a wild-card that
5294can be 1, 2 or 3.  In the ‘THRESH*_INTERP’ extensions, you see that all
5295the blank tiles have been interpolated using their nearest neighbors
5296(the relevant option here is ‘--interpnumngb’).  In the following
5297‘THRESH*_SMOOTH’ extensions, you can see the tile values after smoothing
5298(configured with ‘--smoothwidth’ option).  Finally, in
5299‘QTHRESH-APPLIED’, you see the thresholded image: pixels with a value of
53001 will be eroded later, but pixels with a value of 2 will pass the
5301erosion step un-touched.
5302
5303   Let’s get back to the problem of optimizing the result.  You have two
5304strategies for detecting the outskirts of the merging galaxies: 1)
5305Increase the tile size to get more accurate measurements of skewness.
53062) Strengthen the outlier rejection parameters to discard more of the
5307tiles with signal.  Fortunately in this image we have a sufficiently
5308large region on the right of the image that the galaxy doesn’t extend
5309to.  So we can use the more robust first solution.  In situations where
5310this doesn’t happen (for example if the field of view in this image was
5311shifted to the left to have more of M51 and less sky) you are limited to
5312a combination of the two solutions or just to the second solution.
5313
5314*Skipping convolution for faster tests:* The slowest step of NoiseChisel
5315is the convolution of the input dataset.  Therefore when your dataset is
5316large (unlike the one in this test), and you are not changing the input
5317dataset or kernel in multiple runs (as in the tests of this tutorial),
5318it is faster to do the convolution separately once (using *note
5319Convolve::) and use NoiseChisel’s ‘--convolved’ option to directly feed
5320the convolved image and avoid convolution.  For more on ‘--convolved’,
5321see *note NoiseChisel input::.
5322
5323   To better identify the skewness caused by the flat NGC 5195 and M51
5324tidal features on the tiles under it, we have to choose a larger tile
5325size.  Let’s try a tile size of 100 by 100 pixels and inspect the check
5326image.
5327
5328     $ astnoisechisel r.fits -h0 --tilesize=100,100 --checkqthresh
5329     $ ds9 -mecube r_qthresh.fits -zscale -cmap sls -zoom to fit
5330
5331   You can clearly see the effect of this increased tile size: the tiles
5332are much larger and when you look into ‘VALUE1_NO_OUTLIER’, you see that
5333all the tiles are nicely grouped on the right side of the image (the
5334farthest from M51, where we don’t see a gradient in ‘QTHRESH_ERODE’).
5335Things look good now, so let’s remove ‘--checkqthresh’ and let
5336NoiseChisel proceed with its detection.
5337
5338     $ astnoisechisel r.fits -h0 --tilesize=100,100
5339     $ ds9 -mecube r_detected.fits -zscale -cmap sls -zoom to fit
5340
5341   The detected pixels of the ‘DETECTIONS’ extension have expanded a
5342little, but not as much.  Also, the gradient in the ‘SKY’ image is
5343almost fully removed (and doesn’t fall over M51 anymore).  However, on
5344the bottom-right of the m51 detection, we see many holes gradually
5345increasing in size.  This hints that there is still signal out there.
5346Let’s check the next series of detection steps by adding the
5347‘--checkdetection’ option this time:
5348
5349     $ astnoisechisel r.fits -h0 --tilesize=100,100 --checkdetection
5350     $ ds9 -mecube r_detcheck.fits -zscale -cmap sls -zoom to fit
5351
5352   The output now has 16 extensions, showing every step that is taken by
5353NoiseChisel.  The first and second (‘INPUT’ and ‘CONVOLVED’) are clear
5354from their names.  The third (‘THRESHOLDED’) is the thresholded image
5355after finding the quantile threshold (last extension of the output of
5356‘--checkqthresh’).  The fourth HDU (‘ERODED’) is new: its the name-stake
5357of NoiseChisel, or eroding pixels that are above the threshold.  By
5358erosion, we mean that all pixels with a value of ‘1’ (above the
5359threshold) that are touching a pixel with a value of ‘0’ (below the
5360threshold) will be flipped to zero (or “carved” out)(3).  You can see
5361its effect directly by going back and forth between the ‘THRESHOLDED’
5362and ‘ERODED’ extensions.
5363
5364   In the fifth extension (‘OPENED-AND-LABELED’) the image is “opened”,
5365which is a name for eroding once, then dilating (dilation is the inverse
5366of erosion).  This is good to remove thin connections that are only due
5367to noise.  Each separate connected group of pixels is also given its
5368unique label here.  Do you see how just beyond the large M51 detection,
5369there are many smaller detections that get smaller as you go more
5370distant?  This hints at the solution: the default number of erosions is
5371too much.  Let’s see how many erosions take place by default (by adding
5372‘-P | grep erode’ to the previous command)
5373
5374     $ astnoisechisel r.fits -h0 --tilesize=100,100 -P | grep erode
5375
5376We see that the value of ‘erode’ is ‘2’.  The default NoiseChisel
5377parameters are primarily targeted to processed images (where there is
5378correlated noise due to all the processing that has gone into the
5379warping and stacking of raw images, see *note NoiseChisel optimization
5380for detection::).  In those scenarios 2 erosions are commonly necessary.
5381But here, we have a single-exposure image where there is no correlated
5382noise (the pixels aren’t mixed).  So let’s see how things change with
5383only one erosion:
5384
5385     $ astnoisechisel r.fits -h0 --tilesize=100,100 --erode=1 \
5386                      --checkdetection
5387     $ ds9 -mecube r_detcheck.fits -zscale -cmap sls -zoom to fit
5388
5389   Looking at the ‘OPENED-AND-LABELED’ extension again, we see that the
5390main/large detection is now much larger than before.  While the
5391immediately-outer connected regions are still present, they have
5392decreased dramatically, so we can pass this step.
5393
5394   After the ‘OPENED-AND-LABELED’ extension, NoiseChisel goes onto
5395finding false detections using the undetected pixels.  The process is
5396fully described in Section 3.1.5.  (Defining and Removing False
5397Detections) of arXiv:1505.01664 (https://arxiv.org/pdf/1505.01664.pdf).
5398Please compare the extensions to what you read there and things will be
5399very clear.  In the last HDU (‘DETECTION-FINAL’), we have the final
5400detected pixels that will be used to estimate the Sky and its Standard
5401deviation.  We see that the main detection has indeed been detected very
5402far out, so let’s see how the full NoiseChisel will estimate the Sky and
5403its standard deviation (by removing ‘--checkdetection’):
5404
5405     $ astnoisechisel r.fits -h0 --tilesize=100,100 --erode=1
5406     $ ds9 -mecube r_detected.fits -zscale -cmap sls -zoom to fit
5407
5408   The ‘DETECTIONS’ extension of ‘r_detected.fits’ closely follows what
5409the ‘DETECTION-FINAL’ of the check image (looks good!).  If you go ahead
5410to the ‘SKY’ extension, things still look good.  But it can still be
5411improved.
5412
5413   Look at the ‘DETECTIONS’ again, you will see the right-ward edges of
5414M51’s detected pixels have many “holes” that are fully surrounded by
5415signal (value of ‘1’) and the signal stretches out in the noise very
5416thinly (the size of the holes increases as we go out).  This suggests
5417that there is still undetected signal and that we can still dig deeper
5418into the noise.
5419
5420   With the ‘--detgrowquant’ option, NoiseChisel will “grow” the
5421detections in to the noise.  Its value is the ultimate limit of the
5422growth in units of quantile (between 0 and 1).  Therefore
5423‘--detgrowquant=1’ means no growth and ‘--detgrowquant=0.5’ means an
5424ultimate limit of the Sky level (which is usually too much and will
5425cover the whole image!).  See Figure 2 of arXiv:1909.11230
5426(https://arxiv.org/pdf/1909.11230.pdf) for more on this option.  Try
5427running the previous command with various values (from 0.6 to higher
5428values) to see this option’s effect on this dataset.  For this
5429particularly huge galaxy (with signal that extends very gradually into
5430the noise), we’ll set it to ‘0.75’:
5431
5432     $ astnoisechisel r.fits -h0 --tilesize=100,100 --erode=1 \
5433                      --detgrowquant=0.75
5434     $ ds9 -mecube r_detected.fits -zscale -cmap sls -zoom to fit
5435
5436   Beyond this level (smaller ‘--detgrowquant’ values), you see many of
5437the smaller background galaxies (towards the right side of the image)
5438starting to create thin spider-leg-like features, showing that we are
5439following correlated noise for too much.  Please try it for your self by
5440changing it to ‘0.6’ for example.
5441
5442   When you look at the ‘DETECTIONS’ extension of the command shown
5443above, you see the wings of the galaxy being detected much farther out,
5444But you also see many holes which are clearly just caused by noise.
5445After growing the objects, NoiseChisel also allows you to fill such
5446holes when they are smaller than a certain size through the
5447‘--detgrowmaxholesize’ option.  In this case, a maximum area/size of
544810,000 pixels seems to be good:
5449
5450     $ astnoisechisel r.fits -h0 --tilesize=100,100 --erode=1 \
5451                      --detgrowquant=0.75 --detgrowmaxholesize=10000
5452     $ ds9 -mecube r_detected.fits -zscale -cmap sls -zoom to fit
5453
5454   When looking at the raw input image (which is very “shallow”: less
5455than a minute exposure!), you don’t see anything so far out of the
5456galaxy.  You might just think to yourself that “this is all noise, I
5457have just dug too deep and I’m following systematics”!  If you feel like
5458this, have a look at the deep images of this system in Watkins et al.
5459[2015] (https://arxiv.org/abs/1501.04599), or a 12 hour deep image of
5460this system (with a 12-inch telescope):
5461<https://i.redd.it/jfqgpqg0hfk11.jpg>(4).  In these deeper images you
5462clearly see how the outer edges of the M51 group follow this exact
5463structure, below in *note Achieved surface brightness level::, we’ll
5464measure the exact level.
5465
5466   As the gradient in the ‘SKY’ extension shows, and the deep images
5467cited above confirm, the galaxy’s signal extends even beyond this.  But
5468this is already far deeper than what most (if not all) other tools can
5469detect.  Therefore, we’ll stop configuring NoiseChisel at this point in
5470the tutorial and let you play with the other options a little more,
5471while reading more about it in the papers (Akhlaghi and Ichikawa [2015]
5472(https://arxiv.org/abs/1505.01664) and Akhlaghi [2019]
5473(https://arxiv.org/abs/1909.11230)) and *note NoiseChisel::.  When you
5474do find a better configuration feel free to contact us for feedback.
5475Don’t forget that good data analysis is an art, so like a sculptor,
5476master your chisel for a good result.
5477
5478   To avoid typing all these options every time you run NoiseChisel on
5479this image, you can use Gnuastro’s configuration files, see *note
5480Configuration files::.  For an applied example of setting/using them,
5481see *note Option management and configuration files::.
5482
5483*This NoiseChisel configuration is NOT GENERIC:* Don’t use the
5484configuration derived above, on another instrument’s image _blindly_.
5485If you are unsure, just use the default values.  As you saw above, the
5486reason we chose this particular configuration for NoiseChisel to detect
5487the wings of the M51 group was strongly influenced by the noise
5488properties of this particular image.  Remember *note NoiseChisel
5489optimization for detection::, where we looked into the very deep XDF
5490image which had strong correlated noise?
5491
5492   As long as your other images have similar noise properties (from the
5493same data-reduction step of the same instrument), you can use your
5494configuration on any of them.  But for images from other instruments,
5495please follow a similar logic to what was presented in these tutorials
5496to find the optimal configuration.
5497
5498*Smart NoiseChisel:* As you saw during this section, there is a clear
5499logic behind the optimal parameter value for each dataset.  Therefore,
5500we plan to capabilities to (optionally) automate some of the choices
5501made here based on the actual dataset, please join us in doing this if
5502you are interested.  However, given the many problems in existing
5503“smart” solutions, such automatic changing of the configuration may
5504cause more problems than they solve.  So even when they are implemented,
5505we would strongly recommend quality checks for a robust analysis.
5506
5507   ---------- Footnotes ----------
5508
5509   (1) by constant, we mean that it has a single value in the region we
5510are measuring.
5511
5512   (2) In processed images, where the Sky value can be over-estimated,
5513this constant shift can be negative.
5514
5515   (3) Pixels with a value of ‘2’ are very high signal-to-noise pixels,
5516they are not eroded, to preserve sharp and bright sources.
5517
5518   (4) The image is taken from this Reddit discussion:
5519<https://www.reddit.com/r/Astronomy/comments/9d6x0q/12_hours_of_exposure_on_the_whirlpool_galaxy/>
5520
5521
5522File: gnuastro.info,  Node: Image surface brightness limit,  Next: Achieved surface brightness level,  Prev: NoiseChisel optimization,  Up: Detecting large extended targets
5523
55242.3.3 Image surface brightness limit
5525------------------------------------
5526
5527In *note NoiseChisel optimization:: we showed how to customize
5528NoiseChisel for a single-exposure SDSS image of the M51 group.  When
5529presenting your detection results in a paper or scientific conference,
5530usually the first thing that someone will ask (if you don’t explicitly
5531say it!), is the dataset’s _surface brightness limit_ (a standard
5532measure of the noise level), and your target’s surface brightness (a
5533measure of the signal, either in the center or outskirts, depending on
5534context).  For more on the basics of these important concepts please see
5535*note Quantifying measurement limits::).  Here, we’ll measure these
5536values for this image.
5537
5538   Let’s start by measuring the surface brightness limit masking all the
5539detected pixels and have a look at the noise distribution with the
5540‘astarithmetic’ and ‘aststatistics’ commands below.
5541
5542     $ astarithmetic r_detected.fits -hINPUT-NO-SKY set-in \
5543                     r_detected.fits -hDETECTIONS set-det \
5544                     in det nan where -odet-masked.fits
5545     $ ds9 det-masked.fits
5546     $ aststatistics det-masked.fits
5547
5548From the ASCII histogram, we see that the distribution is roughly
5549symmetric.  We can also quantify this by measuring the skewness
5550(difference between mean and median, divided by the standard deviation):
5551
5552     $ aststatistics det-masked.fits --mean --median --std \
5553                     | awk '{print ($1-$2)/$3}'
5554
5555Showing that the mean is larger than the median by $0.08\sigma$, in
5556other words, as we saw in *note NoiseChisel optimization::, a very small
5557residual signal still remains in the undetected regions and it was up to
5558you as an exercise to improve it.  So let’s continue with this value.
5559Now, we will use the masked image and the surface brightness limit
5560equation in *note Quantifying measurement limits:: to measure the
5561$3\sigma$ surface brightness limit over an area of $25 \rm{arcsec}^2$:
5562
5563     $ nsigma=3
5564     $ zeropoint=22.5
5565     $ areaarcsec2=25
5566     $ std=$(aststatistics det-masked.fits --sigclip-std)
5567     $ pixarcsec2=$(astfits det-masked.fits --pixelscale --quiet \
5568                            | awk '{print $3*3600*3600}')
5569     $ astarithmetic --quiet $nsigma $std x \
5570                     $areaarcsec2 $pixarcsec2 x \
5571                     sqrt / $zeropoint counts-to-mag
5572     26.0241
5573
5574   The customizable steps above are good for any type of mask.  For
5575example your field of view may contain a very deep part so you need to
5576mask all the shallow parts _as well as_ the detections before these
5577steps.  But when your image is flat (like this), there is a much simpler
5578method to obtain the same value through MakeCatalog (when the standard
5579deviation image is made by NoiseChisel).  NoiseChisel has already
5580calculated the minimum (‘MINSTD’), maximum (‘MAXSTD’) and median
5581(‘MEDSTD’) standard deviation within the tiles during its processing and
5582has stored them as FITS keywords within the ‘SKY_STD’ HDU. You can see
5583them by piping all the keywords in this HDU into ‘grep’.  In Grep, each
5584‘.’ represents one character that can be anything so ‘M..STD’ will match
5585all three keywords mentioned above.
5586
5587     $ astfits r_detected.fits --hdu=SKY_STD | grep 'M..STD'
5588
5589   The ‘MEDSTD’ value is very similar to the standard deviation derived
5590above, so we can safely use it instead of having to mask and run
5591Statistics.  In fact, MakeCatalog also uses this keyword and will report
5592the dataset’s $n\sigma$ surface brightness limit as keywords in the
5593output (not as measurement columns, since its related to the noise, not
5594labeled signal):
5595
5596     $ astmkcatalog r_detected.fits -hDETECTIONS --output=sbl.fits \
5597                    --forcereadstd --ids
5598
5599Before looking into the measured surface brightness limits, let’s review
5600some important points about this call to MakeCatalog first:
5601   • We are only concerned with the noise (not the signal), so we don’t
5602     ask for any further measurements, because they can un-necessarily
5603     slow it down.  However, MakeCatalog requires at least one column,
5604     so we’ll only ask for the ‘--ids’ column (which doesn’t need any
5605     measurement!).  The output catalog will therefore have a single row
5606     and a single column, with 1 as its value(1).
5607   • If we don’t ask for any noise-related column (for example the
5608     signal-to-noise ratio column with ‘--sn’, among other noise-related
5609     columns), MakeCatalog is not going to read the noise standard
5610     deviation image (again, to speed up its operation when it is
5611     redundant).  We are thus using the ‘--forcereadstd’ option (short
5612     for “force read standard deviation image”) here so it is ready for
5613     the surface brightness limit measurements that are written as
5614     keywords.
5615
5616   With the command below you can see all the keywords that were
5617measured with the table.  Notice the group of keywords that are under
5618the “Surface brightness limit (SBL)” title.
5619
5620     $ astfits sbl.fits -h1
5621
5622Since all the keywords of interest here start with ‘SBL’, we can get a
5623more cleaner view with this command.
5624
5625     $ astfits sbl.fits -h1 | grep ^SBL
5626
5627   Notice how the ‘SBLSTD’ has the same value as NoiseChisel’s ‘MEDSTD’
5628above.  Using ‘SBLSTD’, MakeCatalog has determined the $n\sigma$ surface
5629brightness limiting magnitude in these header keywords.  The multiple of
5630$\sigma$, or $n$, is the value of the ‘SBLNSIG’ keyword which you can
5631change with the ‘--sfmagnsigma’.  The surface brightness limiting
5632magnitude within a pixel (‘SBLNSIG’) and within a pixel-agnostic area of
5633‘SBLAREA’ arcsec$^2$ are stored in ‘SBLMAG’.
5634
5635   You will notice that the two surface brightness limiting magnitudes
5636above have values around 3 and 4 (which is not correct!).  This is
5637because we haven’t given a zero point magnitude to MakeCatalog, so it
5638uses the default value of ‘0’.  SDSS image pixel values are calibrated
5639in units of “nanomaggy” which are defined to have a zero point magnitude
5640of 22.5(2).  So with the first command below we give the zero point
5641value and with the second we can see the surface brightness limiting
5642magnitudes with the correct values (around 25 and 26)
5643
5644     $ astmkcatalog r_detected.fits -hDETECTIONS --zeropoint=22.5 \
5645                    --output=sbl.fits --forcereadstd --ids
5646     $ astfits sbl.fits -h1 | grep ^SBL
5647
5648   As you see from ‘SBLNSIG’ and ‘SBLAREA’, the default multiple of
5649sigma is 1 and the default area is 1 arcsec$^2$.  Usually higher values
5650are used for these two parameters.  Following the manual example we did
5651above, you can ask for the multiple of sigma to be 3 and the area to be
565225 arcsec$^2$:
5653
5654     $ astmkcatalog r_detected.fits -hDETECTIONS --zeropoint=22.5 \
5655                    --output=sbl.fits --sfmagarea=25 --sfmagnsigma=3 \
5656                    --forcereadstd --ids
5657     $ astfits sbl.fits -h1 | awk '/^SBLMAG /{print $3}'
5658     26.02296
5659
5660   You see that the value is identical to the custom surface brightness
5661limiting magnitude we measured above (a difference of $0.00114$
5662magnitudes is negligible and hundreds of times larger than the typical
5663errors in the zero point magnitude or magnitude measurements).  But it
5664is much more easier to have MakeCatalog do this measurement, because
5665these values will be appended (as keywords) into your final catalog of
5666objects within that image.
5667
5668*Custom STD for MakeCatalog’s Surface brightness limit:* You can
5669manually change/set the value of the ‘MEDSTD’ keyword in your input STD
5670image with *note Fits:::
5671
5672     $ std=$(aststatistics masked.fits --sigclip-std)
5673     $ astfits noisechisel.fits -hSKY_STD --update=MEDSTD,$std
5674
5675   With this change, MakeCatalog will use your custom standard deviation
5676for the surface brightness limit.  This is necessary in scenarios where
5677your image has multiple depths and during your masking, you also mask
5678the shallow regions (as well as the detections of course).
5679
5680   We have successfully measured the image’s $3\sigma$ surface
5681brightness limiting magnitude over 25 arcsec$^2$.  However, as discussed
5682in *note Quantifying measurement limits:: this value is just an
5683extrapolation of the per-pixel standard deviation.  Issues like
5684correlated noise will cause the real noise over a large area to be
5685different.  So for a more robust measurement, let’s use the upper-limit
5686magnitude of similarly sized region.  For more on the upper-limit
5687magnitude, see the respective item in *note Quantifying measurement
5688limits::.
5689
5690   In summary, the upper-limit measurements involve randomly placing the
5691footprint of an object in undetected parts of the image many times.
5692This results in a random distribution of brightness measurements, the
5693standard deviation of that distribution is then converted into
5694magnitudes.  To be comparable with the results above, let’s make a
5695circular aperture that has an area of 25 arcsec$^2$ (thus with a radius
5696of $2.82095$ arcsec).
5697
5698     zeropoint=22.5
5699     r_arcsec=2.82095
5700
5701     ## Convert the radius (in arcseconds) to pixels.
5702     r_pixel=$(astfits r_detected.fits --pixelscale -q \
5703                       | awk '{print '$r_arcsec'/($1*3600)}')
5704
5705     ## Make circular aperture at pixel (100,100) position is irrelevant.
5706     echo "1 100 100 5 $r_pixel 0 0 1 1 1" \
5707          | astmkprof --background=r_detected.fits \
5708                      --clearcanvas --mforflatpix --type=uint8 \
5709                      --output=lab.fits
5710
5711     ## Do the upper-limit measurement, ignoring all NoiseChisel's
5712     ## detections as a mask for the upper-limit measurements.
5713     astmkcatalog lab.fits -h1 --zeropoint=$zeropoint -osbl.fits \
5714                  --sfmagarea=25 --sfmagnsigma=3 --forcereadstd \
5715                  --valuesfile=r_detected.fits --valueshdu=INPUT-NO-SKY \
5716                  --upmaskfile=r_detected.fits --upmaskhdu=DETECTIONS \
5717                  --upnsigma=3 --checkuplim=1 --upnum=1000 \
5718                  --ids --upperlimitsb
5719
5720   The ‘sbl.fits’ catalog now contains the upper-limit surface
5721brightness for a circle with an area of 25 arcsec$^2$.  You can check
5722the value with the command below, but the great thing is that now you
5723have both the surface brightness limiting magnitude in the headers
5724discussed above, and the upper-limit surface brightness within the
5725table.  You can also add more profiles with different shapes and sizes
5726if necessary.  Of course, you can also use ‘--upperlimitsb’ in your
5727actual science objects and clumps to get an object-specific or
5728clump-specific value.
5729
5730     $ asttable sbl.fits -cUPPERLIMIT_SB
5731     25.9119
5732
5733You will get a slightly different value from the command above.  In
5734fact, if you run the MakeCatalog command again and look at the measured
5735upper-limit surface brightness, it will be slightly different with your
5736first trial!  Please try exactly the same MakeCatalog command above a
5737few times to see how it changes.
5738
5739   This is because of the _random_ factor in the upper-limit
5740measurements: every time you run it, different random points will be
5741checked, resulting in a slightly different distribution.  You can
5742decrease the random scatter by increasing the number of random checks
5743(for example setting ‘--upnum=100000’, compared to 1000 in the command
5744above).  But this will be slower and the results won’t be exactly
5745reproducible.  The only way to ensure you get an identical result later
5746is to fix the random number generator function and seed like the command
5747below(3).  This is a very important point regarding any statistical
5748process involving random numbers, please see *note Generating random
5749numbers::.
5750
5751     export GSL_RNG_TYPE=ranlxs1
5752     export GSL_RNG_SEED=1616493518
5753     astmkcatalog lab.fits -h1 --zeropoint=$zeropoint -osbl.fits \
5754                  --sfmagarea=25 --sfmagnsigma=3 --forcereadstd \
5755                  --valuesfile=r_detected.fits --valueshdu=INPUT-NO-SKY \
5756                  --upmaskfile=r_detected.fits --upmaskhdu=DETECTIONS \
5757                  --upnsigma=3 --checkuplim=1 --upnum=1000 \
5758                  --ids --upperlimitsb --envseed
5759
5760   But where do all the random apertures of the upper-limit measurement
5761fall on the image?  It is good to actually inspect their location to get
5762a better understanding for the process and also detect possible
5763bugs/biases.  When MakeCatalog is run with the ‘--checkuplim’ option, it
5764will print all the random locations and their measured brightness as a
5765table in a file with the suffix ‘_upcheck.fits’.  With the first command
5766below you can use Gnuastro’s ‘asttable’ and ‘astscript-ds9-region’ to
5767convert the successful aperture locations into a DS9 region file, and
5768with the second can load the region file into the detections and
5769sky-subtracted image to visually see where they are.
5770
5771     ## Create a DS9 region file from the check table (activated
5772     ## with '--checkuplim')
5773     asttable lab_upcheck.fits --noblank=RANDOM_SUM \
5774              | astscript-ds9-region -c1,2 --mode=img \
5775                                     --radius=$r_pixel
5776
5777     ## Have a look at the regions in relation with NoiseChisel's
5778     ## detections.
5779     ds9 r_detected.fits[INPUT-NO-SKY] -regions load ds9.reg
5780     ds9 r_detected.fits[DETECTIONS] -regions load ds9.reg
5781
5782   In this example, we were looking at a single-exposure image that has
5783no correlated noise.  Because of this, the surface brightness limit and
5784the upper-limit surface brightness are very close.  They will have a
5785bigger difference on deep datasets with stronger correlated noise (that
5786are the result of stacking many individual exposures).  As an exercise,
5787please try measuring the upper-limit surface brightness level and
5788surface brightness limit for the deep HST data that we used in the
5789previous tutorial (*note General program usage tutorial::).
5790
5791   ---------- Footnotes ----------
5792
5793   (1) Recall that NoiseChisel’s output is a binary image: 0-valued
5794pixels are noise and 1-valued pixel are signal.  NoiseChisel doesn’t
5795identify sub-structure over the signal, this is the job of Segment, see
5796*note Extract clumps and objects::.
5797
5798   (2) From <https://www.sdss.org/dr12/algorithms/magnitudes>
5799
5800   (3) You can use any integer for the seed.  One recommendation is to
5801run MakeCatalog without ‘--envseed’ once and use the randomly generated
5802seed that is printed on the terminal.
5803
5804
5805File: gnuastro.info,  Node: Achieved surface brightness level,  Next: Extract clumps and objects,  Prev: Image surface brightness limit,  Up: Detecting large extended targets
5806
58072.3.4 Achieved surface brightness level
5808---------------------------------------
5809
5810In *note NoiseChisel optimization:: we customized NoiseChisel for a
5811single-exposure SDSS image of the M51 group and in *note Image surface
5812brightness limit:: we measured the surface brightness limit and the
5813upper-limit surface brightness level (which are both measures of the
5814noise level).  In this section, let’s do some measurements on the
5815outer-most edges of the M51 group to see how they relate to the noise
5816measurements found in the previous section.
5817
5818   For this measurement, we’ll need to estimate the average flux on the
5819outer edges of the detection.  Fortunately all this can be done with a
5820few simple commands using *note Arithmetic:: and *note MakeCatalog::.
5821First, let’s separate each detected region, or give a unique
5822label/counter to all the connected pixels of NoiseChisel’s detection map
5823with the command below.  Recall that with the ‘set-’ operator, the
5824popped operand will be given a name (‘det’ in this case) for easy usage
5825later.
5826
5827     $ astarithmetic r_detected.fits -hDETECTIONS set-det \
5828                     det 2 connected-components -olabeled.fits
5829
5830   You can find the label of the main galaxy visually (by opening the
5831image and hovering your mouse over the M51 group’s label).  But to have
5832a little more fun, let’s do this automatically (which is necessary in a
5833general scenario).  The M51 group detection is by far the largest
5834detection in this image, this allows us to find its ID/label easily.
5835We’ll first run MakeCatalog to find the area of all the labels, then
5836we’ll use Table to find the ID of the largest object and keep it as a
5837shell variable (‘id’):
5838
5839     # Run MakeCatalog to find the area of each label.
5840     $ astmkcatalog labeled.fits --ids --geoarea -h1 -ocat.fits
5841
5842     ## Sort the table by the area column.
5843     $ asttable cat.fits --sort=AREA_FULL
5844
5845     ## The largest object, is the last one, so we'll use '--tail'.
5846     $ asttable cat.fits --sort=AREA_FULL --tail=1
5847
5848     ## We only want the ID, so let's only ask for that column:
5849     $ asttable cat.fits --sort=AREA_FULL --tail=1 --column=OBJ_ID
5850
5851     ## Now, let's put this result in a variable (instead of printing)
5852     $ id=$(asttable cat.fits --sort=AREA_FULL --tail=1 --column=OBJ_ID)
5853
5854     ## Just to confirm everything is fine.
5855     $ echo $id
5856
5857We can now use the ‘id’ variable to reject all other detections:
5858
5859     $ astarithmetic labeled.fits $id eq -oonly-m51.fits
5860
5861   Open the image and have a look.  To separate the outer edges of the
5862detections, we’ll need to “erode” the M51 group detection.  So in the
5863same Arithmetic command as above, we’ll erode three times (to have more
5864pixels and thus less scatter), using a maximum connectivity of 2
5865(8-connected neighbors).  We’ll then save the output in ‘eroded.fits’.
5866
5867     $ astarithmetic labeled.fits $id eq 2 erode 2 erode 2 erode \
5868                     -oeroded.fits
5869
5870In ‘labeled.fits’, we can now set all the 1-valued pixels of
5871eroded.fits’ to 0 using Arithmetic’s ‘where’ operator added to the
5872previous command.  We’ll need the pixels of the M51 group in
5873labeled.fits’ two times: once to do the erosion, another time to find
5874the outer pixel layer.  To do this (and be efficient and more readable)
5875we’ll use the ‘set-i’ operator (to give this image the name ‘‘i’’).  In
5876this way we can use it any number of times afterwards, while only
5877reading it from disk and finding M51’s pixels once.
5878
5879     $ astarithmetic labeled.fits $id eq set-i i \
5880                     i 2 erode 2 erode 2 erode 0 where -oedge.fits
5881
5882   Open the image and have a look.  You’ll see that the detected edge of
5883the M51 group is now clearly visible.  You can use ‘edge.fits’ to mark
5884(set to blank) this boundary on the input image and get a visual feeling
5885of how far it extends:
5886
5887     $ astarithmetic r.fits -h0 edge.fits nan where -oedge-masked.fits
5888
5889   To quantify how deep we have detected the low-surface brightness
5890regions (in units of signal to-noise ratio), we’ll use the command
5891below.  In short it just divides all the non-zero pixels of ‘edge.fits5892in the Sky subtracted input (first extension of NoiseChisel’s output) by
5893the pixel standard deviation of the same pixel.  This will give us a
5894signal-to-noise ratio image.  The mean value of this image shows the
5895level of surface brightness that we have achieved.  You can also break
5896the command below into multiple calls to Arithmetic and create temporary
5897files to understand it better.  However, if you have a look at *note
5898Reverse polish notation:: and *note Arithmetic operators::, you should
5899be able to easily understand what your computer does when you run this
5900command(1).
5901
5902     $ astarithmetic edge.fits -h1                  set-edge \
5903                     r_detected.fits -hSKY_STD      set-skystd \
5904                     r_detected.fits -hINPUT-NO-SKY set-skysub \
5905                     skysub skystd / edge not nan where meanvalue --quiet
5906
5907   We have thus detected the wings of the M51 group down to roughly
59081/3rd of the noise level in this image which is a very good achievement!
5909But the per-pixel S/N is a relative measurement.  Let’s also measure the
5910depth of our detection in absolute surface brightness units; or
5911magnitudes per square arc-seconds (see *note Brightness flux
5912magnitude::).  We’ll also ask for the S/N and magnitude of the full edge
5913we have defined.  Fortunately doing this is very easy with Gnuastro’s
5914MakeCatalog:
5915
5916     $ astmkcatalog edge.fits -h1 --valuesfile=r_detected.fits \
5917                    --zeropoint=22.5 --ids --surfacebrightness --sn \
5918                    --magnitude
5919     $ asttable edge_cat.fits
5920     1      25.6971       55.2406       15.8994
5921
5922   We have thus reached an outer surface brightness of $25.70$
5923magnitudes/arcsec$^2$ (second column in ‘edge_cat.fits’) on this single
5924exposure SDSS image!  This is very similar to the surface brightness
5925limit measured in *note Image surface brightness limit:: (which is a big
5926achievement!).  But another point in the result above is very
5927interesting: the total S/N of the edge is $55.24$ with a total edge
5928magnitude(2) of 15.90!!!  This very large for such a faint signal
5929(recall that the mean S/N per pixel was 0.32) and shows a very important
5930point in the study of galaxies: While the per-pixel signal in their
5931outer edges may be very faint (and invisible to the eye in noise), a lot
5932of signal hides deeply buried in the noise.
5933
5934   In interpreting this value, you should just have in mind that
5935NoiseChisel works based on the contiguity of signal in the pixels.
5936Therefore the larger the object, the deeper NoiseChisel can carve it out
5937of the noise (for the same outer surface brightness).  In other words,
5938this reported depth, is the depth we have reached for this object in
5939this dataset, processed with this particular NoiseChisel configuration.
5940If the M51 group in this image was larger/smaller than this (the field
5941of view was smaller/larger), or if the image was from a different
5942instrument, or if we had used a different configuration, we would go
5943deeper/shallower.
5944
5945   ---------- Footnotes ----------
5946
5947   (1) ‘edge.fits’ (extension ‘1’) is a binary (0 or 1 valued) image.
5948Applying the ‘not’ operator on it, just flips all its pixels (from ‘0’
5949to ‘1’ and vice-versa).  Using the ‘where’ operator, we are then setting
5950all the newly 1-valued pixels (pixels that aren’t on the edge) to
5951NaN/blank in the sky-subtracted input image (‘r_detected.fits’,
5952extension ‘INPUT-NO-SKY’, which we call ‘skysub’).  We are then dividing
5953all the non-blank pixels (only those on the edge) by the sky standard
5954deviation (‘r_detected.fits’, extension ‘SKY_STD’, which we called
5955‘skystd’).  This gives the signal-to-noise ratio (S/N) for each of the
5956pixels on the boundary.  Finally, with the ‘meanvalue’ operator, we are
5957taking the mean value of all the non-blank pixels and reporting that as
5958a single number.
5959
5960   (2) You can run MakeCatalog on ‘only-m51.fits’ instead of ‘edge.fits5961to see the full magnitude of the M51 group in this image.
5962
5963