def textObsE(objects,name):
    '''export observations to Text file'''
    f=open(name,'w')
    f.write('Date  Time  "Object"  "Observer"  "Telescope"  "Site"  Long.  Lat.  Elev. "Note"\n')
    obs={}  #list of all observations
    for i in objects:
        o=objects[i]['obs']
        for j in o:
            if j in obs:
                #multiple objects observed at same time
                off=1
                while j+str(off) in obs: off+=1
                obs[j+str(off)]=o[j]
            else: obs[j]=o[j]
    #sort by Object and JD
    #for i in objects:
    #    AllObs=objects[i]['obs']
    #    for j in sortObs(AllObs):
    #        o=AllObs[j]

    #sort all observations by JD
    for i in sortObs(obs):
        o=obs[i]
        f.write(o.date.replace(" ","  ")+'  ')
        f.write('"'+o.obj+'"  ')
        f.write('"'+o.observer+'"  ')
        f.write('"'+o.telescope+'"  ')
        f.write('"'+o.site.name+'"  ')
        f.write(stars.printDMS(o.site.lon)+'  ')
        f.write(stars.printDMS(o.site.lat)+'  ')
        f.write(str(o.site.ele)+'  ')
        f.write('"'+o.note.replace('\n','; ')+'"\n')
    f.close()

    return obs
def aptE(objects,name):
    '''export objects to APT file'''
    f=open(name,'w')
    f.write('<?xml version="1.0"?>\n<Objects>\n')
    for i in objects:
        o=objects[i]['object']
        f.write('\t<Obj>\n')
        f.write('\t\t<Object>'+o.name+'</Object>\n')
        f.write('\t\t<NameNotes>'+o.note.replace('\n','; ')+'</NameNotes>\n')
        if 'galaxy' in o.type.lower(): typ='G'
        elif 'open' in o.type.lower(): typ='OC'
        elif 'globular' in o.type.lower(): typ='GC'
        elif 'planetary' in o.type.lower(): typ='PN'
        elif 'star' in o.type.lower(): typ='S'
        elif 'reflection' in o.type.lower(): typ='RN'
        elif 'emission' in o.type.lower(): typ='EN'
        elif 'dark' in o.type.lower(): typ='DN'
        elif 'diffuse' in o.type.lower(): typ='DFN'
        elif 'asterism' in o.type.lower(): typ='A'
        elif 'nebula' in o.type.lower(): typ='N'
        elif 'supernova' in o.type.lower(): typ='SN'
        else:
            typ=o.type
            print(typ)
        f.write('\t\t<Type>'+typ+'</Type>\n')
        f.write('\t\t<Const>'+o.const+'</Const>\n')
        f.write('\t\t<Mag>'+str(o.mag)+'</Mag>\n')
        f.write('\t\t<Size>'+o.size.replace("'",'')+'</Size>\n')
        f.write('\t\t<RA>'+stars.printDMS(o.ra)+'</RA>\n')
        f.write('\t\t<DEC>'+stars.printDMS(o.dec)+'</DEC>\n')
        f.write('\t\t<Angle></Angle>\n')
        f.write('\t</Obj>\n')
    f.write('</Objects>\n')
    f.close()
def maximE(objects,name):
    '''export objects to MaximDL file'''
    f=open(name,'w')
    f.write('"NAME","R.A.","DEC.","MAG"\n')
    for i in objects:
        o=objects[i]['object']
        f.write('"'+o.name+'/",')
        f.write('"'+stars.printDMS(o.ra).replace(':',' ')+'",')
        f.write('"'+stars.printDMS(o.dec).replace(':',' ')+'",')
        f.write('"'+str(o.mag)+'"\n')
    f.close()
def sipsE(objects,name):
    '''export objects to SIPS file'''
    f=open(name,'w')
    f.write('[objects]\n')
    for i in objects:
        o=objects[i]['object']
        f.write(o.name.replace(' ','_')+' ')
        f.write(stars.printDMS(o.ra).replace(':',' ')+' ')
        f.write(stars.printDMS(o.dec).replace(':',' ')+' ')
        f.write(o.const+' ')
        f.write(o.type.replace(' ','_')+' ')
        f.write(o.size.replace(' ','_')+' ')
        f.write(str(o.mag)+'mag\n')
    f.close()
def textE(objects,name):
    '''export objects to Text file'''
    f=open(name,'w')
    f.write('"Name"  RA  Dec  Const.  Mag  "Size"  "Type"  "Note"\n')
    for i in objects:
        o=objects[i]['object']
        f.write('"'+o.name+'"  ')
        f.write(stars.printDMS(o.ra)+'  ')
        f.write(stars.printDMS(o.dec)+'  ')
        f.write(o.const+'  ')
        f.write(str(o.mag)+'  ')
        f.write('"'+o.size+'"  ')
        f.write('"'+o.type+'"  ')
        f.write('"'+o.note.replace('\n','; ')+'"\n')
    f.close()
def excelE(objects,name,jd,jd0,lon,lat):
    '''export objects to Excel file'''
    try: wb=xlwt.Workbook()
    except:
        messagebox.showerror('Export to Excel','Export to Excel is not possible! Please, install package "xlwt".')
        warnings.simplefilter('module')
        warnings.warn('Module "xlwt" not installed! Export to Excel will not be possible!',ImportWarning,stacklevel=2)
        return
    ws=wb.add_sheet('Objects')

    ws.write(0,0,'Name')
    ws.write(0,1,'RA')
    ws.write(0,2,'DEC')
    ws.write(0,3,'Alt.')
    ws.write(0,4,'Azm.')
    ws.write(0,5,'Rise')
    ws.write(0,6,'Transit')
    ws.write(0,7,'Transit Alt.')
    ws.write(0,8,'Set')
    ws.write(0,9,'Const.')
    ws.write(0,10,'Mag')
    ws.write(0,11,'Size')
    ws.write(0,12,'Type')
    ws.write(0,13,'Note')
    for i in range(14): ws.col(i).width=3500
    row=1
    for i in objects:
        o=objects[i]['object']
        ws.write(row,0,o.name)
        ws.write(row,1,stars.printDMS(o.ra))
        ws.write(row,2,stars.printDMS(o.dec))
        a,h=o.altAz(jd,lon,lat)
        ws.write(row,3,stars.printDMS(h))
        ws.write(row,4,stars.printDMS(a))
        r,t,s=o.rise(jd0,lon,lat)
        if not r=='NA':
            r=stars.printDMS(r)
            s=stars.printDMS(s)
        ws.write(row,5,r)
        ws.write(row,6,stars.printDMS(t))
        ws.write(row,7,round(o.dec+90-lat,2))
        ws.write(row,8,s)
        ws.write(row,9,o.const)
        try: ws.write(row,10,float(o.mag))
        except: ws.write(row,10,o.mag)
        ws.write(row,11,o.size)
        ws.write(row,12,o.type)
        ws.write(row,13,o.note.replace('\n','; '))
        row+=1
    wb.save(name)
def excelObsE(objects,name):
    '''export observations to Excel file'''
    try: wb=xlwt.Workbook()
    except:
        messagebox.showerror('Export to Excel','Export to Excel is not possible! Please, install package "xlwt".')
        warnings.simplefilter('module')
        warnings.warn('Module "xlwt" not installed! Export to Excel will not be possible!',ImportWarning,stacklevel=2)
        return
    ws=wb.add_sheet('Observations')

    time_format=xlwt.XFStyle()
    time_format.num_format_str='hh:mm:ss'
    date_format=xlwt.XFStyle()
    date_format.num_format_str='YYYY-MM-DD'
    #date_time_format=xlwt.XFStyle()
    #date_time_format.num_format_str='YYYY-MM-DD hh:mm:ss'

    ws.write(0,0,'Date')
    ws.write(0,1,'Time')
    ws.write(0,2,'JulDat')
    ws.write(0,3,'Object')
    ws.write(0,4,'RA')
    ws.write(0,5,'DEC')
    ws.write(0,6,'Alt.')
    ws.write(0,7,'Azm.')
    ws.write(0,8,'Const.')
    ws.write(0,9,'Mag')
    ws.write(0,10,'Size')
    ws.write(0,11,'Type')
    ws.write(0,12,'Observer')
    ws.write(0,13,'Telescope')
    ws.write(0,14,'Site')
    ws.write(0,15,'Long.')
    ws.write(0,16,'Lat.')
    ws.write(0,17,'Elev.')
    ws.write(0,18,'Note')

    for i in range(19): ws.col(i).width=3500
    row=1

    obs={}  #list of all observations
    for i in objects:
        o=objects[i]['obs']
        obj=objects[i]['object']
        for j in o:
            if j in obs:
                #multiple objects observed at same time
                off=1
                while j+str(off) in obs: off+=1
                obs[j+str(off)]=copy.deepcopy(o[j])
                obs[j+str(off)].obj=obj
            else:
                obs[j]=copy.deepcopy(o[j])
                obs[j].obj=obj
    #sort by Object and JD
    #for i in objects:
    #     AllObs=objects[i]['obs']
    #     obj=objects[i]['object']
    #     for j in sortObs(AllObs):
    #         o=AllObs[j]

    #sort all observations by JD
    for i in sortObs(obs):
        o=obs[i]
        ws.write(row,0,o.datetimeObject.date(),date_format)
        ws.write(row,1,o.datetimeObject.time(),time_format)
        #ws.write(row,19,o.datetimeObject,date_time_format)
        ws.write(row,2,o.jd)
        #ws.write(row,3,o.obj)
        #ws.write(row,4,stars.printDMS(obj.ra))
        #ws.write(row,5,stars.printDMS(obj.dec))
        #a,h=obj.altAz(o.jd,o.site.lon,o.site.lat)
        ws.write(row,3,o.obj.name)
        ws.write(row,4,stars.printDMS(o.obj.ra))
        ws.write(row,5,stars.printDMS(o.obj.dec))
        a,h=o.obj.altAz(o.jd,o.site.lon,o.site.lat)
        ws.write(row,6,stars.printDMS(h))
        ws.write(row,7,stars.printDMS(a))
        #ws.write(row,8,obj.const)
        #try: ws.write(row,9,float(obj.mag))
        #except: ws.write(row,9,obj.mag)
        #ws.write(row,10,obj.size)
        #ws.write(row,11,obj.type)
        ws.write(row,8,o.obj.const)
        try: ws.write(row,9,float(o.obj.mag))
        except: ws.write(row,9,o.obj.mag)
        ws.write(row,10,o.obj.size)
        ws.write(row,11,o.obj.type)
        ws.write(row,12,o.observer)
        ws.write(row,13,o.telescope)
        ws.write(row,14,o.site.name)
        ws.write(row,15,stars.printDMS(o.site.lon))
        ws.write(row,16,stars.printDMS(o.site.lat))
        ws.write(row,17,o.site.ele)
        ws.write(row,18,o.note.replace('\n','; '))
        row+=1
    wb.save(name)

    return obs