#! /usr/bin/python
#Utility usefull to make a horizon map of Observatory site
import os

filename = False
defaultMin = '0'
defaultMax = '90'
defaultWhy = 'Mount Limit'
deg = 0
map = {}

while 1:
   filename = raw_input('Please insert OBS name (max 8 char) : ')
   if filename.replace(' ','') == '':
      print "Filename isn't optional .. please insert it!"
      filename = False
   else:
      break

print 'Filename set to : ', filename

while 1:
   defaultMin = raw_input('Insert default Horizon Low ALT Limit ('+defaultMin+') :')
   defaultMax = raw_input('Insert default Horizon Max ALT Limit ('+defaultMax+') :')
   defaultWhy = raw_input("Insert default Limit's Motivation ("+defaultWhy+") :")
   if defaultMin.replace(' ','') != '' or defaultMax.replace(' ','') != '' or defaultWhy.replace(' ','') != '':
       try:
          dMin = int(defaultMin)
          dMax = int(defaultMax)
          break
       except:
          defaultMin = '0'
          defaultMax = '90'
          print 'Insert only numeric values please!'
   else:
      dMin = 0
      dMax = 90
      break

print 'Default Values set to Low = ',dMin,' Max = ',dMax
print '\n'
print "Now we can start to insert Map's values\nStart from North (AZ = 0 deg), finish to 359 deg\nInsert range (from/to) eg.: 15/30 indicates range from AZ = 15 deg. to AZ = 30 deg.\nand then insert ALT Min limit (overriding default value only for range previsius inserted) \neg.: 15 fix min ALT limit to 15 deg.(mountain or another fixed obstacle)\nFinally system ask you limit's motivation"

degRangeOK = False
altMinOK = False
whyMinOK = False
addAnother = False
finished = False
while deg < 359:
   while 1 and not degRangeOK:
      degRange = raw_input('Insert range : ')
      if '/' in degRange:
         min, max = degRange.split('/')
         try:
            min = int(min)
            max = int(max)
            degRangeOK = True
         except:
            print 'Please insert only numeric values (eg. 15/30)'
      print 'Range to set from ',min,' to ',max,' deg.'
      altMinOK = False
   
   
   
   while 1 and not altMinOK:
      altMin = raw_input('Insert Min value for range '+degRange+' : ')
      try:
         altMin = int(altMin)
         altMinOK = True
         whyMinOK = False
      except:
         print 'Insert only numeric value (eg. 15)'
   
   while 1 and not whyMinOK:
      whyMin = raw_input('Type the motivation (Default='+defaultWhy+'): ')
      if whyMin.replace(' ','') != '':
         whyMinOK = True
      else:
         print 'Please write a valid Motivation for Limit!'

   if deg == 0 and min != 0:
      for x in range (0, min):
         map[x] = [dMin, dMax, defaultWhy]
         deg = x
      print 'Range 0/',min,' set to defaults value (',dMin,'/',dMax,')' 
      x = False
   
   if not finished:
      if deg < min:
         for x in range(deg, min):
            map[x] = [dMin, dMax, defaultWhy]
         x = False
      for x in range (min, max+1):
         map[x] = [altMin, dMax, whyMin]
         deg = x
      print 'Range ',degRange,' deg. set to min ALT =',altMin
      addAnother = False
      x = False
   
   
   while 1 and not addAnother:
      confirm = raw_input('Insert another range ? (y/n)')
      if confirm.lower() == 'y':
         degRangeOK = False
         addAnother = True
      elif confirm.lower() == 'n':
         addAnother = False
         if deg < 359:
            for x in range (deg+1, 360):
               map[x] = [dMin, dMax, defaultWhy]
               deg = x
            print 'Range from ',deg,' to end (359) set to default values (',dMin,'/',dMax,')'
            degRangeOK = True
            finished = True
            x = False
         break
      else:
         print 'Type y or n only please!!'

if not addAnother:
   mapFile = open(filename, 'w')
   textMap = ''
   for degs in map.keys():
      textMap += str(degs)+'!'+str(map[degs][0])+'!'+str(map[degs][1])+'!'+map[degs][2]+'\n'
   mapFile.write(textMap)
   mapFile.close()
      
print 'map file ',filename,' successifull saved!'
