def createNewTriples(Win):
    """Add entries to the triples tables based on new images in the db"""

    win.help("Building list of exposures to look for triples")

    cols=('e.expnum', 'object',
      'mjdate',
      'uttime',
      'elongation',
      'filter',
      'obs_iq_refccd','qso_status' )
    header='%6s %-10s%-12s%10s%10s%10s%8s%10s' %  cols  


    pointings=getNewTriples()
    num_p=len(pointings)

    for pointing in pointings:
        pid=pointing[0]
	mjd=pointing[1]
        expnums=getExpnums(pointing=pid,night=mjd)
        num_p=num_p-1
        while (1):
            ### Loop over this pointing until keystroke gets us out

            win.help("Select (space)  members of triplets - %d remaining" % num_p )

            ## start with an empty list
            explist=[]
            choices=[]

            current_date=''
            for expnum in expnums:
                info=getExpInfo(expnum[0])
                row=()
                if not str(info['triple'])=='None' :
                       continue
                if str(info['obs_iq_refccd'])=='None':
                    info['obs_iq_refccd']=-1.0

                choices.append('%6d %10s %15s %10s %8.2f %10s %8.2f %10s' % (
                    int(info['e.expnum']),
                    str(info['object']),
                    str(info['mjdate']),
                    str(info['uttime']),
                    float(str(info['elongation'])),
                    str(info['filter']),
                    float(str(info['obs_iq_refccd'])),
                    str(info['qso_status'])
                    ))
                explist.append(expnum[0])

            if len(choices)<3:
                ### we need to provide at least 3 choices,
                ### otherwise this isn't a triple (is it)
                break

            ### win.list returns the user's choices as a list.
            choice_list=win.list(header,choices)

            ### zero length list implies were done.
            if choice_list==None:
                break

            ### is this actually a triple?
            if len(choice_list)!=3:
                win.help("Must have 3 members to make a tripple")
                continue

            ### Create a new line in the triple table
            sql = "INSERT INTO triples (id, pointing ) VALUES ( NULL, %s ) "
            cfeps.execute(sql, ( pid, ) )
	    sql = "SELECT id FROM triples WHERE pointing=%s order by id desc" 
	    cfeps.execute(sql, ( pid, ) )
	    ttt=cfeps.fetchall()
            triple= ttt[0][0]
	    win.help(str(triple))

            ### record the members of this new triple.
            sql = "INSERT INTO triple_members (triple, expnum) VALUES ( %s, %s)";
	    win.help(sql)

            for exp in choice_list:
                cfeps.execute(sql,(triple,explist[exp]))

    return(0)
Add entries to the triples tables based on new images in the db