	PRO TapDir2, archdir, drecs, cpiosw, startT=startTime,  $
	  endT=endTime,tDirfn=tDirfn, softw, tardirs, cdidxpos, ver, $
	tapeName, alldata, weekDir, mktapverno=mktapverno
;	------------------------------------------------------------
;+							11-Jun-93
;   	NAME:
;		TapDir
;	PURPOSE:
;		Write the tape directory file for an archive tape.
;	CALLING SEQUENCE:
;		TapDir, archdir, drecs, cpiosw, [startT=starttime, $
;			endT=endTime, tDirfn=tDirfn], softw, tardirs, $
;			cdidxpos, ver, tapeName, alldata,weekDir, $
;			mktapverno=mktapverno
;	INPUT/KEYWORD:
;		archdir		list of data directories to backup.
;		startTime	String: 'yymmdd.hhmm'--stating the 1st
;				day of the archive tape.
;		endTime		String:	'yymmdd.hhmm'--stating the last
;				day of the archive tape.
;		tDirfn		fileID for the binary tape dir. file.
;		softw		tar command for software archive
;		tardirs		paths and sizes of each software tar file
;		ver		version # of mktap
;		alldata		flag to override prompts and get all data
;		weekDir		path to weekly files
;		mktapverno	ver# of data structure to tape dir. file.
;	Returned/Results:
;		drecs		list of fileIDs vs file prefix w/ sizes
;		cpiosw		list of cpio soft switches used 
;		tDirfn		compute fileID for the binary tape dir. file.
;				if one is not supplied. ex: yy_wka.01
;		cdidxpos	index position for "cd" commands
;		tapeName	derived tapeName "yy_wka.01-0.043
;		
;	General Notes on TapDir file:
;		Header: size of file, # of logical data records, 
;			date written, machine information (!version),
;			Reformatter ver #,
;			Tape # & Name, start date of data,
;			Last date of data, # of tailing files following
;			the cpio archives, spare byte array.
;
;		Logical data records:
;			fileId       	bytarr(11)???
;			cpio_h_size     byte
;			ada file size	(0 bytes indicates file does 
;			bda file size    not exist)
;			cba file size	Long ....
;			hda file size
;			sfr file size
;			spr file size
;			wda file size
;			sp1 spare #1
;			sp2 spare #2
;			sp3 spare #3
;	HISTORY:
;		written by GAL, 5-sep-91
;		23-Sep-91, debug and integration with UNIX and 
;			the real world...
;		23-Oct-91, changed tape dir fileName to be:
;				yy_wkd.a	"d" for directory.
;		24-Oct-91, changed the tape-directory-header section
;			to include # files following cpio archive and
;			spare byte array.
;		10-Feb-92, added feature for data within multiple 
;			directories.
;		17-Feb-92, added feature for weekly files (obs,...)
;		18-Feb-92, updated filename for tape dir. files
;		19-Feb-92, updated for bunch of changes
;		24-Feb-92, update to read old tape-dir files and compute
;			version # for the new tape-dir file.
;		5-Mar-92, add alldata flag
;		17-Apr-92, add weekDir 
;		 6-Nov-92 (MDM) - Added file remove before OPENW
;		11-jun-93, added mktapverno to formal parameter list.
;-
;	-------------------------------------------------------------
;	ON_ERROR, 2 	;force a return to caller on error

	fprefix = PrefixCk(/noobs)	;get standard prefix list w/o obs

;	Create Output Data-Structures for Tape-directory file:
	MkDirSt, tardirs, arch_hdrec, arch_drec, mktapverno=mktapverno


;	How many days will this Directory file span (default is all data
;	within a single directory [usually 7 days])!!!
;	Note: if startTime and endTime are undefined user will be prompted.
; 	startTime = '910905'		;Formats for dates 
; 	endTime   = '910906'

	ndays = INTARR(N_elements(archdir))	;# of days from each dir
	startTime = STRARR(N_elements(archdir))	;start time from each dir
	endTime   = STRARR(N_elements(archdir))	;end  " ""
	cdidxpos  = INTARR(N_elements(archdir))	;where to place the cd cmd
	FOR i= 0, N_elements(archdir)-1 DO BEGIN
	  cd, archdir(i)			;change directory
	  ndays(i) = fnddur(alldata,startT=starts, endT=ends)
	  startTime(i) = starts		& starts = ''
	  endTime(i)   = ends		& ends = ''
	  
	  IF (i eq 0) THEN BEGIN	;Get the valid weekly log fileID from 
;print, 'ck GetObsID call --i,startTime(0):', i, startTime(0)
;stop, 'ck all...'
	    obsID = GetObsID(startT=startTime(0)) ;1st directory start time. 
	  ENDIF
	ENDFOR

;	Give user a Summary of the Archive to be FORMED:
	Print, '	'
	Print, 'Archive will include ', STRTRIM(string(n_elements(archdir)),2), $
		' Directory(s).
	FOR i= 0, N_elements(archdir)-1 DO BEGIN
	  Print, 'From Directory ', archdir(i),' --'
	  Print, 'The archive will span ', strtrim(string(ndays(i)),2), ' days', $
		', From (dates in fileID format): ', startTime(i), $
		' thru ', endTime(i),'.'
	ENDFOR
;	stop, 'Debug stop ar TAPDIR2: ck new features out:'

	firstTimeID = 1			;first time flag for fileIDs
	firstTime = 1			;for saving data-recs
	FOR i=0, N_elements(archdir)-1 DO BEGIN	;Directory loop

;	  Find the Unique list of Fileids for each day (e.g. list(nFileids))
;	  The best way maybe to read the space ephemeris log just as the 
;	  reformatter does....However, Mons says that ATT or ADA files will be 
;	  written for every orbit regardless of the presence of other data.

	  cd, archdir(i)		;move to directory
	  prefix = 'ada'		
;	  fileIDs = fndids(prefix, startT=startTime, idx=i)	;ret days worth
	  fileIDs = fndrids(prefix, startT=startTime(i), endT=endTime(i))
;	  Print, 'i and archdir: ', i, archdir(i)
;	  help, fileIds

	  IF (fileIDs(0) ne '') THEN BEGIN	;Some files found for that day
;	    SAVE first and last VALID file ID 
            IF (firstTimeID) THEN BEGIN	
	      frstID = fileIDs(0)
	      firstTimeID = 0			;flag set to false
	      nfileIDs = N_Elements(fileIDs)	
	      lstID = fileIDs(nfileIDs-1)       ;set last id
	    ENDIF
	    nfileIDs = N_Elements(fileIDs)
	    lstID = fileIDs(nfileIDs-1)         ;set last id

	    da_recs = Load_recs(arch_drec,fprefix,obsID,fileIDs, $
			firstTime,reforVer, wkprelst, wkextlst, weekDir)
						;load logical data recs

	    IF (firstTime) THEN BEGIN
	      drecs = da_recs
	      firstTime = 0			;set firstTime flag false
	      cdidxpos(i) = 0 			;where to place the cd cmd
	    ENDIF ELSE BEGIN
	      cdidxpos(i) = N_ELEMENTS(drecs)	;where to place the cd cmd
	      drecs = [drecs, da_recs]		;save as a linear array
	    ENDELSE
	  ENDIF
	ENDFOR
; print, 'script positions for cd cmds :', cdidxpos

;	Collect information for the logical header-record:
        nrecs = N_ELEMENTS(drecs)              	;number of logical data-recs
	cpiosw = '-oBc'				;default cpio switches
	str_reforver = '-'+STRING(reforver/1000.,format='(f5.3)')
	taperel  = 1			;assume 1st cut of tape
	str_taperel = '.'+string(taperel,format='(i2.2)')
	tapeName = drecs(0).fileID(0:5)		;use the weeky fileID
	tmp_tDirfn = [tapeName, byte(str_taperel)]	;working tapeDir fileID
	tapeName = [tmp_tDirfn, byte(str_reforver)]

;stop, 'stop 2 ck tapename and archdir'

	hdrec = Load_hd(arch_hdrec,arch_drec,nrecs,tapeName,reforVer, $
		frstID,lstID, cpiosw, softw, tardirs, wkprelst, $
		wkextlst, ver, taperel)


	IF (NOT KeyWORD_SET(tDirfn)) THEN BEGIN	;no user specified dir file name
	  tDirfn = string(tmp_tDirfn)				;yy_wka.01
	  tmp_tDirfid = STRING(drecs(0).fileID(0:5))		;yy_wka
	  tapeRel = cktapdir(hdrec, drecs, tmp_tDirfid)	;ck tDirfn

	  hdrec.tapeRel = taperel
	  str_taperel = '.'+string(taperel,format='(i2.2)')	    
	  tapeName = drecs(0).fileID(0:5)		;use the weeky fileID
	  tmp_tDirfn = [tapeName, byte(str_taperel)]	;working tapeDir fileID
	  hdrec.tapeName = [tmp_tDirfn, byte(str_reforver)]
	  tapeName = hdrec.tapeName
	  tDirfn = STRING(tmp_tDirfn)
	ENDIF

	cd, archdir(0)			;move back to 1st DATA directory
;	Create BINARY directory file:
	binarydirfn = '$DIR_GEN_XBD/xbd'	;path and prefix
;	binarydirfn = 'xbd'	;path and prefix

	spawn, 'rm -f ' + binarydirfn+tDirfn			;MDM added 6-Nov-92
	OPENW, /GET_lun, unit, binarydirfn+tDirfn ;ck archscrpt for filename	
	WRITEU, unit, hdrec, drecs ;write header and logical data recs
	Free_lun, unit

;	Write ascII ver
	ascdirfn = '$DIR_GEN_XAD/xad'
;	ascdirfn = 'xad
	WrtTapDir, hdrec, drecs, tDirfn, ascdirfn	
;	STOP,'**** stop to save hdrec and drecs:'

	END
