;setupfile.pro is a collection of programs used to deal with setup-files ; ;version 21/10/2002 HJD ; ;version history ;3/5/1999 first collection of setupfile routines into setupfile.pro ;7/4/2000 added 'nowarn' keyword to parasign ;21/10/2002 added comment keyword to savesetupfile ; ;the calling program needs to contain the following statements before ;accessing any of the programs in this file: ; '@setupfile' ; common parrs,pvarr,pnarr pro readsetupfile,setfname ;reads the setup-file 'setfname' ;and returns two arrays into the common block 'parrs': ;pvarr contains values of the parmeters ;pnarr contains names of the parameters ;HJD 1/1/1998 ;we read the whole file into two arrays of strings common parrs,pvarr,pnarr datstr=strarr(200) ;can deal with 200 parameters comstr=strarr(200) ;lines with comments ndat=0l & ncom=0 & tstr= ' ' openr,UN1,/get_lun,setfname ;do that here so file errors show up NOW while(not eof(UN1)) do begin readf,UN1,tstr ;in following, lines with a length of <= 2 characters or ;lines beginning with '#' are considered comment lines. if (strmid(tstr,0,1) eq '#') or strlen(tstr) le 2 then begin comstr(ncom)=tstr ncom=ncom+1 endif else begin datstr(ndat)=tstr ndat=ndat+1 endelse endwhile if ndat ge 1 then datstr=datstr(0:ndat-1) if ncom ge 1 then comstr=comstr(0:ncom-1) ;print,ndat,' parameter values read from file ',setfname ;print,ncom,' comment lines read' free_lun,UN1 ;define data arrays pnarr=strarr(ndat) pvarr=strarr(ndat) ;read in the main data block for k=0,ndat-1 do begin tok=str_sep(strtrim(strcompress(datstr(k)),2)," ") if n_elements(tok) lt 2 then print,"error in setupfile with line: ",tok pnarr(k)=tok(0) pvarr(k)=tok(1) ; print,pnarr(k)," : ",pvarr(k) endfor ;end of reading stuff end function parasign,parname,default,nowarn=nowarn ;assigns a paramaeter 'parname' to its value. ;used in combination with readsetupfile.pro ;The value that belongs to parname from ;arrays pvarr and pnarr ;is returned in variabletype of ;pvarr, which is normally of type string ; ;the second parameter is optional and assigns a ;default value if parname is absent. ;nowarn supresses printing of warning messages ; ;HJD 7/4/2000 common parrs,pvarr,pnarr foundflag = 0 for i=0,n_elements(pnarr)-1 do begin if pnarr(i) eq parname then begin parvalue = pvarr(i) foundflag = 1 endif endfor if keyword_set(nowarn) then wflag=0 else wflag=1 if foundflag eq 0 then begin ;no default specified if n_params() eq 1 then begin ;an error in param-file is present print,"STOPPED: parameter ",parname," not found in parameter file" stop endif if n_params() eq 2 then begin ;default is being used if wflag then begin print,"WARNING: parameter ",parname," not found in parameter file" print," using default: ", parname," = ",default endif parvalue=default endif endif return,parvalue end function paratest,parname,warn=warn ;tests, if parameter 'parname' is in pnarr ;yes: returns 1, no: returns 0 ;warnflag is optional and returns warning if parname is absent ;used in combination with readsetupfile.pro ;HJD 27/4/1999 common parrs,pvarr,pnarr foundflag = 0 for i=0,n_elements(pnarr)-1 do begin if pnarr(i) eq parname then foundflag = 1 endfor if foundflag eq 0 and keyword_set(warn) then print,"WARNING: parameter ",parname," not found in setup-file" return,foundflag end pro paraput,parname,value ;assigns a new value to parameter 'parname' ;which is kept in the pnarr array. ; ;Intended for use with program-modified setup files saved by ;subsequent call to 'savesetupfile' ; ;HJD 30/4/1999 common parrs,pvarr,pnarr foundflag = 0 for i=0,n_elements(pnarr)-1 do begin if pnarr(i) eq parname then begin pvarr(i)=value foundflag = 1 endif endfor if foundflag eq 0 then print,"Error: parameter: ",parname," not in setup file" end pro savesetupfile,setfname,comment=comment ;saves setupfile from pvarr,pnarr ;used to save modified setupfiles ;comment lines are not saved in this version ;if comment keyword is set, setupfile is prepended by '#' comment symbols ;HJD 30/4/1999 ; 21/10/2002 added comment keyword common parrs,pvarr,pnarr if keyword_set(comment) then comchar="# " else comchar="" openw,unout,setfname,/get_lun printf,unout,"# auto-saved setup file" for i=0,n_elements(pvarr)-1 do begin printf,unout,comchar,pnarr(i)," ",pvarr(i) endfor free_lun,unout end