Getting Started Documentation Glish Learn More Programming Contact Us
Version 1.9 Build 1556
News FAQ
Search Home


next up previous contents index
Next: An advanced example Up: Introduction to Programming for AIPS++ Previous: A First Program

Second Version

We now continue by slightly expanding this program.

First, we will supply input arguments to the program in order to set various parameters and to determine the type of output desired.

Next, we will replace the scalars x and y by a class of objects known to aips++ as a Vector. In order to do this we must include <aips/Vector.h>. As we will see, that the same class can be used to contain different types of objects. This is known in C++ as a template.

Elements of a Vector may be addressed as they are in FORTRAN, that is by the name of the vector followed by a subscript in parentheses. aips++ however allows some operations to be applied to an entire vector. We will illustrate this.

Finally, we will provide the possibility of creating a file in aips++ format i.e. in a format that can be read by other aips++ programs. (The source code for this program is found in $AIPSROOT/code/trial/implement/test/tExample2.cc).

   1  #include <aips/aips.h>
   2  #include <aips/IO/AipsIO.h>
   3  #include <aips/IO/AipsIOError.h>
   4  #include <aips/Inputs.h>
   5  #include <aips/Mathematics.h>
   6  #include <aips/Arrays/Vector.h>
   7  #include <iostream.h>
   8
   9  main(int argc, char **argv)
  10  {
  11   Input inputs(1);
  12   inputs.Version("$@w{Revision}$");
  13   inputs.Create("vectorsize","15","Number of vector elements?","Integer");
  14   inputs.Create("usefile","True","Create an AIPS++ File?","Bool");
  15   inputs.Create("file","/tmp/sample2.test","Output file name?","Outfile");
  16   try {
  17      inputs.ReadArguments(argc, argv);
  18
  19      const Int hold = inputs.GetInt("vectorsize");
  20      const Bool usefile = inputs.GetBool("usefile");
  21      const char *filename = inputs.GetString("file");
  22
  23      const Float   interval(10.0);
  24      const uInt    version(1);
  25      Vector<float> x(hold), y(hold);
  26
  27      indgen(x);
  28      y = x * sin(x / interval);
  29      if (usefile) {
  30         AipsIO outfile(filename, AipsIO::Out);
  31         outfile << x << y;
  32      }else{
  33         for (Int i=0; i<x.nelements(); i++) {
  34            cout << x(i) << "\t" << y(i) << endl;
  35         }
  36      }
  37   }
  38
  39
  40   catch (AipsIOError x) {
  41      cerr << "aipsioerror: error " << x.getMesg() << endl;
  42      return 1;
  43   }
  44   catch (AipsError x) {
  45      cerr << "aipserror: error " << x.getMesg() << endl;
  46      return 1;
  47   }
  48
  49   return 0;
  50  }

The following points should be observed concerning this version (as before, the numbers refer to those prepended to each line of the listing):

2-
The AipsIO class refers to real IO as opposed to the acquisition of command line arguments which is the role of the Input class.

7-
The Vector class is used to handle aips++ style of vectors which differs, of course, from the simple arrays predefined in the C++ language.

13-15-
The Create member function of Input builds from the command line three parameters referenced as vectorsize, usefile and outfile. All parameters are optional. The value stated as the second argument in each case is the default value suplied for use when the program is started without that specific parameter. The third argument is used for prompting (when requested by the program user - see below). The fourth argument is the type of the parameter's value. Two additional arguments are optional and are omitted in this sample program.

17-
This line supplies the value of each parameter. The default values will be used unless specific values have been supplied. A keyword that doesn't match those anticipated in the program will be discarded as an error but the program will continue.

19-21-
Each of the parameters read from the input line or supplied by default is assigned to a const variable to be used (but not modified) in the program. Note the use of char datatype, rather that a String, for the variable filename.

25-
The aips++ Vector data type is a template which means that vectors may consist of different data types such as Int or Float or Complex. The space required by the vectors is assigned at run time so the arrays can be dimensioned on the basis of an input parameter. Note the use of the round, rather than the square, brackets for the dimensions of the Vector.

27-
The function indgen is supplied as part of the ArrayMath class. In this simple case it fills each element of the array specified as an argument with the integers 0,1,2,3,...,nelements()-1 (In other, more elaborate cases, it can fill the array with other sequences.)

28-
This Vector statement is the equivalent of a loop operating element by element.

30-
The definition of the AipsIO item called outfile within the if block prevents the file from being opened unless it is needed. The type of file (output) is specified in the second argument. Options are: In, Out, Update, Append, New, NewNoReplace, Scratch and Delete. Note that we did not need to OPEN the file nor do we need to CLOSE it unless it needs to be rewritten (or reread).

31-
In this line the < < operator is overloaded so that the output is an entire vector.
33-34-
Here we deliberately use the subscript notation for elements of a Vector. One of the side benefits of using the aips++ Vector data type is that the class contains a function, called nelements() which returns the current size of the vector. Its use here is obviously more flexible and, at the same time, more robust than the use of a constant.

40-43-
A new class of error is caught by these statements.

As before, to compile this program one should use the gmake command. Once compiled, the program can be started by simply typing sample2. The result is a file in the /tmp directory. This file is aips++ compliant which means that the information is preceeded by some aips++ identification. As noted at the beginning of this section, the file is in binary format which means, of course, that it is not suitable for display on a terminal.

As it runs, the program DOES display one line on the terminal; that line identifies the version of the program being run. (The reason this line did not appear in the output produced by the sample1 program is because we never read the inputs.)

The same program can be started with a request to send output to the screen i.e. to override the default value of the usefile and vectorsize parameters. When we start it by typing @* sample2 usefile=false vectorsize=5 we get the following

sample2: Version $@w{Revision}$
0       0
1       0.0998334
2       0.397339
3       0.886561
4       1.55767

The version number has NOT been inserted for the string @wRevision because we have not submitted the program to the aips++ system library.

It my very well happen that you wish to run a program but you do not remember the names of the arguments nor their expected values. The solution is to run the program by stating its name followed by the string help=prompt. For example, if we type sample2 help=prompt we get the following:

sample2: Version $@w{Revision}$

Number of vector elements? [15]: vectorsize=

Create an {\tt aips++} File? [True]: usefile=

Output file name? [/tmp/sample2.test]: outfile=

Notice the appearance of the text that we inserted as one of the inputs.Create arguments. That text is followed by the default value (also specified as a inputs.Create argument). Finally we have the name of the argument. We can either supply a new value or merely hit the RETURN key to accept the default value shown in the square parentheses.


next up previous contents index
Next: An advanced example Up: Introduction to Programming for AIPS++ Previous: A First Program   Contents   Index
Please send questions or comments about AIPS++ to aips2-request@nrao.edu.
Copyright © 1995-2000 Associated Universities Inc., Washington, D.C.

Return to AIPS++ Home Page
2006-10-15