1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
|
.. index:: Encoding Data
Encoding Data
=============
The encoding process is structured as the reverse of the decoding process. Data in memory is described in an **Encoder** object. This data is then encoded into a sequence of frames and written to an output data stream.
.. index:: Encoding Data; Encoder
Encoder
-------
The **Encoder** provides several options for handling memory layouts, in the same way as the **Decoder**.
Row-major layout
In :ref:`row-major layout <decoder-row-major-layout>`, consecutive elements in a data row reside adjacent to each other in memory, and the block of memory comprises a sequence of rows.
.. tabs::
.. group-tab:: C
.. code-block:: c
long nrows = 1000;
int ncols = 6;
double data[nrows][ncols];
// set up the data here...
odc_encoder_t* encoder = NULL;
odc_new_encoder(&encoder);
odc_encoder_add_column(encoder, "column0", ODC_INTEGER);
odc_encoder_add_column(encoder, "column1", ODC_INTEGER);
odc_encoder_add_column(encoder, "column2", ODC_REAL);
odc_encoder_add_column(encoder, "column3", ODC_STRING);
odc_encoder_add_column(encoder, "column4", ODC_REAL);
// column3 is a 16-byte string column (hence takes 2 cols in the array --> ncols=6)
odc_encoder_column_set_data_size(encoder, 3, 16);
odc_encoder_set_data_array(encoder, data, ncols*sizeof(double), nrows, 0);
// encode the data here...
odc_free_encoder(encoder);
.. group-tab:: C++
.. note::
C++ interface does not support data encoding from a row-major layout. In this case, recommended API is C. Alternatively, you can construct a :ref:`custom memory layout <encoder-custom-layout>` encoder instead.
.. group-tab:: Fortran
.. code-block:: fortran
integer(8), parameter :: nrows = 1000
integer, parameter :: ncols = 6
real(8), target :: data(ncols, nrows)
! set up the data here...
type(odc_encoder) :: encoder
logical, parameter :: column_major = .false.
integer, target :: outunit
integer(8), target :: bytes_written
rc = encoder%initialise()
rc = encoder%add_column("column1", ODC_INTEGER)
rc = encoder%add_column("column2", ODC_INTEGER)
rc = encoder%add_column("column3", ODC_REAL)
rc = encoder%add_column("column4", ODC_STRING)
rc = encoder%add_column("column5", ODC_REAL)
! column4 is a 16-byte string column (hence takes 2 cols in the array --> ncols=6)
rc = encoder%column_set_data_size(4, 16)
rc = encoder%set_data(data, column_major)
! encode the data here...
rc = encoder%free()
Column-major layout
In :ref:`column-major layout <decoder-column-major-layout>`, consecutive elements in a single data column are adjacent to each other in memory, and the block of memory comprises a sequence of columns.
.. tabs::
.. group-tab:: C
.. code-block:: c
long nrows = 1000;
int ncols = 6;
double data[ncols][nrows];
// set up the data here...
odc_encoder_t* encoder = NULL;
odc_new_encoder(&encoder);
odc_encoder_add_column(encoder, "column0", ODC_INTEGER);
odc_encoder_add_column(encoder, "column1", ODC_INTEGER);
odc_encoder_add_column(encoder, "column2", ODC_REAL);
odc_encoder_add_column(encoder, "column3", ODC_STRING);
odc_encoder_add_column(encoder, "column4", ODC_REAL);
// column3 is a 16-byte string column (hence takes 2 cols in the array --> ncols=6)
odc_encoder_column_set_data_size(encoder, 3, 16);
odc_encoder_set_data_array(encoder, data, ncols*sizeof(double), nrows, sizeof(double));
// encode the data here...
odc_free_encoder(encoder);
.. group-tab:: C++
.. note::
C++ interface does not support data encoding from a column-major layout. In this case, recommended API is C. Alternatively, you can construct a :ref:`custom memory layout <encoder-custom-layout>` encoder instead.
.. group-tab:: Fortran
.. code-block:: fortran
integer(8), parameter :: nrows = 1000
integer, parameter :: ncols = 6
real(8), target :: data(nrows, ncols)
! set up the data here...
type(odc_encoder) :: encoder
logical, parameter :: column_major = .true.
integer, target :: outunit
integer(8), target :: bytes_written
rc = encoder%initialise()
rc = encoder%add_column("column1", ODC_INTEGER)
rc = encoder%add_column("column2", ODC_INTEGER)
rc = encoder%add_column("column3", ODC_REAL)
rc = encoder%add_column("column4", ODC_STRING)
rc = encoder%add_column("column5", ODC_REAL)
! column4 is a 16-byte string column (hence takes 2 cols in the array --> ncols=6)
rc = encoder%column_set_data_size(4, 16);
! column major is the default in Fortran, so the column_major argument can be omitted
rc = encoder%set_data(data)
! encode the data here...
rc = encoder%free()
.. _`encoder-custom-layout`:
Custom layout
For a :ref:`custom periodic layout <decoder-custom-layout>`, a periodic memory layout can be specified for each column independently to match the data layout of a specific source of data.
.. tabs::
.. group-tab:: C
.. code-block:: c
long nrows = 1000;
uint64_t data0[nrows];
uint64_t data1[nrows];
double data2[nrows];
char data3[nrows][16];
double data4[nrows];
// set up the data here...
odc_encoder_t* encoder = NULL;
odc_new_encoder(&encoder);
odc_encoder_set_row_count(encoder, nrows);
odc_encoder_add_column(encoder, "column0", ODC_INTEGER);
odc_encoder_add_column(encoder, "column1", ODC_INTEGER);
odc_encoder_add_column(encoder, "column2", ODC_REAL);
odc_encoder_add_column(encoder, "column3", ODC_STRING);
odc_encoder_add_column(encoder, "column4", ODC_REAL);
// column3 is a 16-byte string column
odc_encoder_column_set_data_size(encoder, 3, 16);
odc_encoder_column_set_data_array(encoder, 0, sizeof(uint64_t), sizeof(uint64_t), data0);
odc_encoder_column_set_data_array(encoder, 1, sizeof(uint64_t), sizeof(uint64_t), data1);
odc_encoder_column_set_data_array(encoder, 2, sizeof(double), sizeof(double), data2);
odc_encoder_column_set_data_array(encoder, 3, 16, 16, data3);
odc_encoder_column_set_data_array(encoder, 4, sizeof(double), sizeof(double), data4);
// encode the data here...
odc_free_encoder(encoder);
.. group-tab:: C++
.. code-block:: cpp
size_t nrows = 1000;
uint64_t data0[nrows];
uint64_t data1[nrows];
double data2[nrows];
char data3[nrows][16];
double data4[nrows];
// set up the data here...
std::vector<ColumnInfo> columns = {
ColumnInfo{std::string("column0"), ColumnType(INTEGER), sizeof(uint64_t)},
ColumnInfo{std::string("column1"), ColumnType(INTEGER), sizeof(uint64_t)},
ColumnInfo{std::string("column2"), ColumnType(REAL), sizeof(double)},
ColumnInfo{std::string("column3"), ColumnType(STRING), 16},
ColumnInfo{std::string("column4"), ColumnType(REAL), sizeof(double)},
};
std::vector<ConstStridedData> strides {
// ptr, nrows, element_size, stride
{data0, nrows, sizeof(uint64_t), sizeof(uint64_t)},
{data1, nrows, sizeof(uint64_t), sizeof(uint64_t)},
{data2, nrows, sizeof(double), sizeof(double)},
{data3, nrows, 16, 16},
{data4, nrows, sizeof(double), sizeof(double)},
};
// encode the data here...
.. group-tab:: Fortran
.. code-block:: fortran
use, intrinsic :: iso_c_binding
integer(8), parameter :: nrows = 1000
integer(8), target :: data1(nrows)
integer(8), target :: data2(nrows)
real(8), target :: data3(nrows)
character(16), target :: data4(nrows)
real(8), target :: data5(nrows)
! set up the data here...
type(odc_encoder) :: encoder
integer, target :: outunit
integer(8), target :: bytes_written
rc = encoder%initialise()
rc = encoder%set_row_count(nrows)
rc = encoder%add_column("column1", ODC_INTEGER)
rc = encoder%add_column("column2", ODC_INTEGER)
rc = encoder%add_column("column3", ODC_REAL)
rc = encoder%add_column("column4", ODC_STRING)
rc = encoder%add_column("column5", ODC_REAL)
! column4 is a 16-byte string column
rc = encoder%column_set_data_size(4, 16)
rc = encoder%column_set_data_array(1, 8, stride=8, data=c_loc(data1))
rc = encoder%column_set_data_array(2, 8, stride=8, data=c_loc(data2))
rc = encoder%column_set_data_array(3, 8, stride=8, data=c_loc(data3))
rc = encoder%column_set_data_array(4, 16, stride=16, data=c_loc(data4))
rc = encoder%column_set_data_array(5, 8, stride=8, data=c_loc(data5))
! encode the data here...
rc = encoder%free()
Once an **Encoder** describing the data has been constructed, the data can be encoded into frames.
.. tabs::
.. group-tab:: C
C supports data encoding in three ways.
File descriptor
Data can be encoded into an already open file descriptor using ``odc_encode_to_file_descriptor()`` function.
.. code-block:: c
#include <fcntl.h>
#include <unistd.h>
int file_descriptor = open("imaginary/path.odb", O_CREAT|O_TRUNC|O_WRONLY, 0666);
long bytes_encoded;
odc_encode_to_file_descriptor(encoder, file_descriptor, &bytes_encoded);
close(file_descriptor);
Memory buffer
Data can be encoded into a pre-allocated memory buffer using ``odc_encode_to_buffer()`` function.
.. code-block:: c
char buffer[4096];
long bytes_encoded;
odc_encode_to_buffer(encoder, buffer, sizeof(buffer), &bytes_encoded);
.. note::
In case an insufficiently large buffer is supplied, an error will be returned.
Stream handler
Data can be encoded via a stream handler using ``odc_encode_to_stream()`` function. A write callback function is called for each chunk of data to be written to the output stream in an analogue to the POSIX ``write()`` function.
.. code-block:: c
long write_fn(void* context, const void* buffer, long length) {
// user defined action
return length; // return handled length
}
// user defined context, passed unchanged to callback
void* context;
long bytes_encoded;
odc_encode_to_stream(encoder, context, write_fn, &bytes_encoded);
.. group-tab:: C++``
C++ supports data encoding into `eckit`_ ``DataHandle`` objects. There are a range of available ``DataHandle`` classes supporting a wide range of output types, and they can be extended as required to support other outputs.
``FileHandle`` (eckit)
.. code-block:: cpp
#include "eckit/io/FileHandle.h"
const Length length;
FileHandle fh("imaginary/path.odb");
fh.openForWrite(length);
AutoClose closer(fh);
encode(fh, columns, strides);
.. group-tab:: Fortran
Fortran supports data encoding to standard I/O.
.. code-block:: fortran
integer :: outunit
integer(8), target :: bytes_written
open(newunit=outunit, file="imaginary/path.odb", access="stream", form="unformatted")
rc = encoder%encode(outunit, bytes_written)
close(outunit)
.. index:: Encoding Data; Bitfields
Bitfields
---------
Bitfield columns can be used to store data for *flags*, up to a maximum of 32-bits per column. Within an integer, the bits can be identified and named by their offset. Groups of bits can be named and identified as well as individual bits, therefore each item has an offset and a size.
.. tabs::
.. group-tab:: C
.. code-block:: c
long nrows = 1000;
int ncols = 1;
uint64_t data[nrows];
// set up the data here...
odc_encoder_t* encoder = NULL;
odc_new_encoder(&encoder);
odc_encoder_set_row_count(encoder, nrows);
odc_encoder_add_column(encoder, "flags", ODC_BITFIELD);
odc_encoder_column_add_bitfield(encoder, 0, "flag_a", 1);
odc_encoder_column_add_bitfield(encoder, 0, "flag_b", 2);
odc_encoder_column_add_bitfield(encoder, 0, "flag_c", 3);
odc_encoder_column_add_bitfield(encoder, 0, "flag_d", 1);
odc_encoder_column_set_data_array(encoder, 0, sizeof(uint64_t), sizeof(uint64_t), data);
// encode the data here...
odc_free_encoder(encoder);
.. group-tab:: C++
.. code-block:: cpp
size_t nrows = 1000;
uint64_t data[nrows];
// set up the data here...
std::vector<ColumnInfo::Bit> bitfields = {
// name, size, offset+=size(n-1)
{"flag_a", 1, 0},
{"flag_b", 2, 1},
{"flag_c", 3, 3},
{"flag_d", 1, 6},
};
std::vector<ColumnInfo> columns = {
ColumnInfo{std::string("flags"), ColumnType(BITFIELD), sizeof(uint64_t), bitfields},
};
std::vector<ConstStridedData> strides {
// ptr, nrows, element_size, stride
{data, nrows, sizeof(uint64_t), sizeof(uint64_t)},
};
// encode the data here...
.. group-tab:: Fortran
.. code-block:: fortran
integer(8), parameter :: nrows = 1000
integer, parameter :: ncols = 1
integer(8), target :: data(nrows)
! set up the data here...
type(odc_encoder) :: encoder
rc = encoder%initialise()
rc = encoder%set_row_count(nrows)
rc = encoder%add_column("flags", ODC_BITFIELD)
rc = encoder%column_add_bitfield(1, "flag_a", 1)
rc = encoder%column_add_bitfield(1, "flag_b", 2)
rc = encoder%column_add_bitfield(1, "flag_c", 3)
rc = encoder%column_add_bitfield(1, "flag_d", 1)
rc = encoder%column_set_data_array(1, 8, stride=8, data=c_loc(data))
! encode the data here...
rc = encoder%free()
.. index:: Encoding Data; Properties
Properties
----------
An arbitrary dictionary of string key:value pairs can be associated with a frame.
.. tabs::
.. group-tab:: C
.. code-block:: c
const char* property_key = "encoded_by";
const char* property_value = "ECMWF";
odc_encoder_add_property(encoder, property_key, property_value);
.. group-tab:: C++
.. code-block:: cpp
std::map<std::string, std::string> properties = {
{ "encoded_by", "ECMWF" },
};
// pass properties to encode()
.. group-tab:: Fortran
.. code-block:: fortran
rc = encoder%add_property("encoded_by", "ECMWF")
.. _`eckit`: https://github.com/ecmwf/eckit
|