Viewing contents of file '../idllib/sdss/allpro/fetch_file_list.pro'
pro fetch_file_list, dir, files, fnums, start=start, nframes=nframes,$
                     run=run, camcol=camcol, rerun=rerun,  $
                     fieldmin=fieldmin, fieldmax=fieldmax, $
                     front=front, nchar=nchar

;+
; NAME:
; 	fetch_file_list	
;
; PURPOSE:
;	for use with read_tsObj
;	will return the list of tsObj files in directory dir 
;	(the new field-seperarted tsObj files) between start and start+nframe
;       Can also have a front other than tsObj if so desired, but
;       must have form front-run-camcol-rerun-field.extension
;
; CALLING SEQUENCE:
;	fetch_file_list,dir, files, fnums [, start=start, nframes=nframes,
;                    run=run, camcol=camcol, rerun=rerun,  
;                    fieldmin=fieldmin, fieldmax=fieldmax, 
;                    front=front, nchar=nchar]
;
; INPUTS:
;	dir:  the full directory where the tsObj files live.
;         Must be of the form: "..../run/rerun/calibChunks/camcol/" or
;         fetch_file_list will fail.
;
; OPTIONAL INPUTS:
;       start: the first field to consider (default is first)
;	nframes: the total number of files to fetch (default is max possible)
;       front:  The front string in the files.  Default is "tsObj"
;       nchar:  The number of characters in the name.  
;               Default is 25 for tsObj files.
;
; OUTPUTS:
;	files: a string array of the file names (full path)
;	fnums: a "long" array of the field numbers	
;       run: the run 
;	camcol: the camera collumn
;	rerun: the rerun number
;		(presumably you had to have known these three
;		variables to get the directory 
;		but you may not have had them handy
;		so are output if you need them)
;
;       fieldmin, fieldmax: first and last field numbers
;
; EXTERNAL CALLS:
;      	none 
; METHOD:
;	uses find_file function   
; EXAMPLE
;	IDL> fetch_dir,run,camcol,rerun,dir
;	IDL> fetch_file_list,dir,files,start=13,nf=20
;	IDL> help,files   
;   	FILES           STRING    = Array[100]
;	IDL> print,files(0)
;	/usr/sdss/data02/imaging/273/1/calibChunks/2/tsObj-000273-2-1-0011.fit
;
;
; NOTES
;
; HISTORY:  written by David Johnston -University of Chicago
;       Physics Department October 9 1999
;       11/13/99  Added "front" and "nchar" inputs  Erin Scott Sheldon
;       2/20/2000 made start, nframes optional. defaults set.  E.S.S.
;       Now requires strict directory format. Gets run/camcol/rerun from
;       directory name. Checks for files with those numbers. 13-Nov-2000 E.S.S.
;-


  if n_params() LT 1 then begin
      print,'-Syntax: fetch_file_list, dir [, files, fnums, start=start, nframes=nframes,run=run, camcol=camcol, rerun=rerun, fieldmin=fieldmin, fieldmax=fieldmax, front=front, nchar=nchar]'
      return
  endif

  IF n_elements(front) EQ 0 THEN front = 'tsObj'
  IF n_elements(nchar) EQ 0 THEN nchar = 25

  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  ;; Check run, rerun, camcol
  ;; only works if using SDSS directory structure
  ;; get run,rerun,camcol from directory name
  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

  tmp = str_sep(dir, '/calibChunks/')
  IF n_elements(tmp) LT 2 THEN BEGIN 
      print,' DIRECTORY STRUCTURE IS INVALID FOR DIRECTORY ',dir
      print,' MUST BE OF THE FORM */run/rerun/calibChunks/camcol/'
      return
  ENDIF 
  ;; camcol
  cstr = ( str_sep(tmp[1], '/')  )[0]
  camcol = fix(cstr)

  ;; separate calibChunks and check for proper 
  ;; directory structure

  tmp = str_sep(tmp[0],'/')
  ntmp=n_elements(tmp)
  IF ntmp LT 2 THEN BEGIN 
      print,' DIRECTORY STRUCTURE IS INVALID FOR DIRECTORY ',dir
      print,' MUST BE OF THE FORM */run/rerun/calibChunks/camcol/'
      return
  ENDIF 
  ;; rerun
  rrstr=tmp[ntmp-1]
  rerun = fix(rrstr)
  
  ;; run (allow corrected directory structure)
  rstr = tmp[ntmp-2]
  tmprun = str_sep(rstr,'corr')
  IF n_elements(tmprun) EQ 2 THEN run = long(tmprun[1]) ELSE run = long(tmprun[0])
  rstr = run2string(run)

  ;;all files in that directory
  files=findfile(dir)
  num=n_elements(files)

  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  ;; get front, run, camcol, rerun for each file
  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

  ffront=strarr(num)
  frun=strarr(num)
  fcamcol=strarr(num)
  frerun=strarr(num)
  FOR i=0, num-1 DO BEGIN 
      a=str_sep(files(i),'-')
      ffront[i]=a[0]
      IF (n_elements(a) EQ 5) THEN BEGIN ;must be proper format
          frun[i] = a[1]
          fcamcol[i] = a[2]
          frerun[i] = a[3]
      ENDIF 
  ENDFOR 

  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  ;; check for files with this front
  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

  wf=where( (ffront eq front) and (strlen(files) eq nchar) ,num)
  IF num EQ 0 THEN BEGIN
      print,' NO '+front+' FILES FOUND IN DIRECTORY: ',dir
      wf=where(ffront EQ front,nw) & print,nw
      files=''
      return 
  ENDIF 
  files=files[wf]
  frun=frun[wf]
  fcamcol=fcamcol[wf]
  frerun=frerun[wf]

  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  ;; check for files with this run number
  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

  wr=where(frun EQ rstr,num)
  IF num EQ 0 THEN BEGIN 
      print,' NO FILES WITH RUN = ',rstr,' IN DIRECTORY ',dir
      files=''
      return
  ENDIF 
  files = files[wr]
  fcamcol=fcamcol[wr]
  frerun=frerun[wr]

  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  ;; check for files with this camcol number
  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

  wc=where(fcamcol EQ cstr,num)
  IF num EQ 0 THEN BEGIN 
      print,' NO FILES WITH CAMCOL = ',cstr,' IN DIRECTORY ',dir
      files=''
      return
  ENDIF 
  files = files[wc]
  frerun = frerun[wc]

  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  ;; check for files with this rerun number
  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

  wrr=where(frerun EQ rrstr,num)
  IF num EQ 0 THEN BEGIN 
      print,' NO FILES WITH RERUN = ',rrstr,' IN DIRECTORY ',dir
      files=''
      return
  ENDIF 
  files = files[wrr]

  first=files(0)
  last=files(num-1)
  
  af=str_sep(first,'-')
  al=str_sep(last,'-')
  
  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  ;; check for files with correct format
  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

  IF n_elements(af) NE 5 THEN BEGIN
      print,' FILE NAMES NOT IN PROPER FORMAT: front-run-camcol-rerun-field.extension'
      files=''
      return
  ENDIF 

  ff=str_sep(af(4),'.')
  fieldmin=long(ff(0))		;the first field number
  
  fl=str_sep(al(4),'.')
  fieldmax=long(fl(0))		;the last field number
  
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Special cases: user wants all or start,nframes not entered
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  
  IF n_elements(start) EQ 0 THEN start = fieldmin
  IF n_elements(nframes) EQ 0 THEN nframes = fieldmax - start + 1
  
  IF nframes LT 1 THEN BEGIN
      print
      print,' nframes must be greater than 0.  Setting to 1'
      nframes = 1
  ENDIF 
  
  IF start LT fieldmin THEN BEGIN
      print
      print,' Start is less than first field.  Beginning with field ',ntostr(fieldmin) 
      start=fieldmin
  ENDIF 
                                ;can't get files before the first one
  IF start GT fieldmax THEN BEGIN
      print
      print,' Start is larger than last field.  Reading last field only.'
      start = fieldmax
      nframes = 1
  ENDIF 
  IF start+nframes-1 GT fieldmax THEN BEGIN
      print
      print,' Cannot read past last field.  Reading to field ',ntostr(fieldmax)
      nframes=fieldmax-start+1
  ENDIF 
                                ;can't get more files than there are
  
  fin=start+nframes-1		;this will be the last file
  
  fnums=lonarr(num)		;the field numbers
  
  for i=0, num-1 do begin
      a=str_sep(files(i),'-')
      fnum=str_sep(a(4),'.')
      fnums(i)=long(fnum(0))
  endfor
  
  w=where(fnums ge start and fnums le fin)
                                ;select only the fields in the range
  files=files(w)
  
                                ;check for trailing backslash on dir
  len=strlen(dir)
  lastch=strmid(dir,len-1,1)
  if lastch ne '/' then dir=dir+'/'
  
  files=dir+files
  fnums=fnums(w)
  
  return
end