text:
#!/dark/usr/anaconda2/bin/python
#/usr/bin/env python
import sys,os,cgi,string,glob

#sys.path.append( '/Users/svalenti/Sites/lib/python2.7/site-packages')
##!/opt/epd/bin/python
#import sys,os,cgi,string,glob
#sys.path.append('/home/supernova/lib/python2.6/site-packages')

import ephem, datetime
import numpy as np
from matplotlib.dates import DateFormatter, MinuteLocator
from matplotlib.font_manager import FontProperties
import matplotlib.dates as mdates
import StringIO
import base64
import agnkey

#os.environ['HOME']='../tmp/'

####################################################################################

def trigger(SN0,SN_RA,SN_DEC,_targid,_form,observations={},proposal=agnkey.util.readpass['proposal']):
    sss='<form style="background-color:orange" action="agnupdatetable.py" enctype="multipart/form-data" method="post">'+\
        '

 TRIGER NEW OBSERVATION (use carefully)  <input type="submit" value="Send">

'+\
        '<input type="hidden" name="sn_name" value='+str(SN0)+'>'+\
        '<input type="hidden" name="SN_RA" value="'+str(SN_RA)+'">'+\
        '<input type="hidden" name="SN_DEC" value="'+str(SN_DEC)+'">'+\
        '<input type="hidden" name="targid" value="'+str(_targid)+'">'
    for key in observations:
        if 'xxx' in key:
            sss=sss+'<input type="hidden" name="'+str(key)+'" value="'+str(observations[key])+'">'
    sss=sss+'<input type="hidden" name="type" value="trigger">'+\
         '<input type="hidden" name="outputformat" value="'+str(_form)+'">'+\
         '
'+\
         '
 U 	 B 	 V 	 R 	 I 	 u 	 g 	 r 	 i 	 z

'
    for i in 'UBVRIugriz':
        if i in ['B','V','g','r','i']:
            sss=sss+' <input type="checkbox" name="'+str(i)+'" value="'+str(i)+'" checked>	'
        else:
            sss=sss+' <input type="checkbox" name="'+str(i)+'" value="'+str(i)+'" >	'
    sss=sss+' select filters

'
    for i in 'UBVRIugriz':
        sss=sss+ '<select name="n'+str(i)+'">'
        sss=sss+ '<option value="2"> 2</option>  <option value="1">1</option> <option value="3">3</option><option value="5">5</option> <option value="10">10</option>'
        sss=sss+ '</select>	'
    sss=sss+ ' number of exposure

'
    for i in 'UBVRIugriz':
        sss=sss+ '
<select name="exp'+str(i)+'"  width:100px;>'+\
             '<option  width=10 value="120"> 120</option>'+\
             '<option value="15">15</option>'+\
             '<option value="30">30</option>'+\
             '<option value="45">45</option>'+\
             '<option value="60">60</option>'+\
             '<option value="90">90</option>'+\
             '<option value="120">120</option>'+\
             '<option value="150">150</option>'+\
             '<option value="180">180</option>'+\
             '<option value="200">200</option>'+\
             '<option value="240">240</option>'+\
             '<option value="300">300</option>'+\
             '<option value="360">360</option>'+\
             '<option value="400">400</option>'+\
             '<option value="600">600</option>'
        sss=sss+ '</select>
	'
    sss=sss+ 'exposure time 	'+\
         '
'+\
         '
'+\
         '
'+\
         '
 site: <select name="site">'+\
         '<option value="any"> any</option>  <option value="lsc">lsc</option>'+\
         '<option value="elp">elp</option>   <option value="coj">coj</option> <option value="cpt">cpt</option>'+\
         '</select>
'+\
         '
 number of triggers:  <select name="ntriggers">'+\
         '<option value="1"> 1 in the next 24h </option>  <option value="2"> 2 in the next 48h </option>'+\
         '<option value="3"> 2 in the next 24h</option>   <option value="4"> 4 in the next 48h </option>'+\
         '<option value="5"> 1 in the next 6 days</option>   <option value="6"> 2 in the next 6 days </option>'+\
         '<option value="7"> 3 in the next 6 days</option>   <option value="8"> 7 in the next 7 days </option>'+\
         '<option value="9"> 1 observation in the next 8h </option>   <option value="10">  3 observations in the next 12h </option>'+\
         '<option value="13"> 24 observations in the next 24 days </option>   <option value="14">  12 observations in the next 24 days </option>'+\
         '<option value="15"> 21 observations in the next 7 days </option>'+\
         '</select>
'+\
         '
 airmass limit:  <select name="airmass">'+\
         '<option value=2> 2 </option> '+\
         '<option value=2.5> 2.5 </option> '+\
         ' <option value=1.2> 1.2 </option>'+\
         '<option value=1.5> 1.5 </option>'+\
         ' <option value=3> 3 </option>'+\
         ' <option value=4> 4 </option>'+\
         '</select>
'+\
         '
 proposal: <select name="proposal">'
    for kk in proposal:
             sss=sss+'<option value="'+kk+'"> '+kk+' </option> '
    sss=sss+'</select>
'+\
         '
 instrument  <select name="instrument">'+\
         '<option value="sbig"> sbig </option>  <option value="sinistro">sinistro</option>'+\
         '<option value="spectral">spectral</option>'+\
         '<option value="ONEOF">ONEOF</option>'+\
         '
'+\
         '</form>'
    return sss

######################################################################################################
def triggerfloyds(SN0,SN_RA,SN_DEC,_targetid,_form,observations={},proposal=agnkey.util.readpass['proposal']):
    sss='<form style="background-color:#D8D8D8 " action="agnupdatetable.py" enctype="multipart/form-data" method="post">'+\
        '

 TRIGER NEW OBSERVATION WITH FLOYDS(use carefully)  <input type="submit" value="Send">

'+\
        '<input type="hidden" name="sn_name" value='+str(SN0)+'>'+\
        '<input type="hidden" name="SN_RA" value="'+str(SN_RA)+'">'+\
        '<input type="hidden" name="SN_DEC" value="'+str(SN_DEC)+'">'+\
        '<input type="hidden" name="targid" value="'+str(_targetid)+'">'
    for key in observations:
        if 'xxx' in key:
            sss=sss+'<input type="hidden" name="'+str(key)+'" value="'+str(observations[key])+'">'
    sss=sss+'<input type="hidden" name="type" value="triggerfloyds">'+\
         '<input type="hidden" name="outputformat" value="'+str(_form)+'">'
    sss=sss+ '
exposure time: <select name="expfloyds'+'"  width:100px;>'+\
         '<option  width=10 value="300"> 300</option>'+\
         '<option value="600">600</option>'+\
         '<option value="900">900</option>'+\
         '<option value="1200">1200</option>'+\
         '<option value="1500">1500</option>'+\
         '<option value="1800">1800</option>'+\
         '<option value="2700">2700</option>'+\
         '<option value="3600">3600</option>'+\
         '<option value="60">60</option>'+\
         '<option value="30">30</option>'
    sss=sss+ '</select>
	'
    sss=sss+'
 number of spectra : <select name="nexpfloyds'+'"  width:100px;>'+\
         '<option  width=10 value="1"> 1</option>'+\
         '<option  width=10 value="2"> 2</option>'+\
         '<option  width=10 value="3"> 3</option>'+\
         '<option  width=10 value="4"> 4</option>'+ '</select>
	'
    sss=sss+'
'+\
         '
'+\
         '
'+\
         '
 site: <select name="site">'+\
         '<option value="any"> any</option>'+\
         '<option value="ogg">FTN</option>'+\
         '<option value="coj">FTS</option>'+\
         '</select>
'+\
         '
 slit: <select name="slit">'+\
         '<option value="default">default </option>'+\
         '<option value="1.2">1.2</option>'+\
         '<option value="1.6">1.6</option>'+\
         '<option value="2.0">2.0</option>'+\
         '<option value="6.0">6.0</option>'+\
         '</select>
'+\
         '
 number of triggers:  <select name="ntriggers">'+\
         '<option value="1"> 1 in the next 24h </option>'+\
         '<option value="2"> 2 in the next 48h </option>'+\
         '<option value="3"> 2 in the next 24h</option> '+\
         '<option value="4"> 4 in the next 48h </option>'+\
         '<option value="5"> 1 in the next 6 days</option>'+\
         '<option value="6"> 2 in the next 6 days </option>'+\
         '<option value="7"> 3 in the next 6 days</option>'+\
         '<option value="8"> 7 in the next 7 days </option>'+\
         '<option value="9"> 1 observation in the next 8h </option>'+\
         '<option value="10"> 3 observations in the next 12h </option>'+\
         '<option value="11"> 1 observations in the next 1h </option>'+\
         '<option value="12"> 1 observations in the next 2h </option>'+\
         '</select>
'+\
         '
 airmass limit:  <select name="airmass">'+\
         '<option value=2> 2 </option>'+\
         '<option value=1.2> 1.2 </option>'+\
         '<option value=1.5> 1.5 </option>'+\
         '<option value=2.5> 2.5 </option>'+\
         ' <option value=3> 3 </option>'+\
         ' <option value=4> 4 </option>'+\
         '</select>
'+\
         '
 proposal: <select name="proposal">'
    for kk in proposal:
             sss=sss+'<option value="'+kk+'"> '+kk+' </option> '
    sss=sss+'</select>
'+\
         '
'+\
         '</form>'
    return sss

################################################################################
def visibility(_ra0,_dec0,_plot=True,xx='300',yy='200'):
  from datetime import datetime, timedelta
  import matplotlib
  from matplotlib.dates import DateFormatter, MinuteLocator
  import string,ephem,datetime,sys
  import numpy as np
  import matplotlib.dates as mdates
  from matplotlib.ticker import MultipleLocator
  #########################################################################
  site_ll = {
      'tst' : {'latitude' : '+34.433161', 'longitude' : '-119.8631' , 'color': 'k'},
      'sqa' : {'latitude' : '+34.6924533','longitude' : '-120.0422217' , 'color': 'c'},
      'ogg' : {'latitude' : '+20.706900', 'longitude' : '-156.25800', 'color': '#3366dd'},
      'bpl' : {'latitude' : '+34.433161', 'longitude' : '-119.8631',  'DOMA' : '01.08', 'color': 'g'},
      'elp' : {'latitude' : '+30.679833', 'longitude' : '-104.015173','DOMA' : '03.10','color': '#700000'},
      'lsc' : {'latitude' : '-30.167367', 'longitude' : '-70.8049',   'DOMA' : '05.02', 'DOMB' : '10.03', 'DOMC' : '11.04', 'color': 'm'},
      'cpt' : {'latitude' : '-32.3826',   'longitude' : '+20.8124',   'DOMA' : '06.05', 'DOMB' : '07.06', 'DOMC' : '08.10', 'color': '#004f00'},
      'coj' : {'latitude' : '-31.2733',   'longitude' : '+149.438',   'DOMA' : '14.12', 'DOMB' : '02.13', 'SPARE' : '09.00','color': '#fac900' }}

 ###########################################################################
  col='brgmc'
  sun = ephem.Sun()
  star = ephem.FixedBody()
  star._ra = str(float(_ra0)/15.)
  star._dec = str(float(_dec0))
  td = datetime.timedelta(minutes=10)
  obs = ephem.Observer()       #will contain ephemeris for start time
  utnow = obs.date.datetime()  #current utdate/time
  if _plot:
    import pylab as plt
    plt.figure(num=1, figsize=(10, 7))
    plt.clf()
    ax=plt.axes([.1,.25,.6,.7])
    ax.plot([utnow,utnow],[0.1,90],'r--',label='Now')

  ii=0
  dates1=[]
  for site in ['lsc','elp','cpt','coj','ogg']:
        obs = ephem.Observer()       #will contain ephemeris for start time
        moon = ephem.Moon()
        utdate = obs.date  #current utdate/time
        obs.date = utdate
        obs.lat = site_ll[site]['latitude']
        obs.long = site_ll[site]['longitude']
        sun.compute(obs)

        obs.horizon='-12'
        nextrise = obs.next_rising(sun,use_center=True)
        nextset = obs.next_setting(sun,use_center=True)

        nextriseutc= nextrise.datetime()
        nextsetutc= nextset.datetime()
        if nextsetutc > nextriseutc:
            nextsetutc=nextsetutc+datetime.timedelta(days=-1)
        date = nextsetutc
        dates,dates2=[],[]
        altitude,distance=[],[]
        lha=[]
        HA=[]
        while date < nextriseutc:
            date += td
            dates.append(date)
            dates2.append(date.hour)
            obs.date = date
            moon.compute(obs)
            star.compute(obs)
            gg,tt,ll=string.split(str(ephem.separation((star.az, star.alt), (moon.az, moon.alt))),':')
            hh,mm,ss=string.split(str(star.alt),':')
            lha.append(abs((obs.sidereal_time()-star._ra)*24.0/(2.0*ephem.pi)))
##############################
            if '-' in hh:
                sign=-1
                de=0
            else:
                sign=1.
                de = (np.abs(float(hh))+(float(mm)/60.0 + float(ss)/3600.0))*sign
            altitude.append(de)
##############################
            if '-' in gg:
                sign=-1
                de1=0
            else:
                sign=1.
                de1 = (abs(float(gg))+(float(tt)/60.0 + float(ll)/3600.0))*sign
            distance.append(de1)
        dist= '%3.1f' % (np.mean(np.array(distance)))
        stringa=site+' '+str(dist)+" $^{o}$ "
        dates1=np.array(list(dates1)+list(dates))

        lha=[abs(i) if abs(i)<=12 else abs(i-24) for i in lha]

        altitudegood=[altitude[x] if (lha[x] <= 4.8) else None for x in range(0,len(lha))]
        altitudebad=[altitude[x] if (lha[x] > 4.8) else None for x in range(0,len(lha))]
        if _plot:
            ax.plot(dates, altitudegood, '-', label=stringa, color=site_ll[site]['color'],linewidth=3)
            ax.plot(dates, altitudebad, ':', label='', color=site_ll[site]['color'])

        ii=ii+1
  if _plot:
    # Plot airmass limit line (airmass=2)
    ax.plot([min(dates1),max(dates1)],[30,30],'g-.',label='airmass limit')
    ax.set_xlabel('UT time (hours)',fontsize=18)
    ax.set_ylabel('altitude',fontsize=18)
    ax.set_ylim(0.,90)
    ax.legend(numpoints=1,markerscale=1.5,loc=(1.07,0.5),ncol=1)
    plt.xticks(rotation='vertical')
    ax2 = plt.twinx()

    ax.xaxis.set_major_formatter( matplotlib.dates.DateFormatter('%m-%dT%H'))
    ax2.xaxis.set_major_formatter( matplotlib.dates.DateFormatter('%m-%dT%H'))
    ax2.set_ylabel('airmass',fontsize=18)
    ax2.set_yticks([19.471,23.578,30,41.8,53.13,65.38,90])
    ax2.set_yticklabels(['3.0','2.5','2.0','1.5','1.25','1.1','1.0'])
    ax.format_xdata = matplotlib.dates.DateFormatter('%Y-%m-%d:%h')
    ax2.format_xdata = matplotlib.dates.DateFormatter('%Y-%m-%d:%h')
    ax.xaxis.set_major_locator(MultipleLocator(1./12.))
    ax.xaxis.set_minor_locator(MultipleLocator(1./120.))
    pngplot = StringIO.StringIO()
    plt.savefig(pngplot,format='png')
    pngplot.seek(0)
    data_uri = pngplot.read().encode("base64").replace("\n", "")
    html = ''.format(data_uri)
    return html

################################################################################
def visibilityiair(_ra0,_dec0,_plot=True,xx='300',yy='200'):
  from datetime import datetime, timedelta
  import matplotlib
  from matplotlib.dates import DateFormatter, MinuteLocator
  import string,ephem,datetime,sys
  import numpy as np
  import matplotlib.dates as mdates
  from matplotlib.ticker import MultipleLocator
  #########################################################################
  site_ll = {
      'tst' : {'latitude' : '+34.433161', 'longitude' : '-119.8631' , 'color': 'k'},
      'sqa' : {'latitude' : '+34.6924533','longitude' : '-120.0422217' , 'color': 'c'},
      'ogg' : {'latitude' : '+20.706900', 'longitude' : '-156.25800', 'color': '#3366dd'},
      'bpl' : {'latitude' : '+34.433161', 'longitude' : '-119.8631',  'DOMA' : '01.08', 'color': 'g'},
      'elp' : {'latitude' : '+30.679833', 'longitude' : '-104.015173','DOMA' : '03.10','color': '#700000'},
      'lsc' : {'latitude' : '-30.167367', 'longitude' : '-70.8049',   'DOMA' : '05.02', 'DOMB' : '10.03', 'DOMC' : '11.04', 'color': 'm'},
      'cpt' : {'latitude' : '-32.3826',   'longitude' : '+20.8124',   'DOMA' : '06.05', 'DOMB' : '07.06', 'DOMC' : '08.10', 'color': '#004f00'},
      'coj' : {'latitude' : '-31.2733',   'longitude' : '+149.438',   'DOMA' : '14.12', 'DOMB' : '02.13', 'SPARE' : '09.00','color': '#fac900' }}

 ###########################################################################
  col='brgmc'
  sun = ephem.Sun()
  star = ephem.FixedBody()
  star._ra = str(float(_ra0)/15.)
  star._dec = str(float(_dec0))
  td = datetime.timedelta(minutes=10)
  obs = ephem.Observer()       #will contain ephemeris for start time
  utnow = obs.date.datetime()  #current utdate/time
  if _plot:
    import pylab as plt
    plt.figure(num=1, figsize=(float(xx)/50, float(yy)/50))
    plt.clf()
    ax=plt.axes([.1,.25,.6,.7])
    ax.plot([utnow,utnow],[0.1,90],'r--',label='Now')

  ii=0
  dates1=[]
  for site in ['lsc','elp','cpt','coj','ogg']:
        obs = ephem.Observer()       #will contain ephemeris for start time
        moon = ephem.Moon()
        utdate = obs.date  #current utdate/time
        obs.date = utdate
        obs.lat = site_ll[site]['latitude']
        obs.long = site_ll[site]['longitude']
        sun.compute(obs)

        obs.horizon='-12'
        nextrise = obs.next_rising(sun,use_center=True)
        nextset = obs.next_setting(sun,use_center=True)

        nextriseutc= nextrise.datetime()
        nextsetutc= nextset.datetime()
        if nextsetutc > nextriseutc:
            nextsetutc=nextsetutc+datetime.timedelta(days=-1)
        date = nextsetutc
        dates,dates2=[],[]
        altitude,distance=[],[]
        lha=[]
        HA=[]
        while date < nextriseutc:
            date += td
            dates.append(date)
            dates2.append(date.hour)
            obs.date = date
            moon.compute(obs)
            star.compute(obs)
            gg,tt,ll=string.split(str(ephem.separation((star.az, star.alt), (moon.az, moon.alt))),':')
            hh,mm,ss=string.split(str(star.alt),':')
            lha.append(abs((obs.sidereal_time()-star._ra)*24.0/(2.0*ephem.pi)))
##############################
            if '-' in hh:
                sign=-1
                de=0
            else:
                sign=1.
                de = (np.abs(float(hh))+(float(mm)/60.0 + float(ss)/3600.0))*sign
            altitude.append(de)
##############################
            if '-' in gg:
                sign=-1
                de1=0
            else:
                sign=1.
                de1 = (abs(float(gg))+(float(tt)/60.0 + float(ll)/3600.0))*sign
            distance.append(de1)
        dist= '%3.1f' % (np.mean(np.array(distance)))
        stringa=site.upper()+' (Moon dist: '+str(dist)+"$^{o}$)"
        dates1=np.array(list(dates1)+list(dates))
        altitudegood=[altitude[x] if (lha[x] <= 4.8) else None for x in range(0,len(lha))]
        altitudebad=[altitude[x] if (lha[x] > 4.8) else None for x in range(0,len(lha))]
        if _plot:
            ax.plot(dates, altitudegood, '-', label=stringa, color=site_ll[site]['color'],linewidth=3)
            ax.plot(dates, altitudebad, ':', label='', color=site_ll[site]['color'])

        ii=ii+1
  if _plot:
    # Plot airmass limit line (airmass=2)
    ax.plot([min(dates1),max(dates1)],[30,30],'g-.',label='Airmass limit')

    titlefont = FontProperties()
    titlefont.set_size(18)
    titlefont.set_family('sans-serif')
    titlefont.set_style('normal')

    ax.set_xlabel('UT',fontproperties=titlefont)
    ax.set_ylabel('Altitude',fontproperties=titlefont)
    ax.set_ylim(0.,90)
    leg=ax.legend(numpoints=1,markerscale=1.5,loc=(1.1,0.5),ncol=1,fancybox=True)
    leg.get_frame().set_alpha(0)
    for label in leg.get_texts():
        label.set_fontsize(16)
    plt.xticks(rotation='vertical')
    ax2 = plt.twinx()

    ax.xaxis.set_major_formatter( matplotlib.dates.DateFormatter('%m-%dT%H'))
    ax2.xaxis.set_major_formatter( matplotlib.dates.DateFormatter('%m-%dT%H'))
    ax2.set_ylabel('Airmass',fontproperties=titlefont)
    ax2.set_yticks([19.471,23.578,30,41.8,53.13,65.38,90])
    ax2.set_yticklabels(['3.0','2.5','2.0','1.5','1.25','1.1','1.0'])
    ax.format_xdata = matplotlib.dates.DateFormatter('%Y-%m-%d:%h')
    ax2.format_xdata = matplotlib.dates.DateFormatter('%Y-%m-%d:%h')
    ax.xaxis.set_major_locator(MultipleLocator(1./12.))
    ax.xaxis.set_minor_locator(MultipleLocator(1./120.))
    pngplot = StringIO.StringIO()
    plt.savefig(pngplot,format='png',transparent=True)
    pngplot.seek(0)
    data_uri = pngplot.read().encode("base64").replace("\n", "")
    html = ''.format(data_uri)
    return html

##############################
def addnewobject(_form):
    ddd='<form action="agnupdatetable.py" enctype="multipart/form-data" method="post">'+\
        '<input type="hidden" name="targid" value="">'+\
         '<input name="sn_name" size="30" maxlength="30" type="text" />'+\
         '<input name="SN_RA" size="12" maxlength="12" type="text"  placeholder="00:00:00.000"/>'+\
         '<input name="SN_DEC" size="13" maxlength="13" type="text" placeholder="+00:00:00.000"/>'+\
         '<input type="hidden" name="type" value="newobject">'+\
         '<input type="hidden" name="outputformat" value="'+str(_form)+'">'+\
         '<input type="submit" value="add new object">
'+\
         '</form>'
    return ddd
###########################################################

def grafico(filename,hh='50',vv='50'):
    if filename:
        ggg=''''''+filename+"	"
    else:
       ggg='''	'''
    return ggg

##############################################################
def grafico1(filename,hh='150',vv='180'):
     ggg='''

                	'''
     return ggg

##########################################################################
def addnote(_targid,SN0,SN_RA,SN_DEC,_user,_form):
    ddd='<form action="agnupdatetable.py" enctype="multipart/form-data" method="post">'+\
        '
Add note : <textarea rows="1" cols="100" wrap="physical" name="note"></textarea>'+\
        '<input type="hidden" name="targid" value="'+str(_targid)+'">'+\
        '<input type="hidden" name="sn_name" value='+str(SN0)+'>'+\
        '<input type="hidden" name="SN_RA" value="'+str(SN_RA)+'">'+\
        '<input type="hidden" name="SN_DEC" value="'+str(SN_DEC)+'">'+\
        '<input type="hidden" name="user" value="'+str(_user)+'">'+\
        '<input type="hidden" name="type" value="add">'+\
        '<input type="hidden" name="outputformat" value="'+str(_form)+'">'+\
        '<input type="submit" value="Send">
'+\
        '</form>'  #
    return ddd

##############################################

def markasbad(_id,_targid,_user,_key,_form):
    line='<form action="agnupdatetable.py" enctype="multipart/form-data" method="post">'+\
        '<input type="hidden" name="id" value="'+str(_id)+'">'+\
        '<input type="hidden" name="key" value="'+str(_key)+'">'+\
        '<input type="hidden" name="note" value="bad">'+\
        '<input type="hidden" name="targid" value="'+str(_targid)+'">'+\
        '<input type="hidden" name="user" value="'+str(_user)+'">'+\
        '<input type="hidden" name="type" value="markasbad">'+\
        '<input type="hidden" name="outputformat" value="'+str(_form)+'">'+\
        '<input type="submit" value="mark as bad"></form>'
    return line

def stopobservation(_id,_targid,_user,_key,_form):
    line='<form action="agnupdatetable.py" enctype="multipart/form-data" method="post">'+\
        '<input type="hidden" name="id" value="'+str(_id)+'">'+\
        '<input type="hidden" name="key" value="'+str(_key)+'">'+\
        '<input type="hidden" name="note" value="0">'+\
        '<input type="hidden" name="targid" value="'+str(_targid)+'">'+\
        '<input type="hidden" name="user" value="'+str(_user)+'">'+\
        '<input type="hidden" name="type" value="stopobservation">'+\
        '<input type="hidden" name="outputformat" value="'+str(_form)+'">'+\
        '<input type="submit" value="stop observations"></form>'
    return line

###################################################################################3

def objectinfo(obj,_user,_key,_form):
    line='<form action="agnupdatetable.py" enctype="multipart/form-data" method="post">'+\
        '<input type="hidden" name="id" value="'+str(obj['id'])+'">'+\
        '<input type="hidden" name="key" value="'+str(_key)+'">'+\
        '<input type="hidden" name="targid" value="'+str(obj['targid'])+'">'+\
        '<input type="hidden" name="sn_name" value='+str(obj['name'])+'>'+\
        '<input type="hidden" name="SN_RA" value="'+str(obj['ra_sn'])+'">'+\
        '<input type="hidden" name="SN_DEC" value="'+str(obj['dec_sn'])+'">'+\
        '<input type="hidden" name="user" value="'+str(_user)+'">'+\
        '<input type="hidden" name="type" value="catalogue2">'+\
        '<input type="hidden" name="outputformat" value="'+str(_form)+'">'+\
        '
<textarea rows="1" cols="10" wrap="physical" name="note"></textarea>
'+\
        '<input value="update" type="submit"/> </form>'
    return line

######################################

def recinfo(obj,_user,_form):
  line='<form action="agnupdatetable.py" method="post">'+\
        '<input type="hidden" name="targid" value="'+str(obj['targid'])+'">'+\
        '<input type="hidden" name="sn_name" value='+str(obj['name'])+'>'+\
        '<input type="hidden" name="SN_RA" value="'+str(obj['ra_sn'])+'">'+\
        '<input type="hidden" name="SN_DEC" value="'+str(obj['dec_sn'])+'">'+\
        '<input type="hidden" name="user" value="'+str(_user)+'">'+\
        '<input type="hidden" name="type" value="recinfo">'+\
        '<input type="hidden" name="outputformat" value="'+str(_form)+'">'+\
        '
<textarea rows="1" cols="10" wrap="physical" name="note"></textarea>
'+\
        '<input value="update" type="submit"/> </form>'
  return line

######################################

def delspectrum(SN,SN_RA,SN_DEC,_targid,_id,_namefile,_user,_form):
    ddd='<form action="agnupdatetable.py" enctype="multipart/form-data" method="post">'+\
        '<input type="hidden" name="targid" value="'+str(_targid)+'">'+\
        '<input type="hidden" name="id" value="'+str(_id)+'">'+\
        '<input type="hidden" name="sn_name" value='+str(SN)+'>'+\
        '<input type="hidden" name="SN_RA" value="'+str(SN_RA)+'">'+\
        '<input type="hidden" name="SN_DEC" value="'+str(SN_DEC)+'">'+\
        '<input type="hidden" name="namefile" value="'+str(_namefile)+'">'+\
        '<input type="hidden" name="user" value="'+str(_user)+'">'+\
        '<input type="hidden" name="outputformat" value="'+str(_form)+'">'+\
        '<input type="hidden" name="type" value="delspectrum">'+\
        '<input type="submit" value="delete"></form>'
    return ddd

######################################

def delfromdb(SN,SN_RA,SN_DEC,_targid,ii,_user,_form):
   ddd='<form action="agnupdatetable.py" enctype="multipart/form-data" method="post">'+\
      '<input type="hidden" name="targid" value="'+str(_targid)+'">'+\
      '<input type="hidden" name="id" value="'+str(ii['id'])+'">'+\
      '<input type="hidden" name="note" value="'+str(ii['note'])+'">'+\
      '<input type="hidden" name="sn_name" value='+str(SN)+'>'+\
      '<input type="hidden" name="SN_RA" value="'+str(SN_RA)+'">'+\
      '<input type="hidden" name="SN_DEC" value="'+str(SN_DEC)+'">'+\
      '<input type="hidden" name="user" value="'+str(_user)+'">'+\
      '<input type="hidden" name="outputformat" value="'+str(_form)+'">'+\
      '<input type="hidden" name="type" value="delete">'+\
      '<input type="submit" value="delete"></form>'
   return ddd

###################################################################################3
###################################################################################3

def archivereducedspectrum(img):
    import string,re
    from astropy.io import fits as pyfits
    import agnkey

    hdr = pyfits.getheader(img)
    from agnkey.agnabsphotdef import deg2HMS
    try:     _targid=agnkey.agnsqldef.targimg(img)
    except:  _targid=''

    if 'TELID' in hdr:           _telescope=hdr.get('TELID')
    elif 'TELESCOPE' in hdr:     _telescope=hdr.get('TELESCOP')
    else:                        _telescope='other'

    #    object
    _object=hdr.get('object')
    if not _object: _object='spectrum'
    _object=re.sub('\}','', _object)
    _object=re.sub('\{','', _object)
    _object=re.sub('\[','', _object)
    _object=re.sub('\]','', _object)
    _object=re.sub('\(','', _object)
    _object=re.sub('\)','', _object)
    _object=re.sub('-','', _object)
    _object=re.sub(' ','', _object)
    #    dateobs
    if 'DATE-OBS' in hdr:
        _dateobs=hdr.get('DATE-OBS')
        if 'T' in _dateobs: _dateobs=string.split(_dateobs,'T')[0]
    else:     _dateobs=''
    #   UT
    if 'UTSTART' in hdr:  _ut=hdr.get('UTSTART')
    elif 'UT' in hdr:
        _ut=hdr.get('UT')
    elif 'UTC-OBS' in hdr:
        _ut=hdr.get('UTC-OBS')
    elif 'DATE-OBS' in hdr:
        _ut=hdr.get('DATE-OBS')
    else:
        _ut=''
    if 'T' in _ut:    _ut=string.split(_ut,'T')[1]

    # ra and  dec
    if 'RA' in hdr:    _ra=hdr.get('RA')
    else: _ra=''
    if 'DEC' in hdr:    _dec=hdr.get('DEC')
    else: _dec=''

    if ':' in str(_ra):      _ra,_dec=deg2HMS(_ra,_dec)

    # JD
    if 'MJD' in hdr: _mjd=hdr.get('MJD')
    elif 'MJD-OBS' in hdr: _mjd=hdr.get('MJD-OBS')
    elif 'JD' in hdr: _mjd=hdr.get('JD') - 2400000.5
    elif 'DATE-OBS' in hdr:
        dd=''
        try:
            dd=datetime.datetime.strptime(hdr.get('DATE-OBS')[0:-6],'%Y-%m-%dT%H:%M:%S')
        except:
            try:
                dd=datetime.datetime.strptime(hdr.get('DATE-OBS')[0:-6],'%Y-%m-%dT%H:%M')
            except: pass
        if dd:     _mjd=agnkey.agnsqldef.JDnow(dd,False)  - 2400000.5
        else:      _mjd=''
    else:    _mjd=''

    if 'GRISM' in hdr:
        _grism=re.sub('/','_',hdr.get('GRISM'))
    elif 'GRATING' in hdr:
        _grism=hdr.get('GRATING')
    else:
        _grism='grism'

    if _telescope in ['fts','ftn']:
        dictionary={'dateobs':_dateobs,'exptime':hdr.get('exptime'), 'filter':hdr.get('filter'),'mjd':float(hdr.get('MJD-OBS')),\
                    'telescope':_telescope,'airmass':hdr.get('airmass'),'objname':_object,'ut':_ut,\
                    'instrument':hdr.get('instrume'),'ra0':_ra,'dec0':_dec,'slit':hdr.get('APERWID'),\
                    'targid':_targid,'grism':_grism, 'original':hdr.get('arcfile'),'PROPID':hdr.get('PROPID'),\
                    'dateobs2':hdr.get('DATE-OBS')}

    elif 'gemini' in _telescope.lower():
        if 'south' in _telescope.lower(): _telescope='gs'
        else: _telescope='gn'
        dictionary={'dateobs':_dateobs,'exptime':hdr.get('exptime'), 'filter':hdr.get('filter'),'mjd':_mjd,\
                    'telescope':_telescope,'airmass':hdr.get('AIRMASS'),'objname':_object,'ut':_ut,\
                    'instrument':hdr.get('INSTRUME'),'ra0':_ra,'dec0':_dec,'slit':hdr.get('slit'),'targid':_targid,'grism':_grism,\
                    'original':hdr.get('arcfile')}

    else:
        dictionary={'dateobs':_dateobs,'exptime':hdr.get('exptime'), 'filter':hdr.get('filter'),'mjd':_mjd,\
                    'telescope':_telescope,'airmass':hdr.get('airmass'),'objname':_object,'ut':_ut,\
                    'instrument':hdr.get('instrume'),'ra0':_ra,'dec0':_dec,'slit':hdr.get('slit'),'targid':_targid,'grism':_grism,\
                    'original':hdr.get('arcfile'),'observer':hdr.get('observer')}
        dictionary['namefile']=string.split(img,'/')[-1]

    dictionary['namefile']=string.split(img,'/')[-1]
    _dir=re.sub(string.split(img,'/')[-1],'',img)
    dictionary['directory']=_dir
    return dictionary

########################################

def uploadspectrum(img,_output,_force,_permission,_filetype='fits'):
    from astropy.io import fits as pyfits
    import agnkey
    import numpy as np
    import re,string,sys,os
    import tarfile
    if _filetype=='tar':
        os.system('tar -xf ../tmp/'+img)
        tar=tarfile.open(img)
        imglist=tarfile.TarFile.getnames(tar)
        tar.close()
    else:
        imglist = [img]
    note = ''
    for img in imglist:
          note= note + 'input= '+img+'\n'
          try:
              hdr=pyfits.open(img)[0].header
          except:
              return 'problem '+img+'  reading file. Check that it is in a fits format  '
          if 'TELID' in hdr:  _tel=hdr.get('TELID')
          elif 'TELESCOP' in hdr:    _tel=hdr.get('TELESCOP')
          else:                    _tel='other'
          if not _tel: _tel='other'
          _tel = re.sub(' ','',_tel)
          note = note + 'telescope= ' + _tel + '\n'
          ###################################### # gemini files in jerord format needs some trick
          if 'gemini' in _tel.lower():
              data,hdr0 = pyfits.getdata(img, 'sci', header=True)
              try:        hdr0.__delitem__('AIRMASS')
              except:     hdr0.remove('AIRMASS')
              hed=['TELESCOP','OBSERVAT','RA','DEC','UT','ST','EXPTIME','MASKNAME',\
                   'GRATING','CENTWAVE','OBSMODE','GAIN','RDNOISE','MJD-OBS',\
                   'PIXSCALE','DATE-OBS','AIRMASS']
              for jj in hed:
                  hdr0.update(jj,hdr[jj])
              if 'south' in _tel.lower(): _tel='gs'
              else: _tel='gn'
              pyfits.writeto(re.sub('.fits','0.fits',img), np.float32(data), hdr0)
              img=re.sub('.fits','0.fits',img)
          dictionary=archivereducedspectrum(img)
          _grism=dictionary['grism']
          _date=dictionary['dateobs']
          _date=re.sub('-','',_date)
          if 'T' in _date: string.split(_date,'T')[0]
          _ut=dictionary['ut']
          _object=dictionary['objname']
          if not _output:
                  _output=str(_object)+'_'+str(_date)+'_'+str(_grism)+'_'+re.sub(':','',str(_ut))+'.fits'
          _output=re.sub('/','_',_output)

          if agnkey.util.host=='SVMAC':
              directory='/Users/svalenti/redu2/AGNKEY/spectra/'+_date+'_'+_tel
              directory1=re.sub('/Users/svalenti/redu2/','../',directory)
          elif agnkey.util.host=='deneb':
              directory='/home/cv21/ANKEY_www/AGNKEY/spectra/'+_date+'_'+_tel
              directory1=re.sub('/home/cv21/AGNKEY_www/','../',directory)
          elif agnkey.util.host=='dark':
              directory='/dark/hal/AGNKEY/spectra/'+_date+'_'+_tel
              directory1=re.sub('/dark/hal/','../../',directory)

          dictionary['directory']=directory+'/'
          dictionary['namefile']=_output

          note=note+'output= '+_output+'\n'
          if not dictionary['objname']: note=note+'ERROR= OBJECT not defined '
          else:                         note=note+'objname= '+str(dictionary['objname'])+'\n'
          if not dictionary['directory']: note=note+'ERROR= directory not defined '
          else:                         note=note+'directory= '+str(dictionary['directory'])+'\n'
          if not dictionary['ra0']:     note=note+'ERROR= RA not defined \n'
          else:                         note=note+'ra= '+str(dictionary['ra0'])+'\n'
          if not dictionary['dec0']:    note=note+'ERROR= DEC not defined \n'
          else:                         note=note+'dec= '+str(dictionary['dec0'])+'\n'
          if not dictionary['targid']:  note=note+'ERROR= TARGID not defined (it will be automatic generated when RA,DEC and OBJECT are defined)\n'
          else:                         note=note+'targid= '+str(dictionary['targid'])+'\n'
          if not dictionary['dateobs']: note=note+'ERROR= DATE-OBS not defined \n'
          else:                         note=note+'dateobs= '+str(dictionary['dateobs'])+'\n'

          if 'ERROR' in note:
              return note
          else:
              if os.path.isdir(directory1): print 'directory there'
              else:                        os.system('mkdir '+directory1)
              if os.path.isfile(directory1+'/'+_output):
                  note=note+'file already there'+'\n'
                  if _force=='force':
                      note=note+'replace file'+'\n'
                      os.system('rm '+directory1+'/'+_output)
                      os.system('cp '+img+' '+directory1+'/'+_output)
              else:
                  os.system('cp '+img+' '+directory1+'/'+_output)
              #if _tel in ['ftn','fts']:
              #    datarawtable='datareduspectra'
              #else:

              datarawtable='dataspectraexternal'
              if datarawtable=='dataspectraexternal':
                  dictionary['access']=_permission
                  note=note+'acces= '+str(dictionary['access'])+'\n'

              note=note+'database= '+datarawtable+'\n'
              if not agnkey.agnsqldef.getfromdataraw(agnkey.agnsqldef.conn,datarawtable,'namefile', string.split(_output,'/')[-1],column2='namefile'):
                  agnkey.agnsqldef.insert_values(agnkey.agnsqldef.conn,datarawtable,dictionary)
              else:
                  if _force=='update':
                      note=note+'update database'+'\n'
                      for voce in dictionary:
                          if voce!='id' and voce!='namefile':
                              agnkey.agnsqldef.updatevalue(datarawtable,voce,dictionary[voce],string.split(_output,'/')[-1])
                  elif _force=='force':
                      note=note+'replace line in the database'+'\n'
                      agnkey.agnsqldef.deleteredufromarchive(string.split(_output,'/')[-1],datarawtable,'namefile')
                      agnkey.agnsqldef.insert_values(agnkey.agnsqldef.conn,datarawtable,dictionary)
                  else: note=note+'database not changed'+'\n'
    return note

########################################################################################
def obsin(targid,_days=7):
    import agnkey
    import datetime
    import time
    import re
    def JDnow(datenow='',verbose=False):
        _JD0=2455927.5
        if not datenow:
            datenow=datetime.datetime(time.gmtime().tm_year, time.gmtime().tm_mon, time.gmtime().tm_mday, time.gmtime().tm_hour, time.gmtime().tm_min, time.gmtime().tm_sec)
        _JDtoday=_JD0+(datenow-datetime.datetime(2012, 01, 01,00,00,00)).seconds/(3600.*24)+\
            (datenow-datetime.datetime(2012, 01, 01,00,00,00)).days
        if verbose: print 'JD= '+str(_JDtoday)
        return _JDtoday

    _JDn=JDnow()

    command='select l.name,l.targid,l.ra_sn,l.dec_sn,o.filters,o.exptime,o.windowstart,o.windowend,o.tracknumber, o.reqnumber, o.tarfile, o.status from obslog as o join lsc_sn_pos as l where windowend > '+str(_JDn)+' and l.targid=o.targid and l.targid="'+str(targid)+'"'
    command1='select l.name,l.targid,l.ra_sn,l.dec_sn, o.filters,o.exptime,o.windowstart,o.windowend,o.tracknumber, o.reqnumber, o.tarfile, o.status from obslog as o join lsc_sn_pos as l where windowend > '+str(_JDn-float(_days))+'and windowend < '\
+str(_JDn)+' and l.targid=o.targid and l.targid="'+str(targid)+'"'

    aa=agnkey.agnsqldef.query([command])
    cc=agnkey.agnsqldef.query([command1])
    ll0={}
    if aa:
        for i in aa[0].keys(): ll0[i]=[]
        for jj in range(0,len(aa)):
            for kk in aa[0].keys():
                ll0[kk].append(aa[jj][kk])
    ll1={}
    if cc:
        for i in cc[0].keys(): ll1[i]=[]
        for jj in range(0,len(cc)):
            for kk in cc[0].keys():
                ll1[kk].append(cc[jj][kk])

    readpass=agnkey.util.readpasswd(agnkey.util.workingdirectory,agnkey.util.realpass)
    username,passwd=readpass['odinuser'],readpass['odinpasswd']
    lll0=''
    lll1=''
    if ll0:
        ccc='BGCOLOR="#CCFF66"'
        for i in range(0,len(ll0['name'])):
            if float(ll0['tracknumber'][i])>1:
                if ll0['status'][i]:
                    _status=ll0['status'][i]
                else:
                    _status=''
#                if _status=='PENDING': _status=''
                _date1 = agnkey.util.jd2date(ll0['windowend'][i])
                _date0 = agnkey.util.jd2date(ll0['windowstart'][i])
                if ll0['tarfile'][i]:
                    _tarfile=ll0['tarfile'][i]
                else:
                    _tarfile=''
                if ll0['reqnumber'][i]:
                    _reqnumber=ll0['reqnumber'][i]
                else:
                    _reqnumber=''
                if 'floyds' in ll0['filters'][i]:
                  if _reqnumber:
                    if not _tarfile:
                        try:
                            _date=re.sub('-','',_dict['requests'][ll0['tracknumber'][i]]['schedule'][0]['frames'][0]['day_obs'])
                            _tarfile=_reqnumber+'_'+str(_date)+'.tar.gz'
                            agnkey.agnsqldef.updatevalue('obslog','tarfile',_tarfile,str(ll0['tracknumber'][i]),connection='agnkey',namefile0='tracknumber')
                        except:
                            _tarfile=''
                    if _tarfile:
                        lll0=lll0+'
'+str(ll0['name'][i])+'	'+str(ll0['filters'][i])+'	'+str(ll0['exptime'][i])+'	'+\
                              str(_date0)+'	'+str(_date1)+'	'+\
                              str(ll0['tracknumber'][i])+'	'+str(_status)+\
                              '	'+' download tar '+'
'

                    else:
                        lll0=lll0+'
'+str(ll0['name'][i])+'	'+str(ll0['filters'][i])+'	'+str(ll0['exptime'][i])+'	'+\
                              str(_date0)+'	'+str(_date1)+'	'+\
                              str(ll0['tracknumber'][i])+'	'+str(_status)+'
'
#                            str(ll0['windowstart'][i])+'	'+str(ll0['windowend'][i])+'	'+'	'+str(_date0)+'	'+\

                  else:
                      lll0=lll0+'
'+str(ll0['name'][i])+'	'+str(ll0['filters'][i])+'	'+str(ll0['exptime'][i])+'	'+\
                            str(_date0)+'	'+str(_date1)+'	'+\
                            str(ll0['tracknumber'][i])+'	'+str(_status)+'
'
#                            str(ll0['windowstart'][i])+'	'+str(ll0['windowend'][i])+'	'+'	'+str(_date0)+'	'+\
                else:
                    lll0=lll0+'
'+str(ll0['name'][i])+'	'+str(ll0['filters'][i])+'	'+str(ll0['exptime'][i])+'	'+\
                          str(_date0)+'	'+str(_date1)+'	'+\
                          str(ll0['tracknumber'][i])+'	'+str(_status)+'
'
#                        str(ll0['windowstart'][i])+'	'+str(ll0['windowend'][i])+\

    if ll1:
        for i in range(0,len(ll1['name'])):
            if float(ll1['tracknumber'][i])>1:
                _date1 = agnkey.util.jd2date(ll1['windowend'][i])
                _date0 = agnkey.util.jd2date(ll1['windowstart'][i])
                if ll1['status'][i]:
                    _status=ll1['status'][i]
                else:
                    _status=''
                if ll1['tarfile'][i]:
                    _tarfile=ll1['tarfile'][i]
                else:
                    _tarfile=''
                if ll1['reqnumber'][i]:
                    _reqnumber=ll1['reqnumber'][i]
                else:
                    _reqnumber=''
                if 'floyds' in ll1['filters'][i]:
                   if _reqnumber:
                            if _tarfile:
                                agnkey.agnsqldef.updatevalue('obslog','tarfile',_tarfile,str(ll1['tracknumber'][i]),connection='agnkey',namefile0='tracknumber')
                                lll1=lll1+'
'+str(ll1['name'][i])+'	'+str(ll1['filters'][i])+'	'+str(ll1['exptime'][i])+'	'+\
                                      str(_date0)+'	'+str(_date1)+'	'+\
                                      str(ll1['tracknumber'][i])+'	'+str(_status)+\
                                      '	'+' download tar '+'
'
                                      #str(ll1['windowstart'][i])+'	'+str(ll1['windowend'][i])+'	'+

                            else:
                                lll1=lll1+'
'+str(ll1['name'][i])+'	'+str(ll1['filters'][i])+'	'+str(ll1['exptime'][i])+'	'+\
                                      str(_date0)+'	'+str(_date1)+'	'+\
                                      str(ll1['tracknumber'][i])+'	'+str(_status)+'
'
                                      #str(ll1['windowstart'][i])+'	'+str(ll1['windowend'][i])+'	'+
                   else:
                       pass
                else:
                    lll1=lll1+'
'+str(ll1['name'][i])+'	'+str(ll1['filters'][i])+'	'+str(ll1['exptime'][i])+'	'+\
                          str(_date0)+'	'+str(_date1)+'	'+\
                          str(ll1['tracknumber'][i])+'	'+str(_status)+'
'
                    #str(ll1['windowstart'][i])+'	'+str(ll1['windowend'][i])+'	'+\
    return lll0,lll1

##########################################################################################

########################################################################################
def obsin2(targid,_days=7):
    import agnkey
    import datetime
    import time
    import re
    def JDnow(datenow='',verbose=False):
        _JD0=2455927.5
        if not datenow:
            datenow=datetime.datetime(time.gmtime().tm_year, time.gmtime().tm_mon, time.gmtime().tm_mday, time.gmtime().tm_hour, time.gmtime().tm_min, time.gmtime().tm_sec)
        _JDtoday=_JD0+(datenow-datetime.datetime(2012, 01, 01,00,00,00)).seconds/(3600.*24)+\
            (datenow-datetime.datetime(2012, 01, 01,00,00,00)).days
        if verbose: print 'JD= '+str(_JDtoday)
        return _JDtoday

    _JDn=JDnow()

    command='select l.name, l.targid, l.ra_sn, l.dec_sn, o.filters, o.exptime, o.mode, g.windowstart, g.windowend, g.tracknumber, '+\
        'g.reqnumber, g.tarfile, g.status from triggers as o join triggerslog as g join lsc_sn_pos as l '+\
        'where o.id = g.triggerid and g.windowend > '+str(_JDn)+' and l.targid=o.targid and l.targid="'+str(targid)+'"'
    command1='select l.name,l.targid, l.ra_sn, l.dec_sn, o.filters, o.exptime, o.mode, g.windowstart, g.windowend, g.tracknumber, '+\
        'g.reqnumber, g.tarfile, g.status from triggers as o join triggerslog as g join lsc_sn_pos as l '+\
        'where o.id = g.triggerid and g.windowend > '+str(_JDn-float(_days))+'and g.windowend < '+\
        str(_JDn)+' and l.targid=o.targid and l.targid="'+str(targid)+'"'

    aa=agnkey.agnsqldef.query([command])
    cc=agnkey.agnsqldef.query([command1])
    ll0={}
    if aa:
        for i in aa[0].keys(): ll0[i]=[]
        for jj in range(0,len(aa)):
            for kk in aa[0].keys():
                ll0[kk].append(aa[jj][kk])
    ll1={}
    if cc:
        for i in cc[0].keys(): ll1[i]=[]
        for jj in range(0,len(cc)):
            for kk in cc[0].keys():
                ll1[kk].append(cc[jj][kk])

    readpass=agnkey.util.readpasswd(agnkey.util.workingdirectory,agnkey.util.realpass)
    username,passwd=readpass['odinuser'],readpass['odinpasswd']
    lll0=''
    lll1=''
    if ll0:
        ccc='BGCOLOR="#CCFF66"'
        for i in range(0,len(ll0['name'])):
            if float(ll0['tracknumber'][i])>=0:
                _date1 = agnkey.util.jd2date(ll0['windowend'][i])
                _date0 = agnkey.util.jd2date(ll0['windowstart'][i])
                if ll0['status'][i]:
                    _status=ll0['status'][i]
                else:
                    _status=''
#                if _status=='PENDING': _status=''
                if ll0['tarfile'][i]:
                    _tarfile=ll0['tarfile'][i]
                else:
                    _tarfile=''
                if ll0['reqnumber'][i]:
                    _reqnumber=ll0['reqnumber'][i]
                else:
                    _reqnumber=''
                if 'floyds' in ll0['filters'][i]:
                  if _reqnumber:
                    if not _tarfile:
                        try:
                            _date=re.sub('-','',_dict['requests'][ll0['tracknumber'][i]]['schedule'][0]['frames'][0]['day_obs'])
                            _tarfile=_reqnumber+'_'+str(_date)+'.tar.gz'
                            agnkey.agnsqldef.updatevalue('triggerslog','tarfile',_tarfile,str(ll0['tracknumber'][i]),connection='agnkey',namefile0='tracknumber')
                        except:
                            _tarfile=''
                    if _tarfile:
                        lll0=lll0+'
'+str(ll0['name'][i])+'	'+str(ll0['filters'][i])+'	'+str(ll0['exptime'][i])+'	'+\
                              str(_date0)+'	'+str(_date1)+'	'+str(ll0['mode'][i])+'	'+\
                              str(ll0['tracknumber'][i])+'	'+str(_status)+\
                              '	'+' download tar '+'
'
                              #str(ll0['windowstart'][i])+'	'+str(ll0['windowend'][i])+'	'+\
                    else:
                        lll0=lll0+'
'+str(ll0['name'][i])+'	'+str(ll0['filters'][i])+'	'+str(ll0['exptime'][i])+'	'+\
                              str(_date0)+'	'+str(_date1)+'	'+str(ll0['mode'][i])+'	'+\
                              str(ll0['tracknumber'][i])+'	'+str(_status)+'
'
#                              str(ll0['windowstart'][i])+'	'+str(ll0['windowend'][i])+'	'+\
                  else:
                      lll0=lll0+'
'+str(ll0['name'][i])+'	'+str(ll0['filters'][i])+'	'+str(ll0['exptime'][i])+'	'+\
                            str(_date0)+'	'+str(_date1)+'	'+str(ll0['mode'][i])+'	'+\
                            str(ll0['tracknumber'][i])+'	'+str(_status)+'
'
                            #str(ll0['windowstart'][i])+'	'+str(ll0['windowend'][i])+'	'+\
                else:
                    lll0=lll0+'
'+str(ll0['name'][i])+'	'+str(ll0['filters'][i])+'	'+str(ll0['exptime'][i])+'	'+\
                          str(_date0)+'	'+str(_date1)+'	'+str(ll0['mode'][i])+'	'+\
                          str(ll0['tracknumber'][i])+'	'+str(_status)+'
'
                          #str(ll0['windowstart'][i])+'	'+str(ll0['windowend'][i])+'	'+\

    if ll1:
        for i in range(0,len(ll1['name'])):
            if float(ll1['tracknumber'][i])>=0:
                _date1 = agnkey.util.jd2date(ll1['windowend'][i])
                _date0 = agnkey.util.jd2date(ll1['windowstart'][i])
                if ll1['status'][i]:
                    _status=ll1['status'][i]
                else:
                    _status=''
                if ll1['tarfile'][i]:
                    _tarfile=ll1['tarfile'][i]
                else:
                    _tarfile=''
                if ll1['reqnumber'][i]:
                    _reqnumber=ll1['reqnumber'][i]
                else:
                    _reqnumber=''
                if 'floyds' in ll1['filters'][i]:
                   if _reqnumber:
                            if _tarfile:
                                agnkey.agnsqldef.updatevalue('triggerslog','tarfile',_tarfile,str(ll1['tracknumber'][i]),connection='agnkey',namefile0='tracknumber')
                                lll1=lll1+'
'+str(ll1['name'][i])+'	'+str(ll1['filters'][i])+'	'+str(ll1['exptime'][i])+'	'+\
                                      str(_date0)+'	'+str(_date1)+'	'+str(ll1['mode'][i])+'	'+\
                                      str(ll1['tracknumber'][i])+'	'+str(_status)+\
                                      '	'+' download tar '+'
'
                                #str(ll1['windowstart'][i])+'	'+str(ll1['windowend'][i])+'	'+\
                            else:
                                lll1=lll1+'
'+str(ll1['name'][i])+'	'+str(ll1['filters'][i])+'	'+str(ll1['exptime'][i])+'	'+\
                                      str(_date0)+'	'+str(_date1)+'	'+str(ll1['mode'][i])+'	'+\
                                      str(ll1['tracknumber'][i])+'	'+str(_status)+'
'
                                      #str(ll1['windowstart'][i])+'	'+str(ll1['windowend'][i])+'	'+\
                   else:
                       pass
                else:
                    lll1=lll1+'
'+str(ll1['name'][i])+'	'+str(ll1['filters'][i])+'	'+str(ll1['exptime'][i])+'	'+\
                          str(_date0)+'	'+str(_date1)+'	'+str(ll1['mode'][i])+'	'+\
                          str(ll1['tracknumber'][i])+'	'+str(_status)+'
'
                    #str(ll1['windowstart'][i])+'	'+str(ll1['windowend'][i])+\
    return lll0,lll1

##########################################################################################

def get_filtclr():
 filtclr={}
 filtclr['U']='purple'
 filtclr['B']='blue'
 filtclr['V']='#00bc00'
 filtclr['R']='red'
 filtclr['I']= '#CCCC33'
 filtclr['Bessell-B']='blue'
 filtclr['Bessell-V']='#00bc00'
 filtclr['Bessell-R']='red'
 filtclr['Bessell-I']='#CCCC33'
 filtclr['up']='purple'
 filtclr['gp']='green'
 filtclr['rp']='red'
 filtclr['ip']='#CCCC33'
 filtclr['zs']='black'
 filtclr['w']='#a883b8'
 filtclr['SDSS-U']='purple'
 filtclr['SDSS-G']='green'
 filtclr['SDSS-R']='red'
 filtclr['SDSS-I']='black'
 filtclr['Pan-Starrs-Z']='black'
 filtclr['H-Alpha']='grey'
 return filtclr

##############################################################################

def sqlquery(db,command):
   import MySQLdb,os,string
   lista=''
   try:
       cursor = db.cursor(MySQLdb.cursors.DictCursor)
       cursor.execute(command)
       lista = cursor.fetchall()
       if cursor.rowcount == 0:
           pass
       cursor.close()
   except MySQLdb.Error, e:
       lista = "Error %d: %s" % (e.args[0], e.args[1])
       print lista
   return lista

#################################################################

# lcdata='[ {label: "COJ B",  points: {show: true, fill: true, fillColor: "blue", type: "o", radius: 2, errorbars: "y", yerr: {show:true, upperCap: "-", lowerCap: "-", radius: 2} },  color: "blue",  data: [ [-17.2193814586, 4.426, 0.161], [-17.2162217363, 0.322, 0.469], [-9.20783068286, 2.1532, 0.155], [-9.2047520373, 1.238, 0.264] ] },{}]'
# mint, maxt, minmag, maxmag=-18.85779960560616, 0.8032181585673273, -0.5191500000005, 6.7708166666705

def plot_phot(db,targid, width=450, height=250, plottype='flot', magtype='psfmag'):
 import random
 # Get the data string:
 lcdata, mint, maxt, minmag, maxmag = load_lc_data(db,targid,plottype,magtype)

 if lcdata == '':
     print '''No photometry to display'''
     return ''
 # Make the flot plot:
 r = random.randrange(0, 10001)
 print '''

''' %(targid, magtype, str(r), str(width), str(height))
# print 'd'
 print ''' '''
 return ''

#################################################################################

def load_lc_data(db,targid,plottype='flot',magtype='psfmag',_ft=1):
 filtclr=get_filtclr()
 if magtype in  ['psfmag','apmag','appmagap1','appmagap2','appmagap3','mag']:
     if magtype=='psfmag':
      dmagtype='psfdmag'
     elif magtype=='apmag':
      dmagtype='psfdmag'
     elif magtype=='appmagap1':
      dmagtype='dappmagap1'
     elif magtype=='appmagap2':
      dmagtype='dappmagap2'
     elif magtype=='appmagap3':
      dmagtype='dappmagap3'
     elif magtype=='mag':
      dmagtype='dmag'

     invert=False
     query = '''SELECT %s, ((mjd+2400000.5)-(to_days(now())+1721059.5+hour(curtime())/24+minute(curtime())/60/24)) as daysago ''' % magtype
     query += '''FROM dataredulco '''
     query += '''WHERE %s is not null and targid=%s ''' % (magtype,targid)
     query += ''' and %s !=  9999   ''' % magtype
     query += '''ORDER BY daysago desc'''
     cursor = sqlquery(db,query)
     if len(cursor) == 0:
         if magtype=='mag':
             magtype='psfmag'
             dmagtype='psfdmag'
             query = '''SELECT %s, ((mjd+2400000.5)-(to_days(now())+1721059.5+hour(curtime())/24+minute(curtime())/60/24)) as daysago ''' % magtype
             query += '''FROM dataredulco '''
             query += '''WHERE %s is not null and targid=%s ''' % (magtype,targid)
             query += ''' and %s !=  9999   ''' % magtype
             query += '''ORDER BY daysago desc'''
             cursor = sqlquery(db,query)
             if len(cursor) == 0:
                 return ('', 0, 0, 0, 0)
             else:
                invert=True
         else:
             return ('', 0, 0, 0, 0)

     days,psfmag=zip(*[[i['daysago'],i[magtype]] for i in cursor])
     maxt=max(days)
     mint=min(days)
     interval = maxt-mint
     mint = mint-0.1*interval
     maxt = maxt+0.1*interval

     minmag = min(psfmag)
     maxmag = max(psfmag)

     query  = '''SELECT distinct p.filter, p.telescope, p.instrument '''
     query += '''FROM dataredulco as p '''
     query += '''WHERE p.%s is not null ''' % magtype
     query += ''' and p.filetype = %s ''' % str(_ft)
     query += ''' and %s !=  9999   '''  %  magtype
     query += '''AND p.targid=%s ''' % targid

     cursor = sqlquery(db,query)
     if len(cursor)>=1:
         if plottype == 'flot':
             lcdata = '[ '
             for row in cursor:
                 lcdata += '''{label: "%s %s", ''' %(row['telescope'],row['filter'])
                 if row['filter'] in filtclr:
                     clr = filtclr[row['filter']]
                 else:
                     clr = 'black'
                 lcdata += ''' points: {show: true, fill: true, fillColor: "%s", type: "o", radius: 2, errorbars: "y", yerr: {show:true, upperCap: "-", lowerCap: "-", radius: 2} }, ''' %clr
                 lcdata += ''' color: "%s", ''' %clr
                 lcdata += ''' data: [ '''
                 query =  '''SELECT ((p.mjd+2400000.5)-(to_days(now())+1721059.5+hour(curtime())/24+minute(curtime())/60/24)) as daysago, '''
                 query += '''p.%s, p.%s  ''' % (magtype,dmagtype)
                 query += '''FROM dataredulco as p '''
                 query += '''WHERE p.%s is not null ''' % magtype
                 query += ''' and p.filetype = %s ''' % str(_ft)
                 query += ''' and p.%s !=  9999   '''  % magtype
                 query += '''AND p.targid=%s ''' %targid
                 query += '''AND p.filter='%s' ''' %row['filter']
                 datacursor = sqlquery(db,query)
                 for datarow in datacursor:
                     if datarow[dmagtype] == 9999: #HACK, need to find out why this happens (not null mag but 9999 emag)
                         datarow[dmagtype] = 0
                     lcdata += '''[%s, %s, %s], ''' %(datarow['daysago'], datarow[magtype], datarow[dmagtype])
                 if lcdata[-2:] == ', ':
                     lcdata = lcdata[:-2] # remove last apostrophe
                 lcdata += ''' ] }, '''
             lcdata = lcdata[:-2] # remove last apostrophe
             lcdata += ''' ]'''

         elif plottype == 'google':
             lcdata  = '''['Days Ago','Mag','MagLow','MagHigh',],'''
         else:
             lcdata = '[]'
     else:
         lcdata = '[]'
 else:
     lcdata = []
     mint = 0
     maxt = 0
     minmag = 0
     maxmag = 0
 return (lcdata, mint, maxt, minmag, maxmag)

########################################################################################################################
def triggercadence(SN0,SN_RA,SN_DEC,_targid,_form,observations={},proposal=agnkey.util.readpass['proposal']):
    sss='<form style="background-color:#F5F5DC" action="agnupdatetable.py" enctype="multipart/form-data" method="post">'+\
        '

 TRIGER NEW OBSERVATION  <input type="submit" value="Send">

'+\
        '<input type="hidden" name="sn_name" value='+str(SN0)+'>'+\
        '<input type="hidden" name="SN_RA" value="'+str(SN_RA)+'">'+\
        '<input type="hidden" name="SN_DEC" value="'+str(SN_DEC)+'">'+\
        '<input type="hidden" name="targid" value="'+str(_targid)+'">'
    for key in observations:
        if 'xxx' in key:
            sss=sss+'<input type="hidden" name="'+str(key)+'" value="'+str(observations[key])+'">'
    sss=sss+'<input type="hidden" name="type" value="triggercadence">'+\
         '<input type="hidden" name="outputformat" value="'+str(_form)+'">'+\
         '
'+\
         '
 U 	 B 	 V 	 R 	 I 	 u 	 g 	 r 	 i 	 z

'
    for i in 'UBVRIugriz':
        if i in ['B','V','g','r','i']:
            sss=sss+' <input type="checkbox" name="'+str(i)+'" value="'+str(i)+'" checked>	'
        else:
            sss=sss+' <input type="checkbox" name="'+str(i)+'" value="'+str(i)+'" >	'
    sss=sss+' select filters

'
    for i in 'UBVRIugriz':
        sss=sss+ '<select name="n'+str(i)+'">'
        sss=sss+ '<option value="2"> 2</option>  <option value="1">1</option> <option value="3">3</option><option value="5">5</option> <option value="10">10</option>'
        sss=sss+ '</select>	'
    sss=sss+ ' number of exposure

'
    for i in 'UBVRIugriz':
        sss=sss+ '
<select name="exp'+str(i)+'"  width:100px;>'+\
             '<option  width=10 value="120"> 120</option>'+\
             '<option value="15">15</option>'+\
             '<option value="30">30</option>'+\
             '<option value="45">45</option>'+\
             '<option value="60">60</option>'+\
             '<option value="90">90</option>'+\
             '<option value="120">120</option>'+\
             '<option value="150">150</option>'+\
             '<option value="180">180</option>'+\
             '<option value="200">200</option>'+\
             '<option value="240">240</option>'+\
             '<option value="300">300</option>'+\
             '<option value="360">360</option>'+\
             '<option value="400">400</option>'+\
             '<option value="600">600</option>'
        sss=sss+ '</select>
	'
    sss=sss+ 'exposure time 	'+\
         '
'+\
         '
'+\
         '
'+\
         '
 site: <select name="site">'+\
         '<option value="any"> any</option>  <option value="lsc">lsc</option>'+\
         '<option value="elp">elp</option>   <option value="coj">coj</option> <option value="cpt">cpt</option>'+\
         '</select>
'+\
         '
 cadence [days]: '+\
         '<input name="cadence" size="4" maxlength="4" type="text" />
'+\
         '
 airmass limit: '+\
         '<input name="airmass" size="4" maxlength="4" default="1.6" type="text" />
'+\
         '
 obs mode:  <select name="obsmode">'+\
         '<option value="normal"> normal </option> '+\
         '<option value="ToO"> too </option> '+\
         '</select>
'+\
         '
 proposal: <select name="proposal">'
    for kk in proposal:
             sss=sss+'<option value="'+kk+'"> '+kk+' </option> '
    sss=sss+'</select>
'+\
         '
 instrument  <select name="instrument">'+\
         '<option value="sbig"> sbig </option>  <option value="sinistro">sinistro</option>'+\
         '<option value="spectral">spectral</option>'+\
         '<option value="ONEOF">ONEOF</option>'+\
         '
'+\
         '</form>'
    return sss

#################################
######################################################################################################
def triggerfloydscadence(SN0,SN_RA,SN_DEC,_targetid,_form,observations={},proposal=agnkey.util.readpass['proposal']):
    sss='<form style="background-color:#F5F5DC " action="agnupdatetable.py" enctype="multipart/form-data" method="post">'+\
        '

 TRIGER NEW OBSERVATION   <input type="submit" value="Send">

'+\
        '<input type="hidden" name="sn_name" value='+str(SN0)+'>'+\
        '<input type="hidden" name="SN_RA" value="'+str(SN_RA)+'">'+\
        '<input type="hidden" name="SN_DEC" value="'+str(SN_DEC)+'">'+\
        '<input type="hidden" name="targid" value="'+str(_targetid)+'">'
    for key in observations:
        if 'xxx' in key:
            sss=sss+'<input type="hidden" name="'+str(key)+'" value="'+str(observations[key])+'">'
    sss=sss+'<input type="hidden" name="type" value="triggerfloydscadence">'+\
         '<input type="hidden" name="outputformat" value="'+str(_form)+'">'
    sss=sss+ '
exposure time: <select name="expfloyds'+'"  width:100px;>'+\
         '<option  width=10 value="300"> 300</option>'+\
         '<option value="600">600</option>'+\
         '<option value="900">900</option>'+\
         '<option value="1200">1200</option>'+\
         '<option value="1500">1500</option>'+\
         '<option value="1800">1800</option>'+\
         '<option value="2700">2700</option>'+\
         '<option value="3600">3600</option>'+\
         '<option value="60">60</option>'+\
         '<option value="30">30</option>'
    sss=sss+ '</select>
	'
    sss=sss+'
 number of spectra : <select name="nexpfloyds'+'"  width:100px;>'+\
         '<option  width=10 value="1"> 1</option>'+\
         '<option  width=10 value="2"> 2</option>'+\
         '<option  width=10 value="3"> 3</option>'+\
         '<option  width=10 value="4"> 4</option>'+ '</select>
	'
    sss=sss+'
'+\
         '
'+\
         '
'+\
         '
 site: <select name="site">'+\
         '<option value="any"> any</option>'+\
         '<option value="ogg">FTN</option>'+\
         '<option value="coj">FTS</option>'+\
         '</select>
'+\
         '
 slit: <select name="slit">'+\
         '<option value="default">default </option>'+\
         '<option value="1.2">1.2</option>'+\
         '<option value="1.6">1.6</option>'+\
         '<option value="2.0">2.0</option>'+\
         '<option value="6.0">6.0</option>'+\
         '</select>
'+\
         '
 cadence [days]: '+\
         '<input name="cadence" size="4" maxlength="4" type="text" />
'+\
         '
 airmass limit: '+\
         '<input name="airmass" size="4" maxlength="4" default="1.6" type="text" />
'+\
         '
 obs mode:  <select name="obsmode">'+\
         '<option value="normal"> normal </option> '+\
         '<option value="ToO"> too </option> '+\
         '</select>
'+\
         '
 mode:  <select name="acmode">'+\
         '<option value="wcs" > wcs </option>'+\
         ' <option value="brightest" > brightest </option>'+\
         '</select>
'+\
         '
 proposal: <select name="proposal">'
    for kk in proposal:
             sss=sss+'<option value="'+kk+'"> '+kk+' </option> '
    sss=sss+'</select>
'+\
         '
'+\
         '</form>'
    return sss

################################################################################

max_stars_repo_name: svalenti/agnkey

max_issues_repo_name: svalenti/agnkey

max_forks_repo_name: svalenti/agnkey

reason: import numpy,from astropy


text:
'''
相机类
本脚本基于MindVision官方demo from http://www.mindvision.com.cn/rjxz/list_12.aspx?lcid=139
我们为各个相机指定了编号，
请使用者确认使用的是MindVision相机并修改本脚本中参数以适应对应的传感器安装方案
'''
import cv2
import numpy as np
from _sdk import mvsdk
import yaml
import time
from datetime import datetime

from radar_class.config import camera_match_list,CAMERA_CONFIG_DIR,CAMERA_YAML_PATH,CAMERA_CONFIG_SAVE_DIR,preview_location

def open_camera(camera_type, is_init, date):
    # try to open the camera
    cap = None
    init_flag = False
    try:
        cap = HT_Camera(camera_type, is_init, date)
        r, frame = cap.read()  # read once to examine whether the cap is working
        assert r, "[INFO] Camera not init"  # 读取失败则报错
        r, frame = cap.read()
        if not is_init:  # 若相机已经启动过一次则不进行调参
            # 建立预览窗口
            cv2.namedWindow("preview of {0}".format(camera_type), cv2.WINDOW_NORMAL)
            cv2.resizeWindow("preview of {0}".format(camera_type), 840, 640)
            cv2.setWindowProperty("preview of {0}".format(camera_type), cv2.WND_PROP_TOPMOST, 1)
            win_loc = 0 if camera_type in [0, 1] else 1
            # 移动至合适位置
            cv2.moveWindow("preview of {0}".format(camera_type), *preview_location[win_loc])
            cv2.imshow("preview of {0}".format(camera_type), frame)
            key = cv2.waitKey(0)
            cv2.destroyWindow("preview of {0}".format(camera_type))
            # 按其他键则不调参使用默认参数，按t键则进入调参窗口，可调曝光和模拟增益
            if key == ord('t') & 0xFF:
                cap.NoautoEx()
                # camera_type > 1 表示该相机为上相机
                # 我们采用的上相机曝光区间比较短故采用微秒为单位调整，左右相机曝光区间长采用ms为单位调整
                if camera_type > 1:
                    tune_exposure(cap, high_reso=True)
                else:
                    tune_exposure(cap)
            cap.saveParam(date)  # 保存参数，以便在比赛中重启时能够自动读取准备阶段设置的参数
        init_flag = True
    except Exception as e:
        # If cap is not open, hcamera will be -1 and cap.release will do nothing. If camera is open, cap.release will close the camera
        cap.release()
        print("[ERROR] {0}".format(e))
    return init_flag, cap

class Camera_Thread(object):
    def __init__(self, camera_type, video=False, video_path=None):
        '''
        the Camera_Thread class which will restart camera when it broken

        :param camera_type: 相机编号
        :param video: 是否使用视频
        :param video_path: 视频路径

        '''
        self._camera_type = camera_type
        self._date = datetime.now().strftime('%Y-%m-%d %H-%M-%S')
        self._open = False
        self._cap = None
        self._is_init = False
        self._video = video
        self._video_path = video_path
        # try to open it once
        self.open()

    def open(self):
        # if camera not open, try to open it
        if not self._video:
            if not self._open:
                self._open, self.cap = open_camera(self._camera_type, self._is_init, self._date)
                if not self._is_init and self._open:
                    self._is_init = True
        else:
            if not self._open:
                self.cap = cv2.VideoCapture(self._video_path)
                self._open = True
                if not self._is_init and self._open:
                    self._is_init = True

    def is_open(self):
        '''
        check the camera opening state
        '''
        return self._open

    def read(self):
        if self._open:
            r, frame = self.cap.read()
            if not r:
                self.cap.release()  # release the failed camera
                self._open = False
            return r, frame
        else:
            return False, None

    def release(self):
        if self._open:
            self.cap.release()
            self._open = False

    def __del__(self):
        if self._open:
            self.cap.release()
            self._open = False

def read_yaml(camera_type):
    '''
    读取相机标定参数,包含外参，内参，以及关于雷达的外参

    :param camera_type:相机编号
    :return: 读取成功失败标志位，相机内参，畸变系数，和雷达外参，相机图像大小
    '''
    yaml_path = "{0}/camera{1}.yaml".format(CAMERA_YAML_PATH,
                                            camera_type)
    try:
        with open(yaml_path, 'rb')as f:
            res = yaml.load(f, Loader=yaml.FullLoader)
            K_0 = np.float32(res["K_0"]).reshape(3, 3)
            C_0 = np.float32(res["C_0"])
            E_0 = np.float32(res["E_0"]).reshape(4, 4)
            imgsz = tuple(res['ImageSize'])

        return True, K_0, C_0, E_0, imgsz
    except Exception as e:
        print("[ERROR] {0}".format(e))
        return False, None, None, None, None

class HT_Camera:
    def __init__(self, camera_type=0, is_init=True, path=None):
        '''
        相机驱动类

        :param camera_type:相机编号
        :param is_init: 相机是否已经启动过一次，若是则使用path所指向的参数文件
        :param path: 初次启动保存的参数文件路径名称（无需后缀，实际使用时即为创建时间）
        '''
        # 枚举相机
        DevList = mvsdk.CameraEnumerateDevice()
        # 得到存在相机序列号
        existing_camera_name = [dev.GetSn() for dev in DevList]

        if not camera_match_list[camera_type] in existing_camera_name:
            # 所求相机不存在
            self.hCamera = -1
            return

        camera_no = existing_camera_name.index(camera_match_list[camera_type])  # 所求相机在枚举列表中编号
        DevInfo = DevList[camera_no]
        print("{} {}".format(DevInfo.GetFriendlyName(), DevInfo.GetPortType()))
        print(DevInfo)

        self.camera_type = camera_type

        # 打开相机
        try:
            self.hCamera = mvsdk.CameraInit(DevInfo, -1, -1)
        except mvsdk.CameraException as e:
            self.hCamera = -1
            print("CameraInit Failed({}): {}".format(e.error_code, e.message))
            return

        # 获取相机特性描述
        cap = mvsdk.CameraGetCapability(self.hCamera)

        # 判断是黑白相机还是彩色相机
        monoCamera = (cap.sIspCapacity.bMonoSensor != 0)

        # 黑白相机让ISP直接输出MONO数据，而不是扩展成R=G=B的24位灰度
        if monoCamera:
            mvsdk.CameraSetIspOutFormat(self.hCamera, mvsdk.CAMERA_MEDIA_TYPE_MONO8)
        else:
            mvsdk.CameraSetIspOutFormat(self.hCamera, mvsdk.CAMERA_MEDIA_TYPE_BGR8)

        # 相机模式切换成连续采集
        mvsdk.CameraSetTriggerMode(self.hCamera, 0)

        if not is_init:
            # default camera parameter config
            param_path = "{0}/camera_{1}.Config".format(CAMERA_CONFIG_DIR,
                                                        camera_type)

            mvsdk.CameraReadParameterFromFile(self.hCamera, param_path)
        else:
            # 初次启动后，保存的参数文件
            param_path = "{0}/camera_{1}_of_{2}.Config".format(CAMERA_CONFIG_SAVE_DIR,
                                                               camera_type, path)

            mvsdk.CameraReadParameterFromFile(self.hCamera, param_path)

        mvsdk.CameraSetAeState(self.hCamera, 0)

        print(f"[INFO] camera exposure time {mvsdk.CameraGetExposureTime(self.hCamera) / 1000:0.03f}ms")
        print(f"[INFO] camera gain {mvsdk.CameraGetAnalogGain(self.hCamera):0.03f}")

        # 让SDK内部取图线程开始工作
        mvsdk.CameraPlay(self.hCamera)

        # 计算RGB buffer所需的大小，这里直接按照相机的最大分辨率来分配
        FrameBufferSize = cap.sResolutionRange.iWidthMax * cap.sResolutionRange.iHeightMax * (1 if monoCamera else 3)

        # 分配RGB buffer，用来存放ISP输出的图像
        # 备注：从相机传输到PC端的是RAW数据，在PC端通过软件ISP转为RGB数据（如果是黑白相机就不需要转换格式，但是ISP还有其它处理，所以也需要分配这个buffer）
        self.pFrameBuffer = mvsdk.CameraAlignMalloc(FrameBufferSize, 16)

    def read(self):
        if self.hCamera == -1:
            return False, None
        try:
            pRawData, FrameHead = mvsdk.CameraGetImageBuffer(self.hCamera, 200)
            mvsdk.CameraImageProcess(self.hCamera, pRawData, self.pFrameBuffer, FrameHead)
            mvsdk.CameraReleaseImageBuffer(self.hCamera, pRawData)

            # 此时图片已经存储在pFrameBuffer中，对于彩色相机pFrameBuffer=RGB数据，黑白相机pFrameBuffer=8位灰度数据
            # 把pFrameBuffer转换成opencv的图像格式以进行后续算法处理
            frame_data = (mvsdk.c_ubyte * FrameHead.uBytes).from_address(self.pFrameBuffer)
            frame = np.frombuffer(frame_data, dtype=np.uint8)
            frame = frame.reshape(
                (FrameHead.iHeight, FrameHead.iWidth,
                 1 if FrameHead.uiMediaType == mvsdk.CAMERA_MEDIA_TYPE_MONO8 else 3))
            return True, frame
        except mvsdk.CameraException as e:
            print(e)
            return False, None

    def setExposureTime(self, ex=30):
        if self.hCamera == -1:
            return
        mvsdk.CameraSetExposureTime(self.hCamera, ex)

    def setGain(self, gain):
        if self.hCamera == -1:
            return
        mvsdk.CameraSetAnalogGain(self.hCamera, gain)

    def saveParam(self, path):

        if self.hCamera == -1:
            return
        param_path = "{0}/camera_{1}_of_{2}.Config".format(CAMERA_CONFIG_SAVE_DIR,
                                                           self.camera_type, path)
        mvsdk.CameraSaveParameterToFile(self.hCamera, param_path)

    def NoautoEx(self):
        '''
        设置不自动曝光
        '''
        if self.hCamera == -1:
            return
        mvsdk.CameraSetAeState(self.hCamera, 0)

    def getExposureTime(self):
        if self.hCamera == -1:
            return -1
        return int(mvsdk.CameraGetExposureTime(self.hCamera) / 1000)

    def getAnalogGain(self):
        if self.hCamera == -1:
            return -1
        return int(mvsdk.CameraGetAnalogGain(self.hCamera))

    def release(self):
        if self.hCamera == -1:
            return
        # 关闭相机
        mvsdk.CameraUnInit(self.hCamera)
        # 释放帧缓存
        mvsdk.CameraAlignFree(self.pFrameBuffer)

def tune_exposure(cap: HT_Camera, high_reso=False):
    '''
    :param cap: camera target
    :param high_reso: 采用微秒/毫秒为单位调整曝光时间
    '''
    cv2.namedWindow("exposure press q to exit", cv2.WINDOW_NORMAL)
    cv2.resizeWindow("exposure press q to exit", 1280, 960)
    cv2.moveWindow("exposure press q to exit", 300, 300)
    cv2.setWindowProperty("exposure press q to exit", cv2.WND_PROP_TOPMOST, 1)
    if high_reso:
        cv2.createTrackbar("ex", "exposure press q to exit", 0, 1, lambda x: None)
        cv2.setTrackbarMax("ex", "exposure press q to exit", 30000)
        cv2.setTrackbarMin("ex", "exposure press q to exit", 0)
        cv2.setTrackbarPos("ex", "exposure press q to exit", int(cap.getExposureTime() * 1000))
        # 模拟增益区间为0到256
        cv2.createTrackbar("g1", "exposure press q to exit", 0, 1, lambda x: None)
        cv2.setTrackbarMax("g1", "exposure press q to exit", 256)
        cv2.setTrackbarMin("g1", "exposure press q to exit", 0)
        cv2.setTrackbarPos("g1", "exposure press q to exit", int(cap.getAnalogGain()))
    else:
        cv2.createTrackbar("ex", "exposure press q to exit", 0, 1, lambda x: None)
        cv2.setTrackbarMax("ex", "exposure press q to exit", 120)
        cv2.setTrackbarMin("ex", "exposure press q to exit", 0)
        cv2.setTrackbarPos("ex", "exposure press q to exit", int(cap.getExposureTime()))
        cv2.createTrackbar("g1", "exposure press q to exit", 0, 1, lambda x: None)
        cv2.setTrackbarMax("g1", "exposure press q to exit", 256)
        cv2.setTrackbarMin("g1", "exposure press q to exit", 0)
        cv2.setTrackbarPos("g1", "exposure press q to exit", int(cap.getAnalogGain()))

    flag, frame = cap.read()

    while (flag and cv2.waitKey(1) != ord('q') & 0xFF):
        if high_reso:
            cap.setExposureTime(cv2.getTrackbarPos("ex", "exposure press q to exit"))
        else:
            cap.setExposureTime(cv2.getTrackbarPos("ex", "exposure press q to exit") * 1000)
        cap.setGain(cv2.getTrackbarPos("g1", "exposure press q to exit"))

        cv2.imshow("exposure press q to exit", frame)
        flag, frame = cap.read()

    ex = cv2.getTrackbarPos("ex", "exposure press q to exit")
    g1 = cv2.getTrackbarPos("g1", "exposure press q to exit")
    if high_reso:
        ex = ex / 1000
    print(f"finish set exposure time {ex:.03f}ms")
    print(f"finish set analog gain {g1}")
    cv2.destroyWindow("exposure press q to exit")

if __name__ == '__main__':
    # demo to show the Camera_Thread class usage
    import cv2

    # initialize
    cap = Camera_Thread(0)

    cv2.namedWindow("out", cv2.WINDOW_NORMAL)

    k = cv2.waitKey(1)
    while True:
        # check whether the camera is working, if not, try to reopen it
        if not cap.is_open():
            cap.open()

        # receive one frame
        flag, frame = cap.read()

        if not flag:
            time.sleep(0.1)
            continue

        cv2.imshow("out", frame)

        k = cv2.waitKey(1)
        if k & 0xff == ord('q'):
            break

    cap.release()
    cv2.destroyAllWindows()

ext: py

reason: import numpy


text:
from copy import deepcopy
from itertools import chain
from operator import contains
from pathlib import Path

from frictionless import Resource
from glom import glom, Iter, T
from glom.matching import MatchError
import numpy as np  # noqa: F401
import pandas as pd
import pytest

from friendly_data.converters import _schema, to_df
from friendly_data.dpkg import _ensure_posix
from friendly_data.dpkg import create_pkg, resource_, set_idxcols
from friendly_data.dpkg import entry_from_res
from friendly_data.dpkg import fullpath
from friendly_data.dpkg import index_levels
from friendly_data.dpkg import idxpath_from_pkgpath
from friendly_data.dpkg import pkg_from_files
from friendly_data.dpkg import pkg_from_index
from friendly_data.dpkg import read_pkg
from friendly_data.dpkg import res_from_entry
from friendly_data.dpkg import pkgindex
from friendly_data.dpkg import write_pkg
from friendly_data.helpers import match, noop_map, is_windows
from friendly_data.io import dwim_file, relpaths
import friendly_data_registry as registry

from .conftest import expected_schema, escape_path, assert_log

@pytest.mark.skipif(not is_windows(), reason="only relevant for windows")
def test_ensure_posix(pkg_meta):
    pkgdir = Path("testing/files/mini-ex")
    files = chain(pkgdir.glob("inputs/*"), pkgdir.glob("outputs/*"))
    pkg = create_pkg(pkg_meta, relpaths(pkgdir, files), pkgdir)
    # NOTE: count windows path separators in the resource path, should be 0 as the
    # spec requires resource paths to be POSIX paths
    npathsep = glom(
        pkg, ("resources", Iter().map("path").map(T.count("\\")).all(), sum)
    )
    assert npathsep == 0

    # check inline data resources are skipped
    for i, res in enumerate(pkg.resources):
        if "output" in res["path"]:
            continue
        pkg.resources[i]["data"] = to_df(res).to_dict("records")
        pkg.resources[i]["format"] = "json"
        pkg.resources[i].pop("path")
        pkg.resources[i].pop("mediatype", None)
        pkg.resources[i]["scheme"] = ""
    assert _ensure_posix(pkg)

@pytest.mark.parametrize(
    "update",
    [
        {},
        {
            "timestep": {"name": "timestep", "type": "string"},
            "capacity_factor": {"name": "capacity_factor", "type": "float"},
        },
    ],
)
def test_resource_infer(update):
    pkgdir = Path("testing/files/mini-ex")
    spec = {"path": "outputs/capacity_factor.csv"}
    if update:
        spec["schema"] = {"fields": update}

    col_types = glom(
        resource_(spec, pkgdir).schema.fields,
        ([({1: "name", 2: "type"}, T.values(), tuple)], dict),
    )
    expected = {
        "carrier": "string",
        "region": "string",
        "technology": "string",
        "timestep": "datetime",
        "capacity_factor": "number",
    }
    expected.update((v["name"], v["type"]) for v in update.values())
    assert col_types == expected

def test_resource_err():
    with pytest.raises(ValueError, match="Incomplete resource.+\n.+'path' is missing"):
        resource_({})

@pytest.mark.parametrize(
    "basepath,path,opts,ncols",
    [
        ("testing/files/skip_test", "commented_dst.csv", {"skip": 1}, 4),
        ("testing/files/xls_sheet_test", "sheet_2.xlsx", {"sheet": 2}, 5),
    ],
)
def test_resource_opts(basepath, path, opts, ncols):
    spec = {"path": path, **opts}
    res = resource_(spec, basepath)
    assert len(res.schema.fields) == ncols

def test_pkg_creation():
    pkgdir = Path("testing/files/random")
    pkg_meta = {"name": "test", "licenses": "CC0-1.0"}
    pkg = create_pkg(pkg_meta, relpaths(pkgdir, "data/sample-ok-?.csv"), pkgdir)
    for resource in pkg["resources"]:
        # match datapackage field type for datetime
        ts_cols = [
            field.name
            for field in resource.schema.fields
            if "datetime" in field["type"]
        ]
        expected = expected_schema(resource)
        expected.update((col, "datetime") for col in ts_cols)
        assert _schema(resource, noop_map()) == expected

def test_pkg_creation_skip_rows(pkg_meta):
    resources = [{"path": "commented_dst.csv", "skip": 1}]
    pkg = create_pkg(pkg_meta, resources, basepath="testing/files/skip_test")
    expected = ["timestep", "UK", "Ireland", "France"]
    assert glom(pkg, ("resources.0.schema.fields", Iter("name").all())) == expected

def test_pkg_read():
    pkgdir = Path("testing/files/random")
    dpkg_json = pkgdir / "datapackage.json"
    pkg = read_pkg(dpkg_json)
    assert all(fullpath(res).exists() for res in pkg["resources"])

def test_zippkg_read(rnd_pkg, tmp_path_factory):
    with tmp_path_factory.mktemp("ziptest-") as tmpdir:
        zipfile = tmpdir / "testpackage.zip"
        rnd_pkg.to_zip(f"{zipfile}")

        # unzip to current dir
        _ = read_pkg(zipfile)
        assert (zipfile.parent / "datapackage.json").exists()

        # unzip to different dir
        _ = read_pkg(zipfile, extract_dir=zipfile.parent / "foo")
        assert (zipfile.parent / "foo/datapackage.json").exists()

def test_pkg_read_error(tmp_pkgdir):
    # unsupported archive: tarball
    tarball = Path("/path/to/package.tar")
    with pytest.raises(ValueError, match=f".*{tarball.name}:.+"):
        read_pkg(tarball)

    _, dest = tmp_pkgdir
    pkg_json = dest / "datapackage.json"
    pkg_json.unlink()
    # NOTE: escape for Windows paths
    path = escape_path(pkg_json) if is_windows() else pkg_json
    with pytest.raises(FileNotFoundError, match=f"{path}: not found"):
        read_pkg(dest)
    with pytest.raises(FileNotFoundError, match=f"{path}: not found"):
        read_pkg(pkg_json)

@pytest.mark.parametrize("ext", [".yaml", ".yml", ".json"])
def test_pkgindex(ext):
    fpath = Path(f"testing/files/indices/index{ext}")
    idx = pkgindex.from_file(fpath)
    assert isinstance(idx, list)
    assert len(idx) == 3
    assert sum(map(len, idx.records(["path"]))) == len(idx)
    assert sum(map(len, idx.records(["path", "idxcols"]))) == len(idx) * 2
    assert sum(map(len, idx.records(["path", "idxcols", "skip"]))) == len(idx) * 3

    with pytest.raises(MatchError):
        idx.records(["path", "indexcols"])  # bad key: indexcols -> idxcols

def test_pkgindex_bad_file(caplog):
    fpath = Path("testing/files/indices/badindex.yaml")
    with pytest.raises(MatchError):
        # a bad key in the index file will trigger this error; aliases -> alias
        pkgindex.from_file(fpath)

    assert_log(caplog, "aliases: bad key in index file", "ERROR")

def test_pkgindex_errors(tmp_path):
    idxfile = tmp_path / "index.yaml"
    idxfile.touch()
    with pytest.raises(ValueError, match=f".*{idxfile.name}: bad index file"):
        pkgindex.from_file(idxfile)

    idxfile = idxfile.with_suffix(".txt")
    with pytest.raises(RuntimeError, match=f".*{idxfile.name}:.+"):
        pkgindex.from_file(idxfile)

@pytest.mark.parametrize("is_df", [False, True])
@pytest.mark.parametrize(
    "csvfile, idxcols",
    [
        ("inputs/cost_energy_cap.csv", ["cost", "region", "technology"]),
        ("inputs/cost_energy_cap.csv", ["cost", "region"]),  # specified columns only
        ("inputs/energy_eff.csv", ["region", "technology"]),
        ("inputs/description.csv", ["technology"]),
        (
            "outputs/capacity_factor.csv",
            ["carrier", "region", "technology", "timestep"],
        ),
        # specified columns only
        ("outputs/capacity_factor.csv", ["carrier", "technology", "timestep"]),
        ("outputs/resource_area.csv", ["region", "technology"]),
    ],
)
def test_index_levels(is_df, csvfile, idxcols):
    csvfile = Path("testing/files/mini-ex") / csvfile
    df = pd.read_csv(csvfile, index_col=idxcols)
    _, coldict = index_levels(df if is_df else csvfile, idxcols)
    assert all(map(contains, idxcols, coldict))
    # NOTE: metadata for categorical index columns isn't set as categorical if
    # they are not in the registry
    all_idxcols = glom(registry.getall(), ("idxcols", ["name"]))
    lvl_counts = {
        k: len(v)
        for k, v in zip(
            df.index.names,
            df.index.levels
            if isinstance(df.index, pd.MultiIndex)
            else [df.index.unique()],
        )
        if "timestep" not in k and k in all_idxcols  # category columns only
    }
    catcols = glom(
        coldict.values(),
        (
            Iter(match({"name": str, "constraints": {"enum": list}, str: str}))
            .map({1: "name", 2: ("constraints.enum", len)})
            .map(T.values())
            .all(),
            dict,
        ),
    )
    assert catcols == lvl_counts  # check all levels are found
    # TODO: aliased columns

@pytest.mark.parametrize("is_df", [False, True])
@pytest.mark.parametrize(
    "idxcols",
    [
        # 5 cases, partial (region) and completely set enum (in registry),
        # string (loc_*) or int (spore) enum not set (not in registry), and
        # datetime types (timestep) FIXME: doesn't cover completely set enum
        ("loc_from", "loc_to"),  # string enum not set
        ("spore", "region"),  # int enum not set, + partial
        ("spore", "loc_from", "loc_to"),  # int & string enum not set
        ("timestep", "spore", "loc_from", "loc_to"),  # datetime & enum not set
        ("timestep", "region", "spore", "loc_from", "loc_to"),  # all + partial
    ],
)
def test_index_levels2(is_df, idxcols):
    csvfile = Path("testing/files/index_levels/transmission_flows.csv")
    file_or_df = pd.read_csv(csvfile, index_col=idxcols) if is_df else csvfile
    file_or_df, coldict = index_levels(file_or_df, idxcols)
    assert all(col in coldict for col in idxcols)
    for col in idxcols:
        if col == "timestep":
            continue
        if registry.get(col, "idxcols"):
            nvals = len(file_or_df.index.get_level_values(col).unique())
            assert glom(coldict, (f"{col}.constraints.enum", len)) == nvals
        else:
            # when not in the registry, metadata will always be incomplete
            pass

def test_set_idxcols():
    pkgdir = Path("testing/files/iamc")
    res = set_idxcols("nameplate_capacity.csv", pkgdir)
    # scenario, unit, year
    assert len(glom(res, "schema.primaryKey")) == 3

def test_res_from_entry():
    pkgdir = Path("testing/files/mini-ex")
    idxfile = pkgindex.from_file(pkgdir / "index.yaml")
    for entry in idxfile.records(["path", "idxcols", "alias"]):
        res = res_from_entry(entry, pkgdir)
        assert isinstance(res, Resource)
        assert glom(res, "schema.primaryKey") == entry["idxcols"]

def test_entry_from_res():
    pkgdir = Path("testing/files/alias_test")
    idx = dwim_file(pkgdir / "index.yaml")
    for entry in idx:
        expected = deepcopy(entry)
        res = res_from_entry(entry, pkgdir)
        assert entry_from_res(res) == expected

@pytest.mark.parametrize("idx_t", [".yaml", ".json"])
def test_pkg_from_index(idx_t, pkg_meta):
    idxpath = Path("testing/files/mini-ex/index").with_suffix(idx_t)
    pkgdir, pkg, _ = pkg_from_index(pkg_meta, idxpath)
    assert pkgdir == idxpath.parent
    assert len(pkg["resources"]) == 5  # number of datasets
    indices = glom(pkg, ("resources", Iter().map("schema.primaryKey").all()))
    assert len(indices) == 5
    # FIXME: not sure what else to check

def test_pkg_from_index_skip_rows(pkg_meta):
    _, pkg, _ = pkg_from_index(pkg_meta, "testing/files/skip_test/index.yaml")
    expected = ["timestep", "UK", "Ireland", "France"]
    assert glom(pkg, ("resources.0.schema.fields", Iter("name").all())) == expected

def test_pkg_from_index_aliased_cols(pkg_w_alias):
    ref = registry.get("region", "idxcols")
    col_schema, *_ = glom(
        pkg_w_alias,
        (
            "resources.0.schema.fields",
            Iter(match({"alias": str, str: object})).all(),
        ),
    )
    assert col_schema.pop("name") == "node"
    col_schema["name"] = col_schema.pop("alias")
    col_schema["constraints"]["enum"] = []  # registry doesn't include levels
    assert col_schema == ref

def test_pkg_from_files(pkg_meta, caplog):
    pkgdir = Path("testing/files/mini-ex")
    files = list(chain(pkgdir.glob("inputs/*"), pkgdir.glob("outputs/*")))
    # with index file
    _, pkg, idx = pkg_from_files(pkg_meta, pkgdir / "index.yaml", files)
    # files not in index: inheritance.csv, and loc_coordinates.csv
    assert len(pkg["resources"]) - len(idx) == 2

    # with directory containing index file
    _pkgdir, pkg, idx = pkg_from_files(pkg_meta, pkgdir, files)
    assert _pkgdir == pkgdir
    assert len(pkg["resources"]) - len(idx) == 2
    assert_log(caplog, "multiple indices:", "WARNING")

def test_pkg_from_files_no_index(pkg_meta, caplog):
    pkgdir = Path("testing/files/random")
    _, pkg, idx = pkg_from_files(pkg_meta, pkgdir, pkgdir.glob("data/*"))
    assert idx is None
    assert len(pkg["resources"]) == 3
    assert_log(caplog, "no index file", "WARNING")

def test_idxpath_from_pkgpath(tmp_path, caplog):
    idxpath = tmp_path / "index.json"
    assert idxpath_from_pkgpath(tmp_path) == ""
    assert_log(caplog, f"{tmp_path.name}: no index file found", "WARNING")

    idxpath.touch()
    assert idxpath_from_pkgpath(tmp_path) == idxpath

    idxpath.with_suffix(".yaml").touch()
    idxpath.with_suffix(".yml").touch()
    # NOTE: returns the lexicographically first match
    assert idxpath_from_pkgpath(tmp_path) == idxpath.with_suffix(".json")
    assert_log(caplog, "multiple indices:", "WARNING")

def test_write_pkg(pkg, tmp_path):
    res = write_pkg(pkg, tmp_path)
    assert len(res) == 1
    assert res[0].exists()

    assert not (tmp_path / "index.yaml").exists()

def test_write_pkg_idx(pkg, tmp_path):
    idx = pkgindex.from_file(f"{pkg.basepath}/index.yaml")

    res = write_pkg(pkg, tmp_path, idx=idx)
    assert len(res) == 2
    assert all([p.exists() for p in res])

    with pytest.raises(TypeError):
        write_pkg(pkg, tmp_path, idx)

@pytest.mark.skip(reason="not implemented")
def test_write_pkg_archive(pkg, tmp_path_factory):
    with tmp_path_factory.mktemp("pkgwrite-") as tmpdir:
        zipfile = tmpdir / "testpkg.zip"

        # save pkg as zip
        write_pkg(pkg, f"{zipfile}")
        assert zipfile.exists()

        # unsupported archive: tarball
        tarball = tmpdir / "testpkg.tar"
        with pytest.raises(ValueError, match=f".*{tarball.name}:.+"):
            write_pkg(pkg, f"{tarball}")

max_stars_repo_name: sentinel-energy/sentinel-achive

max_issues_repo_name: sentinel-energy/sentinel-achive

max_forks_repo_name: sentinel-energy/sentinel-achive


text:
import pysam,sys,os,numpy
from scipy.signal import find_peaks_cwt
import argparse
def get_max_distance(l):
    mi=min(l)
    ma=max(l)
    return ma-mi

def is_peak_in_range(peaks,a,b):
    if len(numpy.where(numpy.logical_and(peaks>a,peaks<b))[0]) >= 1:
        return "PRESENT"
    else:
        return "ABSENT"

def get_sum_in_range(freq,a,b):
    sum=0
    for i in range(a,b,1):
        sum+=freq[i]
    return sum

parser = argparse.ArgumentParser(description='calculate normalized fragment length distribution from PE bam file')
parser.add_argument('-i',dest='inBam',required=True,help='Input Bam File')
parser.add_argument('-o',dest='out',required=True,help='Output tab-delimited File')
args = parser.parse_args()
if not os.path.exists(args.inBam+".bai"):
    pysam.index(args.inBam)
samfile = pysam.AlignmentFile(args.inBam, "rb")
refcoordsperread=dict()
for read in samfile.fetch():
    if read.is_unmapped:
        continue
    if read.is_supplementary:
        continue
    if read.is_secondary:
        continue
    if read.is_duplicate:
        continue
    if read.is_proper_pair:
        if not read.query_name in refcoordsperread:
            refcoordsperread[read.query_name]=list()
        refcoordsperread[read.query_name].append(int(read.reference_start))
        refcoordsperread[read.query_name].append(int(read.reference_end))
fraglens=list()
for k,v in refcoordsperread.items():
	fraglens.append(get_max_distance(refcoordsperread[k]))
o=open(args.out,'w')
s=len(fraglens)
freqs=dict()
for i in range(0,max(fraglens)+1,1):
    freqs[i]=0
for i in range(min(fraglens),max(fraglens)+1,1):
    c=fraglens.count(i)
    freqs[i]=c
    if c != 0:
        o.write("%d\t%.6f\n"%(i,c*1000.0/s))

# peaks=find_peaks_cwt(freqs.values(),numpy.array([25]))
# freq.values() may contain a string of zeros which can lead to the following error:
# Traceback (most recent call last):
#   File "/gpfs/gsfs11/users/kopardevn/GitRepos/CCBR_ATACseq/workflow/scripts/ccbr_atac_bam2FLD.py", line 60, in <module>
#     peaks=find_peaks_cwt(freqs.values(),numpy.array([25]))
#   File "/usr/local/Anaconda/envs/py3.7/lib/python3.7/site-packages/scipy/signal/_peak_finding.py", line 1291, in find_peaks_cwt
#     cwt_dat = cwt(vector, wavelet, widths, window_size=window_size)
#   File "/usr/local/Anaconda/envs/py3.7/lib/python3.7/site-packages/scipy/signal/wavelets.py", line 480, in cwt
#     output[ind] = convolve(data, wavelet_data, mode='same')
#   File "/usr/local/Anaconda/envs/py3.7/lib/python3.7/site-packages/scipy/signal/signaltools.py", line 1259, in convolve
#     raise ValueError("volume and kernel should have the same "
# ValueError: volume and kernel should have the same dimensionality
# Here is a solution:
peaks=find_peaks_cwt(list(map(lambda x:x+1,freqs.values())),numpy.array([25]))

nfr_peak=is_peak_in_range(peaks,20,90)
mono_nuc_peak=is_peak_in_range(peaks,120,250)
di_nuc_peak=is_peak_in_range(peaks,300,500)

nfr_reads=get_sum_in_range(freqs,0,150)
mono_nuc_reads=get_sum_in_range(freqs,150,300)
non_nfr_reads=sum(freqs.values())-nfr_reads
nfr_to_mono_nuc_ratio=nfr_reads*1.0/mono_nuc_reads
nfr_to_other_ratio=nfr_reads*1.0/non_nfr_reads

o.write("# PEAKS : %s\n"%(peaks))
o.write("# NUCLEOSOME_FREE_PEAK_PRESENT : %s\n"%(nfr_peak))
o.write("# MONONUCLEOSOME_PEAK_PRESENT : %s\n"%(mono_nuc_peak))
o.write("# DINUCLEOSOME_PEAK_PRESENT : %s\n"%(di_nuc_peak))
o.write("# NUCLEOSOME_FREE_READ_FRACTION : %0.3f\n"%(nfr_reads*1.0/s))
o.write("# MONONUCLEOSOME_READ_FRACTION : %0.3f\n"%(mono_nuc_reads*1.0/s))
o.write("# NFR_TO_MONONUCLEOSOME_READS_RATIO : %0.3f\n"%(nfr_to_mono_nuc_ratio))
o.write("# NFR_TO_NON_NFR_READS_RATIO : %0.3f\n"%(nfr_to_other_ratio))

o.close()

reason: from scipy


text:
import cv2
import mediapipe as mp
import time

import numpy as np

class HandDetector():
    def __init__(self, mode = False, maxHands = 2, detectionCon = 0.5, trackCon = 0.5):
        landmark_list = None
        self.mode = mode
        self.maxHands = maxHands
        self.detectionCon = detectionCon
        self.trackCon = trackCon

        self.mpHands = mp.solutions.hands
        self.hands = self.mpHands.Hands(self.mode, self.maxHands, model_complexity = 1,
                                        min_detection_confidence = self.detectionCon,
                                        min_tracking_confidence = self.trackCon)
        self.mpDraw = mp.solutions.drawing_utils

    def find_hands(self, img, draw = True):
        imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        self.result = self.hands.process(imgRGB)
        if self.result.multi_hand_landmarks:
            for handLms in self.result.multi_hand_landmarks:
                if draw:
                    self.mpDraw.draw_landmarks(img, handLms,
                                               self.mpHands.HAND_CONNECTIONS)
        return img

    def find_position(self, img, handNo = 0, draw = True):

        self.landmark_List = []
        if self.result.multi_hand_landmarks:
            single_Hand = self.result.multi_hand_landmarks[handNo]
            for id, lm in enumerate(single_Hand.landmark):
                h, w, c = img.shape
                cx, cy = int(lm.x * w), int(lm.y * h)
                self.landmark_List.append([id, cx, cy])
                if draw:
                    self.mpDraw.draw_landmarks(img, single_Hand,
                                               self.mpHands.HAND_CONNECTIONS)

        return self.landmark_List

    def finger_up(self, landmark_list):

        self.fingers = [(landmark_list[8][2], landmark_list[6][2], 0),
                   (landmark_list[12][2], landmark_list[10][2], 1),
                   (landmark_list[16][2], landmark_list[14][2], 2),
                   (landmark_list[20][2], landmark_list[18][2], 3),
                   (landmark_list[5][1], landmark_list[4][1], 4)]

        self.fingerup = [0, 0, 0, 0, 0]
        for finger in self.fingers:
            if finger[0] < finger[1]:
                self.fingerup[finger[2]] = 1
        return self.fingerup

    def distance_tips(self, index1, index2, landmark_list):
        return np.linalg.norm(np.array(landmark_list[index1][1:]) - np.array(landmark_list[index2][1:]))

    def distance(self, p1, p2):
        return np.linalg.norm(np.array(p1) - np.array(p2))

max_stars_repo_name: VDHARV/hand-tracking

max_issues_repo_name: VDHARV/hand-tracking

max_forks_repo_name: VDHARV/hand-tracking