; $Id: opan_selectgroups_focus_runs.pro,v 1.1 2005/11/16 09:22:10 tregenna Exp $
;###############################################################################
;
; NAME:
;  OPAN_SELECTGROUPS
;
; PURPOSE:
;  Function that returns an array of groups (integers) contained
;  in a standard hyphenated or comma-separated format.
;
; CATEGORY:
;  DAVE, Data Analysis, PAN, curve fitting
;
; AUTHOR:
;   Robert M. Dimeo, Ph.D.
;   NIST Center for Neutron Research
;   100 Bureau Drive
;   Gaithersburg, MD 20899
;   Phone: (301) 975-8135
;   E-mail: robert.dimeo@nist.gov
;   http://www.ncnr.nist.gov/staff/dimeo
;
; LICENSE:
;  The software in this file is written by an employee of
;  National Institute of Standards and Technology
;  as part of the DAVE software project.
;
;  The DAVE software package is not subject to copyright protection
;  and is in the public domain. It should be considered as an
;  experimental neutron scattering data reduction, visualization, and
;  analysis system. As such, the authors assume no responsibility
;  whatsoever for its use, and make no guarantees, expressed or
;  implied, about its quality, reliability, or any other
;  characteristic. The use of certain trade names or commercial
;  products does not imply any endorsement of a particular product,
;  nor does it imply that the named product is necessarily the best
;  product for the stated purpose. We would appreciate acknowledgment
;  if the DAVE software is used of if the code in this file is
;  included in another product.
;
;###############################################################################
;==========================================
function opan_selectgroups_FOCUS_RUNS,strdets,group_leader = group_leader
;
; Input:    string containing detector numbers separated either by
;           commas or - (hyphens).  A hyphen indicates to include the
;           two detectors and all detectors in between.  A comma simply
;           means to append that detector to the current list.
;
; Output:   a pointer to an array containing all of the detectors to be
;      summed together.
;
; Catch errors in input string
;nerror = strmid(strdets,0)
nerror = strmid(strdets,0,1)
if (nerror[0] eq '-') or (nerror[0] eq ',') or (nerror[0] eq ' ') then begin
  void = dialog_message(dialog_parent=group_leader,'Not a valid input field')
  return,0
endif
strdets = strcompress(strdets)
test1 = strpos(strdets,'-,')
test2 = strpos(strdets,',-')
if test1[0] ne -1 or test2[0] ne -1 then begin
  void = dialog_message(dialog_parent=group_leader,'Not a valid input field')
  return,0
endif

; count the number of commas occurring in the string
i = 0
inum = 0
cnt = 0
commacnt = intarr(500)
while (inum ne -1) do begin
  i = strpos(strdets,',',i)
  inum = i[0]
  if (inum ne -1) then begin
    commacnt[cnt] = inum
    cnt = cnt+1
    inum = inum + 1
    i = inum
  endif
endwhile
ncommas = cnt
if ncommas gt 0 then begin
  commaPos = intarr(ncommas)
  commaPos = commacnt[0:ncommas-1]
endif
;commaPos = [-1,commaPos[0:ncommas-1]]

; if there are no commas then there are two possibilities
; either there is a single detector present.....
ndash = strpos(strdets,'-')
if (ncommas eq 0) and (ndash[0] eq -1) then begin
  ;detnum = fix(strdets)
  detnum = long(strdets)
  ndets = 1
  dets = detnum
endif

; or there are a sum of detectors present
if (ncommas eq 0) and (ndash[0] ne -1) then begin
  pos = strpos(strdets,'-')
  len = strlen(strdets)
  numstr1 = strmid(strdets,0,pos[0])
  numstr2 = strmid(strdets,pos[0]+1,len[0]-1)
 ; num1 = fix(numstr1)
  num1 = long(numstr1)
 ; num2 = fix(numstr2)
  num2 = long(numstr2)
;print,'num1=',num1
;print,'num2=',num2
if num1 gt num2 then begin
  void = dialog_message(dialog_parent=group_leader,'Not a valid input field')
  return,0
endif
  ndets = num2[0] - num1[0] + 1
 ; dets = intarr(ndets)
  dets = lonarr(1000)
  dets = num1[0] + INDGEN(ndets)
endif

; are there one or more commas?
if (ncommas gt 0) then begin
;  dets = intarr(1000)
  dets = lonarr(1000)
  totlen=strlen(strdets)       ; total length of the input string
  smallLen = intarr(ncommas+1)  ; array defining the length of all substrings in string
                    ; variable strdets
  smallLen[0] = commaPos[0]
  for i=1,ncommas-1 do begin
      smallLen[i] = commaPos[i]-commaPos[i-1]-1
  endfor
  smallLen[ncommas] = totlen-commaPos[ncommas-1]-1

  substrings = strarr(ncommas+1)    ; string array representing string form of detectors to sum
  substrings[0]=strmid(strdets,0,smallLen[0])
  for i = 1,ncommas do begin
    substrings[i]=strmid(strdets,commaPos[i-1]+1,smallLen[i])
  endfor

  ; convert substrings to an array of numbers
  count = 0
  for i = 0,ncommas do begin

    dashpresent = strpos(substrings[i],'-')
    dash = dashpresent[0]

    if dash eq -1 then begin
 ;     detnum = fix(substrings[i])
      detnum = long(substrings[i])
;      print,'detnum=',detnum
      ndets = 1
      dets[count] = detnum[0]
      count = count+1
    endif

    if dash ne -1 then begin

      pos = strpos(substrings[i],'-')
      len = strlen(substrings[i])
      numstr1 = strmid(substrings[i],0,pos[0])
      numstr2 = strmid(substrings[i],pos[0]+1,len[0]-1)
;      print,'numstr1=',numstr1
;      print,'numstr2=',numstr2
  ; num1 = fix(numstr1)
  num1 = long(numstr1)
 ; num2 = fix(numstr2)
  num2 = long(numstr2)
      ndets = num2[0] - num1[0] + 1
      singledets = intarr(ndets)
      singledets = num1[0] + INDGEN(ndets)
      dets[count:count+ndets-1] = singledets[0:ndets-1]
      count = count+ndets
    endif
  endfor

ndets = count
;tempdets = intarr(ndets)
tempdets = lonarr(ndets)
tempdets[0:ndets-1] = dets[0:ndets-1]
;dets = intarr(ndets)
dets = lonarr(ndets)
dets[0:ndets-1] = tempdets[0:ndets-1]
endif
RETURN,dets
END