pro mmap1pos, serial, pos, m1, m2, trans, laspos, image, obs, $
	s1, s2, amp1, amp2, phase1, phase2, offset1, offset2, c1, c2, $
	m1sig, m2sig, Tsig, $
	save=save, sub10=sub10, summary=summary, $
	sum_arr=sum_arr, uncert=uncert, $
	progver=progver, qstop=qstop, $
	contrast_save=contrast_save, head=head, $
	part1_save=part1_save, part2_save=part2_save, $
	all_heads=all_heads, $

	nimg=nimg, img_shift=img_shift, m1img_avoid=m1img_avoid, m2img_avoid=m2img_avoid, $
	imgnum=image_arr, nodefringe=nodefringe, $
	used_imgnum=imgnum
;
;SAMPLE CALLING SEQUENCE:
;	mmap1pos, 620, 0, m1, m2, trans
;	mmap1pos, '/data5/optest/log/log/sci160k/940121_150107', 0, nimg=27, img_shift=1
;	mmap1pos, '/data5/optest/log/log/sci160k/940121_150107', 0, nimg=27, img_shift=indgen(27)<1	;shift 0 for first, and 1 for all others
;
;	
;
;
;Nominal is position   #0 (center) 	=   0- 27
;			1		=  27- 53
;			2		=  54
;			3		=  81
;			4		= 108
;			5		= 135
;			6		= 162
;			7		= 189
;			8		= 216-242
;
;For 10x10 x9 positions, the following applies
;			Position 0 	= 0
;			Position 1	= 107
;			Position 2	= 214
;For the 0-106, there is 1 laser, 4 nominal, 2 dark and then 10x10.
;
;HISTORY:
;	Written Jan-94 by M.Morrison
;V2.0	 3-Feb-94 (MDM) - Modified to save the results as FITS files
;			  and to track the version number
;			- Rotate the images for series 618-713 because
;			  amplifier B was being used
;V2.01	 7-Feb-94 (MDM) - Expanded to handle 24 image series 575-586
;	10-Feb-94 (MDM) - Added /QSTOP option
;V2.02	15-Feb-94 (MDM) - Added /CONTRAST_SAVE option to save the contast
;			  images.
;			- Added OFFSET1, OFFSET2, C1 and C2 as output
;			  for DO1POS
;			- Added HEAD as output of DO1POS
;			- Modified to handle series 504 = 10x10 solar DTMAP
;V2.03	17-Feb-94 (MDM) - Modified to handle series CAT274
;V2.04	18-Feb-94 (MDM) - Modified to handle series 588 = 10x10 solar DTMAP
;V2.05	18-Feb-94 (MDM) - Modified to optionally save AMP, OFFSET and PHASE
;			- Changed the way SAVE_DO1POS worked quite a bit
;V2.06	18-Feb-94 (MDM) - Corrected error instroduced in V2.05 which was
;			  not saving the laser positions 
;V2.07	22-Feb-94 (MDM) - Modified to handle series 509 (solar DTMAP)
;V2.08	23-Feb-94 (MDM) - Modified how it recognized the 10x10 "big" scans
;V3.00	 4-Mar-94 (MDM) - **** Renamed from DO1POS to MMAP1POS
;			- Expanded to be able to process telemetry and
;			  microvax files
;                       - Expanded to return and save the sigma on the 
;			  M1 and M2 fits (image
;V3.10	 8-Mar-94 (MDM) - Expanded to handle raw telemtry files
;V3.11	 6-Apr-94 (MDM) - Added "all_heads" optional keyword output
;V3.12	12-Apr-94 (MDM) - Added "all_heads" passed to MAPLASER
;V3.13	14-Jun-94 (MDM) - Added case to handle APR911
;V3.14	15-Jun-94 (MDM) - Added "used_imgnum" output keyword for the
;			  images selected
;			- Added Tsigma as output
;V3.15	 1-Sep-94 (MDM) - Allowed 22 image series of Apr-94 to work
;			  automatically
;V3.16	 6-Oct-94 (MDM) - Modified to work on 10x10 oct548 thru oct555
;			  series
;V3.17	 4-Aug-95 (MDM) - Modified to work with raw telemetry for the 10x10 detune
;			  series
;-
;
if (keyword_set(progver)) then begin
    serial = '3.170'
    return
end
;
if (data_type(serial) eq 7) then begin		;string type ==> telemetry file
    if (n_elements(nimg) eq 0) then nimg = 27		;assume a 27 image series
    imgnum = indgen(nimg) + pos*nimg
    if (nimg eq 21) then begin
	if (n_elements(sub10) eq 0) then sub10 = 0
	imgnum = [103, indgen(10) + 10*sub10+2, indgen(10)*10+sub10+2]	;MDM added 22-Feb-94
    end
    if (keyword_set(img_shift)) then imgnum = imgnum + img_shift
    qsub10 = (n_elements(sub10) ne 0)
    q900 = 0	;TODO - check for number of images in the telemetry file

end else begin
    read_mdi, serial, dummy, ff, /list_files
    ;
    nfiles = n_elements(ff)
    nimg = 29						;simple laser scan - 29 or 30 images in the directory
    if (nfiles eq 22) then nimg=22				;MDM added 1-Sep-94
    if (nfiles gt 50) then nimg=27				;27 * 9 p positions = 243 images or 10x10*9 = 900 plus images
    if (nfiles ge 100) and (nfiles le 105) then nimg = 21	;10x10 solar or laser dtune
    if (nfiles ge 40) and (nfiles le 44) then nimg=22	;opt241-opt272 series
    if (serial ge 575) and (serial le 586) then nimg = 24
    if (serial eq 904) then nimg = 22		;MDM added 2-Sep-94
    q900 = (nfiles gt 400)				 	;is it a 10x10x9 series
    qsub10 = (n_elements(sub10) ne 0)
    if (q900) then if (n_elements(sub10) eq 0) then sub10 = 0
    if (nimg eq 29) and (serial ne 624) then begin
	if (n_elements(ff) ne 29) and (not q900) then begin
	    print, n_elements(ff), ' files for a 29 image series.  Aborting
	    return	;TODO - a few special cases need to be handled
        end
    end
    ;
    imgnum = indgen(nimg) + pos*nimg
    ;
    if (serial ge 545) and (serial le 555) then begin
	nimg = 22	;MDM added 6-Oct-94
	imgnum = [1, 0, indgen(10) + 10*sub10+2, indgen(10)*10+sub10+2]
	print, 'OCT545 thru OCT555 Series'
	q900 = 0
	if (serial eq 553) then imgnum = [3, 0, indgen(10) + 10*sub10+2+2, indgen(10)*10+sub10+2+2]
    end
    ;
    if (nimg eq 21) then begin
	if (n_elements(sub10) eq 0) then sub10 = 0
	case serial of
	    504: imgnum = [100, indgen(10) + 10*sub10, indgen(10)*10+sub10]		;MDM added 15-Feb-94
	    911: imgnum = [100, indgen(10) + 10*sub10, indgen(10)*10+sub10]		;MDM added 14-Jun-94
	    else: imgnum = [103, indgen(10) + 10*sub10+2, indgen(10)*10+sub10+2]	;MDM added 22-Feb-94
	endcase
    end
    ;
    if (q900) then begin
	imgpos0 = 107*pos
	first7 = indgen(7) + imgpos0
	second10 = indgen(10) + imgpos0 + sub10*10 + 7
	last10 =   indgen(10)*10 + imgpos0 + sub10 + 7
	imgnum = [first7, second10, last10]
	if (serial eq 613) and (pos eq 3) then imgnum(7:*) = imgnum(7:*) + 1	;extra dark at ~328, first7 are ok
	if (serial eq 613) and (pos gt 3) then imgnum = imgnum + 1        ;extra dark at ~328, all 27 shifted one
    end
    ;
    qfixed_dark = 0
    if (serial eq 613) then qfixed_dark = 1
    ;------ Special handling because of missing images (seq 686 and 680 are ok)
    ;
    if (serial eq 624) then imgnum(9:*) = imgnum(9:*) + 1	;image 9 inserted into series - 30 image series
    if (serial eq 678) and (pos eq 3) then imgnum(4) = imgnum(4)-1	;0085.fits is bad for 678 - it is not a m1/m2 tuning image
    ;
    ;;if (serial eq 685) and (pos eq 3) then imgnum(2:*) = imgnum(2:*)-1	;missing an image between 82-84 - artificially dup it
    ;;if (serial eq 685) and (pos gt 3) then imgnum = imgnum - 1	;missing an image between 82-84 
    if (serial eq 685) and (pos eq 8) then return			;missing the last image
    ;
    if (serial eq 687) then imgnum = imgnum + 1	;the first image is a bogus image in that series (so is the last one)
    if (serial eq 274) then imgnum = imgnum + 1	;the first image is a bogus image in that series (so is the last one)
    ;
    if (serial ge 860) and (serial lt 950) and (nimg eq 22) then imgnum = [1,0,indgen(20)+2]	;obs and dark are reversed
    ;
    if (nfiles ge 40) and (nfiles le 44) then begin		;opt241-opt272 series
	if (n_elements(sub10) eq 0) then sub10 = 0
	;nominal is 1 dark, 1 obs, 10 m1 varies, 10 m2 varies, 10 m1 varies, 10 m2 varies, dark (=0-42)
	;22 set order is: 27 set: 1 dark, 1 obs, 10 m1 varies, 10 m2 varies

	;        dark obs   m1/m2
	imgnum = [0, 1, indgen(20)+2+sub10*20]
	if (serial eq 247) then imgnum = imgnum + 1		;extra bogus image as first image
	if (serial eq 255) then begin
	    imgnum = (imgnum - 1)		;missing obs
	    imgnum(0) = 0			;dark image
	end

	if (serial ge 241) and (serial le 245) then imgnum = [41, 0, indgen(20)+1+sub10*20]
	if (serial eq 241) then imgnum = [44, 0, indgen(20)+4+sub10*20]	;wierd gap in file names, 0000, 0004, 0005
	if (serial eq 242) then return
	if (serial eq 257) then imgnum(1:*) = imgnum(1:*) + 1		;missing image "0001"
	if (serial eq 271) then imgnum(1:*) = imgnum(1:*) + 2		;missing image "0001" and "0002"
    end
    ;
    m1img_avoid = get_img_avoid(serial, pos, sub10, 0)	;MDM added 7-Mar-94
    m2img_avoid = get_img_avoid(serial, pos, sub10, 1)
end
;
if (keyword_set(image_arr)) then imgnum = image_arr
nimg = n_elements(imgnum)
image = intarr(64,64,nimg)
m1 = fltarr(64,64)
m2 = m1
trans = m1
all_heads = 0b
for i=0,nimg-1 do begin
    if (imgnum(i) ge 0) then begin
	read_mdi, serial, imgnum(i), img, head, /qprint

	if (get_nbytes(all_heads) eq 1) then all_heads = strarr(n_elements(head), nimg)
	all_heads(*,i) = head

	siz = size(img)
	if (siz(0) ne 2) then return
	image(0,0,i) = rebin(img, 64, 64) 
    end
end
;
if (keyword_set(summary)) then begin
    m1 = raw_summary(image)
    str = string(serial, pos, sub10, m1, format='(1x,3i3,2x,a)')
    if (data_type(summary) eq 7) then file_append, summary, str
end else begin
    if (getenv('ys_batch') eq "") then begin
	for i=0,nimg-1 do tvscl,rebin(image(*,*,i), 128, 128), i
	wait, 2
    end
    ;
    maplaser,image,m1,m2,trans,obs,laspos,s1, s2, amp1, amp2, $
		phase1, phase2, offset1, offset2, m1sig, m2sig, $
		uncert, sum_arr, tsig, $
		qfixed_dark=qfixed_dark, qstop=qstop, $
		m1img_avoid=m1img_avoid, m2img_avoid=m2img_avoid, nodefringe=nodefringe, $
		all_heads=all_heads
    c1 = 2*amp1 / (offset1+amp1)
    c2 = 2*amp2 / (offset2+amp2)
end

qrep = q900 or qsub10
if (keyword_set(save)) then begin
    save_mmap, head, serial, pos, sub10, qrep, m1, item='_m1',      laspos=laspos, uncert=uncert, sum_arr=sum_arr, /map_ver
    save_mmap, head, serial, pos, sub10, qrep, m2, item='_m2',      laspos=laspos, uncert=uncert, sum_arr=sum_arr, /map_ver
    save_mmap, head, serial, pos, sub10, qrep, m1sig, item='_m1s',  laspos=laspos, uncert=uncert, sum_arr=sum_arr, /map_ver
    save_mmap, head, serial, pos, sub10, qrep, m2sig, item='_m2s',  laspos=laspos, uncert=uncert, sum_arr=sum_arr, /map_ver
    save_mmap, head, serial, pos, sub10, qrep, trans, item='_tr',   laspos=laspos, uncert=uncert, sum_arr=sum_arr, /map_ver
    save_mmap, head, serial, pos, sub10, qrep, tsig,  item='_trs',  laspos=laspos, uncert=uncert, sum_arr=sum_arr, /map_ver
end
if (keyword_set(contrast_save)) then begin
    save_mmap, head, serial, pos, sub10, qrep, c1, item='_c1',      laspos=laspos, uncert=uncert, sum_arr=sum_arr, /map_ver
    save_mmap, head, serial, pos, sub10, qrep, c2, item='_c2',      laspos=laspos, uncert=uncert, sum_arr=sum_arr, /map_ver
end
if (keyword_set(part1_save)) then begin
    save_mmap, head, serial, pos, sub10, qrep, amp1, item='_a1',    laspos=laspos, uncert=uncert, sum_arr=sum_arr, /map_ver
    save_mmap, head, serial, pos, sub10, qrep, offset1, item='_o1', laspos=laspos, uncert=uncert, sum_arr=sum_arr, /map_ver
    save_mmap, head, serial, pos, sub10, qrep, phase1, item='_p1',  laspos=laspos, uncert=uncert, sum_arr=sum_arr, /map_ver
end
if (keyword_set(part2_save)) then begin
    save_mmap, head, serial, pos, sub10, qrep, amp2, item='_a2',    laspos=laspos, uncert=uncert, sum_arr=sum_arr, /map_ver
    save_mmap, head, serial, pos, sub10, qrep, offset2, item='_o2', laspos=laspos, uncert=uncert, sum_arr=sum_arr, /map_ver
    save_mmap, head, serial, pos, sub10, qrep, phase2, item='_p2',  laspos=laspos, uncert=uncert, sum_arr=sum_arr, /map_ver
end

end
