@@ -10,6 +10,8 @@ import (
1010 "strings"
1111)
1212
13+ var grades = []string {"A+" , "A" , "A-" , "B+" , "B" , "B-" , "C+" , "C" , "C-" , "D+" , "D" , "D-" , "F" , "W" , "P" , "CR" , "NC" , "I" }
14+
1315func loadGrades (csvDir string ) map [string ]map [string ][]int {
1416
1517 // MAP[SEMESTER] -> MAP[SUBJECT + NUMBER + SECTION] -> GRADE DISTRIBUTION
@@ -73,68 +75,48 @@ func csvToMap(csvFile *os.File, logFile *os.File) map[string][]int {
7375 if err != nil {
7476 log .Panicf ("Error parsing %s: %s" , csvFile .Name (), err .Error ())
7577 }
76- // look for the subject column and w column
77- subjectCol := - 1
78- catalogNumberCol := - 1
79- sectionCol := - 1
80- wCol := - 1
81- aPlusCol := - 1
82-
83- headerRow := records [0 ]
84-
85- for j := 0 ; j < len (headerRow ); j ++ {
86- switch {
87- case headerRow [j ] == "Subject" :
88- subjectCol = j
89- case headerRow [j ] == "Catalog Number" || headerRow [j ] == "Catalog Nbr" :
90- catalogNumberCol = j
91- case headerRow [j ] == "Section" :
92- sectionCol = j
93- case headerRow [j ] == "W" || headerRow [j ] == "Total W" || headerRow [j ] == "W Total" :
94- wCol = j
95- case headerRow [j ] == "A+" :
96- aPlusCol = j
97- }
98- if wCol == - 1 || subjectCol == - 1 || catalogNumberCol == - 1 || sectionCol == - 1 || aPlusCol == - 1 {
99- continue
100- } else {
101- break
78+
79+ indexMap := make (map [string ]int )
80+
81+ for j , col := range records [0 ] {
82+ switch col {
83+ case "Catalog Number" , "Catalog Nbr" :
84+ indexMap ["Catalog Number" ] = j
85+ case "W" , "Total W" , "W Total" :
86+ indexMap ["W" ] = j
87+ default :
88+ indexMap [col ] = j
10289 }
10390 }
10491
105- if wCol == - 1 {
106- logFile .WriteString ("could not find W column" )
107- //log.Panicf("could not find W column")
108- }
109- if sectionCol == - 1 {
110- logFile .WriteString ("could not find Section column" )
111- log .Panicf ("could not find Section column" )
112- }
113- if subjectCol == - 1 {
114- logFile .WriteString ("could not find Subject column" )
115- log .Panicf ("could not find Subject column" )
116- }
117- if catalogNumberCol == - 1 {
118- logFile .WriteString ("could not find catalog # column" )
119- log .Panicf ("could not find catalog # column" )
92+ // required columns
93+ for _ , name := range []string {"Section" , "Subject" , "Catalog Number" , "A+" } {
94+ if _ , ok := indexMap [name ]; ! ok {
95+ logFile .WriteString (fmt .Sprintf ("could not find %s column" , name ))
96+ log .Panicf ("could not find %s column" , name )
97+ }
12098 }
121- if aPlusCol == - 1 {
122- logFile .WriteString ("could not find A+ column" )
123- log .Panicf ("could not find A+ column" )
99+
100+ // optional columns
101+ for _ , name := range []string {"W" , "P" , "CR" , "NC" , "I" } {
102+ if _ , ok := indexMap [name ]; ! ok {
103+ logFile .WriteString (fmt .Sprintf ("could not find %s column\n " , name ))
104+ }
124105 }
125106
107+ sectionCol := indexMap ["Section" ]
108+ subjectCol := indexMap ["Subject" ]
109+ catalogNumberCol := indexMap ["Catalog Number" ]
110+
126111 distroMap := make (map [string ][]int )
127112
128- for _ , record := range records {
113+ for _ , record := range records [ 1 :] {
129114 // convert grade distribution from string to int
130- intSlice := [14 ]int {}
131-
132- for j := 0 ; j < 13 ; j ++ {
133- intSlice [j ], _ = strconv .Atoi (record [aPlusCol + j ])
134- }
135- // add w number to the grade_distribution slice
136- if wCol != - 1 {
137- intSlice [13 ], _ = strconv .Atoi (record [wCol ])
115+ intSlice := make ([]int , len (grades ))
116+ for i , col := range grades {
117+ if pos , ok := indexMap [col ]; ok {
118+ intSlice [i ], _ = strconv .Atoi (record [pos ])
119+ }
138120 }
139121
140122 // add new grade distribution to map, keyed by SUBJECT + NUMBER + SECTION
0 commit comments