;+
; Project     : SOHO - CDS
;
; Name        : CDS_ASRUN_POINT
;
; Purpose     : create map structures for ASRUN CDS pointings from catalog
;
; Category    : planning
;
; Syntax      : IDL> cds_asrun_point,cmap,tstart,tend
;
; Inputs      : TSTART = plot start time [def=current date]
;
; Opt. Inputs : TEND = plot end time [def = 24 hour window]
;
; Outputs     : CMAP = CDS pointings saved in MAP structures
;
; Keywords    : COUNT = # of pointings found
;               STUDY = study program number (e.g. s22906r04)
;
; History     : Written, 20-May-1998,  D.M. Zarro (SM&A/GSFC)
;             : Modified, 23-Jul-2001,  D.M. Zarro (EITI/GSFC) - added STUDY
;
; Contact     : DZARRO@SOLAR.STANFORD.EDU
;-

pro cds_asrun_point,cmap,tstart,tend,count=count,err=err,study=study

err=''
count=0
delvarx,cmap

;-- user can search by study program number

seq=-1
if exist(study) then begin
 if is_number(study) then prog=long(study) else begin
  if is_string(study) then begin
   spos=strpos(study,'s')
   rpos=strpos(study,'r')
   have_s=spos gt -1
   have_r=rpos gt -1
   case 1 of
    have_s and have_r: begin
     prog=long(strmid(study,spos+1,rpos-spos+1))
     seq=fix(strmid(study,rpos+1,1000))
    end
    have_s and (not have_r): begin
     prog=long(strmid(study,spos+1,100))
     seq=-1
    end
    else: begin
     err='Unrecognized study input'
     message,err,/cont
     return
    end
   endcase
  endif
 endelse
endif

;-- check time and/or prog inputs

dprint,'% prog, seq ',prog,seq

if is_number(prog) then begin
 list_exper,prog,plans,count 
 if count gt 0 then begin
  if seq gt -1 then begin
   keep=where(seq eq plans.seq_ind,count)
   if count gt 0 then plans=plans(keep)
  endif
 endif
endif else begin
 t1=get_def_times(tstart,tend,dend=t2)
 list_exper,t1,t2,plans,count
endelse

if count eq 0 then begin
 err='No matching CDS ASRUN plans found'
 message,err,/cont & return
endif

;-- make pointing structures

for i=0,count-1 do begin
 time=anytim2utc(plans(i).date_obs,/vms)
 tend=anytim2utc(plans(i).date_end,/vms)
 ras_id=plans(i).ras_id
 ras_var=plans(i).ras_var
 study=get_cds_study(plans(i).study_id,plans(i).studyvar)
 eng=study.n_raster_def eq 0 
 fixed=study.var_point eq 'N'
 temp=make_map(fltarr(2,2),xc=plans(i).xcen,yc=plans(i).ycen,$
               dx=plans(i).ixwidth/2.,dy=plans(i).iywidth/2.,$
               time=time,tend=tend,/soho,id=plans(i).obs_prog,eng=eng,fixed=fixed,$
               seq=plans(i).seq_ind)
 cmap=merge_struct(cmap,temp)
endfor

count=n_elements(cmap)

return & end

