[SciPy-user] Help needed: Problem in translating a file read routine from Matlab to Python-Scipy

Travis Oliphant oliphant at ee.byu.edu
Tue Oct 12 11:01:26 CDT 2004


Guasina Luca wrote:

>I am trying to translate a Matlab program to Python - Scipy.
>The task consist of reading a big data file from a FORTRAN code; the
>file is written UNFORMATTED and contain a series of arrays.
>Anyone can help me ? I am new user of Python and Scipy...
>I am working in Windows XP with IDLE 1.02 and Python 2.3.3
>
>  
>
Binary files written from FORTRAN have special support in scipy.io

The expected usage is for you to open the file using

fid = io.fopen()
and then use fid.fort_read  (to handle the Fortran record mark 
automatically) or
fid.fread (to handle binary reading yourself).

The record you are skipping past is a 4-byte integer surrounding any 
data written using a single fortran write statement that gives the 
number of bytes written.
fid.fort_read will use this record to read the correct number of bytes, 
but it should not be necessary.

The attached luca.py file works on my Linux box just fine.  The output 
is also attached

What you did is less clean and not the expected form.  However it should 
have worked and probably would have if you would have opened the file in 
binary mode (this is handled automatically by io.fopen).  Windows 
unfortunately treats text files and binary files differently so you 
probably need to open the file using 'rb' .

But, I suggest you look at the attached luca.py file to see a more 
elegant way to do it and to realize that your elem data is redundant 
(and should be written using integers anyway).

-Travis Oliphant

-------------- next part --------------
A non-text attachment was scrubbed...
Name: luca.zip
Type: application/zip
Size: 8244 bytes
Desc: not available
Url : http://www.scipy.net/pipermail/scipy-user/attachments/20041012/d78f141a/luca-0001.zip
-------------- next part --------------
len(x) =  1000
len(y) =  10
len(z) =  5
x is [ 0.          0.001       0.002       0.003       0.004       0.005     
       0.006       0.007       0.008       0.009       0.01        0.011     
       0.012       0.013       0.014       0.015       0.016       0.017     
       0.018       0.019       0.02        0.021       0.022       0.023     
       0.024       0.025       0.026       0.027       0.028       0.029     
       0.03        0.031       0.032       0.033       0.034       0.035     
       0.036       0.037       0.038       0.039       0.04        0.041     
       0.042       0.043       0.044       0.045       0.046       0.047     
       0.048       0.049       0.05        0.051       0.052       0.053     
       0.05400001  0.055       0.05600001  0.057       0.05800001  0.059     
       0.06000001  0.061       0.06200001  0.06300001  0.064       0.06500001
       0.06600001  0.06700001  0.068       0.06900001  0.07000001  0.07100001
       0.072       0.07300001  0.074       0.075       0.07600001  0.07700001
       0.078       0.079       0.08        0.081       0.082       0.083     
       0.084       0.085       0.086       0.087       0.088       0.089     
       0.09        0.091       0.09199999  0.09299999  0.094       0.095     
       0.09599999  0.097       0.09799999  0.09899999  0.09999999  0.101     
       0.10199999  0.10299999  0.10399999  0.10499999  0.10599999  0.10699999
       0.10799999  0.10899999  0.10999998  0.11099999  0.11199999  0.11299999
       0.11399999  0.11499999  0.11599998  0.11699998  0.11799999  0.11899999
       0.11999998  0.12099998  0.12199998  0.12299998  0.12399998  0.12499999
       0.12599999  0.12699999  0.12799999  0.12899999  0.12999998  0.13099998
       0.13199998  0.13299999  0.13399999  0.13499999  0.13599998  0.13699998
       0.13799998  0.13899998  0.13999999  0.14099999  0.14199997  0.14299998
       0.14399998  0.14499998  0.14599998  0.14699998  0.14799997  0.14899997
       0.14999998  0.15099998  0.15199998  0.15299998  0.15399997  0.15499997
       0.15599997  0.15699998  0.15799998  0.15899998  0.15999997  0.16099997
       0.16199997  0.16299997  0.16399997  0.16499998  0.16599996  0.16699997
       0.16799997  0.16899997  0.16999997  0.17099997  0.17199996  0.17299996
       0.17399997  0.17499997  0.17599997  0.17699997  0.17799996  0.17899996
       0.17999996  0.18099996  0.18199997  0.18299997  0.18399996  0.18499996
       0.18599996  0.18699996  0.18799996  0.18899997  0.18999995  0.19099995
       0.19199996  0.19299996  0.19399996  0.19499996  0.19599995  0.19699995
       0.19799995  0.19899996  0.19999996  0.20099996  0.20199995  0.20299995
       0.20399995  0.20499995  0.20599996  0.20699996  0.20799994  0.20899995
       0.20999995  0.21099995  0.21199995  0.21299995  0.21399994  0.21499994
       0.21599995  0.21699995  0.21799995  0.21899995  0.21999994  0.22099994
       0.22199994  0.22299995  0.22399995  0.22499995  0.22599994  0.22699994
       0.22799994  0.22899994  0.22999994  0.23099995  0.23199993  0.23299994
       0.23399994  0.23499994  0.23599994  0.23699994  0.23799993  0.23899993
       0.23999994  0.24099994  0.24199994  0.24299994  0.24399993  0.24499993
       0.24599993  0.24699993  0.24799994  0.24899994  0.24999993  0.25099993
       0.25199994  0.25299993  0.25399995  0.25499994  0.25599995  0.25699994
       0.25799996  0.25899994  0.25999996  0.26099995  0.26199996  0.26299995
       0.26399997  0.26499996  0.26599997  0.26699996  0.26799998  0.26899996
       0.26999998  0.27099997  0.27199998  0.27299997  0.27399999  0.27499998
       0.27599999  0.27699998  0.278       0.27899998  0.28        0.28099999
       0.28200001  0.28299999  0.28400001  0.285       0.28600001  0.287     
       0.28800002  0.289       0.29000002  0.29100001  0.29200003  0.29300001
       0.29400003  0.29500002  0.29600003  0.29700002  0.29800004  0.29900002
       0.30000004  0.30100003  0.30200005  0.30300003  0.30400005  0.30500004
       0.30600005  0.30700004  0.30800006  0.30900005  0.31000006  0.31100005
       0.31200007  0.31300005  0.31400007  0.31500006  0.31600007  0.31700006
       0.31800008  0.31900007  0.32000008  0.32100007  0.32200009  0.32300007
       0.32400009  0.32500008  0.32600009  0.32700008  0.3280001   0.32900009
       0.3300001   0.33100009  0.33200011  0.33300009  0.33400011  0.3350001 
       0.33600011  0.3370001   0.33800012  0.33900011  0.34000012  0.34100011
       0.34200013  0.34300011  0.34400013  0.34500012  0.34600013  0.34700012
       0.34800014  0.34900013  0.35000014  0.35100013  0.35200015  0.35300013
       0.35400015  0.35500014  0.35600016  0.35700014  0.35800016  0.35900015
       0.36000016  0.36100015  0.36200017  0.36300015  0.36400017  0.36500016
       0.36600018  0.36700016  0.36800018  0.36900017  0.37000018  0.37100017
       0.37200019  0.37300017  0.37400019  0.37500018  0.3760002   0.37700018
       0.3780002   0.37900019  0.3800002   0.38100019  0.38200021  0.3830002 
       0.38400021  0.3850002   0.38600022  0.3870002   0.38800022  0.38900021
       0.39000022  0.39100021  0.39200023  0.39300022  0.39400023  0.39500022
       0.39600024  0.39700022  0.39800024  0.39900023  0.40000024  0.40100023
       0.40200025  0.40300024  0.40400025  0.40500024  0.40600026  0.40700024
       0.40800026  0.40900025  0.41000026  0.41100025  0.41200027  0.41300026
       0.41400027  0.41500026  0.41600028  0.41700026  0.41800028  0.41900027
       0.42000028  0.42100027  0.42200029  0.42300028  0.42400029  0.42500028
       0.4260003   0.42700028  0.4280003   0.42900029  0.43000031  0.43100029
       0.43200031  0.4330003   0.43400031  0.4350003   0.43600032  0.4370003 
       0.43800032  0.43900031  0.44000033  0.44100031  0.44200033  0.44300032
       0.44400033  0.44500032  0.44600034  0.44700032  0.44800034  0.44900033
       0.45000035  0.45100033  0.45200035  0.45300034  0.45400035  0.45500034
       0.45600036  0.45700034  0.45800036  0.45900035  0.46000037  0.46100035
       0.46200037  0.46300036  0.46400037  0.46500036  0.46600038  0.46700037
       0.46800038  0.46900037  0.47000039  0.47100037  0.47200039  0.47300038
       0.47400039  0.47500038  0.4760004   0.47700039  0.4780004   0.47900039
       0.48000041  0.48100039  0.48200041  0.4830004   0.48400041  0.4850004 
       0.48600042  0.48700041  0.48800042  0.48900041  0.49000043  0.49100041
       0.49200043  0.49300042  0.49400043  0.49500042  0.49600044  0.49700043
       0.49800044  0.49900043  0.50000042  0.50100046  0.50200045  0.50300044
       0.50400043  0.50500041  0.5060004   0.50700045  0.50800043  0.50900042
       0.51000041  0.51100039  0.51200038  0.51300043  0.51400042  0.5150004 
       0.51600039  0.51700038  0.51800036  0.51900041  0.5200004   0.52100039
       0.52200037  0.52300036  0.52400035  0.52500039  0.52600038  0.52700037
       0.52800035  0.52900034  0.53000033  0.53100038  0.53200036  0.53300035
       0.53400034  0.53500032  0.53600031  0.53700036  0.53800035  0.53900033
       0.54000032  0.54100031  0.54200029  0.54300034  0.54400033  0.54500031
       0.5460003   0.54700029  0.54800028  0.54900032  0.55000031  0.5510003 
       0.55200028  0.55300027  0.55400026  0.55500031  0.55600029  0.55700028
       0.55800027  0.55900025  0.56000024  0.56100029  0.56200027  0.56300026
       0.56400025  0.56500024  0.56600022  0.56700027  0.56800026  0.56900024
       0.57000023  0.57100022  0.57200021  0.57300025  0.57400024  0.57500023
       0.57600021  0.5770002   0.57800019  0.57900023  0.58000022  0.58100021
       0.5820002   0.58300018  0.58400017  0.58500022  0.5860002   0.58700019
       0.58800018  0.58900017  0.59000015  0.5910002   0.59200019  0.59300017
       0.59400016  0.59500015  0.59600013  0.59700018  0.59800017  0.59900016
       0.60000014  0.60100013  0.60200012  0.60300016  0.60400015  0.60500014
       0.60600013  0.60700011  0.6080001   0.60900015  0.61000013  0.61100012
       0.61200011  0.61300009  0.61400008  0.61500013  0.61600012  0.6170001 
       0.61800009  0.61900008  0.62000006  0.62100011  0.6220001   0.62300009
       0.62400007  0.62500006  0.62600005  0.62700009  0.62800008  0.62900007
       0.63000005  0.63100004  0.63200003  0.63300008  0.63400006  0.63500005
       0.63600004  0.63700002  0.63800001  0.63900006  0.64000005  0.64100003
       0.64200002  0.64300001  0.64399999  0.64500004  0.64600003  0.64700001
       0.648       0.64899999  0.64999998  0.65100002  0.65200001  0.653     
       0.65399998  0.65499997  0.65599996  0.65700001  0.65799999  0.65899998
       0.65999997  0.66099995  0.66199994  0.66299999  0.66399997  0.66499996
       0.66599995  0.66699994  0.66799992  0.66899997  0.66999996  0.67099994
       0.67199993  0.67299992  0.67399991  0.67499995  0.67599994  0.67699993
       0.67799991  0.6789999   0.67999989  0.68099993  0.68199992  0.68299991
       0.6839999   0.68499988  0.68599987  0.68699992  0.6879999   0.68899989
       0.68999988  0.69099987  0.69199985  0.6929999   0.69399989  0.69499987
       0.69599986  0.69699985  0.69799984  0.69899988  0.69999987  0.70099986
       0.70199984  0.70299983  0.70399982  0.70499986  0.70599985  0.70699984
       0.70799983  0.70899981  0.7099998   0.71099985  0.71199983  0.71299982
       0.71399981  0.71499979  0.71599978  0.71699983  0.71799982  0.7189998 
       0.71999979  0.72099978  0.72199976  0.72299981  0.7239998   0.72499979
       0.72599977  0.72699976  0.72799975  0.72899979  0.72999978  0.73099977
       0.73199975  0.73299974  0.73399973  0.73499978  0.73599976  0.73699975
       0.73799974  0.73899972  0.73999971  0.74099976  0.74199975  0.74299973
       0.74399972  0.74499971  0.74599969  0.74699974  0.74799973  0.74899971
       0.7499997   0.75099969  0.75199968  0.75299972  0.75399971  0.7549997 
       0.75599968  0.75699967  0.75799966  0.75899971  0.75999969  0.76099968
       0.76199967  0.76299965  0.76399964  0.76499969  0.76599967  0.76699966
       0.76799965  0.76899964  0.76999962  0.77099967  0.77199966  0.77299964
       0.77399963  0.77499962  0.77599961  0.77699965  0.77799964  0.77899963
       0.77999961  0.7809996   0.78199959  0.78299963  0.78399962  0.78499961
       0.7859996   0.78699958  0.78799957  0.78899962  0.7899996   0.79099959
       0.79199958  0.79299957  0.79399955  0.7949996   0.79599959  0.79699957
       0.79799956  0.79899955  0.79999954  0.80099958  0.80199957  0.80299956
       0.80399954  0.80499953  0.80599952  0.80699956  0.80799955  0.80899954
       0.80999953  0.81099951  0.8119995   0.81299955  0.81399953  0.81499952
       0.81599951  0.8169995   0.81799948  0.81899953  0.81999952  0.8209995 
       0.82199949  0.82299948  0.82399946  0.82499951  0.8259995   0.82699949
       0.82799947  0.82899946  0.82999945  0.83099949  0.83199948  0.83299947
       0.83399945  0.83499944  0.83599943  0.83699948  0.83799946  0.83899945
       0.83999944  0.84099942  0.84199941  0.84299946  0.84399945  0.84499943
       0.84599942  0.84699941  0.84799939  0.84899944  0.84999943  0.85099941
       0.8519994   0.85299939  0.85399938  0.85499942  0.85599941  0.8569994 
       0.85799938  0.85899937  0.85999936  0.86099941  0.86199939  0.86299938
       0.86399937  0.86499935  0.86599934  0.86699939  0.86799937  0.86899936
       0.86999935  0.87099934  0.87199932  0.87299937  0.87399936  0.87499934
       0.87599933  0.87699932  0.87799931  0.87899935  0.87999934  0.88099933
       0.88199931  0.8829993   0.88399929  0.88499933  0.88599932  0.88699931
       0.8879993   0.88899928  0.88999927  0.89099932  0.8919993   0.89299929
       0.89399928  0.89499927  0.89599925  0.8969993   0.89799929  0.89899927
       0.89999926  0.90099925  0.90199924  0.90299928  0.90399927  0.90499926
       0.90599924  0.90699923  0.90799922  0.90899926  0.90999925  0.91099924
       0.91199923  0.91299921  0.9139992   0.91499925  0.91599923  0.91699922
       0.91799921  0.9189992   0.91999918  0.92099923  0.92199922  0.9229992 
       0.92399919  0.92499918  0.92599916  0.92699921  0.9279992   0.92899919
       0.92999917  0.93099916  0.93199915  0.93299919  0.93399918  0.93499917
       0.93599916  0.93699914  0.93799913  0.93899918  0.93999916  0.94099915
       0.94199914  0.94299912  0.94399911  0.94499916  0.94599915  0.94699913
       0.94799912  0.94899911  0.94999909  0.95099914  0.95199913  0.95299911
       0.9539991   0.95499909  0.95599908  0.95699912  0.95799911  0.9589991 
       0.95999908  0.96099907  0.96199906  0.96299911  0.96399909  0.96499908
       0.96599907  0.96699905  0.96799904  0.96899909  0.96999907  0.97099906
       0.97199905  0.97299904  0.97399902  0.97499907  0.97599906  0.97699904
       0.97799903  0.97899902  0.97999901  0.98099905  0.98199904  0.98299903
       0.98399901  0.984999    0.98599899  0.98699903  0.98799902  0.98899901
       0.989999    0.99099898  0.99199897  0.99299902  0.993999    0.99499899
       0.99599898  0.99699897  0.99799895  0.99899894]
y is [ 0.     0.001  0.002  0.003  0.004  0.005  0.006  0.007  0.008  0.009]
z is [ 0.     0.001  0.002  0.003  0.004]


More information about the SciPy-user mailing list