typedef struct {
	int id;
	int run;
	int camcol;
	int field;
	int parent;
	int nchild;
	double ra;
	double dec;
	float psfcounts[5];
	int objc_flags;
	int objc_type;
	float objc_colc;
	float objc_rowc;
	int saturbit;
	int saturflag;
	int edgebit;
	int edgeflag;
	int blendbit;
	int blendflag;
	int brightbit;
	int brightflag;
	int nodeblendbit;
	int nodeblendflag;
	int trustRHLflag;
} SHORTOBJ;

proc readchn {run col fieldlo index indir} {

set tblcol [tblColNew]
fitsRead $tblcol $indir/$run/calibChunks/tsObj-[format %06d $run]-$col-0-[format %04d $fieldlo].fit \
	-binary -hdu [expr $index+1]
set size [exprGet $tblcol.rowCnt]
set field [expr $fieldlo+$index]
echo field $field size is $size
set outchn [chainNew SHORTOBJ $size]
set brightbit 2
set blendbit 8
set nodeblendbit 64
set saturbit [expr 4*pow(16,4)]
set edgebit 4

exprEval "$outchn.id = $tblcol.id;
	  $outchn.run = $run;
	  $outchn.field = $field;
	  $outchn.camcol = $col;
          $outchn.parent=$tblcol.parent;
          $outchn.nchild=$tblcol.nchild;
          $outchn.ra=$tblcol.ra;
          $outchn.dec=$tblcol.dec;
          $outchn.psfcounts\[0\] = $tblcol.psfcounts\[0\];
          $outchn.psfcounts\[1\] = $tblcol.psfcounts\[1\];
          $outchn.psfcounts\[2\] = $tblcol.psfcounts\[2\];
          $outchn.psfcounts\[3\] = $tblcol.psfcounts\[3\];
          $outchn.psfcounts\[4\] = $tblcol.psfcounts\[4\];
          $outchn.objc_flags=$tblcol.objc_flags;
          $outchn.objc_rowc=$tblcol.objc_rowc;
          $outchn.objc_colc=$tblcol.objc_colc;
          $outchn.objc_type=$tblcol.objc_type;
          $outchn.brightbit = $brightbit;
          $outchn.edgebit =  $edgebit;
          $outchn.saturbit =  $saturbit;
          $outchn.nodeblendbit =  $nodeblendbit;
          $outchn.blendbit =  $blendbit;
	  $outchn.edgeflag = $outchn.objc_flags & $outchn.edgebit;
	  $outchn.saturflag = $outchn.objc_flags & $outchn.saturbit;
	  $outchn.brightflag = $outchn.objc_flags & $outchn.brightbit;
	  $outchn.blendflag = $outchn.objc_flags & $outchn.blendbit;
	  $outchn.nodeblendbit = $outchn.objc_flags & $outchn.nodeblendbit;
          $outchn.trustRHLflag= !$outchn.brightflag && 
		(!$outchn.blendflag || ( $outchn.blendflag && !$outchn.nodeblendbit));
	";

tblColDel $tblcol

return $outchn

}


proc plotemc {which runcol device gmr rmi imz} {
global color colorb colorq colorqb colorqid

set f(0) u
set f(1) g
set f(2) r
set f(3) i
set f(4) z

        if { $device == "XW" } { 
        pgBegin
        } else { 
        pgBegin $which$runcol.ps/CPS
        }
        
        pgAsk 0
        pgSubp 2 2

        loop j 0 3 {

                set ja $j
                set jb [expr $j+1]
                set jc [expr $j+2]

        pgSch 1.8
        pgSlw 3
	if { $j == 0 } {
        pgEnv -1 4 -1 3 1 0
	pgMove -1 $gmr
	pgDraw 4 $gmr
	} elseif { $j == 1 } {
        pgEnv -1 3 -1 2.5 1 0
	pgMove $gmr -1 
	pgDraw $gmr 2.5
	pgMove -1 $rmi
	pgDraw 3 $rmi
	} else {
        pgEnv -1 2.5 -1 2.5 1 0
	pgMove $rmi -1
	pgDraw $rmi 2.5
	pgMove -1 $imz
	pgDraw 2.5 $imz
	
	}

         pgPoint $color($j) $colorb($j) 1
		if { [llength $colorq($j)] > 0 } {
	 		pgPoint $colorq($j) $colorqb($j) 4
			loop k 0 [llength $colorq($j)] {
		  pgText [lindex $colorq($j) $k] [lindex $colorqb($j) $k] [lindex  $colorqid($j) $k]
			}
		}

        pgLabel "$f($ja)-$f($jb)" "$f($jb)-$f($jc)" "Color Color ($which$runcol)"
        }

        if { $device != "XW"} {
                pgEnd
        }

}



proc findQSOs {run col field0 startfield nfields rootdir ilimit zlimit trustRHL gmr rmi imz} {
	global color colorb colorq colorqb colorqid


	set offset [expr $startfield-$field0]
	set first 1
	loop i 0 $nfields {
                set chna [readchn $run $col $field0 [expr $offset+$i] $rootdir]
		if {$first == 1} {
			set chn $chna
			set first 0
		} else {
                	chainJoin $chn $chna
		}
        }
	
	if { $trustRHL == 1 } {
	set chns [chainSearch $chn "{objc_type == 6} {64 <= objc_rowc < 1426 } 
		{edgeflag == 0} {saturflag == 0 } {trustRHLflag == 1}"]
	} else {
	set chns [chainSearch $chn "{objc_type == 6} {64 <= objc_rowc < 1426 } 
		{edgeflag == 0} {saturflag == 0} {parent == -1} {nchild == 0} {brightflag == 0}"]
	}

	set size [chainSize $chns]
	echo run: $run camcol:$col size: $size

	loop j 0 3 { 
          set color($j) ""
          set colorb($j) ""
	  set colorq($j) ""
	  set colorqb($j) ""
	  set colorqid($j) ""
        }


 set cursor [chainCursorNew $chns]
        set i 0
        while { [set cur [chainWalk $chns $cursor NEXT]] != "" } {
                incr i
                if { [expr $i/1000.0 == int($i/1000.0)] } { echo $i }

		if { [exprGet $cur.psfcounts<3>] < $ilimit && [exprGet $cur.psfcounts<4>] < $zlimit } {
                loop j 0 5 {
                set mag($j) [exprGet $cur.psfcounts<$j>]
		}

		loop j 0 4 {
                set colorm($j) [expr $mag($j)-$mag([expr $j+1])]
		}

		loop j 0 3 {
                append color($j) "$colorm($j) "
                append colorb($j) "$colorm([expr $j+1]) "
		}

			# find those hi-z points...
			if {$colorm(1) > $gmr && $colorm(2) > $rmi && $colorm(3) < $imz } {
				loop j 0 3 {
                			append colorq($j) "$colorm($j) "
                			append colorqb($j) "$colorm([expr $j+1]) "
					append $colorqid($j) "[exprGet $cur.id] "
				}
		echo Candidate: [exprGet $cur.run] [exprGet $cur.camcol] [exprGet $cur.field]  \
	[exprGet $cur.id] [format %.2f [exprGet $cur.objc_rowc]] [format %.2f [exprGet $cur.objc_colc]] \
	[csubstr [degToHMS [exprGet $cur.ra]] 0 11] [csubstr [degToDMS [exprGet $cur.dec]] 0 11] \
	[format %.2f [exprGet $cur.psfcounts<0>]] [format %.2f [exprGet $cur.psfcounts<1>]] \
	[format %.2f [exprGet $cur.psfcounts<2>]] [format %.2f [exprGet $cur.psfcounts<3>]] \
	[format %.2f [exprGet $cur.psfcounts<4>]] [exprGet $cur.parent]
			}
                }

        }

        chainCursorDel $chns $cursor

        plotemc star QQ-r$run-c$col CPS $gmr $rmi $imz

}