📄 buffer.c
字号:
/* * Ray2mesh : software for geophysicists. * Compute various scores attached to the mesh cells, based on geometric information that rays bring when the traverse the cell. * * Copyright (C) 2003, St閜hane Genaud and Marc Grunberg * * This tool is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the Free * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */#include <ray/raydescartes.h> /* for coord_geo_t */#include <assert.h>#include "buffer.h"/** \brief Count the number of lines of file pointed to by fd. * * PRECOND : fd is open * POSTCOND : file pointer is the same as the one passed in input */int get_number_of_lines(FILE * fd){ int cnt = 0;/*fpos_t org;*/ long org; org = ftell(fd); rewind(fd); while (!feof(fd)) if (fgetc(fd) == '\n') cnt++; fseek(fd, org, SEEK_SET); return (cnt);}/** * \brief reads a given number of ray data lines from current file position. * * get_ray_data : assumes a 8 columns file, with lat,lon,depth source * lat,lon,depth dest, and raycode + ray travel-time * all angles in degrees. Given the file descriptor * fd assumed to be open, read nb file lines or less if EOF encountered. * buff is an allocated coord_geo_t zone where the coordinates read are * put. * * @param fd an open file descriptor of the data file * @param nb the number of lines to read * @param nbread the number of lines really read * @param nberr the number of lines incorrectly formatted (corrupted) * * PRECOND : fd is open and nb >=1 * **/struct raydata_t *get_raydata(FILE * fd, const int nb, int *nbread, int *nberr){ int i; struct raydata_t *raydata_buff = NULL; char phase[64]; int val; char line[1024]; assert(nb >= 1); *nbread = 0; *nberr=0; /* Alloc */ raydata_buff = (struct raydata_t *) calloc(nb, sizeof(struct raydata_t)); assert(raydata_buff); i = 0; while (i < nb ) { fgets(line, 1024, fd); if (feof(fd)) { /*fprintf(stderr, "get_raydata: end of file\n");*/ break; } val = sscanf(line, "%ld %lf %lf %lf %lf %lf %lf %s %lf\n", &(raydata_buff[i].event_id), &(raydata_buff[i].src.lat), &(raydata_buff[i].src.lon), &(raydata_buff[i].src.prof), &(raydata_buff[i].dest.lat), &(raydata_buff[i].dest.lon), &(raydata_buff[i].dest.prof), phase, &(raydata_buff[i].ray_travel_time)); switch (val) { case 9: /*if (strlen(phase) > RAYCODE_MAX_STRING_LENGTH) { fprintf(stderr, "get_raydata: FIXME raycode length\n"); }*/ strcpy(raydata_buff[i].phase, phase); i++; break; default: (*nberr)++; fprintf(stderr, "get_raydata: input ray file corrupted, read only %d/9 items (line %d)\n", val, i+(*nberr)); continue; } } /* realloc */ if (i!=0){ raydata_buff = (struct raydata_t *) realloc (raydata_buff, i * sizeof(struct raydata_t)); assert(raydata_buff); } /* number of ray in the raydata structure */ (*nbread) = i; /* conversion to radians */ for (i = 0; i < (*nbread); i++) { raydata_buff[i].src.lat *= TO_RAD; raydata_buff[i].src.lon *= TO_RAD; raydata_buff[i].dest.lat *= TO_RAD; raydata_buff[i].dest.lon *= TO_RAD; }#ifdef DEBUG fprintf(stderr, "%s:get_raydata():%d nbread = %d/%d\n", __FILE__,__LINE__, *nbread, nb);#endif return (raydata_buff);}/*----------------------------------------------------------------------*//* get_raydata_src_dest : wrapper to avoid to declare raydata_t in main *//* Given the file desc fd, the *//*----------------------------------------------------------------------*//*struct coord_geo_t * get_raydata_src_dest(FILE *fd,int nb,int *nbread){struct raydata_t *buf;struct coord_geo_t *src_dest;int i, ptr_coord=0; buf = get_raydata(fd,nb,nbread); src_dest = (struct coord_geo_t *) malloc ((*nbread)*2*sizeof (struct coord_geo_t)); assert (src_dest); for (i=0; i<*nbread ; i++ ) { src_dest[ptr_coord] = buf[i].src; src_dest[ptr_coord+1] = buf[i].dest; } free( buf ); return(src_dest);}*/void show_raydata_buffer (struct raydata_t * raydata_buff, int nbitem, char *prefix) { int i; fprintf(stderr, "nb item=%d\n", nbitem); for (i=0; i<nbitem; i++) { fprintf(stderr, "%s %ld %g %g %g %g %g %g %s %g\n", prefix, raydata_buff[i].event_id, raydata_buff[i].src.lat * TO_DEG, raydata_buff[i].src.lon * TO_DEG, raydata_buff[i].src.prof, raydata_buff[i].dest.lat * TO_DEG, raydata_buff[i].dest.lon * TO_DEG, raydata_buff[i].dest.prof, raydata_buff[i].phase, raydata_buff[i].ray_travel_time); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -