Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions app/classes/report/base_table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ def without_location_selects
'""',
'""',
'""',
'""'
'""',
"observations.inat_id"
]
end

Expand Down Expand Up @@ -111,7 +112,8 @@ def with_location_selects
"locations.east",
"locations.west",
"locations.high",
"locations.low"
"locations.low",
"observations.inat_id"
]
end

Expand Down
193 changes: 193 additions & 0 deletions app/classes/report/nama.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
# frozen_string_literal: true

module Report
# Format for export to NAMA.
class Nama < CSV
NAMA_PROJECT_NAME = "North American Mycological Association"

NAMA_LABELS = [
"Record Number",
"Field Photo",
"iNat number",
"MO number",
"MycoFlora Number",
"Genus",
"Det",
"Species_Epithet",
"Subspecies_Epithet",
"Variety_Epithet",
"Forma_Epithet",
"Sensu",
"Synonym",
"DetBy",
"Collector",
"CollDate",
"Country",
"StateProv",
"County",
"Town",
"Site Name",
"Precise Location",
"LatDeg",
"LongDeg",
"Elevation in M",
"Substrate",
"Tree Associates",
"Habit",
"CollNote",
"Disposition",
"DNA Status",
"CollNumber",
"Specimen status",
"Walk Number"
].freeze

def labels
NAMA_LABELS
end

def format_row(row)
["N/A", field_photo(row)] +
id_fields(row) +
name_fields(row) +
people_fields(row) +
[row.obs_when] +
location_fields(row) +
notes_fields(row)
end

private

def field_photo(row)
return "Yes" if /\+ *photo/i.match?(row.notes_export_formatted)

""
end

def id_fields(row)
[
row.inat_id,
row.obs_id,
field_slip_code(row)
]
end

def field_slip_code(row)
field_slip = FieldSlip.find_by(observation_id: row.obs_id)
field_slip&.code
end

def name_fields(row)
[
row.genus,
det(row),
row.species,
row.subspecies,
row.variety,
row.form,
sensu(row.name_author),
synonym(row)
]
end

def det(row)
obs_name = row.notes_to_hash[:Field_Slip_ID]
return "aff." if obs_name&.include?("aff.")

""
end

def sensu(author)
return author if /sensu/.match?(author)

""
end

def synonym(row)
fs_name = row.notes_to_hash[:Field_Slip_ID]
obs_name = row.name_text_name
return "" if fs_name&.include?(obs_name)

fs_name&.delete("_")
end

def people_fields(row)
[
identifier(row),
collector(row)
]
end

def identifier(row)
user_name(row.notes_to_hash[:Field_Slip_ID_By])
end

def collector(row)
user_name(row.notes_to_hash[:Collector])
end

def user_name(name_str)
return name_str unless name_str

@user_names ||= {}
return @user_names[name_str] if @user_names.include?(name_str)

result = find_user_name(name_str)
@user_names[name_str] = result
end

def find_user_name(name_str)
login = name_str[/\A_user (.*?)_/, 1]
return name_str unless login

user = User.find_by(login:)
return user.unique_text_name if user

name_str
end

def location_fields(row)
[
row.country,
row.state,
row.county,
town(row.locality),
site(row.locality),
"",
row.best_lat(4),
row.best_lng(4),
row.best_alt
]
end

def town(locality)
return locality.split(",", 2).first if locality&.include?(",")

""
end

def site(locality)
return locality.split(",", 2).second if locality&.include?(",")

locality
end

def notes_fields(row)
[
row.notes_to_hash[:Substrate],
row.notes_to_hash[:"Trees/Shrubs"],
row.notes_to_hash[:Habit],
row.notes_to_hash[:Other],
"",
sequence_code(row.notes_to_hash[:Other_Codes]),
"",
"",
""
]
end

def sequence_code(codes)
codes && (codes[/NEF24-\d+/] || "")
end
end
end
4 changes: 4 additions & 0 deletions app/classes/report/row.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,5 +169,9 @@ def val(num)
def add_val(val, num)
@vals[25 + num] = val
end

def inat_id
@vals[26]
end
end
end
7 changes: 7 additions & 0 deletions app/classes/report/row_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ def best_low
obs_alt || loc_low
end

def best_alt
return obs_alt if obs_alt
return (loc_high + loc_low) / 2 if loc_high && loc_low

loc_high || loc_low
end

# --------------------

def genus
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/observations/downloads_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ def create_report(args)
Report::Symbiota.new(args)
when "fundis"
Report::Fundis.new(args)
when "nama"
Report::Nama.new(args)
else
raise("Invalid download type: #{format.inspect}")
end
Expand Down
3 changes: 3 additions & 0 deletions app/views/controllers/observations/downloads/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
<%= radio_with_label(form: f, field: :format, value: :fundis,
checked: (@format == "fundis") && "checked",
label: :download_observations_fundis.t) %>
<%= radio_with_label(form: f, field: :format, value: :nama,
checked: (@format == "nama") && "checked",
label: :download_observations_nama.t) %>
</div>

<p><%= :download_observations_encoding.t %>:</p>
Expand Down
1 change: 1 addition & 0 deletions config/locales/en.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1523,6 +1523,7 @@
download_observations_darwin: Darwin Core
download_observations_symbiota: Symbiota
download_observations_fundis: FunDiS
download_observations_nama: North American Mycological Association (NAMA)
download_observations_what_is_this: what is this?
download_observations_encoding: Choose character encoding
download_observations_ascii: ASCII (no accents)
Expand Down
Loading