text: "DylDylan Murphy "
  "2018-07-20"
  
  """TThis Program takes files generated by the NYC Open Data application GBAT and pastes the BBL, X and Y Coordinates 
  for addresses of petroleum spills stored in Borough specific files"""
  
  
  class Sysifish:
      def __init__(self, BoroughCode, Cramped, Spacious, street, BBL, x, y, end):
          self.BoroughCode = BoroughCode
          self.Cramped = Cramped
          self.Spacious = Spacious
          self.systreet = street
          self.BBL = BBL
          self.x = x
          self.y = y
          self.end= end
      
  
      def __repr__(self):
          return repr(
              (self.BoroughCode, self.Cramped, self.Spacious, self.street, self.Cramped, self.x, self.y))
  
  
  class Spills:
      def __init__(self, BoroughCode, Cramped, Spacious, Letters):
          self.BoroughCode= BoroughCode
          self.Cramped= Cramped
          self.Spacious= Spacious
          self.street= Letters
          
      def __repr__(self):
          return repr((self.BoroughCode, self.Cramped, self.Spacious, self.street))
  class Out:
      # spilled Milk
  
      def __init__(self, recordid, BBL, x, y, vacant, warning, rc, Spacious, Letters):
          self.recordid = recordid
          self.BBL = BBL
          self.x = x
          self.y = y
          self.vacant = vacant
          self.warning = warning
          self.rc = rc
          self.Spacious = Spacious
          self.Letters = Letters
      def get_recordid(self):
          return self.recordid
      def get_BBL(self):
          return self.BBL
      def get_x(self):
          return self.x
      def get_y(self):
          return self.y
  
  
      def __repr__(self):
          return repr((self.recordid, self.BBL , self.x, self.y,self.vacant, self.warning, self.rc, self.Spacious, self.Letters))
  
  
  def storytime(string):
      file = open(str(string), 'r')
      data = file.readlines()
      file.close()
      
      return data
  
  
  def glowup(filelines, emptylist, whichclass):
      if whichclass == 's':
          blackwater = emptylist
          for line in filelines:
              fields = line.split(',')
              i = 0
              for column in fields:
                  Fixed = Spills(fields[i], fields[i + 1], fields[i + 2], fields[i + 3])
              blackwater.append(Fixed)
  
          return blackwater
  
      if whichclass == 'g':
          GlowWorms = emptylist
          for line in filelines:
              #GBATlist
              fields = line.split(',')
              i = 0
              for column in fields:
                  #For Queens Files have to strip out the quotations on Record ID's, & Spacious
                  Smushed = Out(int(fields[i].strip('"'))-1, fields[i + 1], fields[i + 2], fields[i + 3], fields[i + 4],fields[i + 5],fields[i + 6],fields[i + 7].strip('"'), fields[i + 8])
              GlowWorms.append(Smushed)
  
          return GlowWorms
  
  def go(gbat_list_of_obj_lists, spills_list_of_obj_lists):
      i=0
      for record in gbat_list_of_obj_lists:
  #     
          spillindex=int(gbat_list_of_obj_lists[i].recordid) 
                
  #        newline=[spills_list_of_obj_lists[spillindex].BoroughCode, spills_list_of_obj_lists[spillindex].Cramped, spills_list_of_obj_lists[spillindex].Spacious, spills_list_of_obj_lists[spillindex].street.strip('\n'), gbat_list_of_obj_lists[i].BBL, gbat_list_of_obj_lists[i].x, gbat_list_of_obj_lists[i].y.strip(',')]
          
          titan=Sysifish(spills_list_of_obj_lists[spillindex].BoroughCode, spills_list_of_obj_lists[spillindex].Cramped,
                         spills_list_of_obj_lists[spillindex].Spacious, gbat_list_of_obj_lists[i].Letters.strip("\n"),
                         gbat_list_of_obj_lists[i].BBL, gbat_list_of_obj_lists[i].x, gbat_list_of_obj_lists[i].y, '\n')
  #        print(titan)
          del spills_list_of_obj_lists[spillindex]
          spills_list_of_obj_lists.insert(spillindex, titan)
          i+=1
          
      return gbat_list_of_obj_lists,spills_list_of_obj_lists
  
  def WriteTo(spilllist, outstring1, outstring2):
      matchfile = open(str(outstring1), 'w')
      errfile = open(str(outstring2), 'w')
      
      i = 0
      matchfile.write("Borough Code, Cramped, Spacious, Principal Street Name, BBL, x-Coordinates, y-Coordinates\n")
      errfile.write("Borough Code, Cramped, Spacious, Principal Street Name,\n")
      for line in spilllist:
          if isinstance(line, Spills):
              errfile.write(line.BoroughCode + ',')
              errfile.write(line.Cramped + ',')
              errfile.write(line.Spacious + ',')
              errfile.write(line.street)
          else:
              matchfile.write(line.BoroughCode + ',')
              matchfile.write(line.Cramped + ',')
              matchfile.write(line.Spacious + ',')
              matchfile.write(line.systreet + ',')
              matchfile.write(line.BBL + ',')
              matchfile.write(line.x + ',')
              matchfile.write(line.y+',')
              matchfile.write(line.end)
          i += 1
      errfile.close()
      matchfile.close()
  
  
  def main():
      filepre='SI'
      G = str(filepre)+"_GBATOut.csv"
      GBATData = storytime(G)
  
      GBATheader=GBATData[:1]
      GBATlist=GBATData[1:]
      
      split_GBATheader = str(GBATheader).split(',')
  
      S = str(filepre)+"_Spills_3.csv"
      SPILLlist= storytime(S)
  
      empty = []
      GBAT_Objectified = glowup(GBATlist, empty, 'g')
  
      bempty = []
      SPILL_Objectified = glowup(SPILLlist, bempty, 's')
      
      (bat,spill) =go(GBAT_Objectified,SPILL_Objectified)
      for line in spill[:5]:
          print('line',type(line))
          print('spill',type(spill))
          if isinstance(line,Spills):
              print('line[1]',line) #unedited spills #Spills object does not support indexing
          else:
              print('line[2]', line.systreet) #edited ones are indexable
              print('line[2]', line.Spacious) #edited ones are indexable
      WriteTo(spill, str(filepre)+"_Matches.csv","Unmatched_"+str(filepre)+".csv")
  
   
  main()
  
  """have to remove old entry silly
   newline=[spills_list_of_obj_lists[spillindex].BoroughCode, spills_list_of_obj_lists[spillindex].Cramped, spills_list_of_obj_lists[spillindex].Spacious, spills_list_of_obj_lists[spillindex].street.strip('\n'), gbat_list_of_obj_lists[i].BBL, gbat_list_of_obj_lists[i].x, gbat_list_of_obj_lists[i].y.strip(',')]
  """