pro rd_obs2,obsfile,imid,filter,jd,exptime,rac,decc,xmid,obj,focus,fwhm,bias,$
        gain,apercor,ra,dec,mag,err,x,chip,xcen,ycen,sky,sharp,chi,ra2m,dec2m,$
        idno2m,j2m,h2m,k2m
; This routine reads one of Mark Everett's 2nd-gen ascii data files, containing
; information about the image and all stars from one 4-shooter integration,
; also containing 2MASS data for all stars that may lie within field
; On input,
;   obsfile = string containing name of input file
; On output,
;  imid = string (up to 24 char) containing name of input image
;  filter = string w/ filter name left-justified in 9 chars, blank-padded
;  jd = Julian date (double)
;  exptime = exposure time (s) (float)
;  rac = RA of image center (decimal hours) (double)
;  dec = Dec of image center (decimal degrees0 (double)
;  xmid = airmass at middle of image (float)
;  obj = object class: {bias,dark,domeflat,skyflat,target} = {1,2,3,4,5}
;  focus = telescope focus value (float)
;  fwhm(4) = psf fwhm estimate (pixels) for each chip (float)
;  bias(4) = bias from overscan for each chip (float)
;  gain(4) = gain in e-/adu for each chip (float)
;  apercor(4) = aperture corrections to photometry for each chip (mags)
;  ra(nst) = ra (decimal hours) for each star (double)
;  dec(nst) = dec (decimal degrees) for each star (double)
;  mag(nst) = instrumental mag for each star (float)
;  err(nst) = error estimate for each star (float)
;  x(nst) = airmass for each star (float)
;  chip(nst) = chip on which each star was found (1:4) (short)
;  xcen(nst) = x position of each star on chip (pix) (float)
;  ycen(nst) = y position of each star on chip (pix) (float)
;  sky(nst) = sky estimate for each star (e-) (float)
;  sharp(nst) = daophot sharpness param for each star (float)
;  chi(nst) = daophot goodness-of-fit param for each star (float)
;  ra2m(nst2m) = ra (decimal hours) of 2mass stars in field
;  dec2m(nst2m) = dec (decimal degrees) of 2mass stars in field
;  idno2m(nst2m) = long integer containing psym_dec ???? for each 2mass star
;  j2m(nst2m) = 2mass J magnitude
;  h2m(nst2m) = 2mass H magnitude
;  k2m(nst2m) = 2mass K magnitude

openr,iun,obsfile,/get_lun

; get image-related parameters
ss=''
readf,iun,ss
ss=strtrim(ss,2)
len=strlen(ss)
if(len gt 24) then ss=strmid(ss,0,24)
imid='                        '
strput,imid,ss,0
readf,iun,ss
ss=strtrim(ss,2)
len=strlen(ss)
if(len gt 9) then ss=strmid(ss,0,9)
filter='         '
strput,filter,ss,0
jd=double(0.)
exptime=float(0.)
rac=double(0.)
decc=double(0.)
xmid=float(0.)
obj=fix(0)
nst=long(0)
readf,iun,jd
readf,iun,exptime
readf,iun,rac
readf,iun,decc
readf,iun,xmid
readf,iun,obj
readf,iun,focus
readf,iun,v1,v2,v3,v4
fwhm=float([v1,v2,v3,v4])
readf,iun,nst2m
readf,iun,nst
readf,iun,v1,v2,v3,v4
bias=float([v1,v2,v3,v4])
readf,iun,v1,v2,v3,v4
gain=float([v1,v2,v3,v4])
readf,iun,v1,v2,v3,v4
apercor=float([v1,v2,v3,v4])

; count lines of input, since we can't always trust nst from header
;nst=0
;point_lun,-iun,fposn
;while(not eof(iun)) do begin
;  readf,iun,ss
;  nst=nst+1
;endwhile
;point_lun,iun,fposn


; make star output arrays
if(nst2m gt 0) then begin
ra2m=dblarr(nst2m)
dec2m=dblarr(nst2m)
idno2m=lonarr(nst2m)
j2m=fltarr(nst2m)
h2m=fltarr(nst2m)
k2m=fltarr(nst2m)

; read the 2mass data
w1=double(0.)
w2=double(0.)
ii=long(0)
for i=0,nst2m-1 do begin
  readf,iun,w1,w2,ii,w3,w4,w5
  ra2m(i)=w1
  dec2m(i)=w2
  idno2m(i)=ii
  j2m(i)=w3
  h2m(i)=w4
  k2m(i)=w5
endfor
endif

if(nst gt 0) then begin
ra=dblarr(nst)
dec=dblarr(nst)
mag=fltarr(nst)
err=fltarr(nst)
x=fltarr(nst)
chip=intarr(nst)
xcen=fltarr(nst)
ycen=fltarr(nst)
sky=fltarr(nst)
sharp=fltarr(nst)
chi=fltarr(nst)
; read the 4-shooter data
w1=double(0.)
w2=double(0.)
w6=fix(0)
for i=0,nst-1 do begin
  readf,iun,w1,w2,w3,w4,w5,w6,w7,w8,w9,w10,w11
  ra(i)=w1
  dec(i)=w2
  mag(i)=w3
  err(i)=w4
  x(i)=w5
  chip(i)=w6
  xcen(i)=w7
  ycen(i)=w8
  sky(i)=w9
  sharp(i)=w10
  chi(i)=w11
endfor
endif

close,iun
free_lun,iun

end