I'm trying to import data from the output of an old nuclear physics code. The data is tally results of particles crossing particular geometric cells. The output is generated with a great deal of extraneous information and no end of line delimiters. The information I want is generated in a 3 line form. I've bolded the information I'm looking to extract in the following example:

cell (1<6[5 5 0]<10[1 1 0]<9)
multiplier bin: -1.00000E+00 1 -6
0.00000E+00 0.0000

I can extract the cell information using:

a = FindList["input.txt", "1<", RecordSeparators -> {{"cell ("}, {")"}}]

However, I can't figure out to extract the second part. Is there a way to use string matching based on the fact that it will always be 6-digit scientific notation number followed by a single space then a 5 digit (<1) uncertainty?

EDIT: This scientific number will not always be zero. I should have written it more generally as X.XXXXXE+XX 0.YYYY where X and Y are any arbitrary value. (Even the plus sign is not constant for some values the value will be X.XXXXXE-XX 0.YYYY)

EDIT: To provide some more information on the output: The code is calculating the particle flux through a series of cells inside nested lattices. I'm currently working in a 11x11 lattice nested into a 5x5 lattice. Thus the full output would consists of 3025 versions of those 3 lines each with unique cell identifier varying from (1<6[-5:5 -5:5 0]<10[-2:2 -2:2 0]<9) and whatever the particle flux is in each cell as the numeric value in scientific notation. The "multiplier bin:..." line is a constant that multiplies the flux and will never change.

  • $\begingroup$ @george2079 I am not sure that your post-editing had clarified what OP want from the edited output... :(( $\endgroup$ Dec 18 '17 at 19:24
  • $\begingroup$ maybe useful if you showed more of the file. Are those asterisks in there? $\endgroup$
    – george2079
    Dec 18 '17 at 19:25
  • $\begingroup$ The asterisks are how the site bolded the text. When it was edited, the bolding disappeared and the asterisks appeared. $\endgroup$
    – T. Cardew
    Dec 18 '17 at 19:26
  • $\begingroup$ oh i see, i fixed the edit. I guess we cant do formatting inside a code block. (sorry bout that) $\endgroup$
    – george2079
    Dec 18 '17 at 19:29
  • $\begingroup$ can you clarify what you mean by no end of line delimiters. (how do you have blocks of 3 lines with no delimiters?) $\endgroup$
    – george2079
    Dec 18 '17 at 19:50

probably best to do something like this:

lines = Import[file, "Lines"];
cells = Flatten@
  Position[lines, s_String /; StringMatchQ[s, "cell (" ~~ __ ~~ ")"]]

(* indices where each cell starts *)

then extract data from the ith cell like:

ImportString[lines[[cells[[i]] + 2]], "Table"]

{ 0.,0.}

  • $\begingroup$ Thank you. I was able to get it working based off of this suggestion but I had to use StringContainsQ not StringMatchQ. I'm not sure why. $\endgroup$
    – T. Cardew
    Dec 18 '17 at 22:51
  • $\begingroup$ there may be a blank at the end of the line or something like that. $\endgroup$
    – george2079
    Dec 18 '17 at 23:50

You can Import the file and store all the words in a list to extract (and convert) those you want:


(*  {"cell", "1<6[5", "5", "0]<10[1", "1", "0", "9", "multiplier", "bin", \
     "1.00000E+00", "1", "-6", "0.00000E+00", "0.0000"}  

As you have solved the first part, for the second one:

ToExpression@Take[TextWords[Import["input.txt"]], -2]

(* {0.,0.}  *)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.