📄 read_data.c
字号:
doubletag_sig_dur(s) Signal *s;{ if (s) { if (s->end_time > s->start_time) return (s->end_time - s->start_time); fprintf(stderr, "tag_sig_dur entered with end_time unset (%s)\n", s->name); return (100.0); }}/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */install_tag_fea_methods(s) Signal *s;{ if (s && s->utils) { extern int tag_write_data(); s->utils->index_to_time = tag_index_to_time; s->utils->time_to_index = tag_time_to_index; s->utils->buf_end = tag_buf_end; s->utils->buf_start = tag_buf_start; s->utils->sig_dur = tag_sig_dur; s->utils->write_data = tag_write_data; }}#define D_SAMNO ((double) (buf->tag - very_first_tag))/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ *//* Read data from ESPS TAGGED FEA file. */get_esps_fea_tag(s, start, page_size) Signal *s; double start, page_size;{ int i, j, n, dim; int old_size, loc, sam_to_skip, dstart, first_tag, very_first_tag; struct fea_data *buf; /* FEA record */ caddr_t *ptr; /* pointers to field elements in buf */ caddr_t *spp = (caddr_t *) s->data; FILE *strm = s->header->strm; struct header *hdr = s->header->esps_hdr; int max_buff_size = max_buff_bytes / intern_size_rec(hdr); double get_genhd_val(), ssf; /* "source file" sample * frequency */ dim = s->dim; old_size = s->buff_size; if (!allo_esps_fea(hdr, dim, &buf, &ptr)) { fprintf(stderr, "%s: trouble allocating FEA record & pointer array.\n", "get_esps_fea_tag"); return FALSE; } ssf = get_genhd_val("src_sf", hdr, -1.0); if (ssf < 0.0) { fprintf(stderr, "No vald source sampling frequency found in %s; using 8k\n", s->name); ssf = 8000.0; } very_first_tag = get_genhd_val("start", hdr, 0.0); if (fseek(strm, (long) s->bytes_skip, 0)) { fprintf(stderr, "get_esps_fea_tag: Can't skip header of tagged FEA file.\n"); return FALSE; } start -= s->start_time; /* to simplify arithmetic for frame counting */ if (start < 0.0) start = 0.0; /* First, seek to the start of interesting data. */ sam_to_skip = -1; do { dstart = ftell(strm); /* so we can return to start of data */ if (get_fea_rec(buf, hdr, strm) == EOF) { fprintf(stderr, "Problems seeking to start of data\n"); return (FALSE); } if (!very_first_tag && (sam_to_skip < 0)) { very_first_tag = buf->tag; *add_genhd_l("start", (long *) NULL, 1, hdr) = very_first_tag; } sam_to_skip++; } while ((D_SAMNO / ssf) < start); first_tag = buf->tag; /* * For now take the brute-force approach of seeking once to determine the * number of records required, then again to actually get the data... */ fseek(strm, (long) dstart, 0); loc = 0; do { if (get_fea_rec(buf, hdr, strm) == EOF) { if (debug_level) fprintf(stderr, "EOF when counting records in get_esps_fea_tag()\n"); s->end_time = s->start_time + (D_SAMNO / ssf); s->file_size = sam_to_skip + loc; break; } loc++; } while ((D_SAMNO / ssf) < (start + page_size)); if (loc <= max_buff_size) s->buff_size= loc; else { s->buff_size = max_buff_size; fprintf(stderr, "%s: read request exceeds max_buff_size; limiting to %d recs.\n", "get_esps_fea_tag", s->buff_size); } if (!s->buff_size) { fprintf(stderr, "Problems when counting records in get_esps_fea_tag()\n"); return (FALSE); } /* If end_time not set, scan the file to determine last-record's tag. */ if (s->end_time < s->start_time) { while (get_fea_rec(buf, hdr, strm) != EOF) loc++; s->end_time = s->start_time + D_SAMNO / ssf; s->file_size = sam_to_skip + loc; } fseek(strm, (long) dstart, 0); if (s->data && s->x_dat && (old_size == s->buff_size) && (sam_to_skip == s->start_samp)) return TRUE; s->start_samp = sam_to_skip; /* Generate an average frame rate in case it's needed somewhere. */ s->freq = (ssf * (double) (s->start_samp + s->buff_size) / (buf->tag - very_first_tag)); if (!spp) { spp = (caddr_t *) malloc(dim * sizeof(caddr_t)); if (!spp) { fprintf(stderr, "get_esps_fea: Can't allocate data pointer array.\n"); free((char *) ptr); return FALSE; } for (i = 0; i < dim; i++) spp[i] = NULL; old_size = 0; } s->data = (caddr_t) spp; if (old_size < s->buff_size) { if (s->x_dat) free(s->x_dat); if (!(s->x_dat = (double *) malloc(s->buff_size * sizeof(double)))) { fprintf(stderr, "Can't allocate time array in get_esps_fea_tag()\n"); return FALSE; } for (i = 0; i < dim; i++) /* Allocate an array for each element. */ { int typsize = sig_type_size(s->types[i]); if (typsize == 0) { fprintf(stderr, "get_esps_fea_tag: Unknown data type %d.\n", s->types[i]); return FALSE; break; } if (spp[i]) free((char *) spp[i]); if (!(spp[i] = (caddr_t) malloc(s->buff_size * typsize))) { fprintf(stderr, "get_esps_fea_tag: Can't allocate data array.\n"); while (--i >= 0) free((char *) spp[i]); free(s->data); s->data = NULL; s->buff_size = 0; free((char *) ptr); return FALSE; } } } loc = 0; while (loc < s->buff_size) { if (get_fea_rec(buf, hdr, strm) == EOF) { fprintf(stderr, "Weird error in get_fea_rec in get_esps_fea_tag\n"); return (FALSE); } else { s->x_dat[loc] = s->start_time + (D_SAMNO / ssf); for (i = 0; i < dim; i++) switch (s->types[i]) { case P_SHORTS: case P_USHORTS: ((short *) spp[i])[loc] = *(short *) ptr[i]; break; case P_INTS: case P_UINTS: ((int *) spp[i])[loc] = *(int *) ptr[i]; break; case P_FLOATS: ((float *) spp[i])[loc] = *(float *) ptr[i]; break; case P_DOUBLES: ((double *) spp[i])[loc] = *(double *) ptr[i]; break; case P_CHARS: case P_UCHARS: ((char *) spp[i])[loc] = *(char *) ptr[i]; break; } loc++; } } free((char *) ptr); free_fea_rec(buf); return TRUE;}/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ *//* * Read ascii signals from a file into double arrays. Convert from double to * the desired internal type. This routine assumes all vector elements are * on one line ane all lines are terminated with '\n'. A maximum of * A_BUF_SIZE-1 characters are permitted per line (i.e. per sample). */get_ascii(s, start, page_size) Signal *s; double start, page_size;{#define A_BUF_SIZE 8000 FILE *fp, *fdopen(); char bufin[A_BUF_SIZE], *cp, *get_next_item(); register int i, j, dim; double **spp; int sam_to_skip, old_size; int max_buff_size = max_buff_bytes / (s->dim * sizeof(double)); sam_to_skip = ((start - s->start_time) * s->freq) + 0.5; dim = s->dim; old_size = s->buff_size; s->buff_size = (int) (0.5 + (page_size * s->freq)); if (s->buff_size > max_buff_size) { s->buff_size = max_buff_size; fprintf(stderr, "%s: read request exceeds max_buff_size; limiting to %f sec.\n", "get_ascii", ((double) s->buff_size) / s->freq); /* * fprintf(stderr,"get_ascii: large file -- using buff size * %d\n",s->buff_size); */ } if (s->data && (old_size == s->buff_size) && (sam_to_skip == s->start_samp)) return (TRUE); s->start_samp = sam_to_skip; fp = fdopen(s->file, "r"); if (!fseek(fp, s->bytes_skip, 0)) { while (sam_to_skip) { if (!fgets(bufin, A_BUF_SIZE, fp)) { fprintf(stderr, "Error while seeking in ascii file in get_ascii()\n"); fclose(fp); close_sig_file(s); return (FALSE); } sam_to_skip--; } if (!(spp = (double **) s->data)) { spp = (double **) malloc(sizeof(double *) * dim); for (i = 0; i < dim; i++) spp[i] = NULL; old_size = 0; } if (spp) { s->data = (caddr_t) spp; for (i = 0; i < dim; i++) /* allocate an array for each dim. */ if (old_size != s->buff_size) { if (spp[i]) free(spp[i]); if (!(spp[i] = (double *) malloc(s->buff_size * sizeof(double)))) { fprintf(stderr, "Can't allocate data array in get_ascii()\n"); while (--i >= 0) free(spp[i]); free(spp); fclose(fp); close_sig_file(s); return (FALSE); } } for (j = 0; j < s->buff_size; j++) { if (fgets(bufin, A_BUF_SIZE, fp)) { cp = bufin; for (i = 0; i < dim; i++) { if (!sscanf(cp, "%lf", &(spp[i][j]))) spp[i][j] = 0.0; cp = get_next_item(cp); } } else { if (j) { s->buff_size = j; /* also forces loop termination */ check_file_size(s, TRUE); } else { fprintf(stderr, "Error while reading ascii input\n"); fclose(fp); close_sig_file(s); return (FALSE); } } } fclose(fp); close_sig_file(s); if ((s->type & VECTOR_SIGNALS) != P_DOUBLES) { int type; Signal *s2, *convert(); type = s->type; s->type = P_DOUBLES; if (s2 = (Signal *) convert(s, type)) { spp = (double **) s2->data; s2->data = s->data; s->data = (caddr_t) spp; s->type = type; free_signal(s2); } else { fprintf(stderr, "Couldn't convert from doubles to type %d in get_ascii()\n", type); return (FALSE); } } check_file_size(s, FALSE); return (TRUE); } else fprintf(stderr, "Can't allocate buffer pointer array in get_ascii()\n"); } else fprintf(stderr, "Can't fseek to data start in get_ascii()\n"); fclose(fp); close_sig_file(s); return (FALSE);}/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */voidcheck_file_size(s, flag) Signal *s; int flag; /* if non-zero, allow new file_size */{ if (!s) return; if (flag && ((s->start_samp + s->buff_size) < s->file_size)) { s->file_size = s->start_samp + s->buff_size; fprintf(stderr, "Adjusting file size of %s to %d samples\n", s->name, s->file_size); } /* adjust end time if necessary */ if ((int) s->freq && ((s->start_time + ((double) s->file_size) / s->freq) != s->end_time)) s->end_time = s->start_time + ((double) s->file_size) / s->freq;}/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */#define READIT(type) { \ type *data, **spp, *buf, *p, *q; \ if(!(spp = (type**)s->data)) { \ spp = (type**)malloc(sizeof(type*) * dim); \ for(i=0; i < dim; i++) spp[i] = NULL; \ old_size = 0; \ } \ if(spp) { \ s->data = (caddr_t)spp; \ for(i=0; i < dim; i++) /* allocate an array for each dim. */ \ if(old_size != s->buff_size) { \ if(spp[i]) free(spp[i]); \ if(!(spp[i] = (type*)malloc(s->buff_size * sizof))) { \ fprintf(stderr,"Can't allocate data array in get_binary()\n"); \ while(--i >= 0) free(spp[i]); \ free(spp); \ s->data = NULL; \ return(FALSE); \ } \ } \ if(dim > 1) { \ if(!(buf = (type*)malloc(buf_bytes))) { \ free(spp); \ fprintf(stderr,"Can't allocate data buffer in get_binary()\n"); \ return(FALSE); \ } \ loc = 0; \ do { \ int bytes_left = bytes_per_sam * (s->buff_size - loc); \ if (buf_bytes > bytes_left) buf_bytes = bytes_left; \ if((nread = read(s->file,buf,buf_bytes)) >= bytes_per_sam) { \ nsread = nread/bytes_per_sam; \ for(i=0; i < dim; i++) \ for(j=0, p = spp[i] + loc, q = buf + i; j++ < nsread; \ q += dim) *p++ = *q; \ loc += nsread; \ } \ if(nread != buf_bytes) { \ s->buff_size = loc; \ truncate = 1; \ break; \ } \ } while(loc < s->buff_size); \ free(buf); \ } else { /* one dimensional binary data */ \ if((nread = read(s->file,(char*)spp[0],nbytes)) != nbytes) { \ truncate = 1; \ if(! (s->buff_size = nread/sizof)) { \ fprintf(stderr,"Unable to read any data in get_binary()\n"); \ free(spp[0]); \ free(spp); \ s->data = NULL; \ return(FALSE); \ } \ } \ } \ s->start_samp = sam_to_skip; \ check_file_size(s,truncate); \ return(TRUE); \ } else \ fprintf(stderr,"Can't allocate data pointer array in get_binary()\n"); \ }/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */get_binary(s, start, page_size, sizof) Signal *s; double start, page_size; int sizof;{ register int i, j, dim, nsread, bsize = 500; int sam_to_skip, nbytes, old_size, bytes_per_sam, seek, nelem, loc, buf_bytes, nread; /* max buffer size per channel */ int max_buff_size = max_buff_bytes / (s->dim * sizof); int truncate = 0;/* flag for too-short files */ sam_to_skip = ((start - s->start_time) * s->freq) + 0.5; dim = s->dim; old_size = s->buff_size; s->buff_size = (int) (0.5 + (page_size * s->freq)); if (s->buff_size > max_buff_size) { s->buff_size = max_buff_size; fprintf(stderr, "%s: read request exceeds max_buff_size; limiting to %f sec.\n", "get_binary", ((double) s->buff_size) / s->freq); } if (s->data && (old_size == s->buff_size) && (sam_to_skip == s->start_samp)) return (TRUE); bytes_per_sam = dim * sizof; seek = s->bytes_skip + (sam_to_skip * bytes_per_sam); if (s->file_size < (sam_to_skip + s->buff_size)) { /* is the file really * that small? */#ifndef OS5 s->file_size = (lseek(s->file, 0, L_XTND) - s->bytes_skip) / bytes_per_sam;#else s->file_size = (lseek(s->file, 0, SEEK_END) - s->bytes_skip) / bytes_per_sam;#endif if (s->file_size <= sam_to_skip) { fprintf(stderr, "File %s too small to reach requested segment at time %g\n", s->name, start); return (FALSE); } } /* * I hate this end_time kluge; excellent example of overspecification! dt */ check_file_size(s, FALSE);#ifndef OS5 if (lseek(s->file, seek, L_SET) == seek) {#else if (lseek(s->file, seek, SEEK_SET) == seek) {#endif nelem = dim * s->buff_size; nbytes = sizof * nelem; buf_bytes = bytes_per_sam * bsize; if (buf_bytes > nbytes) buf_bytes = nbytes; switch (sizof) { case sizeof(char): READIT(unsigned char); break; case sizeof(short): READIT(short); break; case sizeof(int): READIT(int); break;#ifdef FLOATS_AND_LONGS_DIFFER case sizeof(float): READIT(float); break;#endif case sizeof(double): READIT(double); break; default: fprintf(stderr, "Unknown data type (size) in get_binary()\n"); break; } } else fprintf(stderr, "Can't seek to data segment start in get_binary()\n"); return (FALSE);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -