英文:
ECCODES C API : updating keys offset, count and countTotal
问题
我正在使用codes_grib_multi_handle_write()
函数通过迭代h = codes_grib_handle_new_from_samples(NULL,"GRIB2")
来创建多消息grib。每个句柄都使用函数codes_grib_multi_handle_append(h,startSection,mh)
添加到多句柄mh
中。
似乎获得的多重grib消息的"offset"键仍然为0,而"count"键仍然为1。只有"countTotal"键递增。是否有一种方法可以根据存在的消息数量来更新这些键?
我尝试过:
// 设置偏移
size_t totalLength = 0, size2 = 0;
const void* buffer = NULL;
CODES_CHECK(codes_get_message_size(h, &totalLength), 0);
buffer = (unsigned char*)malloc(totalLength * sizeof(char));
CODES_CHECK(codes_get_message(h, &buffer, &size2), 0);
fprintf(stderr, "循环中的大小:%ld\n", size2);
//CODES_CHECK(codes_set_long(h, "offset", offset),0); //=> 只读错误 !!
//CODES_CHECK(codes_set_long(h, "count", count),0); //=> 只读错误 !!
offset += size2;
count += 1;
英文:
I am using the codes_grib_multi_handle_write()
function to create a multi message grib by iterating over a h = codes_grib_handle_new_from_samples(NULL,"GRIB2")
. Each handle is added to the multi handle mh
with the function codes_grib_multi_handle_append(h, startSection, mh)
It seems that the "offset" key of the messages of the multigrib obtained remains at 0 and that the "count" key remains at 1. Only the "countTotal" key is incremented. Is there a way to see these keys update based on the number of messages present?
I tried :
// set offset
size_t totalLength = 0, size2 = 0;
const void* buffer = NULL;
CODES_CHECK(codes_get_message_size(h,&totalLength),0);
buffer=(unsigned char*)malloc(totalLength*sizeof(char));
CODES_CHECK(codes_get_message(h, &buffer, &size2),0);
fprintf(stderr,"size in loop : %ld\n", size2);
//CODES_CHECK(codes_set_long(h, "offset", offset),0); //=> pb en read only !!
//CODES_CHECK(codes_set_long(h, "count", count),0); //=> pb en read only !!
offset += size2;
count += 1;
答案1
得分: 0
codes_grib_multi_handle_write()
和 codes_grib_multi_handle_append()
函数不适用于在单个文件中写入多个消息。它们用于写入多字段的GRIB数据。因此,需要使用函数 codes_get_message
和 fwrite
。以下是由ECMWF团队友好提供的快速示例,感谢他们:
#include <stdio.h>
#include "eccodes.h"
int main(int argc, char* argv[])
{
FILE* in = NULL;
FILE* out = NULL;
codes_handle* h = NULL;
const void* buffer = NULL;
size_t size = 0;
int err = 0;
if (argc != 3) return 1;
codes_grib_multi_support_on(NULL); /* NOTA BENE */
in = fopen(argv[1], "rb");
out = fopen(argv[2], "wb");
/* loop over the messages in the source grib and copy them */
while ((h = codes_handle_new_from_file(0, in, PRODUCT_GRIB, &err)) != NULL) {
CODES_CHECK(codes_get_message(h, &buffer, &size), 0);
if (fwrite(buffer, 1, size, out) != size) {
perror(argv[1]);
return 1;
}
codes_handle_delete(h);
}
fclose(out);
fclose(in);
return 0;
}
英文:
The codes_grib_multi_handle_write()
and codes_grib_multi_handle_append()
functions are not suitable for writing multiple messages in a single file. It is used to write a multi-field grib. This is why it is necessary to use the functions codes_get_message
and fwrite
. Here is a quick example kindly given by the ECMWF team, thanks to them:
#include <stdio.h>
#include "eccodes.h"
int main(int argc, char* argv[])
{
FILE* in = NULL;
FILE* out = NULL;
codes_handle* h = NULL;
const void* buffer = NULL;
size_t size = 0;
int err = 0;
if (argc != 3) return 1;
codes_grib_multi_support_on(NULL); /* NOTA BENE */
in = fopen(argv[1], "rb");
out = fopen(argv[2], "wb");
/* loop over the messages in the source grib and copy them */
while ((h = codes_handle_new_from_file(0, in, PRODUCT_GRIB, &err)) != NULL) {
CODES_CHECK(codes_get_message(h, &buffer, &size), 0);
if (fwrite(buffer, 1, size, out) != size) {
perror(argv[1]);
return 1;
}
codes_handle_delete(h);
}
fclose(out);
fclose(in);
return 0;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论