pro ssw_gevloc_dbase, time0, time1, instrument=instrument, $
   dbase_dir=dbase_dir,_extra=_extra, debug=debug

common ssw_gevloc_dbase_blk, strtemp

debug=keyword_set(debug)

if not data_chk(strtemp,/struct) then begin 
   gevloc=get_gevloc_data()
   gevloc.helio=''   
   strtemp=gevloc(0)
endif

sswdb=str_replace(get_logenv('SSW'),'solarsoft','sdb')
if not keyword_set(dbasedir) then dbasedir=concat_dir(sswdb,'gevloc')
if n_elements(instrument) eq 0 then instrument='eit'

mgrid=timegrid(anytim(time0,/ecs), $
   reltime(time1,days=31,out='ecs'),/month,out='ecs')   ; monthly dbase files 
nm=n_elements(mgrid)

fdcmd=instrument+'_closest_fd,ts,tp,fbefore,fafter,status=status,_extra=_extra,/local'

catdir=concat_dir(dbasedir,instrument + '_genxcat')

for m=0,nm-2 do begin
   gev=get_gev(mgrid(m),mgrid(m+1),count=count)
   box_message,'Doing range: ' + arr2str([mgrid(m),mgrid(m+1)],' - ')
   if count gt 0 then begin 
       gevrecs=replicate(strtemp,count)
       gevrecs.date_obs=anytim(gev,/ccsds)       
       decode_gev,gev,fstart,fstop,fpeak,class=class,out='ecs'
       gevrecs.fstart=fstart
       gevrecs.fstop=fstop
       gevrecs.fpeak=fpeak
       gevrecs.class=class
       gevrecs.ename='gev_'+time2file(gevrecs.date_obs)
       gevdirs=concat_dir(dbasedir,gevrecs.ename)
       for e=0,count-1 do begin          ; for each event
          ts=gevrecs(e).fstart
          tp=gevrecs(e).fpeak
          status=0               ; initiate find status to 'failure'

          estat=execute(fdcmd)   ; execute instrument find command
          if debug then stop,'post execute'
          if estat then begin
             if status then begin 
                if not file_exist(gevdirs(e)) then $
                    spawn,['mkdir',gevdirs(e)],/noshell
                    gevrecs(e).lfiles=arr2str(ssw_strsplit([fbefore,fafter], $
                                '/',/tail))
                    read_eit,[fbefore,fafter],index,data
                    cdelt1=gt_tagval(index,/cdelt1,missing=0.0)
                    cdbad=where(cdelt1 eq 0,bcnt)
                    if bcnt gt 0 then begin 
                       box_message,'At least one bad cdelt found...
                       index(cdbad).cdelt1=1024./index(cdbad).naxis1*eit_pixsize()
                       index(cdbad).cdelt2=index(cdbad).cdelt1
                    endif
                    eit_prep,index,data=data,oindex,odata
                    pnames='eitp_'+string(oindex.wavelnth,format='(I3.3)')+ $
                       '_'+time2file(oindex)+'_'+ $
                       string(oindex.naxis2,format='(I4.4)')+'.fits'
                    pnames=concat_dir(gevdirs(e),pnames)
                    mwritefits,oindex(1),fix(round(odata(*,*,1))),outfile=pnames(1)
              if abs(ssw_deltat(oindex,/min)) gt 24 then begin 
                    ssw_register,oindex,odata,index,data,/derotate,ref_index=1
              endif else begin
                 box_message,'dT "close enough"..., not registering'
                 index=temporary(oindex)
                 data=temporary(odata)
              endelse
              xycen=ssw_flare_locator(index,data,oindex,ldata=ldata,$
                flare_helio=flare_helio,_extra=_extra,$
                limb_pad=50, dark_flare=dark_flare, $
                 smooth=round(25./(index(0).cdelt1) ), nofill=nofill)
              dname=str_replace(last_nelem(pnames),'eitp','eitdiff')
              mwritefits,last_nelem(oindex),fix(round(ldata)),outfile=dname
              fhns=flare_helio(0) & fhew=flare_helio(1)
               hstring=$
                (['S','N'])(fhns gt 0)+ string(abs(fhns),format='(i2.2)') + $
                (['E','W'])(fhew gt 0)+ string(abs(fhew),format='(i2.2)')

              gevrecs(e).helio=hstring
              gevrecs(e).xcen=xycen(0)
              gevrecs(e).ycen=xycen(1) 
                    if debug then stop,'post writefits'
             endif else box_message,'No valid before/after pair found
          endif else box_message,'Problem with execute command'
       endfor
       ssok=where(gevrecs.helio ne '',okcnt)
       if okcnt gt 0 then begin
          write_genxcat,gevrecs(ssok),prefix=instrument+'_gevloc_',  $
             /day_round,/n_elem,catdir=catdir,/geny
       endif else box_message,'No location solutions for month(!) ' + mgrid(m)

   endif else box_message,'Zero GEV records for month(!) '+mgrid(m)

endfor


; get GEV dbase

return
end

