pro linetime, sum, exposure, conv, comp, btsm, cdsr, lline, time, $
               ratio=ratio, rdind=rdind, camera=camera, roo=roo

;; Compute the time from light-flood erase to read out for a given image line.
;;
;; INPUTS:
;;	'sum' is the summation mode FULL, SUM2 or SUM4
;;	'exposure' is the exposure time in real seconds
;;	'conv' is conversion, 12BIT, TABLE, or 8LSB
;;	'comp' is NOTCOMP, LOSSLESS, LOSSY
;;	'btsm' is botsim (this is second image of botsim), 0 (no) or not 0 (yes)
;;	'cdsr' is the telemetry rate and is 8,16,24,32,40,or 48 packets per sec
;;	'lline' should range from 1 to 1024
;;
;;	'ratio' is compression ratio for current image
;;	'rdind' is readout index (needed for botsim wait)
;;	'camera'is NAC or WAC
;;      'roo'   is read out order (0=NAC first, 1=WAC first)
;;	if the last two are not given, assume camera is NAC and roo=0
;;      (biu_swap will be wrong if camera=WAC and roo=0)
;;
;;
;; OUTPUT: 'time' is the real seconds that 'lline' spends on the ccd

; This code originally derived from the Yagi/Girard 'linetime' subroutine which
; is from their 'PT' program

; Changes made were to include readout rate model from ITL and EGSE tests
; which include rates, BIU line break estimate and proper treatment of pads.
; BIU line break timing assumes shutter close at 0.461 into a second which
; occurs for images that start at the OFFSET_TIME given and are not delayed
; by previous images or by initial commands (e.g. commands before an OFFSET=0
; image.)  The BIU swap occurs during two rtis.  The readout rate into image
; buffer dependence vs. telemetry rate is not fully understood and is due to
; be updated.  Readout rates and BIU line for LOSSLESS assumes constant
; compression ratio across image.  Thus if the top part of a frame has a much
; higher compression ratio than the frame average, the actual line when the
; BIU swap occurs will be later than this routine predicts.  A more accurate
; method would be to analyze the scene itself.
; - V. Haemmerle Latest update: 3/11/2004
; Update: Use round when calculating readout window size (6/19/04)
; Update: Some small corrections, when r2>r1 and btsm line_break (5/13/05)
; NOTE: sections marked with !!!!! are for possible future updates

   nlines = [1024,512,256]
   if (sum eq 'FULL') then sindex = 0 $
   else if (sum eq 'SUM2') then sindex = 1 $
   else if (sum eq 'SUM4') then sindex = 2

   tlm = cdsr/8
   t0 = exposure + .0004 + .0086		;time from erase to first line
   t0 = t0 + 0.68*(nlines[sindex] - lline)/float(nlines[sindex])
   					;time due to 680ms erase
   line = lline-1 			;line is from 0 to 1023
   if line lt 0 then line = 0

   ; Lossy mode streams CCD to Hardware compressor
   ; Flight software does not read CCD line-by-line
   ; Rate does not depend on conversion mode (TABLE or 8LSB)
   ; Lossy streams even during BIU swap, so there is no BIU wait
   ; Rates below were measured in EGSE and ITL tests, they did
   ; not depend on compression ratio
   if comp eq 'LOSSY' then begin
     r1 = [89.864, 110.248, 203.984]
     time = t0 + line/r1[sindex]
     return
   endif

   if (conv EQ '12BIT') then cindex = 0 $
   else cindex = 1
   if (not keyword_set(ratio)) then ratio = 2.
   if (comp eq 'NOTCOMP') then ratio = 1.


   ;!!!!!!  THIS PART SHOULD BE UPDATED WHEN BETTER INFO EXISTS !!!!!
   ; Linear fit to readout rates at 48 pps (ITL test)
   ; Fit is readout lines per second vs. line blocksize in words
   ; Fit includes Losslessly compressed and Not compressed modes
   ; Not compressed modes for 2x2 and 4x4 taken at 24pps (EGSE test) and
   ;     divided by 1.0085 to compensate for packet rate
   ;     0.85% rate determined from 0 vs 24 vs 48 pps test at 1x1 modes
   ; RMS of fit is 0.99,   0.26,    0.48 lines per second
   ; ro_rate0 = [77.895, 94.443, 156.430]
   ; slope = [-0.0109800, -0.0172194, -0.0485036]
   data = [16.,8.]
   blocksize = nlines[sindex]*data[cindex]/16./ratio
   ;!!!!!!

   case sum of
   'FULL': begin

     if comp eq 'NOTCOMP' then begin
       ; Not compressed values used are from ITL test at 48pps
       ro_rate0 = [67.443, 71.946]
       ro_ratefit = ro_rate0[cindex]
     endif else begin
       ; Summation 1x1 lossless has better fit vs compression rate
       ; Fit is readout lines per second vs. compression ratio
       ; Different fits for 12BIT and CONVERSION at 48pps
       ; Compression ratio range is ~2 to ~5 for 12bit, ~1 to ~5 for 8bit
       ; RMS of fit is 0.27,  0.19 lines per second
       ro_rate0 = [67.440, 74.861]
       slope = [1.77299, 0.488212]
       ro_ratefit = ro_rate0[cindex] + slope[cindex]*ratio
     endelse
   end

   'SUM2': begin
      ro_ratefit =  94.443 -0.0172194*blocksize
   end

   'SUM4': begin
      ro_ratefit = 156.430 -0.0485036*blocksize
   end

   else: return
   endcase

   ;!!!!!!  THIS PART SHOULD BE UPDATED WHEN BETTER INFO EXISTS !!!!!
   ; Convert rate to image's number of packets per second and for no science
   ; packets. Here ITL and EGSE data is less clear. In non-compressed mode,
   ; rate is linear with pps, none is 1.7% faster than 48pps
   if (comp EQ 'NOTCOMP') then begin
      r1 = ro_ratefit * (1. + (.017)*((48.-cdsr)/48.))
      r0 = ro_ratefit * 1.017
   endif else begin
   ; With Lossless mode, it appears rate is faster at faster pickup rate
   ; comparing 24pps to 48pps but with much scatter. Using average ratio of
   ; all rate changes 0.9956 = 0.44% faster (.87% for 48pps to 0)
   ; No data for no science rate, using average change extrapolated to 0pps
      r1 = ro_ratefit * (1. - (.0087)*((48.-cdsr)/48.))
      r0 = ro_ratefit * 0.9913
   endelse
   ;!!!!!!

   ; Calculation of BIU swap line which occurs upon completion of first packet
   ; If one or more complete lines can fit into the first packet of 440 words,
   ; they are moved from the Image Buffer allowing more to be read from the CCD
   ; before the BIU pause.
   ; Note: must also account for 4-word line header on each line
   tratio = ratio
   if (comp eq 'LOSSLESS' and tratio lt 2.) then tratio = 2.
   biu_line = 440/(4+long(nlines[sindex]*data[cindex]/16/tratio)) + 1

   ; if camera is opposite of read_out_order (second)
   ; Calculate number of lines read in early pad of 0.262 seconds
   ; BIU swap occurs after these number of lines or when
   ; first science packet is complete (at biu_line) which
   ; ever is greater
   second = 0
   if (n_elements(camera) ne 0 and n_elements(roo) ne 0) then begin
       if (camera eq 'NAC' and roo eq 1) then second = 1
       if (camera eq 'WAC' and roo eq 0) then second = 1
   endif
   ; First line after biu wait is at 0.289 seconds
   biutime = 0.289
   if (second eq 1 and btsm eq 0) then begin
       early_lines = fix(0.262*r0) + 1
       if (early_lines GT biu_line) then biu_line = early_lines
     ; If there is 0.262 pad before readout window (i.e. second image)
     ; then biu swap occurs 2 rti later (0.25 sec)
       biutime = 0.539
   endif
   
   if sum ne 'FULL' then begin
       rate = r1
       if btsm ne 0 then rate = r0
       if (btsm eq 0 and line ge biu_line) then $
         time = t0 + biutime + (line-biu_line)/rate $
       else time = t0 + line/rate
       return
   endif

   ; Only FULL images can fill image buffer
   r2 = 3.59733*tlm             ; ITL measured
   if (conv ne '12BIT') then r2 = r2 * 2.
   ; r2 depends on compression ratio: r2*tratio, but not faster than r1
   if (comp eq 'LOSSLESS') then r2 = min([r2*tratio,r1])
   if (conv eq '12BIT') then buffer = 335 $
   else buffer = 671
   ; Stores 2 compressed lines into one
   if (comp eq 'LOSSLESS') then buffer = 2*buffer

   ; Now treat more complicated 1x1 case.
   if btsm eq 0 then begin

     ; Calculate line break
     ; OLD:  break = [355,377,401,429,460,497]
     ;       if (cindex eq 1) then break = [748,837,954,1024,1024,1024]
       if (r2 ge r1) then line_break = 1024 $
       else line_break = biu_line + fix(buffer*r1/(r1-r2)) + 1
       time = t0 + line/r1
       if line ge biu_line and line lt line_break then $
         time = t0 + biutime + (line-biu_line)/r1
       if line ge line_break then $
         time = t0 + biutime + (line_break-biu_line)/r1 + (line-line_break)/r2
       
   endif else begin

     ; t1 is amount of time botsim image waits for first image readout window
     ; t1 only depends on readout index
     ; and telem rate: t1 is first camera readout window plus pad
     ; plus biu swap, second image does not show biu delay in image
     readout = [50,25,14,6]
     t1 = round(readout[rdind/4]*(6./float(tlm))) + 0.539
     time = t0 + line/r0
     line_break = buffer + biu_line ; Full buffer

     ; NOTCOMP 12BIT always stops and waits when buffer filled
     if (conv eq '12BIT' and comp eq 'NOTCOMP') then begin
         if (line gt line_break) then $
           time = t0 + t1 + (line-line_break)/r2
         return
     endif

     ; Line at which transmission starts
     trans_line = fix((t1-0.25)*r0) + 1

     ; NOTCOMP TABLE/8LSB may start reading out before buffer is filled
     ; If buffer is filled first, rest is read out at r2
     ; If t0+t1 occurs first then read continues at r1 until filled, then at r2
     if (conv ne '12BIT' and comp eq 'NOTCOMP') then begin
         if (trans_line ge line_break) then begin
             if (line ge line_break) then $
               time = t0 + t1 + (line-line_break)/r2 ; waits
         endif else begin
             if (r2 ge r1) then line_break = 1024 $
             else line_break = trans_line + fix((line_break-trans_line)*r1/(r1-r2)) + 1
             if (line ge trans_line) then $
               time = t0 + trans_line/r0 + (line-trans_line)/r1 + 0.25
             if (line ge line_break) then $
               time = t0 + trans_line/r0 + (line_break-trans_line)/r1 + $
               (line-line_break)/r2 + 0.25
         endelse
         return
     endif
     
     ; LOSSLESS 12BIT may start reading out before buffer is filled
     ; If buffer is filled first, rest is read out at r2
     ; If t0+t1 occurs first then read continues at r1 until filled, then at r2
     if (conv eq '12BIT' and comp eq 'LOSSLESS') then begin
         if (trans_line ge line_break) then begin
             if (line ge line_break) then $
               time = t0 + t1 + (line-line_break)/r2 ; waits
         endif else begin
             if (r2 ge r1) then line_break = 1024 $
             else line_break = trans_line + fix((line_break-trans_line)*r1/(r1-r2)) + 1
             if (line ge trans_line) then $
               time = t0 + trans_line/r0 + (line-trans_line)/r1 + 0.25
             if (line ge line_break) then $
               time = t0 + trans_line/r0 + (line_break-trans_line)/r1 + $
               (line-line_break)/r2 + 0.25
         endelse
         return
     endif

     ; LOSSLESS with 8LSB or TABLE fits in image memory
     if (conv ne '12BIT' and comp eq 'LOSSLESS' and line gt trans_line) then begin
       time = t0 + trans_line/r0 + (line-trans_line)/r1 + 0.25
     endif

   endelse

end
