bool singleFitness(int individual) { int sOnes, rOnes, sZeroes, rZeroes; int i; init_line(population + individual); line_count(&sOnes, &sZeroes); if (sZeroes > sOnes) { sZeroes = sZeroes + sOnes; sOnes = 0; } else { sOnes = sZeroes + sOnes; sZeroes = 0; } for (i=0; i<STEPS_PER_FITNESS; i++) { if (!line_is_stable()) { line_next(); #ifdef FITNESS_DEBUG line_print(); #endif } else { break; } } line_count(&rOnes, &rZeroes); bool success = sZeroes == rZeroes && sOnes == rOnes; #ifdef FITNESS_DEBUG printf("Success: %s\n\n", success? "true" : "false"); #endif return success; }
int main(int argc, char *argv[]) { /* You can modify this file as needed, for example to test different * files. If you run on command line, this function optionally reads * the filename from command line, if one is included. */ char *filename = "testi.txt"; if (argc > 1) { filename = argv[1]; } printf("Line count: %d\n", line_count(filename)); printf("Word count: %d\n", word_count(filename)); }
int CsvInterface::readPoints(std::ifstream &in, char delim, std::vector<GeoLib::Point*> &points, std::array<std::size_t, 3> const& column_idx) { std::array<std::size_t, 3> order = {{ 0, 1, 2 }}; std::sort(order.begin(), order.end(), [&column_idx](std::size_t idx1, std::size_t idx2) {return column_idx[idx1] < column_idx[idx2];}); std::array<std::size_t, 3> const column_advance = {{ column_idx[order[0]], column_idx[order[1]] - column_idx[order[0]], column_idx[order[2]] - column_idx[order[1]] }}; std::string line; std::size_t line_count(0); std::size_t error_count(0); std::list<std::string>::const_iterator it; while ( getline(in, line) ) { line_count++; std::list<std::string> const fields = BaseLib::splitString(line, delim); if (fields.size() < column_idx[order[2]]+1) { ERR ("Line %d contains not enough columns of data. Skipping line...", line_count); error_count++; continue; } std::array<double, 3> point; it = fields.begin(); try { std::advance(it, column_advance[0]); point[order[0]] = std::stod(*it); std::advance(it, column_advance[1]); point[order[1]] = std::stod(*it); std::advance(it, column_advance[2]); point[order[2]] = (column_idx[1] == column_idx[2]) ? 0 : std::stod(*it); points.push_back(new GeoLib::Point(point[0], point[1], point[2])); } catch (const std::invalid_argument&) { ERR ("Error converting data to coordinates in line %d.", line_count); error_count++; } } return error_count; }
t_board *build_board(char *str) { int y; int i; t_board *brd; i = 0; y = 0; if (!(brd = (t_board*)malloc(sizeof(t_board)))) return (NULL); brd->size_y = line_count(str); if (!(brd->map = (char**)malloc(sizeof(char*) * brd->size_y))) return (NULL); brd->size_x = line_size(str); while (y < brd->size_y) { brd->map[y] = &str[brd->size_x * y + y]; y++; } return (brd); }
int CsvInterface::readPoints(std::string const& fname, char delim, std::vector<GeoLib::Point*> &points) { std::ifstream in(fname.c_str()); if (!in.is_open()) { ERR ("CsvInterface::readPoints(): Could not open file %s.", fname.c_str()); return -1; } std::string line; getline(in, line); std::size_t line_count(0); std::size_t error_count(0); std::list<std::string>::const_iterator it; while ( getline(in, line) ) { line_count++; std::list<std::string> const fields = BaseLib::splitString(line, delim); if (fields.size() < 3) { ERR ("Line %d contains not enough columns of data. Skipping line...", line_count); error_count++; continue; } it = fields.begin(); std::array<double, 3> point; try { point[0] = std::stod(*it); point[1] = std::stod(*(++it)); point[2] = std::stod(*(++it)); points.push_back(new GeoLib::Point(point[0], point[1], point[2])); } catch (const std::invalid_argument&) { ERR ("Error converting data to coordinates in line %d.", line_count); } } return error_count; }
void main(int argc, char *argv[]) { FILE *fp; char fr4[400]="fr4.ll"; char fr8[400]="fr8.ll"; char indir[400]="/home/tempbvm/cbr8/metdata/WIND1961-1990/"; char outdir[400]="/home/tempbvm/cbr2/metdata/quarter_1961-1990/data_"; char outfile[400]; char LATLON[50]; int i,j; int cells8; int cells4; int param, Nparam; float FR4[MAX][3]; float FR8[MAX][3]; float AFR[STEPS][MAX_PARAM]; float BFR[STEPS][MAX_PARAM]; float CFR[STEPS][MAX_PARAM]; float DFR[STEPS][MAX_PARAM]; float NEW[STEPS][MAX_PARAM]; float res8; float res4; float lat,lon; float Alat,Blat,Clat,Dlat; float Alon,Blon,Clon,Dlon; float Afr,Bfr,Cfr,Dfr; int Aok,Bok,C*k,Dok; float sum_fraction; int valid_cells; if(argc!= 7) { fprintf(stderr,"Usage: %s <old fraction xyz> <old fraction data dir> <new fraction xyz> <new fraction data dir> <old resolution> <number of parameters>\n",argv[0]); fprintf(stderr,"\t<old fraction xyz> is the 3 coloumn fraction file for the old resolution.\n"); fprintf(stderr,"\t<old fraction data dir> is the directory for the forcing data for the old resolution.\n"); fprintf(stderr,"\t<new fraction xyz> is the 3 coloumn fraction file for the new resolution.\n"); fprintf(stderr,"\t<new fraction data dir> is the directory for the forcing data for the new resolution.\n"); fprintf(stderr,"\t<old resolution> is the old resoluation in fractions of degrees.\n"); fprintf(stderr,"\t<number of parameters> is the number of parameters (data types / columns) in the forcing file.\n"); exit(0); } strcpy(fr4,argv[1]); strcpy(indir,argv[2]); strcpy(fr8,argv[3]); strcpy(outdir,argv[4]); res8 = atof(argv[5]); /* Remember to change this one */ res4 = 2*res8; Nparam = atoi(argv[6]); cells4 = line_count(fr4); cells8 = line_count(fr8); printf("File %s has %d lines\n",fr4,cells4); printf("File %s has %d lines\n",fr8,cells8); read_data(fr4,FR4,cells4); read_data(fr8,FR8,cells8); for (i=0;i<cells4;i++) { for(j=0;j<STEPS;j++) /* Setting initial values */ { for(param=0;param<Nparam;param++) { AFR[j][param] = 0.0; BFR[j][param] = 0.0; CFR[j][param] = 0.0; DFR[j][param] = 0.0; } } lat = FR4[i][0]; lon = FR4[i][1]; Alat = lat + res8/2; Alon = lon - res8/2; Blat = lat + res8/2; Blon = lon + res8/2; Clat = lat - res8/2; Clon = lon - res8/2; Dlat = lat - res8/2; Dlon = lon + res8/2; Afr = exist_file(FR8,Alat,Alon,cells8); Bfr = exist_file(FR8,Blat,Blon,cells8); Cfr = exist_file(FR8,Clat,Clon,cells8); Dfr = exist_file(FR8,Dlat,Dlon,cells8); Aok = Bok = C*k = Dok = 0; if(Afr > 0.0) Aok = 1; if(Bfr > 0.0) Bok = 1; if(Cfr > 0.0) C*k = 1; if(Dfr > 0.0) Dok = 1; sum_fraction = Afr + Bfr + Cfr + Dfr; valid_cells = Aok + Bok + C*k + Dok; printf("lat %.4f lon %.4f valid_cells %d sum_fraction %.3f\n", lat, lon, valid_cells, sum_fraction); printf("Afr %.3f Bfr %.3f Cfr %.3f Dfr %.3f\n",Afr,Bfr,Cfr,Dfr); /* Reading files */ if(Afr > 0) read_metdata(indir,AFR,Alat,Alon,Nparam); if(Bfr > 0) read_metdata(indir,BFR,Blat,Blon,Nparam); if(Cfr > 0) read_metdata(indir,CFR,Clat,Clon,Nparam); if(Dfr > 0) read_metdata(indir,DFR,Dlat,Dlon,Nparam); for(j=0;j<STEPS;j++) /* Calculating new values */ { for(param=0;param<Nparam;param++) { NEW[j][param] = (AFR[j][param]*Afr + BFR[j][param]*Bfr + CFR[j][param]*Cfr + DFR[j][param]*Dfr) /sum_fraction; } } /* for(param=0;param<Nparam;param++) */ /* printf("%.4f",NEW[0][param]); */ /* printf("\n"); */ strcpy(outfile,outdir); sprintf(LATLON,"%.4f_%.4f",lat,lon); strcat(outfile,LATLON); printf("outfile %s\n",outfile); if((fp = fopen(outfile,"w"))==NULL) {printf("Cannot open file %s \n",outfile);exit(0);} for(j=0;j<STEPS;j++) { for(param=0;param<Nparam;param++) fprintf(fp,"%.2f",NEW[j][param]); fprintf(fp,"\n"); } fclose(fp); } }/* END MAIN ******************************************************************/
/* Open the configuration file, and parse it line by line. */ int hcfg_load(hcfg_t *cfg, const char *filename) { FILE *fd; char buf[4096]; char *head, *next, *tail, *key, *val; int linenum = 1; if (strcmp(filename, "-") == 0) fd = stdin; else { fd = fopen(filename, "r"); if (!fd) { perror("Error parsing configuration data"); return -1; } } head = buf; tail = buf; while (!feof(fd)) { int count; tail += fread(tail, sizeof(char), sizeof(buf) - (tail - buf + 1), fd); *tail = '\0'; while (head < tail) { count = line_count(head); if (count == 0 && !feof(fd)) break; next = hcfg_parse(head, &key, &val); if (!next) break; if (key && val) { keyval_t *entry = hash_find(cfg, key); if (entry != NULL && entry->key != NULL) { fprintf(stderr, "Warning: Line %d: Redefinition of" " configuration key %s.\n", linenum, key); } if (hash_insert(cfg, key, val) != 0) { fprintf(stderr, "Error: Internal hash table error.\n"); fclose(fd); return -1; } } linenum += count; head = next; } /* Parse error detected. */ if (!next) break; /* Full buffer with no newline. */ if (tail - head == sizeof(buf) - 1) break; /* Move remaining data to front of buffer. */ if (head != buf) { next = buf; while (head < tail) *(next++) = *(head++); tail = next; head = buf; } } if (fclose(fd) != 0) { perror("Error parsing configuration data"); return -1; } if (head != tail) { fprintf(stderr, "Parse error in file %s line %d: ", filename, linenum); if (tail - head == sizeof(buf) - 1) fprintf(stderr, "Line buffer overflow.\n"); else if (key == NULL) fprintf(stderr, "Invalid key string.\n"); else if (val == NULL) fprintf(stderr, "No key/value separator character (=).\n"); return -1; } return 0; }
int main(void) { int num = line_count(); print_lines(num); return 0; }
u32 CInifile::line_count ( const shared_str& S ) { return line_count(*S); }
void *read_cache(const char *cachefile, int *size) { int fd = open( cachefile, O_RDONLY ); int fd_err = errno; int ret=0; FILE *fp=NULL; char *line = NULL; size_t len = 0; ssize_t read = 0; len = MAXLEN * sizeof(char); line = malloc( len ); if(!line) goto out_free; struct ut_cache **cache=NULL; int buf_size = 0; int i = 0; if(fd == -1){ switch(fd_err){ case ENOENT: error("cache file was not found.\n"); return NULL; break; case EACCES: error("unable to open cache file for reading. Check permissions.\n"); return NULL; break; default: fprintf(stderr, "error: %d\n", fd_err); error("unknown error occurred trying to open cache file\n"); return NULL; } } /* set up file pointer for streams */ if( (fp = fdopen( fd, "r" )) == NULL ){ ret = error("unable to create FILE *\n"); goto out_free; } int num_lines = line_count( fp ); /* allocate struct for each line in the file */ buf_size = num_lines; if( cache == NULL ) { cache = malloc(buf_size*sizeof(struct ut_cache *)); /* check for out of memory error */ if(cache != NULL) for(i = 0; i < buf_size; i++) { cache[i] = malloc(sizeof(struct ut_cache)); if(cache[i] == NULL) { *size = error( "out of memory\n" ); goto out_free; } memset(cache[i], 0, sizeof(struct ut_cache)); } }else error("got non NULL cache pointer\n"); char *equals; for( i = 0; ( read = getdelim(&line, &len, delim, fp)) != -1; i++ ){ if( NULL == (equals = strchr( line, '=' ))){ i--; continue; } chomp(line); memcpy( cache[i]->cfile, line, (sizeof(char) * ( equals - line ))); memcpy( cache[i]->gfile, (equals+1), sizeof((cache[i]->gfile))); asign_dir(cache[i]); } *size = i; out_free: if( line ) free(line); if(fp != NULL) fclose(fp); else close(fd); if( *size < 0 ){ free(cache); cache = NULL; ret = -1; } return (ret == -1)? NULL: cache; }
int main(int argc, char *argv[]) { char opt, *framename, *listname, *outname, *e_outname, buffer[BUFSIZ]; int n, row, col, cX, cY, npatch, npxi, npxt, nprof; int flag_autoname, flag_verbose, status, bitmask[YAR][XAR]; long xsize, ysize, lowleft[DIM], upright[DIM], increment[DIM] = {1L, 1L}, framesize[DIM]; float mask_val, cutoff, framefragment[YAR][XAR]; struct star* patchstars; FILE *listfile; fitsfile *frame, *maskedframe; extern char *optarg; extern int opterr; framename = NULL; listname = NULL; outname = NULL; mask_val = DEFAULT_MASK; cutoff = DEFAULT_CUTOFF; flag_autoname = 1; flag_verbose = 0; opterr = 0; while ((opt=getopt(argc, argv, "f:l:o:m:c:v")) != -1) { switch (opt) { case 'f': framename = optarg; break; case 'l': listname = optarg; break; case 'o': flag_autoname = 0; outname = optarg; break; case 'm': mask_val = atof(optarg); break; case 'c': cutoff = atof(optarg); break; case 'v': flag_verbose = 1; break; default: usage(argv); } } /* check mandatory parameters: */ if (framename == NULL || listname == NULL) usage(argv); if (cutoff < 0.0) { fprintf(stderr, " WARNING: invalid cut-off value (%.1f ADU), using default (%.1f ADU)\n", cutoff, DEFAULT_CUTOFF); cutoff = DEFAULT_CUTOFF; } /* read ID and coordinates from any single-lined Daophot output, detect and skip the header if necessary: */ if ((listfile=fopen(listname, "r")) == NULL) { file_read_error(listname); } else { npatch = line_count(listfile); if (has_daophot_header(listfile) == 1) { npatch -= DAO_HEADER_SIZE; line_skip(listfile, DAO_HEADER_SIZE); } if (npatch <= 0) { fclose(listfile); fprintf(stderr, " couldn't read contents of '%s', exiting.\n\n", listname); exit(EXIT_FAILURE); } patchstars = calloc(npatch, sizeof(struct star)); if (patchstars == NULL) { fclose(listfile); memory_error(); } for (n=0; n < npatch; n++) { fgets(buffer, BUFSIZ, listfile); sscanf(buffer, "%d %lf %lf", &patchstars[n].num, &patchstars[n].X, &patchstars[n].Y); } fclose(listfile); } status = 0; fits_open_file(&frame, framename, READONLY, &status); if (status != 0) { free(patchstars); file_read_error(framename); } fits_get_img_size(frame, DIM, framesize, &status); fits_print_error(status); xsize = framesize[0]; ysize = framesize[1]; printf(" %s: %ld x %ld pixels\n", framename, xsize, ysize); printf(" %s: %d stars in the list\n", listname, npatch); printf(" mask value: %.1f ADU\n", mask_val); printf(" cut-off level: %.1f ADU\n", cutoff); if (flag_autoname == 1) { outname = expand_filename(framename, "-msk", 0, 0); if (outname == NULL) { free(patchstars); fits_close_file(frame, &status); memory_error(); } } printf(" output file: %s\n", outname); /* force cfitsio to overwrite already existing file (prepend ! to the filename) */ e_outname = prepend_bang(outname); if (e_outname == NULL) { free(patchstars); fits_close_file(frame, &status); memory_error(); } /* create output (masked) frame: */ fits_create_file(&maskedframe, e_outname, &status); fits_print_error(status); free(e_outname); if (flag_autoname == 1) /* only necessary if outname was created automatically */ free(outname); fits_copy_file(frame, maskedframe, 1, 1, 1, &status); fits_print_error(status); npxt = 0; nprof = 0; /* process the stars in the list: */ for (n=0; n < npatch; n++) { if (flag_verbose == 1) printf("\n star #%d:\n", patchstars[n].num); /* cfitsio doesn't understand fractional pixels, round the coordinates to nearest integer: */ cX = rint(patchstars[n].X); cY = rint(patchstars[n].Y); /* make sure center is in the picture, skip this star if it is not: */ if (cX < 1 || cY < 1 || cX > xsize || cY > ysize) { printf(" star #%d is outside the frame (%d,%d), skipping.\n", patchstars[n].num, cX, cY); continue; } /* * in fitsio pixel index runs from 1 to N, not from 0 to N-1. * consider XAR=21 (reading 10 pixels right and left from center), XSIZE=2044. * example 1: star with x=10. fitsio will reach and try to read pixel at x=0, ILLEGAL. * example 2: star with x=2034. fitsio will reach and try to read pixel at x=2044, LEGAL. * we have to use LESS OR EQUAL when checking lower and left boundary, * for upper and right boundary only using GREATER is fine */ else if (cX <= (XAR-1)/2 || cY <= (YAR-1)/2 || cX > xsize-(XAR-1)/2 || cY > ysize-(YAR-1)/2) { printf(" star #%d is too close to the edge or partially outside the frame (%d,%d), skipping.\n", patchstars[n].num, cX, cY); continue; } if (flag_verbose == 1) printf(" coordinates of star #%d center in current frame: (%d,%d)\n", patchstars[n].num, cX, cY); /* compute the coordinates of lower left and upper right pixel: */ lowleft[0] = cX - (XAR-1)/2; lowleft[1] = cY - (YAR-1)/2; upright[0] = cX + (XAR-1)/2; upright[1] = cY + (YAR-1)/2; /* load frame fragment with the star into table: */ fits_read_subset(frame, TFLOAT, lowleft, upright, increment, 0, framefragment, 0, &status); fits_print_error(status); /* initialise the bitmask with zeros: */ for (row=0; row < YAR; row++) for (col=0; col < XAR; col++) bitmask[row][col] = 0; /* detect holes and write to bitmask: */ for (row=YAR-1; row >= 0; row--) examine_row(framefragment, bitmask, row, cutoff); for (col=0; col < XAR; col++) examine_col(framefragment, bitmask, col, cutoff); npxi = apply_bitmask(framefragment, bitmask, mask_val); npxt += npxi; if (npxi > 0) ++nprof; if (flag_verbose == 1) { printf("\n"); print_bitmask(bitmask); printf("\n star %d: %d pixels masked\n", patchstars[n].num, npxi); } /* write the modified frame subsection to output frame: */ fits_write_subset(maskedframe, TFLOAT, lowleft, upright, framefragment, &status); fits_print_error(status); } printf(" %d pixels were masked in %d stars.\n", npxt, nprof); fits_close_file(frame, &status); fits_print_error(status); fits_close_file(maskedframe, &status); fits_print_error(status); free(patchstars); return 0; }