Class: NIFTI::NWrite

Inherits:
Object
  • Object
show all
Defined in:
lib/nifti/n_write.rb

Overview

The NWrite class handles the encoding of an NObject instance to a valid NIFTI string. The String is then written to file.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj, file_name, options = {}) ⇒ NWrite

Creates an NWrite instance.

Parameters

  • obj – A NObject instance which will be used to encode a NIfTI string.

  • file_name – A string, either specifying the path of a DICOM file to be loaded, or a binary DICOM string to be parsed.

  • options – A hash of parameters.

Options



25
26
27
28
29
30
# File 'lib/nifti/n_write.rb', line 25

def initialize(obj, file_name, options = {})
  @obj = obj
  @file_name = file_name
  # Array for storing error/warning messages:
  @msg = Array.new
end

Instance Attribute Details

#msgObject (readonly)

An array which records any status messages that are generated while encoding/writing the DICOM string.



11
12
13
# File 'lib/nifti/n_write.rb', line 11

def msg
  @msg
end

#successObject (readonly)

A boolean which reports whether the DICOM string was encoded/written successfully (true) or not (false).



13
14
15
# File 'lib/nifti/n_write.rb', line 13

def success
  @success
end

Instance Method Details

#init_variablesObject

Creates various variables used when encoding the DICOM string.



136
137
138
139
# File 'lib/nifti/n_write.rb', line 136

def init_variables
  # Until a DICOM write has completed successfully the status is 'unsuccessful':
  @success = false
end

#open_file(file) ⇒ Object

Tests if the path/file is writable, creates any folders if necessary, and opens the file for writing.

Parameters

  • file – A path/file string.



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/nifti/n_write.rb', line 104

def open_file(file)
  # Check if file already exists:
  if File.exist?(file)
    # Is it writable?
    if File.writable?(file)
      @file = get_new_file_writer(file)
    else
      # Existing file is not writable:
      @msg << "Error! The program does not have permission or resources to create the file you specified: (#{file})"
    end
  else
    # File does not exist.
    # Check if this file's path contains a folder that does not exist, and therefore needs to be created:
    folders = file.split(File::SEPARATOR)
    if folders.length > 1
      # Remove last element (which should be the file string):
      folders.pop
      path = folders.join(File::SEPARATOR)
      # Check if this path exists:
      unless File.directory?(path)
        # We need to create (parts of) this path:
        require 'fileutils'
        FileUtils.mkdir_p path
      end
    end
    # The path to this non-existing file is verified, and we can proceed to create the file:
    @file = get_new_file_writer(file)
  end
end

#writeObject

Handles the encoding of NIfTI information to string as well as writing it to file.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/nifti/n_write.rb', line 33

def write
  # Check if we are able to create given file:
  open_file(@file_name)
  # Go ahead and write if the file was opened successfully:
  if @file
    # Initiate necessary variables:
    init_variables
    @file_endian = false
    # Create a Stream instance to handle the encoding of content to a binary string:
    @stream = Stream.new(nil, @file_endian)
    # Tell the Stream instance which file to write to:
    @stream.set_file(@file)

    # Write Header and Image
    write_basic_header
    write_extended_header
    write_image

    # As file has been written successfully, it can be closed.
    @file.close
    # Mark this write session as successful:
    @success = true
  end

end

#write_basic_headerObject

Write Basic Header



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/nifti/n_write.rb', line 60

def write_basic_header
  HEADER_SIGNATURE.each do |header_item|
    begin
      name, length, type = *header_item
      str = @stream.encode(@obj.header[name], type)
      padded_str = @stream.encode_string_to_length(str, length)
      # puts @stream.index, name, str.unpack(@stream.vr_to_str(type))
      # pp padded_str.unpack(@stream.vr_to_str(type))

      @stream.write padded_str
      @stream.skip length
    rescue StandardError => e
      puts name, length, type, e
      raise e
    end
  end
end

#write_extended_headerObject

Write Extended Header



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/nifti/n_write.rb', line 79

def write_extended_header
  unless @obj.extended_header.empty?
    @stream.write @stream.encode([1,0,0,0], "BY")
    @obj.extended_header.each do |extension|
    @stream.write @stream.encode extension[:esize], "UL"
    @stream.write @stream.encode extension[:ecode], "UL"
    @stream.write @stream.encode_string_to_length(@stream.encode(extension[:data], "STR"), extension[:esize] - 8)
    end
  else
    @stream.write @stream.encode([0,0,0,0], "BY")
  end
end

#write_imageObject

Write Image



93
94
95
96
# File 'lib/nifti/n_write.rb', line 93

def write_image
  type = NIFTI_DATATYPES[@obj.header['datatype']]
  @stream.write @stream.encode(@obj.image, type)
end