INTELLIGENT WORK FORUMS FOR COMPUTER PROFESSIONALS
Come Join Us!
Are you a Computer / IT professional? Join Tek-Tips now!
- Talk With Other Members
- Be Notified Of Responses
To Your Posts
- Keyword Search
- One-Click Access To Your
Favorite Forums
- Automated Signatures
On Your Posts
- Best Of All, It's Free!
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.
Partner With Us!
"Best Of Breed" Forums Add Stickiness To Your Site

(Download This Button Today!)
Feedback
"...I have answered some questions and have gotten answers for my questions. Anywhere you can do this on one page helps tremendously..."
Geography
Where in the world do Tek-Tips members come from?
|
Write a program to read the data from a file
|
|
Please i need help with fortran , i need to write a program to read the data from a file, then extracting this data to another file with a specific fortran, The file is shown , the data i need to extract is marked in red(see the attached file). CODE ENTRY 13753 20040225 20040517 20050926 0000 SUBENT 13753001 20040225 20040517 20050926 0000 BIB 12 28 INSTITUTE (1USALAS,1USALRL,1USAOHO) #(1USALAS) Los Alamos National Laboratory, NM, USA #(1USALRL) Lawrence Livermore National Laboratory, Livermore, CA, USA #(1USAOHO) Ohio University, Athens, OH, USA REFERENCE (J,PR/C,63,044608,200104) # (J,PR/C,63,044608,200104) Journ.: Physical Review, Part C, Nuclear Physics, Vol.63, p.044608 (2001) USA #+ #NSR=2001AB14 #DOI=10.1103/PhysRevC.63.044608 AUTHOR (W.P.Abfalterer,F.B.Bateman,F.S.Dietrich,R.W.Finlay, R.C.Haight,G.L.Morgan) TITLE Measurement of neutron total cross sections up to 560 MeV FACILITY (MESON,1USALAS) LANSCE facility at WNR. #(MESON) Meson facility #(1USALAS) Los Alamos National Laboratory, NM, USA INC-SOURCE (EVAP) 800-MeV protons on water-cooled tungsten target. METHOD (TOF) Spectra normalized to monitor counts. #(TOF) Time-of-flight DETECTOR (SCIN) 2 thick slabs of BC404 located at 37.70m and 39.61 m from neutron source. Plastic scintillator veto counters placed in front of detectors to reject charged particles. Monitor counter consisted of a circular plastic scintillator. #(SCIN) Scintillation detector CORRECTION Corrected for . deadtime, . accidental coincidences between veto counters and main detectors, . background, . inscattering. ERR-ANALYS (ERR-S) Statistical uncertainty given. STATUS Data received by email from F. Dietrich, 10 July 2001. (APRVD) Approved by F. Dietrich, 15 January 2002. Update approved by F.Dietrich, 24 February 2004. HISTORY (20010710C) VM (20020125A) Author approval added. ENDBIB 28 NOCOMMON 0 0 ENDSUBENT 31 SUBENT 13753029 20010711 20011023 20050926 0000 BIB 2 3 REACTION (90-TH-232(N,TOT),,SIG) #(90-TH-232(N,TOT),,SIG) Quantity: [CS] Cross section # Process: [TOT] Total SAMPLE 580.37-g natural thorium sample. Inverse sample thickness 3.331 b/atom. ENDBIB 3 NOCOMMON 0 0 DATA 3 467 EN DATA ERR-S MEV B B 5.293 7.484 0.02085 5.346 7.493 0.02161 5.399 7.478 0.02243 5.454 7.43 0.02066 5.509 7.385 0.02028 5.564 7.365 0.02018 5.62 7.363 0.02014 5.676 7.313 0.01982 5.733 7.296 0.01951 5.791 7.257 0.01927 5.849 7.206 0.01911 5.908 7.198 0.01916 5.967 7.135 0.019 6.027 7.124 0.01889 6.088 7.089 0.01885 6.149 7.029 0.01886 6.211 7.018 0.01971 6.273 7.015 0.02168 6.336 6.961 0.02162 6.4 6.937 0.01883 6.464 6.882 0.01779 6.529 6.848 0.01725 6.595 6.801 0.01655 6.661 6.759 0.01644 6.728 6.712 0.01634 6.796 6.702 0.01625 6.864 6.665 0.01604 6.933 6.631 0.01581 7.003 6.616 0.01577 7.073 6.573 0.01577 7.144 6.534 0.01592 7.216 6.503 0.01653 7.289 6.492 0.01793 7.362 6.462 0.01982 7.436 6.426 0.02028 7.51 6.415 0.01998 7.586 6.371 0.01964 7.662 6.347 0.02018 7.739 6.326 0.02164 7.817 6.303 0.02175 7.896 6.26 0.02092 7.975 6.248 0.02034 8.055 6.222 0.02005 8.136 6.217 0.01955 8.218 6.202 0.01825 8.3 6.141 0.01726 8.384 6.116 0.01678 8.468 6.097 0.01652 8.553 6.061 0.01639 8.639 6.051 0.01636 8.726 6.027 0.01641 8.814 5.999 0.01642 8.902 5.993 0.01645 8.992 5.958 0.01661 9.082 5.929 0.01695 9.173 5.909 0.01724 9.266 5.899 0.0174 9.359 5.875 0.01733 9.453 5.858 0.0172 9.548 5.868 0.01714 9.644 5.845 0.01714 9.741 5.828 0.01719 9.838 5.819 0.01718 9.937 5.804 0.01707 10.04 5.793 0.01706 10.14 5.764 0.01703 10.24 5.752 0.01702 10.34 5.751 0.01707 10.45 5.738 0.01725 10.55 5.731 0.01751 10.66 5.719 0.01781 10.77 5.738 0.01814 10.87 5.719 0.0184 10.98 5.71 0.01859 11.09 5.71 0.01867 11.2 5.704 0.0187 11.32 5.699 0.0187 11.43 5.715 0.01876 11.55 5.727 0.01878 11.66 5.687 0.01873 11.78 5.68 0.01881 11.9 5.704 0.01915 12.02 5.707 0.01951 12.14 5.691 0.01965 12.26 5.696 0.01942 12.38 5.708 0.01929 12.51 5.718 0.01936 12.63 5.708 0.01943 12.76 5.712 0.01959 12.89 5.729 0.01989 13.02 5.73 0.02007 13.15 5.722 0.02006 13.28 5.721 0.02005 13.41 5.763 0.02022 13.55 5.78 0.02031 13.69 5.78 0.02024 13.82 5.773 0.0201 13.96 5.774 0.02004 14.1 5.811 0.02019 14.24 5.826 0.0203 14.39 5.837 0.0204 14.53 5.865 0.02059 14.68 5.882 0.02092 14.82 5.877 0.02113 14.97 5.877 0.02123 15.12 5.887 0.01898 15.28 5.907 0.01377 15.43 5.924 0.01387 15.58 5.936 0.01401 15.74 5.943 0.01415 15.9 5.974 0.01426 16.06 5.986 0.01429 16.22 6. 0.01422 16.38 6.005 0.01414 16.55 6.001 0.01409 16.72 6.021 0.01403 16.88 6.05 0.01404 17.05 6.054 0.01406 17.22 6.054 0.01406 17.4 6.088 0.01407 17.57 6.096 0.01407 17.75 6.104 0.01409 17.93 6.108 0.01412 18.11 6.113 0.01416 18.29 6.117 0.01419 18.47 6.144 0.01422 18.66 6.154 0.01425 18.85 6.162 0.01427 19.04 6.192 0.01434 19.23 6.187 0.01436 19.42 6.188 0.01438 19.62 6.187 0.01434 19.81 6.198 0.01426 20.01 6.202 0.01416 20.21 6.201 0.01406 20.42 6.206 0.01395 20.62 6.212 0.01384 20.83 6.2 0.01377 21.04 6.198 0.01372 21.25 6.196 0.01364 21.46 6.196 0.01358 21.68 6.189 0.01351 21.9 6.184 0.01342 22.12 6.207 0.01333 22.34 6.2 0.01325 22.56 6.188 0.01317 22.79 6.171 0.01311 23.02 6.152 0.01305 23.25 6.156 0.01304 23.48 6.142 0.01299 23.72 6.141 0.01293 23.96 6.131 0.01286 24.2 6.108 0.0128 24.44 6.086 0.01273 24.69 6.081 0.01265 24.94 6.055 0.01257 25.19 6.048 0.01251 25.44 6.038 0.01242 25.7 6.007 0.01232 25.95 5.985 0.01222 26.21 5.989 0.01216 26.48 5.974 0.01207 26.74 5.954 0.01199 27.01 5.927 0.01188 27.28 5.909 0.01179 27.56 5.877 0.0117 27.84 5.85 0.01161 28.12 5.829 0.01151 28.4 5.803 0.01141 28.68 5.791 0.01133 28.97 5.757 0.01123 29.26 5.734 0.01114 29.56 5.706 0.01105 29.85 5.678 0.01097 30.15 5.65 0.01089 30.46 5.609 0.0108 30.76 5.596 0.01073 31.07 5.572 0.01067 31.38 5.541 0.01059 31.7 5.504 0.01051 32.02 5.487 0.01044 32.34 5.46 0.01037 32.67 5.431 0.0103 32.99 5.397 0.01022 33.33 5.372 0.01014 33.66 5.342 0.01007 34. 5.313 0.01001 34.34 5.28 0.009935 34.69 5.257 0.009859 35.03 5.238 0.009794 35.39 5.216 0.009735 35.74 5.193 0.00968 36.1 5.166 0.009604 36.46 5.129 0.009539 36.83 5.105 0.009487 37.2 5.078 0.00942 37.57 5.065 0.009362 37.95 5.045 0.009312 38.33 5.02 0.009264 38.72 4.987 0.0092 39.11 4.963 0.009135 39.5 4.955 0.009091 39.9 4.939 0.009034 40.3 4.917 0.008979 40.7 4.895 0.008936 41.11 4.874 0.008876 41.53 4.877 0.008837 41.94 4.867 0.008792 42.36 4.85 0.008746 42.79 4.84 0.008705 43.22 4.831 0.008665 43.65 4.815 0.008614 44.09 4.802 0.008573 44.54 4.797 0.008547 44.98 4.787 0.008515 45.44 4.765 0.008472 45.89 4.758 0.008437 46.35 4.762 0.008409 46.82 4.755 0.00838 47.29 4.755 0.008351 47.77 4.753 0.008326 48.25 4.75 0.008293 48.73 4.756 0.008269 49.22 4.75 0.008242 49.72 4.75 0.008218 50.22 4.76 0.008199 50.72 4.77 0.008183 51.23 4.776 0.008171 51.74 4.771 0.00815 52.26 4.771 0.008128 52.79 4.786 0.008115 53.32 4.793 0.008098 53.86 4.791 0.008084 54.4 4.807 0.008078 54.94 4.813 0.008066 55.5 4.825 0.008051 56.05 4.838 0.008048 56.62 4.864 0.008046 57.19 4.869 0.008028 57.76 4.875 0.008027 58.34 4.886 0.008018 58.93 4.9 0.00801 59.52 4.912 0.008002 60.12 4.927 0.007996 60.72 4.936 0.007987 61.33 4.951 0.007993 61.95 4.969 0.00799 62.57 4.982 0.007982 63.2 5.001 0.007979 63.84 5.016 0.007977 64.48 5.025 0.007973 65.13 5.037 0.007979 65.78 5.056 0.007974 66.44 5.06 0.007973 67.11 5.066 0.007962 67.78 5.086 0.00796 68.46 5.105 0.007961 69.15 5.122 0.007969 69.85 5.134 0.007958 70.55 5.133 0.007955 71.26 5.143 0.007954 71.97 5.161 0.00796 72.7 5.176 0.00796 73.43 5.179 0.007958 74.17 5.185 0.007949 74.91 5.193 0.007946 75.67 5.213 0.007945 76.43 5.214 0.007939 77.19 5.223 0.007931 77.97 5.224 0.007924 78.75 5.232 0.007913 79.54 5.231 0.007906 80.34 5.232 0.007905 81.15 5.235 0.007899 81.97 5.243 0.007899 82.79 5.241 0.007897 83.62 5.235 0.007882 84.46 5.233 0.007863 85.31 5.231 0.007857 86.17 5.226 0.007849 87.04 5.221 0.007831 87.91 5.227 0.00783 88.79 5.219 0.007825 89.69 5.215 0.007809 90.59 5.199 0.007783 91.5 5.183 0.007769 92.42 5.185 0.007766 93.35 5.181 0.007749 94.28 5.167 0.007738 95.23 5.158 0.007729 96.19 5.143 0.007709 97.16 5.114 0.007686 98.13 5.104 0.007665 99.12 5.098 0.007662 100.1 5.085 0.007641 101.1 5.056 0.007621 102.1 5.039 0.0076 103.2 5.017 0.007587 104.2 5.011 0.007582 105.2 4.989 0.007542 106.3 4.963 0.007514 107.4 4.947 0.007502 108.5 4.913 0.007481 109.5 4.882 0.007459 110.6 4.868 0.007439 111.8 4.846 0.007416 112.9 4.823 0.007393 114. 4.788 0.007369 115.2 4.758 0.007357 116.3 4.734 0.007332 117.5 4.722 0.007311 118.7 4.687 0.007281 119.9 4.66 0.007257 121.1 4.637 0.007249 122.3 4.611 0.007229 123.5 4.573 0.007194 124.8 4.542 0.007166 126. 4.515 0.007153 127.3 4.494 0.007131 128.6 4.464 0.007111 129.8 4.431 0.007091 131.1 4.403 0.007075 132.5 4.37 0.007052 133.8 4.341 0.007032 135.1 4.318 0.007012 136.5 4.286 0.006989 137.9 4.253 0.006974 139.3 4.222 0.006957 140.7 4.195 0.006935 142.1 4.167 0.006916 143.5 4.14 0.006897 144.9 4.107 0.006881 146.4 4.077 0.006874 147.9 4.043 0.006844 149.4 4.014 0.006823 150.9 3.996 0.006818 152.4 3.967 0.006797 153.9 3.934 0.006778 155.5 3.909 0.006765 157. 3.883 0.006759 158.6 3.857 0.006747 160.2 3.833 0.006737 161.8 3.819 0.00673 163.4 3.789 0.006704 165.1 3.759 0.006696 166.7 3.732 0.0067 168.4 3.703 0.006681 170.1 3.686 0.00667 171.8 3.664 0.006664 173.5 3.634 0.006651 175.3 3.615 0.006638 177. 3.601 0.006636 178.8 3.581 0.006643 180.6 3.554 0.006627 182.4 3.535 0.006612 184.3 3.514 0.006615 186.1 3.5 0.006617 188. 3.484 0.006607 189.9 3.47 0.006607 191.8 3.449 0.006611 193.7 3.431 0.006604 195.7 3.418 0.006604 197.6 3.399 0.006606 199.6 3.387 0.006609 201.6 3.372 0.006599 203.6 3.359 0.006598 205.7 3.336 0.006607 207.7 3.322 0.006613 209.8 3.311 0.006605 211.9 3.307 0.00661 214.1 3.295 0.006614 216.2 3.279 0.006615 218.4 3.274 0.006621 220.6 3.269 0.006632 222.8 3.255 0.006639 225.1 3.242 0.006647 227.3 3.237 0.006645 229.6 3.232 0.006654 231.9 3.225 0.00666 234.2 3.216 0.006669 236.6 3.204 0.006677 239. 3.201 0.006687 241.4 3.201 0.006695 243.8 3.194 0.006711 246.2 3.183 0.006721 248.7 3.181 0.006735 251.2 3.176 0.006747 253.7 3.163 0.006758 256.3 3.167 0.006776 258.9 3.161 0.006781 261.5 3.16 0.006785 264.1 3.158 0.006797 266.8 3.152 0.006817 269.4 3.15 0.006834 272.1 3.154 0.00685 274.9 3.155 0.006864 277.6 3.148 0.00687 280.4 3.143 0.006882 283.3 3.138 0.006898 286.1 3.145 0.006919 289. 3.154 0.006931 291.9 3.149 0.006943 294.8 3.146 0.006969 297.8 3.146 0.006982 300.8 3.144 0.006988 303.8 3.145 0.006992 306.8 3.147 0.007007 309.9 3.144 0.007035 313. 3.149 0.007052 316.2 3.145 0.007063 319.4 3.149 0.007069 322.6 3.158 0.007083 325.8 3.163 0.007099 329.1 3.166 0.00711 332.4 3.16 0.007117 335.7 3.159 0.007129 339.1 3.168 0.007141 342.5 3.174 0.007158 346. 3.173 0.007175 349.4 3.18 0.007179 353. 3.182 0.007178 356.5 3.177 0.007172 360.1 3.175 0.00718 363.7 3.179 0.007202 367.4 3.187 0.00721 371. 3.188 0.007202 374.8 3.19 0.0072 378.5 3.202 0.007199 382.3 3.204 0.007203 386.2 3.201 0.007209 390.1 3.203 0.007204 394. 3.208 0.007204 398. 3.221 0.007203 402. 3.224 0.007202 406. 3.225 0.007208 410.1 3.228 0.007221 414.2 3.223 0.007226 418.4 3.227 0.007218 422.6 3.24 0.007214 426.8 3.253 0.007208 431.1 3.259 0.007219 435.4 3.265 0.00724 439.8 3.269 0.007261 444.2 3.268 0.007274 448.7 3.282 0.007291 453.2 3.292 0.007318 457.8 3.291 0.007351 462.4 3.293 0.00739 467. 3.292 0.007431 471.7 3.304 0.00748 476.4 3.312 0.00753 481.2 3.314 0.007593 486.1 3.331 0.007676 490.9 3.34 0.007766 495.9 3.343 0.007861 500.9 3.35 0.03443 505.9 3.351 0.03448 511. 3.347 0.03447 516.1 3.354 0.03457 521.3 3.362 0.0347 526.5 3.367 0.03479 531.8 3.373 0.03491 537.2 3.369 0.03493 542.6 3.375 0.03507 548. 3.389 0.03529 553.5 3.401 0.0355 559.1 3.405 0.03565 ENDDATA 469 ENDSUBENT 477 ENDENTRY 2
|
|
Tony1984:
I think you ahould use other languages like 'sed' to do this task. In linux try
CODE --> sedsed -ne '/^DATA 3 467/,/^ENDDATA 469/p' file.data
where file.data is the file containing your data.
Gordon Shumway |
|
for now , i wanna use fortran to handle this kind of files |
|
You can try something like this
CODE --> F90program extract
implicit none
integer, parameter :: skip_initial_lines = 52
integer, parameter :: print_middle_lines = 471
integer :: i
character(len=100) :: line
character(len=10) :: c1, c2, c3
double precision :: r1, r2, r3
open(unit=100, file='file.data')
open(unit=200, file='newfile.data')
!=== read and skip junk
do i = 1, skip_initial_lines
read(100,*) line
enddo
!=== read and print header
do i = 1, 3
read(100,*) c1, c2, c3
write(200,*) c1, c2, c3
enddo
!=== read and print data
do i = 1, print_middle_lines - 4
read(100,*) r1, r2, r3
write(200,'(3(f15.8))') r1, r2, r3
enddo
!=== read and print footer
read(100,*) c1, c2
write(200,*) c1, c2
close(100)
close(200)
end program extract Gordon Shumway |
|
Questions:
(1) Is the number of initial junk lines constant (=52) or do you need to trigger your reading by a keyword ? There are some ENDBIB - tags, that would indicate that the number of lines is not fixed.
(2) is the number of datalines fixed ? The tag ENDDATA seems to indicate otherwise
(3) Do the first three lines marked red contain valid data ?
If your number of initial lines is not fixed for all files, the number lines containing neither, and you need the data contained in these first three lines, I would propose a little different from Gordon's.
CODEprogram extract
implicit none
character*100 cBuffer ! input buffer
character*4 cKey ! buffer to read keyword, adapt size to actual size of keyword
character*7 cEnd ! key word indicating end of data
integer iStat ! reading status
integer int1, int2 ! integers from first dataline
logical lCopyFlag ! flag indicates data should be copied to new file
real rDat1, rDat2, rDat3 ! real data from file
lCopyFlag = .false. ! initialise flag
open (unit = 100, file = 'file.dat') ! adapt name of inputfile here
open (unit = 200, file = 'fileout.dat') ! adapt name of outputfile here
do
read (100, '(a100)', iostat = iStat) cBuffer
if (istat .ne. 0) exit ! exit at the end of the file
if (.not. lCopyFlag) then ! we have not yet encountered the keyword
read (cBuffer, *) cKey ! checkif we have hit onthe keyword
if (cKey .eq. 'DATA') then ! if we have encountered keyword
lCopyFlag = .true. ! set flag
! process the next lines if necessary to save them
read (cBuffer, *) cKey, int1, int2 ! read integers
write (200, '(2i5)') ! write the integers to your new file, adapt format if necessary
read (100, '(a100)') cBuffer ! just read and copy the next two lines, adapt if needed
write (200, *) trim (cBuffer) ! skip this if you do not need the two lines
read (100, '(a100)') cBuffer ! just read and copy the next two lines, adapt if needed
write (200, *) trim (cBuffer) ! skip this if you do not need the two lines
else
cycle ! just take the next record when keyword not found
endif
else
! if we have the flag
read (cBuffer, *) cEnd ! see if endtag is reached
if (cEnd .eq. 'ENDDATA') then
read (cBuffer, *) cEnd, int1 ! if you need this final integer
write (200, '(i10)') int1
exit
endif
read (cBuffer, *) rDat1, rDat2, rDat3 ! read the data you need
write (200, '(3f12.5)') ! and copy to output, you might want to adapt this format
endif
enddo
end program
Looks a little bit complicated, but allows for some flexibility in the datafiles.
Norbert The optimist believes we live in the best of all possible worlds - the pessimist fears this might be true. |
|
|
mikrom (Programmer) |
3 Jul 12 11:18 |
I see in the file this line
467 seems to be number of numeric data lines. But what is the meaning of the number 3? Isn't it a number of data columns. If yes, then you can get the next file with 1, 2 or 4,... columns, so the reading of the data must be programmed to fit this situations. |
|
Tony1984: Do you still want to do it with fortran? Gordon Shumway |
|
|
mikrom (Programmer) |
3 Jul 12 15:09 |
If the data file get complicated, I personally would use the clever idea of melmacianalf, i.e. preprocessing with sed, awk, perl, python, or something else ... althought the native Fortran programmers didn't like this idea very well
http://www.tek-tips.com/viewthread.cfm?qid=1680512 |
|
thank you guyes, im stuck in this kind of data,
if you have any other sugesstions please let me know about it,
it seems its complicated to do it by fortran, other codes are ok. |
|
mikrom:
Many thanks for the link to the previous post. Gordon Shumway |
|
Tony:
You did not say much...you just say extract the data in red....if that is all you want, it can be done very simply.
As much as I appreciate Norbert's effort, I am afraid the source he provides is going to scare Tony away!...it's just too much at this point...like I always say: "baby steps...baby steps".
Tony, here is what I think is an easy to understand code. Just compile it and run it from the command line like this: "pulldata < file.dat > fileout.dat"
CODEprogram pulldata
character str*4, line*200
integer i, val, nrows
! look for 'DATA' line
do
read(*,'(a)') line
if (line(1:4) == 'DATA') exit
end do
! read number of rows, read data and write it back out
read(line,*) str, val, nrows
write(*,*) line
do i = 1, nrows+3
read(*,'(a)') line
write(*,*) line
end do
end program pulldata |
|
Yes, I know, my code is complicated - and it might be getting more awesome still if it needs to be adapted to variable numbers of columns.
But I understood Tony's point was to have the data in a different format (as I am apt to mistyping, I think I am quite good to interpret mistypings as well  ). If he just wants to copy line by line without modifying the format, well, then he can just copy the file and use his editor to delete the unwanted lines.
So I guess best thing would be if Tony could give some more information on what is needed. All the input here shows that we can do quite a lot of different things reading the file - and that Gordon's first reply offers a very simple solution. And some background on why it has to be fortran to just copy the data would be interesting too.
Norbert
The optimist believes we live in the best of all possible worlds - the pessimist fears this might be true. |
|
Sorry !
It ocurred to me myself, that Tony could easily use his editor to removethe unwanted lines (and then rework the format with a much simpler code) - after I had finished my coding example.
Peace !
Norbert
The optimist believes we live in the best of all possible worlds - the pessimist fears this might be true. |
|
|
 |
|