Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added unpack complex logic #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions internal/CodeTable.c.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,36 @@ func code_table_5_0(sec [][]unsigned_char) int {
func code_table_6_0(sec [][]unsigned_char) int {
return int(sec[6][5])
}

func code_table_5_6(sec [][]unsigned_char) int {
if code_table_5_0(sec) != 3 {
return -1
}

return int(sec[5][47])
}

func code_table_5_4(sec [][]unsigned_char) int {
if code_table_5_0(sec) < 2 || code_table_5_0(sec) > 3 {
return -1
}

return int(sec[5][21])
}

func code_table_5_5(sec [][]unsigned_char) int {
if code_table_5_0(sec) < 2 || code_table_5_0(sec) > 3 {
return -1
}

return int(sec[5][22])
}

func code_table_5_1(sec [][]unsigned_char) int {
switch code_table_5_0(sec) {
case 0, 1, 2, 3, 40, 41, 42:
return (int)(sec[5][20])
default:
return -1
}
}
40 changes: 40 additions & 0 deletions internal/Code_Values.c.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,43 @@ func fixed_surfaces(sec [][]unsigned_char, type1 *int, surface1 *float, undef_va
}
return nil
}

func sub_missing_values(sec [][]unsigned_char, missing1, missing2 *float) int {

i := code_table_5_5(sec)
if i < 1 || i > 2 {
return 0
}
j := code_table_5_1(sec)
p := sec[5]
if j == 0 { // ieee
if p[23] == 255 && p[24] == 255 && p[25] == 255 && p[26] == 255 {
*missing1 = UNDEFINED
} else {
*missing1 = ieee2flt(p[23:])
}
if i == 2 {
if p[27] == 255 && p[28] == 255 && p[29] == 255 && p[30] == 255 {
*missing1 = UNDEFINED
} else {
*missing2 = ieee2flt(p[27:])
}
}
} else if j == 1 { // integer
if p[23] == 255 && p[24] == 255 && p[25] == 255 && p[26] == 255 {
*missing1 = UNDEFINED
} else {
*missing1 = 0
int4(p[23:])
}
if i == 2 {
if p[27] == 255 && p[28] == 255 && p[29] == 255 && p[30] == 255 {
*missing1 = UNDEFINED
} else {
*missing2 = 0
int4(p[27:])
}
}
}
return i
}
54 changes: 54 additions & 0 deletions internal/bitstream.c.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,57 @@ func rd_bitstream_flt(p []unsigned_char, offset int, u []float, n_bits int, n in
}
return nil
}

func rd_bitstream(p []unsigned_char, offset int, u []int, n_bits int, n int) error {

var tbits unsigned_int
var i, t_bits, new_t_bits int

// not the best of tests

if INT_MAX <= 2147483647 && n_bits > 31 {
return fatal_error_i("rd_bitstream: n_bits is %d", n_bits)
}

if offset < 0 || offset > 7 {
return fatal_error_i("rd_bitstream_flt: illegal offset %d", offset)
}

if n_bits == 0 {
for i = 0; i < n; i++ {
u[i] = 0.0
}
return nil
}

t_bits = 8 - offset
p_index := 0
tbits = unsigned_int(p[p_index]) & unsigned_int(ones[t_bits])
p_index++

for i = 0; i < n; i++ {

for n_bits-t_bits >= 8 {
t_bits += 8
tbits = (tbits << 8) | unsigned_int(p[p_index])
p_index++
}

if n_bits > t_bits {
new_t_bits = 8 - (n_bits - t_bits)
u[i] = int(int(tbits<<unsigned_int(n_bits-t_bits) | (unsigned_int(p[p_index]) >> unsigned_int(new_t_bits))))
t_bits = new_t_bits
tbits = unsigned_int(p[p_index]) & unsigned_int(ones[t_bits])
p_index++
} else if n_bits == t_bits {
u[i] = int(tbits)
tbits = 0
t_bits = 0
} else {
t_bits -= n_bits
u[i] = int(tbits >> unsigned_int(t_bits))
tbits = tbits & unsigned_int(ones[t_bits])
}
}
return nil
}
6 changes: 3 additions & 3 deletions internal/geo.c.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ func polar2ll(sec [][]unsigned_char, llat *[]double, llon *[]double) error {
}
*/
*llat = make([]double, nnpnts, nnpnts)
*llat = make([]double, nnpnts, nnpnts)
*llon = make([]double, nnpnts, nnpnts)

lat = *llat
lon = *llon
Expand Down Expand Up @@ -561,7 +561,7 @@ func lambert2ll(sec [][]unsigned_char, llat *[]double, llon *[]double) error {
return fatal_error("bad GDS, lon1r < 0.0", "")
}

if fabs(latin1r-latin2r) < 1E-09 {
if fabs(latin1r-latin2r) < 1e-09 {
n = sin(latin1r)
} else {
n = log(cos(latin1r)/cos(latin2r)) /
Expand Down Expand Up @@ -597,7 +597,7 @@ func lambert2ll(sec [][]unsigned_char, llat *[]double, llon *[]double) error {
}
*/
*llat = make([]double, nnpnts, nnpnts)
*llat = make([]double, nnpnts, nnpnts)
*llon = make([]double, nnpnts, nnpnts)

lat = *llat
lon = *llon
Expand Down
7 changes: 7 additions & 0 deletions internal/grib2.c.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ func GB2_Sec3_npts(sec [][]unsigned_char) unsigned_int {
return uint4(sec[3][6:])
}

func GB2_Sec5_nval(sec [][]unsigned_char) unsigned_int {
if sec[5] != nil {
return uint4(sec[5][5:])
}
return 0
}

// #define GDS_Scan_y(scan) ((scan & 64) == 64)
func GDS_Scan_y(scan int) bool {
return (scan & 64) == 64
Expand Down
33 changes: 33 additions & 0 deletions internal/int8.c.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,39 @@ func uint2(p []unsigned_char) unsigned_int {
return (unsigned_int(p[0]) << 8) + unsigned_int(p[1])
}

func int_n(p []unsigned_char, n int) int {
if n == 0 {
return 0
}

j := 0
sign := p[0]
pIndex := 0

for i := n; i > 1; i-- {
j = (j * 256) + int(p[pIndex])
pIndex++
}

if (sign & 0x80) != 0 {
j = -j
}

return j

}

func uint_n(p []unsigned_char, n int) unsigned_int {
j := unsigned_int(0)
pIndex := 0
for i := n; i > 0; i-- {
j = (j << 8) + unsigned_int(p[pIndex])
pIndex++
}
return j

}

//
// floating point values are often represented as int * power of 10
//
Expand Down
4 changes: 1 addition & 3 deletions internal/unpk.c.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,7 @@ func unpk_grib(sec [][]unsigned_char, data []float) error {
}
return nil
} else if packing == 2 || packing == 3 { // complex
return fatal_error("unpk_complex is not supported")
// TODO: unpk_complex
// return unpk_complex(sec, data, ndata)
return unpk_complex(sec, data, ndata)
} else if packing == 200 { // run length
return fatal_error("unpk_run_length is not supported")
// TODO: unpk_run_length
Expand Down
Loading