Skip to content

Commit 6eba734

Browse files
committed
Merge branch 'master' of github.com:bsurc/pracprog
2 parents 2a95418 + cec7c3f commit 6eba734

File tree

2 files changed

+176
-136
lines changed

2 files changed

+176
-136
lines changed

ebird/cmd/ebird_import.go

Lines changed: 162 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -6,126 +6,157 @@ package main
66

77
import (
88
"bufio"
9+
"database/sql"
910
"fmt"
11+
"log"
1012
"os"
1113
"strconv"
1214
"strings"
15+
16+
_ "github.com/mattn/go-sqlite3"
1317
)
1418

1519
type Obs struct {
16-
GlobalUniqueIdentifier string
17-
Last_edited_date string
18-
Taxonomic_order string
19-
Category string
20-
Common_name string
21-
Scientific_name string
22-
Subspecies_common_name string
23-
Subspecies_scientific_name string
24-
Observation_count string
25-
Breeding_bird_atlas_code string
26-
Breeding_bird_atlas_category string
27-
AgeSex string
28-
Country string
29-
Country_code string
30-
State string
31-
State_code string
32-
County string
33-
County_code string
34-
Iba_code string
35-
Bcr_code string
36-
Usfws_code string
37-
Atlas_block string
38-
Locality string
39-
Locality_id string
40-
Locality_type string
41-
Latitude float64
42-
Longitude float64
43-
Observation_date string
44-
Time_observations_started string
45-
Observer_id string
46-
Sampling_event_identifier string
47-
Protocol_type string
48-
Protocol_code string
49-
Project_code string
50-
Duration_minutes int
51-
Effort_distance_km float64
52-
Effort_area_ha float64
53-
Number_observers int
54-
All_species_reported bool
55-
Group_identifier string
56-
Has_media bool
57-
Approved bool
58-
Reviewed bool
59-
Reason string
60-
Trip_comments string
61-
Species_comments string
20+
GlobalUniqueIdentifier string
21+
LastEditedDate string
22+
TaxonomicOrder string
23+
Category string
24+
CommonName string
25+
ScientificName string
26+
SubspeciesCommonName string
27+
SubspeciesScientificName string
28+
ObservationCount int
29+
BreedingBirdAtlasCode string
30+
BreedingBirdAtlasCategory string
31+
AgeSex string
32+
Country string
33+
CountryCode string
34+
State string
35+
StateCode string
36+
County string
37+
CountyCode string
38+
IBACode string
39+
BCRCode string
40+
USFWSCode string
41+
AtlasBlock string
42+
Locality string
43+
LocalityID string
44+
LocalityType string
45+
Latitude float64
46+
Longitude float64
47+
ObservationDate string
48+
TimeObservationsStarted string
49+
ObserverID string
50+
SamplingEventIdentifier string
51+
ProtocolType string
52+
ProtocolCode string
53+
ProjectCode string
54+
DurationMinutes int
55+
EffortDistanceKM float64
56+
EffortAreaHA float64
57+
NumberObservers int
58+
AllSpeciesReported bool
59+
GroupIdentifier string
60+
HasMedia bool
61+
Approved bool
62+
Reviewed bool
63+
Reason string
64+
TripComments string
65+
SpeciesComments string
6266
}
6367

6468
func decodeObs(vals []string) (Obs, error) {
6569
var birds Obs
6670
var err error
67-
if len(vals) != 43 {
68-
return birds, fmt.Errorf("bad values, got:%d, want: 33", len(vals))
71+
if len(vals) != 47 {
72+
return birds, fmt.Errorf("bad values, got:%d, want: 46", len(vals))
6973
}
7074
birds.GlobalUniqueIdentifier = vals[0]
71-
birds.Last_edited_date = vals[1]
72-
birds.Taxonomic_order = vals[2]
75+
birds.LastEditedDate = vals[1]
76+
birds.TaxonomicOrder = vals[2]
7377
birds.Category = vals[3]
74-
birds.Common_name = vals[4]
75-
birds.Scientific_name = vals[5]
76-
birds.Subspecies_common_name = vals[6]
77-
birds.Subspecies_scientific_name = vals[7]
78-
birds.Observation_count = vals[8]
79-
birds.Breeding_bird_atlas_code = vals[9]
80-
birds.Breeding_bird_atlas_category = vals[10]
78+
birds.CommonName = vals[4]
79+
birds.ScientificName = vals[5]
80+
birds.SubspeciesCommonName = vals[6]
81+
birds.SubspeciesScientificName = vals[7]
82+
if vals[8] == "X" {
83+
birds.ObservationCount = -1
84+
} else {
85+
birds.ObservationCount, err = strconv.Atoi(vals[8])
86+
if err != nil {
87+
return birds, err
88+
}
89+
}
90+
birds.BreedingBirdAtlasCode = vals[9]
91+
birds.BreedingBirdAtlasCategory = vals[10]
8192
birds.AgeSex = vals[11]
8293
birds.Country = vals[12]
83-
birds.Country_code = vals[13]
94+
birds.CountryCode = vals[13]
8495
birds.State = vals[14]
85-
birds.State_code = vals[15]
96+
birds.StateCode = vals[15]
8697
birds.County = vals[16]
87-
birds.County_code = vals[17]
88-
birds.Iba_code = vals[18]
89-
birds.Bcr_code = vals[19]
90-
birds.Usfws_code = vals[20]
91-
birds.Atlas_block = vals[21]
98+
birds.CountyCode = vals[17]
99+
birds.IBACode = vals[18]
100+
birds.BCRCode = vals[19]
101+
birds.USFWSCode = vals[20]
102+
birds.AtlasBlock = vals[21]
92103
birds.Locality = vals[22]
93-
birds.Locality_id = vals[23]
94-
birds.Locality_type = vals[24]
95-
birds.Latitude, err = strconv.ParseFloat(vals[25], 64)
96-
if err != nil {
97-
return birds, err
104+
birds.LocalityID = vals[23]
105+
birds.LocalityType = vals[24]
106+
if vals[25] != "" {
107+
birds.Latitude, err = strconv.ParseFloat(vals[25], 64)
108+
if err != nil {
109+
return birds, err
110+
}
98111
}
99-
/*
100-
birds.Longitude, err = strconv.ParseFloat(vals[24], 64)
112+
if vals[26] != "" {
113+
birds.Longitude, err = strconv.ParseFloat(vals[26], 64)
101114
if err != nil {
115+
fmt.Println(vals[21:27])
102116
return birds, err
103117
}
104-
birds.Observation_date = vals[25]
105-
birds.Time_observations_started = vals[26]
106-
birds.Observer_id = vals[27]
107-
birds.Sampling_event_identifier = vals[28]
108-
birds.Protocol_type = vals[29]
109-
birds.Protocol_code = vals[30]
110-
birds.Project_code = vals[31]
111-
birds.Duration_minutes, err = strconv.Atoi(vals[32])
118+
}
119+
birds.ObservationDate = vals[27]
120+
birds.TimeObservationsStarted = vals[28]
121+
birds.ObserverID = vals[29]
122+
birds.SamplingEventIdentifier = vals[30]
123+
birds.ProtocolType = vals[31]
124+
birds.ProtocolCode = vals[32]
125+
birds.ProjectCode = vals[33]
126+
if vals[34] != "" {
127+
birds.DurationMinutes, err = strconv.Atoi(vals[34])
112128
if err != nil {
113129
return birds, err
114130
}
115-
Effort_distance_km float64
116-
Effort_area_ha float64
117-
Number_observers int
118-
All_species_reported bool
119-
Group_identifier string
120-
Has_media bool
121-
Approved bool
122-
Reviewed bool
123-
Reason string
124-
Trip_comments string
125-
Species_comments string
126-
*/
127-
return birds, nil
131+
}
128132

133+
if vals[35] != "" {
134+
birds.EffortDistanceKM, err = strconv.ParseFloat(vals[35], 64)
135+
if err != nil {
136+
return birds, err
137+
}
138+
}
139+
if vals[36] != "" {
140+
birds.EffortAreaHA, err = strconv.ParseFloat(vals[36], 64)
141+
if err != nil {
142+
return birds, err
143+
}
144+
}
145+
if vals[37] != "" {
146+
birds.NumberObservers, err = strconv.Atoi(vals[37])
147+
if err != nil {
148+
return birds, err
149+
}
150+
}
151+
birds.AllSpeciesReported = vals[38] == "1"
152+
birds.GroupIdentifier = vals[39]
153+
birds.HasMedia = vals[40] == "1"
154+
birds.Approved = vals[41] == "1"
155+
birds.Reviewed = vals[42] == "1"
156+
birds.Reason = vals[43]
157+
birds.TripComments = vals[44]
158+
birds.SpeciesComments = vals[45]
159+
return birds, nil
129160
}
130161

131162
func help() {
@@ -152,20 +183,55 @@ func main() {
152183
help()
153184
os.Exit(1)
154185
}
186+
187+
var db *sql.DB
188+
db, err = sql.Open("sqlite3", "ebird.db")
189+
if err != nil {
190+
log.Fatal(err)
191+
}
192+
193+
var sql string
194+
sql = `CREATE TABLE IF NOT EXISTS test_table (
195+
first_name TEXT,
196+
last_name TEXT,
197+
age INTEGER,
198+
height FLOAT
199+
)`
200+
_, err = db.Exec(sql)
201+
if err != nil {
202+
log.Fatal(err)
203+
}
204+
205+
sql = `INSERT INTO test_table(first_name, last_name, age, height)
206+
VALUES(?,?,?,?)`
207+
stmt, err := db.Prepare(sql)
208+
if err != nil {
209+
log.Fatal(err)
210+
}
211+
_, err = stmt.Exec("kyle", "shannon", 39, 5.75)
212+
if err != nil {
213+
log.Fatal(err)
214+
}
215+
stmt.Close()
216+
155217
var scn *bufio.Scanner
156218
scn = bufio.NewScanner(fin)
157219
var hasRow bool
158220
var values []string
159221
hasRow = scn.Scan()
160-
var nextObs Obs
222+
//var nextObs Obs
161223
for hasRow == true {
162224
hasRow = scn.Scan()
163225
if hasRow == false {
164226
break
165227
}
166228
values = strings.Split(scn.Text(), "\t")
167-
_ = values
168-
229+
_, err := decodeObs(values)
230+
if err != nil {
231+
for i, v := range values {
232+
fmt.Printf("%d -> %s\n", i, v)
233+
}
234+
log.Fatal(err, values)
235+
}
169236
}
170-
171237
}

ebird/cmd/ebird_import_test.go

Lines changed: 14 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -18,48 +18,16 @@ func TestDecodeObs(t *testing.T) {
1818
var got Obs
1919
var want Obs
2020
want.GlobalUniqueIdentifier = "URN:CornellLabOfOrnithology:EBIRD:OBS67812684"
21+
want.County = "Ada"
22+
want.Latitude = 43.49179
2123
var vals []string
2224
vals = []string{
23-
"URN:CornellLabOfOrnithology:EBIRD:OBS67812684", // 0
24-
"2013-05-16 16:17:25.0",
25-
"1513",
26-
"species",
27-
"Gray Partridge",
28-
"Perdix perdix", // 5
29-
"",
30-
"",
31-
"X",
32-
"",
33-
"", // 10
34-
"",
35-
"United States",
36-
"US",
37-
"Idaho",
38-
"US-ID", // 15
39-
"Ada",
40-
"US-ID-001",
41-
"",
42-
"9",
43-
"", // 20
44-
"",
45-
"Kuna",
46-
"L191852",
47-
"T",
48-
"43.49179", // 25
49-
"-116.41996",
50-
"1969-02-01",
51-
"",
52-
"obsr180108",
53-
"S4840007", // 30
54-
"Incidental",
55-
"P20",
56-
"EBIRD",
57-
"",
58-
"",
59-
"",
60-
"",
61-
"0",
62-
"", "0", "1", "0",
25+
"URN:CornellLabOfOrnithology:EBIRD:OBS67812684", "2013-05-16 16:17:25.0",
26+
"1513", "species", "Gray Partridge", "Perdix perdix", "", "", "X", "",
27+
"", "", "United States", "US", "Idaho", "US-ID", "Ada", "US-ID-001", "",
28+
"9", "", "", "Kuna", "L191852", "T", "43.49179", "-116.41996",
29+
"1969-02-01", "", "obsr180108", "S4840007", "Incidental", "P20", "EBIRD",
30+
"", "", "", "", "0", "", "0", "1", "0", "", "", "", "",
6331
}
6432

6533
var err error
@@ -70,4 +38,10 @@ func TestDecodeObs(t *testing.T) {
7038
if got.GlobalUniqueIdentifier != want.GlobalUniqueIdentifier {
7139
t.Errorf("decoding failed, got: %s, want: %s", got.GlobalUniqueIdentifier, want.GlobalUniqueIdentifier)
7240
}
41+
if got.Latitude != want.Latitude {
42+
t.Errorf("got: %f, want: %f", got.Latitude, want.Latitude)
43+
}
44+
if got.County != want.County {
45+
t.Errorf("got: %s, want: %s", got.County, want.County)
46+
}
7347
}

0 commit comments

Comments
 (0)