Viewing contents of file '../idllib/contrib/esrg_ucsb/less.pro'
pro less,p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11,p12,index=index
;+
; ROUTINE:    less
;
; PURPOSE:    print interactively selected portions of arrays or vectors
;
; USEAGE:     less,p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11,p12,index=index
;
;             
;
; INPUT:      
;  p1,p2,...  a set of vector quantities to print. all vectors must
;             be of the same length
;
;
;
; KEYWORD INPUT:
;
;   index     if set, the array indicies of the first parameter, p1,
;             are printed.  This only has effect when p1 has dimension
;             greater than two.  Indicies for array dimensions greater
;             than 5 are not printed. This option disables the 2-D display
;             mode.
;             
;
; DISCUSSION:
;             When the size of the array is greater than can be viewed
;             in one display screen, LESS enters an interactive mode
;             which allows the user to page through the array(s). Many
;             of the movement commands are similar to the unix command
;             "less".  While in the interactive display mode type "h"
;             to get a listing of accepted interactive commands.
;
;             If only one input parameter is specified, LESS will
;             display it as 2-D table with the values of the first
;             index shown across the screen and the values of the
;             second index shown down the screen.  The number of rows
;             and columns are chosen to fit the screen.  Up to 5
;             dimensions can be accomodated.  Movement in the 3rd,4th,
;             and 5th is accomplished by entering index values while
;             in display mode.
;
; EXAMPLE:
;
;    x=sqrt(findgen(20)+23)
;    y=sin(.1*x)
;
;    less,x,y                                            ; 
;    less,x,y,x+y,x-y,x*y,(x+y)*(x-y),x/y
;    less,rebin(x,4,5),y,/index
;
;    less,dist(20,500)
;
;  author:  Paul Ricchiazzi                            may94
;           Institute for Computational Earth System Science
;           University of California, Santa Barbara
;-
;
np=n_params()

if np eq 0 then begin
  xhelp,'less'
  return
endif

term_size,xsize,ysize

if np eq 1 and (size(p1))(0) ge 2 and not keyword_set(index) then begin
  array=1
  ptype=size(p1) & ptype=ptype(ptype(0)+1)
  fieldw=[0,4,8,12,15,25,0,20]
  ncolumns=(xsize-12)/fieldw(ptype)
  
  ny=(size(p1))(2)
  nx=(size(p1))(1)
  ncol=ncolumns<nx
  nrow=(ysize-2)<ny
  i1=0
  k=0
  l=0
  m=0
  parm1='p1(i1:i1+ncol-1,j,k,l,m)'
  fmt='(i,'+strcompress(ncol,/r)
  tfmt='(12x,'+strcompress(ncol,/r)

  case ptype of
   1: begin & fmt=fmt+'i4)'   & tfmt=tfmt+'i4)'    & end  ; byte
   2: begin & fmt=fmt+'i8)'   & tfmt=tfmt+'i8)'    & end  ; integer
   3: begin & fmt=fmt+'i12)'  & tfmt=tfmt+'i12)'   & end  ; long
   4: begin & fmt=fmt+'g)'    & tfmt=tfmt+'i15)'   & end  ; float
   5: begin & fmt=fmt+'g)'    & tfmt=tfmt+'i25)'   & end  ; double
   6: message,'Sorry, cannot deal with complex variables'
   7: begin & fmt=fmt+'(x,a)' & tfmt=tfmt+'i)'     & end  ; string
  endcase

  cmd="for j=j1,j1+nrow-1 do print,f='"+fmt+"',j,"+parm1

endif else begin
  array=0
  ny=n_elements(p1)
  nx=1
  i1=0
  parm1='p1(j)'
  fmt='(i'
  sz=size(p1)
  if keyword_set(index) and sz(0) ge 2 then begin
    index='j mod sz(1),j/sz(1) mod sz(2)'
    if sz(0) ge 3 then index=index+',j/(sz(1)*sz(2)) mod sz(3)'
    if sz(0) ge 4 then index=index+',j/(sz(1)*sz(2)*sz(3)) mod sz(4)'
    if sz(0) ge 5 then index=index+',j/(sz(1)*sz(2)*sz(3)*sz(4)) mod sz(5)'
    if keyword_set(index) then parm1=index+','+parm1
    fmt='('+strcompress(sz(0)+1)+'i'
  endif
  far=['',',i',',i',',i',',g',',g',',g,g',',x,a']
  for nparm=1,12 do begin
    flag=execute("tp=size(p"+strtrim(nparm,2)+")")
    tp=tp(tp(0)+1)
    fmt=fmt+far(tp)
  endfor
  fmt=fmt+')'  

  nrow=(ysize-2)<ny
  ncol=1
  cmd="for j=j1,j1+nrow-1 do print,f='"+fmt+"',j,"+parm1
  for j=2,np do cmd=cmd+",p"+strcompress(string(j),/remove_all)+"(j)"
endelse

j1=0L
if (size(p1))(0) ge 3 then nk=(size(p1))(3) else nk=1
if (size(p1))(0) ge 4 then nl=(size(p1))(4) else nl=1
if (size(p1))(0) ge 5 then nm=(size(p1))(5) else nm=1

lfmt='("enter ",a,$)'
;print,ncol,nrow,' ',cmd

if nrow eq ny and ncol eq nx and (array eq 0 or nk eq 1) then begin
  if array eq 1 then print,f=tfmt,i1+indgen(ncol)
  flag=execute(cmd)
endif else begin
  quit=0
  repeat begin
    if array eq 1 then print,f=tfmt,i1+indgen(ncol)
    flag=execute(cmd)

    if array eq 1 then begin
      wstr=string('(',i1,':',(i1+ncol-1)<(nx-1))
      wstr=wstr + string(',',j1,':',(j1+nrow-1)<(ny-1))
      if (size(p1))(0) ge 3 then wstr=wstr+string(',',k) 
      if (size(p1))(0) ge 4 then wstr=wstr+string(',',l)
      if (size(p1))(0) ge 5 then wstr=wstr+string(',',m)
      wstr=wstr+')'
      wstr=strcompress(wstr,/r)
      print,f='(a,$,": ")',wstr
    endif
       
    case get_kbrd(1) of
      'b': j1=(j1-nrow-1L)>0
      'q': quit=1
      'l': i1=(i1-ncol)>0<(nx-ncol)
      'r': i1=(i1+ncol)>0<(nx-ncol)
      ';': i1=(i1+ncol)>0<(nx-ncol)
      'L': i1=0L
      'R': i1=nx-ncol
      ':': i1=nx-ncol
      'g': j1=0L
      'G': j1=(ny-1L-nrow)
      'j': j1=(j1+1)<(ny-nrow)
      'k': j1=(j1-1)>0L
      ' ': j1=(j1+nrow)<(ny-nrow)
      'i': k=(k+1+nk) mod nk
      'o': l=(l+1+nl) mod nl
      'p': m=(m+1+nm) mod nm
      'I': k=(k-1+nk) mod nk
      'O': l=(l-1+nl) mod nl
      'P': m=(m-1+nm) mod nm
      '1': begin
             print,f=lfmt,strcompress(string('i (i=',i1,' max=',nx-1,')'))
             xxx='' & read,xxx
             valid=min(byte(xxx),max=max) ge byte('0') and max le byte('9')
             if valid(0) then i1=long(xxx)
           end
      '2': begin
             print,f=lfmt,strcompress(string('j (j=',j1,' max=',ny-1,')')) 
             xxx='' & read,xxx
             valid=min(byte(xxx),max=max) ge byte('0') and max le byte('9')
             if valid(0) then j1=long(xxx)
           end
      '3': begin
             print,f=lfmt,strcompress(string('k (k=',k,' max=',nk-1,')')) 
             xxx='' & read,xxx
             valid=min(byte(xxx),max=max) ge byte('0') and max le byte('9')
             if valid(0) then k=long(xxx)
           end
      '4': begin
             print,f=lfmt,strcompress(string('l (l=',l,' max=',nl-1,')')) 
             xxx='' & read,xxx
             valid=min(byte(xxx),max=max) ge byte('0') and max le byte('9')
             if valid(0) then l=long(xxx)
           end
      '5': begin
             print,f=lfmt,strcompress(string('m (m=',m,' max=',nm-1,')')) 
             xxx='' & read,xxx
             valid=min(byte(xxx),max=max) ge byte('0') and max le byte('9')
             if valid(0) then k=long(xxx)
           end
      'h': begin
            for ns=1,ysize-20 do print,''
            print,'q   = quit'
            print,'l   = move left one screen'
            print,'L   = goto first column'
            print,'r,; = move right one screen,  '
            print,'R,: = goto Last column'
            print,'k   = move up 1'
            print,'j   = move down 1'
            print,'b   = move up one screen'
            print,'spc = move down one screen'
            print,'g   = goto top row'
            print,'G   = goto last row'
            print,'i   = increment k (I decrements)'
            print,'o   = increment l (O decrements)'
            print,'p   = increment m (P decrements)'
            print,'1   = set value of first index  (i)'
            print,'2   = set value of second index (j)'
            print,'3   = set value of third index  (k)'
            print,'4   = set value of fourth index (l)'
            print,'5   = set value of fifth index  (m)'
            print,f='(/,"hit any key:",$)'
            dum=get_kbrd(1)
            print,f='(a,a)',string(13b),"            "
           end
     else: print,f='(a,$)',string(7b)
    end
    print,f='(a)',string(13b)
  endrep until quit
endelse

if flag eq 0 then message,'illegal output operation'

end