// paramgroup - Accept a parameter group name as the first argument and
//              then read a list of variable types, prefixes and values
//              Each parameter is created in the workspace and bit 14 is set
//		for use in a protocol.  Each parameter is added to a "dg"
//		and "ap" template string and channel parameters are added
//		to a string "clearparams" which is a list of parameters for
//		which bit 14 is reset to zero in the protocol. 

//              Argument 1 is the parameter goup name. This name must begin 
//              with a lower case "suffix" and end with one or more uppercase 
//              "channel identifiers". A channel identifier must be an upper-
//              case letter "A to Z". Parameter names are appended with the 
//              upper-case channel identifiers and then the lower-case suffix. 
//              If a prefix (see below) contains an uppercase charater the channel
//              identifiers are appended as lower case. 

//              Argument 2 is a conditional statement within paraentheses that is
//              used to optionally display the parameter group in the "dg" and "ap" 
//              templates. See chapter 5 of the user programming manual for the 
//              allowed format.  Use the empty string '' if a conditonal statement
//              is not needed. For example '(aXc7 > 0)' will remove the display
//              of the c7X parameter group in "dg" and "ap" of sXc7 = 0. 

//              Arguments 3 to 5 and groups of 3 three thereafter contain 
//              information for individual parameters. 
//              
//                 Argument 3 is the "prefix".  Any lower-case string is allowed
//                 and the prefix may contain an upper-case character as described.  
//                 Solidpack uses conventions for the prefix base on parameter 
//                 types, but paramgroup does not enforce a strict relationship 
//                 between the prefix and the type. The prefix preceeds the channel
//                 identifiers and the suffix in the parameter name. An upper case 
//                 channel identifier can be included in the prefix to deginate 
//                 one channel of two-channel parameter groups. In this case the
//                 actual channel identifiers are supplied in lower case (see above). 
//
//                 Argument 4 is the parameter type.  See the documentation for 
//                 createparam to see the allowed types.  SolidsPack parameter types
//                 are a superset of the VNMRJ parameters types.  

//                 Argument 5 is a default value that is supplied only if the parameter
//                 is actually created. Generally default values have no relationship 
//                 with NMR calibrations.  They are usually values that simply allow 
//                 the sequence to display properly with dps. 

//              Arguments 6 to 8, 9 to 11, etc repeat arguments 3 to 5 for the
//              following parameters. A full set of three arguments must be supplied
//              for each parameter with ending commas (or a final ').  If the format 
//              is incorrect paramgroup will fail to operate. 

//              An example paramgroup statement for the "HX" cross-polarization macro:

//              paramgroup('HX','',
//                    'sh','string','c',                    
//                    'ch','string','fr',
//                    'aH','amplitude',2000,
//                    'aX','amplitude',2000,   
//                    'b','real',0,
//                    'd','real',0,           
//                    't','pulse',1000,
//                    'of','frequency',0,
//                    'fr','channel','dec',
//                    'to','channel','obs'
//              )
//

"******** Obtain and Parse the Parameter-Group Name ********"

$groupname = $1
length($groupname):$groupnamelength
$char =''
$char1 = ''
$chnl = ''
$suffix = ''
$index = 1
while ($index <= $groupnamelength) do
   substr($groupname,$index,1):$char
   format($char,'lower'):$char1
   if ($char <> $char1) then
      $chnl = $chnl + $char
      $index = $index + 1
   else
      if ($chnl = '') then
         $suffix = $suffix + $char
         $index = $index + 1
      else
         $index = $groupnamelength + 1
      endif
   endif
endwhile

$lchnl =''
format($chnl,'lower'):$lchnl

"********Initialize Temporary Parameters********"

exists('clearparams','parameter'):$e
if ($e < 0.5) then
   create('clearparams','string')
   clearparams = ''
endif

exists('aprowindex','parameter'):$e
if $e < 0.5 then
   create('aprowindex','real')
   setlimit('aprowindex',1024,0,1)
   aprowindex = 0
endif

exists('apcharindex','parameter'):$e
if $e < 0.5 then
   create('apcharindex','real')
   setlimit('apcharindex',1024,0,1)
   apcharindex = 0
endif

exists('dgrowindex','parameter'):$e
if $e < 0.5 then
   create('dgrowindex','real')
   setlimit('dgrowindex',1024,0,1)
   dgrowindex = 0
endif

exists('dgcharindex','parameter'):$e
if $e < 0.5 then
   create('dgcharindex','real')
   setlimit('dgcharindex',1024,0,1)
   dgcharindex = 0
endif

"********Add a title row*********" 

$dgcolumnstring = ''
$dgrowindex = 1.0 + trunc(dgrowindex/18)
format($dgrowindex,1,0):$dgcolumnstring

$apcolumnstring = ''
$aprowindex = 1.0 + trunc(aprowindex/54)
format($aprowindex,1,0):$apcolumnstring

$dg = $dgcolumnstring + $2 + ':' + $groupname + ':'
$ap = $apcolumnstring + $2 + ':' + $groupname + ':'

dgcharindex = dgcharindex + $groupnamelength + 3
dgrowindex = dgrowindex + 1

apcharindex = apcharindex + $groupnamelength + 3
aprowindex = aprowindex + 1

if ((aprowindex < 73) and (apcharindex < 1024)) then 
   setprotect('ap','clear',4)
   ap = ap + $ap
   setvalue('ap',ap,'processed')
   setprotect('ap','on',4)
else
   if (aprowindex >= 72) then 
      write('error','Maximum Rows exceeded')
   endif
   if (apcharindex >= 1023) then 
      write('error','Maximum Characters exceeded')
   endif
   abort
endif

if ((dgrowindex < 73) and (dgcharindex < 1024)) then 
   setprotect('dg','clear',4)
   dg = dg + $dg
   setprotect('dg','on',4)
else
   if (dgrowindex >= 72) then 
      write('error','Maximum Rows exceeded')
   endif
   if (dgcharindex >= 1023) then 
      write('error','Maximum Characters exceeded')
   endif
   abort
endif

"******** Sort Parameter Names, Types and Values into 3 Arrays *********" 

$index = 3
$index2 = 1
$indexstring = ''
$value1 = '' 
$value2 = ''
$value3 = ''
$flag = 0

while ($index <= $#) do
   format($index,1,0):$indexstring 
   $indexstring = '$' + $indexstring
   $value1 = {$indexstring}
   length($value1):$value1length
   $index2 = 1
   while ($index2 <= $value1length) do
      substr($value1,$index2,1):$char
      format($char,'lower'):$char1
      if ($char <> $char1) then
         $flag = 1
      endif 
      $index2 = $index2 + 1
   endwhile
   if ($flag > 0.5) then
      $value1 = $value1 + $lchnl + $suffix
      $flag = 0
   else 
      $value1 = $value1 + $chnl + $suffix
   endif
   $index = $index + 1
   format($index,1,0):$indexstring 
   $indexstring = '$' + $indexstring
   $value2 = {$indexstring}
   $index = $index + 1
   format($index,1,0):$indexstring 
   $indexstring = '$' + $indexstring
   if (typeof($indexstring)) then 
      $value3 = {$indexstring}
      createparam($value1,$value2,$value3)
   else 
      $value4 = {$indexstring}
      createparam($value1,$value2,$value4)
   endif
   $index = $index + 1
endwhile

"******** terminate the format strings ********"

length(dg):$dglength
$dg=''
substr(dg,1,$dglength-1):$dg

length(ap):$aplength
$ap=''
substr(ap,1,$aplength-1):$ap

setprotect('dg','clear',4)
dg = $dg + ';'
setprotect('dg','on',4)
setprotect('ap','clear',4)
ap = $ap + ';'
setvalue('ap',ap,'processed')
setprotect('ap','on',4)
"************************************************"

