2017-10-27 09:57:49 +00:00
|
|
|
/*
|
2019-04-15 13:44:45 +00:00
|
|
|
* Copyright 2005-2019 ECMWF.
|
2017-10-27 09:57:49 +00:00
|
|
|
*
|
|
|
|
* This software is licensed under the terms of the Apache Licence Version 2.0
|
|
|
|
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
|
|
|
|
*
|
|
|
|
* In applying this licence, ECMWF does not waive the privileges and immunities granted to it by
|
|
|
|
* virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction.
|
|
|
|
*/
|
|
|
|
|
2017-12-01 13:28:57 +00:00
|
|
|
/*
|
|
|
|
* Description:
|
|
|
|
* Split an input file (GRIB, BUFR etc) into chunks of roughly the same size.
|
|
|
|
* The output files are named input_01, input_02 etc. This is much faster than grib_copy/bufr_copy
|
2019-08-08 02:13:37 +00:00
|
|
|
*
|
|
|
|
* 2019-07-26 W.Qu Allow an input file to be split into each individual message (if nchunk=-1)
|
2017-12-01 13:28:57 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2017-10-27 09:57:49 +00:00
|
|
|
#include "grib_api_internal.h"
|
2017-10-27 18:05:50 +00:00
|
|
|
#include <assert.h>
|
2017-10-27 09:57:49 +00:00
|
|
|
|
2017-10-27 18:05:50 +00:00
|
|
|
static int verbose = 0;
|
2019-08-08 02:13:37 +00:00
|
|
|
static const char* OUTPUT_FILENAME_FORMAT = "%s_%03d"; /* x_001, x_002 etc */
|
2017-10-27 09:57:49 +00:00
|
|
|
static void usage(const char* prog)
|
|
|
|
{
|
2018-09-19 10:42:21 +00:00
|
|
|
printf("Usage: %s [-v] nchunks infile\n",prog);
|
2019-08-08 02:13:37 +00:00
|
|
|
printf("nchunks=-1, split infile into individual grib/bufr message\n");
|
2017-10-27 09:57:49 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2017-10-27 18:05:50 +00:00
|
|
|
static int split_file(FILE* in, const char* filename, const int nchunks, unsigned long *count)
|
2017-10-27 09:57:49 +00:00
|
|
|
{
|
|
|
|
void* mesg=NULL;
|
|
|
|
FILE* out;
|
2019-08-08 02:13:37 +00:00
|
|
|
size_t size=0,read_size=0,insize=0,chunk_size, msg_size=0, num_msg=0;
|
2017-10-27 09:57:49 +00:00
|
|
|
off_t offset=0;
|
|
|
|
int err=GRIB_SUCCESS;
|
|
|
|
int i;
|
|
|
|
char* ofilename;
|
|
|
|
grib_context* c=grib_context_get_default();
|
|
|
|
|
|
|
|
if (!in) return 1;
|
|
|
|
|
2017-10-27 18:05:50 +00:00
|
|
|
/* name of output file */
|
2017-10-27 09:57:49 +00:00
|
|
|
ofilename=(char*)calloc(1,strlen(filename)+10);
|
|
|
|
|
|
|
|
fseeko(in, 0, SEEK_END);
|
|
|
|
insize = ftello(in);
|
|
|
|
fseeko(in, 0, SEEK_SET);
|
2019-08-08 02:13:37 +00:00
|
|
|
if(nchunks == -1){
|
|
|
|
chunk_size = size;
|
|
|
|
}else{
|
|
|
|
assert(nchunks > 0);
|
|
|
|
chunk_size=insize/nchunks;
|
|
|
|
}
|
2017-10-27 09:57:49 +00:00
|
|
|
|
|
|
|
i=1;
|
2017-10-27 18:05:50 +00:00
|
|
|
sprintf(ofilename, OUTPUT_FILENAME_FORMAT, filename, i);
|
2017-10-27 09:57:49 +00:00
|
|
|
out=fopen(ofilename,"w");
|
|
|
|
if (!out) {
|
2017-10-27 18:05:50 +00:00
|
|
|
perror(ofilename);
|
|
|
|
free(ofilename);
|
|
|
|
return GRIB_IO_PROBLEM;
|
2017-10-27 09:57:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while ( err!=GRIB_END_OF_FILE ) {
|
2017-10-27 18:05:50 +00:00
|
|
|
mesg=wmo_read_any_from_file_malloc(in, 0, &size, &offset, &err);
|
2019-08-08 02:13:37 +00:00
|
|
|
num_msg++;
|
|
|
|
/*printf("=1=%d\t%d\t%d\n",*count,size,insize);*/
|
|
|
|
if ( mesg!=NULL && err==0 ) {
|
|
|
|
if (fwrite(mesg,1,size,out)!=size ) {
|
2017-10-27 18:05:50 +00:00
|
|
|
perror(ofilename);
|
|
|
|
free(ofilename);
|
|
|
|
fclose(out);
|
|
|
|
return GRIB_IO_PROBLEM;
|
2017-10-27 09:57:49 +00:00
|
|
|
}
|
2017-10-27 18:05:50 +00:00
|
|
|
grib_context_free(c,mesg);
|
|
|
|
read_size+=size;
|
2019-08-08 02:13:37 +00:00
|
|
|
msg_size+=size;
|
|
|
|
if (read_size>chunk_size && msg_size < insize) {
|
|
|
|
if (verbose) printf("Wrote output file %s (%d msgs)\n", ofilename, num_msg);
|
2017-10-27 18:05:50 +00:00
|
|
|
fclose(out);
|
|
|
|
i++;
|
|
|
|
/* Start writing to the next file */
|
2019-08-08 02:13:37 +00:00
|
|
|
/*printf("=2=%d\t%d\n",*count,msg_size);*/
|
2017-10-27 18:05:50 +00:00
|
|
|
sprintf(ofilename, OUTPUT_FILENAME_FORMAT, filename, i);
|
|
|
|
out=fopen(ofilename,"w");
|
|
|
|
if (!out) {
|
|
|
|
perror(ofilename);
|
|
|
|
free(ofilename);
|
|
|
|
return GRIB_IO_PROBLEM;
|
|
|
|
}
|
|
|
|
read_size=0;
|
2019-08-08 02:13:37 +00:00
|
|
|
num_msg=0;
|
2017-10-27 18:05:50 +00:00
|
|
|
}
|
|
|
|
(*count)++;
|
2017-10-27 09:57:49 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-08 02:13:37 +00:00
|
|
|
if (verbose) printf("Wrote output file %s (%d msgs)\n", ofilename,num_msg-1);
|
2017-10-27 09:57:49 +00:00
|
|
|
fclose(out);
|
2017-10-27 10:20:09 +00:00
|
|
|
free(ofilename);
|
2017-10-27 09:57:49 +00:00
|
|
|
|
|
|
|
if (err==GRIB_END_OF_FILE) err=GRIB_SUCCESS;
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc,char* argv[])
|
|
|
|
{
|
|
|
|
FILE* infh = NULL;
|
|
|
|
char* filename;
|
2017-10-27 18:05:50 +00:00
|
|
|
int i, status=0;
|
|
|
|
struct stat s;
|
2017-10-27 09:57:49 +00:00
|
|
|
int err=0,nchunks=0;
|
|
|
|
unsigned long count=0;
|
|
|
|
|
|
|
|
if (argc <3) usage(argv[0]);
|
|
|
|
|
|
|
|
i=1;
|
|
|
|
if (strcmp(argv[i], "-v")==0) {
|
2017-10-27 18:05:50 +00:00
|
|
|
i++;
|
|
|
|
verbose = 1;
|
|
|
|
if (argc !=4) usage(argv[0]);
|
2017-10-27 09:57:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* add some error checking */
|
|
|
|
nchunks=atoi(argv[i]);
|
2019-08-08 02:13:37 +00:00
|
|
|
if (nchunks<1 && nchunks!=-1) {
|
|
|
|
fprintf(stderr,"ERROR: Invalid number %d. Please specify a positive integer. or -1 for spliting each message\n", nchunks);
|
2017-10-27 18:05:50 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2017-10-27 09:57:49 +00:00
|
|
|
|
|
|
|
i++;
|
|
|
|
filename=argv[i];
|
2017-10-27 18:05:50 +00:00
|
|
|
if (stat(filename, &s)==0) {
|
|
|
|
if (S_ISDIR(s.st_mode)) {
|
|
|
|
fprintf(stderr, "ERROR: %s: Is a directory\n", filename);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2019-03-14 12:34:41 +00:00
|
|
|
infh=fopen(filename,"rb");
|
2017-10-27 09:57:49 +00:00
|
|
|
if (!infh) {
|
2017-10-27 18:05:50 +00:00
|
|
|
perror(filename);
|
|
|
|
return 1;
|
2017-10-27 09:57:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
count=0;
|
2017-10-27 18:05:50 +00:00
|
|
|
err=split_file(infh, filename, nchunks, &count);
|
2017-10-27 09:57:49 +00:00
|
|
|
if (err) {
|
2017-10-27 18:05:50 +00:00
|
|
|
fprintf(stderr,"ERROR: Failed to split file %s", filename);
|
|
|
|
fprintf(stderr,"\n");
|
|
|
|
status = 1;
|
|
|
|
} else {
|
|
|
|
if (verbose) printf ("%7lu %s\n", count, filename);
|
2017-10-27 09:57:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fclose(infh);
|
|
|
|
|
2017-10-27 18:05:50 +00:00
|
|
|
return status;
|
2017-10-27 09:57:49 +00:00
|
|
|
}
|