/* Program to correct SPECSYS and SPECSYSG in the WAPP CIMAFITS headers */ /* Mikael Lerner --- 28-03-2006 */ #include #include #include #include #include #include #include #define BLOCK 40000000 int elements[] = { 18, 19 }; int check_block(char *p, int block) { unsigned char *p1; int i, j; for ( i = 0 ; i < 8 ; i++ ) { for ( j = 0 ; j < 2 ; j++ ) { p1 = p + 48960 + block * 16 * 1288 + ( i * 2 + j ) * 1288 + 154 * 8 + 2; if ( *p1++ != j ) return -1; if ( *p1 != i ) return -1; } } return(0); } int reshuffle(char *p, int block, int element) { char *p1; char values[16][8]; int i, j; /* for ( i = 0 ; i < 8 ; i++ ) { for ( j = 0 ; j < 2 ; j++ ) { p1 = p + 48960 + block * 16 * 1288 + ( i * 2 + j ) * 1288 + element * 8; memcpy(values[((i+1)*2+j)%16], p1, 8); } } */ for ( i = 0 ; i < 8 ; i++ ) { for ( j = 0 ; j < 2 ; j++ ) { p1 = p + 48960 + block * 16 * 1288 + ( i * 2 + j ) * 1288 + element * 8; /* memcpy(p1, values[i*2+j], 8); */ memcpy(p1, "TOPOCENT", 8); } } return(0); } main(int argc, char *argv[]) { char file[256]; char *p; int file_id; int rows, i, j; if ( argc < 2 ) { printf("ERROR: no file name given!\n"); exit(1); } strncpy(file, argv[1], sizeof(file)-1); printf("Correcting file '%s' ...\n", file); if ( ( p = (char *) calloc(BLOCK, 1) ) == NULL ) { printf("ERROR: failed to allocate memory!\n"); exit(1); } if ( ( file_id = open(file, O_RDWR) ) == -1 ) { printf("ERROR: can't open file '%s'!\n", file); exit(1); } if ( read(file_id, p, BLOCK) != BLOCK ) { printf("ERROR: can't read file '%s'!\n", file); close(file_id); exit(1); } if ( sscanf(&p[3220], "%d", &rows) != 1 ) { printf("ERROR: can't read number of rows in file '%s'!\n", file); close(file_id); exit(1); } if ( rows < 16 || rows > 42000 || (rows/16)*16 != rows ) { printf("ERROR: bad number of rows %d in file '%s'!\n", rows, file); close(file_id); exit(1); } printf("Processing %d rows (=%d ALFA scans) ...\n", rows, rows/16); for ( i = 0 ; i < rows/16 ; i++ ) { if ( check_block(p, i) == -1 ) { printf("ERROR: ALFA scan %d in file '%s' is not in valid order!\n", i+1, file); close(file_id); exit(1); } } for ( i = 0 ; i < rows/16 ; i++ ) { for ( j = 0 ; j < sizeof(elements)/sizeof(int) ; j++ ) { if ( reshuffle(p, i, elements[j]) == -1 ) { printf("ERROR: can't reshuffle ALFA scan %d in file '%s'!\n", i+1, file); close(file_id); exit(1); } } } if ( lseek(file_id, 0, SEEK_SET) == -1 ) { printf("ERROR: can't reposition file '%s' for writing!\n", file); close(file_id); exit(1); } if ( write(file_id, p, BLOCK) != BLOCK ) { printf("ERROR: failed writing to file '%s'!\n", file); printf("FATAL ERROR: file '%s' is now corrupted!\n", file); close(file_id); exit(1); } close(file_id); printf("Wrote modifications to file '%s'.\n", file); exit(0); }