#!/usr/local/bin/perl # exsex version 1.2 # extracts columns of data from SExtractor catalogs # documentation is probably available at # http://www.astrsp-mrs.fr/gwyn/exsex.html # Author: Stephen Gwyn $name=shift @ARGV; if ($ARGV[0] =~/\.param$/) { $paramfile = shift @ARGV; # for CATLOG_TYPE ASCII @param=@ARGV; for (@param) {$_ = uc} @cat = &readascii($name,$paramfile,@param); } elsif ($ARGV[0]=~/header/) { $headerfile = shift @ARGV; # for a seperate header file @param=@ARGV; for (@param) {$_=uc} @cat = &readseparateheader($name,$headerfile,@param) } else { @param=@ARGV; # for CATLOG_TYPE ASCII_HEADER for (@param) {$_ = uc} @cat = &readasciiheader($name,@param); } for $cat (@cat) { # dump the results %cat=%{$cat}; for (@param) { print $cat{$_}," "; } print "\n"; } ################# ################# sub readseparateheader { use strict; my $catalog = shift @_; my $header = shift @_; my $col; my %colindex=(); my %coloff=(); my %colname=(); my $toss; my $colnum; my $param; my @returncat = (); my $id; my @a; my $ok; my %line=(); my @params= @_; my $coloff=0; foreach $col (@params) { # set all the column number to -1 $colindex{$col}=-1; # (until we get real values) $colname{$col}=$col; if ($colname{$col}=~s/(\(*\d\))//) {$coloff{$col}=$1} else {$coloff{$col}=0} # kludge in case there are any MAG_APER(1) type params $coloff{$col}=~s/\(|\)//g; } my $id=-1; open HEADER,$header or die "Header file $header not found\n"; while (
) { chomp; # toss any newlines ($toss,$colnum,$param)=split; # extract the column numbers for $col (keys %colindex) { # check to see if $param is one we want if ($param eq $colname{$col}) { if (defined $colindex{$col}) { # save it if it is $coloff = ($coloff{$col}) ? $coloff{$col}-1 : 0; # and the offset $colindex{$col}=$colnum-1+$coloff ; } } } } close HEADER; $ok=1; for $col (keys %colindex) { # check to see if we found if ($colindex{$col}<0) { # all the desired parameters print STDERR "parameter $col not found \n"; # complain if we haven't $ok=0; } } if ($ok==0) { print STDERR "At least one parameter was not found in the catalog $catalog \n" } open CATALOG,$catalog or die "Catalog $catalog not found\n"; while () { @a=split; $id++; for $col (keys %colindex) { # for the other lines next if ($colindex{$col}<0); # extract the data $returncat[$id]{$col}=$a[$colindex{$col}]; # and put it in an array of hashes } } close CATALOG; @returncat; ## this works because returncat is an array } ## (even though it is an array of references) #################### sub readasciiheader { use strict; my $catalog = shift @_; open CATALOG,$catalog or die "Catalog $catalog not found\n"; my $col; my %colindex=(); my %coloff=(); my %colname=(); my $toss; my $colnum; my $param; my @returncat = (); my $id; my @a; my $ok; my %line=(); my @params= @_; my $coloff=0; foreach $col (@params) { # set all the column number to -1 $colindex{$col}=-1; # (until we get real values) $colname{$col}=$col; if ($colname{$col}=~s/(\(*\d\))//) {$coloff{$col}=$1} else {$coloff{$col}=0} # kludge in case there are any MAG_APER(1) type params $coloff{$col}=~s/\(|\)//g; } my $id=-1; while () { chomp; # toss any newlines if (/^#/) { # if we are in the header ($toss,$colnum,$param)=split; # extract the column numbers for $col (keys %colindex) { # check to see if $param is one we want if ($param eq $colname{$col}) { if (defined $colindex{$col}) { # save it if it is $coloff = ($coloff{$col}) ? $coloff{$col}-1 : 0; # and the offset $colindex{$col}=$colnum-1+$coloff ; } } } } else { @a=split; $id++; for $col (keys %colindex) { # for the other lines next if ($colindex{$col}<0); # extract the data $returncat[$id]{$col}=$a[$colindex{$col}]; # and put it in an array of hashes } } } close CATALOG; $ok=1; for $col (keys %colindex) { # check to see if we found if ($colindex{$col}<0) { # all the desired parameters print STDERR "parameter $col not found \n"; # complain if we haven't $ok=0; } } if ($ok==0) { print STDERR "At least one parameter was not found in the catalog $catalog \n" } @returncat; ## this works because returncat is an array } ## (even though it is an array of references) #################### sub readascii { use strict; my $catalog = shift @_; open CATALOG,$catalog or die "Catalog $catalog not found\n"; my $paramfile = shift @_; open PARAMFILE,$paramfile or die "Parameter file $paramfile not found\n"; my @params= @_; my $col; my @a; my %colindex=(); my %colname=(); my %coloff=(); my $coloff; my $toss; my $colnum; my $param; my @returncat = (); my $id; my $ok; my %line=(); my $off; my $param; foreach $col (@params) { # set all the column number to -1 $colindex{$col}=-1; # (until we get real values) $colname{$col}=$col; if ($colname{$col}=~s/(\(*\d\))//) {$coloff{$col}=$1} else {$coloff{$col}=0} # kludge in case there are any MAG_APER(1) type params $coloff{$col}=~s/\(|\)//g; } ### get the parameters from the parameterfile ######### my $colnum=-1; while() { chomp; $param=$_; $off = ($param=~s/\((\d*)\)//) ? $1 : 1; $colnum+=$off; for $col (keys %colindex) { # check to see if $param is one we want if ($param eq $colname{$col}) { if (defined $colindex{$col}) { # save it if it is $coloff = ($coloff{$col}) ? $coloff{$col}-1 : 0; # and the offset $colindex{$col}=$colnum-1+$coloff ; } } } } ### check to see if we found all the parameters ######### $ok=1; for $param (keys %colindex) { # check to see if we found if ($colindex{$param}<0) { # all the desired parameters print STDERR "parameter $param not found in $paramfile\n"; $ok=0; } } if ($ok==0) { print STDERR "At least one parameter was not found in the catalog $catalog \n" } ### get the data from the catalog ######### my $id=-1; while () { chomp; # toss any newlines @a=split; $id++; for $param (keys %colindex) { next if ($colindex{$param}<0); $returncat[$id]{$param}=$a[$colindex{$param}]; } } close CATALOG; @returncat; ## this works because returncat is an array } ## (even though it is an array of references) ####################