This repository was archived by the owner on Feb 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsert-SecondaryColumn.rb
More file actions
61 lines (55 loc) · 2.09 KB
/
Insert-SecondaryColumn.rb
File metadata and controls
61 lines (55 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
58
59
60
61
# Insert cells into a secondary column using selection criteria applied to cells of a primary column.
require 'Datavyu_API.rb'
######################### PARAMS ##########################
primaryColumn = 'primary' # Name of primary column
secondaryColumn = 'secondary' # Name of secondary column
selectCriteria = { # Codes and their valid values to use as selection criteria
'code01' => ['s'],
'code02' => ['h','o']
}
selectCriteraAll = false # Sets the condition for selection. Set to false to select if any criterion matches. Set to true to select iff all critera match.
setOnset = true # Set true to copy over onsets
setOffset = true # Set true to copy over offsets
clobber = false # Set true to overwriter secondaryColumn if it already exists
secondaryCodes = ['s','h','o'] # Codes to initialize in secondary column if creating new column
verbose = 1 # Verbosity, higher = more verbose output
######################### MAIN ROUTINE ####################
begin
# Fetch primary and secondary columns.
puts "Fetching column #{primaryColumn}" if verbose > 0
colPri = getVariable(primaryColumn)
raise "ColumnNotFound: #{primaryColumn}" if colPri.nil?
if clobber
puts "Creating column #{secondaryColumn}" if verbose > 0
colSec = createVariable(secondaryColumn,secondaryCodes)
else
puts "Fetching column #{secondaryColumn}" if verbose > 0
colSec = getVariable(secondaryColumn)
raise "Column already exists: #{secondaryColumn}" unless colSec.nil?
end
# Select cells based on selectCriteria
candCells = colPri.cells.select{
|c|
# Warning: logic ahead
valid = selectCriteraAll
selectCriteria.each_pair{
|code,values|
if selectCriteraAll
valid &= values.include?(c.get_arg(code))
else
valid |= values.include?(c.get_arg(code))
end
}
valid
}
for cell in candCells
ncell = colSec.make_new_cell()
ncell.change_arg('onset',cell.onset) if setOnset
ncell.change_arg('offset',cell.offset) if setOffset
end
puts "Saving column #{secondaryColumn} to database" if verbose > 0
setVariable(colSec)
rescue StandardError => e
puts e.message
puts e.backtrace
end