Skip to content

Commit a3e84b2

Browse files
authored
FEAT: support input distance file (#112)
1 parent f93c2a5 commit a3e84b2

File tree

9 files changed

+91
-9
lines changed

9 files changed

+91
-9
lines changed

docs/source/Module/explain_-R.rst_

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
.. _-R:
22

3-
**-R**\ *r1,r2,...*
4-
震中距 (km)。多个震中距使用逗号分隔
3+
**-R**\ *file*\|\ *r1,r2,...*
4+
震中距 (km)。可以传入逗号分隔的多个震中距,或者仅含有一列震中距的文件 *file*

docs/source/Module/greenfn.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ greenfn
1313
|-M|\ *model*
1414
|-D|\ *depsrc/deprcv*
1515
|-N|\ *nt/dt*\ [**+w**\ *zeta*][**+n**\ *fac*]
16-
|-R|\ *r1,r2,...*
16+
|-R|\ *file*\|\ *r1,r2,...*
1717
|-O|\ *outdir*
1818
[ |-H|\ *f1/f2* ]
1919
[ |-L|\ *args* ]

docs/source/Module/travt.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ travt
1212
**grt travt**
1313
|-M|\ *model*
1414
|-D|\ *depsrc/deprcv*
15-
|-R|\ *r1,r2,...*
15+
|-R|\ *file*\|\ *r1,r2,...*
1616

1717

1818
描述

example/multi_traces/run1.sh

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#!/bin/bash
22

33
# 震中距数组
4-
distarr=($(seq -f "%.2f" 0.5 0.01 1.5 | tr '\n' ','))
4+
# 输出到文件
5+
seq -f "%.2f" 0.5 0.01 1.5 > dists
6+
distarr=($(cat dists | tr '\n' ','))
57
distarr=${distarr%,} # 删除最后的逗号
68

79
# 震源台站都在地表
@@ -16,4 +18,7 @@ modname="mod1"
1618
out="GRN"
1719

1820
# compute Green's Functions
19-
grt greenfn -M${modname} -O${out} -N${nt}/${dt} -D${depsrc}/${deprcv} -R${distarr} -Gv # 只生成垂直力源的格林函数
21+
# OPTION-1: input distances array
22+
# grt greenfn -M${modname} -O${out} -N${nt}/${dt} -D${depsrc}/${deprcv} -R${distarr} -Gv # 只生成垂直力源的格林函数
23+
# OPTION-2:
24+
grt greenfn -M${modname} -O${out} -N${nt}/${dt} -D${depsrc}/${deprcv} -Rdists -Gv # 只生成垂直力源的格林函数

pygrt/C_extension/include/grt/common/const.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ typedef int MYINT; ///< 整数
8080

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

83+
#define GRT_NUM_STR "0123456789" ///< 包括所有数字的字符串宏
84+
8385
#define GRT_MAX(a, b) ((a) > (b) ? (a) : (b)) ///< 求两者较大值
8486
#define GRT_MIN(a, b) ((a) < (b) ? (a) : (b)) ///< 求两者较小值
8587

pygrt/C_extension/include/grt/common/util.h

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,31 @@
2222
* @param[in] delim 分隔符
2323
* @param[out] size 分割后的子字符串数组长度
2424
*
25-
* @return split 子字符串数组
25+
* @return 子字符串数组
2626
*/
2727
char ** grt_string_split(const char *string, const char *delim, int *size);
2828

29+
/**
30+
* 从文本文件中,将每行内容读入字符串数组
31+
*
32+
* @param[in,out] fp 文件指针
33+
* @param[out] size 读入的字符串数组长度
34+
*
35+
* @return 字符串数组
36+
*
37+
*/
38+
char ** grt_string_from_file(FILE *fp, int *size);
39+
40+
/**
41+
* 判断字符串是否由特定的若个字符组成(充分条件)
42+
*
43+
* @param[in] str 待检查的字符串
44+
* @param[in] alws 允许的字符集合
45+
*
46+
* @return 是否符合
47+
*/
48+
bool grt_string_composed_of(const char *str, const char *alws);
49+
2950
/**
3051
* 指定分隔符,获得字符串的分割出的子字符串数。
3152
* 相当于是 grt_string_split 函数的简化版本

pygrt/C_extension/src/common/util.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,42 @@ char ** grt_string_split(const char *string, const char *delim, int *size)
4343
return s_split;
4444
}
4545

46+
char ** grt_string_from_file(FILE *fp, int *size){
47+
char **s_split = NULL;
48+
*size = 0;
49+
s_split = (char**)realloc(s_split, sizeof(char*)*(*size+1));
50+
s_split[*size] = NULL;
51+
52+
size_t len=0;
53+
while(grt_getline(&s_split[*size], &len, fp) != -1){
54+
s_split[*size][strlen(s_split[*size])-1] = '\0'; // 换行符换为终止符
55+
(*size)++;
56+
s_split = (char**)realloc(s_split, sizeof(char*)*(*size+1));
57+
s_split[*size] = NULL;
58+
}
59+
return s_split;
60+
}
61+
62+
bool grt_string_composed_of(const char *str, const char *alws){
63+
bool allowed[256] = {false}; // 初始全为false(不允许)
64+
65+
// 标记允许的字符
66+
for (int i = 0; alws[i] != '\0'; i++) {
67+
unsigned char c = alws[i]; // 转为无符号避免负数索引
68+
allowed[c] = true;
69+
}
70+
71+
// 检查目标字符串中的每个字符
72+
for (int i = 0; str[i] != '\0'; i++) {
73+
unsigned char c = str[i];
74+
if (!allowed[c]) { // 若字符不在允许集合中
75+
return false;
76+
}
77+
}
78+
79+
// 所有字符均在允许集合中
80+
return true;
81+
}
4682

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

pygrt/C_extension/src/dynamic/grt_greenfn.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,16 @@ static void getopt_from_command(GRT_MODULE_CTRL *Ctrl, int argc, char **argv){
652652
case 'R':
653653
Ctrl->R.active = true;
654654
Ctrl->R.s_raw = strdup(optarg);
655-
Ctrl->R.s_rs = grt_string_split(optarg, ",", &Ctrl->R.nr);
655+
// 如果输入仅由数字、小数点和间隔符组成,则直接读取
656+
if(grt_string_composed_of(optarg, GRT_NUM_STR ".,")){
657+
Ctrl->R.s_rs = grt_string_split(optarg, ",", &Ctrl->R.nr);
658+
}
659+
// 否则从文件读取
660+
else {
661+
FILE *fp = GRTCheckOpenFile(command, optarg, "r");
662+
Ctrl->R.s_rs = grt_string_from_file(fp, &Ctrl->R.nr);
663+
fclose(fp);
664+
}
656665
// 转为浮点数
657666
Ctrl->R.rs = (MYREAL*)realloc(Ctrl->R.rs, sizeof(MYREAL)*(Ctrl->R.nr));
658667
for(MYINT i=0; i<Ctrl->R.nr; ++i){

pygrt/C_extension/src/travt/grt_travt.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,16 @@ static void getopt_from_command(GRT_MODULE_CTRL *Ctrl, int argc, char **argv){
492492
// 震中距数组,-Rr1,r2,r3,r4 ...
493493
case 'R':
494494
Ctrl->R.active = true;
495-
Ctrl->R.s_rs = grt_string_split(optarg, ",", &Ctrl->R.nr);
495+
// 如果输入仅由数字、小数点和间隔符组成,则直接读取
496+
if(grt_string_composed_of(optarg, GRT_NUM_STR ".,")){
497+
Ctrl->R.s_rs = grt_string_split(optarg, ",", &Ctrl->R.nr);
498+
}
499+
// 否则从文件读取
500+
else {
501+
FILE *fp = GRTCheckOpenFile(command, optarg, "r");
502+
Ctrl->R.s_rs = grt_string_from_file(fp, &Ctrl->R.nr);
503+
fclose(fp);
504+
}
496505
// 转为浮点数
497506
Ctrl->R.rs = (MYREAL*)realloc(Ctrl->R.rs, sizeof(MYREAL)*(Ctrl->R.nr));
498507
for(MYINT i=0; i<Ctrl->R.nr; ++i){

0 commit comments

Comments
 (0)