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 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
|
/* ----------------------------- MNI Header -----------------------------------
@NAME : minc_modify_header
@INPUT : argc, argv - command line arguments
@OUTPUT : (none)
@RETURNS : error status
@DESCRIPTION: Modifies the header of a minc file, in place if possible.
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : March 31, 1995 (Peter Neelin)
@MODIFIED :
* $Log: minc_modify_header.c,v $
* Revision 6.13 2008-01-17 02:33:02 rotor
* * removed all rcsids
* * removed a bunch of ^L's that somehow crept in
* * removed old (and outdated) BUGS file
*
* Revision 6.12 2008/01/12 19:08:15 stever
* Add __attribute__ ((unused)) to all rcsid variables.
*
* Revision 6.11 2007/12/11 12:43:01 rotor
* * added static to all global variables in main programs to avoid linking
* problems with libraries (compress in mincconvert and libz for example)
*
* Revision 6.10 2004/11/01 22:38:38 bert
* Eliminate all references to minc_def.h
*
* Revision 6.9 2004/06/11 15:19:34 bert
* Fix attribute append operation
*
* Revision 6.8 2004/05/25 21:33:51 bert
* Add -dappend and -sappend
*
* Revision 6.7 2004/02/02 18:27:06 bert
* Include config.h
*
* Revision 6.6 2003/11/14 16:52:24 stever
* More last-minute fixes.
*
* Revision 6.5 2001/04/17 18:40:16 neelin
* Modifications to work with NetCDF 3.x
* In particular, changed NC_LONG to NC_INT (and corresponding longs to ints).
* Changed NC_UNSPECIFIED to NC_NAT.
* A few fixes to the configure script.
*
* Revision 6.4 2000/12/13 16:19:37 neelin
* Removed debugging statements
*
* Revision 6.3 2000/11/03 16:35:40 neelin
* Modified -dinsert option to allow multiple double values to be
* inserted in an attribute.
*
* Revision 6.2 2000/09/13 14:12:35 neelin
* Added support for bzipped files (thanks to Steve Robbins).
*
* Revision 6.1 1999/10/19 14:45:17 neelin
* Fixed Log subsitutions for CVS
*
* Revision 6.0 1997/09/12 13:24:07 neelin
* Release of minc version 0.6
*
* Revision 5.0 1997/08/21 13:25:06 neelin
* Release of minc version 0.5
*
* Revision 4.0 1997/05/07 20:01:35 neelin
* Release of minc version 0.4
*
* Revision 3.1 1995/11/16 13:16:19 neelin
* Added include of math.h to get declaration of strtod under SunOs
*
* Revision 3.0 1995/05/15 19:32:21 neelin
* Release of minc version 0.3
*
* Revision 1.2 1995/04/04 19:10:56 neelin
* Fixed handling of compressed files.
*
* Revision 1.1 1995/04/04 15:51:42 neelin
* Initial revision
*
@COPYRIGHT :
Copyright 1995 Peter Neelin, McConnell Brain Imaging Centre,
Montreal Neurological Institute, McGill University.
Permission to use, copy, modify, and distribute this
software and its documentation for any purpose and without
fee is hereby granted, provided that the above copyright
notice appear in all copies. The author and McGill University
make no representations about the suitability of this
software for any purpose. It is provided "as is" without
express or implied warranty.
---------------------------------------------------------------------------- */
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <minc.h>
#include <ParseArgv.h>
/* Constants */
#define MINC_EXTENSION ".mnc"
#define BZIP_EXTENSION ".bz"
#define BZIP2_EXTENSION ".bz2"
#define GZIP_EXTENSION ".gz"
#define COMPRESS_EXTENSION ".Z"
#define PACK_EXTENSION ".z"
#define ZIP_EXTENSION ".zip"
#ifndef TRUE
# define TRUE 1
#endif
#ifndef FALSE
# define FALSE 0
#endif
/* Typedefs */
typedef enum {
Insert_attribute, Delete_attribute, Append_attribute
} Attribute_Action;
/* Functions */
int get_attribute(char *dst, char *key, char *nextarg);
/* Argument variables */
int attribute_list_size = 0;
int attribute_list_alloc = 0;
struct {
Attribute_Action action;
char *variable;
char *attribute;
char *value;
int num_doubles;
double *double_values;
} *attribute_list = NULL;
/* Argument table */
static ArgvInfo argTable[] = {
{NULL, ARGV_HELP, NULL, NULL,
"Options for specifying attribute values by name."},
{"-sinsert", ARGV_FUNC, (char *) get_attribute, NULL,
"Insert string attribute (<var>:<attr>=<value>)."},
{"-dinsert", ARGV_FUNC, (char *) get_attribute, NULL,
"Insert a double precision attribute (<var>:<attr>=<value>(,...))."},
{"-delete", ARGV_FUNC, (char *) get_attribute, NULL,
"Delete an attribute (<var>:<attr>)."},
{"-sappend", ARGV_FUNC, (char *) get_attribute, NULL,
"Append string attribute (<var>:<attr>=<value>)."},
{"-dappend", ARGV_FUNC, (char *) get_attribute, NULL,
"Append a double precision attribute (<var>:<attr>=<value>(,...))."},
{NULL, ARGV_END, NULL, NULL, NULL}
};
/* Main program */
int main(int argc, char *argv[])
{
char *pname;
char *filename, *tempfile, *newfile;
char string[1024];
char *variable_name, *attribute_name;
int created_tempfile;
int done_redef;
int iatt;
int mincid, varid;
int variable_exists, attribute_exists;
nc_type attribute_type, new_type;
int attribute_length, new_length;
void *new_value;
int total_length, alloc_length, ival;
char *zeros;
int old_ncopts;
/* Parse the command line */
pname=argv[0];
if (ParseArgv(&argc, argv, argTable, 0) || (argc != 2)) {
(void) fprintf(stderr, "\nUsage: %s [<options>] <file.mnc>\n",
pname);
(void) fprintf(stderr, " %s [-help]\n\n", pname);
exit(EXIT_FAILURE);
}
filename = argv[1];
/* Create temp file name. First try looking for minc extension, then
a compression extension. Chop off the unwanted extension. */
(void) strncpy(string, filename, sizeof(string)-1);
tempfile = strstr(string, MINC_EXTENSION);
if (tempfile != NULL) {
tempfile += strlen(MINC_EXTENSION);
if (*tempfile == '\0')
tempfile = NULL;
}
else {
tempfile = strstr(string, GZIP_EXTENSION);
if (tempfile == NULL)
tempfile = strstr(string, BZIP_EXTENSION);
if (tempfile == NULL)
tempfile = strstr(string, BZIP2_EXTENSION);
if (tempfile == NULL)
tempfile = strstr(string, COMPRESS_EXTENSION);
if (tempfile == NULL)
tempfile = strstr(string, PACK_EXTENSION);
if (tempfile == NULL)
tempfile = strstr(string, ZIP_EXTENSION);
}
if (tempfile != NULL) {
*tempfile = '\0';
tempfile = string;
}
/* If tempfile == NULL, then either we have a minc file or we don't know
how to edit the file in place. Check that it is a minc file. */
if (tempfile == NULL) {
newfile = miexpand_file(filename, tempfile, TRUE, &created_tempfile);
if (created_tempfile) {
if (newfile != NULL) {
(void) remove(newfile);
free(newfile);
}
(void) fprintf(stderr, "Cannot edit file \"%s\" in place.\n",
filename);
exit(EXIT_FAILURE);
}
}
/* Expand the file. */
newfile = miexpand_file(filename, tempfile, FALSE, &created_tempfile);
if (newfile == NULL) {
(void) fprintf(stderr, "Error decompressing file \"%s\"\n",
filename);
exit(EXIT_FAILURE);
}
/* If a new file was created, get rid of the old one */
if (created_tempfile) {
(void) remove(filename);
}
/* Open the file */
mincid = miopen(newfile, NC_WRITE);
/* Loop through attribute list, modifying values */
done_redef = FALSE;
ncopts = NC_VERBOSE;
zeros = NULL;
alloc_length = 0;
for (iatt=0; iatt < attribute_list_size; iatt++) {
/* Get variable and attribute name */
variable_name = attribute_list[iatt].variable;
attribute_name = attribute_list[iatt].attribute;
/* Check for attribute existence */
if (strlen(variable_name) == 0) {
varid = NC_GLOBAL;
variable_exists = TRUE;
}
else {
old_ncopts = ncopts; ncopts = 0;
varid = ncvarid(mincid, variable_name);
ncopts = old_ncopts;
variable_exists = (varid != MI_ERROR);
}
attribute_type = NC_CHAR;
attribute_length = 0;
if (variable_exists) {
old_ncopts = ncopts; ncopts = 0;
attribute_exists =
(ncattinq(mincid, varid, attribute_name,
&attribute_type, &attribute_length) != MI_ERROR);
ncopts = old_ncopts;
}
else
attribute_exists = FALSE;
/* Are we inserting or deleting? */
switch (attribute_list[iatt].action) {
case Insert_attribute:
case Append_attribute:
if (attribute_list[iatt].value != NULL) {
new_type = NC_CHAR;
new_length = strlen(attribute_list[iatt].value)+1;
new_value = (void *) attribute_list[iatt].value;
}
else {
new_type = NC_DOUBLE;
new_length = attribute_list[iatt].num_doubles;
new_value = (void *) attribute_list[iatt].double_values;
}
/* For append we have to copy the entire attribute, if it
* already exists.
*/
if (attribute_list[iatt].action == Append_attribute &&
attribute_exists) {
char *tmp_value;
/* Verify that the existing type matches the newly
* requested type. Don't allow a -dappend on a
* string attribute, for example.
*/
if (new_type != attribute_type) {
fprintf(stderr,
"Can't append %s data to %s attribute %s:%s.\n",
(new_type == NC_DOUBLE) ? "double" : "string",
(attribute_type == NC_DOUBLE) ? "double" : "string",
variable_name, attribute_name);
exit(EXIT_FAILURE);
}
new_type = attribute_type;
tmp_value = malloc((attribute_length + new_length) * nctypelen(new_type));
ncattget(mincid, varid, attribute_name, tmp_value);
/* For string attributes, remove any trailing null
* character before appending.
*/
if (new_type == NC_CHAR && tmp_value[attribute_length-1] == 0) {
attribute_length--;
}
memcpy(tmp_value + attribute_length * nctypelen(new_type),
new_value,
new_length * nctypelen(new_type));
new_length += attribute_length;
new_value = (void *) tmp_value;
}
total_length = attribute_length*nctypelen(attribute_type);
if (!attribute_exists ||
(total_length < new_length*nctypelen(new_type))) {
if (! done_redef) {
done_redef = TRUE;
(void) ncredef(mincid);
}
}
else if (!done_redef && attribute_exists && (total_length > 0)) {
if (total_length > alloc_length) {
if (zeros != NULL) free(zeros);
zeros = malloc(total_length);
alloc_length = total_length;
for (ival=0; ival < alloc_length; ival++)
zeros[ival] = '\0';
}
(void) ncattput(mincid, varid, attribute_name, NC_CHAR,
total_length, zeros);
(void) ncsync(mincid);
}
if (!variable_exists) {
old_ncopts = ncopts; ncopts = 0;
varid = micreate_group_variable(mincid, variable_name);
ncopts = old_ncopts;
if (varid == MI_ERROR) {
varid = ncvardef(mincid, variable_name, NC_INT,
0, NULL);
}
variable_exists = (varid != MI_ERROR);
}
if (variable_exists) {
(void) ncattput(mincid, varid, attribute_name,
new_type, new_length, new_value);
}
break;
case Delete_attribute:
if (attribute_exists) {
if (! done_redef) {
done_redef = TRUE;
(void) ncredef(mincid);
}
(void) ncattdel(mincid, varid, attribute_name);
}
break;
default:
(void) fprintf(stderr, "Program error: unknown action %d\n",
(int) attribute_list[iatt].action);
exit(EXIT_FAILURE);
}
}
ncopts = NC_VERBOSE | NC_FATAL;
/* Close the file */
(void) miclose(mincid);
/* Free stuff */
free(newfile);
if (zeros != NULL) free(zeros);
exit(EXIT_SUCCESS);
}
/* ----------------------------- MNI Header -----------------------------------
@NAME : get_attribute
@INPUT : dst - client data passed by ParseArgv
key - matching key in argv
nextarg - argument following key in argv
@OUTPUT : (none)
@RETURNS : TRUE because nextarg is used.
@DESCRIPTION: Gets attributes from command line. Syntax for argument is
"<var>:<att>=<value>". Numeric values are converted to
double precision.
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : May 3, 1994 (Peter Neelin)
@MODIFIED : March 31, 1995 (P.N)
- grabbed from rawtominc
---------------------------------------------------------------------------- */
int get_attribute(char *dst, char *key, char *nextarg)
/* ARGSUSED */
{
int need_double;
Attribute_Action action;
char *format;
char *variable;
char *attribute;
char *value;
char *end, *cur;
double *dvalues;
int num_doubles, ivalue;
/* Check the key */
if (strcmp(key, "-sinsert") == 0) {
action = Insert_attribute;
need_double = FALSE;
format = "<var>:<attr>=<val>";
}
else if (strcmp(key, "-dinsert") == 0) {
action = Insert_attribute;
need_double = TRUE;
format = "<var>:<attr>=<val>";
}
else if (strcmp(key, "-delete") == 0) {
action = Delete_attribute;
need_double = FALSE;
format = "<var>:<attr>";
}
else if (strcmp(key, "-sappend") == 0) {
action = Append_attribute;
need_double = FALSE;
format = "<var>:<attr>=<val>";
}
else if (strcmp(key, "-dappend") == 0) {
action = Append_attribute;
need_double = TRUE;
format = "<var>:<attr>=<val>";
}
/* Check for a following argument */
if (nextarg == NULL) {
(void) fprintf(stderr,
"\"%s\" option requires an additional argument\n",
key);
exit(EXIT_FAILURE);
}
/* Get the variable name */
variable = nextarg;
attribute = strchr(variable, ':');
if (attribute == NULL) {
(void) fprintf(stderr,
"%s option requires argument %s\n",
key, format);
exit(EXIT_FAILURE);
}
*attribute = '\0';
attribute++;
/* Get the value */
value = NULL;
num_doubles = 0;
dvalues = NULL;
if (action == Insert_attribute || action == Append_attribute) {
value = strchr(attribute, '=');
if (value == NULL) {
(void) fprintf(stderr,
"%s option requires argument <var>:<attr>=<val>\n",
key);
exit(EXIT_FAILURE);
}
*value = '\0';
value++;
/* Convert to double precision */
if (need_double) {
/* Count the commas */
num_doubles = 1;
for (cur=value; *cur != '\0'; cur++) {
if (*cur == ',') num_doubles++;
}
/* Allocate a list */
dvalues = malloc(sizeof(*dvalues) * num_doubles);
/* Loop over values */
cur = value;
for (ivalue=0; ivalue < num_doubles; ivalue++) {
/* Get the value */
dvalues[ivalue] = strtod(cur, &end);
if (end == cur) {
(void) fprintf(stderr,
"\"%s\" option requires a numeric argument\n",
key);
exit(EXIT_FAILURE);
}
cur = end;
/* Skip whitespace and the comma */
while (isspace((int) *cur)) {cur++;}
if ((*cur != '\0') && (*cur != ',')) {
(void) fprintf(stderr,
"\"%s\" option requires a numeric argument\n",
key);
exit(EXIT_FAILURE);
}
else if (*cur == ',') {
cur++;
}
} /* End of loop over double values */
/* Clear the value string */
value = NULL;
} /* Endif need_double */
} /* Endif insert */
/* Save the information */
attribute_list_size++;
if (attribute_list_size > attribute_list_alloc) {
attribute_list_alloc += 10;
if (attribute_list == NULL) {
attribute_list =
malloc(attribute_list_alloc * sizeof(*attribute_list));
}
else {
attribute_list =
realloc(attribute_list,
attribute_list_alloc * sizeof(*attribute_list));
}
}
attribute_list[attribute_list_size-1].action = action;
attribute_list[attribute_list_size-1].variable = variable;
attribute_list[attribute_list_size-1].attribute = attribute;
attribute_list[attribute_list_size-1].value = value;
attribute_list[attribute_list_size-1].num_doubles = num_doubles;
attribute_list[attribute_list_size-1].double_values = dvalues;
return TRUE;
}
|