Viewing contents of file '../idllib/contrib/esrg_ucsb/fordata.pro'
;+ 
; ROUTINE:      fordata
;
; USEAGE:       fordata,name,v,format=format,margin=margin,ng=ng,append=append
;
; PURPOSE:      print a fortran data statement to file "junk" 
;               defining variable NAME with data values v
;
; input:
;   name        name of variable or string to placed between the fortran
;               "data" and "/" keywords, as in 
; 
;               data NAME / ...
;
;   v           vector of values
;
; keyword input
;   format      format specifier (string) of the form 
;               x# or x## x#.# x##.# where x = { i, g, f, e } 
;               and # is a digit (default = 'g10.4').  Note that
;               the format string does not include parenthesis.
;
;   append      append to file "junk"
;
;   margin      number of leading blanks (margin=0 starts at column 7)
;               (default=2)
;
;   ng          number of elements written in an implied fortran loop
;               for example ng=5 in the following snippit of fortran 
;
;               data (v(i),i=1,5)/10,12,24,11,22/
;               data (v(i),i=6,10)/13,44,45,22,33/
;
;               The default value is chosen to produce 20 fortran
;               continuation statements.  pick a smaller value if 
;               the number of continuations allowed on your fortran
;               compiler is less than 20.  Set ng to a large number
;               if no implied loops are desired.
;
; output:       none
; 
; Side effects: writes data statement to file "junk"
;
; EXAMPLE:
;
;   v=sin(findgen(200))
;   fordata,'(v(i),i=1,100)',v(0:99),f='f12.6'
;   fordata,'(v(i),i=101,200)',v(100:199),f='f12.6',/append
;
;; automatically put in fortran implied do list to avoid an excessive
;; number of continuation statements    
;
;   fordata,'v',v
;                                  
;
;  author:  Paul Ricchiazzi                            jun92
;           Institute for Computational Earth System Science
;           University of California, Santa Barbara
;-
pro dataprnt,name,v,format=format,lun=lun,margin=margin

type=strmid(format,0,1)
if margin eq 0 then lb='' else lb=strtrim(string(margin),2)+'x,'
iw=strlen(string(f='('+format+')',0))

ncol=(72-6-margin)/(iw+1)
if ncol eq 0 then  message,"field width too wide"
nn=n_elements(v)
nlines=((nn+ncol-1)/ncol) 

f1=strcompress(string(nlines-1),/remove_all)             ; number of lines - 1
f2=strcompress(string(ncol),/remove_all)                 ; number of columns
f3=strcompress(string(nn-ncol*(nlines-1)-1),/remove_all) ; number of columns
                                                         ;  in last line

if f1 eq 0 then begin
  frmt="(5x,'&',"
endif else begin
  frmt="(" + f1 + "(5x,'&'," + lb + f2 + "(" + format + ",','),/),5x,'&',"
endelse
frmt=frmt + lb

if f3 eq 0 then begin
  frmt=frmt + format + ",'/')"
endif else begin
  frmt=frmt + f3 + "(" + format + ",',')," + format + ",'/')"
endelse

;       f1           f2 frm                 f3  frm     frm
;       vv           vv vvv                 vv  vvv     vvv
;frmt="(  (5x,'&',3x,  (   ,',')),5x,'&',3x,  (   ,','),  ,'/')"

if n_elements(lun) eq 0 then begin
  print,form='(6x,"data ",a,"/")',name
  print,form=frmt,v
endif else begin
  printf,lun,form='(6x,"data ",a,"/")',name
  printf,lun,form=frmt,v
endelse
end

pro fordata,name,v,format=format,append=append,margin=margin,ng=ng

if n_params() eq 0 then begin
  xhelp,'fordata'
  return
end

if n_params() ne 2 then message,'must supply both NAME and V'
if keyword_set(format) eq 0 then format='g10.4'
if n_elements(margin) eq 0 then margin=2
if n_elements(append) eq 0 then append=0

openw,lun,/get_lun,"junk",append=append

iw=strlen(string(f='('+format+')',0))

ncol=(72-6-margin)/(iw+1)
if ncol eq 0 then  message,"field width too wide"

if n_elements(ng) eq 0 then ng=20*ncol

nn=n_elements(v)
nlines=((nn+ncol-1)/ncol) 
if nn gt ng then begin
  i1=0
  while i1 lt nn do begin
    i2=(i1+ng-1)<(nn-1)
    nm='('+name + '(i),i='+string(i1+1,',',i2+1)+')'
    nm=strcompress(nm,/remove)
    dataprnt,nm,v(i1:i2),format=format,lun=lun,margin=margin
    i1=i1+ng
  endwhile
endif
free_lun,lun
return
end