Catalogs and Input Dictionaries

class galsim.Catalog(file_name, dir=None, file_type=None, comments='#', hdu=1)[source]

A class storing the data from an input catalog.

Each row corresponds to a different object to be built, and each column stores some item of information about that object (e.g. flux or half_light_radius).

Parameters:
  • file_name – Filename of the input catalog. (Required)

  • dir – Optionally a directory name can be provided if file_name does not already include it.

  • file_type – Either ‘ASCII’ or ‘FITS’. If None, infer from file_name ending. [default: None]

  • comments – The character used to indicate the start of a comment in an ASCII catalog. [default: ‘#’]

  • hdu – Which hdu to use for FITS files. [default: 1]

After construction, the following attributes are available:

Attributes:
  • nobjects – The number of objects in the catalog.

  • ncols – The number of columns in the catalog.

  • isfits – Whether the catalog is a fits catalog.

  • names – For a fits catalog, the valid column names.

get(index, col)[source]

Return the data for the given index and col in its native type.

For ASCII catalogs, col is the column number. For FITS catalogs, col is a string giving the name of the column in the FITS table.

Also, for ASCII catalogs, the “native type” is always str. For FITS catalogs, it is whatever type is specified for each field in the binary table.

getFloat(index, col)[source]

Return the data for the given index and col as a float if possible

getInt(index, col)[source]

Return the data for the given index and col as an int if possible

readAscii()[source]

Read in an input catalog from an ASCII file.

readFits()[source]

Read in an input catalog from a FITS file.

class galsim.OutputCatalog(names, types=None, _rows=(), _sort_keys=())[source]

A class for building up a catalog for output, typically storing truth information about a simulation.

Each row corresponds to a different object, and each column stores some item of information about that object (e.g. flux or half_light_radius).

Note: no type checking is done when the data are added in addRow(). It is up to the user to make sure that the values added for each row are compatible with the types given here in the types parameter.

Parameters:
  • names – A list of names for the output columns.

  • types – A list of types for the output columns. [default: None, which assumes all columns are float]

After construction, the following attributes are available:

Attributes:
  • nobjects – The number of objects so far in the catalog.

  • ncols – The number of columns in the catalog.

  • names – The names of the columns.

  • types – The types of the columns.

  • rows – The rows of data that have been accumulated so far.

addRow(row, sort_key=None)[source]

Add a row of data to the catalog.

Warning: no type checking is done at this point. If the values in the row do not match the column types, you may get an error when writing, or you may lose precision, depending on the nature of the mismatch.

Parameters:
  • row – A list with one item per column in the same order as the names list.

  • sort_key – If the rows may be added out of order, you can provide a sort_key, which will be used at the end to re-sort the rows.

getNCols()[source]

Equivalent to sef.ncols.

getNObjects()[source]

Equivalent to sef.nobjects.

getNames()[source]

Equivalent to sef.names.

getTypes()[source]

Equivalent to sef.types.

makeData()[source]

Returns a numpy array of the data as it should be written to an output file.

property ncols

The number of columns in the OutputCatalog.

property nobjects

The number of objects in the OutputCatalog.

setTypes(types)[source]

Equivalent to sef.types = types.

write(file_name, dir=None, file_type=None, prec=8)[source]

Write the catalog to a file.

Parameters:
  • file_name – The name of the file to write to.

  • dir – Optionally a directory name can be provided if file_name does not already include it. [default: None]

  • file_type – Which kind of file to write to. [default: determine from the file_name extension]

  • prec – Output precision for ASCII. [default: 8]

writeAscii(file_name, prec=8)[source]

Write catalog to an ASCII file.

Parameters:
  • file_name – The name of the file to write to.

  • prec – Output precision for floats. [default: 8]

writeFits(file_name)[source]

Write catalog to a FITS file.

Parameters:

file_name – The name of the file to write to.

writeFitsHdu()[source]

Write catalog to a FITS hdu.

Returns:

an HDU with the FITS binary table of the catalog.

class galsim.Dict(file_name, dir=None, file_type=None, key_split='.')[source]

A class that reads a python dict from a file.

After construction, it behaves like a regular python dict, with one exception. In order to facilitate getting values in a hierarchy of fields, we allow the ‘.’ character to chain keys together for the get() method. So,:

>>> d.get('noise.properties.variance')

is expanded into:

>>> d['noise']['properties']['variance']

Furthermore, if a “key” is really an integer, then it is used as such, which accesses the corresponding element in a list. e.g.:

>>> d.get('noise_models.2.variance')

is equivalent to:

>>> d['noise_models'][2]['variance']

This makes it much easier to access arbitrary elements within parameter files.

Caveat: The above prescription means that an element whose key really has a ‘.’ in it won’t be accessed correctly. This is probably a rare occurrence, but the workaround is to set key_split to a different character or string and use that to chain the keys.

Parameters:
  • file_name – Filename storing the dict.

  • dir – Optionally a directory name can be provided if file_name does not already include it. [default: None]

  • file_type – Options are ‘Pickle’, ‘YAML’, or ‘JSON’ or None. If None, infer from file_name extension (‘.p*’, ‘.y*’, ‘.j*’ respectively). [default: None]

  • key_split – The character (or string) to use to split chained keys. (cf. the description of this feature above.) [default: ‘.’]