Ec/Data/extract_data.ml

92 lines
2.1 KiB
OCaml
Raw Normal View History

2020-11-27 12:13:02 +01:00
let read_file filename =
let ic = open_in filename in
let rec aux accu =
try
let new_accu =
(input_line ic) :: accu
in
aux new_accu
with End_of_file ->
(close_in ic; List.rev accu)
in
aux []
let rec find_x x = function
| [] -> []
| line :: rest ->
let line = String.trim line in
match String.split_on_char ' ' line with
| x_test :: _ ->
if x_test = x then line :: rest
else find_x x rest
| _ -> find_x x rest
let get find_x conversion lines =
match find_x lines with
| line :: rest ->
begin
match String.split_on_char '=' line with
| _ :: n :: _ -> conversion @@ String.trim n, rest
| _ -> raise Exit
end
| [] -> raise Exit
let read_float x =
match String.split_on_char ' ' x with
| x :: _ -> float_of_string x
| [] -> invalid_arg x
let find_summary = find_x "N_det"
let get_ndet = get find_summary int_of_string
let find_nstates = find_x "N_states"
let get_nstates = get find_nstates int_of_string
let find_nsop = find_x "N_sop"
let get_nsop = get find_nsop int_of_string
let find_variance = find_x "Variance"
let get_variance = get find_variance read_float
let find_pt2 = find_x "PT2"
let get_pt2 = get find_pt2 read_float
let find_rpt2 = find_x "rPT2"
let get_rpt2 = get find_rpt2 read_float
let find_e = find_x "E"
let get_e = get find_e float_of_string
let rec read_block lines =
let ndet, lines = get_ndet lines in
let nstates, lines = get_nstates lines in
let nsop, lines = get_nsop lines in
Printf.printf "%10d %10d" ndet nsop ;
let rec aux lines = function
| 0 -> (Printf.printf "\n%!" ; lines)
| n -> (
let e, lines = get_e lines in
let variance, lines = get_variance lines in
let pt2, lines = get_pt2 lines in
let rpt2, lines = get_rpt2 lines in
Printf.printf " %16.10e %16.10f %16.10f" pt2 rpt2 e;
aux lines (n-1) )
in
aux lines nstates
let rec read_blocks lines =
try
let lines = read_block lines in
read_blocks lines
with Exit -> ()
let () =
Array.iteri (fun i fname ->
if i = 0 then () else
let lines = read_file fname in
read_blocks lines) Sys.argv