Function magscl,img,lin,max
;
;+
;    Name: magscl
;
;    Purpose: 
;      Makes a new img byte-scaled appropriately for displaying magnetograms
;      (or any signed image whose data is symmetrical about 0 in meaning)
;      lin -- approx. linear byte-scaling around grey (127) for -lin < img < +lin
;      max = saturation value set to 254 (0) for img > max (< -max)
;      For lin << abs(img) < max, scaling is approx. logarithmic
;      To emphasize details in weak (strong) fields, make lin smaller (larger)
;
;    Input Parameters:
;      img - 
;      lin -
;      max -
;
;    Output Parameters:
;
;    Keyword Parameters:
;      None
;
;    Calling Example:
;      blapp    = off_limb_mask*bytscl(magscl(off_limb_mask*blapp, 25, 1000)); <-- from sp_make_cube_index.pro
;
;    History:
;       TDT  9-Mar-2003
;       PGS 07-Jul-2020 Cleaned up formatting and added standard comment structure.
;
;
;-
;
lin = abs(lin)  &  max=abs(max)
t=float(img)
w=where(t ge 0.)
wm=where(t lt 0.)
;t(w)=127+bytscl(sqrt(t(w)/max),0.,1.)/2
;t(wm)=127-bytscl(sqrt(abs(t(wm)/max)),0.,1.)/2
t(w)=t(w)/lin
t(wm)=abs(t(wm)/lin)
tmax=float(max)/float(lin)
tmax=alog(tmax+sqrt(1.+tmax^2))
t=bytscl(alog(t+sqrt(1.+t^2)),0.,tmax)/2
t(w)=127+t(w)
t(wm)=127-t(wm)
return,byte(t)
end
