pro sotsp_reform,index,data,rindex,rdata, $
   scan_info=scan_info, status=status, only_maps=only_maps, concat=concat, $
   debug=debug, error=error, slitmap=slitmap
;+
;   Name: sotsp_reform
;
;   Purpose: reform SOT-SP repeted maps -> n2D "index,data", deals with
;   duplicate and missing scan columns
;
;   Input Parameters:
;      index - L1/L2 index (read_sotsp output)
;      data - L1/L2 data (readd_sotsp output)
;
;   Keyword Parameters:
;      scan_info - TIMES/SLITPOS etc structure (keyword output from read_sotsp)
;      status (output) - return "true" if data are 3D, i.e., if data contain
;                        more than one slit potion and multiple repeats
;      only_maps - vector of desired maps (default=all ~36/38)
;      concat - if set, concatenate maps in x direction / removes channel dim
;      error (output) - return "true" if map comprises only one slit position
;      slitmap (output) - returns slitmap generaed by sotsp_rasterize.pro
;
;   Calling Sequence:
;      sotsp_reform, index, data , oindex, odata [,scan_info=scan_info]
;      sotsp_reform, ii,dd, oii, odd, only_maps=<map subscripts> [,/concat]
;
;   Calling Example/Context:
;      read_sotsp,<l2file>, ii,dd,scan_info=si
;      sotsp_reform, ii, dd, oii, odd ,only_maps=[33,0,1,2,3] ,/concat, scan=si
;      help, dd, odd & print,oii.ftype
;  DD              FLOAT     = Array[4156, 256, 36] ; << in
;  ODD             FLOAT     = Array[65, 256, 319]  ; << out 
;  Magnitude of Stokes V,Magnetic Field,Magnetic Field Inclination,Magnetic Field Azimuth,Doppler Shift of 630.15nm
;
;   History:
;      3-Jun-2008 - S.L.Freeland - SOT-SP ssw helper utility
;      3-Sep-2020 - M.DeRosa - now uses sotsp_rasterize.pro to determine
;                              raster and identify duplicate/missing columns
;     24-Nov-2020 - M.DeRosa - fix bug when reforming output array for
;                              sit-and-stare observations when /concat switch
;                              is set (a bug that was inadvertantly introduced
;                              when sotsp_rasterize was added, I believe)
;     20-Dec-2021 - M.DeRosa - fixed issue with status output variable, which
;                              should only be 1 if data are 3D, i.e., if data
;                              contain more than one slit position and multiple
;                              repeats (this was the original behavior of this
;                              routine prior to the 3-Sep-2020 modifications);
;                              also removed commented-out blocks
;     22-Dec-2021 - M.DeRosa - added slitmap keyword for convenience
;-

status=0
debug=keyword_set(debug)

if not data_chk(scan_info,/struct) then begin 
   box_message,'Single map/scan - no action currently taken'
   return
endif

;  get slitmap
slitmap=sotsp_rasterize(scan_info)

;  create output array
if size(slitmap,/n_dim) eq 1 then nmaps=1 else nmaps=(size(slitmap,/dim))(1)
ny=data_chk(data,/ny)
nx=(size(slitmap,/dim))(0)
nim=data_chk(data,/nimages)
rdata=make_array(dim=[nx,ny,nim,nmaps],type=size(data,/type))
error=nx eq 1  ;  single slit position if nx=1
if (nx gt 1) and (nmaps gt 1) then status=1  ;  set status flag

;  determine value for missing data
missing=make_array(dim=[nim],type=size(data,/type))
for ii=0,nim-1 do missing(ii)=median(data(*,*,ii))
missing=rebin(reform(missing,[1,nim],/overwrite),[ny,nim])

;  put data in output array
for jj=0,nmaps-1 do for ii=0,nx-1 do begin  ;  loop over all rows/columns
  if slitmap(ii,jj) ge 0 then rdata(ii,*,*,jj)=data(slitmap(ii,jj),*,*) else rdata(ii,*,*,jj)=missing
endfor
rindex=index

;  only_maps keyword selects particular variables
if keyword_set(only_maps) then begin 
  rindex=rindex(only_maps)
  rdata=temporary(rdata(*,*,only_maps,*))
endif

;  concat keyword concatenates variables
if keyword_set(concat) then begin 
  alltypes=arr2str(rindex.ftype)
  nf=(size(rdata))(3)
  dx=data_chk(rdata,/nx)
  dy=data_chk(rdata,/ny)
  ni=(size(rdata))(4)
  retval=make_array(nf*dx,dy,ni,type=data_chk(rdata,/type))
  for ii=0l,nf-1 do retval(ii*nx,0,0)=reform(rdata(*,*,ii,*),[dx,dy,ni])
  delvarx,rdata
  rdata=retval
  rindex=rindex(0)
  rindex.ftype=alltypes
endif

if debug then stop

end 
