summaryrefslogtreecommitdiffstats
path: root/tools/h5repack/h5repack.c
blob: 2449c36d4cb908393dec4fd27a8268a42c5ffe40 (plain)
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * Copyright by the Board of Trustees of the University of Illinois.         *
 * All rights reserved.                                                      *
 *                                                                           *
 * This file is part of HDF5.  The full HDF5 copyright notice, including     *
 * terms governing use, modification, and redistribution, is contained in    *
 * the files COPYING and Copyright.html.  COPYING can be found at the root   *
 * of the source code distribution tree; Copyright.html can be found at the  *
 * root level of an installed copy of the electronic HDF5 document set and   *
 * is linked from the top-level documents page.  It can also be found at     *
 * http://hdfgroup.org/HDF5/doc/Copyright.html.  If you do not have          *
 * access to either file, you may request a copy from help@hdfgroup.org.     *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#include <stdlib.h>
#include <string.h>
#include "h5repack.h"
#include "h5tools_utils.h"

extern char  *progname;


/*-------------------------------------------------------------------------
 * File: h5repack.c
 * Purpose: Public API functions
 *-------------------------------------------------------------------------
 */

static int check_options(pack_opt_t *options);


/*-------------------------------------------------------------------------
 * Function: aux_initglb_filter
 *
 * Purpose: auxiliary function, initialize the options global filter
 *
 * Return: void
 *
 *-------------------------------------------------------------------------
 */
static void aux_initglb_filter(pack_opt_t *options)
{
 int k;
 options->filter_g.filtn  = -1;
 for ( k=0; k<CDVALUES; k++)
  options->filter_g.cd_values[k] = -1;
}


/*-------------------------------------------------------------------------
 * Function: h5repack
 *
 * Purpose: locate all high-level HDF5 objects in the file
 *  and compress/chunk them using options
 *
 * Algorythm: 2 traversals are made to the file; the 1st builds a list of
 *  the objects, the 2nd makes a copy of them, using the options;
 *  the reason for the 1st traversal is to check for invalid
 *  object name requests
 *
 * Return: 0, ok, -1, fail
 *
 * Programmer: pvn@ncsa.uiuc.edu
 *
 * Date: September, 22, 2003
 *
 *-------------------------------------------------------------------------
 */
int h5repack(const char* infile,
             const char* outfile,
             pack_opt_t *options)
{
 /* check input */
 if (check_options(options)<0)
  return -1;

 /* check for objects in input that are in the file */
 if (check_objects(infile,options) < 0)
  return -1;

 /* copy the objects  */
 if (copy_objects(infile,outfile,options) < 0)
  return -1;


 return 0;
}



/*-------------------------------------------------------------------------
 * Function: h5repack_init
 *
 * Purpose: initialize options
 *
 * Return: 0, ok, -1, fail
 *
 *-------------------------------------------------------------------------
 */

int h5repack_init (pack_opt_t *options,
                   int verbose)
{
 memset(options,0,sizeof(pack_opt_t));
 options->threshold = 1024;
 options->verbose   = verbose;
 return (options_table_init(&(options->op_tbl)));
}

/*-------------------------------------------------------------------------
 * Function: h5repack_end
 *
 * Purpose: free options table
 *
 *-------------------------------------------------------------------------
 */

int h5repack_end  (pack_opt_t *options)
{
 return options_table_free(options->op_tbl);
}

/*-------------------------------------------------------------------------
 * Function: h5repack_addfilter
 *
 * Purpose: add a compression -f option to table
 *   Example: -f dset:GZIP=6
 *
 * Return: 0, ok, -1, fail
 *
 *-------------------------------------------------------------------------
 */

int h5repack_addfilter(const char* str,
                       pack_opt_t *options)
{
 obj_list_t      *obj_list=NULL; /*one object list for the -f and -c option entry */
 filter_info_t   filt;           /*filter info for the current -f option entry */
 int             n_objs;         /*number of objects in the current -f or -c option entry */

 if (options->all_filter==1){
  error_msg(progname, "invalid compression input: 'all' option is present \
   with other objects <%s>\n",str);
  return -1;
 }

 /* parse the -f option */
 obj_list=parse_filter(str,&n_objs,&filt,options);
 if (obj_list==NULL)
 {
  return -1;
 }

 if (options->all_filter==1)
 {
  /* if we are compressing all set the global filter type */
  aux_initglb_filter(options);
  options->filter_g=filt;
 }

 if (options->all_filter==0)
  options_add_filter(obj_list,n_objs,filt,options->op_tbl);

 free(obj_list);
 return 0;
}


/*-------------------------------------------------------------------------
 * Function: h5repack_addlayout
 *
 * Purpose: add a layout option
 *
 * Return: 0, ok, -1, fail
 *
 *-------------------------------------------------------------------------
 */


int h5repack_addlayout(const char* str,
                       pack_opt_t *options)
{

 obj_list_t  *obj_list=NULL;     /*one object list for the -t and -c option entry */
 int         n_objs;             /*number of objects in the current -t or -c option entry */
 pack_info_t pack;               /*info about layout to extract from parse */
 int         j;

 init_packobject(&pack);

 if (options->all_layout==1){
  error_msg(progname, "invalid layout input: 'all' option \
   is present with other objects <%s>\n",str);
  return -1;
 }

 /* parse the layout option */
 obj_list=parse_layout(str,&n_objs,&pack,options);
 if (obj_list==NULL)
  return -1;

 /* set global layout option */
 if (options->all_layout==1 )
 {
  options->layout_g=pack.layout;
  if (pack.layout==H5D_CHUNKED)
  {
   /* -2 means the NONE option, remove chunking
      and set the global layout to contiguous */
   if (pack.chunk.rank==-2)
   {
    options->layout_g = H5D_CONTIGUOUS;
   }
   /* otherwise set the global chunking type */
   else
   {
    options->chunk_g.rank=pack.chunk.rank;
    for (j = 0; j < pack.chunk.rank; j++)
     options->chunk_g.chunk_lengths[j] = pack.chunk.chunk_lengths[j];
   }
  }
 }

 if (options->all_layout==0)
  options_add_layout(obj_list,
   n_objs,
   &pack,
   options->op_tbl);

 free(obj_list);
 return 0;
}


/*-------------------------------------------------------------------------
 * Function: check_options
 *
 * Purpose: print options, checks for invalid options
 *
 * Return: void, return -1 on error
 *
 * Programmer: pvn@ncsa.uiuc.edu
 *
 * Date: September, 22, 2003
 *
 *-------------------------------------------------------------------------
 */
static int check_options(pack_opt_t *options)
{
 unsigned int   i;
 int            k, j, has_cp=0, has_ck=0;
 char           slayout[30];

/*-------------------------------------------------------------------------
 * objects to layout
 *-------------------------------------------------------------------------
 */
 if (options->verbose && have_request(options) /* only print if requested */)
 {
  printf("Objects to modify layout are...\n");
  if (options->all_layout==1)  {
   switch (options->layout_g)
   {
   case H5D_COMPACT:
    strcpy(slayout,"compact");
    break;
   case H5D_CONTIGUOUS:
    strcpy(slayout,"contiguous");
    break;
   case H5D_CHUNKED:
    strcpy(slayout,"chunked");
    break;
   default:
    strcpy(slayout,"unknown");
    break;
   }
   printf(" Apply %s layout to all\n", slayout);
   if (H5D_CHUNKED==options->layout_g) {
    printf("with dimension [");
    for ( j = 0; j < options->chunk_g.rank; j++)
     printf("%d ",(int)options->chunk_g.chunk_lengths[j]);
    printf("]\n");
   }
  }
 }/* verbose */

 for ( i = 0; i < options->op_tbl->nelems; i++)
 {
  char* name=options->op_tbl->objs[i].path;

  if (options->op_tbl->objs[i].chunk.rank>0)
  {
   if (options->verbose){
    printf(" <%s> with chunk size ",name);
    for ( k = 0; k < options->op_tbl->objs[i].chunk.rank; k++)
     printf("%d ",(int)options->op_tbl->objs[i].chunk.chunk_lengths[k]);
    printf("\n");
   }
   has_ck=1;
  }
  else if (options->op_tbl->objs[i].chunk.rank==-2)
  {
   if (options->verbose)
    printf(" <%s> %s\n",name,"NONE (contigous)");
   has_ck=1;
  }
 }

 if (options->all_layout==1 && has_ck){
  error_msg(progname, "invalid chunking input: 'all' option\
   is present with other objects\n");
  return -1;
 }

/*-------------------------------------------------------------------------
 * objects to filter
 *-------------------------------------------------------------------------
 */

 if (options->verbose && have_request(options) /* only print if requested */)
 {
  printf("Objects to apply filter are...\n");
  if (options->all_filter==1)
  {
   H5Z_filter_t filtn=options->filter_g.filtn;
   switch (filtn)
   {
   case H5Z_FILTER_NONE:
     printf(" Uncompress all\n");
    break;
   case H5Z_FILTER_SHUFFLE:
   case H5Z_FILTER_FLETCHER32:
     printf(" All with %s\n",get_sfilter(filtn));
    break;
   case H5Z_FILTER_SZIP:
   case H5Z_FILTER_DEFLATE:
     printf(" All with %s, parameter %d\n",
      get_sfilter(filtn),
      options->filter_g.cd_values[0]);
    break;
   };
  }
 } /* verbose */

 for ( i = 0; i < options->op_tbl->nelems; i++)
 {
  pack_info_t pack  = options->op_tbl->objs[i];
  char*       name  = pack.path;

  for ( j=0; j<pack.nfilters; j++)
  {
   if (options->verbose)
   {
    printf(" <%s> with %s filter\n",
     name,
     get_sfilter(pack.filter[j].filtn));
   }

   has_cp=1;

  } /* j */
 } /* i */

 if (options->all_filter==1 && has_cp){
  error_msg(progname, "invalid compression input: 'all' option\
   is present with other objects\n");
  return -1;
 }


 return 0;
}

/*-------------------------------------------------------------------------
 * Function: read_info
 *
 * Purpose: read comp and chunk options from file
 *
 * Return: void, exit on error
 *
 * Programmer: pvn@ncsa.uiuc.edu
 *
 * Date: September, 22, 2003
 *
 *-------------------------------------------------------------------------
 */

void read_info(const char *filename,
               pack_opt_t *options)
{

 char stype[10];
 char comp_info[1024];
 FILE *fp;
 char c;
 int  i, rc=1;
 char  *srcdir = getenv("srcdir"); /* the source directory */
 char  data_file[512]="";          /* buffer to hold name of existing file */

 /* compose the name of the file to open, using the srcdir, if appropriate */
 if (srcdir){
  strcpy(data_file,srcdir);
  strcat(data_file,"/");
 }
 strcat(data_file,filename);


 if ((fp = fopen(data_file, "r")) == (FILE *)NULL) {
  error_msg(progname, "cannot open options file %s\n", filename);
  exit(1);
 }

 /* cycle until end of file reached */
 while( 1 )
 {
  rc=fscanf(fp, "%s", stype);
  if (rc==-1)
   break;

 /*-------------------------------------------------------------------------
  * filter
  *-------------------------------------------------------------------------
  */
  if (strcmp(stype,"-f") == 0) {

   /* find begining of info */
   i=0; c='0';
   while( c!=' ' )
   {
    fscanf(fp, "%c", &c);
    if (feof(fp)) break;
   }
   c='0';
   /* go until end */
   while( c!=' ' )
   {
    fscanf(fp, "%c", &c);
    comp_info[i]=c;
    i++;
    if (feof(fp)) break;
    if (c==10 /*eol*/) break;
   }
   comp_info[i-1]='\0'; /*cut the last " */

   if (h5repack_addfilter(comp_info,options)==-1){
    error_msg(progname, "could not add compression option\n");
    exit(1);
   }
  }
 /*-------------------------------------------------------------------------
  * layout
  *-------------------------------------------------------------------------
  */
  else if (strcmp(stype,"-l") == 0) {

   /* find begining of info */
   i=0; c='0';
   while( c!=' ' )
   {
    fscanf(fp, "%c", &c);
    if (feof(fp)) break;
   }
   c='0';
   /* go until end */
   while( c!=' ' )
   {
    fscanf(fp, "%c", &c);
    comp_info[i]=c;
    i++;
    if (feof(fp)) break;
    if (c==10 /*eol*/) break;
   }
   comp_info[i-1]='\0'; /*cut the last " */

   if (h5repack_addlayout(comp_info,options)==-1){
    error_msg(progname, "could not add chunck option\n");
    exit(1);
   }
  }
 /*-------------------------------------------------------------------------
  * not valid
  *-------------------------------------------------------------------------
  */
  else {
   error_msg(progname, "bad file format for %s", filename);
   exit(1);
  }
 }

 fclose(fp);
 return;
}


/*-------------------------------------------------------------------------
 * Function: have_request
 *
 * Purpose: check if a filter or layout was requested
 *
 * Return: 1 yes, 0 no
 *
 * Date: May, 24, 2007
 *
 *-------------------------------------------------------------------------
 */
int have_request(pack_opt_t *options)
{

    if (options->all_filter || options->all_layout || options->op_tbl->nelems)
        return 1;

    return 0;

}