void read_data() { /* read from external file to internal table */ table_read("mydata","./workspace/eg03_mixednormal_fakedata.txt",1); PAUSE return; }
void read_data() { /* read in a 1-column table from an external file, into an internal table called mydata */ /* change the second parameter when reading real data instead of fake data */ // TODO: change the third parameter to the actual number of columns! table_read("mydata", "./workspace/mymodel_fakedata.txt", 0); /* add columns for the likelihood function to write predictions */ // table_addcolumn("mydata", <column_name>); // ... PAUSE // examine printout to visually check the proper data was read return; }
void read_data() { /* read table from external file into internal file called 'mydata' */ table_read("mydata","./workspace/EgDataz2_seedrain.txt",4); numdata = table_numrows("mydata"); /* in this case, we are going to write into our internal table, the model predictions */ /* so we need an extra columnd for that */ table_addcolumn("mydata","predlambda"); /* model predicted average seedling count */ table_addcolumn("mydata","simulatedcount"); /* model output: random draw for seedling count */ PAUSE return; }
cache_entry * load_cache_table (char *file_name) { cache_entry *cache = NULL; cache_entry **last = &cache; table *file = table_open (file_name); table_entry *entry; while ((entry = table_read (file)) != NULL) { cache_entry *new_rule = ZALLOC (cache_entry); new_rule->line = entry->line; new_rule->entry_type = name2i (entry->field[ca_type], cache_type_map); new_rule->name = entry->field[ca_derived_name]; filter_parse (&new_rule->original_fields, entry->field[ca_field_name]); new_rule->type = entry->field[ca_type_def]; /* expression is the concatenation of the remaining fields */ if (entry->nr_fields > ca_expression) { int len = 0; int chi; for (chi = ca_expression; chi < entry->nr_fields; chi++) { len += strlen (" : ") + strlen (entry->field[chi]); } new_rule->expression = NZALLOC (char, len); strcpy (new_rule->expression, entry->field[ca_expression]); for (chi = ca_expression + 1; chi < entry->nr_fields; chi++) { strcat (new_rule->expression, " : "); strcat (new_rule->expression, entry->field[chi]); } } /* insert it */ *last = new_rule; last = &new_rule->next; }
/** * read FITS file and fill 'IMAGE' structure (with headers and tables) * can't work with image stack - opens the first image met * works only with binary tables */ IMAGE* readFITS(char *filename, IMAGE **fits){ FNAME(); #define TRYRET(f, ...) do{TRYFITS(f, __VA_ARGS__); if(fitsstatus) goto returning;}while(0) fitsfile *fp; int i, j, hdunum = 0, hdutype, nkeys, keypos; int naxis; long naxes[2]; char card[FLEN_CARD]; IMAGE *img = MALLOC(IMAGE, 1); TRYRET(fits_open_file, &fp, filename, READONLY); FITSFUN(fits_get_num_hdus, fp, &hdunum); if(hdunum < 1){ WARNX(_("Can't read HDU")); fitsstatus = 1; goto returning; } // get image dimensions TRYRET(fits_get_img_param, fp, 2, &img->dtype, &naxis, naxes); if(naxis > 2){ WARNX(_("Images with > 2 dimensions are not supported")); fitsstatus = 1; goto returning; } img->width = naxes[0]; img->height = naxes[1]; DBG("got image %ldx%ld pix, bitpix=%d", naxes[0], naxes[1], img->dtype); // loop through all HDUs KeyList *list = img->keylist; int imghdu = -1; for(i = 1; !(fits_movabs_hdu(fp, i, &hdutype, &fitsstatus)); ++i){ int hdutype; TRYFITS(fits_get_hdu_type, fp, &hdutype); // types: IMAGE_HDU , ASCII_TBL, BINARY_TBL if(fitsstatus) continue; DBG("HDU type %d", hdutype); //if(hdutype == ASCII_TBL || hdutype == BINARY_TBL){ if(hdutype == BINARY_TBL){ table_read(img, fp); continue; } if(imghdu < 1) imghdu = i; TRYFITS(fits_get_hdrpos, fp, &nkeys, &keypos); if(fitsstatus) continue; //DBG("HDU # %d of %d keys", i, nkeys); for(j = 1; j <= nkeys; ++j){ FITSFUN(fits_read_record, fp, j, card); if(!fitsstatus){ if(!list_add_record(&list, card)){ /// "Не могу добавить запись в список" WARNX(_("Can't add record to list")); } //DBG("key %d: %s", j, card); } } } img->keylist = list; if(fitsstatus == END_OF_FILE){ fitsstatus = 0; }else{ fits_report_error(stderr, fitsstatus); goto returning; } if(fits_movabs_hdu(fp, imghdu, &hdutype, &fitsstatus)){ WARNX(_("Can't open image HDU #%d"), imghdu); fitsstatus = 1; goto returning; } size_t sz = naxes[0] * naxes[1]; img->data = MALLOC(double, sz); int stat = 0; TRYFITS(fits_read_img, fp, TDOUBLE, 1, sz, NULL, img->data, &stat); if(stat) WARNX(_("Found %d pixels with undefined value"), stat); DBG("ready"); #undef TRYRET returning: if(fitsstatus){ imfree(&img); } FITSFUN(fits_close_file, fp); if(fits){ FREE(*fits); *fits = img; } return img; }
/* * perform some basic tests */ static void io_test(table_t *tab_p) { int ret, bucket_n, entry_n; table_t *tab2_p; (void)printf("Performing I/O tests:\n"); (void)fflush(stdout); #if 0 { long key, data; (void)table_clear(tab_p); key = 1; data = 2; (void)table_insert(tab_p, &key, sizeof(key), &data, sizeof(data), NULL, 0); key = 3; data = 4; (void)table_insert(tab_p, &key, sizeof(key), &data, sizeof(data), NULL, 0); key = 5; data = 6; (void)table_insert(tab_p, &key, sizeof(key), &data, sizeof(data), NULL, 0); (void)table_adjust(tab_p, 0); dump_table(tab_p); } #endif ret = table_info(tab_p, &bucket_n, &entry_n); if (ret != TABLE_ERROR_NONE) { (void)fprintf(stderr, "could not get info of table: %s\n", table_strerror(ret)); exit(1); } (void)printf("Table we are writing has %d buckets and %d entries\n", bucket_n, entry_n); /* * dump the table to disk */ int pmode = 0640; #ifdef win32 pmode = _S_IREAD | _S_IWRITE; #endif (void)unlink(TABLE_FILE); ret = table_write(tab_p, TABLE_FILE, pmode); if (ret != TABLE_ERROR_NONE) { (void)fprintf(stderr, "could not write table to '%s': %s\n", TABLE_FILE, table_strerror(ret)); exit(1); } #if 0 dump_table(tab_p); #endif /* * now read back in the table */ tab2_p = table_read(TABLE_FILE, &ret); if (tab2_p == NULL) { (void)fprintf(stderr, "could not read in file '%s': %s\n", TABLE_FILE, table_strerror(ret)); exit(1); } (void)printf("Testing table-read...\n"); if (test_eq(tab_p, tab2_p, 0)) { (void)printf(" equal.\n"); } else { (void)printf(" NOT equal.\n"); } ret = table_free(tab2_p); if (ret != TABLE_ERROR_NONE) { (void)fprintf(stderr, "could not free read table: %s\n", table_strerror(ret)); exit(1); } /* * mmap in the table */ tab2_p = table_mmap(TABLE_FILE, &ret); if (tab2_p == NULL) { (void)fprintf(stderr, "could not mmap file '%s': %s\n", TABLE_FILE, table_strerror(ret)); exit(1); } (void)printf("Testing table-mmap...\n"); if (test_eq(tab2_p, tab_p, 0)) { (void)printf(" equal.\n"); } else { (void)printf(" NOT equal.\n"); } ret = table_munmap(tab2_p); if (ret != TABLE_ERROR_NONE) { (void)fprintf(stderr, "could not munmap file '%s': %s\n", TABLE_FILE, table_strerror(ret)); exit(1); } }