# Reading a Fortran binary file using Mathematica

**URL:** <https://community.wolfram.com/t/reading-a-fortran-binary-file-using-mathematica/19767>\
**Category:** Questions\
**Tags:** import-and-export\
**Created:** [March 17, 2022, 8:28am UTC](https://community.wolfram.com/t/reading-a-fortran-binary-file-using-mathematica/19767 "2022-03-17T08:28:31Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![hongyizhao](https://avatars.discourse-cdn.com/v4/letter/h/8491ac/32.png) [@hongyizhao](https://community.wolfram.com/u/hongyizhao)\
**Post date:** [March 17, 2022, 8:28am UTC](https://community.wolfram.com/t/reading-a-fortran-binary-file-using-mathematica/19767/1 "2022-03-17T08:28:31Z")

</div>

I want to use Mathematica to read a FORTRAN binary file, for example, [kLG\_1.data](https://github.com/hongyi-zhao/irvsp/blob/master/IRVSPDATA/kLittleGroups/kLG_1.data). Would someone please give me some tips?

Regards,  
HZ

---

<div class="post-metadata">

**Author:** ![jbaldwinpsw](https://avatars.discourse-cdn.com/v4/letter/j/ee7513/32.png) [@jbaldwinpsw](https://community.wolfram.com/u/jbaldwinpsw)\
**Post date:** [March 17, 2022, 4:39pm UTC](https://community.wolfram.com/t/reading-a-fortran-binary-file-using-mathematica/19767/2 "2022-03-17T16:39:05Z")

</div>

Here is a brute force approach.

```
getBinary[filename_] := Module[{data},
  (* Imports a specific kind of binary Fortran output file *)
  
(* Import data as a list of ascii codes *)
data = Import[filename, "Binary"];

(* Keep only desired characters *)
data = Select[data,
   # == 32 || # == 40 || # == 45 || # == 46 || 48 <= # <= 57 || 65 <= # <= 90 || 97 <= # <= 122 &];

(* Change "(" to "},{" pair *)
data = Flatten[data /. 40 -> {125, 44, 123}];

(* Convert to character *)
data = FromCharacterCode[#] & /@ data;

(* Add beginning and ending curly brackets *)
data = StringJoin[Flatten[{{"{{"}, data, {"}}"}}]];

(* Get rid of extra spaces *)
data = StringReplace[data, " " -> ""];
data = StringReplace[data, " }" -> "}"];

(* Convert remaining spaces into commas *)
data = StringReplace[data, " " -> ","];

(* Remove empty lists *)
data = StringReplace[data, ",{}" -> ""];

(* Change minuses before commas *)
data = StringReplace[data, "-," -> "minus,"];

(* Convert to an expression *)
data = ToExpression[data];

(* Remove first and last element *)
data[[2 ;; Length[data] - 1]]]

getBinary["kLG_1.data"]

```

Resulting in the following:

```
(* {{0.123, 0.313, 0.427, 0, GP1, 1, 2, GP, 0}, 
    {0.123, 0.313, 0.427, 0, -GP2, 1, 2, GP, 0},
    {0.5, 0.5, 0.5, 1, R1, 1, 2, R, 1},
    {0.5, 0.5, 0.5, 1, -R2, 1, 2, R, 1}, 
    {0, 0.5, 0.5, 1, T1, 1, 2, T, 1}, 
    {0, 0.5, 0.5, 1, -T2, 1, 2, T, 1}, 
    {0.5, 0, 0.5, 1, U1, 1, 2, U, 1}, 
    {0.5, 0, 0.5, 1, -U2, 1, 2, U, 1},
    {0.5, 0.5, 0, 1, V1, 1, 2, V, 1},
    {0.5, 0.5, 0, 1, -V2, 1, 2, V, 1}, 
    {0.5, 0, 0, 1, X1, 1, 2, X, 1}, 
    {0.5, 0, 0, 1, -X2, 1, 2, X, 1}, 
    {0, 0.5, 0, 1, Y1, 1, 2, Y, 1},
    {0, 0.5, 0, 1, -Y2, 1, 2, Y, 1},
    {0, 0, 0.5, 1, Z1, 1, 2, Z, 1},
    {0, 0, 0.5, 1, -Z2, 1, 2, Z, 1},
    {0, 0, 0, 1, GM1, 1, 2, GM, 1},
    {0, 0, 0, 1, -GM2, 1, 2, GM, 1}} *)

```

---

<div class="post-metadata">

**Author:** ![hongyizhao](https://avatars.discourse-cdn.com/v4/letter/h/8491ac/32.png) [@hongyizhao](https://community.wolfram.com/u/hongyizhao)\
**Post date:** [March 18, 2022, 1:48am UTC](https://community.wolfram.com/t/reading-a-fortran-binary-file-using-mathematica/19767/3 "2022-03-18T01:48:24Z")

</div>

I still have some questions about your code, as described below.

(1). How did you come up with the following code?

```auto
      (*Remove some characters*)
        data = Select[data, # >= 32 && # != 124 && # != 240 && # != 63 &];

```

(2). You have used the following code snippet:

```auto
(*Change ")" to "},{" pair*)

data = Flatten[data /. 40 -> {125, 44, 123}];

```

But the “)” in the decimal ascii table is represented by 41, as shown below:

```auto
$ ascii -d | grep -Eo '(12[35]|4[014]) [^]+'
40 (
41 )
123 {
44 ,
125 }

```

(3). What is the correspondence between your data extraction logic and the structure of the file shown below? The attached file is the internal format of this FORTRAN binary shown in Emacs obtained by (M-x hexl-find-file):

 ![3061Selection_028.png](https://global.discourse-cdn.com/wolfram/original/2X/9/9ba9acaa3046fd78af14063c1447d2239ec3f332.png)

(4). In fact, I’ve so many such Fortran binary files located [here](https://github.com/hongyi-zhao/irvsp/tree/master/IRVSPDATA/kLittleGroups), and your above code can only manipulate/handle the one discussed in this issue. I want to find a general method to deal with all these files.

Regards,  
HZ

---

<div class="post-metadata">

**Author:** ![jbaldwinpsw](https://avatars.discourse-cdn.com/v4/letter/j/ee7513/32.png) [@jbaldwinpsw](https://community.wolfram.com/u/jbaldwinpsw)\
**Post date:** [March 18, 2022, 4:50am UTC](https://community.wolfram.com/t/reading-a-fortran-binary-file-using-mathematica/19767/4 "2022-03-18T04:50:01Z")

</div>

(1) I wanted to ignore all characters below character code 32, and remove characters with character codes 63 (?), 124 (|), and 240 (ð - hex f0).

(2) That was a mistake in the comment I wrote. Sorry about that. I meant to have used “(” rather than “)”. I’ve now fixed that. (A new record seemed to start with “(”.)

(3) Logic? I opened up the file in NotePad and saw what appeared to be lines of data surrounded by nulls and some odds and ends of other characters. The “(” character seemed to precede each line of data. Other characters also seemed to consistently precede the start of a record (such as a hex 10) and could have been chosen to do the job.

---

<div class="post-metadata">

**Author:** ![jbaldwinpsw](https://avatars.discourse-cdn.com/v4/letter/j/ee7513/32.png) [@jbaldwinpsw](https://community.wolfram.com/u/jbaldwinpsw)\
**Post date:** [March 18, 2022, 5:25am UTC](https://community.wolfram.com/t/reading-a-fortran-binary-file-using-mathematica/19767/5 "2022-03-18T05:25:49Z")

</div>

I’ve tried some of the other files and found elements that look like “GM-” which don’t convert to a _Mathematica_ expression. What should be done with those?

For a temporary fix I changed things like “GM-” to “GMminus”.

---

<div class="post-metadata">

**Author:** ![hongyizhao](https://avatars.discourse-cdn.com/v4/letter/h/8491ac/32.png) [@hongyizhao](https://community.wolfram.com/u/hongyizhao)\
**Post date:** [March 18, 2022, 7:36am UTC](https://community.wolfram.com/t/reading-a-fortran-binary-file-using-mathematica/19767/6 "2022-03-18T07:36:58Z")

</div>

As far as your revised version is concerned, I have the following comments:

(1) In the ultimate output, I think you should restore the changes you’ve done by `"GM-" to "GMminus"` as an expedience.

(2) I checked with [`kLG_ 158.data`](https://github.com/hongyi-zhao/irvsp/blob/master/IRVSPDATA/kLittleGroups/kLG_158.data), and find the following strange output:

```auto
{FkSUFkSUFkSUFkSU}

```

This is obviously not reasonable data.

BTW, if you are on \*nix platforms, the following method can be used to quickly check the content of these files, taking [`kLG_ 158.data`](https://github.com/hongyi-zhao/irvsp/blob/master/IRVSPDATA/kLittleGroups/kLG_158.data) as an example:

```bash
$ strings kLG_158.data | grep -v '^\?' | sed -re 's/\($//' | awk '!a[$0]++'
P3c1      
0 0 0.5 1 A1 1 12 A 0                   
0 0 0.5 1 A2 1 12 A 0                   
0 0 0.5 1 A3 2 12 A -1                  
0 0 0.5 1 -A4 1 12 A 1                  
0 0 0.5 1 -A5 1 12 A 1                  
0 0 0.5 1 -A6 2 12 A 1                  
0 0 0.427 0 DT1 1 12 DT 0               
0 0 0.427 0 DT2 1 12 DT 0               
0 0 0.427 0 DT3 2 12 DT 0               
0 0 0.427 0 -DT4 1 12 DT 0              
0 0 0.427 0 -DT5 1 12 DT 0              
0 0 0.427 0 -DT6 2 12 DT 0              
0 0 0 1 GM1 1 12 GM 1                   
0 0 0 1 GM2 1 12 GM 1                   
0 0 0 1 GM3 2 12 GM 1                   
0 0 0 1 -GM4 1 12 GM 0                  
0 0 0 1 -GM5 1 12 GM 0                  
0 0 0 1 -GM6 2 12 GM -1                 
0.333333 0.333333 0.5 1 H1 1 6 H -1     
0.333333 0.333333 0.5 1 H2 1 6 H -1     
0.333333 0.333333 0.5 1 H3 1 6 H -1     
0.333333 0.333333 0.5 1 -H4 1 6 H 1     
0.333333 0.333333 0.5 1 -H5 1 6 H 1     
0.333333 0.333333 0.5 1 -H6 1 6 H 1     
0.333333 0.333333 0 1 K1 1 6 K 1        
0.333333 0.333333 0 1 K2 1 6 K 1        
0.333333 0.333333 0 1 K3 1 6 K 1        
0.333333 0.333333 0 1 -K4 1 6 K -1      
0.333333 0.333333 0 1 -K5 1 6 K -1      
0.333333 0.333333 0 1 -K6 1 6 K -1      
0.333333 0.333333 0.427 0 P1 1 6 P 0    
0.333333 0.333333 0.427 0 P2 1 6 P 0    
0.333333 0.333333 0.427 0 P3 1 6 P 0    
0.333333 0.333333 0.427 0 -P4 1 6 P 0   
0.333333 0.333333 0.427 0 -P5 1 6 P 0   
0.333333 0.333333 0.427 0 -P6 1 6 P 0   
0.123 0 0.427 0 D1 1 4 D 0              
0.123 0 0.427 0 D2 1 4 D 0              
0.123 0 0.427 0 -D3 1 4 D 0             
0.123 0 0.427 0 -D4 1 4 D 0             
0.5 0 0.5 1 L1 1 4 L 0                  
0.5 0 0.5 1 L2 1 4 L 0                  
0.5 0 0.5 1 -L3 1 4 L 1                 
0.5 0 0.5 1 -L4 1 4 L 1                 
0.5 0 0 1 M1 1 4 M 1                    
0.5 0 0 1 M2 1 4 M 1                    
0.5 0 0 1 -M3 1 4 M 0                   
0.5 0 0 1 -M4 1 4 M 0                   
0.123 0 0.5 0 R1 1 4 R 0                
0.123 0 0.5 0 R2 1 4 R 0                
0.123 0 0.5 0 -R3 1 4 R 0               
0.123 0 0.5 0 -R4 1 4 R 0               
0.123 0 0 0 SM1 1 4 SM 0                
0.123 0 0 0 SM2 1 4 SM 0                
0.123 0 0 0 -SM3 1 4 SM 0               
0.123 0 0 0 -SM4 1 4 SM 0               
0.5 0 0.427 0 U1 1 4 U 0                
0.5 0 0.427 0 U2 1 4 U 0                
0.5 0 0.427 0 -U3 1 4 U 0               
0.5 0 0.427 0 -U4 1 4 U 0               
0.123 0.313 0 0 B1 1 2 B 0              
0.123 0.313 0 0 -B2 1 2 B 0             
0.123 0.123 0.427 0 C1 1 2 C 0          
0.123 0.123 0.427 0 -C2 1 2 C 0         
0.123 0.313 0.5 0 E1 1 2 E 0            
0.123 0.313 0.5 0 -E2 1 2 E 0           
0.123 0.313 0.427 0 GP1 1 2 GP 0        
0.123 0.313 0.427 0 -GP2 1 2 GP 0       
0.123 0.123 0 1 LD1 1 2 LD 1            
0.123 0.123 0 1 -LD2 1 2 LD -1          
0.123 0.123 0.5 1 Q1 1 2 Q -1           
0.123 0.123 0.5 1 -Q2 1 2 Q 1         

```

(3) The corresponding FORTRAN code snippet that reads these files appears to be located [here](https://github.com/hongyi-zhao/irvsp/blob/2bb6ca2fb2628022ed8bad99cc3e48dac1cf90cc/src_irvsp_v2/nonsymm.f90#L202). But I failed to figure out the logic used by it.

Regards,  
HZ

---

<div class="post-metadata">

**Author:** ![jbaldwinpsw](https://avatars.discourse-cdn.com/v4/letter/j/ee7513/32.png) [@jbaldwinpsw](https://community.wolfram.com/u/jbaldwinpsw)\
**Post date:** [March 18, 2022, 4:30pm UTC](https://community.wolfram.com/t/reading-a-fortran-binary-file-using-mathematica/19767/7 "2022-03-18T16:30:06Z")

</div>

I have had to guess as to the format of the file and the structure of the data and assumed from your question that you wanted everything done in _Mathematica_. Your “\*nix platforms” code seems to do most of the work. I would recommend starting from there.

---

<div class="post-metadata">

**Author:** ![jbaldwinpsw](https://avatars.discourse-cdn.com/v4/letter/j/ee7513/32.png) [@jbaldwinpsw](https://community.wolfram.com/u/jbaldwinpsw)\
**Post date:** [March 18, 2022, 9:03pm UTC](https://community.wolfram.com/t/reading-a-fortran-binary-file-using-mathematica/19767/8 "2022-03-18T21:03:06Z")

</div>

Here is hopefully a more robust version (again only using _Mathematica_) (updated to reflect additional restrictions)

```
getBinary[filename_] := Module[{data},
  (*Imports a specific kind of binary Fortran output file*)
  
  (*Import data as a list of ascii codes*)
  data = Import[filename, "Binary"];
  
  (*Keep only desired characters*)
  data = Select[
    data, # == 32 || # == 40 || # == 45 || # == 46 || 48 <= # <= 57 || 65 <= # <= 90 || 97 <= # <= 122 &];
  
  (* Convert to character and split into potential rows of data*)
  data = FromCharacterCode[#] & /@ data;
  data = StringSplit[StringJoin[data], "("];
  
  (* Remove trailing blanks *)
  data = StringTrim[data];
  
  (* Convert remaining spaces into commas *)
  data = StringReplace[#, " " -> ","] & /@ data;
  
  (* Split each row of data into the 9 elements *)
  data = StringSplit[#, ","] & /@ data;
  
  (* Make the first element being the "headline" *)
  data = {data[[1, 1]], data[[2 ;;]]};
  
  (* Remove any elements that don't have length 9 *)
  data[[2]] = Select[data[[2]], Length[#] == 9 &];
  
  (* The last list in data[[2]] is apparently a duplicate of the previous list, so remove it. *)
  data[[2]] = data[[2, 1 ;; Length[data[[2]]] - 1]];
  
  (* Convert to expressions but leave elements 5 and 8 as strings *)
  data[[2]] = {ToExpression[#[[1]]], ToExpression[#[[2]]], 
      ToExpression[#[[3]]], ToExpression[#[[4]]], #[[5]], 
      ToExpression[#[[6]]], ToExpression[#[[7]]], #[[8]], 
      ToExpression[#[[9]]]} & /@ data[[2]];
  
  data
  ]

d = getBinary["kLG_1.data"]

```

The result is

```
{"P1", {{0.123, 0.313, 0.427, 0, "GP1", 1, 2, "GP", 0}, {0.123, 0.313, 0.427, 0, "-GP2", 1, 2, "GP", 0}, 
{0.5, 0.5, 0.5, 1, "R1", 1, 2, "R", 1}, {0.5, 0.5, 0.5, 1, "-R2", 1, 2, "R", 1}, {0, 0.5, 0.5, 1, "T1", 1, 2, "T", 1}, {0, 0.5, 0.5, 1, "-T2", 1, 2, "T", 1},
{0.5, 0, 0.5, 1, "U1", 1, 2, "U", 1}, {0.5, 0, 0.5, 1, "-U2", 1, 2, "U", 1}, {0.5, 0.5, 0, 1, "V1", 1, 2, "V", 1}, 
{0.5, 0.5, 0, 1, "-V2", 1, 2, "V", 1}, {0.5, 0, 0, 1, "X1", 1, 2, "X", 1}, {0.5, 0, 0, 1, "-X2", 1, 2, "X", 1},
{0, 0.5, 0, 1, "Y1", 1, 2, "Y", 1}, {0, 0.5, 0, 1, "-Y2", 1, 2, "Y", 1}, {0, 0, 0.5, 1, "Z1", 1, 2, "Z", 1},
{0, 0, 0.5, 1, "-Z2", 1, 2, "Z", 1}, {0, 0, 0, 1, "GM1", 1, 2, "GM", 1}, {0, 0, 0, 1, "-GM2", 1, 2, "GM", 1}}}

```

---

<div class="post-metadata">

**Author:** ![hongyizhao](https://avatars.discourse-cdn.com/v4/letter/h/8491ac/32.png) [@hongyizhao](https://community.wolfram.com/u/hongyizhao)\
**Post date:** [March 19, 2022, 3:03am UTC](https://community.wolfram.com/t/reading-a-fortran-binary-file-using-mathematica/19767/9 "2022-03-19T03:03:31Z")

</div>

Your code is basically close to perfect. According to my test results, the problem as described below still exists.

(1). Taking `kLG_ 12.data` as an example, as you can see, there are so many **Null** in the first line:

```
{
 {C2, Null, Null, Null, "", Null, Null, "", 6},
 {0, 0, 0.5, 1, "A1", 1, 8, "A", 1},
 {0, 0, 0.5, 1, "A1-", 1, 8, "A", 1},
 {0, 0, 0.5, 1, "A2", 1, 8, "A", 1},
 {0, 0, 0.5, 1, "A2-", 1, 8, "A", 1},
 {0, 0, 0.5, 1, "-A3", 1, 8, "A", 0},
 {0, 0, 0.5, 1, "-A4", 1, 8, "A", 0},
 {0, 0, 0.5, 1, "-A5", 1, 8, "A", 0},
 {0, 0, 0.5, 1, "-A6", 1, 8, "A", 0},
 {0, 0, 0, 1, "GM1", 1, 8, "GM", 1},
 {0, 0, 0, 1, "GM1-", 1, 8, "GM", 1},
 {0, 0, 0, 1, "GM2", 1, 8, "GM", 1},
 {0, 0, 0, 1, "GM2-", 1, 8, "GM", 1},
 {0, 0, 0, 1, "-GM3", 1, 8, "GM", 0},
 {0, 0, 0, 1, "-GM4", 1, 8, "GM", 0},
 {0, 0, 0, 1, "-GM5", 1, 8, "GM", 0},
 {0, 0, 0, 1, "-GM6", 1, 8, "GM", 0},
 {0, 1, 0.5, 1, "M1", 1, 8, "M", 1},
 {0, 1, 0.5, 1, "M1-", 1, 8, "M", 1},
 {0, 1, 0.5, 1, "M2", 1, 8, "M", 1},
 {0, 1, 0.5, 1, "M2-", 1, 8, "M", 1},
 {0, 1, 0.5, 1, "-M3", 1, 8, "M", 0},
 {0, 1, 0.5, 1, "-M4", 1, 8, "M", 0},
 {0, 1, 0.5, 1, "-M5", 1, 8, "M", 0},
 {0, 1, 0.5, 1, "-M6", 1, 8, "M", 0},
 {0, 1, 0, 1, "Y1", 1, 8, "Y", 1},
 {0, 1, 0, 1, "Y1-", 1, 8, "Y", 1},
 {0, 1, 0, 1, "Y2", 1, 8, "Y", 1},
 {0, 1, 0, 1, "Y2-", 1, 8, "Y", 1},
 {0, 1, 0, 1, "-Y3", 1, 8, "Y", 0},
 {0, 1, 0, 1, "-Y4", 1, 8, "Y", 0},
 {0, 1, 0, 1, "-Y5", 1, 8, "Y", 0},
 {0, 1, 0, 1, "-Y6", 1, 8, "Y", 0},
 {0.123, 0, 0.427, 1, "B1", 1, 4, "B", 1},
 {0.123, 0, 0.427, 1, "B2", 1, 4, "B", 1},
 {0.123, 0, 0.427, 1, "-B3", 1, 4, "B", 0},
 {0.123, 0, 0.427, 1, "-B4", 1, 4, "B", 0},
 {0.5, 0.5, 0.5, 1, "L1", 1, 4, "L", 1},
 {0.5, 0.5, 0.5, 1, "L1-", 1, 4, "L", 1},
 {0.5, 0.5, 0.5, 1, "-L2", 1, 4, "L", 1},
 {0.5, 0.5, 0.5, 1, "-L3", 1, 4, "L", 1},
 {0, 0.313, 0, 1, "LD1", 1, 4, "LD", 1},
 {0, 0.313, 0, 1, "LD2", 1, 4, "LD", 1},
 {0, 0.313, 0, 1, "-LD3", 1, 4, "LD", 0},
 {0, 0.313, 0, 1, "-LD4", 1, 4, "LD", 0},
 {0, 0.313, 0.5, 1, "U1", 1, 4, "U", 1},
 {0, 0.313, 0.5, 1, "U2", 1, 4, "U", 1},
 {0, 0.313, 0.5, 1, "-U3", 1, 4, "U", 0},
 {0, 0.313, 0.5, 1, "-U4", 1, 4, "U", 0},
 {0.5, 0.5, 0, 1, "V1", 1, 4, "V", 1},
 {0.5, 0.5, 0, 1, "V1-", 1, 4, "V", 1},
 {0.5, 0.5, 0, 1, "-V2", 1, 4, "V", 1},
 {0.5, 0.5, 0, 1, "-V3", 1, 4, "V", 1},
 {0.123, 0.313, 0.427, 1, "GP1", 1, 2, "GP", 1},
 {0.123, 0.313, 0.427, 1, "-GP2", 1, 2, "GP", 1},
 {0.123, 0.313, 0.427, 1, "-GP2", 1, 2, "GP", 1}
}

```

(2) The last two lines are the same, which is why I use **awk ‘!a[$0]++’** in my solution.

(3) Your code will remove the “+” symbol in the output, for example, the following one:

```
0.5 0.5 0 1 V1+ 1 4 V 1   

```

will be displayed as the following error result:

```
0.5 0.5 0 1 V1 1 4 V 1  

```

Regards,  
HZ

---

<div class="post-metadata">

**Author:** ![jbaldwinpsw](https://avatars.discourse-cdn.com/v4/letter/j/ee7513/32.png) [@jbaldwinpsw](https://community.wolfram.com/u/jbaldwinpsw)\
**Post date:** [March 19, 2022, 4:57am UTC](https://community.wolfram.com/t/reading-a-fortran-binary-file-using-mathematica/19767/10 "2022-03-19T04:57:17Z")

</div>

I’ve updated the code in the following ways:

(1) The numeric fields are tested to see if they are numeric. If any of those fields are not numeric, then the observation is deleted.

(2) Duplicates are deleted. (Now if the only duplicates that should be deleted are the last two lines, then the code could be modified to reflect that.)

(3) A “+” is now included as a legitimate character.

---

<div class="post-metadata">

**Author:** ![hongyizhao](https://avatars.discourse-cdn.com/v4/letter/h/8491ac/32.png) [@hongyizhao](https://community.wolfram.com/u/hongyizhao)\
**Post date:** [March 19, 2022, 7:32am UTC](https://community.wolfram.com/t/reading-a-fortran-binary-file-using-mathematica/19767/11 "2022-03-19T07:32:29Z")

</div>

Great. It works perfectly, except that the headline which denoting the space group name should also be preserved, as shown below, taking **kLG\_1.data** as an example:

(1) The result given by your code:

```
{
 {0.123, 0.313, 0.427, 0, "GP1", 1, 2, "GP", 0},
 {0.123, 0.313, 0.427, 0, "-GP2", 1, 2, "GP", 0},
 {0.5, 0.5, 0.5, 1, "R1", 1, 2, "R", 1},
 {0.5, 0.5, 0.5, 1, "-R2", 1, 2, "R", 1},
 {0, 0.5, 0.5, 1, "T1", 1, 2, "T", 1},
 {0, 0.5, 0.5, 1, "-T2", 1, 2, "T", 1},
 {0.5, 0, 0.5, 1, "U1", 1, 2, "U", 1},
 {0.5, 0, 0.5, 1, "-U2", 1, 2, "U", 1},
 {0.5, 0.5, 0, 1, "V1", 1, 2, "V", 1},
 {0.5, 0.5, 0, 1, "-V2", 1, 2, "V", 1},
 {0.5, 0, 0, 1, "X1", 1, 2, "X", 1},
 {0.5, 0, 0, 1, "-X2", 1, 2, "X", 1},
 {0, 0.5, 0, 1, "Y1", 1, 2, "Y", 1},
 {0, 0.5, 0, 1, "-Y2", 1, 2, "Y", 1},
 {0, 0, 0.5, 1, "Z1", 1, 2, "Z", 1},
 {0, 0, 0.5, 1, "-Z2", 1, 2, "Z", 1},
 {0, 0, 0, 1, "GM1", 1, 2, "GM", 1},
 {0, 0, 0, 1, "-GM2", 1, 2, "GM", 1}
}

```

(2) The result given by my \*nix tools solution:

```
$ strings kLG_1.data | grep -v '^\?' | sed -re 's/\($//' | awk '!a[$0]++'
P1        
0.123 0.313 0.427 0 GP1 1 2 GP 0        
0.123 0.313 0.427 0 -GP2 1 2 GP 0       
0.5 0.5 0.5 1 R1 1 2 R 1                
0.5 0.5 0.5 1 -R2 1 2 R 1               
0 0.5 0.5 1 T1 1 2 T 1                  
0 0.5 0.5 1 -T2 1 2 T 1                 
0.5 0 0.5 1 U1 1 2 U 1                  
0.5 0 0.5 1 -U2 1 2 U 1                 
0.5 0.5 0 1 V1 1 2 V 1                  
0.5 0.5 0 1 -V2 1 2 V 1                 
0.5 0 0 1 X1 1 2 X 1                    
0.5 0 0 1 -X2 1 2 X 1                   
0 0.5 0 1 Y1 1 2 Y 1                    
0 0.5 0 1 -Y2 1 2 Y 1                   
0 0 0.5 1 Z1 1 2 Z 1                    
0 0 0.5 1 -Z2 1 2 Z 1                   
0 0 0 1 GM1 1 2 GM 1                    
0 0 0 1 -GM2 1 2 GM 1             

```

The headline, that is, the **P1** here is also a part of the real data, so should be preserved in the output.

Regards,  
HZ

---

<div class="post-metadata">

**Author:** ![jbaldwinpsw](https://avatars.discourse-cdn.com/v4/letter/j/ee7513/32.png) [@jbaldwinpsw](https://community.wolfram.com/u/jbaldwinpsw)\
**Post date:** [March 19, 2022, 5:35pm UTC](https://community.wolfram.com/t/reading-a-fortran-binary-file-using-mathematica/19767/12 "2022-03-19T17:35:30Z")

</div>

Updated code above.

---

<div class="post-metadata">

**Author:** ![hongyizhao](https://avatars.discourse-cdn.com/v4/letter/h/8491ac/32.png) [@hongyizhao](https://community.wolfram.com/u/hongyizhao)\
**Post date:** [March 19, 2022, 11:31pm UTC](https://community.wolfram.com/t/reading-a-fortran-binary-file-using-mathematica/19767/13 "2022-03-19T23:31:03Z")

</div>

Some additional side remarks:

(1) Though the above code and the \*nix tools can extract the strings/ASCII represented data in the Fortran binary file, they are essentially meaningless methods, as described [below](https://fortran-lang.discourse.group/t/read-out-the-content-of-a-fortran-binary-file-based-on-the-limited-information-given-by-its-hexadecimal-representation/3000/15?u=hongyi):

> Sorry, this line of reasoning / method of extracting information makes no sense at all. The Linux/Unix string utility displays printable characters when they occur consecutively in clumps of 4 or more. Everything else in the file is ignored! When a Fortran unformatted file is written, most of the bytes in the file correspond to “unprintable” characters, so by ignoring them you are discarding almost all the numerical information (integers, reals, etc.) and carrying on as if what is left is all that is important.

> For instance, for the data file kLG\_1.data, your application of strings and sed leaves discards 90 percent of the content of the file.

> The question is, do you have the Fortran program sources that produced every one of the unformatted data files that are of interest to you? Or, at least complete and precise documentation of the structure and content of the files? If not, you are simply inviting us to go with you for a walk in a minefield while wearing blindfolds.

(2) The only correct way to ensure correct reading of such files is described [here](https://fortran-lang.discourse.group/t/read-out-the-content-of-a-fortran-binary-file-based-on-the-limited-information-given-by-its-hexadecimal-representation/3000/17?u=hongyi).

---

<div class="post-metadata">

**Author:** ![jbaldwinpsw](https://avatars.discourse-cdn.com/v4/letter/j/ee7513/32.png) [@jbaldwinpsw](https://community.wolfram.com/u/jbaldwinpsw)\
**Post date:** [March 20, 2022, 12:09am UTC](https://community.wolfram.com/t/reading-a-fortran-binary-file-using-mathematica/19767/14 "2022-03-20T00:09:05Z")

</div>

I’m sorry the author of that comment has such limited experience. If they had access to your hex output they should have seen that there really weren’t any such reals or double precision numbers. Many of the bytes were nulls and just a few “)”, “|”, and “?”.

But the main source of whether what is found by the _Mathematica_ code or your grep/sed/awk commands is legit is your assessment of the output found so far. If what comes out (rows of 9 elements, two of which are character strings) matches what you think should be there, then that’s all that’s needed.

So, yes, in general one does need to worry about reals and double precision numbers stored as binary characters. But it doesn’t appear to be the case for your files.

And, yes, going through the Fortran code would answer this decisively. However, I’m not going to take the time for that. But to repeat, that really doesn’t appear necessary in this case.

The internet is full of ignorant responses. (Including me sometimes.)

---

<div class="post-metadata">

**Author:** ![hongyizhao](https://avatars.discourse-cdn.com/v4/letter/h/8491ac/32.png) [@hongyizhao](https://community.wolfram.com/u/hongyizhao)\
**Post date:** [March 20, 2022, 12:44am UTC](https://community.wolfram.com/t/reading-a-fortran-binary-file-using-mathematica/19767/15 "2022-03-20T00:44:02Z")

</div>

> I’m sorry the author of that comment has such limited experience. If they had access to your hex output they should have seen that there really weren’t any such reals or double precision numbers. Many of the bytes were nulls and just a few “)”, “|”, and “?”.

I think he commented on how to ensure the correctness of the final data in general.

> But the main source of whether what is found by the Mathematica code or your grep/sed/awk commands is legit is your assessment of the output found so far. If what comes out (rows of 9 elements, two of which are character strings) matches what you think should be there, then that’s all that’s needed.

> And, yes, going through the Fortran code would answer this decisively. However, I’m not going to take the time for that. But to repeat, that really doesn’t appear necessary in this case.

I want data and metadata, which together determine the structure of the data and the corresponding meaning of its fields. For this purpose, only the accurate reading method consistent with the Fortran source code is meaningful in the subsequent data analysis. Otherwise, what I finally get is a pile of meaningless garbage.

> The internet is full of ignorant responses. (Including me sometimes.)

Nobody is perfect; everyone makes mistakes. It is important to thoroughly understand what we really want, and constantly seek, explore, and finally achieve our goals.

---

<div class="post-metadata">

**Author:** ![jbaldwinpsw](https://avatars.discourse-cdn.com/v4/letter/j/ee7513/32.png) [@jbaldwinpsw](https://community.wolfram.com/u/jbaldwinpsw)\
**Post date:** [March 20, 2022, 1:48am UTC](https://community.wolfram.com/t/reading-a-fortran-binary-file-using-mathematica/19767/16 "2022-03-20T01:48:56Z")

</div>

My comments were about the “meaningless” statement made by that respondent.

On a different subject: If you are asking the same question simultaneously on different forums, you should mention that on each forum as you might find the answer on one forum and the rest of us not knowing about that answer (or instructive set of comments) would be wasting our time. And I don’t think you want to do that.

---

<div class="post-metadata">

**Author:** ![hongyizhao](https://avatars.discourse-cdn.com/v4/letter/h/8491ac/32.png) [@hongyizhao](https://community.wolfram.com/u/hongyizhao)\
**Post date:** [March 20, 2022, 1:56am UTC](https://community.wolfram.com/t/reading-a-fortran-binary-file-using-mathematica/19767/17 "2022-03-20T01:56:34Z")

</div>

Thank you for your good advice and excellent practical experience. I will pay attention to and comply with the recommendations in the future.

Yours,  
Hongyi
