/* NAME op.go DESCRIPTION op.go provides functionality for editing and reading bytes slices directly in order to insert/read timestamp and gps data in psi. AUTHOR Saxon Milton LICENSE op.go is Copyright (C) 2018 the Australian Ocean Lab (AusOcean) It is free software: you can redistribute it and/or modify them under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. It is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with revid in gpl.txt. If not, see http://www.gnu.org/licenses. */ package psi import ( "encoding/binary" "errors" ) func TimeBytes(time uint64) (s []byte) { s = make([]byte, timeSize) binary.BigEndian.PutUint64(s[:], time) return s } func chkTime(d []byte) error { if d[timeTagIndx] != timeDescTag { return errors.New("PSI does not contain a time descriptor, cannot update") } return nil } // Updatetime func UpdateTime(d []byte, t int) error { err := chkTime(d) if err != nil { return err } ts := TimeBytes(uint64(t)) for i := range d[timeIndx : timeIndx+8] { d[i+timeIndx] = ts[i] } return nil } func TimeOf(d []byte) (t uint64, err error) { err = chkTime(d) if err != nil { return 0, err } for i := range d[timeIndx : timeIndx+8] { t |= uint64(d[i+timeIndx]) << uint(56-i*8) } return t, nil } func GpsStrBytes(l string) (out []byte) { out = make([]byte, gpsSize) copy(out, []byte(l)) return }