;+
; NAME:
;	EDIT_GRPDTB
; PURPOSE:
;	Allows creation of new BCS RAM gouper plans
; CATEGORY:
; CALLING SEQUENCE:
.	.run edit_grpdtb
; INPUTS:
; OPTIONAL INPUT PARAMETERS:
; KEYWORD PARAMETERS:
; OUTPUTS:
;	If requested, creates file "grpdtb.lis" on current directory.
; OPTIONAL OUTPUT PARAMETERS:
; COMMON BLOCKS:
; SIDE EFFECTS:
; RESTRICTIONS:
; PROCEDURE:
; MODIFICATION HISTORY:
;	RDB  Autumn-91
;	RDB  17-May-94	call to rd_cpu_dtb, wr_cpu_dtb
;			changed .grpid to .planid for reformatter compatability
;-

bcs_cpu_struct

;	  read the database in

;;new_plan = {plan,cre_date:string(' ',format='(a24)')	$
;;		,length:0,grpid:0	$
;;		,ngrp:bytarr(4),grps:intarr(10,4)}

;;print,'Reading in the Grouper Database'
;;plans = replicate(new_plan,190)
;;get_lun,lundtb
;;openr,lundtb,'micro_dtb:bcs_grpdtb.dat'
;;readu,lundtb,plans
;;close,lundtb
;;free_lun,lundtb

rd_cpu_dtb,'plan',plans,ierr
  if ierr ne 0 then stop,'Error in reading PLAN database'

new_plan = {grouper_plan_rec}

option = ''
ans = ''

;
;		Select required option
;
options:
ttl=-1
print,' '
print,'The available options are:'
print,'  L - list; A - Add; E- Exit
read,'* Enter Option: ',option
option = strupcase(strmid(option,0,1))
if option eq 'E' then goto, endit
if option eq 'L' then goto, list_plan
if option eq 'P' then goto, list_plan_print
if option eq 'A' then goto, add_plan
goto, options

;
;	  	List the contents of the database
;
list_plan_print:
ttl=2
openw,ttl,'grpdtb.lis'

list_plan:
rqgrp=0
print,' '
print,'ROM grouper plans are from 128-184 and are fixed'
print,'Plans for RAM may be loaded in slots 5-127'
read,'* Enter ID of required Grouper Plan [-1 for all]: ',rqgrp
ifrom = rqgrp & ito = rqgrp
if rqgrp eq -1 then begin
  ifrom = 0 & ito = 189
endif

for jp = ifrom,ito do begin
;;  if (jp eq 0) or (plans(jp).planid gt 0) then begin
  if (plans(jp).planid ge 0) then begin		;ID=-1 is unfilled slot
	print,plans(jp).planid,plans(jp).ngrp
	yy = 0
	for jg = 0,3 do begin
		ngrp = plans(jp).ngrp(jg)
		if ngrp eq 0 then ngrp = 2
		print,plans(jp).grps(0:ngrp*2-1,jg)
		yy = [yy,plans(jp).grps(0:ngrp*2-1,jg)]
		endfor
	print,'Total number of bins=',plans(jp).length
	yy = yy(1:*)		;get rid of initial 0
	print,yy,format='(16(x,Z2.2))'
	nyy = n_elements(yy)

if ttl gt 0 then begin
  sformat = '(2x,i4,4x,Z2.2,i8,4x,4i1,2x,' + string(nyy,'(i2)') + 'i4)'
  ;;print,'sformat= ',sformat
  printf, ttl, plans(jp).planid, plans(jp).planid, plans(jp).length, 	$
	plans(jp).ngrp, yy, format=sformat
  endif
  endif
  endfor

if ttl gt 0 then close,2
goto, options

;
;	  	Add a new entry to the database
;
add_plan:
n_modified = 0

new_entry:
print,' '
read,'Are there any (further) Grouper Plan entries?: ',ans
if strupcase(strmid(ans,0,1)) ne 'Y' then goto, end_add_plan

read,'* Enter Grouper Plan ID: ',id
jplan = id
;	  check that ID valid, and not already used
if jplan gt '80'x then begin
	print,'** Plans above 80h are in ROM **'
	goto, new_entry
	endif
if (jplan gt 4) and (jplan eq plans(jplan).planid) then begin
	print,'** An entry is already present with this ID **'
	read,'Continue with entry?: ',ans
	if strupcase(strmid(ans,0,1)) ne 'Y' then goto, new_entry
	endif

new_plan.planid  = jplan		;grouper plan ID
new_plan.cre_date = string(!stime,'(a24)')

xx = intarr(2)
yy = intarr(10)
no = 0 & nof = 0
nsets = 0

for k=0,3 do begin
  read,string('* Enter number of sets for Channel',k,': ','(a,i2,a)'),nsets
;;print,'????? min of two sets ?????'
;;print,'????? can NSETS be 0 ?????'
  new_plan.ngrp(k) = nsets 		;no of sets for this channel
  if nsets gt 5 then begin
	print,'Only 5 sets allowed per channel'
	goto, new_entry
	endif

  if nsets gt 0 then begin
    for js = 0,nsets-1 do begin
	read,string('* Enter set',js,' [n,m]: ','(a,i2,a)'),xx
;;print,'????? can N or M be 0 ?????'
;;print,'????? bytes, thus xx(0) cannot be 256 ?????'
 	no = no + xx(0)
 	nof = nof + xx(0)*xx(1)
;	print,no,nof
	yy(js*2) = xx
	endfor
	new_plan.grps(*,k) = yy		;definition of sets
	endif

   endfor

print,'Total number of bins =',no,nof
new_plan.length = no			;plan length
if nof ne 1024 then begin
	print,'Total number of bins used was not 1024:',nof
	read,'Continue with this entry?: ',ans
	if strupcase(strmid(ans,0,1)) ne 'Y' then goto,new_entry
	endif
	

print,'Completed entry is:'
print,new_plan

read,'Add new plan to Grouper Plan Database?: ',ans
if strupcase(strmid(ans,0,1)) eq 'Y' then begin
;	  check again if entry already exists
	if (jplan gt 4) and (jplan eq plans(jplan).planid) then begin
	  print,'** An entry is already present with this ID **'
	  read,'Do you wish to overwrite it?: ',ans
	  if strupcase(strmid(ans,0,1)) ne 'Y' then goto, new_entry
	  endif
	plans(new_plan.planid) = new_plan
	n_modified = n_modified + 1
	print,'Grouper Plan added at entry',new_plan.planid
	endif

goto, new_entry

end_add_plan:
if n_modified gt 0 then begin

;	  write the database out again if required
  print,' '
;;read,'Write modified Grouper Plan Database?: ',ans
;;if strupcase(strmid(ans,0,1)) eq 'Y' then begin
;;	get_lun,lundtb
;;	openw,lundtb,'micro_dtb:bcs_grpdtb.dat'
;;	writeu,lundtb,plans
;;	close,lundtb
;;	free_lun,lundtb
;;
;;	print,'Modified Grouper Plan Database written'
;;	endif else print,'NO CHANGE made to Grouper Plan Database'

  wr_cpu_dtb,'plan',plans,ierr
  if ierr eq 0 then n_modified = 0

endif


goto, options

endit:

end
