casacore
Loading...
Searching...
No Matches
ReadAsciiTable.h
Go to the documentation of this file.
1//# ReadAsciiTable.h: Filling a table from an Ascii file
2//# Copyright (C) 1993,1994,1995,1999,2001,2002
3//# Associated Universities, Inc. Washington DC, USA.
4//#
5//# This library is free software; you can redistribute it and/or modify it
6//# under the terms of the GNU Library General Public License as published by
7//# the Free Software Foundation; either version 2 of the License, or (at your
8//# option) any later version.
9//#
10//# This library is distributed in the hope that it will be useful, but WITHOUT
11//# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12//# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13//# License for more details.
14//#
15//# You should have received a copy of the GNU Library General Public License
16//# along with this library; if not, write to the Free Software Foundation,
17//# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18//#
19//# Correspondence concerning AIPS++ should be addressed as follows:
20//# Internet email: casa-feedback@nrao.edu.
21//# Postal address: AIPS++ Project Office
22//# National Radio Astronomy Observatory
23//# 520 Edgemont Road
24//# Charlottesville, VA 22903-2475 USA
25
26#ifndef TABLES_READASCIITABLE_H
27#define TABLES_READASCIITABLE_H
28
29//# Includes
30#include <casacore/casa/aips.h>
31#include <casacore/casa/BasicSL/String.h>
32#include <casacore/casa/Arrays/IPosition.h>
33#include <casacore/tables/Tables/Table.h>
34
35//# Forward Declarations
36#include <casacore/casa/iosfwd.h>
37
38namespace casacore { //# NAMESPACE CASACORE - BEGIN
39
40class Regex;
41class IPosition;
42class LogIO;
43class TableRecord;
44class TableColumn;
45
46
47// <summary>
48// Filling a table from an Ascii file.
49// </summary>
50// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
51// </reviewed>
52
53// <use visibility=export>
54
55// <prerequisite>
56// <li> <linkto class="Table:description">Table</linkto>
57// </prerequisite>
58
59// <synopsis>
60// Global functions to fill a table from an Ascii file.
61//
62// The table columns are filled from a file containing the data values
63// separated by a separator (optionally followed by whitespace). The
64// default separator is a comma. Non-given values default to 0, False, or
65// blank string (depending on data type). A value is not given between 2
66// consecutive separators or if less values are given than needed.
67// One line per table row should be given.
68// The following two header lines define the columns in the table:
69// <ol>
70// <li> The first line contains the names of the variables in each column.
71// These names may be enclosed in double quotes.
72// <li> The second line contains the data types of each column.
73// Valid types are:
74// <ul>
75// <li> S for Short Integer data
76// <li> I for Integer data
77// <li> R for Real data
78// <li> D for Double Precision data
79// <li> X for Complex data (Real, Imaginary)
80// <li> DX for Double Precision Complex data (R,I)
81// <li> Z for Complex data (Amplitude, Phase)
82// <li> DZ for Double Precision Complex data (A,P)
83// <li> A for ASCII data (must be enclosed in double
84// quotes if it contains one or more blanks)
85// <li> DMS for MVAngle-format position in DMS (converted to radians)
86// In this case a colon separated position is seen as
87// degrees and not as hours.
88// Blanks instead of : can be used as separator.
89// <li> HMS for MVAngle-format position in HMS (converted to radians)
90// Blanks instead of : can be used as separator.
91// </ul>
92// The type can optionally be followed by one or more positive numbers
93// (separated by commas without whitespace) indicating that the column
94// contains an array. The numbers give the shape of the array.
95// E.g. <src>D2,4</src> defines a column containing arrays with
96// shape [2,4]. It "consumes" 8 numbers in each input data line.
97// The last column can contain a 0 in one of the shape numbers.
98// It indicates that the arrays are variable shaped; it "consumes"
99// all remaining numbers in each input data line. If needed,
100// the arrays are filled with default values (0, False, or blank).
101// E.g. <src>I0</src> indicates a variable shaped vector.
102// <src>I0,4</src> with a line with remaining input
103// <src>1 2 3 4 5 6 7 8 9</src> results in an array with shape [3,4]
104// (filled with with 3 zeroes).
105// </ol>
106// If the <src>autoHeader</src> argument is True, the column definition
107// lines should not be given. It recognizes the types from the first data
108// line. It gives the names 'column0', etc. to the columns.
109// It can recognize integer, double, and string types.
110// It is possible to give a shape argument which has the same function
111// as the shape values discussed above.
112// <p>
113// There are two forms of the readAsciiTable function:
114// <ol>
115// <li> The simplest form has two input files.
116// The second input file contains the column data.
117// The first input file contains the keywords (if any)
118// and the column definitions.
119// The keywords in the first file, if there are any, must be enclosed
120// between a line that starts with ".keywords" and a line that starts
121// with ".endkeywords". To define column keywords, .keywords should be
122// followed by whitespace and the column name.
123// Between these two lines each line should contain the following:
124// <ul>
125// <li> The keyword name, e.g., ANYKEY
126// <li> The datatype of the keyword (cf. list of valid types above)
127// <li> The value or values for the keyword (the keyword may contain a
128// scalar or a vector of values). e.g., 3.14159 21.78945
129// </ul>
130// After the keywords definitions, the two column definition lines
131// should follow (unless <src>autoHeader=True</src> is given).
132// <br>For example:
133// <srcblock>
134// .keywords
135// KEYI I 10
136// KEYIV I 11 12 13 14
137// KEYF R 1.2
138// KEYFV R -3.2 0 5.6
139// KEYD D 1.23456789
140// KEYDV D 1 2 3 4 5 6 7 8 9
141// KEYX X -1.5 -3
142// KEYXC X 0 1 2 3 4 5 6 7 8 9
143// KEYZ Z -3 -1.5
144// KEYZV Z 0 0.1 0.2 0.3 0.4 0.5
145// KEYS A "1 2 3 4 5"
146// KEYSV A " 1 2 " "AAA" BBB bbb CCc C "@#$%^&*()"
147// .endkeywords
148// .keywords COLDX
149// IKEYS A "coldx ikey"
150// DKEYS A "coldx dkey"
151// .endkeywords
152// COLI COLF COLD COLX COLZ COLS
153// I R D X Z A
154// </srcblock>
155// defines a table with 12 table keywords (of which 6 contain vector
156// values), 2 keywords for column COLDX, and and 6 columns.
157// The number of rows is determined by the number of
158// lines in the second input file.
159// <li> The other form is to combine the two files in one file.
160// In that case the data lines must be preceeded by the optional
161// keyword and column definitions (without an intermediate blank line).
162// </ol>
163// </synopsis>
164
165// <example>
166// <srcblock>
167// readAsciiTable ("file.in", "", "table.test");
168// </srcblock>
169// creates a table with name <src>table.test</src> from the text file
170// <src>file.in</src>. The text file could look like:
171// <srcblock>
172// COLI COLF COLD COLX COLZ COLS
173// I R D X Z A
174// 1 1.1 1.11 1.12 1.13 1.14 1.15 Str1
175// 10 11 12 13 14 15 16 String17
176// </srcblock>
177// resulting in a table with 6 columns and 2 rows.
178// </example>
179
180// <group name=readAsciiTable>
182
183// Create a table with name as given by tableName.
184// If autoHeader==True, the format is automatically derived from the
185// first data line. It can recognize integer, double, and String types.
186// The columns will be named column1, column2, etc..
187// If the autoShape argument is given with 1 or more axes, all values are
188// treated as a single column with the given shape. Note that one of the
189// can have length 0 indicating a variable shaped array.
190// If autoHeader==False, the layout of the table has to be defined in
191// the first 2 lines of the input file. The remaining lines in the
192// input file contain the data.
193//
194// When the tableDescName is not blank, the table description will
195// be stored in a table description file with the given name.
196// <br>It returns a string containing the format of the columns in
197// the form COL1=R, COL2=D, ...
198//
199// The separator gives the character separating the values. The default
200// is a blank. Note that irrespective of the separator, blanks between
201// values are always ignored. A string value has to be enclosed in
202// double quotes if it has to contain blanks or the separator value.
203//
204// Header and data lines starting with the regular expression given in the
205// commentMarker are ignored. By default no comment marker is present.
206// E.g. "#" ignores all lines starting with the #-sign.
207// " *#" does the same, but the lines to ignore can start with whitespace.
208//
209// The first and last line argument give the 1-relative number of the
210// first and last line to read from the file. firstLine <= 0 is the
211// same as 1. lastLine <= 0 means until end-of-file.
212// Note that lines matching the comment marker are also counted.
213String readAsciiTable (const String& filein, const String& tableDescName,
214 const String& tableName, Bool autoHeader = False,
215 Char separator = ' ',
216 const String& commentMarkerRegex = "",
217 Int firstLine = 1, Int lastLine = -1,
218 const IPosition& autoShape = IPosition());
219
220// This form gets the header info in the given vectors.
221// Each element in the dataTypes vector has to be of the form as would
222// be given in a header line.
223String readAsciiTable (const String& filein, const String& tableproto,
224 const String& tablename,
225 const Vector<String>& columnNames,
226 const Vector<String>& dataTypes,
227 Char separator, const String& commentMarkerRegex,
228 Int firstLine, Int lastLine);
229
230// This form reads TWO Ascii files. The first file may contain
231// keywords and their values as well as the two lines described above for
232// the names and type of variables. The second file is intended for data only.
233//
234// When the tableDescName is not blank, the table description will
235// be stored in a table description file with the given name.
236// <br>It returns a string containing the format of the columns in
237// the form COL1=R, COL2=D, ...
238//
239// The separator gives the character separating the values. The default
240// is a blank. Note that irrespective of the separator, blanks between
241// values are always ignored. A string value has to be enclosed in
242// double quotes if it has to contain blanks or the separator value.
243//
244// Header and data lines starting with the regular expression given in the
245// commentMarker are ignored. By default no comment marker is present.
246// E.g. "#" ignores all lines starting with the #-sign.
247// " *#" does the same, but the lines to ignore can start with whitespace.
248//
249// The first and last line argument give the 1-relative number of the
250// first and last line to read from the data file. firstLine <= 0 is the
251// same as 1. lastLine <= 0 means until end-of-file.
252// Note that lines matching the comment marker are also counted.
253// <group>
254String readAsciiTable (const String& headerFile, const String& dataFile,
255 const String& tableDescName, const String& tablename,
256 Char separator = ' ',
257 const String& commentMarkerRegex = "",
258 Int firstLine = 1, Int lastLine = -1);
259//# Note that this char* version is needed, because of the first version
260//# Taking a Bool as the 4th argument.
261String readAsciiTable (const String& headerFile, const String& dataFile,
262 const String& tableDescName, const char* tablename,
263 Char separator = ' ',
264 const String& commentMarkerRegex = "",
265 Int firstLine = 1, Int lastLine = -1);
266// </group>
267
268// Similar versions as above, but returning a Table object.
269// The format string is returned in the first argument.
270// The type of Table can be given (Plain or Memory).
271// <group>
272Table readAsciiTable (String& formatString, Table::TableType tableType,
273 const String& filein, const String& tableDescName,
274 const String& tableName, Bool autoHeader = False,
275 Char separator = ' ',
276 const String& commentMarkerRegex = "",
277 Int firstLine = 1, Int lastLine = -1,
278 const IPosition& autoShape = IPosition());
279Table readAsciiTable (String& formatString, Table::TableType tableType,
280 const String& filein, const String& tableproto,
281 const String& tablename,
282 const Vector<String>& columnNames,
283 const Vector<String>& dataTypes,
284 Char separator, const String& commentMarkerRegex,
285 Int firstLine, Int lastLine);
286Table readAsciiTable (String& formatString, Table::TableType tableType,
287 const String& headerFile, const String& dataFile,
288 const String& tableDescName, const String& tablename,
289 Char separator = ' ',
290 const String& commentMarkerRegex = "",
291 Int firstLine = 1, Int lastLine = -1);
292Table readAsciiTable (String& formatString, Table::TableType tableType,
293 const String& headerFile, const String& dataFile,
294 const String& tableDescName, const char* tablename,
295 Char separator = ' ',
296 const String& commentMarkerRegex = "",
297 Int firstLine = 1, Int lastLine = -1);
298// </group>
299
300// </group>
301
302
303
304
305// <summary>
306// Helper class for readAsciiTable
307// </summary>
308// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
309// </reviewed>
310
311// <use visibility=local>
312
313// <synopsis>
314// This class contains static functions as helpers for readAsciiTable.
315// </synopsis>
316
318{
319public:
320 // Run the readAsciiTable.
321 static String run (const String& headerfile, const String& filein,
322 const String& tableproto, const String& tablename,
323 Bool autoHeader, const IPosition& autoShape,
324 const Vector<String>& columnNames,
325 const Vector<String>& dataTypes,
326 Char separator,
327 const String& commentMarkerRegex,
328 Int firstLine, Int lastLine);
329 static Table runt (String& formatString, Table::TableType tableType,
330 const String& headerfile, const String& filein,
331 const String& tableproto, const String& tablename,
332 Bool autoHeader, const IPosition& autoShape,
333 const Vector<String>& columnNames,
334 const Vector<String>& dataTypes,
335 Char separator,
336 const String& commentMarkerRegex,
337 Int firstLine, Int lastLine);
338
339 // Read a position using MVAngle.
340 // If isDMS is True, a position with : is treated as DMS instead of HMS.
341 // This function is a bit more relaxed than MVAngle::read.
342 // It allows whitespace. Furthermore it allows whitespace as separator :.
343 static double stringToPos (const String& pos, Bool isDMS);
344
345private:
346 // Define types.
349
350
351 // Do the actual run.
352 static String doRun (const String& headerfile, const String& filein,
353 const String& tableproto, const String& tablename,
354 Bool autoHeader, const IPosition& autoShape,
355 const Vector<String>& columnNames,
356 const Vector<String>& dataTypes,
357 Char separator,
358 Bool testComment, const Regex& commentMarker,
359 Int firstLine, Int lastLine);
360
361 // Do the actual work of making and filling the table.
362 static Table makeTab (String& formatString, Table::TableType tableType,
363 const String& headerfile, const String& filein,
364 const String& tableproto,
365 const String& tablename,
366 Bool autoHeader, const IPosition& autoShape,
367 const Vector<String>& columnNames,
368 const Vector<String>& dataTypes,
369 Char separator,
370 Bool testComment, const Regex& commentMarker,
371 Int firstLine, Int lastLine);
372
373 // Get the next line. Skip lines to be ignored.
374 // It returns False when no more lines are available.
375 static Bool getLine (ifstream& file, Int& lineNumber,
376 char* line, Int lineSize,
377 Bool testComment, const Regex& commentMarker,
378 Int firstLine, Int lastLine);
379
380 // Get the next part of the line using the separator as delimiter.
381 // Leading blanks are ignored.
382 static Int getNext (const Char* string, Int strlen, Char* result,
383 Int& at, Char separator);
384
385 // Derive the types from the values in the first data line.
386 static void getTypes (const IPosition& shape,
387 const Char* in, Int leng,
388 Char* string1, Char* string2, Char separator);
389
390 // Turn the string into a Bool value.
391 // Empty string, value 0 and any value starting with f, F, n or N are False.
392 static Bool makeBool (const String& str);
393
394 // Handle a keyword set.
395 static void handleKeyset (Int lineSize, char* string1,
396 char* first, char* second,
397 TableRecord& keysets,
398 LogIO& logger,
399 const String& fileName,
400 ifstream& jFile,
401 Int& lineNumber,
402 Char separator,
403 Bool testComment,
404 const Regex& commentMarker,
405 Int firstLine, Int lastLine);
406
407 // Get the shape and type from the type string.
408 static Int getTypeShape (const String& typestr,
409 IPosition& shape, Int& type);
410
411 // Get the next scalar value with the given type from string1.
412 static Bool getValue (char* string1, Int lineSize, char* first,
413 Int& at1, Char separator,
414 Int type, void* value);
415
416 // Handle the next scalar with the given type from the data line and
417 // put it into the table column.
418 static void handleScalar (char* string1, Int lineSize, char* first,
419 Int& at1, Char separator,
420 Int type,
421 TableColumn& tabcol, rownr_t rownr);
422
423 // Get the next array with the given type from string1.
424 // It returns the shape (for variable shaped arrays).
425 static IPosition getArray (char* string1, Int lineSize, char* first,
426 Int& at1, Char separator,
427 const IPosition& shape, Int varAxis,
428 Int type, void* valueBlock);
429
430 // Get the next array with the given type from the data line and
431 // put it into the table column.
432 static void handleArray (char* string1, Int lineSize, char* first,
433 Int& at1, Char separator,
434 const IPosition& shape, Int varAxis,
435 Int type,
436 TableColumn& tabcol, rownr_t rownr);
437};
438
439
440
441} //# NAMESPACE CASACORE - END
442
443#endif
Helper class for readAsciiTable.
static Int getNext(const Char *string, Int strlen, Char *result, Int &at, Char separator)
Get the next part of the line using the separator as delimiter.
static IPosition getArray(char *string1, Int lineSize, char *first, Int &at1, Char separator, const IPosition &shape, Int varAxis, Int type, void *valueBlock)
Get the next array with the given type from string1.
static Bool getLine(ifstream &file, Int &lineNumber, char *line, Int lineSize, Bool testComment, const Regex &commentMarker, Int firstLine, Int lastLine)
Get the next line.
static void getTypes(const IPosition &shape, const Char *in, Int leng, Char *string1, Char *string2, Char separator)
Derive the types from the values in the first data line.
static Table runt(String &formatString, Table::TableType tableType, const String &headerfile, const String &filein, const String &tableproto, const String &tablename, Bool autoHeader, const IPosition &autoShape, const Vector< String > &columnNames, const Vector< String > &dataTypes, Char separator, const String &commentMarkerRegex, Int firstLine, Int lastLine)
static Table makeTab(String &formatString, Table::TableType tableType, const String &headerfile, const String &filein, const String &tableproto, const String &tablename, Bool autoHeader, const IPosition &autoShape, const Vector< String > &columnNames, const Vector< String > &dataTypes, Char separator, Bool testComment, const Regex &commentMarker, Int firstLine, Int lastLine)
Do the actual work of making and filling the table.
static void handleKeyset(Int lineSize, char *string1, char *first, char *second, TableRecord &keysets, LogIO &logger, const String &fileName, ifstream &jFile, Int &lineNumber, Char separator, Bool testComment, const Regex &commentMarker, Int firstLine, Int lastLine)
Handle a keyword set.
static String doRun(const String &headerfile, const String &filein, const String &tableproto, const String &tablename, Bool autoHeader, const IPosition &autoShape, const Vector< String > &columnNames, const Vector< String > &dataTypes, Char separator, Bool testComment, const Regex &commentMarker, Int firstLine, Int lastLine)
Do the actual run.
static void handleScalar(char *string1, Int lineSize, char *first, Int &at1, Char separator, Int type, TableColumn &tabcol, rownr_t rownr)
Handle the next scalar with the given type from the data line and put it into the table column.
static double stringToPos(const String &pos, Bool isDMS)
Read a position using MVAngle.
static Bool getValue(char *string1, Int lineSize, char *first, Int &at1, Char separator, Int type, void *value)
Get the next scalar value with the given type from string1.
static void handleArray(char *string1, Int lineSize, char *first, Int &at1, Char separator, const IPosition &shape, Int varAxis, Int type, TableColumn &tabcol, rownr_t rownr)
Get the next array with the given type from the data line and put it into the table column.
static Int getTypeShape(const String &typestr, IPosition &shape, Int &type)
Get the shape and type from the type string.
static Bool makeBool(const String &str)
Turn the string into a Bool value.
static String run(const String &headerfile, const String &filein, const String &tableproto, const String &tablename, Bool autoHeader, const IPosition &autoShape, const Vector< String > &columnNames, const Vector< String > &dataTypes, Char separator, const String &commentMarkerRegex, Int firstLine, Int lastLine)
Run the readAsciiTable.
String: the storage and methods of handling collections of characters.
Definition String.h:223
TableType
Define the possible table types.
Definition Table.h:187
struct Node * first
Definition malloc.h:328
this file contains all the compiler specific defines
Definition mainpage.dox:28
const Bool False
Definition aipstype.h:42
TableExprNode shape(const TableExprNode &array)
Function operating on any scalar or array resulting in a Double array containing the shape.
Definition ExprNode.h:1991
int Int
Definition aipstype.h:48
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
LatticeExprNode value(const LatticeExprNode &expr)
This function returns the value of the expression without a mask.
uInt64 rownr_t
Define the type of a row number in a table.
Definition aipsxtype.h:44
char Char
Definition aipstype.h:44
String readAsciiTable(const String &headerFile, const String &dataFile, const String &tableDescName, const char *tablename, Char separator=' ', const String &commentMarkerRegex="", Int firstLine=1, Int lastLine=-1)
String readAsciiTable(const String &headerFile, const String &dataFile, const String &tableDescName, const String &tablename, Char separator=' ', const String &commentMarkerRegex="", Int firstLine=1, Int lastLine=-1)
This form reads TWO Ascii files.
Table readAsciiTable(String &formatString, Table::TableType tableType, const String &filein, const String &tableproto, const String &tablename, const Vector< String > &columnNames, const Vector< String > &dataTypes, Char separator, const String &commentMarkerRegex, Int firstLine, Int lastLine)
String readAsciiTable(const String &filein, const String &tableproto, const String &tablename, const Vector< String > &columnNames, const Vector< String > &dataTypes, Char separator, const String &commentMarkerRegex, Int firstLine, Int lastLine)
This form gets the header info in the given vectors.
String readAsciiTable(const String &filein, const String &tableDescName, const String &tableName, Bool autoHeader=False, Char separator=' ', const String &commentMarkerRegex="", Int firstLine=1, Int lastLine=-1, const IPosition &autoShape=IPosition())
Create a table with name as given by tableName.
Table readAsciiTable(String &formatString, Table::TableType tableType, const String &headerFile, const String &dataFile, const String &tableDescName, const String &tablename, Char separator=' ', const String &commentMarkerRegex="", Int firstLine=1, Int lastLine=-1)
Table readAsciiTable(String &formatString, Table::TableType tableType, const String &filein, const String &tableDescName, const String &tableName, Bool autoHeader=False, Char separator=' ', const String &commentMarkerRegex="", Int firstLine=1, Int lastLine=-1, const IPosition &autoShape=IPosition())
Similar versions as above, but returning a Table object.
Table readAsciiTable(String &formatString, Table::TableType tableType, const String &headerFile, const String &dataFile, const String &tableDescName, const char *tablename, Char separator=' ', const String &commentMarkerRegex="", Int firstLine=1, Int lastLine=-1)