;+
; NAME:
;	BCS_GRP_PLAN
; PURPOSE:
;	Return information about a BCS mode
; CATEGORY:
; CALLING SEQUENCE:
;	bcs_grp_plan, modeID, nGroup, Group, nSampPChan, qdefined
; INPUTS:
;	modeID		ID of BCS mode
; OPTIONAL INPUT PARAMETERS:
; KEYWORD PARAMETERS:
; OUTPUTS:
;	nGroup		4 long vector; No. of groups for each channel
;	Group		m*4 array of group definition. "m" is derived 
;			as max(ngroup)*2. See below for more info.
;	nSampPChan	No. of sampes per channel
;	qdefined	1 if mode is defined, 0 if not.
;
;  group(*,ichan) = [nout1,nbin1, nout2,nbin2, nout3,nbin3, ...]
;		where ichan is then channel in question
;			nout1 is the number of output values
;			nbin1 is the number of raw input values binned
;
;
; OPTIONAL OUTPUT PARAMETERS:
; COMMON BLOCKS:
;	hold_grp_plan	stored database structure of grouper plans
; SIDE EFFECTS:
; RESTRICTIONS:
;	BCS modeID must be in range 0 to 189; not all are defined
; PROCEDURE:
; MODIFICATION HISTORY:
;	MDM  Autumn-91	Written
;	RDB  18-May-94	Took out hardwired modes - use database
;-

pro bcs_grp_plan, modeID, nGroup, Group, nSampPChan, qdefined

common  hold_grp_plan,grp_plan


qdefined = 0
nGroup = intarr(4)		;not really needed, but null values
nSampPChan = intarr(4)

;	read the grouper plan database
if n_elements(grp_plan) eq 0 then begin
  bcs_cpu_struct
  rd_cpu_dtb,'plan',grp_plan,ierr
  if ierr ne 0 then begin
      print,'** Problem reading BCS Grouper database **'
      return
  endif
endif

qmodeid = modeID

if modeid gt n_elements(grp_plan)-1 then begin
    print,'** Mode ID is too large.  Mode ID = ', modeid
    qmodeID = 0
endif

;	extract the mode
mode = grp_plan(qmodeID)
qdefined = 1

if mode.planid lt 0 then begin
    print,'** Mode ID not recognized.  Mode ID = ', modeid
    qmodeID = 0
endif

if qmodeID ne modeID then begin
    print,'** Using mode 0 **'
    mode = grp_plan(0)
    qdefined = 0
endif


;	extract nGroup and part of grps needed
nGroup = fix(mode.ngrp)

vec = indgen(max(nGroup)*2)
Group = mode.grps(vec,*)

;print,mode.grps
;print,ngroup
;print,group


;	derive no. of samples per channel
nSampPChan = intarr(4)
for ichan=0,3 do begin
    n = 0
    for j=0,nGroup(ichan)-1 do n = n + group(j*2, ichan)
    nSampPChan(ichan) = n
end
;
end
