libsdfio 1.0.0
Reader and writer for ISO 25178-71 SDF surface data files in C
Loading...
Searching...
No Matches
read_heights.c
/* Read an SDF file with the heights API. */
#include <sdfio/file.h>
#include <sdfio/sdfio.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
if (argc < 2)
{
fprintf(stderr, "usage: %s input.sdf\n", argv[0]);
return EXIT_FAILURE;
}
sdfio_reader *reader = NULL;
sdfio_status status = sdfio_open_file(reader, argv[1]);
if (status != SDFIO_OK)
{
fprintf(stderr, "%s: %s\n", sdfio_status_string(status),
return EXIT_FAILURE;
}
const sdfio_header *header = sdfio_reader_header(reader);
size_t heights_size = 0;
float *heights =
sdfio_heights_size(header, SDFIO_TYPE_FLOAT, &heights_size) == SDFIO_OK
? malloc(heights_size)
: NULL;
if (heights == NULL)
{
fprintf(stderr, "the heights do not fit in memory\n");
return EXIT_FAILURE;
}
char trailer[1024];
size_t trailer_size = 0;
heights);
if (status == SDFIO_OK)
{
status = sdfio_read_trailer(reader, trailer, sizeof trailer - 1,
&trailer_size);
}
if (status != SDFIO_OK)
{
fprintf(stderr, "%s: %s\n", sdfio_status_string(status),
free(heights);
return EXIT_FAILURE;
}
double sum = 0.0;
size_t measured = 0;
const size_t count =
(size_t)sdfio_header_rows(header) * sdfio_header_columns(header);
for (size_t i = 0; i < count; ++i)
{
if (!isnan(heights[i]))
{
sum += heights[i];
++measured;
}
}
printf("%s %lu x %lu, mean %g m\ntrailer:\n%.*s",
sdfio_header_revision(header) == SDFIO_ISO_1_0 ? "ISO-1.0"
: "ISO-2.0",
(unsigned long)sdfio_header_columns(header),
(unsigned long)sdfio_header_rows(header),
measured > 0 ? sum / (double)measured : 0.0, (int)trailer_size,
trailer);
size_t position = 0;
size_t name_at = 0;
size_t value_at = 0;
size_t name_size = 0;
size_t value_size = 0;
while (sdfio_trailer_next_field(trailer, trailer_size, &position, &name_at,
&name_size, &value_at, &value_size))
{
printf("field %.*s: %.*s\n", (int)name_size, trailer + name_at,
(int)value_size, trailer + value_at);
}
if (sdfio_trailer_find_field(trailer, trailer_size, "operatorname",
&value_at, &value_size))
{
printf("operator: %.*s\n", (int)value_size, trailer + value_at);
}
free(heights);
return EXIT_SUCCESS;
}
Files by path, and the stream over a C file.
sdfio_status sdfio_open_file(sdfio_reader *reader, const char *path)
Opens the file at a path as the source and reads its header, as sdfio_open_stream() does for a stream...
struct sdfio_header sdfio_header
Header of a file, with the field names of the standard.
Definition header.h:42
sdfio_revision sdfio_header_revision(const sdfio_header *header)
Returns the revision of the format.
uint32_t sdfio_header_rows(const sdfio_header *header)
Returns the number of rows N of the data matrix: 1 for a profile.
uint32_t sdfio_header_columns(const sdfio_header *header)
Returns the number of columns M of the data matrix: the points along x.
const sdfio_layout * sdfio_layout_packed(sdfio_data_type type)
Returns the layout of a plain array: the elements follow each other and the rows follow each other.
sdfio_status sdfio_reader_add_binary(sdfio_reader *reader)
Adds the binary format to the formats that the reader reads, the version numbers bISO-1....
const sdfio_header * sdfio_reader_header(const sdfio_reader *reader)
Returns the header of the opened file, or null if no file is open.
struct sdfio_reader sdfio_reader
Opaque handle of a reader.
Definition reader.h:58
sdfio_status sdfio_reader_add_ascii(sdfio_reader *reader)
Adds the ASCII format to the formats that the reader reads, the version numbers aISO-1....
sdfio_status sdfio_reader_create(const sdfio_allocator *allocator, sdfio_reader **reader)
Creates a reader.
sdfio_status sdfio_read_trailer(sdfio_reader *reader, char *buffer, size_t capacity, size_t *size)
Reads the next part of the trailer, the free text after the data area, into a buffer of the caller.
sdfio_status sdfio_read_heights(sdfio_reader *reader, const sdfio_layout *layout, void *dst)
Reads the remaining rows as heights in metres into a buffer that lies in memory as the layout describ...
const char * sdfio_reader_error_message(const sdfio_reader *reader)
Returns a description of the most recent failure of the reader, or an empty string if none occurred.
void sdfio_reader_free(sdfio_reader *reader)
Releases a reader and closes the file that sdfio_open_file() opened, if any.
Reading and writing of ISO 25178-71 SDF surface data files.
sdfio_status sdfio_heights_size(const sdfio_header *header, sdfio_data_type type, size_t *bytes)
Returns the size in bytes of a buffer for the whole matrix of heights in a type, for sdfio_read_heigh...
bool sdfio_trailer_find_field(const char *text, size_t size, const char *name, size_t *value_at, size_t *value_size)
Finds the value of the field of a name in a trailer text.
bool sdfio_trailer_next_field(const char *text, size_t size, size_t *position, size_t *name_at, size_t *name_size, size_t *value_at, size_t *value_size)
Finds the next field of a trailer text, a line of the form Name = Value, which ISO-2....
@ SDFIO_ISO_1_0
Revision ISO-1.0.
Definition types.h:74
const char * sdfio_status_string(sdfio_status status)
Returns a constant description of a status.
sdfio_status
Result of a function call.
Definition types.h:31
@ SDFIO_OK
The call succeeded.
Definition types.h:33
const sdfio_allocator * sdfio_default_allocator(void)
Returns the allocator that uses malloc() and free().
@ SDFIO_TYPE_FLOAT
float: IEEE 754 binary32.
Definition types.h:114