;+
;
; PROJECT:
;	GOES-12 Solar X-ray Imager (SXI)
;
; NAME:
;	SXIG12_READ
;
; PURPOSE:
;	Program to read a single SXI image, either Level-0
;
; EXPLANATION:
;	TBS
;
; CALLING SEQUENCE:
;   result=sxig12_read,filename,header,index=index,fn_struct=fn_struct,  $
;                NO_SCALE=NO_SCALE,OUTPUTSCALING=OUTPUTSCALING
;
; INPUTS:
;	filename        =   name of level-0 FITS file to read (string)
;                       (may inlcude full directory path)
;
; OUTPUTS:
;	image           =   TBD
;	header          =   FITS primary header as a string array
;
; OPTIONAL KEYWORDS:
;	return_index    =   Named variable to contain header converted
;                       into a structure
;   fn_struct       =   Named variable to contain file name converted
;                       into a structure
;   noscale         =   If set, no scaling of the data is done (same
;                       as 'no_scale' keyword in readfits.pro
;   scaleoption     =   LIN:  LINEAR SCALING
;                       LOG:  LOG SCALING
;   hasshdr         =   Named variable to contain HASS extension header
;                       as a string array
;   hassstruct      =   Named variable to contain HASS extension header
;                       converted into a structure
;   hassdata        =   Named variable to contain HASS data
;
;
; EXTERNAL CALLS:
;   TBD
;
; CALLED BY:
;	SXIM_BATCH or operational Python code
;
; METHOD:
;	TBS
;
; HISTORY:
;
;  003.001 Modified by SMH 6/10/2002
;          Updated HASS offsets and fixed code so CRPIX2 is always updated
;          when YCEN is updated
;  003.002 Modified by SMH 6/11/2002
;          Added linear to log and log to linear conversion code
;  003.003 Modified by SMH 6/13/2002
;          Corrected problem that only the index values and not the header
;          values were being updated regarding pointing for old versions
;          of level-0 input files
;  003.004 Modified by JV 6/18/2002
;          Fixed problem with HASS correction version identification
;          Was 003.500, is 003.006
;  003.005 Modified by SMH 6/23/2002
;          Corrected sign and coordinate errors in HASS corrections
;  NEED TO ADD LEVEL-0 HASS EXTENSION READING
;  NEED TO ADD LIN/LOG CONVERSION
;  NEED TO ADD LEVEL-1 READING
;  003.006 Modified by SMH 1/22/2003
;          Added patch for not reading HASS extension on MCPSI products.
;          Should also verify VERLEVL0 value for replacing pointing keywords!!!
;  003.007 Modified by SG 2/11/2003
;          Uses templates and pointers for FITSHEAD2STRUCT calls.
;  003.008 Modified by SMH 2/24/2003
;          1) Added branches to allow command line use without pointers and
;             without templates
;          2) Added check on existance of HASS data to avoid fatal errors
;          3) Updated version check on replacing HASS data - was 4.100,
;             should have been, and now is, 4.006.  This caused no problem
;             because the algorithm in the PP and here produced the same
;             results.
;
;-
function sxig12_read,filename,header,pinfo,index=index,fn_struct=fn_struct,  $
                NOSCALE=NOSCALE,scaleoption=scaleoption,hasshdr=hasshdr, $
                hassstruct=hassstruct,hassdata=hassdata

;STUB for - Check for single file name versus file list
if size(filename,/n_dimensions) gt 0 then begin
endif

fn_struct=sxi_parse_name(filename)

image=readfits(filename,header,exten_no=0,/silent,noscale=noscale)

;;;;;;;;;;;;;;;;;;;;;;;;
; Test for valid image ;
; and image size       ;
;;;;;;;;;;;;;;;;;;;;;;;;

if PTR_VALID(pinfo) then tmplt = (*(*pinfo).lvl0Hdr0)

;Test for no primary data array
if size(image,/n_dimensions) eq 0 then begin

;Confirm READFITS Error

  if image eq -1 and !error_state.msg ne '' then  begin
    close,/all
    return,-1
  endif

;Or look at limiting cases

  if n_elements(header) eq 143 then begin  ;Short file case with full header

    if PTR_VALID(pinfo) then begin
      index=fitshead2struct(header,tmplt)
    endif else begin
      index=fitshead2struct(header)
    endelse

    message,'ERROR: FULL HEADER, SHORT FILE: '+filename,/continue
    close,/all
    return,-1
  endif else begin                         ;Corrupt file case
    message,'ERROR: CORRUPT FILE '+filename,/continue
    close,/all
    return,-1
  endelse

endif

if PTR_VALID(pinfo) then begin
;Level0Hdr0Template,tmplt
  index=fitshead2struct(header,tmplt)
endif else begin
  index=fitshead2struct(header)
endelse

;Check for 2D data array
if (index.NAXIS ne 2) or (n_elements(image) ne 262144) then begin
  message,'ERROR: CORRUPT IMAGE DATA '+filename
  close,/all
  return,-1
endif



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;                                                 ;
;     NORMAL FITS FILE TO DEAL WITH!!! YEAH!!!    ;
;                                                 ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;Need to identify file type (SXI, UVBI, MCPSI, XPDIAG)

;First: Check if TSYNC packet missing!!!

;Second, fix pointing information

;Version Case statement here!!!
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ;                                ;
    ; Fix XCEN and CRPIX1 if Level-0 ;
    ; processing version is older    ;
    ; than 004.100                   ;
    ;                                ;
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

If float(index.VERLEVL0) lt 004.006 and $
	fn_struct.prefixcode ne 'MCPSI' then begin

  hass_array=readfits(filename,hhdr,exten_no=1,/silent)

  if PTR_VALID(pinfo) then begin
    tmplt = (*(*pinfo).lvl0Hdr1)
    hindex=fitshead2struct(hhdr,tmplt)
  endif else begin
    hindex=fitshead2struct(hhdr)
  endelse

  ;COMPUTE HASS AVERAGES HERE IF NECESSARY
  hassy_offset = -44.480
  hassz_offset = +35.865

  if hindex.naxis ne 2 then begin
    message,'ERROR: MISSING HASS DATA '+filename,/continue
    close,/all
    return,-1
  endif

  index.XCEN=3600.0*hindex.HASSYAVG+hassy_offset
  header=sxi_sxmodpar(header,'XCEN',index.XCEN)

  index.YCEN=-3600.0*hindex.HASSZAVG+hassz_offset
  header=sxi_sxmodpar(header,'YCEN',index.YCEN)

  if index.LIN_DSBL eq 0 then begin ;0=false
    if index.EXPTIME ge 0.333 then begin
      index.XCEN = index.XCEN-15.0*0.5*(index.EXPTIME-0.333)
      header=sxi_sxmodpar(header,'XCEN',index.XCEN)
    endif
  endif

  index.CRPIX1=(index.NAXIS1+1)/2.0 - (index.XCEN - index.CRVAL1)/index.CDELT1
  header=sxi_sxmodpar(header,'CRPIX1',index.CRPIX1)

  index.CRPIX2=(index.NAXIS2+1)/2.0 - (index.YCEN - index.CRVAL2)/index.CDELT2
  header=sxi_sxmodpar(header,'CRPIX2',index.CRPIX2)

  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  ;                             ;
  ; Fix CROTA if Level-0        ;
  ; processing version is older ;
  ; than 003.006                ;
  ;                             ;
  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

  If float(index.VERLEVL0) lt 003.006 then begin
    index.CROTA = -index.CROTA
    header=sxi_sxmodpar(header,'CROTA',index.CROTA)
  endif

   ;Note: The only images that need to have their data rotated are those
   ;      taken with no voltage on the MCP before 2001-08-28 23:00:00 UT

endif

;Default is to not change scaling
if keyword_set(scaleoption) then scaleoption=scaleoption $
   else scaleoption=index.LIN_LOG

;NEED THE LOG2LIN code to add a keyword indicating the change in scaling
;DOES THIS BELONG IN THE DATABASE?

if scaleoption eq 'LIN' and index.LIN_LOG eq 'LOG' then begin
  image=5.16015+5.04269*10.^(0.0019379*image)
endif

if scaleoption eq 'LOG' and index.LIN_LOG eq 'LIN' then begin
  image=516.015*ALOG10(0.198306*image-1.02329)
endif

return,image

end