!------------------------------------------------------------------------------
begin procedure project_restore
   on error return
   let proj 'proj' /lower
   let proj 'proj' /lower
   sic user                                ! rn
   if (isaod) then    ! rn
      sic dir "/SOG/project/"'proj'"/"     ! rn
   endif                                   ! rn
   define character ff*20
   let ff 'proj'.pro
   if file(ff) then
      @ 'proj'.pro
      say "Restored " 'project_entered'
   else
      say "No project parameter file for "'proj'" ("'proj'".pro)"
      delete /variable project_entered
      @ project_input1
   endif
   let do_line = YES
   let do_cal = YES
   say " "
   say "Widget should list :"
   say " "
   say "             number of sources = " 'n_sources'
   say "    number of continuum tables = " 'n_cont'
   say "         number of line tables = " 'n_line'
   say " "
   say "Press <restore> as long as necessary !"
end procedure project_restore
!------------------------------------------------------------------------------
begin procedure project_save
   let proj 'proj' /lower
   let proj 'proj' /lower
   on error return
   if (isaod) then    ! rn
      sic dir "/SOG/project/"'proj'"/"         ! rn
   endif                                   ! rn
   define character text*80
   define logical answer
   sic user
   sic date
   let text 'proj'.pro
   if file(text) then
      let answer /prompt "I-SAVE, Overwrite File "'text'" [YES or NO] ?"
      if .not.answer then
         return
      endif
   endif
   sic out 'proj'.pro
   say "!------------------------------------------------------------------"
   say "!" 'proj'.pro
   say "!  Project parameter file created by " 'sys_info'  'sys_date'
   say "!------------------------------------------------------------------"
   say "if exist(project_entered) then"
   say "  delete /variable project_entered "
   say "end if"
   say "if .not.exist(n_cont) then"
   say "  define integer n_cont /global"
   say "end if"
   say "if .not.exist(proj) then"
   say "  define character proj*4 /global"
   say "end if"
   say "if .not.exist(n_line) then"
   say "  define integer n_line /global"
   say "end if"
   say "if .not.exist(n_sources) then"        ! rn
   say "  define integer n_sources /global"   ! rn
   say "end if"                               ! rn
   say "let n_sources "'n_sources' 
   say "let n_cont "'n_cont' 
   say "let n_line "'n_line' 
   for i 1 to n_sources
      say "if .not.exist(sourcename_"'i'") then"
      say "   define character sourcename_"'i'"*12 ra2000_"'i'"*16 -"
      say "       dec2000_"'i'"*16 xfield_"'i'"*120 yfield_"'i'"*120 -"
      say "       tfield_"'i'"*120 /global"
      say "   define double vlsr_"'i'" /global"
      say "   define integer nfield_"'i'" /global"
      say "end if"
      let text sourcename_'i' /format a12
      say "let sourcename_"'i'  """"'text'""""
      let text ra2000_'i' /format a16
      say "let ra2000_"'i'    """"'text'""""
      let text dec2000_'i' /format a16
      say "let dec2000_"'i'    """"'text'""""
      let text vlsr_'i' /format f10.3
      say "let vlsr_"'i'  'text'
      let text  nfield_'i' /format i2
      say "let nfield_"'i'  'text'
      let text  xfield_'i' /format a120
      say "let xfield_"'i' """"'text'""""
      let text  yfield_'i' /format a120
      say "let yfield_"'i' """"'text'""""
      let text  tfield_'i' /format a120
      say "let tfield_"'i' """"'text'""""
   next
   for i 1 to n_cont
      say "if .not.exist(conttable_"'i'") then"
      say "   define character conttable_"'i'"*20 contbands_"'i'"*80 /global"
      say "end if"
      let text conttable_'i' /format a80 
      say "let conttable_"'i' """"'text'""""
      let text contbands_'i' /format a80 
      say "let contbands_"'i' "-"
      say """"'text'""""
   next
   for i 1 to n_line
      say "if .not.exist(linetable_"'i'") then"
      say "   define character linetable_"'i'"*20 /global"
      say "   define character linename_"'i'"*20  /global"
      say "   define character linevelo_"'i'"*80  /global"
      say "   define double linefreq_"'i'"        /global"
      say "   define double linevres_"'i'"        /global"
      say "end if"
      let text linetable_'i' /format a80  ;say "let linetable_"'i' """"'text'""""
      let text linename_'i' /format a80   ;say "let linename_"'i' """"'text'""""
      let text linevelo_'i' /format a80   ;say "let linevelo_"'i' """"'text'""""
      let text linefreq_'i' /format f16.3 ;say "let linefreq_"'i' 'text'
      let text linevres_'i' /format f16.3 ;say "let linevres_"'i' 'text'
   next
   say "define character project_entered*80 /global"
   sic user
   sic date
   say "let project_entered -"
   say """" 'proj' "parameters saved by " 'sys_info' 'sys_date' """"
   sic out
!
! execute once to check it:
   @ 'proj'.pro
end procedure project_save
!------------------------------------------------------------------------------
begin procedure project_setup
   on error return
   if (astro) then
!     create catalog:
      @ create_catalog
!     now create setup:
      @ create_setup
   endif
! now create clic procedures:
   @ create_tables
!
end procedure project_setup
!------------------------------------------------------------------------------
! gets a single argument (character chain)
! creates a list of values ;
! the first argument is a character chain with the values
! the second (optional) is the number of values
!
begin procedure find_values
   on error return
   if exist(n_values) then
      delete /var n_values
   endif
   if exist(values) then
      delete /var values
   endif
   define integer n_values /global
   if "&2?".eq."?" then 
      let n_values 0
      for i &1 
         let n_values n_values+1  
      next
   else 
      let n_values &2
   endif
   define double values[n_values] /global
   let values &1
end procedure find_values
!------------------------------------------------------------------------------
begin procedure create_tables
   on error return
   define character sname*12 tname*80 lname*80 windows*80 text1*80 text2*80
   def double freq velo[2] vres
   define integer receiver nch k
   let k 1
   sic user
   sic date
!
! continuum
!
   for i 1 to n_cont
      let proj 'proj' /lower
      let proj 'proj' /lower
      let windows contbands_'i' /format a80
      let tname conttable_'i' /format a80
      sic out 'proj'"-"'tname'.clic
      sic user
      sic date
      say "!------------------------------------------------------------------"
      say "!" 'proj'"-"'tname'.clic
      say "!  Continuum table procedure created by " 'sys_info'  'sys_date'
      say "!------------------------------------------------------------------"
      say "set default"
      say "set averag scan"
      @ find_values 'windows'        
      if values[1].ge.275000 then    
         say "set receiver 4"        
      else if values[1].gt.199000 then    
         say "set receiver 3"        
      else if values[1].gt.120000 then    
         say "set receiver 2"        
      else                           
         say "set receiver 1"        
      endif                          
      say "set amplitude   antenna relative"
      say "set phase       antenna relative atm"
      say "set rf          antenna on"
      say "set quality 	   average"
      say "set type 	   object"
      say "set procedure corr"
      say "set selection continuum ssb l09 to l12 /window -"
      say 'windows'
      say "set level 6"
      say "file in 'file_name'"
      @ say_table 'tname' dummy arguments
      sic out 
   next
!
! line
!
   for i 1 to n_line
      let tname linetable_'i' /format a80
      let lname linename_'i'  /format a80
      sic out 'proj'"-"'tname'.clic
      say "!------------------------------------------------------------------"
      say "!" 'proj'"-"'tname'.clic
      say "!  Line table procedure created by " 'sys_info'  'sys_date'
      say "!------------------------------------------------------------------"
      say "set default"
      say "set drop 0.002 0 " ! from polyfix 
      say "set averag scan"
      say "set amplitude   antenna relative"
      say "set phase       antenna relative atmosphere"
      say "set rf          antenna on"
      say "set quality 	   average"
      let freq linefreq_'i'
      let vres linevres_'i'
      let text1 linevelo_'i' /format a80
      @ find_values 'text1'
      let velo[1] values[1]
      let velo[2] values[2]
      if (abs(velo[1])+abs(velo[2]).lt.1e-5) then
         let nch 180
      else
         let nch (nint(velo[2]-velo[1])|vres)
      endif
      if freq.gt.275000 then    
         say "set receiver 4"        
      else if freq.gt.199000 then    
         say "set receiver 3"        
      else if freq.gt.120000 then    
         say "set receiver 2"        
      else                           
         say "set receiver 1"        
      endif                          
      say "set type 	   object"
      say "set procedure corr"
      if (abs(velo[1])+abs(velo[2]).lt.1e-5) then
        say "set selection line dsb l09 to l12 "
      else
        say "set selection line dsb all "
      endif
      say "set level 6"
      say "file in 'file_name'"
      if (abs(velo[1])+abs(velo[2]).lt.1e-5) then
        @ say_table 'tname' "/frequency "'lname'" "'freq' - 
          "/resample 180 91 0 20 F"
      else
        @ say_table 'tname' "/frequency "'lname'" "'freq' - 
          "/resample "'nch'" "'(nch+1.)|2'" "'(velo[1]+velo[2])|2'" "'vres'" V"
      endif
      sic out 
   next
!
end procedure create_tables
!------------------------------------------------------------------------------
begin procedure say_table
   on error return
   if (isaod) then    ! rn
      sic dir "/SOG/project/"'proj'"/"         ! rn
   endif                                   ! rn
   define character sname*12 tname*40 text1*80 
   define integer nf
   sym scanlist "delete"
   del /sym scanlist
   let tname &1
   for j 1 to n_sources
      let sname sourcename_'j' /format a12
      let nf nfield_'j'
      if nf.gt.1 then
         def double offx[nf] offy[nf]
         let text1  xfield_'j' /format a120
         @ find_values 'text1' 'nf'
         let offx 0.01*nint(100.*values)
         let text1  yfield_'j' /format a120
         @ find_values 'text1' 'nf'
         let offy 0.01*nint(100.*values)
         for k 1 to nf
            say "find /source "'sname'" /offset "'offx[k]'" "'offy[k]'" /scan 'scanlist'"
            say "if found.gt.0 then"
            say "  list /short"
            if n_sources.gt.1 then
               let text1 'proj'"-"'tname'"-"'sname'"-"'k'
            else
               let text1 'proj'"-"'tname'"-"'k'
            end if
	    if (.not.isaod) then             ! rn
               say "  if file("""'text1'".uvt"") then"       
               say "    table "'text1'" OLD "                
               say "  else"           
               if "a&2a".eq."adummya" then                                 
                  say "    table "'text1'" NEW " 
               else                                
                  say "    table "'text1'" NEW -"  
                  say "       &2 &3 "   
               endif                        
               say "  end if"                                
               say "end if"                                  
            else                                                ! rn
               if "b&2b".eq."bdummyb" then
                  say "  table "'text1'" NEW "                  ! rn 
               else     
                  say "  table "'text1'" NEW -"                 ! rn        
                  say "       &2 &3 "   
               endif                           ! rn
               say "  sys ""!cp -f "'text1'".uvt /SOG/project/SAVED-UVT/"'text1'"-""'short_file'"".uvt""" ! rn
               say "end if"                                  ! rn
	    endif                                            ! rn   
         next
      else
         say "find /source "'sname'" /scan 'scanlist'"
         say "if found.gt.0 then"
         say "  list /short"
         if n_sources.gt.1 then
            let text1 'proj'"-"'tname'"-"'sname'
         else
            let text1 'proj'"-"'tname'
         end if
	 if (.not.isaod) then             ! rn
            say "  if file("""'text1'".uvt"") then"       
            say "    table "'text1'" OLD "                
            say "  else"               
            if "a&2a".eq."adummya" then                   
               say "    table "'text1'" NEW " 
            else                        
               say "    table "'text1'" NEW -"                              
               say "       &2 &3 "   
            endif                            
            say "  end if"                                
            say "end if"                                  
         else                                             ! rn 
            if "b&2b".eq."bdummyb" then
               say "  table "'text1'" NEW "                 ! rn 
            else 
               say "  table "'text1'" NEW -"                 ! rn                                  
               say "       &2 &3 "   
            endif                
            say "  sys ""!cp -f "'text1'".uvt /SOG/project/SAVED-UVT/"'text1'"-""'short_file'"".uvt""" ! rn
            say "end if"                                  ! rn
	 endif                                            ! rn   
      end if
   next
end procedure say_table
!------------------------------------------------------------------------------

