Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- open Printf
- open ExtLib
- let vtk = "140408aX_t01_ch01.vtk";;
- type vtkInfo = {
- size: int;
- dimensions: int list;
- }
- let grabVtkSizeInfo path =
- let ic = open_in path in
- let rec iterateTillInfoFilled tup channel =
- match tup with
- | (Some x, Some y) -> {size = int_of_string x; dimensions = List.map int_of_string y}
- | _ ->
- let line = input_line channel in
- let nTup = match (line, tup) with
- | (_,(size,_)) when (String.exists line "DIMENSIONS") ->
- let dim = match (String.nsplit line " ") with
- | _ :: tail -> Some tail
- | _ -> None
- in
- (size,dim)
- | (_,(_,dim)) when (String.exists line "POINT_DATA") ->
- let size = match (String.nsplit line " ") with
- | _ :: tail :: [] -> Some tail
- | _ -> None
- in
- (size, dim)
- | _ -> tup
- in
- iterateTillInfoFilled nTup channel
- in
- try
- let res = Some (iterateTillInfoFilled (None, None) ic) in
- close_in_noerr ic
- res
- with End_of_file ->
- close_in_noerr ic
- None
- let pgm = "test.pgm";;
- let oc = open_out pgm;;
- let dimInfo = match (grabVtkSizeInfo vtk) with
- | Some info -> info
- | _ ->
- eprintf "VTK file contained incomplete dimensional data..."
- {size = 0; dimensions = []};;
- fprintf oc "P2\n%d %d\n%d\n%s" 5 5 255 (String.join " " (List.map string_of_int dimInfo.dimensions));;
- let ic = open_in_bin path
- seek_in ic ((in_channel_length ic) - dimInfo.size)
- let () = match dimInfo.dimensions with
- | x :: y :: _ ->
- for i = 1 to x do
- for j = 1 to y do
- let b = input_byte in
- fprintf oc "%d " b
- done
- fprintf oc "\n"
- done
- | _ -> eprintf "No data to write...";;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement