Example #1
0
def makeplot(hitfreq,eventpower,rfi_found,coarsespec_freq,coarsepowers,specid=1,dolog='False', dotext=True, saveplot=''):
  """Plots both coarse power (for coarse spectrum) as a blue line 
  and event power (for hits) as red X's on the same figure. 

  hitfreq is a list of frequencies for hits in a spectrum, eventpower
  is a list of the eventpowers, coarsespec_freq is a list of
  frequencies for the coarse spectra bins, and coarsepowers
  is a list of the powers in each bin.   

  freq_type is either 'topo' or 'bary' (default is topo)

  specid is the value of the specid desired

  if dolog='True', the power on the y-axis will be logarithmic

  where is an optional string to further modify which hits are plotted.
  prefix all columns with an 'h' and a period, such as h.eventpower. do
  not include the phrase 'where' or the semicolon at the end. all other
  syntax rules apply.

  saveplot='' allows you to save the plot by inputting its name as a 
  string. if left blank, no plot is saved

  output is a figure."""
  
  import pylab, numpy, MySQLFunction, jd2gd, math

#  pylab.rc('text', usetex=True)
#  pylab.rc('font', family='serif') 

  #Get additional info for text header
  cmd = 'select beamnum, ra, decl, obstime, IF1_rfFreq, thrscale from config where specid=%d' %specid
  data = MySQLFunction.mysqlcommand(cmd)
  
  #Combine systime with obstime to get complete time
  obstime = data[0][3]

  #Create Gregorian date from obstime
  gd = jd2gd.caldate(obstime)
  dates = ['January','February','March','April','May','June','July',
  'August','September','October','November','December']
  gd = [str(gd[x]) for x in range(len(gd))]

  #Insert zeros to make formatting nice
  if float(gd[2])<10:
    gd[2] = '0' + gd[2]
  if float(gd[3])<10:
    gd[3] = '0' + gd[3]
  if float(gd[4])<10:
    gd[4] = '0' + gd[4]
  if float(gd[5])<10:
    gd[5] = '0' + gd[5]

  #Compile one date string
  date = gd[0] + ' ' + dates[int(gd[1])-1] + ' ' + gd[2] + ' ' + gd[3] + ':' + gd[4] + ':' + gd[5][:2]

  #Calculate center frequency
  rfFreq=int(data[0][4])
  rfFreq = rfFreq/1000000
  rfFreq = rfFreq-50
  cfreq = rfFreq + 100

  #Determine beam and polarization
  beam = int(data[0][0])
  beamnum = str(math.floor(float(beam)/2))[0]
  frac = float(beam)/2
  if math.modf(frac)[0]==0.0:
    newbeam = beamnum + 'a'
  else: newbeam = beamnum + 'b'

  #thrscale
  thrscale=float(data[0][5])/4.0

  #PLOTTING
  #Initialize figure
  fig=pylab.figure(figsize=(12,7))
  ax1 = fig.add_axes([0.1, 0.12, 0.85, 0.75])

  #Divide into RFI flagged or not
  ginds=numpy.where(rfi_found==0)[0]
  rinds=numpy.where(rfi_found>0)[0]  
  coarsepowers/=thrscale

  #Log or not?
  if dolog=='True':
    pylab.semilogy(coarsespec_freq/1000000,coarsepowers,'k')
    pylab.semilogy(hitfreq[rinds]/1000000,eventpower[rinds],'rx')
    pylab.semilogy(hitfreq[ginds]/1000000,eventpower[ginds],'bx')
  elif dolog=='False':
    pylab.plot(coarsespec_freq/1000000,coarsepowers,'k')
    pylab.plot(hitfreq[rinds]/1000000,eventpower[rinds],'rx')
    pylab.plot(hitfreq[ginds]/1000000,eventpower[ginds],'bx')
 
  #Add text to figure
  if dotext:
    pylab.figtext(0.1,.97,'Beam: %s' % newbeam)
    pylab.figtext(0.3,.97,'RA: %s' %data[0][1])
    pylab.figtext(0.5,.97,'Dec: %s' %data[0][2])
    pylab.figtext(0.95,.97,'Date: %s' %date, ha='right')
    pylab.figtext(0.1,.92,'Hit Count: %s' %len(eventpower))
    pylab.figtext(0.95,.92,'Center Freq: %s MHz' %cfreq, ha='right')
  
  #Set x-scale
  lowbound = cfreq - 100
  uppbound = cfreq + 100
  xticks = numpy.linspace(lowbound,uppbound,21)
  pylab.xticks(xticks)

  #Rotate x-labels
  for i in ax1.get_xticklabels(): i.set_rotation(45)

  #Set labels and title
  pylab.xlabel('Frequency (MHz)', size=14)
  pylab.ylabel('Power (arb units)', size=14)
#  pylab.title(' Coarse Spectrum and Hits for Specid=%d' %specid)
  
  #Set axis limits
  if len(eventpower) != 0:
    v = [lowbound,uppbound,min(coarsepowers),max(eventpower)+1000]
  else: v=[lowbound,uppbound,min(coarsepowers),max(coarsepowers)]
  pylab.axis(v)

  #Add grid
  pylab.grid(True)
  
  #Save plot?
  if saveplot != '':
    pylab.savefig('%s' %saveplot)

  return fig
Example #2
0
def makeplot(eventpower,freq,time,errbar=[0], where='',freqtype='topo',vlim=(-1,-1),frac=0.9,saveplot=''):
  """Produces a 'confetti plot' of spectra dynamically. 

  freqtype is the frequency type, either 'binnum', 'bary', or 
  'topo'.

  Input:
  where is a string to include additional information to 
  narrow the results. typically it will be used to specify a 
  range of specids. each column name MUST be prefixed with 
  the first letter of the table name and a period, like 
  c.obstime. don't forget to include 's.specid=c.specid if 
  referencing config and spec. do not include the word 'where' 
  at the beginning or the semicolon at the end. all other common 
  mysql syntax rules apply. Ex: 's.specid>1 and s.specid<=20 and 
  c.beamnum!=8 and c.specid=s.specid'. don't include hit table.

  vlim is a tuple of (vmin,vmax). Values are used in conjuction
  with norm to normalize luminance data.

  frac is the fraction of hits, sorted by eventpower, before which
  the size is 0.1 and after which the size is 1

  saveplot='' allows you to save the plot by inputting its name 
  as a string. if left blank, no plot is saved

  Output:
  Figure instance
  """
  import pylab, numpy, MySQLFunction, command, jd2gd, math

  # initialize figure
  fig=pylab.figure(figsize=(12,7))
  ax1 = fig.add_axes([0.1, 0.14, 0.85, 0.75])

  #If no plot limits specified,
  if vlim==(-1,-1):
    vlim=(numpy.min(eventpower), numpy.max(eventpower))

  # create array of point sizes
  sorted = numpy.sort(eventpower)
  index = math.floor(frac*len(eventpower))
  cutoff = eventpower[index]
  size = [.1 if eventpower[x]<cutoff else 1 for x in xrange(len(eventpower))]

  # plot data
  if len(errbar)>1:
    size=20
    pylab.errorbar(time,freq,xerr=errbar,fmt=None,ecolor='k', capsize=0.0)
  pylab.scatter(time,freq,s=size,c=eventpower,edgecolors='none',vmin=vlim[0],vmax=vlim[1])


  # add grid
  pylab.grid(True,which='both')

  # add labels
  ax1.set_xlabel('Time (seconds)')
  ax1.set_title('Dynamic Spectra - Hits')

  if freqtype=='binnum':
    ax1.set_ylabel('Frequency channel')
  elif freqtype=='topo':
    ax1.set_ylabel('Topocentric Frequency (MHz)')
  elif freqtype=='bary':
    ax1.set_ylabel('Barycentric Frequency (MHz)')

  # gather additional info
  if where=='':
    cmd = command.generate('specid,obstime,AGC_Time,IF1_rfFreq','config')
  elif 'c.' not in where:
    where = where + ' and h.specid=c.specid'
    cmd = command.generate('h.specid,c.obstime,c.AGC_Time,c.IF1_rfFreq','hit h, config c',where=where)
  else:
    where = where + ' and h.specid=c.specid'
    cmd = command.generate('h.specid,c.obstime,c.AGC_Time,c.IF1_rfFreq','hit h, config c',where=where)

  data = MySQLFunction.mysqlcommand(cmd)
  
  # separate into arrays
  length = len(data)
  specid = [data[x][0] for x in xrange(length)]
  day = numpy.asarray([data[x][1] for x in xrange(length)])

  # get specid and hit count
  uniq_IDs = set(specid)
  speccount = len(uniq_IDs)
  hitcount = len(eventpower)

  # determine start and end dates
  start = min(day)
  end = max(day)

  # calculate the min and max RF from the center freq
  # scale y axis accordinly 
  rfctr=float(data[0][3])
  rflo=rfctr-50e6
  rfhi=rflo+200e6
  rflo-=5e6    #Add offsets to get hits away from plot edges
  rfhi+=5e6
  
  # guess whether data is given in Hz or MHz
  if numpy.log10(freq[0])<4:
    rflo/=1e6
    rfhi/=1e6

  # set axes limits
  v = [0,max(time),rflo,rfhi]
  pylab.axis(v)
  if min(freq)<rflo or max(freq)>rfhi: print "WARNING: There are hits outside freq limits"

  # create Gregorian date from obstime
  start = jd2gd.caldate(start)
  end = jd2gd.caldate(end)
  dates = ['January','February','March','April','May','June','July',
  'August','September','October','November','December']
  start = [str(start[x]) for x in range(len(start))]
  end = [str(end[x]) for x in range(len(end))]

  # insert zeros to make formatting nice
  if float(start[2])<10:
    start[2] = '0' + start[2]
  if float(start[3])<10:
    start[3] = '0' + start[3]
  if float(start[4])<10:
    start[4] = '0' + start[4]
  if float(start[5])<10:
    start[5] = '0' + start[5]
  if float(end[2])<10:
    end[2] = '0' + end[2]
  if float(end[3])<10:
    end[3] = '0' + end[3]
  if float(end[4])<10:
    end[4] = '0' + end[4]
  if float(end[5])<10:
    end[5] = '0' + end[5]

  # compile date strings
  date1 = start[0]+' '+dates[int(start[1])-1]+' '+start[2]+' '+start[3]+':'+start[4]+':'+start[5][:2]
  date2 = end[0]+' '+dates[int(end[1])-1]+' '+end[2]+' '+end[3]+':'+end[4]+':'+end[5][:2]

  # add text to figure
  pylab.figtext(0.4,.96,'Spectra Count: %s' %speccount)
  pylab.figtext(0.4,.935,'Hit Count: %s' %hitcount)
  pylab.figtext(0.61,.96,'Vmin,Vmax: %s %s' % (vlim[0], vlim[1]))
  pylab.figtext(0.61,.935,'ALFA RFctr: %4.1f' % (rfctr/1e6))
  pylab.figtext(0.8,.96,'Max Power: %s' %max(eventpower))
  pylab.figtext(0.8,.935,'Min Power: %s' %min(eventpower))
  pylab.figtext(0.1,.96,'Start: %s' %date1)
  pylab.figtext(0.1,.935,'End:   %s' %date2)

  # save plot?
  if saveplot != '':
    pylab.savefig('%s' %saveplot)

  return fig
def makeplot(bins,count,where='',dolog='False',cumulative='False',saveplot=''):
  """Creates histogram of hits per mean power bin.

  where is a string to include additional information to 
  narrow the results. typically it will be used to specify a 
  range of specids. each column name MUST be prefixed with 
  the first letter of the table name and a period, like 
  c.obstime. don't forget to include 'h.specid=c.specid if 
  referencing config and hit. do not include the word 'where' 
  at the beginning or the semicolon at the end. all other common 
  mysql syntax rules apply. Ex: 'h.specid>1 and h.specid<=20 and 
  c.beamnum!=8 and c.specid=h.specid'. 

  setting cumulative='True' makes a cumulative histogram as mean
  power increases

  setting dolog='True' makes the y-axis (count) logarithmic

  saveplot allows the user to have the figure saved by 
  inputting the file name. if left empty, no file will be saved

  returns a figure"""

  import pylab, MySQLFunction, command, jd2gd, math, numpy

  #Data to plot?
  if len(bins) != 0:

   #Initialize figure
   fig=pylab.figure(figsize=(12,7))
   ax1 = fig.add_axes([0.1, 0.14, 0.85, 0.75])

   #Determine bar width
   width = bins[1]-bins[0]

   #Logarithmic?
   if dolog != 'True':
     pylab.bar(bins[:-1],count,width=width)
     ylabel = 'Count'
     var = min(count)*0.5
   else:
     pylab.bar(bins[:-1],count,width=width,log='True')
     ylabel = 'Count (Logarithmic)'
     bottom = int(math.floor(numpy.log10(min(count)+1)))
     var = 10**(bottom)

   #Configure ticks
   pylab.xticks([0,50,100,150,200,250,300,350,400,450,500,500+width],['0','50','100','150','200','250','300','350','400','450','500','%d'%max(bins)])

   #Rotate xticks
   for i in ax1.get_xticklabels(): i.set_rotation(45)

   #Cumulative?
   if cumulative != 'True':  
     cumltv = ''
   else:  
     cumltv = '(Cumulative)'

   #Set axes limits
   v = [min(bins),500+width,var,max(count)*1.1]
   pylab.axis(v)

   #Add grid
   pylab.grid(True,which='both')

   #Add labels
   pylab.xlabel('Mean Power')
   pylab.ylabel('%s'%ylabel)
   pylab.title('Hit Count Per Mean Power Bin %s'%cumltv)

   #Get extra info for plot
   if where=='':
     cmd = command.generate('specid,obstime,AGC_Time','config')
   elif 'c.' not in where:
     where = where + ' and h.specid=c.specid'
   cmd = command.generate('c.specid,c.obstime','config c, hit h',where=where)

   data = MySQLFunction.mysqlcommand(cmd)
  
   #Separate into arrays
   length = len(data)
   specid = [data[x][0] for x in range(length)]
   time = numpy.asarray([data[x][1] for x in range(length)])

   #Get specid count
   uniq_IDs = set(specid)
   speccount = len(uniq_IDs)

   # determine start and end dates
   start = min(time)
   end = max(time)
 
   #Create Gregorian date from obstime
   start = jd2gd.caldate(start)
   end = jd2gd.caldate(end)
   dates = ['January','February','March','April','May','June','July',
  'August','September','October','November','December']
   start = [str(start[x]) for x in range(len(start))]
   end = [str(end[x]) for x in range(len(end))]

   #Insert zeros to make formatting nice
   if float(start[2])<10:
     start[2] = '0' + start[2]
   if float(start[3])<10:
     start[3] = '0' + start[3]
   if float(start[4])<10:
     start[4] = '0' + start[4]
   if float(start[5])<10:
     start[5] = '0' + start[5]
   if float(end[2])<10:
     end[2] = '0' + end[2]
   if float(end[3])<10:
     end[3] = '0' + end[3]
   if float(end[4])<10:
     end[4] = '0' + end[4]
   if float(end[5])<10:
     end[5] = '0' + end[5]

   #Compile date strings
   date1 = start[0]+' '+dates[int(start[1])-1]+' '+start[2]+' '+start[3]+':'+start[4]+':'+start[5][:2]
   date2 = end[0]+' '+dates[int(end[1])-1]+' '+end[2]+' '+end[3]+':'+end[4]+':'+end[5][:2]

   #Add text to figure
   pylab.figtext(0.1,.945,'SpecID Count: %s' %speccount)
   pylab.figtext(0.95,.97,'Start: %s' %date1, ha='right')
   pylab.figtext(0.95,.945,'End:   %s' %date2, ha='right')

   #Save figure?
   if saveplot!='':
     pylab.savefig('%s'%saveplot)

   return fig
Example #4
0
def makeplot(bins,count,freq_type='topo',increment_log=7,where='h.specid<100',dolog='False',saveplot=''):
  """Creates a histogram of hits at different
  frequencies, separated into bins by increments of 
  10^(increment_log).

  freq_type is either 'topo', 'bary', or 'binnum'. default is
  topo

  increment_log is the log of the spacing between bins. must 
  be an integer

  where is a string to include additional information to 
  narrow the results. typically it will be used to specify a 
  range of specids. each column name MUST be prefixed with 
  the first letter of the table name and a period, like 
  c.obstime. do not include the word 'where' at the beginning 
  or the semicolon at the end. all other common mysql syntax 
  rules apply. Ex: 'h.specid>1 and h.specid<=20 and 
  c.beamnum!=8'. by default it will grab only the first 99 
  specids.

  if dolog='True', the hit count is plotted on a log scale

  saveplot allows the user to have the plot saved by inputting
  the file name. if left empty, no file will be saved.

  returns a figure"""
  
  import pylab, numpy, math, MySQLFunction, command, jd2gd

  #Are there data to plot?
  if len(bins)!=0:

    #Initialize figure
    fig=pylab.figure(figsize=(12,7))
    ax1 = fig.add_axes([0.1, 0.14, 0.85, 0.75])

    #Configure x-ticks
    if freq_type!='binnum':
      lowbound = min(bins)/1000000
      uppbound = max(bins)/1000000
      xticks = numpy.arange(lowbound,uppbound+10**(increment_log-6),10)
      pylab.xticks(xticks)
    else: 
      lowbound = min(bins)
      uppbound = max(bins)
      xticks = numpy.arange(lowbound,uppbound+10**(increment_log),5000000)
      pylab.xticks(xticks)

    #Rotate xticks
    for i in ax1.get_xticklabels(): i.set_rotation(45)

    #Create xlabels
    if freq_type=='topo':
      xlabel = 'Topocentric Frequency (MHz)'
    elif freq_type=='bary':
      xlabel = 'Barycentric Frequency (MHz)'
    else: xlabel = 'Bin Number'

    #Determine bar width
    if freq_type != 'binnum':
      width = (10**increment_log)/1000000
    else: width = 10**increment_log

    #Make plots
    if dolog=='True' and freq_type!='binnum':
      pylab.bar(bins/1000000,count,log='True',width=width,align='center')
      ylabel = 'Count (Logarithmic)'
    elif dolog=='True':
      pylab.bar(bins,count,log='True',width=width,align='center')
      ylabel = 'Count (Logarithmic)'
    elif freq_type!='binnum':
      pylab.bar(bins/1000000,count,width=width,align='center')
      ylabel = 'Count'
    else:
      pylab.bar(bins,count,width=width,align='center')
      ylabel = 'Count'

    #Determine y-axis lower limit
    if dolog=='True':
      bottom = int(math.floor(numpy.log10(min(count)+1)))
      var = 10**bottom
    else:
      var = min(count)*0.5
     
    #Set axes limits
    if freq_type=='binnum':
      v = [0,uppbound+width/2,var,max(count)*1.1]
      pylab.axis(v)
    else:
      v=[lowbound-width/2,uppbound+width/2,var,max(count)*1.1]
      pylab.axis(v)
 
    #Add grid
    pylab.grid(True)
 
    #Add labels
    pylab.xlabel('%s'%xlabel)
    pylab.ylabel('%s'%ylabel)
    pylab.title('Event Count Per Bin')

    #Get extra info for plot
    if where=='':
      cmd = command.generate('specid,obstime,AGC_Time','config')
    elif 'c.' not in where:
      where = where + ' and h.specid=c.specid'
    cmd = command.generate('c.specid,c.obstime','config c, hit h',where=where)
  
    #Send command to mysql, return results
    data = MySQLFunction.mysqlcommand(cmd)

    #Separate into arrays
    length = len(data)
    specid = [data[x][0] for x in range(length)]
    time = numpy.asarray([data[x][1] for x in range(length)])

    #Get hit count and specid count
    uniq_IDs = set(specid)
    speccount = len(uniq_IDs)
    hitcount = sum(count)

    #Determine start and end dates
    start = min(time)
    end = max(time)
 
    #Create Gregorian date from obstime
    start = jd2gd.caldate(start)
    end = jd2gd.caldate(end)
    dates = ['January','February','March','April','May','June','July',
  'August','September','October','November','December']
    start = [str(start[x]) for x in range(len(start))]
    end = [str(end[x]) for x in range(len(end))]

    #Insert zeros to make formatting nice
    if float(start[2])<10:
      start[2] = '0' + start[2]
    if float(start[3])<10:
      start[3] = '0' + start[3]
    if float(start[4])<10:
      start[4] = '0' + start[4]
    if float(start[5])<10:
      start[5] = '0' + start[5]
    if float(end[2])<10:
      end[2] = '0' + end[2]
    if float(end[3])<10:
      end[3] = '0' + end[3]
    if float(end[4])<10:
      end[4] = '0' + end[4]
    if float(end[5])<10:
      end[5] = '0' + end[5]

    #Compile date strings
    date1 = start[0]+' '+dates[int(start[1])-1]+' '+start[2]+' '+start[3]+':'+start[4]+':'+start[5][:2]
    date2 = end[0]+' '+dates[int(end[1])-1]+' '+end[2]+' '+end[3]+':'+end[4]+':'+end[5][:2]

    #Create dictionary of bin resolutions
    if freq_type!='binnum':
      binres = {0: '1 Hz', 1: '10 Hz', 2: '100 Hz', 3: '1 KHz', 4: '10 KHz', 5: '100 KHz',
    6: '1 MHz', 7: '10 MHz', 8: '100MHz', 9: '1 GHz'}
    else:
      binres = {0: '1 Bin', 1: '10 Bins', 2: '100 Bins', 3: '1,000 Bins', 4: '10,000 Bins', 5: '100,000 Bins',
    6: '1,000,000 Bins', 7: '10,000,000 Bins', 8: '100,000,000 Bins'}

    #Add text to figure
    pylab.figtext(0.1,.97,'Hit Count: %s' %hitcount)
    pylab.figtext(0.1,.92,'Bin Resolution: %s' %binres[increment_log])
    pylab.figtext(0.1,.945,'SpecID Count: %s' %speccount)
    pylab.figtext(0.95,.97,' Start: %s' %date1, ha='right')
    pylab.figtext(0.95,.945,'End:   %s' %date2, ha='right')

    #Save figure?
    if saveplot!='':
      pylab.savefig('%s'%saveplot)

    return fig
def makeplot(xarr,yarr,data,where='',freqtype='binnum',vlim=(-1,-1), tslim=(-1,-1),saveplot=''):
   """Method to produce the three panel
   dynamic spectrum plot. The main panel
   is a pcolormesh figure of data with
   the mesh defined by xarr and yarr.
   The bottom panel shows a time series of
   the 2D data, and the right panel shows
   the average bandpass

   freqtype is the frequency type for the pcolormesh, either 
   'binnum' or 'topo'.

   Input:
   where is a string to include additional information to 
   narrow the results. typically it will be used to specify a 
   range of specids. each column name MUST be prefixed with 
   the first letter of the table name and a period, like 
   c.obstime. don't forget to include 's.specid=c.specid if 
   referencing config and spec. do not include the word 'where' 
   at the beginning or the semicolon at the end. all other common 
   mysql syntax rules apply. Ex: 's.specid>1 and s.specid<=20 and 
   c.beamnum!=8 and c.specid=s.specid'. don't include hit table.

   saveplot='' allows you to save the plot by inputting its name 
   as a string. if left blank, no plot is saved

   Output:
   Figure instance
   """
   import numpy, pylab, jd2gd, MySQLFunction, command

   #Calculate the time series and average bandpass
   #  for the subpanel plots
   tseries=numpy.mean(data, axis=0)
   bandpass=numpy.mean(data, axis=1)

   #If no plot limits specified,
   if vlim==(-1,-1):
       vlim=(numpy.min(data), numpy.max(data))
   if tslim==(-1,-1):
       tslim=(numpy.min(tseries), numpy.max(tseries))

   #Create figure instance, add axes and turn off labels
   fig=pylab.figure(figsize=(12,7))
   ax1 = fig.add_axes([0.1, 0.3, 0.6, 0.6])
   ax2 = fig.add_axes([0.1, 0.1, 0.6, 0.2], sharex=ax1)
   ax3 = fig.add_axes([0.7, 0.3, 0.2, 0.6], sharey=ax1)

   for i in ax3.get_yticklabels(): i.set_visible(False)
   for i in ax3.get_xticklabels(): i.set_rotation(270)
   for i in ax1.get_xticklabels(): i.set_visible(False)

   #Generate 2D mesh
   T,F=numpy.meshgrid(xarr,yarr)

   #Add plots
   ax1.pcolormesh(T,F,data, vmin=vlim[0], vmax=vlim[1])
   ax2.plot(xarr, tseries, 'r.')
   ax3.step(bandpass, yarr, 'g-')

   #Set axes labels
   ax2.set_xlabel('Time (Seconds)')

   if freqtype=='binnum':
     ax1.set_ylabel('Frequency channel')
   elif freqtype=='topo':
     ax1.set_ylabel('Frequency (MHz)')

   ax1.set_title('Dynamic Spectra - Coarse Bins')
   ax2.set_ylabel('Mean Intensity')
   ax1.set_xlim((min(xarr), max(xarr)))
   ax1.set_ylim((min(yarr), max(yarr)))
   ax2.set_ylim((tslim[0], tslim[1]))

   #Gather additional info
   if where=='':
     cmd = command.generate('specid,obstime,AGC_Time','config')
   elif 'c.' not in where:
     where = where + ' and s.specid=c.specid'
     cmd = command.generate('s.specid,c.obstime,c.AGC_Time','config c, spec s',where=where)

   data = MySQLFunction.mysqlcommand(cmd)
  
   #Separate into arrays
   length = len(data)
   specid = [data[x][0] for x in range(length)]
   day = numpy.asarray([data[x][1] for x in range(length)])
   fracday = numpy.asarray([float(data[x][2])/86400000 for x in range(length)])
   time = day + fracday  

   #Get specid count
   uniq_IDs = set(specid)
   speccount = len(uniq_IDs)

   #Determine start and end dates
   start = min(time)
   end = max(time)
 
   #Create Gregorian date from obstime
   start = jd2gd.caldate(start)
   end = jd2gd.caldate(end)
   dates = ['January','February','March','April','May','June','July',
  'August','September','October','November','December']
   start = [str(start[x]) for x in range(len(start))]
   end = [str(end[x]) for x in range(len(end))]

   #Insert zeros to make formatting nice
   if float(start[2])<10:
     start[2] = '0' + start[2]
   if float(start[3])<10:
     start[3] = '0' + start[3]
   if float(start[4])<10:
     start[4] = '0' + start[4]
   if float(start[5])<10:
     start[5] = '0' + start[5]
   if float(end[2])<10:
     end[2] = '0' + end[2]
   if float(end[3])<10:
     end[3] = '0' + end[3]
   if float(end[4])<10:
     end[4] = '0' + end[4]
   if float(end[5])<10:
     end[5] = '0' + end[5]

   #Compile date strings
   date1 = start[0]+' '+dates[int(start[1])-1]+' '+start[2]+' '+start[3]+':'+start[4]+':'+start[5][:4]
   date2 = end[0]+' '+dates[int(end[1])-1]+' '+end[2]+' '+end[3]+':'+end[4]+':'+end[5][:4]

   #Add text to figure
   pylab.figtext(0.73,.175,'SpecID Count: %s' %speccount)
   pylab.figtext(0.73,.15,'Start: %s' %date1)
   pylab.figtext(0.73,.125,'End: %s' %date2)

   #Save plot?
   if saveplot != '':
     pylab.savefig('%s' %saveplot)

   return fig
Example #6
0
def makeplot(ra,dec,specid,where='',figtype='cart',ellipses='no',saveplot=''):
  """ Makes a plot of RA and DEC.

  where is a string specifying which data from mysql to grab. 
  ONLY include columns from table 'config'. do not include 
  the word 'where' at the beginning or the semicolon at the 
  end. all other common mysql syntax rules apply.

  figtype is the figure type. default is 'cart' for cartesian 
  coordinates. 'sky' plots the ra and dec as a Mollweide
  projection

  if ellipses='yes', the spectra are plotted as individual ellipses
  of fixed size corresponding to ALFA beam width (only for Cartesian
  coordinate axis)

  saveplot allows you to save the figure by specifying the 
  file name as a string. if left empty, the figure is not 
  saved.

  returns a figure"""

  import jd2gd, command

  #Determine values to include in figure
  var1 = min(specid)
  var2 = max(specid)
  var3 = len(specid)

  #Get time info from database
  cmd = command.generate('obstime','config',where=where)
  data = MySQLFunction.mysqlcommand(cmd)

  #Create list
  day = numpy.asarray([data[x][0] for x in xrange(len(data))])

  #Determine start and end dates
  start = min(day)
  end = max(day)
 
  #Create Gregorian date from obstime
  start = jd2gd.caldate(start)
  end = jd2gd.caldate(end)
  dates = ['January','February','March','April','May','June','July',
  'August','September','October','November','December']
  start = [str(start[x]) for x in range(len(start))]
  end = [str(end[x]) for x in range(len(end))]

  #Insert zeros to make formatting nice
  if float(start[2])<10:
    start[2] = '0' + start[2]
  if float(start[3])<10:
    start[3] = '0' + start[3]
  if float(start[4])<10:
    start[4] = '0' + start[4]
  if float(start[5])<10:
    start[5] = '0' + start[5]
  if float(end[2])<10:
    end[2] = '0' + end[2]
  if float(end[3])<10:
    end[3] = '0' + end[3]
  if float(end[4])<10:
    end[4] = '0' + end[4]
  if float(end[5])<10:
    end[5] = '0' + end[5]

  #Compile date strings
  date1 = start[0]+' '+dates[int(start[1])-1]+' '+start[2]+' '+start[3]+':'+start[4]+':'+start[5][:4]
  date2 = end[0]+' '+dates[int(end[1])-1]+' '+end[2]+' '+end[3]+':'+end[4]+':'+end[5][:4]

  #Which fig type?
  patches=[]
  if figtype=='cart':

    #Initialize figure
    fig=pylab.figure(figsize=(12,7))
    ax1 = fig.add_axes([0.1, 0.12, 0.85, 0.75])

    #Plot data
    if ellipses=='yes':
      for x,y in zip(ra,dec):
       circle=matplotlib.patches.Ellipse((x,y),width=.0038888,height=.058333,facecolor='b',edgecolor='b')
       patches.append(circle)
      p=matplotlib.collections.PatchCollection(patches)
      ax1.add_collection(p)
    else: ax1.scatter(ra,dec,s=.1,label='spectra')
   
  else:
    #Convert RA to degrees
    length = len(ra)
    ra = [ra[x]*15 for x in range(length)]
    
    #Convert arrays to radians
    ra = [math.radians(ra[x]) for x in range(length)]
    dec = [math.radians(dec[x]) for x in range(length)]

    #Adjust ra scale
    ra = [ra[x]-numpy.pi for x in range(length)]
    
    #Create figure
    fig = pylab.figure(figsize=(12,7))
    ax1 = fig.add_axes([.1,.12,.70,.85],projection='mollweide')
    
    #Plot data
    ax1.scatter(ra,dec,label='spectra',marker='+',c='b')
    
    #Set xtick labels
    ax1.set_xticklabels(['2','4','6','8','10','12','14','16','18','20','22'])

  #Create galactic plane lines
  #Create l,b arrays
  b = [math.radians(5)]*360 + [math.radians(-5)]*360
  l = list(numpy.linspace(0,2*numpy.pi,360))*2
  
  #Pass l,b pairs to gal2eq
  bounds = []
  for x in range(720):
    bounds.append(astroconvert.gal2eq(l[x],b[x]))

  #Separate into component arrays
  ra_bounds = [bounds[x][0] for x in range(720)]
  dec_bounds = [bounds[x][1] for x in range(720)]

  #Convert to deg if necessary, and plot
  if figtype=='sky':
    ra_bounds = [ra_bounds[x]-numpy.pi for x in range(720)]
    ax1.scatter(ra_bounds,dec_bounds,edgecolor='none',c='r',label='galactic plane')
  else:
    ra_bounds = [math.degrees(ra_bounds[x])/15 for x in range(720)]
    dec_bounds = [math.degrees(dec_bounds[x]) for x in range(720)]
    ax1.plot(ra_bounds,dec_bounds,'r',label='galactic plane')

    #Set axis limits
    ax1.set_xlim(0, 24)
    ax1.set_ylim(0, 40)

  #Set axis labels and title
  pylab.xlabel('RA (hours)')
  pylab.ylabel('Dec (degrees)')
  pylab.title('Right Ascension and Declination')

  #Add legend
  ax1.legend(bbox_to_anchor=(0,0,1,1),loc=0)

  #Add text to figure
  pylab.figtext(0.1,.97,'First Specid: %d' %var1)
  pylab.figtext(0.1,.945,'Last Specid: %d' %var2)
  pylab.figtext(0.1,.92,'Count: %d spectra' %var3)
  pylab.figtext(0.95,.97,' Start: %s' %date1, ha='right')
  pylab.figtext(0.95,.945,'End: %s' %date2, ha='right')

  #Include grid
  pylab.grid(True)
 
  #Save figure?
  if saveplot!='':
    pylab.savefig('%s'%saveplot)

  return fig
def makeplot(bins,count,maximum=24,histtype='fracmax',where='',saveplot=''):
  """Plots data for event count per coarse frequency bin.

  maximum is the maximum number of allowed hits per coarse bin
  
  histtype specifies the type of histogram you intend to 
  plot: 
  
  'fracmax' is the fraction of spectra that reach the 
  max (24) for each coarse bin, and the default option, 
  
  '%full' is the percent full each coarse bin is after adding   
  events for all desired spectra, and
 
  'maxcount' is the number of times each bin reaches the max 
  (24)

  where is a string to include additional information to 
  narrow the results. typically it will be used to specify a 
  range of specids. each column name MUST be prefixed with 
  the first letter of the table name and a period, like 
  c.obstime. do not include the word 'where' at the beginning 
  or the semicolon at the end. all other common mysql syntax 
  rules apply. Ex: 'h.specid>1 and h.specid<=20 and 
  c.beamnum!=8'. 

  saveplot allows the user to have the figure saved by 
  inputting the file name. if left empty, no file will be saved

  returns a figure"""

  import pylab, MySQLFunction, jd2gd, numpy, command

  #Data to plot?
  if len(bins) != 0:

   #Initialize figure
   fig=pylab.figure(figsize=(12,7))
   ax1 = fig.add_axes([0.1, 0.14, 0.85, 0.75])

   #Set axes limits
   v = [0,4096,0,max(count)]
   ax1.axis(v)

   #Configure xticks
   xticks = numpy.arange(0,4097,256)
   ax1.set_xticks(xticks)

   #Rotate xticks
   for i in ax1.get_xticklabels(): i.set_rotation(45)

   #Add grid
   pylab.grid(True)
  
   #Plot data
   pylab.bar(bins,count,width=1,align='center')

   #Get extra info for plot
   if 'c.' not in where:
     where = where + ' and h.specid=c.specid'
   cmd = command.generate('c.specid,c.obstime','config c, hit h',where=where)
   data = MySQLFunction.mysqlcommand(cmd)
  
   #Separate into arrays
   length = len(data)
   specid = numpy.asarray([data[x][0] for x in range(length)])
   time = numpy.asarray([data[x][1] for x in range(length)])

   #Get specid count
   uniq_IDs = set(specid)
   speccount = len(uniq_IDs)
   num = sum(count)
 
   #Get hit count and labels
   if histtype=='fracmax':
     title = 'Fraction of Spectra That Reach Maximum Allowed Hit Count Per Coarse Bin'
     hitcount = int(num*speccount)
     countlabel = 'Max Count'
   if histtype=='%full':
     title = 'Percent of Maximum Allowed Hits Per Coarse Bin'
     hitcount = int(num*speccount*24)
     countlabel = 'Hit Count'
   if histtype=='maxcount':
     title = 'Number of Times Each Coarse Bin Reached The Maximum Allowed Hit Count'
     hitcount = sum(count)
     countlabel = 'Max Count'
   pylab.xlabel('Coarse Bin Number')
   pylab.ylabel('Count')
   pylab.title('%s' %title)  

   #Determine start and end dates
   start = min(time)
   end = max(time)
 
   #Create Gregorian date from obstime
   start = jd2gd.caldate(start)
   end = jd2gd.caldate(end)
   dates = ['January','February','March','April','May','June','July',
  'August','September','October','November','December']
   start = [str(start[x]) for x in range(len(start))]
   end = [str(end[x]) for x in range(len(end))]

   #Insert zeros to make formatting nice
   if float(start[2])<10:
     start[2] = '0' + start[2]
   if float(start[3])<10:
     start[3] = '0' + start[3]
   if float(start[4])<10:
     start[4] = '0' + start[4]
   if float(start[5])<10:
     start[5] = '0' + start[5]
   if float(end[2])<10:
     end[2] = '0' + end[2]
   if float(end[3])<10:
     end[3] = '0' + end[3]
   if float(end[4])<10:
     end[4] = '0' + end[4]
   if float(end[5])<10:
     end[5] = '0' + end[5]

   #Compile date strings
   date1 = start[0]+' '+dates[int(start[1])-1]+' '+start[2]+' '+start[3]+':'+start[4]+':'+start[5][:2]
   date2 = end[0]+' '+dates[int(end[1])-1]+' '+end[2]+' '+end[3]+':'+end[4]+':'+end[5][:2]

   #Add text to figure
   pylab.figtext(0.1,.97,'%s: %d' % (countlabel,hitcount))
   pylab.figtext(0.1,.945,'SpecID Count: %s' %speccount)
   pylab.figtext(0.95,.97,'Start: %s' %date1, ha='right')
   pylab.figtext(0.95,.945,'End:   %s' %date2, ha='right')
   pylab.figtext(0.1,.92,'Limit: %d' %maximum)

   #Save figure?
   if saveplot!='':
     pylab.savefig('%s'%saveplot)

   return fig