; $Id: tasrdm.pro,v 1.2 2002/01/14 20:59:27 amunter Exp $

function at_loc,colind,d
colind=strupcase(colind)
ind=tasid(colind,d)
if (ind eq -1) then begin
  temp=fltarr(n_elements(d.prp)+1,d.points)
  temp(0:n_elements(d.prp)-1,*)=d.data(0:n_elements(d.prp)-1,*)
  d={points: d.points,heads: d.heads,pars: d.pars+1,text: d.text,$
   hdef: d.hdef,hnum: d.hnum,prp: [d.prp,colind],$
   data: temp}
  endif
x=d.data(tasid(colind,d),*)
return,x
end

FUNCTION genname, name, n, extension
IF (n LT 10) THEN RETURN, strcompress(string(name,'00',n,extension),/REMOVE_ALL)
IF (n LT 100) THEN RETURN, strcompress(string(name,'0',n,extension),/REMOVE_ALL)
RETURN, strcompress(string(name,n,extension),/REMOVE_ALL)
END

PRO makecolumn, a, id
;Makes a new data column in the TASCOM structure
;loading the values of id into it, if id exists
;in the hdef structure. This is used to fill up
;the scan data with things written in the header.
jim1 = where(a.hdef EQ id,count1)
jim2 = where(a.prp EQ id,count2)
IF ((count1 NE 0) AND (count2 EQ 0)) THEN BEGIN
	IF (id EQ 'H0') THEN nid = 'H'
	IF (id EQ 'K0') THEN nid = 'K'
	IF (id EQ 'L0') THEN nid = 'L'
	dummy = at_loc(nid,a)
	a.data(tasid(nid,a),*)=a.hnum(where(a.hdef EQ id))
END
END

FUNCTION tasrd_loc,name
;This just reads the file using TASRD and
;creates new data columns for whatever is
;missing from the grid scan stuff. ie. h, k, or l
;Used to read the data from the UBC single
;crystal grid scans around 100 and 010.
a=tasrd(name)
makecolumn, a, 'H0'
makecolumn, a, 'K0'
makecolumn, a, 'L0'
return, a
END


PRO tasrdm, d,llimit,ulimit,name,extension,list=list
;+
;NAME:	TASRDM
;PURPOSE:
;		Read a series of TASCOM files  and
;		creates one big TASCOM data structure
;CATEGORY:
;CALLING SEQUENCE:
;	tasrdm, d,lower_limit,upper_limit,name,extension
;INPUTS:
;	lower_limit - lower value for the file number
;	upper_limit - upper value for the file number
;	name        - file class
;	extension   - file name extension
;
;	For example: tasrdm, d,1,22,'ubc','.dat'
;	will read files ubc001.dat to ubc022.dat and 
;	return an array of 22 elements of the TASCOM data
;	structure
;OPTIONAL INPUT PARAMETERS: none
;KEYWORD PARAMETERS: none
;OUTPUTS: 
;	A TASCOM data structure (see TASRD for
;	a definition of the elements)
;OPTIONAL OUTPUT PARAMETES:
;COMMON BLOCKS:
;SIDE EFFECTS:
;	If the number of files is greater than 999, things will
;	probably not work.
;RESTRICTIONS:
;PROCEDURE:
;MODIFICATION HISTORY:
;	Paul Schleger, Riso National Laboratory, June 9 1993
;-	
IF NOT keyword_set(list) THEN list = INDGEN(ulimit-llimit+1)+llimit
d=tasrd_loc(genname(name,list(0),extension))
FOR I=1,n_elements(list)-1 DO $
	d=tasappend(d,tasrd_loc(genname(name,list(I),extension)),/NOTEXT)
;****** make the text more sensible *****
IF (STRLEN(d.text(0)) LE STRLEN(d.text(1)) ) THEN index = 0 ELSE index=1
d.text(index)=d.text(index)+'  '+genname(name,llimit,extension)+'--'+$
			         genname(name,ulimit,extension)
TAB = STRING(9B)
print, 'TEXT:', TAB, d.text(0)
print, TAB, d.text(1)
print, 'HDEF:', TAB, d.hdef
print, 'PRP:', TAB, d.prp
END

