;Hi Sato-san,
;This file will read in the data file for the 13 October 1995 Cal Mode flare
; and correct the data for channel overflow.  It should work for any future
;event with channel overflow.
;I'm putting this file into my schwartz ucon for future use 


;+
; PROJECT:
;	YOHKOH
; NAME:  HXT_CAL_FIX
;
;
; PURPOSE: 
;	This procedure reads the complete HXT  calibration data file, 
;	corrects for possible counter overflow, and returns the 
;	flash analyzer relative widths from the database files.
;
;
; CATEGORY: YOHKOH/HXT, INSTRUMENT
;
;
; CALLING SEQUENCE: HXT_CAL_FIX,   File, Ut,  Dout,  Flux,  Dt
;
; CALLED BY:
;
;
; CALLS:
;	LOC_FILE, BREAK_PATH, AVE_CTS2, ANYTIM, MAP_MATRIX, FCHECK
;	CHECKVAR, RD_XDA, RD_ROADMAP, GET_HXT_CAL_WIDTHS, EDGE_PRODUCTS
;
;  INPUTS:
;	File:  filename, defaults to CaL flare of 13-oct-95
;
; OUTPUTS:
;      	Ut - Center time of each 8 sec accumulation in int. structure 
;		Yohkoh format
;	Flux - Counts/sec/detector for selected detectors rebinned to standard edges
;	by correcting for PHA non-linearity and gain offset
;	Dt  - Time in seconds of accumulation interval
; OPTIONAL OUTPUTS:
;	EDGES- Standard HXT cal edges
;	IOUT - YOHKOH index structures for hxt cal data
;	DOUT - overflow corrected counts in each bin 64chans X 64sensors X Ntimebins, Fix
; COMMON BLOCKS:
;	hxt_cal_fix	
;
; SIDE EFFECTS:
;	none
;
; RESTRICTIONS:
;	none
;
; PROCEDURE:
;	Read the roadmap, find the CAL records, read the datafile for the CAL
; 	records, correct overflow, rebin to standard bins.	
;
; MODIFICATION HISTORY: 
;	ras, 31-jan-1996
;	ras, 28-apr-1996, finished rebin corrections
; CONTACT:
;	richard.schwartz@gsfc.nasa.gov
;-



pro  hxt_cal_fix, File,   Ut, Dt, Flux, SEL_DET=SEL_DET,$
	WADC=WADC,  CAL=CAL, EDGES=EDGES, $
	IOUT=IOUT,  DOUT=DOUT


common hxt_cal_fix, use_det
use_det = fcheck( sel_det, fcheck( use_det, indgen(64)))
sel_det=use_det

edges = (10.1 + findgen(65)) * 1.252
edgess=edges

edge_products, edges, edges_2=edges, width=wedge


checkvar,  file,  loc_file('hda951013.0410', path=data_paths())

if not exist(cal) then begin
	rd_roadmap, file, rdmp

	cal=where(rdmp.datarectypes eq 2, ncal)
endif else ncal = n_elements( cal )

rd_xda, file, cal, index, data
flux = ave_cts2( index, data,  time=ut, dt=dt,  datarectypes= 2+ intarr(ncal), $
	iout64=iout, dout64=dout)
ut = anytim( anytim( index(0), /sec ) +ut, /ints) 


;dout are the counts in 64 channels x 64 detectors x Ntime corrected for overflow

;Get the relative channel widths for each channel for each sensor
wadc = get_hxt_cal_widths()
wadcs= wadc
;Correct  to counts/regular bin

;dout=dout / rebin( reform(wadc,64,64,1), 64, 64,  n_elements(dout(0,0,*)))

wlast = indgen(10)+n_elements(dout(0,0,*))-10
avg_last = avg(total(total(dout(*,*,wlast),1),1))
y=total(total(dout,1),1)

wuse = where( abs(y-avg_last)/avg_last le .10, nuse)

sout=rebin( dout(*,*,wuse), 64,64,1)/ wadc

;dout=dout * rebin( reform(wadc,64,64,1), 64, 64,  n_elements(dout(0,0,*)))

iwadc=fltarr(65,64)
for i=1,64 do iwadc(i,*) = iwadc(i-1,*)+wadc(i-1,*)
imwadc=.5*(iwadc(0:63,*)+iwadc(1:*,*))
ag=fltarr(6,64)
w=indgen(35)+25
edges64=fltarr(2,64,64)
imap = replicate( {imap_edge, n:0L, w:intarr(256), val:fltarr(256) }, 64)

;dout_sav = dout
;cal_line = fltarr(64)
for i=0,63 do begin 
	yfit=gaussfit((iwadc(w,i)+iwadc(w+1,i))/2. ,sout(w,i),apar)
	ag(0,i) = apar
	edge_products, edges_2= e64, 1.252*(iwadc(*,i)*(37.5/ag(1,i))+10.1)
	edges64(*,*,i) = e64
	map_matrix, e64, fltarr(64)+1., edges, map
	imap(i).w =where( map ne 0.0, nmap)
	imap(i).n=nmap
	imap(i).val = map(imap(i).w(0:nmap-1))
	dout(*,i,*) = map # reform(dout(*,i,*))
endfor

;        out = f_div( total(dout,2), rebin(reform(dt_out,1,n_data), 64, n_data) )/64.
n_data = n_elements( dt )
flux = f_div( total( dout(*,use_det,*),2), rebin( reform( dt, 1,n_data), $
	64, n_data) ) / n_elements(use_det)


end
