go-hep.org/x/hep@v0.38.1/hbook/ntup/ntcsv/ntcsv.go (about)

     1  // Copyright ©2017 The go-hep Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Package ntcsv provides a convenient access to CSV files as n-tuple data.
     6  //
     7  // Examples:
     8  //
     9  //	nt, err := ntcsv.Open("testdata/simple.csv")
    10  //	if err != nil {
    11  //	    log.Fatal(err)
    12  //	}
    13  //	defer nt.DB().Close()
    14  //
    15  // or, with a different configuration for the comma/comment runes:
    16  //
    17  //	nt, err := ntcsv.Open("testdata/simple.csv", ntcsv.Comma(' '), ntcsv.Comment('#'))
    18  //	if err != nil {
    19  //	    log.Fatal(err)
    20  //	}
    21  //	defer nt.DB().Close()
    22  //
    23  // Give our own names to the CSV columns (default: "var1", "var2", ...):
    24  //
    25  //	nt, err := ntcsv.Open("testdata/simple.csv", ntcsv.Columns("var1", "i64", "foo"))
    26  //
    27  // Take the names from the CSV header (note that the header *must* exist):
    28  //
    29  //	nt, err := ntcsv.Open("testdata/simple-with-header.csv", ntcsv.Header())
    30  //
    31  // Override the names from the CSV header with our own:
    32  //
    33  //	nt, err := ntcsv.Open("testdata/simple-with-header.csv", ntcsv.Header(), ntcsv.Columns("v1", "v2", "v3")
    34  package ntcsv // import "go-hep.org/x/hep/hbook/ntup/ntcsv"
    35  
    36  import (
    37  	"fmt"
    38  
    39  	"go-hep.org/x/hep/csvutil/csvdriver"
    40  	"go-hep.org/x/hep/hbook/ntup"
    41  )
    42  
    43  // Open opens a CSV file in read-only mode and returns a n-tuple
    44  // connected to that.
    45  func Open(name string, opts ...Option) (*ntup.Ntuple, error) {
    46  	c := csvdriver.Conn{File: name}
    47  	for _, opt := range opts {
    48  		opt(&c)
    49  	}
    50  
    51  	db, err := c.Open()
    52  	if err != nil {
    53  		return nil, err
    54  	}
    55  
    56  	nt, err := ntup.Open(db, "csv")
    57  	if err != nil {
    58  		db.Close()
    59  		return nil, fmt.Errorf("could not open n-tuple: %w", err)
    60  	}
    61  
    62  	return nt, nil
    63  }
    64  
    65  // Option configures the underlying sql.DB connection to the n-tuple.
    66  type Option func(c *csvdriver.Conn)
    67  
    68  // Comma configures the n-tuple to use v as the comma delimiter between columns.
    69  func Comma(v rune) Option {
    70  	return func(c *csvdriver.Conn) {
    71  		c.Comma = v
    72  	}
    73  }
    74  
    75  // Comment configures the n-tuple to use v as the comment character
    76  // for start of line.
    77  func Comment(v rune) Option {
    78  	return func(c *csvdriver.Conn) {
    79  		c.Comment = v
    80  	}
    81  }
    82  
    83  // Header informs the n-tuple the CSV file has a header line.
    84  func Header() Option {
    85  	return func(c *csvdriver.Conn) {
    86  		c.Header = true
    87  	}
    88  }
    89  
    90  // Columns names the n-tuple columns with the given slice.
    91  func Columns(names ...string) Option {
    92  	return func(c *csvdriver.Conn) {
    93  		if len(names) == 0 {
    94  			return
    95  		}
    96  		c.Names = make([]string, len(names))
    97  		copy(c.Names, names)
    98  	}
    99  }