star-ck

Describe the radiative properties of gas mixtures
git clone git://git.meso-star.fr/star-ck.git
Log | Files | Refs | README | LICENSE

commit 871b1717f95ed2248a0d0ec0d0792e97c6d94942
parent 01a131486e7d7e8cfe21ee4c146bac87522b4f29
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Sat, 14 Oct 2023 13:21:51 +0200

Store the path of the loaded file or the name of the loaded stream

Add public function sck_get_name to return its value.

Update internal load functions to avoid passing this name as an input
argument, since it is now stored directly in the sck data structure.

Diffstat:
Msrc/sck.c | 104+++++++++++++++++++++++++++++++++++++++++++------------------------------------
Msrc/sck.h | 6++++++
Msrc/sck_c.h | 2++
Msrc/test_sck_load.c | 5+++++
4 files changed, 70 insertions(+), 47 deletions(-)

diff --git a/src/sck.c b/src/sck.c @@ -62,6 +62,7 @@ reset_sck(struct sck* sck) sck->pagesize = 0; sck->nnodes = 0; darray_band_purge(&sck->bands); + str_clear(&sck->name); } static INLINE int @@ -77,19 +78,18 @@ read_quad_pt (struct sck* sck, struct quad_pt* quad_pt, FILE* stream, - const char* stream_name, size_t iband, size_t iquad_pt) { res_T res = RES_OK; - ASSERT(sck && quad_pt && stream_name); + ASSERT(sck && quad_pt); quad_pt->band = darray_band_data_get(&sck->bands) + iband; if(fread(&quad_pt->weight, sizeof(quad_pt->weight), 1, stream) != 1) { log_err(sck, "%s: band %lu: quadrature point %lu: could not read the weight.\n", - stream_name, iband, iquad_pt); + sck_get_name(sck), iband, iquad_pt); res = RES_IO_ERR; goto error; } @@ -97,7 +97,7 @@ read_quad_pt if(quad_pt->weight < 0) { log_err(sck, "%s: band %lu: quadrature point %lu: invalid weight %g.\n", - stream_name, iband, iquad_pt, quad_pt->weight); + sck_get_name(sck), iband, iquad_pt, quad_pt->weight); res = RES_BAD_ARG; goto error; @@ -115,15 +115,14 @@ static res_T read_band (struct sck* sck, struct band* band, - FILE* stream, - const char* stream_name) + FILE* stream) { double cumul; size_t iquad_pt; size_t iband; uint64_t nquad_pts; res_T res = RES_OK; - ASSERT(sck && band && stream_name); + ASSERT(sck && band); band->sck = sck; iband = (size_t)(band - darray_band_cdata_get(&sck->bands)); @@ -132,7 +131,7 @@ read_band #define READ(Var, Name) { \ if(fread((Var), sizeof(*(Var)), 1, stream) != 1) { \ log_err(sck, "%s: band %lu: could not read the %s.\n", \ - stream_name, (unsigned long)iband, (Name)); \ + sck_get_name(sck), (unsigned long)iband, (Name)); \ res = RES_IO_ERR; \ goto error; \ } \ @@ -146,14 +145,14 @@ read_band if(band->low < 0 || band->low > band->upp) { log_err(sck, "%s: band %lu: invalid band range [%g, %g].\n", - stream_name, (unsigned long)iband, band->low, band->upp); + sck_get_name(sck), (unsigned long)iband, band->low, band->upp); res = RES_BAD_ARG; goto error; } if(nquad_pts == 0) { log_err(sck, "%s: band %lu: invalid number fo quadrature points (#points=%lu).\n", - stream_name, (unsigned long)iband, (unsigned long)nquad_pts); + sck_get_name(sck), (unsigned long)iband, (unsigned long)nquad_pts); res = RES_BAD_ARG; goto error; } @@ -164,7 +163,7 @@ read_band log_err(sck, "%s: band %lu: could not allocate the list of quadrature points " "(#points=%lu).\n", - stream_name, (unsigned long)iband, (unsigned long)nquad_pts); + sck_get_name(sck), (unsigned long)iband, (unsigned long)nquad_pts); goto error; } @@ -174,7 +173,7 @@ read_band log_err(sck, "%s: band %lu: could not allocate the cumulative of quadrature points " "(#points=%lu).\n", - stream_name, (unsigned long)iband, (unsigned long)nquad_pts); + sck_get_name(sck), (unsigned long)iband, (unsigned long)nquad_pts); goto error; } @@ -184,7 +183,7 @@ read_band struct quad_pt* quad_pt = darray_quad_pt_data_get(&band->quad_pts)+iquad_pt; /* Read current quadrature point */ - res = read_quad_pt(sck, quad_pt, stream, stream_name, iband, iquad_pt); + res = read_quad_pt(sck, quad_pt, stream, iband, iquad_pt); if(res != RES_OK) goto error; /* Compute the quadrature point cumulative */ @@ -196,7 +195,7 @@ read_band if(!eq_eps(cumul, 1.0, 1.e-6)) { log_warn(sck, "%s: band %lu: the weights of the quadrature points are not normalised.\n", - stream_name, (unsigned long)iband); + sck_get_name(sck), (unsigned long)iband); /* Renormalize the cumulative */ FOR_EACH(iquad_pt, 0, nquad_pts) { @@ -216,7 +215,6 @@ error: static res_T map_data (struct sck* sck, - const char* name, const int fd, /* File descriptor */ const size_t filesz, /* Overall filesize */ const char* data_name, @@ -226,12 +224,12 @@ map_data { void* map = NULL; res_T res = RES_OK; - ASSERT(sck && name && filesz && data_name && map_len && out_map); + ASSERT(sck && filesz && data_name && map_len && out_map); ASSERT(IS_ALIGNED((size_t)offset, (size_t)sck->pagesize)); if((size_t)offset + map_len > filesz) { log_err(sck, "%s: the %s to map exceed the file size\n", - name, data_name); + sck_get_name(sck), data_name); res = RES_IO_ERR; goto error; } @@ -239,7 +237,7 @@ map_data map = mmap(NULL, map_len, PROT_READ, MAP_PRIVATE|MAP_POPULATE, fd, offset); if(map == MAP_FAILED) { log_err(sck, "%s: could not map the %s -- %s\n", - name, data_name, strerror(errno)); + sck_get_name(sck), data_name, strerror(errno)); res = RES_IO_ERR; goto error; } @@ -253,14 +251,14 @@ error: } static res_T -map_file(struct sck* sck, FILE* stream, const char* stream_name) +map_file(struct sck* sck, FILE* stream) { size_t filesz; size_t map_len; size_t iband; off_t offset; res_T res = RES_OK; - ASSERT(sck && stream && stream_name); + ASSERT(sck && stream); /* Compute the length in bytes of the k to map for each band/quadrature point */ map_len = ALIGN_SIZE(sck->nnodes * sizeof(float), sck->pagesize); @@ -281,13 +279,12 @@ map_file(struct sck* sck, FILE* stream, const char* stream_name) band->map_len = map_len; /* Mapping per band scattering coefficients */ - res = map_data(sck, stream_name, fileno(stream), filesz, - "scattering coefficients", offset, band->map_len, - (void**)&band->ks_list); + res = map_data(sck, fileno(stream), filesz, "scattering coefficients", + offset, band->map_len, (void**)&band->ks_list); if(res != RES_OK) { log_err(sck, "%s: data mapping error for band %lu\n", - stream_name, (unsigned long)iband); + sck_get_name(sck), (unsigned long)iband); goto error; } @@ -300,12 +297,12 @@ map_file(struct sck* sck, FILE* stream, const char* stream_name) quad_pt = darray_quad_pt_data_get(&band->quad_pts)+iquad_pt; quad_pt->map_len = map_len; - res = map_data(sck, stream_name, fileno(stream), filesz, "correlated Ka", + res = map_data(sck, fileno(stream), filesz, "correlated Ka", offset, quad_pt->map_len, (void**)&quad_pt->ka_list); if(res != RES_OK) { log_err(sck, "%s: data mapping error for band %lu quadrature point %lu\n", - stream_name, (unsigned long)iband, (unsigned long)iquad_pt); + sck_get_name(sck), (unsigned long)iband, (unsigned long)iquad_pt); goto error; } @@ -366,26 +363,25 @@ static res_T load_data (struct sck* sck, FILE* stream, - const char* stream_name, const char* data_name, float** out_data) { float* data = NULL; res_T res = RES_OK; - ASSERT(sck && stream && stream_name && data_name && out_data); + ASSERT(sck && stream && data_name && out_data); data = MEM_ALLOC(sck->allocator, sizeof(float)*sck->nnodes); if(!data) { res = RES_MEM_ERR; log_err(sck, "%s: could not allocate the %s -- %s\n", - stream_name, data_name, res_to_cstr(res)); + sck_get_name(sck), data_name, res_to_cstr(res)); goto error; } if(fread(data, sizeof(float), sck->nnodes, stream) != sck->nnodes) { res = RES_IO_ERR; log_err(sck, "%s: could not read the %s -- %s\n", - stream_name, data_name, res_to_cstr(res)); + sck_get_name(sck), data_name, res_to_cstr(res)); goto error; } @@ -398,7 +394,7 @@ error: } static res_T -load_file(struct sck* sck, FILE* stream, const char* stream_name) +load_file(struct sck* sck, FILE* stream) { size_t padding_bytes; size_t sizeof_header; @@ -426,12 +422,11 @@ load_file(struct sck* sck, FILE* stream, const char* stream_name) ASSERT(!band->ks_list && band->sck == sck); /* Loading per band scattering coefficients */ - res = load_data - (sck, stream, stream_name, "scattering coefficients", &band->ks_list); + res = load_data(sck, stream, "scattering coefficients", &band->ks_list); if(res != RES_OK) { log_err(sck, "%s: data loading error for band %lu\n", - stream_name, (unsigned long)iband); + sck_get_name(sck), (unsigned long)iband); goto error; } @@ -443,12 +438,11 @@ load_file(struct sck* sck, FILE* stream, const char* stream_name) quad_pt = darray_quad_pt_data_get(&band->quad_pts) + iquad_pt; /* Loading absorption coefficients per band and quadrature point */ - res = load_data - (sck, stream, stream_name, "correlated Ka", &quad_pt->ka_list); + res = load_data(sck, stream, "correlated Ka", &quad_pt->ka_list); if(res != RES_OK) { log_err(sck, "%s: data loading error for band %lu quadrature point %lu\n", - stream_name, (unsigned long)iband, (unsigned long)iquad_pt); + sck_get_name(sck), (unsigned long)iband, (unsigned long)iquad_pt); goto error; } @@ -472,17 +466,24 @@ load_stream(struct sck* sck, const struct sck_load_stream_args* args) reset_sck(sck); + res = str_set(&sck->name, args->name); + if(res != RES_OK) { + log_err(sck, "%s: unable to duplicate path to loaded data or stream name\n", + args->name); + goto error; + } + /* Read file header */ if(fread(&sck->pagesize, sizeof(sck->pagesize), 1, args->stream) != 1) { if(ferror(args->stream)) { - log_err(sck, "%s: could not read the pagesize.\n", args->name); + log_err(sck, "%s: could not read the pagesize.\n", sck_get_name(sck)); } res = RES_IO_ERR; goto error; } #define READ(Var, Name) { \ if(fread((Var), sizeof(*(Var)), 1, args->stream) != 1) { \ - log_err(sck, "%s: could not read the %s.\n", args->name, (Name)); \ + log_err(sck, "%s: could not read the %s.\n", sck_get_name(sck), (Name)); \ res = RES_IO_ERR; \ goto error; \ } \ @@ -496,20 +497,20 @@ load_stream(struct sck* sck, const struct sck_load_stream_args* args) log_err(sck, "%s: invalid page size %lu. The page size attribute must be aligned on " "the page size of the operating system (%lu).\n", - args->name, sck->pagesize, (unsigned long)sck->pagesize_os); + sck_get_name(sck), sck->pagesize, (unsigned long)sck->pagesize_os); res = RES_BAD_ARG; goto error; } if(!nbands) { log_err(sck, "%s: invalid number of bands %lu.\n", - args->name, (unsigned long)nbands); + sck_get_name(sck), (unsigned long)nbands); res = RES_BAD_ARG; goto error; } if(!sck->nnodes) { log_err(sck, "%s: invalid number of nodes %lu.\n", - args->name, (unsigned long)sck->nnodes); + sck_get_name(sck), (unsigned long)sck->nnodes); res = RES_BAD_ARG; goto error; } @@ -518,20 +519,20 @@ load_stream(struct sck* sck, const struct sck_load_stream_args* args) res = darray_band_resize(&sck->bands, nbands); if(res != RES_OK) { log_err(sck, "%s: could not allocate the list of bands (#bands=%lu).\n", - args->name, (unsigned long)nbands); + sck_get_name(sck), (unsigned long)nbands); goto error; } /* Read the band description */ FOR_EACH(iband, 0, nbands) { struct band* band = darray_band_data_get(&sck->bands) + iband; - res = read_band(sck, band, args->stream, args->name); + res = read_band(sck, band, args->stream); if(res != RES_OK) goto error; if(iband > 0 && band[0].low < band[-1].upp) { log_err(sck, "%s: bands must be sorted in ascending order and must not " "overlap (band %lu in [%g, %g[ nm; band %lu in [%g, %g[ nm).\n", - args->name, + sck_get_name(sck), (unsigned long)(iband-1), band[-1].low, band[-1].upp, (unsigned long)(iband), band[ 0].low, band[ 0].upp); res = RES_BAD_ARG; @@ -540,10 +541,10 @@ load_stream(struct sck* sck, const struct sck_load_stream_args* args) } if(args->memory_mapping) { - res = map_file(sck, args->stream, args->name); + res = map_file(sck, args->stream); if(res != RES_OK) goto error; } else { - res = load_file(sck, args->stream, args->name); + res = load_file(sck, args->stream); if(res != RES_OK) goto error; } @@ -613,6 +614,7 @@ release_sck(ref_T* ref) sck = CONTAINER_OF(ref, struct sck, ref); if(sck->logger == &sck->logger__) logger_release(&sck->logger__); darray_band_release(&sck->bands); + str_release(&sck->name); MEM_RM(sck->allocator, sck); } @@ -651,6 +653,7 @@ sck_create sck->allocator = allocator; sck->verbose = args->verbose; sck->pagesize_os = (size_t)sysconf(_SC_PAGESIZE); + str_init(allocator, &sck->name); darray_band_init(allocator, &sck->bands); if(args->logger) { sck->logger = args->logger; @@ -1003,6 +1006,13 @@ error: goto exit; } +const char* +sck_get_name(const struct sck* sck) +{ + ASSERT(sck); + return str_cget(&sck->name); +} + /******************************************************************************* * Local functions ******************************************************************************/ diff --git a/src/sck.h b/src/sck.h @@ -175,6 +175,12 @@ sck_compute_hash (const struct sck* sck, hash256_T hash); +/* Returns the path of the file or the name of the stream from which the data + * was loaded */ +SCK_API const char* +sck_get_name + (const struct sck* sck); + END_DECLS #endif /* SCK_H */ diff --git a/src/sck_c.h b/src/sck_c.h @@ -20,6 +20,7 @@ #include <rsys/dynamic_array_double.h> #include <rsys/logger.h> #include <rsys/ref_count.h> +#include <rsys/str.h> struct mem_allocator; @@ -139,6 +140,7 @@ struct sck { struct darray_band bands; size_t pagesize_os; + struct str name; /* path/stream name */ struct mem_allocator* allocator; struct logger* logger; diff --git a/src/test_sck_load.c b/src/test_sck_load.c @@ -21,6 +21,7 @@ #include <rsys/math.h> #include <rsys/mem_allocator.h> #include <math.h> +#include <string.h> static INLINE double rand_canonic(void) @@ -240,6 +241,8 @@ test_load(struct sck* sck) stream_args.stream = fp; CHK(sck_load_stream(sck, &stream_args) == RES_OK); + CHK(!strcmp(sck_get_name(sck), filename)); + CHK(sck_compute_hash(sck, NULL) == RES_BAD_ARG); CHK(sck_compute_hash(NULL, hash0) == RES_BAD_ARG); CHK(sck_compute_hash(sck, hash0) == RES_OK); @@ -266,6 +269,7 @@ test_load(struct sck* sck) stream_args.name = SCK_LOAD_STREAM_ARGS_NULL.name; stream_args.memory_mapping = 1; CHK(sck_load_stream(sck, &stream_args) == RES_OK); + CHK(!strcmp(sck_get_name(sck), SCK_LOAD_STREAM_ARGS_NULL.name)); args.path = "nop"; CHK(sck_load(NULL, &args) == RES_BAD_ARG); @@ -273,6 +277,7 @@ test_load(struct sck* sck) CHK(sck_load(sck, &args) == RES_IO_ERR); args.path = filename; CHK(sck_load(sck, &args) == RES_OK); + CHK(!strcmp(sck_get_name(sck), args.path)); check_sck_load(sck, nbands, nnodes); CHK(sck_compute_hash(sck, hash1) == RES_OK);