forked from kshedden/datareader
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Extract the doConversion helper from stattocsv/main.go over to convert.go so that it can be more easily be reused
- Loading branch information
1 parent
8973783
commit 9ef9588
Showing
2 changed files
with
87 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package datareader | ||
|
||
import ( | ||
"encoding/csv" | ||
"fmt" | ||
"io" | ||
"os" | ||
"time" | ||
) | ||
|
||
func DoConversion(rdr StatfileReader) { | ||
|
||
w := csv.NewWriter(os.Stdout) | ||
|
||
ncol := len(rdr.ColumnNames()) | ||
if err := w.Write(rdr.ColumnNames()); err != nil { | ||
panic(err) | ||
} | ||
|
||
row := make([]string, ncol) | ||
|
||
for { | ||
chunk, err := rdr.Read(1000) | ||
if err != nil && err != io.EOF { | ||
panic(err) | ||
} else if chunk == nil || err == io.EOF { | ||
break | ||
} | ||
|
||
for j := 0; j < len(chunk); j++ { | ||
chunk[j] = chunk[j].UpcastNumeric() | ||
} | ||
|
||
nrow := chunk[0].Length() | ||
|
||
numbercols := make([][]float64, ncol) | ||
stringcols := make([][]string, ncol) | ||
timecols := make([][]time.Time, ncol) | ||
|
||
missing := make([][]bool, ncol) | ||
|
||
for j := 0; j < ncol; j++ { | ||
missing[j] = chunk[j].Missing() | ||
dcol := chunk[j].Data() | ||
switch v := dcol.(type) { | ||
case []time.Time: | ||
timecols[j] = v | ||
case []float64: | ||
numbercols[j] = v | ||
case []string: | ||
stringcols[j] = v | ||
default: | ||
panic(fmt.Sprintf("unknown type: %T", dcol)) | ||
} | ||
} | ||
|
||
for i := range nrow { | ||
for j := range ncol { | ||
if numbercols[j] != nil { | ||
if missing[j] == nil || !missing[j][i] { | ||
row[j] = fmt.Sprintf("%f", numbercols[j][i]) | ||
} else { | ||
row[j] = "" | ||
} | ||
} else if stringcols[j] != nil { | ||
if missing[j] == nil || !missing[j][i] { | ||
row[j] = stringcols[j][i] | ||
} else { | ||
row[j] = "" | ||
} | ||
} else if timecols[j] != nil { | ||
if missing[j] == nil || !missing[j][i] { | ||
row[j] = fmt.Sprintf("%v", timecols[j][i]) | ||
} else { | ||
row[j] = "" | ||
} | ||
} | ||
} | ||
if err := w.Write(row); err != nil { | ||
panic(err) | ||
} | ||
} | ||
} | ||
|
||
w.Flush() | ||
} |