Advertisement
Guest User

Untitled

a guest
May 24th, 2015
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 2.78 KB | None | 0 0
  1. open Printf
  2. open ExtLib
  3.  
  4. let vtk = "140408aX_t01_ch01.vtk";;
  5.  
  6. type vtkInfo = {
  7.         size: int;
  8.         dimensions: int list;
  9. }
  10.  
  11. let grabVtkSizeInfo path =
  12.         let ic = open_in path in
  13.         let rec iterateTillInfoFilled tup channel =
  14.                 match tup with
  15.                         | (Some x, Some y) -> {size = int_of_string x; dimensions = List.map int_of_string y}
  16.                         | _ ->
  17.                                 let line = input_line channel in
  18.                                 let nTup = match (line, tup) with
  19.                                         | (_,(size,_)) when (String.exists line "DIMENSIONS") ->
  20.                                                 let dim = match (String.nsplit line " ") with
  21.                                                         | _ :: tail -> Some tail
  22.                                                         | _ -> None
  23.                                                 in
  24.                                                 (size,dim)
  25.                                         | (_,(_,dim)) when (String.exists line "POINT_DATA") ->
  26.                                                 let size = match (String.nsplit line " ") with
  27.                                                         | _ :: tail :: [] -> Some tail
  28.                                                         | _ -> None
  29.                                                 in
  30.                                                 (size, dim)
  31.                                         | _ -> tup
  32.                                 in
  33.                                 iterateTillInfoFilled nTup channel
  34.                 in
  35.         try
  36.                 let res = Some (iterateTillInfoFilled (None, None) ic) in
  37.                 close_in_noerr ic
  38.                 res
  39.         with End_of_file ->
  40.                 close_in_noerr ic
  41.                 None
  42.  
  43.                
  44.  
  45. let pgm = "test.pgm";;
  46.  
  47. let oc = open_out pgm;;
  48.  
  49. let dimInfo =  match (grabVtkSizeInfo vtk) with
  50.         | Some info -> info
  51.         | _ ->
  52.                         eprintf "VTK file contained incomplete dimensional data..."
  53.                         {size = 0; dimensions = []};;
  54.  
  55. fprintf oc "P2\n%d %d\n%d\n%s" 5 5 255 (String.join " " (List.map string_of_int dimInfo.dimensions));;
  56.  
  57. let ic = open_in_bin path
  58. seek_in ic ((in_channel_length ic) - dimInfo.size)
  59.  
  60. let () = match dimInfo.dimensions with
  61.         | x :: y :: _ ->
  62.                         for i = 1 to x do
  63.                                 for j = 1 to y do
  64.                                         let b = input_byte in
  65.                                         fprintf oc "%d " b
  66.                                 done
  67.                                 fprintf oc "\n"
  68.                         done
  69.         | _ -> eprintf "No data to write...";;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement