From: Muthu <gnu...@us...> - 2007-02-10 21:28:20
|
Update of /cvsroot/octave/octave-forge/main/physical-constants In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv24499 Modified Files: gen.py Log Message: generator updated to produce a new modified constants file Index: gen.py =================================================================== RCS file: /cvsroot/octave/octave-forge/main/physical-constants/gen.py,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- gen.py 10 Feb 2007 09:59:03 -0000 1.1 +++ gen.py 10 Feb 2007 21:28:14 -0000 1.2 @@ -17,7 +17,8 @@ import sys from curses.ascii import * -def make_oct_func(name,Description,Val,Units,Uncertainity): +def make_oct_func(name,Description,Val,Units,Uncertainity,path): + sys.stdout=open(path + string.upper(name)+".m","w"); print "## Copyright (C) 2007 Python Code Generator " print "##" print "## -- WARNING -- Autogenerated - DONOT EDIT -" @@ -61,13 +62,138 @@ print "%" print "%%!assert(%s,%s,eps)"%(string.upper(name),Val) print "%" + sys.stdout.close() + return + +def make_physconst_func(func_metadata,PATH): + ostd=sys.stdout; + sys.stdout=open(PATH+'/physical_constant.m','w'); + print "## Copyright (C) 2007 Muthiah Annamalai " + print "##" + print "## This code is released under GPL" + print """## You should have received a copy of the GNU General Public License +## along with Octave; see the file COPYING. If not, write to the Free +## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +## 02110-1301, USA.""" + print "" + print "## -*- texinfo -*-" + print "## @deftypefn {Function File} [@var{Val},@var{unit},@var{uncertanity},@var{desc},@var{name}] {} =physical_constant(@var{arg})" + print "## @var{arg} is the name of the physical constant to retrieve." + print "## The output arguments are the attributes which you need. " + print "## By default, the return value is the constant value." + print "## if no arguments are passed, depending on the output arguments the entire units structure is returned" + print """## @var{Val} is actual value of the constant. +## @var{Unit} is a Units string. +## @var{uncertanity} is +/- value to constant.""" + print "##" + print "## Autogenerated on "+str(time.ctime()) + print "## from NIST database at http://physics.nist.gov/constants " + print "## @end deftypefn" + print "" + print """## Fundamental Physical Constants --- Complete Listing +## From: http://physics.nist.gov/constants +## Source: Peter J. Mohr and Barry N. Taylor, CODATA Recommended Values of the +## Fundamental Physical Constants: 2002, published in Rev. Mod. Phys. +## vol. 77(1) 1-107 (2005). +## Taken from: physics.nist.gov/cuu/Constants/Table/allascii.txt +## + """ + print "function [rval,unit,uncert,desc,name]=physical_constant(arg)" + print "" + print " persistent unit_data;" + print " if isstruct(unit_data) == 0 " + print " unit_data=struct();"; ## can we use persist smartly here? + #func-name#'es => [Desc, Val,Units, Uncertanity] + k=1; + for key,v in func_metadata.items(): + print " unit_data( %d ).name=\"%s\";"%(k,key); + print " unit_data( %d ).description=\"%s\";"%(k,v[0]); + print " unit_data( %d ).value=%s;"%(k,v[1]); + print " unit_data( %d ).units=\"%s\";"%(k,v[2]); + print " unit_data( %d ).uncertanity=%s;"%(k,v[3]); + k=k+1; + print "" + print " end" -if __name__ == "__main__": - x=file("nist-consts.txt"); - x=x.readlines(); + print "" + print " if nargin < 1 " + print " rval=unit_data;" + print " return;" + print " end" + + #print " if nargin < 1" + #print " if (nargout <= 1)" + #print " rval=unit_data;" + #print " else" + #print " rval=unit_data.value;" + #print " end" + #print " if (nargout >= 2)" + #print " unit=unit_data.units" + #print " end" + #print " if (nargout >= 3)" + #print " uncert=unit_data.uncertanity;" + #print " end" + #print " if (nargout >= 4)" + #print " desc=unit_data.description;" + #print " end" + #print " if (nargout >= 5)" + #print " name=unit_data.name;" + #print " end" + #print " return;" + #print " end" + + print "" + print " matches=[]; pmatches=[];" + print " LN=length(arg);" + print " arg=toupper(arg); " + print " for idx = 1:length(unit_data)" + print " if ( strcmp(arg,unit_data( idx ).name) == 1 )" + print " matches=[matches, idx];" + print " end" + print " if ( strncmp(arg,unit_data( idx ).name,LN) == 1 )" + print " pmatches=[pmatches, idx];" + print " end" + print " end" + print " matches = [matches, pmatches]; " + print " if (length(matches) >= 1)" + print " if (length(matches) > 1)" + print " warning(\" Too many matches. Picking first %s (1'st) by default for %s.\",unit_data(matches(1)).name,arg)" + print " end" + print " match=unit_data(matches(1));" + print " rval=match.value;" + print " if (nargout >= 2)" + print " unit=match.units;" + print " end" + print " if (nargout >= 3)" + print " uncert=match.uncertanity;" + print " end" + print " if (nargout >= 4)" + print " desc=match.description;" + print " end" + print " if (nargout >= 5)" + print " name=match.name;" + print " end" + print " else" + print " error(\" No matches found\")"; + print " end" + print " return;" + print "end" + + ## tests + for key,v in func_metadata.items(): + print "%%!assert( physical_constant( \"%s\" ),%s,eps);"%(key,v[1]) + + sys.stdout.close() + return + +if __name__ == "__main__": + y=file("nist-allascii.txt"); + x=y.readlines()[14:]; ## skip first 14 lines. + y.close(); + replace_dict={',':'_', '/':'_per_', '-':'_', @@ -83,51 +209,58 @@ ostd=sys.stdout; if len(sys.argv) > 1: PATH=sys.argv[1]+"/"; - -##print "1+1; ## avoids Octave thinking this is a separate script" + func_metadata={}; ## this is Python for l in x: - ## serves like a comment. + ## serves like a comment. Description=l[0:55].strip(); - ## function name + ## function name name=Description; - ## discard anything that dont have alphabets in beginning. + ## discard anything that dont have alphabets in beginning. idx=0; - while not isalpha(name[idx]): + NLEN=len(name); + while ( idx < NLEN) and (not isalpha(name[idx])) : idx=idx+1; name=name[idx:] - ## value + ## value Val=l[55:77]; - ## Uncertanity field + ## Uncertanity field Uncertainity=l[77:77+22].strip().replace(" ","") - ## create units. + ## create units. Units=l[99:-1].strip() - ## replace names. + ## replace names. + if name.find("(") >= 0: + name=name.split('(')[0].strip() for k,v in replace_dict.items(): name=name.replace(k,v); - if name.find("(") >= 0: - name=name.split('(')[0] - - ## replace uncertanity values + name=string.upper(name); + + ## replace uncertanity values for k,v in val_dict.items(): Val=Val.replace(k,v) - ## replace uncertanity values + ## replace uncertanity values for k,v in uncert_dict.items(): Uncertainity=Uncertainity.replace(k,v) - sys.stdout=open(PATH + string.upper(name)+".m","w"); - ## call function - ##print "[%s],[%s],[%s],[%s]"%(name, Val, Uncertainity, Units) - make_oct_func(name,Description,Val,Units,Uncertainity) - sys.stdout.close() - sys.stdout=ostd - print string.upper(name) + ## call function + ##print "[%s],[%s],[%s],[%s]"%(name, Val, Uncertainity, Units) + ##make_oct_func(name,Description,Val,Units,Uncertainity,PATH) + ##sys.stdout=ostd + ##print string.upper(name) + + #func-name#'es => [Desc, Val,Units, Uncertanity] + func_metadata[name]=[Description,Val, Units, Uncertainity]; + + ##generate main file. + make_physconst_func(func_metadata,PATH); + + ##python ./gen.py > constants.m && octave -q constants.m |