;nazev=cas (poslednich 8 pismen)

;nastaveni formatu casove osy

function yticks,axis,index,value

   hour=long(value)/3600
   minute=long(value-3600 * hour) / 60
   sec=value mod 60

   return,string(hour, minute, sec, $
          format="(i2.2,':',i2.2,':',i2.2)")
end

pro makesurface,jaky
COMMON vse,base,file_button2,option_menu,rangebutton,titlebutton,barva,pozadi,uhelx,uhelz,pos
COMMON norm,poleint,poleiks,poleips,ips,prvnihodnota,poslednihodnota
COMMON rozsah,xr,yr,zr,xrozsah,yrozsah,zrozsah
COMMON nazvy,naz,xnaz,ynaz,znaz,nazev,xnazev,ynazev,znazev
COMMON druh,jakyprofil
s=size(poleiks)
poleiks2=make_array(poslednihodnota-prvnihodnota+1,s(2),value=0,/float)
poleips2=make_array(poslednihodnota-prvnihodnota+1,s(2),value=0,/float)
poleint2=make_array(poslednihodnota-prvnihodnota+1,s(2),value=0,/float)

for i=0,poslednihodnota-prvnihodnota do poleips2(i,*)=prvnihodnota+i
j=0
for i=0,poslednihodnota-prvnihodnota do begin
   if poleips2(i,0) eq ips(j) then begin
      poleiks2(i,*)=poleiks(j,*) & poleint2(i,*)=poleint(j,*) & j=j+1
   endif else poleiks2(i,*)=poleiks(0,*)
endfor
sp=size(poleint2)
if sp(1) gt 1 then begin
erase

if xr(0) ne '' then begin
   xr1=fix(strmid(xr(0),0,strpos(xr(0),',')))
   xr2=fix(strmid(xr(0),1+strpos(xr(0),','),strlen(xr(0))-(1+strpos(xr(0),','))))
   xran=[xr1,xr2]
   endif else xran=!x.range
if yr(0) ne '' then begin
   yr1=fix(strmid(yr(0),0,strpos(yr(0),',')))
   yr2=fix(strmid(yr(0),1+strpos(yr(0),','),strlen(yr(0))-(1+strpos(yr(0),','))))
   yr11=fix(strmid(yr1,0,2))*3600+fix(strmid(yr1,2,2))*60+fix(strmid(yr1,4,2))
   yr22=fix(strmid(yr2,0,2))*3600+fix(strmid(yr2,2,2))*60+fix(strmid(yr2,4,2))
   yran=[yr11,yr22]
   endif else yran=!y.range
if zr(0) ne '' then begin
   zr1=fix(strmid(zr(0),0,strpos(zr(0),',')))
   zr2=fix(strmid(zr(0),1+strpos(zr(0),','),strlen(zr(0))-(1+strpos(zr(0),','))))
   zran=[zr1,zr2]
   endif else zran=!z.range
if jaky eq 1 then begin
surface,poleint2,poleiks2,poleips2,ytickformat='yticks',ax=uhelx,az=uhelz,$
     title=naz(0),xtitle=xnaz(0),ytitle=ynaz(0),ztitle=znaz(0),$
     xmargin=[pos,pos],ymargin=[pos,pos],zmargin=[pos,pos],charsize=2,$
     xrange=xran,yrange=yran,zrange=zran
jakyprofil=1
endif else begin
shade_surf,poleint2,poleiks2,poleips2,ytickformat='yticks',ax=uhelx,az=uhelz,$
     title=naz(0),xtitle=xnaz(0),ytitle=ynaz(0),ztitle=znaz(0),$
     xmargin=[pos,pos],ymargin=[pos,pos],zmargin=[pos,pos],charsize=2,$
     xrange=xran,yrange=yran,zrange=zran
jakyprofil=2
endelse
endif else begin
 chyba=widget_base()
  chybalab=widget_label(chyba,value="Can't surface one profile only")
  widget_control,chyba,/realize
  wait,2 & widget_control,chyba,/destroy
endelse

end

pro normal
COMMON vse,base,file_button2,option_menu,rangebutton,titlebutton,barva,pozadi,uhelx,uhelz,pos
COMMON norm,poleint,poleiks,poleips,ips,prvnihodnota,poslednihodnota
COMMON rozsah,xr,yr,zr,xrozsah,yrozsah,zrozsah
COMMON nazvy,naz,xnaz,ynaz,znaz,nazev,xnazev,ynazev,znazev
COMMON druh,jakyprofil
s=size(ips) & kolik=s(1) & s=size(poleiks)
erase,!p.background
!y.range=0 ;& !z.range=0 & !x.range=0
!z.range=[min(poleint),max(poleint)]
!x.range=[min(poleiks),max(poleiks)]
if xr(0) ne '' then begin
   xr1=fix(strmid(xr(0),0,strpos(xr(0),',')))
   xr2=fix(strmid(xr(0),1+strpos(xr(0),','),strlen(xr(0))-(1+strpos(xr(0),','))))
   xran=[xr1,xr2]
   endif else xran=!x.range
if yr(0) ne '' then begin
   yr1=(strmid(yr(0),0,strpos(yr(0),',')))
   yr2=(strmid(yr(0),1+strpos(yr(0),','),strlen(yr(0))-(1+strpos(yr(0),','))))
   yr11=fix(strmid(yr1,0,2))*3600+fix(strmid(yr1,2,2))*60+fix(strmid(yr1,4,2))
   yr22=fix(strmid(yr2,0,2))*3600+fix(strmid(yr2,2,2))*60+fix(strmid(yr2,4,2))
   yran=[yr11,yr22]
   endif else begin
        yran=[(prvnihodnota/10)*10,(poslednihodnota/10)*10+10]
   endelse ;yran=!y.range
if zr(0) ne '' then begin
   zr1=fix(strmid(zr(0),0,strpos(zr(0),',')))
   zr2=fix(strmid(zr(0),1+strpos(zr(0),','),strlen(zr(0))-(1+strpos(zr(0),','))))
   zran=[zr1,zr2]
   endif else zran=!z.range

for i=0,kolik-1 do begin
   ips2=make_array(s(2),value=ips(i))
   plot_3Dbox,poleiks(i,*),ips2,poleint(i,*),/noerase,ax=uhelx,az=uhelz,$
             title=naz(0),xtitle=xnaz(0),ytitle=ynaz(0),ztitle=znaz(0),$
             xmargin=[pos,pos],ymargin=[pos,pos],zmargin=[pos,pos],charsize=2,$
             xrange=xran,yrange=yran,zrange=zran,ytickformat='yticks'
  ; if i eq 0 then begin
  ; xran=!x.crange & yran=!y.crange & zran=!z.crange
  ; endif
endfor

jakyprofil=0

end

pro range_event,event
COMMON rozsah,xr,yr,zr,xrozsah,yrozsah,zrozsah
COMMON druh,jakyprofil
widget_control,event.id,get_uvalue=uvalue
case uvalue of
'KONEC':begin
        widget_control,xrozsah,get_value=xr
        widget_control,yrozsah,get_value=yr
        widget_control,zrozsah,get_value=zr
        widget_control,event.top,/destroy
        if jakyprofil eq 0 then normal
        if jakyprofil eq 1 then makesurface,1
        if jakyprofil eq 2 then makesurface,2
        end
'NIC':widget_control,event.top,/destroy
endcase
end

pro title_event,event
COMMON nazvy,naz,xnaz,ynaz,znaz,nazev,xnazev,ynazev,znazev
COMMON druh,jakyprofil
widget_control,event.id,get_uvalue=uvalue
case uvalue of
'KONEC':begin
        widget_control,nazev,get_value=naz
        widget_control,xnazev,get_value=xnaz
        widget_control,ynazev,get_value=ynaz
        widget_control,znazev,get_value=znaz
        widget_control,event.top,/destroy
        if jakyprofil eq 0 then normal
        if jakyprofil eq 1 then makesurface,1
        if jakyprofil eq 2 then makesurface,2
        end
'NIC':widget_control,event.top,/destroy
 endcase
 end

pro save_event,event
widget_control,event.id,get_uvalue=uvalue
case uvalue of
'KONEC':begin
        writelabel=widget_label(widget_info(event.id,/parent),value='Saving...')
        widget_control,widget_info(widget_info(widget_info(event.id,/parent),/parent),/child),get_value=jmeno
        write_gif,jmeno(0)+'.gif',tvrd()
        widget_control,event.top,/destroy
        end

'NIC':widget_control,event.top,/destroy

endcase
end

pro load_event,event
COMMON vse,base,file_button2,option_menu,rangebutton,titlebutton,barva,pozadi,uhelx,uhelz,pos
COMMON loadvse,soubory
COMMON norm,poleint,poleiks,poleips,ips,prvnihodnota,poslednihodnota
COMMON druh,jakyprofil
COMMON uzprof,nahrano
COMMON slidery,xslider,zslider,posslider
COMMON cesty,currpath
widget_control,event.id,get_uvalue=uvalue
case uvalue of
  'KONEC':begin
          widget_control,event.top,/destroy
          posledni=0
          while soubory(posledni) ne '' do posledni=posledni+1
          posledni=posledni-1
          zacatek=strmid(soubory(0),strlen(soubory(0))-10,6)
          konec=strmid(soubory(posledni),strlen(soubory(posledni))-10,6)
          prvnihodnota=(fix(strmid(zacatek,0,2)))*3600+(fix(strmid(zacatek,2,2)))*60+fix(strmid(zacatek,4,2))
          poslednihodnota=(fix(strmid(konec,0,2)))*3600+(fix(strmid(konec,2,2)))*60+fix(strmid(konec,4,2))

          delka=0 ;& ukaz=   ;ukaz=ptrarr(posledni+1)
          for i=0,posledni do begin
           r=readfits(soubory(i)) & s=size(r)
           if s(1) gt delka then delka=s(1)
           ;ukaz(i)=ptr_new(r)
          endfor

          poleint=make_array(posledni+1,delka)
          poleiks=make_array(posledni+1,delka)
          ips=make_array(posledni+1)

          for i=0,posledni do begin
             a=readfits(soubory(i))
             ;a=*ukaz(i)
             aktual=strmid(soubory(i),strlen(soubory(i))-10,6) ;casy
             aktualnihodnota=(fix(strmid(aktual,0,2)))*3600+(fix(strmid(aktual,2,2)))*60+fix(strmid(aktual,4,2))
             s=size(a)
             ;poleiks(i,0:s(1)-1)=a(*,0)-6562.808 & poleint(i,0:s(1)-1)=a(*,1)
             poleiks(i,0:s(1)-1)=a(*,0) & poleint(i,0:s(1)-1)=a(*,1)
             ips(i)=aktualnihodnota
             if s(1) lt delka then begin
            ;    poleiks(i,s(1):delka-1)=a(s(1)-1,0)-6562.808
                poleiks(i,s(1):delka-1)=a(s(1)-1,0)
                poleint(i,s(1):delka-1)=a(s(1)-1,1)
             endif
          endfor
          ;ptr_free,ukaz

          erase & normal & nahrano=1
          widget_control,file_button2,sensitive=1 & widget_control,option_menu,sensitive=1
          widget_control,rangebutton,sensitive=1 & widget_control,titlebutton,sensitive=1
          widget_control,xslider,sensitive=1 & widget_control,zslider,sensitive=1
          widget_control,posslider,sensitive=1
          end

   'NIC':widget_control,event.top,/destroy

   ELSE:begin
         cesta=pickfile(/read,filter='*.fts',path=currpath,get_path=currpath)
         widget_control,widget_info(widget_info(event.id,/parent),/child),set_value=cesta
         if strlen(uvalue) eq 7 then ktery=fix(strmid(uvalue,6,1)) else ktery=fix(strmid(uvalue,6,2))
         soubory(ktery-1)=cesta
        end
endcase
end

pro profiles_event,event
COMMON barva,pozp,barp
COMMON vse,base,file_button2,option_menu,rangebutton,titlebutton,barva,pozadi,uhelx,uhelz,pos
COMMON loadvse,soubory
COMMON rozsah,xr,yr,zr,xrozsah,yrozsah,zrozsah
COMMON nazvy,naz,xnaz,ynaz,znaz,nazev,xnazev,ynazev,znazev
COMMON druh,jakyprofil
COMMON uzprof,nahrano
COMMON cesty,currpath
widget_control,event.id,get_uvalue=uvalue
case uvalue of
  'LOAD':begin
         uhelx=20 & uhelz=20 & pos=4 & xr='' & yr='' & zr=''
         pozp=!p.background & barp=!p.color
         naz='' & xnaz='' & ynaz='' & znaz=''
         jakyprofil=0 ;0 pro normal 1 pro surface
         cd,current=currpath

         soubory=make_array(19,1,/string)
         loadbaseh=widget_base(/column)
         loadlab1=widget_label(loadbaseh,value='Times when the profiles were taken must be '+$
                              "contained in the end of the file name (e.g. '082023.fts')!",/align_left)
         loadlab2=widget_label(loadbaseh,value='You are supposed to take files in right order!',/align_left)
         loadbase=widget_base(loadbaseh,/row)
            loadbase1=widget_base(loadbase,/column) & loadbase2=widget_base(loadbase,/column)
            loadbase11=widget_base(loadbase1,/row) & loadbase12=widget_base(loadbase1,/row) & loadbase13=widget_base(loadbase1,/row)
            loadbase14=widget_base(loadbase1,/row) & loadbase15=widget_base(loadbase1,/row) & loadbase16=widget_base(loadbase1,/row)
            loadbase17=widget_base(loadbase1,/row) & loadbase18=widget_base(loadbase1,/row) & loadbase19=widget_base(loadbase1,/row)
            loadbase110=widget_base(loadbase1,/row) & loadbase21=widget_base(loadbase2,/row) & loadbase22=widget_base(loadbase2,/row)
            loadbase23=widget_base(loadbase2,/row) & loadbase24=widget_base(loadbase2,/row) & loadbase25=widget_base(loadbase2,/row)
            loadbase26=widget_base(loadbase2,/row) & loadbase27=widget_base(loadbase2,/row) & loadbase28=widget_base(loadbase2,/row)
            loadbase29=widget_base(loadbase2,/row) & loadbase210=widget_base(loadbase2,/row)

               load1=cw_field(loadbase11,title=' ')
               load2=cw_field(loadbase12,title=' ')
               load3=cw_field(loadbase13,title=' ')
               load4=cw_field(loadbase14,title=' ')
               load5=cw_field(loadbase15,title=' ')
               load6=cw_field(loadbase16,title=' ')
               load7=cw_field(loadbase17,title=' ')
               load8=cw_field(loadbase18,title=' ')
               load9=cw_field(loadbase19,title=' ')
               load10=cw_field(loadbase110,title=' ')
               load11=cw_field(loadbase21,title=' ')
               load12=cw_field(loadbase22,title=' ')
               load13=cw_field(loadbase23,title=' ')
               load14=cw_field(loadbase24,title=' ')
               load15=cw_field(loadbase25,title=' ')
               load16=cw_field(loadbase26,title=' ')
               load17=cw_field(loadbase27,title=' ')
               load18=cw_field(loadbase28,title=' ')
               load19=cw_field(loadbase29,title=' ')
               load20=cw_field(loadbase210,title=' ')

               load11=widget_button(loadbase11,value='LOAD',uvalue='BROWSE1')
               load12=widget_button(loadbase12,value='LOAD',uvalue='BROWSE2')
               load13=widget_button(loadbase13,value='LOAD',uvalue='BROWSE3')
               load14=widget_button(loadbase14,value='LOAD',uvalue='BROWSE4')
               load15=widget_button(loadbase15,value='LOAD',uvalue='BROWSE5')
               load16=widget_button(loadbase16,value='LOAD',uvalue='BROWSE6')
               load17=widget_button(loadbase17,value='LOAD',uvalue='BROWSE7')
               load18=widget_button(loadbase18,value='LOAD',uvalue='BROWSE8')
               load19=widget_button(loadbase19,value='LOAD',uvalue='BROWSE9')
               load110=widget_button(loadbase110,value='LOAD',uvalue='BROWSE10')
               load111=widget_button(loadbase21,value='LOAD',uvalue='BROWSE11')
               load112=widget_button(loadbase22,value='LOAD',uvalue='BROWSE12')
               load113=widget_button(loadbase23,value='LOAD',uvalue='BROWSE13')
               load114=widget_button(loadbase24,value='LOAD',uvalue='BROWSE14')
               load115=widget_button(loadbase25,value='LOAD',uvalue='BROWSE15')
               load116=widget_button(loadbase26,value='LOAD',uvalue='BROWSE16')
               load117=widget_button(loadbase27,value='LOAD',uvalue='BROWSE17')
               load118=widget_button(loadbase28,value='LOAD',uvalue='BROWSE18')
               load119=widget_button(loadbase29,value='LOAD',uvalue='BROWSE19')
               load120=widget_button(loadbase210,value='LOAD',uvalue='BROWSE20')

               quitload=widget_button(loadbase1,value='OK',uvalue='KONEC')
               cancelload=widget_button(loadbase2,value='CANCEL',uvalue='NIC')

            widget_control,loadbase,/realize
            xmanager,'load',loadbase


         end

'RANGE':begin
        rangebase=widget_base(/column)
           xrozsah=cw_field(rangebase,title='X')
           yrozsah=cw_field(rangebase,title='Y')
           zrozsah=cw_field(rangebase,title='Z')
           konecbase=widget_base(rangebase,/row)
           okbut=widget_button(konecbase,value='OK',uvalue='KONEC')
           cancelbut=widget_button(konecbase,value='CANCEL',uvalue='NIC')
        widget_control,rangebase,/realize
        widget_control,xrozsah,set_value=xr & widget_control,yrozsah,set_value=yr
        widget_control,zrozsah,set_value=zr
        xmanager,'range',rangebase
        end

'TITLE':begin
        titlebase=widget_base(/column)
           nazev=cw_field(titlebase,title='Main Title')
           xnazev=cw_field(titlebase,title='X Title')
           ynazev=cw_field(titlebase,title='Y Title')
           znazev=cw_field(titlebase,title='Z Title')
           konecbase=widget_base(titlebase,/row)
           okbut=widget_button(konecbase,value='OK',uvalue='KONEC')
           cancelbut=widget_button(konecbase,value='CANCEL',uvalue='NIC')
        widget_control,titlebase,/realize
        widget_control,nazev,set_value=naz & widget_control,xnazev,set_value=xnaz
        widget_control,ynazev,set_value=ynaz & widget_control,znazev,set_value=znaz
        xmanager,'title',titlebase
        end

 'NORMAL':normal
 'SURFACE':makesurface,1
 'SHADE':makesurface,2


 'WHITE':begin
        pom=!p.color & !p.color=!p.background & !p.background=pom
        if jakyprofil eq 0 then normal
        if jakyprofil eq 1 then makesurface,1
        if jakyprofil eq 2 then makesurface,2
        end
 'IKS':begin
        widget_control,event.id,get_value=uhelx
        erase
        if nahrano eq 1 then begin
           if jakyprofil eq 0 then normal
           if jakyprofil eq 1 then makesurface,1
           if jakyprofil eq 2 then makesurface,2
        endif
        end
 'ZET':begin
        widget_control,event.id,get_value=uhelz
        erase
        if nahrano eq 1 then begin
           if jakyprofil eq 0 then normal
           if jakyprofil eq 1 then makesurface,1
           if jakyprofil eq 2 then makesurface,2
        endif
        end
  'POS':begin
        widget_control,event.id,get_value=pos
        erase
        if nahrano eq 1 then begin
           if jakyprofil eq 0 then normal
           if jakyprofil eq 1 then makesurface,1
           if jakyprofil eq 2 then makesurface,2
        endif
        end
  'SAVE':begin
         savebase=widget_base(/column)
            savelab=widget_label(savebase,value='The image will be saved as name.gif')
            savetext=cw_field(savebase,title='name ')
            konecbase=widget_base(savebase,/row)
            savebutton=widget_button(konecbase,value='OK',uvalue='KONEC')
            cancelbutton=widget_button(konecbase,value='CANCEL',uvalue='NIC')
         widget_control,savebase,/realize
         xmanager,'save',savebase
         end

  'QUIT':begin
         !p.background=pozp & !p.color=barp
         widget_control,event.top,/destroy
         end
endcase
end

pro profiles
COMMON vse,base,file_button2,option_menu,rangebutton,titlebutton,barva,pozadi,uhelx,uhelz,pos
COMMON nazvy,naz,xnaz,ynaz,znaz,nazev,xnazev,ynazev,znazev
COMMON rozsah,xr,yr,zr,xrozsah,yrozsah,zrozsah
COMMON druh,jakyprofil
COMMON uzprof,nahrano
COMMON barva,pozp,barp
COMMON slidery,xslider,zslider,posslider
;COMMON slide
;barva=0 & pozadi=255 & uhelx=20 & uhelz=20 & pos=4 & xr='' & yr='' & zr=''
;!p.background=0 & !p.color=255 & naz='' & xnaz='' & ynaz='' & znaz=''
;jakyprofil=0 ;0 pro normal 1 pro surface

pozp=!p.background & barp=!p.color
nahrano=0
base=widget_base(xsize=700,ysize=600,/row,mbar=bar)
file_menu=widget_button(bar,value='FILE',/menu)
  file_button1=widget_button(file_menu,value='LOAD',uvalue='LOAD')
  file_button2=widget_button(file_menu,value='SAVE',uvalue='SAVE')
  file_button3=widget_button(file_menu,value='QUIT',uvalue='QUIT')
option_menu=widget_button(bar,value='OPTIONS',/menu)
  option_button1=widget_button(option_menu,value='PROFILE',/menu)
     prof_button1=widget_button(option_button1,value='NORMAL',uvalue='NORMAL')
     prof_button2=widget_button(option_button1,value='SURFACE',uvalue='SURFACE')
     prof_button3=widget_button(option_button1,value='SHADE',uvalue='SHADE')
  option_button2=widget_button(option_menu,value='COLOR',/menu)
     ;color_button1=widget_button(option_button2,value='black lines & white background',uvalue='BLACK')
     color_button2=widget_button(option_button2,value='invert colors',uvalue='WHITE')
base2=widget_base(base,/column)
  napis=widget_label(base2,value='PROFILE EVOLUTION')
  okno=widget_draw(base2,xsize=450,ysize=500)
base3=widget_base(base,/column)
   mezera1=widget_base(base3,ysize=20)
   rangebutton=widget_button(base3,value='RANGE',uvalue='RANGE')
   titlebutton=widget_button(base3,value='TITLE',uvalue='TITLE')
   mezera2=widget_base(base3,ysize=250)
   rotatelabel=widget_label(base3,value='ROTATION')
   xslider=widget_slider(base3,title='x',uvalue='IKS',value=20,min=0,max=360)
   zslider=widget_slider(base3,title='z',uvalue='ZET',value=20,min=0,max=360)
   positionlabel=widget_label(base3,value='POSITION')
   posslider=widget_slider(base3,uvalue='POS',value=4,min=2,max=20,/suppress_value)


widget_control,file_button2,sensitive=0
widget_control,option_menu,sensitive=0
widget_control,rangebutton,sensitive=0
widget_control,titlebutton,sensitive=0
widget_control,xslider,sensitive=0
widget_control,zslider,sensitive=0
widget_control,posslider,sensitive=0
widget_control,base,/realize
xmanager,'profiles',base
end