Adbin code details

The included code contains a couple of useful libraries written in C++ which may be useful to those writing software.

FITS image library

A simple FITS image library, contained within the FITS subdirectory of the distribution, is included. The code isn't great (I'm in the process a redesign and rewrite), but it does the job and is probably fine for small projects.

It can load and save FITS format images. It contains a class for the easy manipulation of images in memory. It converts all images to double format two-dimensional images on loading.

Sample code (untested!)

#include <FITS/FITSFile.h>
#include <FITS/FITSImage.h>

...

// load images
CFITSFile inputfile1("image1.fits", CFITSFile::existingro);
CFITSFile inputfile2("image2.fits", CFITSFile::existingro);

// multiply the images
CFITSImage manip = inputfile1.GetImage();
manip *= inputfile2.GetImage();

// square root result
manip.Sqrt();

// do something pointless to the individual pixels
for(int y=0; y<manip.GetYW(); y++)
  for(int x=0; x<manip.GetXW(); x++)
    manip.SetPixel(x, y, manip.GetPixel(x, y) + x);

{
  // save the output image
  CFITSFile outputfile("out.fits", CFITSFile::create);
  outputfile.SetImage(manip);                 // set output image
  outputfile.SetPosn(inputfile1.GetPosn());   // copy WCS information
  outputfile.WriteImage();
}

Parammm library

Parammm is quite a useful library to handle parameters and GNU-style switches to programs. I wrote it because I didn't like the way popt does things. The library is extensible and I'm fairly happy with the OOP way in which it does things. Here is a list of features:

  1. Can take lots of datatypes (user-addable) as options to switches. For example ints, doubles and strings.
  2. Automatic option help generation, listing syntax and parameters.
  3. Provides a list of strings giving the parameters.

Sample code

#include <string>
#include <parammm/parammm.hh>

using std::string;

...

int main(int argc, char *argv[])
{
  parammm::param params(argc, argv);  // define param object

  int minchan=0, maxchan=1024, noslices=1;
  string sliceoutfilename, backgrndfilename;

  params.add_switch( parammm::pswitch("minchan", 'i',
                                      parammm::pint_opt(&minchan),
                                      "set minimum channel", "CHAN"));
  // take an integer switch ( --minchan=xx or -i xx )
  // note how the return variable and type are specified
  // the final param specifies the help text and the quantity
  // "unit" being taken.

  params.add_switch( parammm::pswitch("maxchan", 'x',
                                      parammm::pint_opt(&maxchan),
                                      "set maximum channel", "CHAN" ));
  // another int parameter

  params.add_switch( parammm::pswitch("noslices", 'n',
                                      parammm::pint_opt(&noslices),
                                      "set number of slices", "NUM" ));
  // another int

  params.add_switch( parammm::pswitch("sliceout", 'o',
                                      parammm::pstring_opt(&sliceoutfilename),
                                      "set output filename", "FILE" ));
  // string parameter

  params.add_switch( parammm::pswitch("background", 'b',
                                      parammm::pstring_opt(&backgrndfilename),
                                      "set background filename", "FILE"));
  // string parameter

  params.set_autohelp("Usage: EqualSpec [OPTION]... inputspectrum.fits\n"
                      "divide a spectrum into equal slices (count-wise)\n"
                      "written by Jeremy Sanders, 2000.",
                      "Report bugs to <jss@ast.cam.ac.uk>");
  // text to output at the top and bottoms of the automatic help
  // text.

  params.enable_autohelp();
  // if we get a dodgy parameter, show automatic help and
  // exit the program

  params.interpret_and_catch();
  // actually process argv

  if(params.args().size() != 1)
    params.show_autohelp();
    // actual parameters (non-switches) are stored in the vector<string>
    // params.args(). show_autohelp() shows the autohelp text and exits

  string filename = params.args()[0];
  // copy the first parameter (program name not stored)

  ...

  return(0);
}

Back to the adaptive-binning page

This is the personal page of Jeremy Sanders (Impressium).