# # Some objects in the GOODS field # unknown 189.2207323 62.2357983 26.87 0.32 galaxy 189.1408929 62.2376331 24.97 0.15 star 189.1409453 62.1696844 25.30 0.12 galaxy 188.9014716 62.2037839 25.95 0.20
>>> import asciidata
>>> example = asciidata.open('example.txt')
>>> print str(example) # # Some objects in the GOODS field # unknown 189.2207323 62.2357983 26.87 0.32 galaxy 189.1408929 62.2376331 24.97 0.15 star 189.1409453 62.1696844 25.30 0.12 galaxy 188.9014716 62.2037839 25.95 0.20
>>> sum1=0.0 >>> sum2=0.0 >>> for index in range(example.nrows): ... sum1 += example[1][index] ... sum2 += example[2][index] ... >>> ave1 = sum1/example.nrows >>> ave2 = sum2/example.nrows >>> print ave1, ave2 189.101010525 62.211724925Please note that indices start with 0, so the first row in the first column is
example[0][0]
.
>>> example.writeto('example_orig.txt')This gives you a file 'example_orig.txt', which is identical to the original 'example.txt'.
>>> for index in range(example.nrows): ... example['diff1'][index] = example[1][index] - ave1 ... example['diff2'][index] = example[2][index] - ave2 ... >>> print str(example) # # Some objects in the GOODS field # unknown 189.2207323 62.2357983 26.87 0.32 1.197218e-01 2.407338e-02 galaxy 189.1408929 62.2376331 24.97 0.15 3.988237e-02 2.590817e-02 star 189.1409453 62.1696844 25.30 0.12 3.993477e-02 -4.204052e-02 galaxy 188.9014716 62.2037839 25.95 0.20 -1.995389e-01 -7.941025e-03There are two new columns, which were created by addressing elements in an unknown column with the name 'diff1' and 'diff2'.
>>> example.header.append('Nov 16 2005: computed and stored differences!') >>> print str(example) # # Some objects in the GOODS field # # Nov 16 2005: computed and stored differences! unknown 189.2207323 62.2357983 26.87 0.32 1.197218e-01 2.407338e-02 galaxy 189.1408929 62.2376331 24.97 0.15 3.988237e-02 2.590817e-02 star 189.1409453 62.1696844 25.30 0.12 3.993477e-02 -4.204052e-02 galaxy 188.9014716 62.2037839 25.95 0.20 -1.995389e-01 -7.941025e-03There is a new commented line at the beginning of the table with your note.
>>> example.flush()Now the file 'example.txt' also has the two new columns.
>>> print example.info() File: example.txt Ncols: 7 Nrows: 4 Delimiter: None Null value: ['Null', 'NULL', 'None', '*'] Comment: # Column name: column1 Column type: <type 'str'> Column format: ['% 7s', '%7s'] Column null value : ['Null'] Column name: column2 Column type: <type 'float'> Column format: ['% 11.7f', '%12s'] Column null value : ['Null'] Column name: column3 Column type: <type 'float'> Column format: ['% 10.7f', '%11s'] Column null value : ['Null'] Column name: column4 Column type: <type 'float'> Column format: ['% 5.2f', '%6s'] Column null value : ['Null'] Column name: column5 Column type: <type 'float'> Column format: ['% 4.2f', '%5s'] Column null value : ['Null'] Column name: diff1 Column type: <type 'float'> Column format: ['% 12.6e', '%13s'] Column null value : ['Null'] Column name: diff2 Column type: <type 'float'> Column format: ['% 12.6e', '%13s'] Column null value : ['Null']So the original columns had default names such as 'column1', 'column2', ... Moreover the method
info()
returns the column
type and format for every column .
>>> import asciidata >>> example2 = asciidata.create(4,10) >>> print example2 Null Null Null Null Null Null Null Null Null Null Null Null Null Null Null Null Null Null Null Null Null Null Null Null Null Null Null Null Null Null Null Null Null Null Null Null Null Null Null NullThe object has four columns with ten rows, all empty so far.
>>> for index in range(example2.nrows): ... example2[0][index] = index+1 ... >>> print example2 1 Null Null Null 2 Null Null Null 3 Null Null Null 4 Null Null Null 5 Null Null Null 6 Null Null Null 7 Null Null Null 8 Null Null Null 9 Null Null Null 10 Null Null NullFor convenience the index starts with 1.
>>> for cindex in range(1, example2.ncols): ... for rindex in range(example2.nrows): ... example2[cindex][rindex] = 0.3*float(example2[0][rindex])**cindex ... >>> print example2 1 3.000000e-01 3.000000e-01 3.000000e-01 2 6.000000e-01 1.200000e+00 2.400000e+00 3 9.000000e-01 2.700000e+00 8.100000e+00 4 1.200000e+00 4.800000e+00 1.920000e+01 5 1.500000e+00 7.500000e+00 3.750000e+01 6 1.800000e+00 1.080000e+01 6.480000e+01 7 2.100000e+00 1.470000e+01 1.029000e+02 8 2.400000e+00 1.920000e+01 1.536000e+02 9 2.700000e+00 2.430000e+01 2.187000e+02 10 3.000000e+00 3.000000e+01 3.000000e+02Please note that the column type of the first column is int since only integer type data was entered. In all other columns numbers of type float were entered, hence their column type is also float.
>>> example2[0][1] = 2.0 >>> print example2 1.000000e+00 3.000000e-01 3.000000e-01 3.000000e-01 2.000000e+00 6.000000e-01 1.200000e+00 2.400000e+00 3.000000e+00 9.000000e-01 2.700000e+00 8.100000e+00 4.000000e+00 1.200000e+00 4.800000e+00 1.920000e+01 5.000000e+00 1.500000e+00 7.500000e+00 3.750000e+01 6.000000e+00 1.800000e+00 1.080000e+01 6.480000e+01 7.000000e+00 2.100000e+00 1.470000e+01 1.029000e+02 8.000000e+00 2.400000e+00 1.920000e+01 1.536000e+02 9.000000e+00 2.700000e+00 2.430000e+01 2.187000e+02 1.000000e+01 3.000000e+00 3.000000e+01 3.000000e+02Now the first column is of type float as well.
>>> newcol = example2.append('comment') >>> example2[newcol][0] = 'small!' >>> example2[newcol][2] = 'bigger!' >>> example2[newcol][7] = 'Huge!' >>> print example2 1.000000e+00 3.000000e-01 3.000000e-01 3.000000e-01 small! 2.000000e+00 6.000000e-01 1.200000e+00 2.400000e+00 Null 3.000000e+00 9.000000e-01 2.700000e+00 8.100000e+00 bigger! 4.000000e+00 1.200000e+00 4.800000e+00 1.920000e+01 Null 5.000000e+00 1.500000e+00 7.500000e+00 3.750000e+01 Null 6.000000e+00 1.800000e+00 1.080000e+01 6.480000e+01 Null 7.000000e+00 2.100000e+00 1.470000e+01 1.029000e+02 Null 8.000000e+00 2.400000e+00 1.920000e+01 1.536000e+02 Huge! 9.000000e+00 2.700000e+00 2.430000e+01 2.187000e+02 Null 1.000000e+01 3.000000e+00 3.000000e+01 3.000000e+02 NullObviously it also is possible to create a new column using the AsciiData append() method. This method returns the column number, which then can be used to fill the new column.
>>> example2.flush() Traceback (most recent call last): File "<stdin>", line 1, in ? File ".../site-packages/asciidata/asciidata.py", line 279, in flush raise "No filename given. Use 'writeto()' instead." No filename given. Use 'writeto()' instead. >>> example2.writeto('example2.txt')Since the object was created from scratch, there is no filename associated with and the method flush() can not be used. The method writeto() must be used instead!
# 1 NUMBER Running object number # 2 MAG_APER Fixed aperture magnitude vector [mag] # 5 MAGERR_APER RMS error vector for fixed aperture mag. [mag] # 8 FLUX_AUTO Flux within a Kron-like elliptical aperture [count] # 9 FLUXERR_AUTO RMS error for AUTO flux [count] # 10 X_IMAGE Object position along x [pixel] # 11 Y_IMAGE Object position along y [pixel] # 12 FLAGS Extraction flags 1 -7.1135 -9.9589 -11.4873 0.1151 0.0168 0.0082 52533.1 580.708 379.715 72.461 3 2 -7.8412 -9.5452 -10.8191 0.0591 0.0246 0.0152 171543 2014.45 341.365 320.621 9 3 -8.1548 -9.6216 -11.0307 0.0444 0.0229 0.0125 267764 1844.97 379.148 196.397 3 4 -9.4534 -11.0534 -11.9600 0.0134 0.0061 0.0053 178541 1290.41 367.213 123.803 3 5 -7.8104 -9.1967 -10.5169 0.0609 0.0339 0.0201 131343 1648.34 305.545 307.027 3 6 -12.1666 -13.2193 -14.1293 0.0011 0.0008 0.0007 2.22738 1938.01 258.692 260.341 3 7 -8.8319 -10.3340 -11.3343 0.0238 0.0119 0.0095 111597 1525.78 336.462 97.060 3 8 -8.9203 -10.3532 -11.7252 0.0219 0.0117 0.0066 129934 917.641 177.377 199.843 3 9 -7.5366 -9.0374 -10.3321 0.0784 0.0393 0.0238 72761.7 1603.15 94.196 131.380 3 10 -6.7963 -8.4304 -9.5685 0.1552 0.0687 0.0482 14072 895.465 265.404 46.241 3
Please note the the jump in the column numbers from #2 MAG_APER to #5 MAGERR_APER and then #8 FLUX_AUTO. MAG_APER and MAGERR_APER are both vector data with three items each. There are three colmns with MAG_APER-values and three columns with MAGERR_APER-values in the table data, however the header contains only one explicit entry for MAG_APER and MAGERR_APER.
In AstroAsciiData an individual column name is given to each of these multiple columns by adding a number to the basic name given in the header.
>>> import asciidata >>> SExample = asciidata.open('SExample.txt')
>>> print SExample.info() File: SExample.cat Ncols: 12 Nrows: 10 Delimiter: None Null value: ['Null', 'NULL', 'None', '*'] comment_char: # Column name: NUMBER Column type: <type 'int'> Column format: ['%5i', '%5s'] Column null value : ['Null'] Column comment : Running object number Column name: MAG_APER Column type: <type 'float'> Column format: ['% 6.4f', '%7s'] Column null value : ['Null'] Column unit : mag Column comment : Fixed aperture magnitude vector Column name: MAG_APER1 Column type: <type 'float'> Column format: ['% 6.4f', '%7s'] Column null value : ['Null'] Column name: MAG_APER2 Column type: <type 'float'> Column format: ['% 7.4f', '%8s'] Column null value : ['Null'] Column name: MAGERR_APER Column type: <type 'float'> Column format: ['% 6.4f', '%7s'] Column null value : ['Null'] Column unit : mag Column comment : RMS error vector for fixed aperture mag. Column name: MAGERR_APER1 Column type: <type 'float'> Column format: ['% 6.4f', '%7s'] Column null value : ['Null'] Column name: MAGERR_APER2 Column type: <type 'float'> Column format: ['% 6.4f', '%7s'] Column null value : ['Null'] Column name: FLUX_AUTO Column type: <type 'float'> Column format: ['% 7.1f', '%8s'] Column null value : ['Null'] Column unit : count Column comment : Flux within a Kron-like elliptical aperture Column name: FLUXERR_AUTO Column type: <type 'float'> Column format: ['% 7.3f', '%8s'] Column null value : ['Null'] Column unit : count Column comment : RMS error for AUTO flux Column name: X_IMAGE Column type: <type 'float'> Column format: ['% 7.3f', '%8s'] Column null value : ['Null'] Column unit : pixel Column comment : Object position along x Column name: Y_IMAGE Column type: <type 'float'> Column format: ['% 6.3f', '%7s'] Column null value : ['Null'] Column unit : pixel Column comment : Object position along y Column name: FLAGS Column type: <type 'int'> Column format: ['%5i', '%5s'] Column null value : ['Null'] Column comment : Extraction flagsIn this list there are the expanded column names MAG_APER, MAG_APER1 and MAG_APER2, and now every data column has a proper name.
>>> for ii in range(SExample.nrows): ... SExample['SNR'][ii]=SExample['FLUX_AUTO'][ii]/SExample['FLUXERR_AUTO'][ii] ... >>> SExample['SNR'].set_colcomment('Singal-to-Noise-Ratio') >>> print SExample['SNR'].info() Column name: SNR Column type: <type 'float'> Column format: ['% 12.6e', '%13s'] Column null value : ['Null'] Column comment : Singal-to-Noise-Ratio
>>> SExample.sort('SNR', descending=1) >>> print SExample['SNR'] Column: SNR 1.451319e+02 1.415957e+02 1.383599e+02 9.046388e+01 8.515625e+01 7.968198e+01 7.314095e+01 4.538671e+01 1.571474e+01 1.149313e-03 >>> SExample.flush()
>>> SExample.toplain() >>> SExample.writeto('SExample.plot') >>>
~>more SExample.cat # 1 NUMBER Running object number # 2 MAG_APER Fixed aperture magnitude vector [mag] # 3 MAG_APER1 # 4 MAG_APER2 # 5 MAGERR_APER RMS error vector for fixed aperture mag. [mag] # 6 MAGERR_APER1 # 7 MAGERR_APER2 # 8 FLUX_AUTO Flux within a Kron-like elliptical aperture [count] # 9 FLUXERR_AUTO RMS error for AUTO flux [count] # 10 X_IMAGE Object position along x [pixel] # 11 Y_IMAGE Object position along y [pixel] # 12 FLAGS Extraction flags # 13 SNR Singal-to-Noise-Ratio 3 -8.1548 -9.6216 -11.0307 0.0444 0.0229 0.0125 267764.0 1844.970 379.148 196.397 3 1.451319e+02 8 -8.9203 -10.3532 -11.7252 0.0219 0.0117 0.0066 129934.0 917.641 177.377 199.843 3 1.415957e+02 4 -9.4534 -11.0534 -11.9600 0.0134 0.0061 0.0053 178541.0 1290.410 367.213 123.803 3 1.383599e+02 1 -7.1135 -9.9589 -11.4873 0.1151 0.0168 0.0082 52533.1 580.708 379.715 72.461 3 9.046388e+01 2 -7.8412 -9.5452 -10.8191 0.0591 0.0246 0.0152 171543.0 2014.450 341.365 320.621 9 8.515625e+01 5 -7.8104 -9.1967 -10.5169 0.0609 0.0339 0.0201 131343.0 1648.340 305.545 307.027 3 7.968198e+01 7 -8.8319 -10.3340 -11.3343 0.0238 0.0119 0.0095 111597.0 1525.780 336.462 97.060 3 7.314095e+01 9 -7.5366 -9.0374 -10.3321 0.0784 0.0393 0.0238 72761.7 1603.150 94.196 131.380 3 4.538671e+01 10 -6.7963 -8.4304 -9.5685 0.1552 0.0687 0.0482 14072.0 895.465 265.404 46.241 3 1.571474e+01 6 -12.1666 -13.2193 -14.1293 0.0011 0.0008 0.0007 2.2 1938.010 258.692 260.341 3 1.149313e-03 ~> ~>more SExample.plot 3 -8.1548 -9.6216 -11.0307 0.0444 0.0229 0.0125 267764.0 1844.970 379.148 196.397 3 1.451319e+02 8 -8.9203 -10.3532 -11.7252 0.0219 0.0117 0.0066 129934.0 917.641 177.377 199.843 3 1.415957e+02 4 -9.4534 -11.0534 -11.9600 0.0134 0.0061 0.0053 178541.0 1290.410 367.213 123.803 3 1.383599e+02 1 -7.1135 -9.9589 -11.4873 0.1151 0.0168 0.0082 52533.1 580.708 379.715 72.461 3 9.046388e+01 2 -7.8412 -9.5452 -10.8191 0.0591 0.0246 0.0152 171543.0 2014.450 341.365 320.621 9 8.515625e+01 5 -7.8104 -9.1967 -10.5169 0.0609 0.0339 0.0201 131343.0 1648.340 305.545 307.027 3 7.968198e+01 7 -8.8319 -10.3340 -11.3343 0.0238 0.0119 0.0095 111597.0 1525.780 336.462 97.060 3 7.314095e+01 9 -7.5366 -9.0374 -10.3321 0.0784 0.0393 0.0238 72761.7 1603.150 94.196 131.380 3 4.538671e+01 10 -6.7963 -8.4304 -9.5685 0.1552 0.0687 0.0482 14072.0 895.465 265.404 46.241 3 1.571474e+01 6 -12.1666 -13.2193 -14.1293 0.0011 0.0008 0.0007 2.2 1938.010 258.692 260.341 3 1.149313e-03 ~>The two files contain the same data. In the SExtractor version the column names are still present in the header.