;+
; NAME: autoregister
; PURPOSE:
; Automagically register a bunch of images.
;
;USAGE:
; This accepts either an image stack datacube or an array of pointers to
; images, and registers and mosaics them together via cross correlation.
; This is mostly just a convenient driver function for subreg and mosf,
; in other words.
;
; INPUTS:
; array either an array of images, or a ptrarray pointing to
; a bunch of images of different size. if the latter,
; autoregister pads as necessary, then registers.
; KEYWORDS:
; /nantozero set NaNs to 0 before registering. This saves a lot of time
; versus running fixpix in subreg.
; /justpad Given an ptrarry, just pad them and concatenate into
; one variable, returned as 'padded'
;
; padded= output variable for padded array
; mosaic= output variable for the mosaiced sum
;
; shifts= use these shifts instead of registering
;
; use _extra to pass keywords to subreg or mosf.
;
; OUTPUTS:
;
; HISTORY:
; Began 2005-04-08 00:33:48 by Marshall Perrin
; 2005-06-30: Subpixel registration now the default
; 2006-04-27: Can now pass shifts in as an argument
;-
PRO autoregister,array,arrayout,expmap=expmap,_extra=_extra,refimage=refimage,$
nantozero=nantozero,mosaic=mosaic,exptimes=exptimes,padded=ar,justpad=justpad,$
subpixel=subpixel,shifts=shifts,px=px,py=py
if n_elements(subpixel) eq 0 then subpixel =1 ; default is now subpixel
if size(array,/type) eq 10 then begin
message,/info,"Passed an array of pointers to images; padding as needed."
ni = n_elements(array)
sizes = fltarr(6,ni)
for i=0,ni-1 do sizes[0,i] = size(*array[i])
ar = fltarr(max(sizes[1,*]),max(sizes[2,*]),ni)+!values.f_nan
for i=0,ni-1 do ar[0:sizes[1,i]-1,0:sizes[2,i]-1,i] = *array[i]
endif else begin
ar = array
endelse
if keyword_set(justpad) then return
if keyword_set(nantozero) then begin
wnan = where(~finite(ar),nanct)
masks = finite(ar)
if nanct gt 0 then ar[wnan]=0
endif
if ~(keyword_set(refimage)) then refimage = 0
if ~(keyword_set(shifts)) and ~(keyword_set(px)) and ~(keyword_set(py)) then subreg,ar[*,*,refimage],ar,shifts,_extra=_extra
;plot,shifts[0,*],shifts[1,*],psym=-2
;stop
; use PX and PY if they're passed in.
if ~keyword_set(px) or ~keyword_set(py) then begin
subreg_shiftstopeaks,ar,shifts,px,py,ref=ar[*,*,refimage]
endif
mosf,ar,px,py,mosaic,expmap,pieces=arrayout,exptimes=exptimes,subpixel=subpixel,$
masks=masks,_extra=_extra
end