"Fossies" - the Fresh Open Source Software Archive

Member "gnuastro-0.8/bin/mknoise/mknoise.c" (4 Nov 2018, 4386 Bytes) of package /linux/privat/gnuastro-0.8.tar.lz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "mknoise.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 0.7_vs_0.8.

    1 /*********************************************************************
    2 MakeNoise - Add noise to a dataset.
    3 MakeNoise is part of GNU Astronomy Utilities (Gnuastro) package.
    4 
    5 Original author:
    6      Mohammad Akhlaghi <mohammad@akhlaghi.org>
    7 Contributing author(s):
    8 Copyright (C) 2015-2018, Free Software Foundation, Inc.
    9 
   10 Gnuastro is free software: you can redistribute it and/or modify it
   11 under the terms of the GNU General Public License as published by the
   12 Free Software Foundation, either version 3 of the License, or (at your
   13 option) any later version.
   14 
   15 Gnuastro is distributed in the hope that it will be useful, but
   16 WITHOUT ANY WARRANTY; without even the implied warranty of
   17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   18 General Public License for more details.
   19 
   20 You should have received a copy of the GNU General Public License
   21 along with Gnuastro. If not, see <http://www.gnu.org/licenses/>.
   22 **********************************************************************/
   23 #include <config.h>
   24 
   25 #include <math.h>
   26 #include <stdio.h>
   27 #include <errno.h>
   28 #include <error.h>
   29 #include <string.h>
   30 #include <stdlib.h>
   31 #include <sys/time.h>            /* Generate random seed. */
   32 
   33 #include <gsl/gsl_rng.h>         /* Used in setrandoms.   */
   34 #include <gnuastro/fits.h>
   35 #include <gsl/gsl_randist.h>     /* To make noise.        */
   36 
   37 #include <gnuastro-internal/timing.h>
   38 
   39 #include "main.h"
   40 
   41 
   42 
   43 
   44 
   45 
   46 
   47 
   48 
   49 
   50 
   51 
   52 
   53 
   54 void
   55 convertsaveoutput(struct mknoiseparams *p)
   56 {
   57   char keyname1[FLEN_KEYWORD];
   58   gal_fits_list_key_t *headers=NULL;
   59   char keyname2[FLEN_KEYWORD], keyname3[FLEN_KEYWORD];
   60   char keyname4[FLEN_KEYWORD], keyname5[FLEN_KEYWORD];
   61 
   62 
   63   /* Add the proper information to the header of the output: */
   64   gal_fits_key_write_filename("INF", p->inputname, &headers, 0);
   65   if( !isnan(p->background_mag) )
   66     {
   67       strcpy(keyname1, "BCKGRND");
   68       gal_fits_key_list_add_end(&headers, GAL_TYPE_FLOAT64, keyname1, 0,
   69                                 &p->background_mag, 0, "Background "
   70                                 "value (in magnitude) for noise.",
   71                                 0, NULL);
   72       strcpy(keyname2, "BZRPNT");
   73       gal_fits_key_list_add_end(&headers, GAL_TYPE_FLOAT64, keyname2, 0,
   74                                 &p->zeropoint, 0,
   75                                 "Zeropoint magnitude of image.", 0, NULL);
   76       strcpy(keyname3, "INSTRU");
   77       gal_fits_key_list_add_end(&headers, GAL_TYPE_FLOAT64, keyname3, 0,
   78                                 &p->instrumental, 0,
   79                                 "Instrumental noise in units of flux.",
   80                                 0, NULL);
   81     }
   82   else
   83     {
   84       strcpy(keyname1, "SIGMA");
   85       gal_fits_key_list_add_end(&headers, GAL_TYPE_FLOAT64, keyname1, 0,
   86                                 &p->sigma, 0, "Total noise sigma", 0, NULL);
   87     }
   88   strcpy(keyname4, "RNGTYPE");
   89   gal_fits_key_list_add_end(&headers, GAL_TYPE_STRING, keyname4, 0,
   90                             (void *)(p->rng_name), 0,
   91                             "Random number generator (by GSL) type.",
   92                             0, NULL);
   93   strcpy(keyname5, "RNGSEED");
   94   gal_fits_key_list_add_end(&headers, GAL_TYPE_ULONG, keyname5, 0,
   95                             &p->rng_seed, 0,
   96                             "Random number generator (by GSL) seed.",
   97                             0, NULL);
   98 
   99   /* Save the output: */
  100   p->input=gal_data_copy_to_new_type_free(p->input, p->cp.type);
  101   p->input->name="NOISED";
  102   gal_fits_img_write(p->input, p->cp.output, headers, PROGRAM_NAME);
  103   p->input->name=NULL;
  104 
  105   /* Write the configuration keywords. */
  106   gal_fits_key_write_filename("input", p->inputname, &p->cp.okeys, 1);
  107   gal_fits_key_write_config(&p->cp.okeys, "MakeNoise configuration",
  108                             "MKNOISE-CONFIG", p->cp.output, "0");
  109 }
  110 
  111 
  112 
  113 
  114 
  115 void
  116 mknoise(struct mknoiseparams *p)
  117 {
  118   double *d, *df, background=p->background;
  119   double instpowtwo = p->instrumental*p->instrumental;
  120 
  121   /* Add the noise: */
  122   df=(d=p->input->array)+p->input->size;
  123   if( !isnan(p->sigma) )
  124     {
  125       do
  126         *d += gsl_ran_gaussian(p->rng, p->sigma);
  127       while(++d<df);
  128     }
  129   else
  130     {
  131       do
  132         *d += ( background
  133                 + gsl_ran_gaussian(p->rng,
  134                                    sqrt( instpowtwo + background + *d )) );
  135       while(++d<df);
  136     }
  137 
  138   /* Convert and save the output in the proper format: */
  139   convertsaveoutput(p);
  140 }