static codefile_t* new_codefile(const char* fn, anbool writing, anbool inmem) { fitsbin_chunk_t chunk; codefile_t* cf = calloc(1, sizeof(codefile_t)); if (!cf) { SYSERROR("Couldn't calloc a codefile struct"); return NULL; } cf->healpix = -1; cf->hpnside = 1; if (inmem) { cf->fb = fitsbin_open_in_memory(); } else { if (writing) cf->fb = fitsbin_open_for_writing(fn); else cf->fb = fitsbin_open(fn); } if (!cf->fb) { ERROR("Failed to create fitsbin"); return NULL; } fitsbin_chunk_init(&chunk); chunk.tablename = "codes"; chunk.required = 1; chunk.callback_read_header = callback_read_header; chunk.userdata = cf; fitsbin_add_chunk(cf->fb, &chunk); fitsbin_chunk_clean(&chunk); return cf; }
static qidxfile* new_qidxfile(const char* fn, anbool writing) { qidxfile* qf; fitsbin_chunk_t chunk; qf = calloc(1, sizeof(qidxfile)); if (!qf) { SYSERROR("Couldn't malloc a qidxfile struct"); return NULL; } // default qf->dimquads = 4; if (writing) qf->fb = fitsbin_open_for_writing(fn); else qf->fb = fitsbin_open(fn); if (!qf->fb) { ERROR("Failed to create fitsbin"); return NULL; } fitsbin_chunk_init(&chunk); chunk.tablename = "qidx"; chunk.required = 1; chunk.callback_read_header = callback_read_header; chunk.userdata = qf; chunk.itemsize = sizeof(uint32_t); fitsbin_add_chunk(qf->fb, &chunk); fitsbin_chunk_clean(&chunk); return qf; }
void test_fitsbin_1(CuTest* ct) { fitsbin_t* in, *out; int i; int N = 6; double outdata[6]; double* indata; char* fn; fitsbin_chunk_t chunk; fn = get_tmpfile(0); out = fitsbin_open_for_writing(fn); CuAssertPtrNotNull(ct, out); CuAssertIntEquals(ct, 0, fitsbin_write_primary_header(out)); for (i=0; i<N; i++) { outdata[i] = i*i; } fitsbin_chunk_init(&chunk); chunk.tablename = "test1"; chunk.itemsize = sizeof(double); chunk.nrows = N; chunk.data = outdata; CuAssertIntEquals(ct, 0, fitsbin_write_chunk(out, &chunk)); CuAssertIntEquals(ct, fitsbin_fix_primary_header(out), 0); CuAssertIntEquals(ct, fitsbin_close(out), 0); fitsbin_chunk_clean(&chunk); // writing shouldn't affect the data values for (i=0; i<N; i++) { CuAssertIntEquals(ct, outdata[i], i*i); } in = fitsbin_open(fn); CuAssertPtrNotNull(ct, in); fitsbin_chunk_init(&chunk); chunk.tablename = "test1"; CuAssertIntEquals(ct, 0, fitsbin_read_chunk(in, &chunk)); CuAssertIntEquals(ct, sizeof(double), chunk.itemsize); CuAssertIntEquals(ct, N, chunk.nrows); indata = chunk.data; CuAssertPtrNotNull(ct, indata); CuAssertIntEquals(ct, 0, memcmp(outdata, indata, sizeof(outdata))); CuAssertIntEquals(ct, 0, fitsbin_close(in)); }
void test_inmemory_fitsbin_1(CuTest* ct) { fitsbin_t* fb; int i; int N = 6; double outdata[6]; double* indata; fitsbin_chunk_t chunk; fb = fitsbin_open_in_memory(); CuAssertPtrNotNull(ct, fb); CuAssertIntEquals(ct, 0, fitsbin_write_primary_header(fb)); for (i=0; i<N; i++) { outdata[i] = i*i; } fitsbin_chunk_init(&chunk); chunk.tablename = "test1"; chunk.itemsize = sizeof(double); chunk.nrows = N; chunk.data = outdata; CuAssertIntEquals(ct, 0, fitsbin_write_chunk(fb, &chunk)); CuAssertIntEquals(ct, 0, fitsbin_fix_primary_header(fb)); fitsbin_chunk_clean(&chunk); // writing shouldn't affect the data values for (i=0; i<N; i++) { CuAssertIntEquals(ct, outdata[i], i*i); } CuAssertIntEquals(ct, 0, fitsbin_switch_to_reading(fb)); fitsbin_chunk_init(&chunk); chunk.tablename = "test1"; CuAssertIntEquals(ct, 0, fitsbin_read_chunk(fb, &chunk)); CuAssertIntEquals(ct, sizeof(double), chunk.itemsize); CuAssertIntEquals(ct, N, chunk.nrows); indata = chunk.data; CuAssertPtrNotNull(ct, indata); CuAssertIntEquals(ct, 0, memcmp(outdata, indata, sizeof(outdata))); CuAssertIntEquals(ct, 0, fitsbin_close(fb)); }
static bl* get_chunks(startree_t* s, il* wordsizes) { bl* chunks = bl_new(4, sizeof(fitsbin_chunk_t)); fitsbin_chunk_t chunk; kdtree_t* kd = s->tree; fitsbin_chunk_init(&chunk); chunk.tablename = "sweep"; chunk.forced_type = fitscolumn_u8_type(); chunk.itemsize = sizeof(uint8_t); chunk.nrows = kd->ndata; chunk.data = s->sweep; chunk.userdata = &(s->sweep); chunk.required = FALSE; bl_append(chunks, &chunk); if (wordsizes) il_append(wordsizes, sizeof(uint8_t)); fitsbin_chunk_clean(&chunk); return chunks; }
void test_fitsbin_2(CuTest* ct) { fitsbin_t* in, *out; int i; int N = 6; double outdata[6]; double* indata; char* fn; fitsbin_chunk_t chunk; fitsbin_chunk_t* ch; fn = get_tmpfile(0); printf("Writing to %s\n", fn); out = fitsbin_open_for_writing(fn); CuAssertPtrNotNull(ct, out); CuAssertIntEquals(ct, 0, fitsbin_write_primary_header(out)); for (i=0; i<N; i++) { outdata[i] = i*i; } fitsbin_chunk_init(&chunk); chunk.tablename = "test2"; chunk.itemsize = 1; //chunk.nrows = N * sizeof(double); chunk.data = outdata; CuAssertIntEquals(ct, 0, fitsbin_write_chunk_header(out, &chunk)); for (i=0; i<N; i++) { CuAssertIntEquals(ct, 0, fitsbin_write_items(out, &chunk, &outdata[i], sizeof(double))); } CuAssertIntEquals(ct, 0, fitsbin_fix_chunk_header(out, &chunk)); fitsbin_chunk_reset(&chunk); chunk.tablename = "test2B"; chunk.itemsize = sizeof(double); //chunk.nrows = N; chunk.data = outdata; CuAssertIntEquals(ct, 0, fitsbin_write_chunk_header(out, &chunk)); for (i=0; i<N; i++) { CuAssertIntEquals(ct, 0, fitsbin_write_items(out, &chunk, outdata + (N-1) - i, 1)); } CuAssertIntEquals(ct, 0, fitsbin_fix_chunk_header(out, &chunk)); CuAssertIntEquals(ct, fitsbin_close(out), 0); fitsbin_chunk_clean(&chunk); // writing shouldn't affect the data values for (i=0; i<N; i++) { CuAssertIntEquals(ct, outdata[i], i*i); } in = fitsbin_open(fn); CuAssertPtrNotNull(ct, in); fitsbin_chunk_init(&chunk); chunk.tablename = "test2"; fitsbin_add_chunk(in, &chunk); chunk.tablename = "test2B"; fitsbin_add_chunk(in, &chunk); CuAssertIntEquals(ct, 0, fitsbin_read(in)); ch = fitsbin_get_chunk(in, 0); CuAssertIntEquals(ct, 1, ch->itemsize); CuAssertIntEquals(ct, N * sizeof(double), ch->nrows); indata = ch->data; CuAssertPtrNotNull(ct, indata); CuAssertIntEquals(ct, 0, memcmp(outdata, indata, sizeof(outdata))); ch = fitsbin_get_chunk(in, 1); CuAssertIntEquals(ct, sizeof(double), ch->itemsize); CuAssertIntEquals(ct, N, ch->nrows); indata = ch->data; CuAssertPtrNotNull(ct, indata); for (i=0; i<N; i++) { CuAssertDblEquals(ct, outdata[N-1 - i], indata[i], 1e-10); } CuAssertIntEquals(ct, 0, fitsbin_close(in)); }
void test_inmemory_fitsbin_2(CuTest* ct) { fitsbin_t* fb; int i; int N = 6; double outdata[6]; double* indata; fitsbin_chunk_t chunk; fitsbin_chunk_t* ch; fb = fitsbin_open_in_memory(); CuAssertPtrNotNull(ct, fb); CuAssertIntEquals(ct, 0, fitsbin_write_primary_header(fb)); for (i=0; i<N; i++) { outdata[i] = i*i; } fitsbin_chunk_init(&chunk); chunk.tablename = "test2"; chunk.itemsize = 1; //chunk.nrows = N * sizeof(double); chunk.data = outdata; CuAssertIntEquals(ct, 0, fitsbin_write_chunk_header(fb, &chunk)); for (i=0; i<N; i++) { CuAssertIntEquals(ct, 0, fitsbin_write_items(fb, &chunk, &outdata[i], sizeof(double))); } CuAssertIntEquals(ct, 0, fitsbin_fix_chunk_header(fb, &chunk)); fitsbin_chunk_reset(&chunk); chunk.tablename = "test2B"; chunk.itemsize = sizeof(double); //chunk.nrows = N; chunk.data = outdata; CuAssertIntEquals(ct, 0, fitsbin_write_chunk_header(fb, &chunk)); for (i=0; i<N; i++) { CuAssertIntEquals(ct, 0, fitsbin_write_items(fb, &chunk, outdata + (N-1) - i, 1)); } CuAssertIntEquals(ct, 0, fitsbin_fix_chunk_header(fb, &chunk)); fitsbin_chunk_clean(&chunk); // writing shouldn't affect the data values for (i=0; i<N; i++) { CuAssertIntEquals(ct, outdata[i], i*i); } CuAssertIntEquals(ct, 0, fitsbin_switch_to_reading(fb)); fitsbin_chunk_init(&chunk); chunk.tablename = "test2"; fitsbin_add_chunk(fb, &chunk); chunk.tablename = "test2B"; fitsbin_add_chunk(fb, &chunk); CuAssertIntEquals(ct, 0, fitsbin_read(fb)); ch = fitsbin_get_chunk(fb, 0); CuAssertIntEquals(ct, 1, ch->itemsize); CuAssertIntEquals(ct, N * sizeof(double), ch->nrows); indata = ch->data; CuAssertPtrNotNull(ct, indata); CuAssertIntEquals(ct, 0, memcmp(outdata, indata, sizeof(outdata))); ch = fitsbin_get_chunk(fb, 1); CuAssertIntEquals(ct, sizeof(double), ch->itemsize); CuAssertIntEquals(ct, N, ch->nrows); indata = ch->data; CuAssertPtrNotNull(ct, indata); for (i=0; i<N; i++) { CuAssertDblEquals(ct, outdata[N-1 - i], indata[i], 1e-10); } CuAssertIntEquals(ct, 0, fitsbin_close(fb)); }
static int write_to_file(startree_t* s, const char* fn, anbool flipped, FILE* fid) { bl* chunks; il* wordsizes = NULL; int i; kdtree_fits_t* io = NULL; // just haven't bothered... assert(!(flipped && fid)); if (fn) { io = kdtree_fits_open_for_writing(fn); if (!io) { ERROR("Failed to open file \"%s\" for writing kdtree", fn); return -1; } } if (flipped) { if (kdtree_fits_write_tree_flipped(io, s->tree, s->header)) { ERROR("Failed to write (flipped) kdtree to file \"%s\"", fn); return -1; } } else { if (fid) { if (kdtree_fits_append_tree_to(s->tree, s->header, fid)) { ERROR("Failed to write star kdtree"); return -1; } } else { if (kdtree_fits_write_tree(io, s->tree, s->header)) { ERROR("Failed to write kdtree to file \"%s\"", fn); return -1; } } } if (flipped) wordsizes = il_new(4); chunks = get_chunks(s, wordsizes); for (i=0; i<bl_size(chunks); i++) { fitsbin_chunk_t* chunk = bl_access(chunks, i); if (!chunk->data) continue; if (flipped) kdtree_fits_write_chunk_flipped(io, chunk, il_get(wordsizes, i)); else { if (fid) { kdtree_fits_write_chunk_to(chunk, fid); } else { kdtree_fits_write_chunk(io, chunk); } } fitsbin_chunk_clean(chunk); } bl_free(chunks); if (flipped) il_free(wordsizes); if (io) kdtree_fits_io_close(io); return 0; }