" BPsvf - modified svf to save the data set, including all WFG files and PSG code"

if ($#>0.5) then
   $args = 'SVF(\''+$1
   $i=1
   while ($i<$#) do
      $i = $i + 1
      $args = $args+'\',\''+${$i}
   endwhile
   $args = $args+'\')'
   exec($args)
   $cmd='cp ' + curexp + '/*.def ' + $1 + '.fid 2> /dev/null;cat'
else
   $filename = ''
   SVF:$filenamej     "saves data(binary), exp log, text file and parameter set"
   $cmd='cp ' + curexp + '/*.def ' + $filename + ' 2> /dev/null;cat'
endif
shell($cmd):$dum

exists('/vnmr/maclib/appdirs','file'):$appdir
if ($appdir=0) then
   exists(userdir+'/seqlib/'+seqfil+'.psg','file'):$SPcodeinlocalseqlib  "saves spinCAD code, if present"
   if ($SPcodeinlocalseqlib=1) then
      copy(userdir+'/spincad/psglib/'+seqfil,$1+'.fid')
   endif
   exists(userdir+'/seqlib/'+seqfil+'.c','file'):$ccodeinlocalseqlib     "saves sequence code"
   if ($ccodeinlocalseqlib=1) then
      copy(userdir+'/seqlib/'+seqfil+'.c',$1+'.fid')
   endif
   if (($SPcodeinlocalseqlib=0)and($ccodeinlocalseqlib=0)) then
      exists('/vnmr/seqlib/'+seqfil+'.psg','file'):$SPcodeinvnmrseqlib   "saves spinCAD code, if present"
      if ($SPcodeinvnmrseqlib=1) then
         copy('/vnmr/spincad/psglib/'+seqfil,$1+'.fid')
      endif
      exists('/vnmr/psglib/'+seqfil+'.c','file'):$ccodeinvnmrseqlib      "saves sequence code"
      if ($ccodeinvnmrseqlib=1) then
         copy('/vnmr/psglib/'+seqfil+'.c',$1+'.fid')
      endif
   endif
else
   exists(seqfil+'.psg','seqlib'):$ret,$spincadseqlibpath
   if ($ret>0) then
      exists(seqfil+'.psg','spincad/psglib'):$ret,$spincadpsglibpath
      copy($spincadpsglibpath,$1+'.fid')
   endif

   exists(seqfil+'.c','seqlib'):$ret,$psglibpath 
   if ($ret=0) then
      exists(seqfil+'.c','psglib'):$retpsg,$psglibpath
   endif
   if ($ret>0) then
      copy($psglibpath,$1+'.fid')
   else
      if ($retpsg>0) then copy($psglibpath,$1+'.fid') endif
   endif
endif

copy(userdir+'/global',$1+'.fid')           "saves global"

// Save Waveforms Using Older Software

exists(curexp+'/psgFile','file'):$fileexists
if ($fileexists=1) then
   nrecords(curexp+'/psgFile'):$lines
   if ($lines>0) then
      $counter=0
      lookup('file',curexp+'/psgFile')
      repeat
         lookup('read'):$record
         cp($record,$1+'.fid')             "saves all .RF and .DEC files"
         $counter=$counter+1
      until ($counter=$lines)
   endif
endif

// Save Waveforms Using VJ2.2C or Later

exists(curexp+'/PsgFile','file'):$fileexists
if ($fileexists=1) then
   nrecords(curexp+'/PsgFile'):$lines
   if ($lines>0) then
      $counter=0
      lookup('file',curexp+'/PsgFile')
      repeat
         lookup('read'):$record
         cp($record,$1+'.fid')             "saves all .RF and .DEC files"
         $counter=$counter+1
      until ($counter=$lines) 
   endif
endif

// Save A Copy of Probefile

exists(userdir+'/probes/'+probe+'/'+probe,'file'):$e
if $e then
   cp(userdir+'/probes/'+probe+'/'+probe,$1+'.fid')  "use user probefile"
else
   exists(systemdir+'/probes/'+probe+'/'+probe,'file'):$e
   if $e then
       cp(systemdir+'/probes/'+probe+'/'+probe,$1+'.fid')
   endif
endif

// Non-Linearly Sampled Data Section                              "

$filename=$1+'.fid'
exists('SPARSE', 'parameter'):$SPARSE
if $SPARSE then
   if (SPARSE='y') then
      exists(sparse_dir+'/'+sparse_file+'.in', 'file'):$spin
      exists(sparse_dir+'/'+sparse_file+'.hdr_3', 'file'):$sphdr1
      if ($spin) then  
         $cmd1='cp '+sparse_dir+'/'+sparse_file+'.in '+$filename
         if $sphdr1 then
            $cmd2='cp '+sparse_dir+'/'+sparse_file+'.hdr_* '+$filename
         else 
            $cmd2=''
         endif
      else 
         $cmd1=''
         $cmd2=''
      endif 
      $cmd3='mkdir '+$filename+'/proc'
      $args=$filename+'/proc/'
      shell($cmd1):$dum
      shell($cmd2):$dum
      shell($cmd3)
      BP_varian2pipeNLS($args,'sfrq')
      BP_varian2pipe($filename+'/proc/','sfrq')
   endif

 " $cmd3='mkdir '+$filename+'/proc' "
 " shell($cmd3) "
 " BP_varian2pipe($filename+'/proc/','sfrq') "

endif  

// Explicit Sampling

exists('EXP_SAM', 'parameter'):$exs
if $exs then
   exists(timetab_name, 'file'):$ttn 
   if $ttn then
      copy(timetab_name,$1+'.fid')
   endif
endif
