GLAMERDOC++
Gravitational Lensing Code Library
image_processing.h
1 /*
2  * image_processing.h
3  *
4  * Created on: Feb 28, 2010
5  * Author: R.B. Metcalf
6  */
7 
8 #ifndef IMAGE_PROCESSING_H_
9 #define IMAGE_PROCESSING_H_
10 
11 #include <complex>
12 #include <vector>
13 #include <tuple>
14 
15 #include "point.h"
16 #include "image_info.h"
17 //#include "Tree.h"
18 //#include "utilities_slsim.h"
19 //#include "image_processing.h"
20 #include "source.h"
21 
22 #ifdef ENABLE_FFTW
23 #include "fftw3.h"
24 #endif
25 
26 // forward declaration
27 struct Grid;
28 struct GridMap;
29 //class Source;
30 
32 enum class PixelMapUnits {
33  ndef // not defined
34  ,surfb // ergs / s / cm**2
35  ,count_per_sec
36  ,ADU // Analogue-to-Digital Units
37  ,mass
38  ,mass_density
39 };
40 
41 std::string to_string(PixelMapUnits unit);
42 
47 class PixelMap
48 {
49 public:
50  PixelMap(const PixelMap& pmap, double res_ratio);
51  PixelMap();
52  PixelMap(const PixelMap& other);
53  PixelMap(PixelMap&& other);
54  PixelMap(const PixelMap& pmap, const double* center, std::size_t Npixels);
55  PixelMap(const PixelMap& pmap,long nx,long ny, std::size_t Npixels);
56  PixelMap(const double* center, std::size_t Npixels, double resolution,PixelMapUnits u = PixelMapUnits::ndef);
57  PixelMap(const double* center, std::size_t Nx, std::size_t Ny, double resolution,PixelMapUnits u = PixelMapUnits::ndef);
58  PixelMap(std::string fitsfilename
59  ,double resolution = -1,PixelMapUnits u = PixelMapUnits::ndef);
60  ~PixelMap(){
61  map.resize(0);
62  };
63 
64  PixelMap& operator=(const PixelMap &other);
65  PixelMap& operator=(PixelMap &&other);
66 
67  void ChangeUnits(PixelMapUnits u){units=u;}
68  PixelMapUnits getUnits() const {return units;}
69 
70  inline bool valid() const { return map.size(); };
71  inline std::size_t size() const { return map.size(); };
72 
73  inline std::size_t getNx() const { return Nx; }
74  inline std::size_t getNy() const { return Ny; }
75  inline double getRangeX() const { return rangeX; }
76  inline double getRangeY() const { return rangeY; }
77  //inline double* getCenter() const{ return &center[0]; }
78  void const getCenter(Point_2d &c) const{ c[0]=center[0]; c[1]=center[1];}
79  Point_2d getCenter() const{
80  Point_2d c(center[0],center[1]);
81  return c;
82  }
83  inline double getResolution() const { return resolution; }
84 
86  double getRA(){return RA;}
88  double getDEC(){return DEC;}
89 
91  void setRAandDec(double RAin,double DECin){
92  RA = RAin;
93  DEC = DECin;
94  }
95 
96  // Zero the whole map
97  void Clean(){for(auto &a : map) a = 0;}
98 
99  void AddImages(ImageInfo *imageinfo,int Nimages,float rescale = 1.);
100  void AddImages(std::vector<ImageInfo> &imageinfo,int Nimages,float rescale = 1.);
102  void AddGridBrightness(Grid &grid);
104  void AddGridMapBrightness(const GridMap &grid);
105  void AddUniformImages(ImageInfo *imageinfo,int Nimages,double value);
106  PosType AddSource(Source &source);
108  PosType AddSource(Source &source,int oversample);
109  void AddPointSource(const Point_2d &x,double flux);
110 
117  void copy_in(const PixelMap& pmap);
118 
127  void paste(const PixelMap& pmap);
128 
130  void paste(const PixelMap& pmap,long nx,long ny);
131 
136  void duplicate(const PixelMap& pmap);
137 
139  template <typename T>
140  PosType AddSource_parallel(T &source,int oversample){
141  Point_2d s_center;
142  source.getTheta(s_center);
143 
144  if( (s_center[0] + source.getRadius() ) < map_boundary_p1[0] ) return 0.0;
145  if( (s_center[0] - source.getRadius() ) > map_boundary_p2[0] ) return 0.0;
146  if( (s_center[1] + source.getRadius() ) < map_boundary_p1[1] ) return 0.0;
147  if( (s_center[1] - source.getRadius() ) > map_boundary_p2[1] ) return 0.0;
148 
149  int nthreads = Utilities::GetNThreads();
150  PosType totals[nthreads];
151  std::vector<std::thread> thr;
152 
153  size_t block = map.size()/nthreads;
154  for(int i = 0; i < nthreads ;++i){
155  thr.push_back(std::thread(&PixelMap::addsource_<T>,this
156  ,i*block,std::min((i+1)*block-1,map.size()-1)
157  ,oversample,std::ref(source)
158  ,std::ref(totals[i])));
159  }
160 
161  for(int ii=0;ii < nthreads;++ii) thr[ii].join();
162 
163  PosType total =0;
164  for(int ii=0;ii < nthreads;++ii) total += totals[ii];
165 
166  return total;
167  }
168 
169  void AddCurve(ImageInfo *curve,double value);
170  void AddCurve(Kist<Point> *imagekist,PosType value);
171  void AddCurve(std::vector<Point_2d> &curve,double value);
172  void AddCurve(std::vector<RAY> &curve,double value);
173 
175  void drawline(double x1[],double x2[],double value,bool add);
177  void DrawLine(long x0,long x1,long y0,long y1,double value,bool add);
178  void DrawLineGS(long x0,long x1,long y0,long y1,double value,bool add);
179  void drawcircle(PosType r_center[],PosType radius,PosType value);
180  void drawdisk(PosType r_center[],PosType radius,PosType value,int Nstrip);
181  void AddGrid(const Grid &grid,double value = 1.0);
182  void AddGrid(const Grid &grid,LensingVariable val);
183 
184  void Renormalize(double factor);
185  void AddValue(std::size_t i, double value);
186  void AssignValue(std::size_t i, double value);
187  void printASCII() const;
188  void printASCIItoFile(std::string filename) const;
189  void printFITS(std::string filename,bool Xflip = false, bool verbose = false);
190  void printFITS(std::string filename,std::vector< std::tuple<std::string,double,std::string> > &extra_header_info, bool verbose);
191 
193  void printFITS(std::string filename
194  ,std::vector<std::string> &headercards
195  );
196 
197  void smooth(double sigma);
198 
199  inline double getValue(std::size_t i) const { return map[i]; }
200  inline double & operator[](std::size_t i) { return map[i]; };
201  double operator[](std::size_t i) const { return map[i]; };
202  inline double operator()(std::size_t i) const { return map[i]; };
203  inline double operator()(std::size_t i,std::size_t j) const { return map[i + Nx*j]; };
204  inline double & operator()(std::size_t i,std::size_t j) { return map[i + Nx*j]; };
205 
206  PixelMap& operator+=(const PixelMap& rhs);
207  void operator+=(double f){map +=f;};
208  //friend PixelMap operator+(const PixelMap&, const PixelMap&);
209  PixelMap operator+(const PixelMap&) const;
210 
211  PixelMap& operator-=(const PixelMap& rhs);
212  //friend PixelMap operator-(const PixelMap&, const PixelMap&);
213  PixelMap operator-(const PixelMap&) const;
214 
215  PixelMap& operator*=(const PixelMap& rhs);
216  //friend PixelMap operator*(const PixelMap&, const PixelMap&);
217  PixelMap operator*(const PixelMap& a) const;
218  PixelMap operator/(const PixelMap& a) const;
219 
220  PixelMap& operator*=(PosType b);
221  PixelMap operator*(PosType b) const;
222 
223  std::valarray<double>& data() { return map; }
224 
226  bool agrees(const PixelMap& other) const;
227 
228  friend void swap(PixelMap&, PixelMap&);
229  static void swap(PixelMap&, PixelMap&);
230 
232  PosType ave() const {return map.sum()/map.size();}
234  PosType sum() const {return map.sum();};
236  size_t size(){return map.size();}
237  double max() const{ return map.max(); }
238  double min() const{ return map.min(); }
239 
240  void FindArc(PosType &radius,PosType *xc,PosType *arc_center,PosType &arclength,PosType &width
241  ,PosType threshold);
242 
244  long find_index(PosType const x[],long &ix,long &iy) const;
246  long find_index(PosType const x[]) const;
247 
249  long find_index(PosType x,PosType y,long &ix,long &iy) const;
251  long find_index(PosType x,PosType y) const;
252 
254  void find_position(PosType x[],std::size_t const index) const;
256  void find_position(PosType x[],std::size_t const ix,std::size_t const iy) const;
257 
260  PosType theta
261  ,PosType scale=1
262  );
263 
265  PosType linear_interpolate(PosType x[]);
266 
268  void drawgrid(int N,PosType value);
269  void drawPoints(std::vector<Point *> points,PosType size,PosType value);
270 
271  void drawPoints(std::vector<Point> points,PosType size,PosType value);
272 
273  void drawCurve(std::vector<Point *> points,PosType value){
274  for(int i=0;i<points.size()-1;++i) drawline(points[i]->x,points[i+1]->x,value,false);
275  }
276  void drawCurve(std::vector<Point> points,PosType value){
277  for(int i=0;i<points.size()-1;++i) drawline(points[i].x,points[i+1].x,value,false);
278  }
279  void drawPoints(std::vector<Point_2d> points,PosType size,PosType value);
280  void drawCurve(std::vector<Point_2d> points,PosType value){
281  for(int i=0;i<points.size()-1;++i) drawline(points[i].x,points[i+1].x,value,false);
282  }
284  void drawSquare(PosType p1[],PosType p2[],PosType value);
285  void drawBox(PosType p1[],PosType p2[],PosType value,int Nstrip);
286 
288  void PowerSpectrum(std::vector<PosType> &power_spectrum
289  ,std::vector<PosType> &lvec
290  ,bool overwrite = true
291  ){
292 
293  if(power_spectrum.size() != lvec.size()) throw std::invalid_argument("these must be the same size");
294 
295  if(overwrite){
296  Utilities::powerspectrum2d(map,Nx,Ny,rangeX,rangeY, lvec, power_spectrum);
297  }else{
298  std::vector<PosType> tmp_power(power_spectrum.size());
299  Utilities::powerspectrum2d(map,Nx,Ny,rangeX,rangeY,lvec,tmp_power);
300  for(size_t ii=0;ii<power_spectrum.size();++ii) power_spectrum[ii] += tmp_power[ii];
301  }
302  }
303 
305  void PowerSpectrum(std::vector<PosType> &power_spectrum
306  ,const std::vector<PosType> &lbins
307  ,std::vector<PosType> &lave
308  ,bool overwrite = true
309  ){
310 
311  if(overwrite){
312  Utilities::powerspectrum2dprebin(map,Nx,Ny,rangeX,rangeY,lbins,power_spectrum,lave);
313  }else{
314  if(power_spectrum.size() != lbins.size()-1) throw std::invalid_argument("these must be the same size");
315  std::vector<PosType> tmp_power(power_spectrum.size());
316  Utilities::powerspectrum2dprebin(map,Nx,Ny,rangeX,rangeY,lbins,tmp_power,lave);
317  for(size_t ii=0;ii<power_spectrum.size();++ii) power_spectrum[ii] += tmp_power[ii];
318  }
319  }
320 
321  void AdaptiveSmooth(PosType value){
322  std::valarray<double> tmp = Utilities::AdaptiveSmooth(data(),Nx,Ny,value);
323  map = tmp;
324  }
325 
327  void find_contour(double level
328  ,std::vector<std::vector<Point_2d> > &points
329  ,std::vector<bool> &hits_edge
330  ) const;
337  void find_islands_holes(double level,
338  std::vector<std::vector<size_t> > &points
339  ) const;
340 
353  void lens_definition(double min_sn_per_image
354  ,double pixel_threshold
355  ,int &Nimages
356  ,double &total_sig_noise_source
357  ,std::vector<size_t> &maxima_indexes
358  ,std::vector<std::vector<size_t> > &image_points
359  ,bool &lens_TF
360  ,double &level
361  ,size_t &n_pix_in_source
362  ,bool verbose = false
363  );
364 
365 
367  std::vector<size_t> maxima(double minlevel) const;
368 
375  //int count_islands(std::list<size_t> &pixel_index,std::vector<std::list<size_t>::iterator> &heads) const;
376  int count_islands(std::vector<size_t> &pixel_index) const;
377 
379  size_t threshold(std::list<size_t> &pixel_index,PosType value){
380  for(size_t i=0;i<map.size();++i) if(value < map[i]) pixel_index.push_back(i);
381  return pixel_index.size();
382  }
383 
385  void flipY(){
386  for(size_t i=0;i<Nx;++i){
387  for(size_t j=0;j<(Ny-1)/2;++j){
388  std::swap( map[i + Nx*j],map[i + Nx*(Ny-j-1)] );
389  }
390  }
391  }
393  void flipX(){
394  for(size_t i=0;i<(Nx-1)/2;++i){
395  for(size_t j=0;j<Ny;++j){
396  std::swap( map[i + Nx*j],map[Nx-i-1 + Nx*j] );
397  }
398  }
399  }
401  void doubleFlip(){
402  flipX();
403  flipY();
404  }
405 
407 
408  void recenter(PosType newcenter[2]
409  );
410  void recenter(Point_2d newcenter
411  );
412 
418  void convolve(PixelMap &kernel,long center_x = 0,long center_y = 0);
419 
424  PixelMap downsize(int n
425  );
426 
427 
429  void addheader(std::string label,long value,std::string comment){
430  bool found = false;
431  for(auto &a : headers_long){
432  if(std::get<0>(a) == label){
433  std::get<1>(a)=value;
434  std::get<2>(a)=comment;
435  found = true;
436  break;
437  }
438  }
439  if(!found) headers_long.push_back(std::make_tuple(label,value,comment));
440  }
441  void addheader(std::string label,size_t value,std::string comment){
442  bool found = false;
443  for(auto &a : headers_long){
444  if(std::get<0>(a) == label){
445  std::get<1>(a)=value;
446  std::get<2>(a)=comment;
447  found = true;
448  break;
449  }
450  }
451  if(!found) headers_long.push_back(std::make_tuple(label,value,comment));
452  }
453  void addheader(std::string label,float value,std::string comment){
454  bool found = false;
455  for(auto &a : headers_float){
456  if(std::get<0>(a) == label){
457  std::get<1>(a)=value;
458  std::get<2>(a)=comment;
459  found=true;
460  break;
461  }
462  }
463  if(!found) headers_float.push_back(std::make_tuple(label,value,comment));
464  }
465  void addheader(std::string label,double value,std::string comment){
466  bool found = false;
467  for(auto &a : headers_float){
468  if(std::get<0>(a) == label){
469  std::get<1>(a)=value;
470  std::get<2>(a)=comment;
471  found=true;
472  break;
473  }
474  }
475  if(!found) headers_float.push_back(std::make_tuple(label,value,comment));
476  }
477  void addheader(std::string label,std::string value,std::string comment){
478  bool found = false;
479  for(auto &a : headers_string){
480  if(std::get<0>(a) == label){
481  std::get<1>(a)=value;
482  std::get<2>(a)=comment;
483  found=true;
484  break;
485  }
486  }
487  if(!found) headers_string.push_back(std::make_tuple(label,value,comment));
488  }
489 
490 private:
491  std::vector<std::tuple<std::string,float,std::string> > headers_float;
492  std::vector<std::tuple<std::string,long,std::string> > headers_long;
493  std::vector<std::tuple<std::string,std::string,std::string> > headers_string;
494 
495  std::valarray<double> map;
496  long Nx;
497  long Ny;
498  double resolution,rangeX,rangeY,center[2];
499  double RA=0,DEC=0; // optional coordinates of center
500  double map_boundary_p1[2],map_boundary_p2[2];
501  PixelMapUnits units= PixelMapUnits::ndef;
502 
503  void AddGrid_(const PointList &list,LensingVariable val);
504 
505  double LeafPixelArea(IndexType i,Branch * branch1);
506  void PointsWithinLeaf(Branch * branch1, std::list <unsigned long> &neighborlist);
507  bool inMapBox(Branch * branch1) const;
508  bool inMapBox(double * branch1) const;
509 
511  bool pixels_are_neighbors(size_t i,size_t j) const;
515  void _count_islands_(size_t current,std::list<size_t> &reservoir
516  ,std::list<size_t>::iterator &group) const;
517 
518  //void addsource_(size_t i1,size_t i2,int oversample,Source source,PosType &total);
519  //void addsource_(size_t i1,size_t i2,int oversample,Po,
520  // PosType &total);
521 
522  template <typename T>
523  void addsource_(size_t i1,size_t i2,int oversample,
524  T &source,
525  PosType &total){
526  PosType tmp_res = resolution*1.0/oversample;
527  PosType tmp = tmp_res*tmp_res;
528  PosType bl = resolution /2 - 0.5*tmp_res;
529  PosType y[2],x[2];
530 
531  total = 0;
532 
533  for(size_t index = i1 ;index <= i2; ++index){
534  find_position(y,index);
535  y[0] -= bl;
536  y[1] -= bl;
537  for(int i = 0 ; i < oversample ; ++i){
538  x[0] = y[0] + i*tmp_res;
539  for(int j=0; j < oversample;++j){
540  x[1] = y[1] + j*tmp_res;
541  map[index] += source.SurfaceBrightness(x)*tmp;
542  total += source.SurfaceBrightness(x)*tmp;
543  }
544  }
545  }
546  }
547 
548  // find if pixel k is in the curve which must be in pixel units, meant to be used in conjunction with Utilities::find_boundaries<>
549  bool incurve(long k,std::vector<Point_2d> &curve) const;
550 };
551 
552 enum class Telescope {Euclid_VIS,Euclid_Y,Euclid_J,Euclid_H,KiDS_u,KiDS_g,KiDS_r,KiDS_i,HST_ACS_I,CFHT_u,CFHT_g,CFHT_r,CFHT_i,CFHT_z};
553 
554 enum class UnitType {counts_x_sec, flux} ;
555 
556 
557 class Obs{
558 public:
559 
560  Obs(size_t Npix_xx,size_t Npix_yy
561  ,double pix_size
562  ,int oversample
563  ,float seeing = 0 // seeing in arcsec
564  );
565 
566  virtual ~Obs(){};
567 
568  size_t getNxInput() const { return Npix_x_input;}
569  size_t getNyInput() const { return Npix_y_input;}
570 
571  size_t getNxOutput() const { return Npix_x_output;}
572  size_t getNyOutput() const { return Npix_y_output;}
573 
574  std::valarray<double> getPSF(){return map_psf;}
575  //void setPSF(std::string psf_file);
576  void setPSF(std::string psf_file,double resolution=0);
577  void setPSF(PixelMap &psf_map);
579  void rotatePSF(double theta
580  ,double scale_x=1
581  ,double scale_y=1
582  );
583 
585  void coaddPSF(double f
586  ,double theta1
587  ,double theta2
588  ,double scale_x
589  ,double scale_y
590  );
591 
592  void ApplyPSF(PixelMap &map_in,PixelMap &map_out);
593  float getPixelSize() const {return pix_size;}
594  void setNoiseCorrelation(std::string nc_file);
595 
596  // virtual methods
597 
598  virtual void AddNoise(PixelMap &pmap
599  ,PixelMap &error_map
600  ,Utilities::RandomNumbers_NR &ran,bool cosmics) = 0;
601 
602  virtual void Convert(PixelMap &map_in
603  ,PixelMap &map_out
604  ,PixelMap &error_map
605  ,bool psf
606  ,bool noise
607  ,Utilities::RandomNumbers_NR &ran,bool cosmics) = 0;
608 
609  virtual float getBackgroundNoise() const = 0;
610 
612  virtual double mag_to_counts(double m) const = 0;
613  virtual double counts_to_mag(double flux) const = 0;
614  virtual double zeropoint() const = 0;
615  virtual void setZeropoint(double zpoint) = 0;
616 
617 
618 protected:
619 
620  double pix_size; // pixel size (in rad)
621  void CorrelateNoise(PixelMap &pmap);
622  float seeing; // full-width at half maximum of the gaussian smoothing
623 
624  // the number of pixels in the real image
625  size_t Npix_x_output,Npix_y_output;
626  // the number of pixels in the oversamples image
627  size_t Npix_x_input,Npix_y_input;
628 
629  float psf_oversample; // psf oversampling factor
630  void downsample(PixelMap &map_in,PixelMap &map_out) const; // downsize from Npix_input to Npix_output
631 
632  PixelMap map_scratch;
633 
634 private:
635  double input_psf_pixel_size;
636  size_t side_ncorr; // pixels on a side of input noise correlation function
637 
638  void fftpsf(); // FFT the psf for later use
639  std::valarray<double> map_psf; // array of the point spread function
640  std::valarray<double> map_psfo; // initial array of the point spread function
641 
642  std::vector<std::complex<double> > fft_psf;
643  std::vector<std::complex<double> > fft_padded;
644  std::vector<double> image_padded;
645  std::vector<double> sqrt_noise_power; // stores sqrt root of power noise spectrum
646 
647  // size of borders for psf convolution
648  size_t nborder_x = 0;
649  size_t nborder_y = 0;
650 
651  // size of padded images
652  size_t n_x = 0;
653  size_t n_y = 0;
654 
655  fftw_plan image_to_fft;
656  fftw_plan fft_to_image;
657 
658  //PixelMap noise_correlation;
659  std::vector<std::complex<double> > noise_fft_image;
660  std::vector<double> noise_in_zeropad;
661  fftw_plan p_noise_r2c;
662  std::vector<double> noise_image_out;
663  fftw_plan p_noise_c2r;
664 };
665 
674 class ObsVIS : public Obs{
675 private:
676 
677  // standard from magnitude to e- per sec
678  double zero_point = 24.4;
679  //double sigma_back_per_qsrttime = 0.00267 * sqrt(5.085000000000E+03);
680 
681  //double gain = 11160; // e-/ADU (Analog Digital Units)
682  //double exp_num = 4;
683  //double exp_time = 2260.; // seconds
684  //double l = 7103.43;
685  //double dl = 3318.28;
686  //double seeing = 0.18;
687 
688  // derived parameters;
689  double sigma_background; // background var in ergs / cm^2 / s / Hz
690  //double sb_to_e; // approximate convertion between ergs / cm^2 / s and e-
691 
692  // adds random cosmic rays to the noise map
693  void cosmics(PixelMap &error_map
694  ,double inv_sigma2 // for one dither
695  ,int nc // number of cosmics to be added
696  ,Utilities::RandomNumbers_NR &ran) const ;
697 
698 public:
699 
700  // exposure times are set to wide survey expectations
701  ObsVIS(size_t Npix_x,size_t Npix_y
702  ,int oversample
703  ,double resolution = 0.1*arcsecTOradians
704  //,double t = 5.085000000000E+03 // observation time in seconds. default is for SC8
705  );
706 
707  ObsVIS(size_t Npix_x
708  ,size_t Npix_y
709  ,const std::vector<double> &exposure_times // in seconds
710  ,int oversample
711  );
712 
713  ObsVIS(size_t Npix_x
714  ,size_t Npix_y
715  ,const std::vector<double> &exposure_times // in seconds
716  ,int oversample
717  ,double resolution
718  ,double background_sigma
719  );
720  ~ObsVIS(){};
721 
723  void AddPoisson(PixelMap &pmap
725  );
726 
728  void AddNoise(PixelMap &pmap
729  ,PixelMap &error_map
731  ,bool cosmic=true);
732 
733  void Convert(PixelMap &map_in
734  ,PixelMap &map_out
735  ,PixelMap &error_map // this is sigma
736  ,bool psf
737  ,bool noise
739  ,bool cosmic=true);
740 
741 
742  double mag_to_counts(double m) const{
743  if(m == 100) return 0;
744  return pow(10,-0.4*(m + zero_point));
745  }
746  double counts_to_mag(double flux) const{
747  if(flux <=0) return 100;
748  return -2.5 * log10(flux) - zero_point;
749  }
750 
751  double zeropoint() const {return zero_point;}
752  void setZeropoint(double zpoint){zero_point=zpoint;}
753 
755  float getBackgroundNoise() const {
756  double dt = Utilities::vec_sum(t_exp);// 3 * t1 + t2;
757 
758  return sigma_background / sqrt(dt);
759  }
760 private:
761  //double t1;
762  //double t2;
763  std::vector<double> t_exp; // exposure times
764 
765 };
766 
775 class Observation : public Obs
776 {
777 public:
778  Observation(Telescope tel_name,double exposure_time,int exposure_num
779  ,size_t Npix_x,size_t Npix_y, float oversample);
780  Observation(float diameter, float transmission, float exp_time, int exp_num, float back_mag, float read_out_noise
781  ,size_t Npix_x,size_t Npix_y,double pix_size,float seeing = 0.);
782  Observation(float diameter, float transmission, float exp_time, int exp_num, float back_mag, float read_out_noise ,std::string psf_file,size_t Npix_x,size_t Npix_y,double pix_size, float oversample = 1.);
783 
784  Observation(float zeropoint_mag, float exp_time, int exp_num, float back_mag, float read_out_noise, size_t Npix_x,size_t Npix_y,double pix_size,float seeing=0);
785  Observation(float zeropoint_mag, float exp_time, int exp_num, float back_mag, float read_out_noise ,std::string psf_file,size_t Npix_x,size_t Npix_y,double pix_size, float oversample = 1.);
786 
787  ~Observation(){};
788 
789  float getExpTime() const {return exp_time;}
790  int getExpNum() const {return exp_num;}
791  float getBackMag() const {return back_mag;}
793  float getRon() const {return read_out_noise;}
795  float getSeeing() const {return seeing;}
796  float getZeropoint() const {return mag_zeropoint;}
797  void setZeropoint(double zpoint){mag_zeropoint=zpoint;}
799  float getBackgroundNoise(float resolution, UnitType unit = UnitType::counts_x_sec) const;
800  float getBackgroundNoise() const {return 0;};
801 
802  void AddNoise(PixelMap &pmap,PixelMap &error_map,Utilities::RandomNumbers_NR &ran,bool dummy);
803 
804  void Convert(PixelMap &map_in
805  ,PixelMap &map_out
806  ,PixelMap &error_map
807  ,bool psf
808  ,bool noise
810  ,bool cosmic=false
811  );
812 
814  //double flux_convertion_factor(){ return pow(10,-0.4*mag_zeropoint); }
815 
816  void setExpTime(float time){exp_time = time;}
817  void setPixelSize(float pixel_size){pix_size=pixel_size;}
818 
819  double mag_to_counts(double m) const {
820  if(m == 100) return 0;
821  return pow(10,-0.4*(m - mag_zeropoint));
822  }
823  double counts_to_mag(double flux) const{
824  if(flux <=0) return 100;
825  return -2.5 * log10(flux) + mag_zeropoint;
826  }
827  double zeropoint() const{
828  return mag_zeropoint;
829  }
830 private:
831 
832  //float diameter; // diameter of telescope (in cm)
833  //float transmission; // total transmission of the instrument
834  float mag_zeropoint; // magnitude of a source that produces one count/sec in the image
835  float exp_time; // total exposure time (in sec)
836  int exp_num; // number of exposures
837  float back_mag; // sky (or background) magnitude in mag/arcsec^2
838  float read_out_noise; // read-out noise in electrons/pixel
839  float gain;
840 
841  bool telescope; // was the observation created from a default telescope?
842  float e_per_s_to_ergs_s_cm2; // e- / s for zero magnitudes
843  float background_flux; // e- / s / arcsec
844 
845  void set_up();
846 
847  void ToCounts(PixelMap &pmap);
848  void ToSurfaceBrightness(PixelMap &pmap);
849  void ToADU(PixelMap &pmap);
850 
851 };
852 
853 void pixelize(double *map,long Npixels,double range,double *center
854  ,ImageInfo *imageinfo,int Nimages,bool constant_sb,bool cleanmap
855  ,bool write_for_skymaker = false, std::string filename="");
856 void _SplitFluxIntoPixels(TreeHndl ptree,Branch *leaf,double *leaf_sb);
857 //void smoothmap(double *map_out,double *map_in,long Npixels,double range,double sigma);
858 
859 namespace Utilities{
860  void LoadFitsImages(std::string dir,const std::string& filespec,std::vector<PixelMap> & images,int maxN,double resolution = -1,bool verbose = false);
861  void LoadFitsImages(std::string dir,std::vector<std::string> filespecs,std::vector<std::string> file_non_specs ,std::vector<PixelMap> & images,std::vector<std::string> & names,int maxN,double resolution = -1,bool verbose = false);
862  void ReadFileNames(std::string dir,const std::string filespec
863  ,std::vector<std::string> & filenames
864  ,const std::string file_non_spec = " "
865  ,bool verbose = false);
866 }
867 
871 public:
872  MultiGridSmoother(double center[],std::size_t Nx,std::size_t Ny,double resolution);
873  MultiGridSmoother(double center[],std::size_t Nx,double resolution);
874  ~MultiGridSmoother(void){
875  maps.clear();
876  }
877 
879  PosType getHighestRes(){return maps[0].getResolution();}
881  PosType getLowestRes(){return maps.back().getResolution();}
882 
884  void add_particles(std::vector<PosType> x,std::vector<PosType> y);
886  void output_map(PixelMap &map,int Nsmooth);
887  void smooth(int Nsmooth,PixelMap &map);
888 
889 private:
890  void _smooth_(int k,size_t i,size_t j,int Nsmooth,PixelMap &map);
891  std::vector<PixelMap> maps;
892  std::vector<Utilities::Interpolator<PixelMap>> interpolators;
893 };
894 
895 #endif
MultiGridSmoother::getHighestRes
PosType getHighestRes()
resolution of finest grid from which interpolation is done
Definition: image_processing.h:879
to_string
std::string to_string(const Band &band)
convert a Band type to a string name.
Definition: InputParams.cpp:878
PixelMap::AddUniformImages
void AddUniformImages(ImageInfo *imageinfo, int Nimages, double value)
Add images with uniform surface brightness set by input parameter value.
Definition: pixelize.cpp:716
Observation::getBackgroundNoise
float getBackgroundNoise(float resolution, UnitType unit=UnitType::counts_x_sec) const
pixel size in radians
Definition: observation.cpp:1158
PixelMap::operator-=
PixelMap & operator-=(const PixelMap &rhs)
Subtract the values of another PixelMap from this one.
Definition: pixelize.cpp:529
PixelMap::AddCurve
void AddCurve(ImageInfo *curve, double value)
Draws a closed curve through the points in curve->imagekist.
Definition: pixelize.cpp:1783
MultiGridSmoother::getLowestRes
PosType getLowestRes()
resolution of coarsest grid from which interpolation is done
Definition: image_processing.h:881
PixelMap::getRA
double getRA()
returns right accention of center
Definition: image_processing.h:86
PixelMap::AddGridMapBrightness
void AddGridMapBrightness(const GridMap &grid)
Add an image from a the surface brightnesses of a GridMap to the PixelMap.
Definition: pixelize.cpp:673
PixelMap::AssignValue
void AssignValue(std::size_t i, double value)
Assigns a value to the i-th pixel.
Definition: pixelize.cpp:490
Source
Base class for all sources.
Definition: source.h:45
PixelMap::AddValue
void AddValue(std::size_t i, double value)
Adds a value to the i-th pixel.
Definition: pixelize.cpp:484
Grid
Structure to contain both source and image trees.
Definition: grid_maintenance.h:25
PixelMap::drawBox
void drawBox(PosType p1[], PosType p2[], PosType value, int Nstrip)
Draws a box (filling the inside with horizontal lines, starting from the top)
Definition: pixelize.cpp:1730
PixelMap::printASCII
void printASCII() const
Print an ASCII table of all the pixel values.
Definition: pixelize.cpp:1218
Branch
The box representing a branch of a binary tree structure. Used specifically in TreeStruct for organiz...
Definition: point.h:624
ObsVIS::AddNoise
void AddNoise(PixelMap &pmap, PixelMap &error_map, Utilities::RandomNumbers_NR &ran, bool cosmic=true)
Applies noise (read-out + Poisson) on an image, returns noise map.
Definition: observation.cpp:76
PixelMap::drawdisk
void drawdisk(PosType r_center[], PosType radius, PosType value, int Nstrip)
Draws a disk.
Definition: pixelize.cpp:1601
Observation::setExpTime
void setExpTime(float time)
returns factor by which code image units need to be multiplied by to get flux units
Definition: image_processing.h:816
PixelMap::recenter
void recenter(PosType newcenter[2])
recenter the map without changing anything else.
Definition: pixelize.cpp:2810
PointList
link list for points, uses the linking pointers within the Point type unlike Kist
Definition: point.h:751
PixelMap::threshold
size_t threshold(std::list< size_t > &pixel_index, PosType value)
get a list of pixels above value
Definition: image_processing.h:379
PixelMap::paste
void paste(const PixelMap &pmap)
Replace overlaping pixel values with those of the input map.
Definition: pixelize.cpp:2746
PixelMap::ave
PosType ave() const
return average pixel value
Definition: image_processing.h:232
PixelMap::setRAandDec
void setRAandDec(double RAin, double DECin)
set the coordinates of center
Definition: image_processing.h:91
Observation::AddNoise
void AddNoise(PixelMap &pmap, PixelMap &error_map, Utilities::RandomNumbers_NR &ran, bool dummy)
Applies realistic noise (read-out + Poisson) on an image, returns noise map.
Definition: observation.cpp:1185
PixelMap::printFITS
void printFITS(std::string filename, bool Xflip=false, bool verbose=false)
Output the pixel map as a fits file.
Definition: pixelize.cpp:1248
PixelMap::addheader
void addheader(std::string label, long value, std::string comment)
add a heaader keyword that will appear in fits output
Definition: image_processing.h:429
PixelMap::drawSquare
void drawSquare(PosType p1[], PosType p2[], PosType value)
Draw a rectangle.
Definition: pixelize.cpp:1699
PixelMap::DrawLine
void DrawLine(long x0, long x1, long y0, long y1, double value, bool add)
line by Bresenham's line algorithm
Definition: pixelize.cpp:1501
PixelMap::AddGridBrightness
void AddGridBrightness(Grid &grid)
Add an image from a the surface brightnesses of a Grid to the PixelMap.
Definition: pixelize.cpp:640
PixelMap::AddImages
void AddImages(ImageInfo *imageinfo, int Nimages, float rescale=1.)
Add an image to the map.
Definition: pixelize.cpp:597
Point_2d
Class for representing points or vectors in 2 dimensions. Not that the dereferencing operator is over...
Definition: point.h:48
PixelMap::drawcircle
void drawcircle(PosType r_center[], PosType radius, PosType value)
Draws a circle.
Definition: pixelize.cpp:1578
ObsVIS
It creates a realistic image from the output of a ray-tracing simulation.
Definition: image_processing.h:674
PixelMap::copy_in
void copy_in(const PixelMap &pmap)
copy a PixelMap into this one.
Definition: pixelize.cpp:2689
Utilities::RandomNumbers_NR
This is a class for generating random numbers. It simplifies and fool proofs initialization and allow...
Definition: utilities_slsim.h:1041
ObsVIS::getBackgroundNoise
float getBackgroundNoise() const
returns std of pixels in e-
Definition: image_processing.h:755
GridMap
A simplified version of the Grid structure for making non-adaptive maps of the lensing quantities (ka...
Definition: gridmap.h:31
PixelMap::AddGrid
void AddGrid(const Grid &grid, double value=1.0)
Fills in pixels where the image plane points in the grid are located with the value given.
Definition: pixelize.cpp:1849
PixelMap::operator+=
PixelMap & operator+=(const PixelMap &rhs)
Add the values of another PixelMap to this one.
Definition: pixelize.cpp:507
PixelMap::count_islands
int count_islands(std::vector< size_t > &pixel_index) const
For a list of pixel indexes this will count and separated islands that are not connected.
Definition: pixelize.cpp:1086
PixelMap::downsize
PixelMap downsize(int n)
Creates a PixelMap with a lower resolution. The value of the pixels are added for the new pixels....
Definition: pixelize.cpp:412
PixelMap::linear_interpolate
PosType linear_interpolate(PosType x[])
interpolate to point x[]
Definition: pixelize.cpp:2419
PixelMap::doubleFlip
void doubleFlip()
rotate the image by 180deg or equivalently reflect it through the origin
Definition: image_processing.h:401
PixelMap::getDEC
double getDEC()
returns declination of center
Definition: image_processing.h:88
MultiGridSmoother
Warning: Not tested yet. Class for doing adaptive smoothing using multiply resolution grids.
Definition: image_processing.h:870
PixelMap::find_contour
void find_contour(double level, std::vector< std::vector< Point_2d > > &points, std::vector< bool > &hits_edge) const
returns a vector of contour curves
Definition: pixelize.cpp:800
TreeStruct
Tree: Exported struct.
Definition: Tree.h:31
PixelMap::operator+
PixelMap operator+(const PixelMap &) const
Add two PixelMaps.
Definition: pixelize.cpp:519
PixelMap::operator*=
PixelMap & operator*=(const PixelMap &rhs)
Multiply the values of another PixelMap by this one.
Definition: pixelize.cpp:550
PixelMap::operator/
PixelMap operator/(const PixelMap &a) const
Multiply two PixelMaps.
Definition: pixelize.cpp:584
PixelMap::smooth
void smooth(double sigma)
Smoothes a map with a Gaussian kernel of width sigma (in arcseconds)
Definition: pixelize.cpp:1389
PixelMap::printASCIItoFile
void printASCIItoFile(std::string filename) const
Print an ASCII table of all the pixel values.
Definition: pixelize.cpp:1228
PixelMap::sum
PosType sum() const
return sum of all pixel values
Definition: image_processing.h:234
Utilities::AdaptiveSmooth
std::valarray< double > AdaptiveSmooth(const std::valarray< double > &map_in, size_t Nx, size_t Ny, double value)
Smooth a 2 dimensional map stored in a valarray with a density dependent kernel.
Definition: utilities.cpp:762
PixelMap::find_islands_holes
void find_islands_holes(double level, std::vector< std::vector< size_t > > &points) const
Definition: pixelize.cpp:819
PixelMap::lens_definition
void lens_definition(double min_sn_per_image, double pixel_threshold, int &Nimages, double &total_sig_noise_source, std::vector< size_t > &maxima_indexes, std::vector< std::vector< size_t > > &image_points, bool &lens_TF, double &level, size_t &n_pix_in_source, bool verbose=false)
Definition: pixelize.cpp:920
Utilities::ReadFileNames
void ReadFileNames(std::string dir, const std::string filespec, std::vector< std::string > &filenames, const std::string file_non_spec=" ", bool verbose=false)
Definition: pixelize.cpp:2248
PixelMap::flipX
void flipX()
reflects the image about the vertical mid-line
Definition: image_processing.h:393
MultiGridSmoother::add_particles
void add_particles(std::vector< PosType > x, std::vector< PosType > y)
Add particles to the map. These do not need to be kept in memory after they are added.
Definition: pixelize.cpp:2513
Utilities::powerspectrum2dprebin
void powerspectrum2dprebin(std::valarray< double > &aa, int nx, int ny, double boxlx, double boxly, const std::vector< double > &ll, std::vector< double > &Pl, std::vector< double > &llave)
Definition: utilities.cpp:680
PixelMap::flipY
void flipY()
reflects the image about the horizontal mid-line
Definition: image_processing.h:385
PixelMap::agrees
bool agrees(const PixelMap &other) const
Check whether two PixelMaps agree in their physical dimensions.
Definition: pixelize.cpp:495
PixelMap::maxima
std::vector< size_t > maxima(double minlevel) const
find maxima that are above minlevel
Definition: pixelize.cpp:881
PixelMap::Renormalize
void Renormalize(double factor)
Multiplies the whole map by a scalar factor.
Definition: pixelize.cpp:478
PixelMap::PowerSpectrum
void PowerSpectrum(std::vector< PosType > &power_spectrum, const std::vector< PosType > &lbins, std::vector< PosType > &lave, bool overwrite=true)
Find the power spectrum of the map.
Definition: image_processing.h:305
Utilities::LoadFitsImages
void LoadFitsImages(std::string dir, const std::string &filespec, std::vector< PixelMap > &images, int maxN, double resolution=-1, bool verbose=false)
Reads all the fits files in a directory into a vector of PixelMaps.
Definition: pixelize.cpp:2140
PixelMap::find_position
void find_position(PosType x[], std::size_t const index) const
get the index for a position, returns -1 if out of map
Definition: pixelize.cpp:2359
PixelMap::FindArc
void FindArc(PosType &radius, PosType *xc, PosType *arc_center, PosType &arclength, PosType &width, PosType threshold)
Find arcs in image WARNING: THIS IS UNDER CONSTRUCTION!
Definition: pixelize.cpp:1993
PixelMap::duplicate
void duplicate(const PixelMap &pmap)
copy a PixelMap that must be the same without creating a new one..
Definition: pixelize.cpp:2677
PixelMap::size
size_t size()
Total number of pixels.
Definition: image_processing.h:236
PixelMap::drawline
void drawline(double x1[], double x2[], double value, bool add)
simple line
Definition: pixelize.cpp:1455
PixelMap::convolve
void convolve(PixelMap &kernel, long center_x=0, long center_y=0)
convolve the image with a kernel.
Definition: pixelize.cpp:2885
PixelMap::find_index
long find_index(PosType const x[], long &ix, long &iy) const
get the index for a position, returns -1 if out of map, this version returns the 2D grid coordinates
Definition: pixelize.cpp:2312
PixelMap::operator-
PixelMap operator-(const PixelMap &) const
Subtract two PixelMaps.
Definition: pixelize.cpp:540
PixelMap::drawgrid
void drawgrid(int N, PosType value)
draw a grid on the image that divides the each demension into N cells
Definition: pixelize.cpp:1637
ObsVIS::AddPoisson
void AddPoisson(PixelMap &pmap, Utilities::RandomNumbers_NR &ran)
add poisson noise to an image that is in units of electrons
Definition: observation.cpp:59
MultiGridSmoother::MultiGridSmoother
MultiGridSmoother(double center[], std::size_t Nx, std::size_t Ny, double resolution)
Definition: pixelize.cpp:2463
PixelMap::AddSource_parallel
PosType AddSource_parallel(T &source, int oversample)
Adds source to map. This version breaks pixels up into blocks and does them in seporate threads.
Definition: image_processing.h:140
MultiGridSmoother::output_map
void output_map(PixelMap &map, int Nsmooth)
Output a map at the resolution of the map smoothed so that no superpixel as less than Nsmooth particl...
Definition: pixelize.cpp:2531
Observation::getRon
float getRon() const
read-out noise in electrons/pixel
Definition: image_processing.h:793
PixelMap::rotate
PixelMap rotate(PosType theta, PosType scale=1)
rotate and scale the image while keeping pixels, resoluiton
Definition: pixelize.cpp:2384
Observation::Convert
void Convert(PixelMap &map_in, PixelMap &map_out, PixelMap &error_map, bool psf, bool noise, Utilities::RandomNumbers_NR &ran, bool cosmic=false)
Converts the input map to a realistic image.
Definition: observation.cpp:1121
Observation::getSeeing
float getSeeing() const
seeing in arcsecs
Definition: image_processing.h:795
PixelMap::PowerSpectrum
void PowerSpectrum(std::vector< PosType > &power_spectrum, std::vector< PosType > &lvec, bool overwrite=true)
Find the power spectrum of the map.
Definition: image_processing.h:288
PixelMap
Image structure that can be manipulated and exported to/from fits files.
Definition: image_processing.h:48
LensingVariable
LensingVariable
output lensing variables
Definition: standard.h:89
Observation::Observation
Observation(Telescope tel_name, double exposure_time, int exposure_num, size_t Npix_x, size_t Npix_y, float oversample)
Creates an observation setup that mimics a known instrument.
Definition: observation.cpp:812
Utilities::GetNThreads
int GetNThreads()
returns the compiler variable N_THREADS that is maximum number of threads to be used.
Definition: utilities.cpp:846
ImageInfo
Structure for storing information about images or curves.
Definition: image_info.h:20
PixelMap::operator*
PixelMap operator*(const PixelMap &a) const
Multiply two PixelMaps.
Definition: pixelize.cpp:576
Utilities
Definition: utilities.h:30
Observation
It creates a realistic image from the output of a ray-tracing simulation.
Definition: image_processing.h:776