Navigation Menu

Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Stanislav Humplik committed May 9, 2015
0 parents commit f985c01
Show file tree
Hide file tree
Showing 10 changed files with 5,620 additions and 0 deletions.
52 changes: 52 additions & 0 deletions .gitignore
@@ -0,0 +1,52 @@
gfs.t00z.pgrb2.0p25.f000

# Created by .ignore support plugin (hsz.mobi)
### JetBrains template
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm

*.iml

## Directory-based project format:
.idea/
# if you remove the above rule, at least ignore the following:

# User-specific stuff:
# .idea/workspace.xml
# .idea/tasks.xml
# .idea/dictionaries

# Sensitive or high-churn files:
# .idea/dataSources.ids
# .idea/dataSources.xml
# .idea/sqlDataSources.xml
# .idea/dynamic.xml
# .idea/uiDesigner.xml

# Gradle:
# .idea/gradle.xml
# .idea/libraries

# Mongo Explorer plugin:
# .idea/mongoSettings.xml

## File-based project format:
*.ipr
*.iws

## Plugin-specific files:

# IntelliJ
/out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties


24 changes: 24 additions & 0 deletions LICENSE
@@ -0,0 +1,24 @@
Copyright (c) 2015, Analogic s.r.o. - Stanislav Humplík
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the Analogic s.r.o. nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY COPYRIGHT HOLDERS ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15 changes: 15 additions & 0 deletions README.md
@@ -0,0 +1,15 @@
GRIB2 Golang experimental parser
================================

Unfinished dirty parser for meteo data

### What works?

- basic binary parsing of GRIB2 GFS files from NOAA
- implemented only "Grid point data - complex packing and spatial differencing"

### TODO?

- implement and test output values
- refactor
- add some kind of tool for exporting values for world/place to json
224 changes: 224 additions & 0 deletions data/bitreader.go
@@ -0,0 +1,224 @@
package data

import (
//"fmt"
"bufio"
"io"
)

type BitReader struct {
reader io.ByteReader
byte byte
offset byte
}

func NewReader(r io.ByteReader) *BitReader {
return &BitReader{r, 0, 0}
}

func (r *BitReader) ReadBit() (bool, error) {
if r.offset == 8 {
r.offset = 0
}
if r.offset == 0 {
var err error
if r.byte, err = r.reader.ReadByte(); err != nil {
return false, err
}
}
bit := (r.byte & (0x80 >> r.offset)) != 0
r.offset++
return bit, nil
}

func (r *BitReader) ReadUint(nbits int) (uint64, error) {
var result uint64
for i := nbits - 1; i >= 0; i-- {
bit, err := r.ReadBit()

if err != nil {
return 0, err
}
if bit {
result |= 1 << uint(i)
}
}

return result, nil
}

func (r *BitReader) ReadInt(nbits int) (int64, error) {
var result int64
var negative int64 = 1
for i := nbits - 1; i >= 0; i-- {
bit, err := r.ReadBit()

if err != nil {
return 0, err
}
if i == (nbits - 1) && bit {
negative = -1
} else if bit {
result |= 1 << uint(i)
}
}
return negative * result, nil
}

func (r *BitReader) readUintsBlock(bits int, count int, compensateByte bool) ([]uint64, error) {
//fmt.Println("Reading", bits, "bits", count, "x")
data := make([]uint64, count)
var err error

if bits != 0 {
for i := 0; i != count; i++ {
data[i], err = r.ReadUint(bits)
if err != nil {
return data, err
}

//fmt.Println(data[i])
}

if compensateByte {
// if we are not fitting last byte seek to byte end
//rest := (bits * count) % 8
//if rest != 0 {
// r.offset += byte(8 - int64(rest))
//}
r.offset = 0
}
}
return data, nil
}

func (r *BitReader) readIntsBlock(bits int, count int, compensateByte bool) ([]int64, error) {
//fmt.Println("Reading", bits, "bits", count, "x")
data := make([]int64, count)
var err error

if bits != 0 {
for i := 0; i != count; i++ {
data[i], err = r.ReadInt(bits)
if err != nil {
return data, err
}
//fmt.Println(data[i])
}

if compensateByte {
// if we are not fitting last byte seek to byte end
//rest := (bits * count) % 8
//if rest != 0 {
// r.offset += byte(8 - int64(rest))
//}
r.offset = 0
}
}
return data, nil
}


////////////////////////////////////////////////////////////////////////////////////

// bitReader wraps an io.Reader and provides the ability to read values,
// bit-by-bit, from it. Its Read* methods don't return the usual error
// because the error handling was verbose. Instead, any error is kept and can
// be checked afterwards.
type bitReader struct {
r io.ByteReader
n uint64
bits uint
err error
}

// newBitReader returns a new bitReader reading from r. If r is not
// already an io.ByteReader, it will be converted via a bufio.Reader.
func newBitReader(r io.Reader) bitReader {
byter, ok := r.(io.ByteReader)
if !ok {
byter = bufio.NewReader(r)
}
return bitReader{r: byter}
}

// ReadBits64 reads the given number of bits and returns them in the
// least-significant part of a uint64. In the event of an error, it returns 0
// and the error can be obtained by calling Err().
func (br *bitReader) ReadBits64(bits uint) (n uint64) {
for bits > br.bits {
b, err := br.r.ReadByte()
if err == io.EOF {
err = io.ErrUnexpectedEOF
}
if err != nil {
br.err = err
return 0
}
br.n <<= 8
br.n |= uint64(b)
br.bits += 8
}

// br.n looks like this (assuming that br.bits = 14 and bits = 6):
// Bit: 111111
// 5432109876543210
//
// (6 bits, the desired output)
// |-----|
// V V
// 0101101101001110
// ^ ^
// |------------|
// br.bits (num valid bits)
//
// This the next line right shifts the desired bits into the
// least-significant places and masks off anything above.
n = (br.n >> (br.bits - bits)) & ((1 << bits) - 1)
br.bits -= bits
return
}

func (r *bitReader) readIntsBlock(bits int, count int, compensateByte bool) ([]int64, error) {
//fmt.Println("Reading", bits, "bits", count, "x")
data := make([]int64, count)

if bits != 0 {
for i := 0; i != count; i++ {
data[i] = int64(r.ReadBits64(uint(bits)))
//fmt.Println(data[i])
}

if compensateByte {
// if we are not fitting last byte seek to byte end
//rest := (bits * count) % 8
//if rest != 0 {
// r.offset += byte(8 - int64(rest))
//}
r.n = 0
}
}
return data, nil
}

func (br *bitReader) ReadBits(bits uint) (n int) {
n64 := br.ReadBits64(bits)
return int(n64)
}

func (br *bitReader) ReadBit() bool {
n := br.ReadBits(1)
return n != 0
}

func (br *bitReader) TryReadBit() (bit byte, ok bool) {
if br.bits > 0 {
br.bits--
return byte(br.n>>br.bits) & 1, true
}
return 0, false
}

func (br *bitReader) Err() error {
return br.err
}

0 comments on commit f985c01

Please sign in to comment.