Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

# 

# LSST Data Management System 

# 

# Copyright 2008-2017 AURA/LSST. 

# 

# This product includes software developed by the 

# LSST Project (http://www.lsst.org/). 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 3 of the License, or 

# (at your option) any later version. 

# 

# This program is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU General Public License for more details. 

# 

# You should have received a copy of the LSST License Statement and 

# the GNU General Public License along with this program. If not, 

# see <https://www.lsstcorp.org/LegalNotices/>. 

# 

 

__all__ = ["ReadTextCatalogConfig", "ReadTextCatalogTask"] 

 

import numpy as np 

 

import lsst.pex.config as pexConfig 

import lsst.pipe.base as pipeBase 

 

 

class ReadTextCatalogConfig(pexConfig.Config): 

header_lines = pexConfig.Field( 

dtype=int, 

default=0, 

doc='Number of lines to skip when reading the text reference file.' 

) 

colnames = pexConfig.ListField( 

dtype=str, 

default=[], 

doc="An ordered list of column names to use in ingesting the catalog. " 

"With an empty list, column names will be discovered from the first line " 

"after the skipped header lines." 

) 

delimiter = pexConfig.Field( 

dtype=str, 

default=',', 

doc='Delimiter to use when reading text reference files. Comma is default.' 

) 

 

## @addtogroup LSST_task_documentation 

## @{ 

## @page ReadTextCatalogTask 

## @ref ReadTextCatalogTask_ "ReadTextCatalogTask" 

## @copybrief ReadTextCatalogTask 

## @} 

 

 

class ReadTextCatalogTask(pipeBase.Task): 

r"""!Read an object catalog from a text file 

 

@anchor ReadTextCatalogTask_ 

 

@section meas_algorithms_readTextCatalog_Contents Contents 

 

- @ref meas_algorithms_readTextCatalog_Purpose 

- @ref meas_algorithms_readTextCatalog_Initialize 

- @ref meas_algorithms_readTextCatalog_Config 

- @ref meas_algorithms_readTextCatalog_Example 

 

@section meas_algorithms_readTextCatalog_Purpose Description 

 

Read an object catalog from a text file. Designed to read foreign catalogs 

so they can be written out in a form suitable for IngestIndexedReferenceTask. 

 

@section meas_algorithms_readTextCatalog_Initialize Task initialisation 

 

@copydoc \_\_init\_\_ 

 

@section meas_algorithms_readTextCatalog_Config Configuration parameters 

 

See @ref ReadTextCatalogConfig 

 

@section meas_algorithms_readTextCatalog_Example A complete example of using ReadTextCatalogTask 

 

Given a file named `table.csv` containing the following: 

 

ra dec flux 

5.5, -45.2, 12453 

19.6, 34.2, 32123 

 

you can read this file with the following code: 

 

from lsst.meas.algorithms.readTextCatalogTask import ReadTextCatalogTask 

task = ReadTextCatalogTask() 

catalogArray = task.run("table.csv") 

 

The resulting `catalogArray` is a numpy structured array containing three fields 

("ra", "dec" and "flux") and two rows of data. For more complex cases, 

config parameters allow you to specify the names of the columns (instead of using automatic discovery) 

and set the number of rows to skip. 

""" 

_DefaultName = 'readCatalog' 

ConfigClass = ReadTextCatalogConfig 

 

def run(self, filename): 

"""Read an object catalog from the specified text file 

 

@param[in] filename path to text file 

@return a numpy structured array containing the specified columns 

""" 

names = True 

if self.config.colnames: 

names = self.config.colnames 

arr = np.genfromtxt(filename, dtype=None, skip_header=self.config.header_lines, 

delimiter=self.config.delimiter, 

names=names) 

# This is to explicitly convert the bytes type into unicode for any column that is read in as bytes 

# string 

newDtype = [] 

for name in arr.dtype.names: 

value = arr.dtype[name] 

if value.kind == 'S': 

value = np.dtype('|U{}'.format(value.itemsize)) 

newDtype.append((name, value)) 

arr = arr.astype(newDtype) 

 

# Just in case someone has only one line in the file. 

return np.atleast_1d(arr)