if pro%narg.eq.0 then
  SAY " "
  SAY "Usage:  @ p_show_sed Name [Frequency]"
  SAY "       where "
  SAY "     Name is the name of the File containing the flux as function of frequency"
  SAY "       (with its extension). The adequate file format is created by "
  SAY "       the SED tools in the SED widget".
  return error
endif
!
if .not.file("&1") then
  message E SED "File &1 does not exist"
  let mapping_error yes
  return base
endif
!
greg1\pencil 0 /WE 2
gtvl\clear
set box 6 22 3 19
!
define logical color
let color pro%narg.gt.2
!
if (color) then
  greg1\column x 1 y 2 z 4 /file &1
  define double yy cc /like z
  define double cmax
  let cc z
  compute cmax max cc
  define char*24 ana /like z
  define integer n
  !
  greg1\colu x 1 y 2 z 3
  accept * * * * ana /col &1
else
  greg1\colu x 1 y 2 z 3 /file &1
endif
!
limits
!
!
!  Flux(nu) = Flux(Ref) (nu/Ref)^alpha
!
! Log(Flux(nu)) = Log(Flux(Ref)) + alpha * (Log(nu) - Log(Ref))
!
define double lfnu d_lfnu lnu w_lfnu /like x
define double alpha fRef lRef sRef d_sRef d_alpha
!
let fRef (&2+0)*1D3     ! Frequency in MHz
if fRef.eq.0 then       ! Use mean Frequency if not specified
  compute fRef mean x
endif
let lnu log(x)
let lRef log(fRef)
let lfnu log(y*1d3)     ! Use mJy
let d_lfnu z/y
let w_lfnu 0
let w_lfnu (1/d_lfnu)^2 /where d_lfnu.ne.0
!
! Weighted fit only if error bars are given.
define integer nloop icol
if all(w_lfnu.eq.0) then
  let nloop 1
  define real mark[3]
  let mark marker_sides marker_style marker_size
  greg1\set marker 4 3 * 
else
  let nloop 2
endif
!
if (color) then
  for i 0 to cmax
    icol = mod(i,7)
    GREG1\PENCIL /COLOUR icol
    !
    SIC\LET yy y
    SIC\LET yy -1 /WHERE cc.ne.i
    if (nloop.eq.2) then
      GREG1\POINTS x yy
      error Y x yy z
    else
      points x yy
    endif
  SIC\NEXT
  pen /col 0
  !
  create directory ident
  change directory ident
  define char*24 bna['cmax+1']
  !
  for i 1 to nxy
     let n cc[i]+1
     let bna[n] 'ana[i]'
  next
  delete /var yy cc
  let n 0
  for i 0 to cmax
    icol = mod(i,8)
    if icol.eq.7
      pencil /colour  GREY
    else
      GREG1\PENCIL /COLOUR mod(i,8)
    endif
    let n i+1
    draw text 1 -n 'bna[n]' /char 9
  next
  change directory ..
  !
  pen /col 0
else
  if (nloop.eq.2) then
    GREG1\POINTS x y
    error Y x y z
  else
    points x y
  endif
  pen /col 0
endif
box
!
greg1\limits
if (nloop.eq.1) then
  greg1\set marker mark[1] mark[2] * !! mark[3]
endif
!
pen /weigh 2
greg1\box
greg1\label "Frequency (MHz)" /X
greg1\label "Flux density (mJy)" /Y
!
compute sRef mean lfnu
!
! Weighted Linear regression using the general ADJUST command
!
! [ADJUST\]ADJUST My_Data "My Command" [/START G1 G2 ... Gn] [/STEP S1
!    S2  ..  Sn]  [/EPSILON  e]  [/WEIGHTS  w]  [/METHOD   Powell|Robust|Sim-
!    plex|Slatec|Anneal] [/ITER Niter] [/QUIET] [/ROOT_NAME Name] /PARAMETERS
!    V1 V2 ... Vn [/BOUNDS Va Lowa Higha [Vb Lowb Highb ...]]
!
! or MFIT
!
!        [SIC\]MFIT Yvar=Func(Xvar,&A,&B,...) [/START A1 B1 ...] [/STEP A2 B2
!    ...]  [/EPSILON e] [/WEIGHTS w] [/METHOD m] [/QUIET]
!
!
define double xmin xmax
compute xmin min x
compute xmax max x
!
define chara string*48
define integer nn
let nn 128
define real xx[nn] yy[nn]
let xx[i] xmin+(i-1)*(xmax-xmin)/(nn-1)
!
let string fRef*1D-3 /format f7.2
greg1\draw text 1 -1 "Reference Frequency "'string'" GHz" 6 /char 7
!
for i 1 to nloop
  create directory fit'i'
  change directory fit'i'
  greg1\pencil  2*i-1 /WE 3
  if i.eq.2 then
    MFIT lfnu=&A+&B*(lnu-lRef) /weight w_lfnu /start  'sRef' 0 /quiet
  else
    MFIT lfnu=&A+&B*(lnu-lRef) /start  'sRef' 0 /quiet
  endif
  !
  let sRef exp(mfit%par[1])
  let d_sRef sRef*mfit%errors[1]
  let alpha mfit%par[2]
  let d_alpha mfit%errors[2]
  !
  say "Flux(" fRef*1d-3 " GHz) " sRef  " +/- " d_sRef /format A5 F7.2 A6 F7.1 A6 F7.1
  let string "Flux(Ref) "'0.1*nint(sRef*10)'" +/- "'0.1*nint(d_sRef*10)'
  greg1\draw text 1 -2*i 'string' 6 /char 7
  say "Spectral Index " alpha " +/- " d_alpha /format A18 F7.2 A6 F7.2
  let string  "Alpha     "'0.01*nint(alpha*100)'" +/- "'0.01*nint(d_alpha*100)'
  greg1\draw text 1 -2*i-1 'string' 6 /char 7
  !
  let yy sRef*1D-3*(xx/fRef)^alpha
  greg1\connect xx yy
  !
  change directory ..
  !
next
greg1\pencil 0 /WEIGHT 1
