pro rd_dpc_table, dpc_struct, infil=infil, genxfil=genxfil, qwrite=qwrite, qforce_rd=qforce_rd
;+
;NAME:
;	rd_dpc_table
;PURPOSE:
;	To read a database file
;SAMPLE CALLING SEQUENCE:
;	rd_dpc_table, dpc_struct
;	rd_dpc_table, /qwrite
;HISTORY:
;	Written 29-Aug-96 by M.Morrison (stripped out of "get_dpc_info")
;-
;
;
if (n_elements(infil) eq 0) then infil = '$MDI_CAL_INFO/dpc_table.txt'
if (n_elements(genxfil) eq 0) then genxfil = '$MDI_CAL_INFO/dpc_table.genx'
;
junk = file_info2(infil, info1)
junk = file_info2(genxfil, info2)
dt = int2secarr(info2, info1)
qread = 0
if (keyword_set(qforce_rd)) then qread = 1
if (keyword_set(qwrite)) then qread = 1
if (dt lt 0) then begin
    print, '.TXT file is newer than .GENX file for DPC_TABLE'
    qread = 1
end
if (not file_exist(genxfil)) then begin
    print, 'Cannot find file: ' + genxfil
    print, 'Will read: ' + infil
    qread = 1
end
;
if (qread) then begin
    print, 'RD_DPC_TABLE: Reading the text file'
    ;
    mat = rd_tfile(infil, 10, delim=string(9b), nocomment='#')
    if (n_elements(mat) eq 1) then return
    mat = mat(*, 2:*)		;skip the title and underline lines
    mat = strtrim(strcompress(mat),2)
    n = n_elements(mat(0,*))
    ;
    dpc_struct = {dpc: 0L, type: ' ', rank: 0L, nx: 0L, ny: 0L, npix: 0L, sum: 1, $
		format_file: ' ', mnem: ' ', comment: ' ', $
		nseg: 0, cadence: 0, imin: 0L, imax: 10000L, $
		ip_list: ' ', pixmap2: ' '}
    dpc_struct = replicate(dpc_struct, n)
    ;
    for i=0,n-1 do begin
	stat = execute( "temp = long('" + mat(0,i) + "'XL)" )
	dpc_struct(i).dpc	= temp
    end
    dpc_struct.type	= reform(mat(1,*))
    dpc_struct.rank	= long(reform(mat(2,*)))
    dpc_struct.nx	= long(reform(mat(3,*)))
    dpc_struct.ny	= long(reform(mat(4,*)))
    dpc_struct.format_file = reform(mat(5,*))
    dpc_struct.mnem	= reform(mat(6,*))
    dpc_struct.comment	= reform(mat(7,*))
    dpc_struct.nseg	= long(reform(mat(8,*)))
    dpc_struct.cadence	= long(reform(mat(9,*)))
    ;
    ss = where((strupcase(dpc_struct.type) eq 'TABLE') or (strupcase(dpc_struct.type) eq 'LIST'), nss)
    if (nss ne 0) then dpc_struct(ss).sum = 0			;carry over from old format file
    ss = where(dpc_struct.ny eq 0, nss)
    if (nss ne 0) then dpc_struct(ss).ny = 1
    ss = where(dpc_struct.nx eq 0, nss)
    if (nss ne 0) then dpc_struct(ss).nx = '10000'x
    dpc_struct.npix	= dpc_struct.nx * dpc_struct.ny
    ;
    ss = where(dpc_struct.format_file ne '.', nss)
    for i=0,nss-1 do begin
	infil_format = concat_dir('$MDI_FORMAT_DIR', dpc_struct(ss(i)).format_file)
	if (file_exist(infil_format)) then begin
	    ;;arr_format = rd_tfile(infil_format, nocomm='#')
	    ;;temp = sxpar(arr_format, 'IP_LIST')	 & if (data_type(temp) eq 7) then dpc_struct(ss(i)).ip_list = temp
	    ;;temp = sxpar(arr_format, 'PIXMAP2')	 & if (data_type(temp) eq 7) then dpc_struct(ss(i)).pixmap2 = temp
	    arr_format = rd_tfile(infil_format, 2, delim=string(9b), nocomm='#')
	    arr_format = arr_format(*, 2:*)		;skip the title and underline lines
	    arr_format = strtrim(arr_format,2)
	    temp = where(arr_format(0,*) eq 'IP_LIST')	& if (temp(0) ne -1) then dpc_struct(ss(i)).ip_list = arr_format(1,temp(0))
	    temp = where(arr_format(0,*) eq 'PIXMAP2')	& if (temp(0) ne -1) then dpc_struct(ss(i)).pixmap2 = arr_format(1,temp(0))
	end
    end
    ;
    if (keyword_set(qwrite)) then begin
	file_delete, genxfil
	savegen, dpc_struct, file=genxfil
    end
    return
end
;
restgen, dpc_struct, file=genxfil
end
