Next Previous Up Contents
Next: Examples
Up: tgroup: Calculates aggregate functions on groups of rows
Previous: tgroup: Calculates aggregate functions on groups of rows

### B.34.1 Usage

The usage of `tgroup` is

       stilts <stilts-flags> tgroup ifmt=<in-format> istream=true|false
                                    icmd=<cmds> ocmd=<cmds>
                                    omode=out|meta|stats|count|checksum|cgi|discard|topcat|samp|plastic|tosql|gui
                                    out=<out-table> ofmt=<out-format>
                                    keys=<expr> ...
                                    aggcols=<expr>;<aggregator>[;<name>] ...
                                    runner=sequential|parallel|parallel<n>|partest
                                    sort=true|false cache=true|false
                                    [in=]<table>

If you don't have the `stilts` script installed, write "`java -jar stilts.jar`" instead of "`stilts`" - see Section 3. The available `<stilts-flags>` are listed in Section 2.1. For programmatic invocation, the Task class for this command is `uk.ac.starlink.ttools.task.TableGroup`.

Parameter values are assigned on the command line as explained in Section 2.3. They are as follows:

`aggcols = <expr>;<aggregator>[;<name>] ...` (String[])
  Defines the aggregate quantities to be calculated for each group of input rows. Each quantity is defined by one entry in this list; entries are space-separated, or can be given by multiple instances of this parameter on the command line.

  Each entry is composed of two or three tokens, separated by semicolon ("`;`") characters:

  - `<expr>`: (required) column name, or expression using the expression language, for the quantity to be aggregated
  - `<aggregator>`: (required) aggregation method
  - `<name>`: (optional) name of output column; if omitted, a name based on the `<expr>` value will be used

  The available `<aggregator>` values are as follows:

  - `count`: counts the number of rows
  - `ngood`: counts the number of non-blank items
  - `sum`: the sum of all the combined values per bin
  - `mean`: the mean of the combined values
  - `median`: the median
  - `stdev`: the sample standard deviation of the combined values
  - `stdev-pop`: the population standard deviation of the combined values
  - `max`: records the maximum value
  - `min`: records the minimum value
  - `array`: collects all non-blank values into an array
  - `array-withblanks`: collects all values into an array; blank values are represented as zero for integers
  - `count-long`: counts the number of rows, works for >2 billion
  - `ngood-long`: counts the number of non-blank items, works for >2 billion
  - `Q.nnn`: quantile nnn (e.g. Q.05 is the fifth percentile)
`cache = true|false` (Boolean)
  Determines whether the results of the aggregation operation will be cached in random-access storage before output. This is set true by default, since accessing rows of the calculated table may be somewhat expensive, and most uses of the results will need all of the cells. But if you anticipate making only a small number of accesses to the output table cells, it could be more efficient to set this false.

  [Default: `true`]

`icmd = <cmds>` (ProcessingStep[])
  Specifies processing to be performed on the input table as specified by parameter `in`, before any other processing has taken place. The value of this parameter is one or more of the filter commands described in Section 6.1. If more than one is given, they must be separated by semicolon characters (";"). This parameter can be repeated multiple times on the same command line to build up a list of processing steps. The sequence of commands given in this way defines the processing pipeline which is performed on the table.

  Commands may alternatively be supplied in an external file, by using the indirection character '`@`'. Thus a value of "`@filename`" causes the file `filename` to be read for a list of filter commands to execute. The commands in the file may be separated by newline characters and/or semicolons, and lines which are blank or which start with a '`#`' character are ignored. A backslash character '`\`' at the end of a line joins it with the following line.

`ifmt = <in-format>` (String)
  Specifies the format of the input table as specified by parameter `in`. The known formats are listed in Section 5.1.1. This flag can be used if you know what format your table is in. If it has the special value `(auto)` (the default), then an attempt will be made to detect the format of the table automatically. This cannot always be done correctly however, in which case the program will exit with an error explaining which formats were attempted. This parameter is ignored for scheme-specified tables.

  [Default: `(auto)`]

`in = <table>` (StarTable)
  The location of the input table. This may take one of the following forms:
  - A filename.
  - A URL.
  - The special value "`-`", meaning standard input. In this case the input format must be given explicitly using the `ifmt` parameter. Note that not all formats can be streamed in this way.
  - A scheme specification of the form `:<scheme-name>:<scheme-args>`.
  - A system command line with either a "`<`" character at the start, or a "`|`" character at the end ("`<syscmd`" or "`syscmd|`"). This executes the given pipeline and reads from its standard output. This will probably only work on unix-like systems.
  In any case, compressed data in one of the supported compression formats (gzip, Unix compress or bzip2) will be decompressed transparently.
`istream = true|false` (Boolean)
  If set true, the input table specified by the `in` parameter will be read as a stream. It is necessary to give the `ifmt` parameter in this case. Depending on the required operations and processing mode, this may cause the read to fail (sometimes it is necessary to read the table more than once). It is not normally necessary to set this flag; in most cases the data will be streamed automatically if that is the best thing to do. However it can sometimes result in less resource usage when processing large files in certain formats (such as VOTable). This parameter is ignored for scheme-specified tables.

  [Default: `false`]

`keys = <expr> ...` (String[])
  List of one or more space-separated words defining the groups within which aggregation should be done. Each word can be a column name or an expression using the expression language. Each expression will appear as one of the columns in the output table. This list corresponds to the contents of an ADQL/SQL `GROUP BY` clause.
`ocmd = <cmds>` (ProcessingStep[])
  Specifies processing to be performed on the output table, after all other processing has taken place. The value of this parameter is one or more of the filter commands described in Section 6.1. If more than one is given, they must be separated by semicolon characters (";"). This parameter can be repeated multiple times on the same command line to build up a list of processing steps. The sequence of commands given in this way defines the processing pipeline which is performed on the table.

  Commands may alternatively be supplied in an external file, by using the indirection character '`@`'. Thus a value of "`@filename`" causes the file `filename` to be read for a list of filter commands to execute. The commands in the file may be separated by newline characters and/or semicolons, and lines which are blank or which start with a '`#`' character are ignored. A backslash character '`\`' at the end of a line joins it with the following line.

`ofmt = <out-format>` (String)
  Specifies the format in which the output table will be written (one of the ones in Section 5.1.2 - matching is case-insensitive and you can use just the first few letters). If it has the special value "`(auto)`" (the default), then the output filename will be examined to try to guess what sort of file is required usually by looking at the extension. If it's not obvious from the filename what output format is intended, an error will result.

  This parameter must only be given if `omode` has its default value of "`out`".

  [Default: `(auto)`]

`omode = out|meta|stats|count|checksum|cgi|discard|topcat|samp|plastic|tosql|gui` (ProcessingMode)
  The mode in which the result table will be output. The default mode is `out`, which means that the result will be written as a new table to disk or elsewhere, as determined by the `out` and `ofmt` parameters. However, there are other possibilities, which correspond to uses to which a table can be put other than outputting it, such as displaying metadata, calculating statistics, or populating a table in an SQL database. For some values of this parameter, additional parameters (`<mode-args>`) are required to determine the exact behaviour.

  Possible values are

  - `out`
  - `meta`
  - `stats`
  - `count`
  - `checksum`
  - `cgi`
  - `discard`
  - `topcat`
  - `samp`
  - `plastic`
  - `tosql`
  - `gui`
  Use the `help=omode` flag or see Section 6.4 for more information.

  [Default: `out`]

`out = <out-table>` (TableConsumer)
  The location of the output table. This is usually a filename to write to. If it is equal to the special value "-" (the default) the output table will be written to standard output.

  This parameter must only be given if `omode` has its default value of "`out`".

  [Default: `-`]

`runner = sequential|parallel|parallel<n>|partest` (RowRunner)
  Selects the threading implementation, i.e. to what extent processing is done in parallel. The options are currently:
  - `sequential`: runs using only a single thread
  - `parallel`: runs using multiple threads for large tables, with parallelism given by the number of available processors
  - `parallel<n>`: runs using multiple threads for large tables, with parallelism given by the supplied value `<n>`
  - `partest`: runs using multiple threads even when tables are small (only intended for testing purposes)

  Using parallel processing can speed up execution considerably; however, depending on the I/O operations required, it can also slow it down by disrupting patterns of disk access. If the content of a file is on a solid state disk, or is already in cache for instance because a similar command has been run recently, then `parallel` will probably be faster. However, if the data is being read directly from a spinning disk, for instance because the file is too large to fit in RAM, then `sequential` or `parallel<n>` with a small `<n>` may be faster.

  The value of this parameter should make only very tiny differences to the output table. If you notice significant discrepancies please report them.

  [Default: `parallel`]

`sort = true|false` (Boolean)
  Determines whether an attempt is made to sort the output table by the values of the `keys` expressions. This may not be possible if no sort order is defined on the keys.

  In most cases such sorting will be a small overhead on the rest of the work done by this task, so the default is `true` but if ordering by key is not useful you may save some resources by setting it `false`. If no sorting is done, the output row order is undefined.

  [Default: `true`]

Next Previous Up Contents
Next: Examples
Up: tgroup: Calculates aggregate functions on groups of rows
Previous: tgroup: Calculates aggregate functions on groups of rows

STILTS - Starlink Tables Infrastructure Library Tool Set
Starlink User Note256
STILTS web page: http://www.starlink.ac.uk/stilts/
Author email: m.b.taylor@bristol.ac.uk
Mailing list: topcat-user@jiscmail.ac.uk