Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pygrt/C_extension/include/grt/common/const.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ typedef int MYINT; ///< 整数
#define GRT_STR_CMPLX_LONG_FMT "%51s" ///< 与复数格式同长度的字符串输出格式(更长)
#define GRT_CMPLX_SPLIT(x) creal(x), cimag(x) ///< 用于打印复数时将实部虚部分开

#define GRT_COMMENT_HEAD '#' ///< # 号, 作为注释的字符

#define GRT_MAX(a, b) ((a) > (b) ? (a) : (b)) ///< 求两者较大值
#define GRT_MIN(a, b) ((a) < (b) ? (a) : (b)) ///< 求两者较小值

Expand Down
21 changes: 21 additions & 0 deletions pygrt/C_extension/include/grt/common/model.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,27 @@ void grt_update_mod1d_omega(GRT_MODEL1D *mod1d, MYCOMPLEX omega);
GRT_MODEL1D * grt_read_mod1d_from_file(const char *command, const char *modelpath, double depsrc, double deprcv, bool allowLiquid);


/**
* 从模型文件中判断各个量的大致精度(字符串长度),以确定浮点数输出位数
*
* @param[in] command 命令名称
* @param[in] modelpath 模型文件路径
* @param[out] diglen 每一列的最大字符串长度
*
*/
void grt_get_model_diglen_from_file(const char *command, const char *modelpath, MYINT diglen[6]);

/**
* 浮点数比较,检查模型中是否存在该速度(不论Vp,Vs)
*
* @param[in] mod1d 模型
* @param[in] vel 输入速度
* @param[in] tol 浮点数比较精度
*
* @return 是否存在
*/
bool grt_check_vel_in_mod(const GRT_MODEL1D *mod1d, const MYREAL vel, const MYREAL tol);

/**
* 计算最大最小速度(非零值)
*
Expand Down
14 changes: 7 additions & 7 deletions pygrt/C_extension/include/grt/common/sacio2.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@
/**
* 读取SAC头段变量
*
* @param command (in)当前程序命令名称
* @param name (in)SAC文件路径
* @param hd (out)SAC头段变量结构体
* @param[in] command 当前程序命令名称
* @param[in] name SAC文件路径
* @param[out] hd SAC头段变量结构体
*/
void grt_read_SAC_HEAD(const char *command, const char *name, SACHEAD *hd);


/**
* 读取SAC文件
*
* @param command (in)当前程序命令名称
* @param name (in)SAC文件路径
* @param hd (out)SAC头段变量结构体
* @param arrout (inout)预分配内存,不需要则设为NULL
* @param[in] command 当前程序命令名称
* @param[in] name SAC文件路径
* @param[out] hd SAC头段变量结构体
* @param[in,out] arrout 预分配内存,不需要则设为NULL
*
* @return 浮点数指针
*/
Expand Down
34 changes: 33 additions & 1 deletion pygrt/C_extension/include/grt/common/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,49 @@
*/
char ** grt_string_split(const char *string, const char *delim, int *size);

/**
* 指定分隔符,获得字符串的分割出的子字符串数。
* 相当于是 grt_string_split 函数的简化版本
*
* @param[in] string 原字符串
* @param[in] delim 分隔符
*
* @return 子字符串数
*/
int grt_string_ncols(const char *string, const char* delim);

/**
* 从路径字符串中找到用/或\\分隔的最后一项
*
* @param path 路径字符串指针
* @param[in] path 路径字符串指针
*
* @return 指向最后一项字符串的指针
*/
const char* grt_get_basename(const char* path);


/**
* 去除字符串首尾空白
*
* @param[in,out] str 字符串
*/
void grt_trim_whitespace(char* str);


/**
* 检查是否为注释行或空行
*
* @param[in] line 读入一行的字符串
*/
bool grt_is_comment_or_empty(const char* line);


/**
* 由于 Windows MSYS2 环境没有 getline 函数(即使定义了 _GNU_SOURCE)
* 所以这里需要使用自定义的 getline 函数,参数与 POSIX 定义相同
*/
ssize_t grt_getline(char **lineptr, size_t *n, FILE *stream);


/**
* 处理单个震中距对应的数据逆变换和SAC保存
Expand Down
4 changes: 2 additions & 2 deletions pygrt/C_extension/src/common/iostats.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ MYINT grt_extract_stats(FILE *bf0, FILE *af0){
// 打印标题
if(bf0 == NULL){
char K[20];
snprintf(K, sizeof(K), GRT_STRING_FMT, "k"); K[0]='#';
snprintf(K, sizeof(K), GRT_STRING_FMT, "k"); K[0]=GRT_COMMENT_HEAD;
fprintf(af0, "%s", K);

for(MYINT im=0; im<GRT_SRC_M_NUM; ++im){
Expand Down Expand Up @@ -107,7 +107,7 @@ MYINT grt_extract_stats_ptam(FILE *bf0, FILE *af0){

snprintf(K2, sizeof(K2), "sum_%s_%d_k", GRT_SRC_M_NAME_ABBR[im], v);
if(icol==0){
snprintf(K, sizeof(K), GRT_STRING_FMT, K2); K2[0]='#';
snprintf(K, sizeof(K), GRT_STRING_FMT, K2); K2[0]=GRT_COMMENT_HEAD;
fprintf(af0, "%s", K);
} else {
fprintf(af0, GRT_STRING_FMT, K2);
Expand Down
41 changes: 38 additions & 3 deletions pygrt/C_extension/src/common/model.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "grt/common/prtdbg.h"
#include "grt/common/attenuation.h"
#include "grt/common/colorstr.h"
#include "grt/common/util.h"

#include "grt/common/checkerror.h"

Expand Down Expand Up @@ -210,19 +211,21 @@ GRT_MODEL1D * grt_read_mod1d_from_file(const char *command, const char *modelpat

const int ncols = 6; // 模型文件有6列,或除去qa qb有四列
const int ncols_noQ = 4;
char line[1024];
int iline = 0;
double h, va, vb, rho, qa, qb;
double (*modarr)[ncols] = NULL;
h = va = vb = rho = qa = qb = 0.0;
int nlay = 0;
mod1d->io_depth = false;

while(fgets(line, sizeof(line), fp)) {
size_t len;
char *line = NULL;

while(grt_getline(&line, &len, fp) != -1) {
iline++;

// 注释行
if(line[0]=='#') continue;
if(grt_is_comment_or_empty(line)) continue;

h = va = vb = rho = qa = qb = 0.0;
MYINT nscan = sscanf(line, "%lf %lf %lf %lf %lf %lf\n", &h, &va, &vb, &rho, &qa, &qb);
Expand Down Expand Up @@ -371,11 +374,43 @@ GRT_MODEL1D * grt_read_mod1d_from_file(const char *command, const char *modelpat

fclose(fp);
GRT_SAFE_FREE_PTR(modarr);
GRT_SAFE_FREE_PTR(line);

return mod1d;
}


void grt_get_model_diglen_from_file(const char *command, const char *modelpath, MYINT diglen[6]){
FILE *fp = GRTCheckOpenFile(command, modelpath, "r");
size_t len;
char *line = NULL;

memset(diglen, 0, sizeof(MYINT)*6);

while(grt_getline(&line, &len, fp) != -1){
char *token = strtok(line, " \n");
for(MYINT i=0; i<6; ++i){
if(token == NULL) break;
diglen[i] = GRT_MAX(diglen[i], (MYINT)strlen(token));
token = strtok(NULL, " \n");
}
}

GRT_SAFE_FREE_PTR(line);
fclose(fp);
}


bool grt_check_vel_in_mod(const GRT_MODEL1D *mod1d, const MYREAL vel, const MYREAL tol){
// 浮点数比较,检查是否存在该速度值
for(MYINT i=0; i<mod1d->n; ++i){
if(fabs(vel - mod1d->Va[i])<tol || fabs(vel - mod1d->Vb[i])<tol) return true;
}
return false;
}



void grt_get_mod1d_vmin_vmax(const GRT_MODEL1D *mod1d, MYREAL *vmin, MYREAL *vmax){
*vmin = 9.0e30;
*vmax = 0.0;
Expand Down
99 changes: 99 additions & 0 deletions pygrt/C_extension/src/common/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#include "grt/common/util.h"
#include "grt/common/model.h"
Expand Down Expand Up @@ -41,6 +42,23 @@ char ** grt_string_split(const char *string, const char *delim, int *size)
}


int grt_string_ncols(const char *string, const char* delim){
int count = 0;

const char *str = string;
while (*str) {
// 跳过所有分隔符
while (*str && strchr(delim, *str)) str++;
// 如果还有非分隔符字符,增加计数
if (*str) count++;
// 跳过所有非分隔符字符
while (*str && !strchr(delim, *str)) str++;
}

return count;
}


const char* grt_get_basename(const char* path) {
// 找到最后一个 '/'
char* last_slash = strrchr(path, '/');
Expand All @@ -60,6 +78,87 @@ const char* grt_get_basename(const char* path) {
}


void grt_trim_whitespace(char* str) {
char* end;

// 去除首部空白
while (isspace((unsigned char)*str)) str++;

// 去除尾部空白
end = str + strlen(str) - 1;
while (end > str && isspace((unsigned char)*end)) end--;

// 写入终止符
*(end + 1) = '\0';
}


bool grt_is_comment_or_empty(const char* line) {
// 跳过前导空白
while (isspace((unsigned char)*line)) line++;

// 检查是否为空行或注释行
return (*line == '\0' || *line == GRT_COMMENT_HEAD);
}


ssize_t grt_getline(char **lineptr, size_t *n, FILE *stream){
if (!lineptr || !n || !stream) {
return -1;
}

char *buf = *lineptr;
size_t size = *n;
size_t len = 0;
int c;

// 如果缓冲区为空,分配初始缓冲区
if (buf == NULL || size == 0) {
size = 128;
buf = malloc(size);
if (buf == NULL) {
return -1;
}
}

// 逐字符读取直到换行符或EOF
while ((c = fgetc(stream)) != EOF) {
// 检查是否需要扩展缓冲区
if (len + 1 >= size) {
size_t new_size = size * 2;
char *new_buf = realloc(buf, new_size);
if (new_buf == NULL) {
free(buf);
return -1;
}
buf = new_buf;
size = new_size;
}

buf[len++] = c;

// 遇到换行符停止读取
if (c == '\n') {
break;
}
}

// 如果没有读取到任何字符且遇到EOF
if (len == 0 && c == EOF) {
return -1;
}

// 添加字符串终止符
buf[len] = '\0';

*lineptr = buf;
*n = size;

return len;
}




/**
* 将一条数据反变换回时间域再进行处理,保存到SAC文件
Expand Down
20 changes: 10 additions & 10 deletions pygrt/C_extension/src/dynamic/signals.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "grt/dynamic/signals.h"
#include "grt/common/const.h"
#include "grt/common/colorstr.h"
#include "grt/common/util.h"


bool grt_check_tftype_tfparams(const char tftype, const char *tfparams){
Expand Down Expand Up @@ -323,17 +324,13 @@ float * grt_get_custom_wave(int *Nt, const char *tfparams){
}

// 逐行读入
char line[1024];
char first_char = '\0';
size_t len;
char *line = NULL;

int nt = 0;
while(fgets(line, sizeof(line), fp)) {
first_char = '\0';
// 读入第一个非空字符,判断是否是注释行
if(sscanf(line, " %c", &first_char) == 1){
if(first_char == '#') continue;
} else {
continue; // 空行,跳过
}
while(grt_getline(&line, &len, fp) != -1) {
// 注释行
if(grt_is_comment_or_empty(line)) continue;

tfarr = (float*)realloc(tfarr, sizeof(float)*(nt+1));
if(sscanf(line, " %f", &tfarr[nt]) < 1){
Expand All @@ -348,6 +345,9 @@ float * grt_get_custom_wave(int *Nt, const char *tfparams){
return NULL;
}

fclose(fp);
GRT_SAFE_FREE_PTR(line);

*Nt = nt;
return tfarr;
}
Expand Down
2 changes: 1 addition & 1 deletion pygrt/C_extension/src/static/grt_static_greenfn.c
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ int static_greenfn_main(int argc, char **argv){

// 输出标题
char XX[20];
sprintf(XX, GRT_STRING_FMT, "X(km)"); XX[0]='#';
sprintf(XX, GRT_STRING_FMT, "X(km)"); XX[0]=GRT_COMMENT_HEAD;
fprintf(stdout, "%s", XX);
fprintf(stdout, GRT_STRING_FMT, "Y(km)");
print_grn_title("");
Expand Down
Loading