def getstepoffset(offset,coord):
   """Get stepdec and declabel given the range in declination plotted"""

   # label steps in arcseconds
   offlabel = array([1  ,5,10,15,20,30,60,300,600,900,1200,1800,3600,18000,36000])
   offstep  = array([0.5,1, 5, 5, 5,10,20, 60,300,300, 300, 600,1200, 3600,18000])
   tmp = abs(offset[1] - offset[0])

   if coord == 'so': # arcsecond offset
      tmp = 3600*tmp
   elif coord == 'mo': # arcminute offset
      offlabel = offlabel/60.
      offstep  = offsetep/60.
      tmp = 60*tmp
   elif coord == 'do': # degree offset
      offlabel = offlabel/3600.
      offstep  = offstep/3600.
   else:
      nlclib.error("getstepoffset(): coord keyword must be one of: so, mo, do!")

   guess = tmp/6. # aim for about 6 labels
   diff  = [abs(guess-a) for a in offlabel]
   idx   = diff.index(min(diff))

   return offstep[idx],offlabel[idx]
def getstepdec(dec):
   """Get stepdec and declabel given the range in declination plotted"""

   # label steps in arcseconds
   tmplabel = [1  ,5,10,15,20,30,60,300,600,900,1200,1800,3600,18000,36000]
   tmpstep  = [0.5,1, 5, 5, 5,10,20, 60,300,300, 300, 600,1200, 3600,18000]

   declabel = map(lambda a: a/3600.,tmplabel) # convert to degrees
   stepdec  = map(lambda a: a/3600.,tmpstep)  # convert to degrees

   tmp = abs(dec[1] - dec[0])
   guess = tmp/6. # aim for about 6 labels
   diff  = [abs(guess-a) for a in declabel]
   idx   = diff.index(min(diff))

   return stepdec[idx],declabel[idx]
def getstepra(ra):
   """Get stepra and ralabel for the range of RA plotted.
      Input ra is two values in degrees"""

   # label steps in hour-seconds
   tmplabel = [1  ,  2,5,10,15,20,30,60,120,300,600,900,1200,1800,3600,18000]
   tmpstep  = [0.5,0.5,1, 2, 5, 5, 5,20, 30, 60,300,300, 300, 600,1200, 3600]

   ralabel = map(lambda a: a/240.,tmplabel) # convert to degrees
   stepra  = map(lambda a: a/240.,tmpstep)  # convert to degrees

   tmp = abs(ra[1] - ra[0])
   guess = tmp/6. # aim for about 6 labels
   diff  = [abs(guess-a) for a in ralabel]
   idx   = diff.index(min(diff))

   return stepra[idx],ralabel[idx]