	FUNCTION MkFnMap, fidmap=map, fnMap=fnMap
;	----------------------------------------------------------------
;+							27-June-91
;	Name:
;		MkFnMap	(Make Filename Map)
;	Purpose:
;		Create a record (fnMap) of the data to be 
;		extracted from reformatted data files.
;	CALLING SEQUENCE:
;		err = MkFnMap([fidmap=map, fnMap=fnMap])
;	Input/Keyword:
;		map	search keys, input times, file prefix, file ids,
;			and fidmap.
;	Output/keyword:
;		fnMap	a copy of the 1st half of the fidmap plus a
;			file name map for data indices.
;	Returned:
;		MkFnMap	returns error status values:
;			0 is failure
;			1 is success
;	History:
;		written by GAL 25-June-91
;		27-June, extended to give valid indices for correct
;		file prefixes.
;-
;	---------------------------------------------------------------
;	ON_ERROR, 2	;force a return to caller on error 

	filprefix = ['bda','hda','sfr','spr','wda']
	n_prefix  = N_ELEMENTS(filprefix)	;number of file types

;	Create file name map if formal parameter is undefined:
	help, fnMap
	IF (NOT KEYWORD_SET(fnMap)) THEN BEGIN
	  mapTemp = "{dummy,key:' ', usrst:' ', " $
                + " usren:' ', nfiles:fix(0), w:' ', filena:' ', " $
                + " fstat:' ', stbda:' ', enbda:' ', sthda:' ', " $
                + " enhda:' ', stsfr:' ', ensfr:' ', stspr:' ', " $
		+ " enspr:' ', stwda:' ', enwda:' '}"
	  fnMap = MAKE_STR(mapTemp)
	ENDIF
         
;	Copy top fields from fidmap:
	fnMap.key = map.key		;load search keys
	fnMap.usrst = map.usrst		;load start and end times
	fnMap.usren = map.usren
	fnMap.w     = STRlowCASE(map.w)	;load instrument requests

;	Create FileName(s) from ID and prefix:
	wArr = MkInsArr(fnmap.w, map.nfid)	;instru list (nfid,5)

	fid = STR2Arr(map.fid)		;convert into string array
	fileNa = CreFn(wArr, n_prefix, filprefix, map.nfid, fid)
	fileNames = fileNa		;save for later
	nfiles = N_ELEMENTS(fileNa)	;number of file names
	fileNa = Arr2Str(fileNa)	;convert into string

	fnMap.nfiles = nfiles		;load file count
	fnMap.fileNa = fileNa		;load file names

;	Generate file name map:
	def = LONG(-1)
	fidmap = Strs2Mat( structure=map, stval=7, enval=16, blank=def)
	
	fileNaMap = LONARR(nfiles, 10)	;initalize file name map
	filecount = 0			;init file counter
	
	FOR i= 0, map.nfid-1 DO BEGIN	;for each file ID
	  FOR j= 0, n_prefix-1 DO BEGIN	;for each file type
	    row = WHERE(wArr(i,*) eq filprefix(j))
	    IF (row(0) ne -1) THEN BEGIN	;instrument j req
	      fileNaMap(filecount,0) = fidmap(i,*)	;copy into new
	      filecount = filecount + 1
	    ENDIF
	  ENDFOR
	ENDFOR

;	Clean up indices on invalid files:
	irow = 0
	newfilidx = Indgen(10) +1
	newfilIdx = (newfilIdx MOD 2)	;set up binary table
        prefixCount = 0		
	FOR i=0, 9 DO BEGIN		;loop thru all fileNaMap indices
	  IF (newfilIdx(i)) THEN BEGIN
	    idx = WHERE(STRMID(fileNames,0,3) NE filprefix(prefixCount))
	    IF (idx(0) ne -1) THEN BEGIN	;invalid prefix is found
	      fileNaMap(idx,i) = def		;set to default flag
	      fileNaMap(idx,i+1) = def		;set ending indices
	    ENDIF 
	    prefixCount = prefixCount + 1	;increment 0 thur 4    
	  ENDIF 
	ENDFOR

;	Load fileNaMap into fnmap structure:
	fileNaMap = STRING(fileNaMap)
	irow = 0 			;row counter
	FOR i= 7, 16 DO BEGIN		;load start and end indices
	  s = Arr2Str(fileNaMap(*,irow))
	  fnmap.(i) = s
	  irow = irow + 1		;increment thru all 10 rows
	ENDFOR 

	err = 1			;set success flag
	RETURN, err
	END	
