Getting Started | Documentation | Glish | Learn More | Programming | Contact Us |
Version 1.9 Build 1556 |
|
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):
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.