-
Notifications
You must be signed in to change notification settings - Fork 0
/
meta.c
879 lines (827 loc) · 23.9 KB
/
meta.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
/**
* @file
* @brief Meta data information
*/
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <sacio/sacio.h>
#include <sacio/timespec.h>
#include "meta.h"
#include "event.h"
#include "datareq.h"
#include "array.h"
#include "cprint.h"
#include "station.h"
#include "xml.h"
#include "slurp.h"
#include "defs.h"
#include "strip.h"
#include "urls.h"
#define KEYN 64 /**< @private */
#define MAXF 20 /**< @private */
/**
* @defgroup meta meta
* @brief Meta data requests and insertion
*
*/
typedef struct meta_data meta_data;
/**
* @brief channel level meta data
* @ingroup meta
* @private
*/
struct meta_data {
char key[MAXF][KEYN]; /**< @private Key values for meta data */
};
/**
* @brief Print out a sac floating point value
*
* @memberof sac
*
* @param s sac
* @param name header name
* @param hdr header index
*
*/
void
sac_show_float(sac *s, char *name, int hdr) {
double v = 0.0;
if(!sac_get_float(s, hdr, &v)) {
return;
}
printf("\t%8s %12.4f\n", name, v);
}
/**
* @brief Print out a sac string value
*
* @memberof sac
*
* @param s sac file
* @param name header name
* @param hdr header index
*
*/
void
sac_show_string(sac *s, char *name, int hdr) {
char v[20];
if(!sac_get_string(s, hdr, v, sizeof(v))) {
return;
}
printf("\t%8s %16s\n", name, v);
}
/**
* @brief Compare a sac header value with a string
*
* @memberof sac
*
* @param sac sac file
* @param hdr header index value
* @param value character string
*
* @return 1 if the strings are the same, 0 if different
*
* Comparison is conducted on both the full string and a trimmed string
* A special case is taken for KHOLE or Location
* If the value is "" or "--" and the khole value is "-- " or "-12345 "
* it is considered a match
*/
int
sac_strcmp(sac *s, int hdr, char *value) {
char h[20] = {0};
sac_get_string(s, hdr, h, sizeof(h));
//printf("'%s' <=> '%s'\n", h, value);
if(hdr == SAC_LOC) {
if((strcmp(value, "--") == 0 || strlen(value) == 0) &&
(strcmp(h, "-- ") == 0 || strcmp(h, SAC_CHAR_UNDEFINED) == 0 )) {
//printf(" Match Loc/Hole Undefined\n");
return 1;
}
}
if(strcmp(h, value) == 0) {
//printf(" Match\n");
return 1;
}
fern_rstrip(h);
//printf("'%s' <=> '%s'\n", h, value);
if(strcmp(h, value) == 0) {
//printf(" Match\n");
return 1;
}
//printf(" No match\n");
return 0;
}
int
xml_find_string_copy_or_2599(xml *x, xmlNode *from, const char *path, const char *key, char *s, size_t n) {
if(! xml_find_string_copy(x, from, path, key, s, n)) {
fern_strlcpy(s, "2599-12-31T23:59:59", n);
}
return 1;
}
/**
* @brief Get an double floating point value from a station xml file
*
* @memberof xml
*
* @param x xml doc
* @param s sac file with header info
* @param which sac header value to identify which header value
* @param value output floating point value
*
* @return 1 on success, 0 on failure
*
* @note possible values for which are SAC_STLA, SAC_STLO, SAC_STEL, SAC_STDP, SAC_CMPAZ, SAC_CMPINC
* @note values for SAC_CMPINC will have 90.0 added to them to convert from SEED format to SAC format
*
*/
int
station_xml_get_double(xml *x, sac *s, int which, double *value) {
char start[128] = {0};
char end[128] = {0};
timespec64 tb = {0,0};
timespec64 te = {0,0};
timespec64 sb = {0,0};
timespec64 se = {0,0};
char *fmt = "//s:Network[@code='%N']/s:Station[@code='%S']"
"/s:Channel[@locationCode='%H' and @code='%C']";
char path[2048] = {0};
char path2[2048] = {0};
sac_get_time(s, SAC_B, &sb);
sac_get_time(s, SAC_E, &se);
sac_fmt(path, sizeof(path), fmt, s);
xmlXPathObject *objs = xml_find_all(x, NULL, (xmlChar *) path);
if(!objs) {
return 0;
}
for(size_t i = 0; i < xpath_len(objs); i++) {
xmlNode *node = xpath_index(objs, i);
xml_find_string_copy_or_2599(x, node, ".", "endDate", end, sizeof end); // Always returns 1
if(!xml_find_string_copy(x, node, ".", "startDate", start, sizeof start)) {
printf("Error finding startDate in channel for metadata\n");
printf("%s\n", path);
continue;
}
if(!timespec64_parse(start, &tb) || !timespec64_parse(end, &te)) {
printf("Error parsing datetime start %s end %s\n", start, end);
continue;
}
if(timespec64_cmp(&tb, &se) <= 0 && timespec64_cmp(&te, &sb) >= 0) {
memset(path2, 0, sizeof path2);
switch(which) {
case SAC_STLA: fern_strlcat(path2, "s:Latitude", sizeof(path)); break;
case SAC_STLO: fern_strlcat(path2, "s:Longitude", sizeof(path)); break;
case SAC_STEL: fern_strlcat(path2, "s:Elevation", sizeof(path)); break;
case SAC_STDP: fern_strlcat(path2, "s:Depth", sizeof(path)); break;
case SAC_CMPAZ: fern_strlcat(path2, "s:Azimuth", sizeof(path)); break;
case SAC_CMPINC: fern_strlcat(path2, "s:Dip", sizeof(path)); break;
}
if(xml_find_double(x, node, path2, NULL, value)) {
if(which == SAC_CMPINC) {
*value = *value + 90;
}
return 1;
} else {
printf("Error finding %s for %s\n", path2, path);
}
}
}
return 0;
}
/**
* @brief Get an floating point value from a station xml file
*
* @memberof xml
*
* @param x xml doc
* @param s sac file with header info
* @param which sac header value to identify which header value
* @param value output floating point value
*
* @return 1 on success, 0 on failure
*
* @note possible values for which are SAC_STLA, SAC_STLO, SAC_STEL, SAC_STDP, SAC_CMPAZ, SAC_CMPINC
* @note values for SAC_CMPINC will have 90.0 added to them to convert from SEED format to SAC format
* @note calls station_xml_get_double)
*
*/
int
station_xml_get_float(xml *x, sac *s, int which, float *value) {
double v = 0.0;
int retval = station_xml_get_double(x, s, which, &v);
*value = (float) v;
return retval;
}
/**
* @brief Count the number of characters in a line
*
* @private
* @ingroup meta
* @memberof meta_data
*
* @param line Line to count within
* @param c character to count the number of
*
* @return number of c in line
*
*/
int
count_chars(char *line, char c) {
int n = 0;
char *p = line;
while(p && (p = strchr(p, c))) {
n++;
p++;
}
return n;
}
/**
* @brief Parse meta data from a input line
*
* @memberof meta_data
* @ingroup meta
* @private
*
* @param line line to parse meta data from
* @param delim delimiter which separates columes
*
* @return meta_data
*
* @note values are copied from the line
*
*/
meta_data *
meta_data_from_line(char *line, char *delim) {
int i = 0;
char *tok = NULL;
meta_data *m = calloc(sizeof(meta_data), 1);
memset(m->key, 0, MAXF * KEYN);
while((tok = strsep(&line, delim))) {
tok = fern_lstrip(fern_rstrip(tok));
fern_strlcpy(m->key[i], tok, KEYN);
i++;
if(i >= MAXF) {
break;
}
}
return m;
}
/**
* @brief Check is meta data line matches a sac file
*
* @memberof meta_data
* @ingroup meta
* @private
*
* @param s sac file
* @param m meta data (net, sta, loc, cha, start, end)
*
* @return 1 on a match, 0 on no match
*
*/
int
sac_matches_nslc(sac *s, meta_data *m) {
return (sac_strcmp(s, SAC_NET, m->key[0]) &&
sac_strcmp(s, SAC_STA, m->key[1]) &&
sac_strcmp(s, SAC_LOC, m->key[2]) &&
sac_strcmp(s, SAC_CHA, m->key[3]));
}
/**
* Meta data fields
*
*/
enum {
META_NET = 0, /**< @private Network */
META_STA, /**< @private Station */
META_LOC, /**< @private Location */
META_CHA, /**< @private Channel */
META_STLA, /**< @private Latitude */
META_STLO, /**< @private Longitude */
META_STEL, /**< @private Elevation */
META_STDP, /**< @private Depth */
META_CMPAZ, /**< @private Component Azimuth */
META_CMPINC, /**< @private Component Inclination */
META_KINST, /**< @private Instrument Name */
META_SCALE, /**< @private Scale Factor */
META_SCALE_FREQ, /**< @private Frequency at Scale Factor */
META_SCALE_UNITS, /**< @private Scale Factor Units*/
META_SAMP_RATE, /**< @private Sample rate */
META_START, /**< @private Start time / On */
META_END, /**< @private End time / Off */
};
/**
* @brief Check if a time range matches a meta data line
*
* @memberof meta_data
* @ingroup meta
* @private
*
* @param sb start time (typically from a sac file)
* @param se end time (typically from a sac file)
* @param m meta data line
*
* @return 1 on match, 0 on no match
*
* @note If sb or se do not exist or they do not exist in the meta data, it is considered a match
*
* @note See https://stackoverflow.com/a/325964 for time overlap function
*/
int
sac_matches_time(timespec64 *sb, timespec64 *se, meta_data *m) {
timespec64 tb = {0,0} , te = {0,0};
// sac times do not exist
if(!se || !sb) {
return 1;
}
// meta data time fields do not exist
if(strlen(m->key[META_START]) == 0 || strlen(m->key[META_END]) == 0) {
return 1;
}
// error parsing meta data keys
if(!timespec64_parse(m->key[META_START], &tb)) {
printf("Error parsing start time: '%s'\n", m->key[META_START]);
return 1;
}
if(!timespec64_parse(m->key[META_END], &te)) {
printf("Error parsing end time: '%s'\n", m->key[META_END]);
return 1;
}
// Date Times overlap
if(timespec64_cmp(&tb, se) <= 0 && timespec64_cmp(&te, sb) >= 0) {
return 1;
}
// Date Times do not overlap
return 0;
}
/**
* @brief Fill meta data for a collection of sac files
*
* @memberof meta_data
* @ingroup meta
* @private
*
* @param files sac files, must be wrapped in an \ref xarray
* @param data xml meta data
* @param ndata length of data
* @param verbose be verbose while parsing and setting
*
* @return 1 on success, 0 on error
*
* @note fields filled are:
* - stla - Station Latitude
* - stlo - Station Longitude
* - stel - Station Elevation
* - stdp - Station Depth
* - cmpaz - Component Azimuth
* - cmpinc - Component Inclination
*/
int
sac_fill_meta_data_from_xml(sac **files, xml *x, int verbose) {
sac *s = NULL;
char *key[] = {"stla", "stlo", "stel",
"stdp", "cmpaz", "cmpinc" };
int fid[] = { SAC_STLA, SAC_STLO, SAC_STEL,
SAC_STDP, SAC_CMPAZ, SAC_CMPINC };
if(!x) {
return 0;
}
for(size_t i = 0; i < xarray_length(files); i++) {
s = files[i];
cprintf("black,bold", "Working on file: ");
printf("%s", s->m->filename);
if(verbose) {
printf("\n");
}
int rv = 0;
for(int j = 0; j < 6; j++) {
int rvi = 0;
float v = 0.0;
if((rvi = station_xml_get_float(x, s, fid[j], &v))) {
sac_set_float(s, fid[j], v);
}
rv += rvi;
}
if(!verbose) {
printf(" [ ");
if(rv == 6) {
cprintf("green,bold", "OK");
} else {
cprintf("red,bold", "Error %d/%d", rv, 6);
}
printf(" ]\n");
} else {
for(int j = 0; j < 6; j++) {
sac_show_float(s, key[j], fid[j]);
}
}
}
return 1;
}
/**
* @brief Parse station meta data from a file
*
* @memberof meta_data
* @ingroup meta
* @private
*
* @param file filename to get meta data from
* @param verbose be verbose in parsing
* @param seed_cmpinc output value to add to Component Inclincation
*
* @return \ref meta_data
*
* @note From mseed2sac, https://github.com/iris-edu/mseed2sac:
* - When comma separators are used the dip field (CMPINC) is assumed to be in
* the SAC convention (degrees down from vertical up/outward), if vertical bars
* are used the dip field is assumed to be in the SEED convention (degrees down
* from horizontal) and converted to SAC convention.
*
* - delimiter = , (comma) ==> seed_cmpinc = 0.0
* - delimiter = | (bar/pipe) ==> seed_cmpinc = 90.0
*
* - See option -msi in mseed2sac:
* - Convert any component inclination values in a metadata file from SEED (dip)
* to SAC convention, this is a simple matter of adding 90 degrees.
*
*/
meta_data **
station_meta_parse(char *file, int verbose, float *seed_cmpinc) {
char *p = NULL;
char line[4096] = {0};
FILE *fp = NULL;
char delim[2] = {0};
int nfields = 0;
if(!(fp = fopen(file, "r"))) {
printf("Error: File does not exist: %s\n", file);
return NULL;
}
meta_data **ms = xarray_new('p');
while(fgets(line, sizeof(line), fp)) {
p = line;
if(line[strlen(line)-1] == '\n') {
line[strlen(line)-1] = 0;
}
while(isspace(*p)) {
p++;
}
if(*p == '#') {
if(verbose) {
printf("meta-data: Skipping comment: %s\n", line);
}
continue;
}
if(delim[0] == 0) {
if(strchr(line,'|')) {
delim[0] = '|'; // miniseed (inclination)
*seed_cmpinc = 90.0;
} else {
delim[0] = ','; // sac convection (dip)
*seed_cmpinc = 0.0;
}
}
nfields = count_chars(line, delim[0]);
if(nfields < 4) {
printf("meta-data: Skipping line, too few fields [%d]: %s\n", nfields, line);
}
meta_data *m = meta_data_from_line(line, delim);
ms = xarray_append(ms, m);
}
return ms;
}
#define IS_OK(v) do { \
if(!v) { \
printf("[ "); \
cprintf("green,bold","OK"); \
printf(" ]"); \
} \
printf("\n"); \
} while(0);
/**
* @brief Report no meta data could be found for a sac file
*
* @private
*
* @memberof meta_data
* @ingroup meta
*
* @param s sac file to complain about
* @param verbose be verbose in complaining
*
*/
static
void no_meta_data(sac *s, int verbose) {
char nslc[64] = {0};
if(verbose) {
sac_fmt(nslc, sizeof(nslc), "%Z", s);
cprintf("", "\n");
cprintf("red,bold", "\tWARNING: Could not find meta data for %s\n", nslc);
} else {
printf("[ "); \
cprintf("red,bold","No Metadata"); \
printf(" ]\n"); \
}
}
/**
* @brief Find a match within a meta data collection
*
* @memberof meta_data
* @ingroup meta
* @private
*
* @param ms \ref meta_data collection, must be enclosed in an \ref xarray
* @param s sac file to find meta data for
*
* @return matching meta_data
*
* @note meta data is matched on Network, Station, Location, Channel, On and Off times
*/
meta_data *
meta_data_find_match(meta_data **ms, sac *s) {
timespec64 sb = {0,0} , se = {0,0};
meta_data *mi = NULL;
size_t n = 0;
if(!ms || !s) {
goto done;
}
n = xarray_length(ms);
if(n == 0) {
goto done;
}
sac_get_time(s, SAC_B, &sb);
sac_get_time(s, SAC_E, &se);
for(size_t j = 0; j < n; j++) {
meta_data *m = ms[j];
if( sac_matches_nslc(s, m) && sac_matches_time(&sb, &se, m)) {
mi = m;
goto done;
}
}
done:
return mi;
}
/**
* @brief Fill meta data for a collection of sac files from a text or XML file
*
* @memberof meta_data
* @ingroup meta
*
* @param files collection of sac files, must be a enclosed in an \ref xarray
* @param verbose be verbose about setting meta data
* @param file file to take data from, can be either an xml file or text file
*
*/
void
sac_array_fill_meta_data_from_file(sac **files, int verbose, char *file) {
float seed_cmpinc = 0.0;
char *endptr = NULL;
char *fields[] = {"net","sta","loc","cha",
"stla", "stlo","stel","stdp",
"cmpaz","cmpinc","kinst"};
int fid[] = {SAC_NET,SAC_STA,SAC_LOC,SAC_CHA,
SAC_STLA, SAC_STLO, SAC_STEL, SAC_STDP, SAC_CMPAZ,
SAC_CMPINC, SAC_INST};
meta_data **ms = NULL;
if(is_xml_file(file)) {
size_t n = 0;
char *data = NULL;
xml *x = NULL;
data = slurp(file, &n);
if(!data || (x = xml_new(data, n)) == NULL) {
return;
}
sac_fill_meta_data_from_xml(files, x, verbose);
xml_free(x);
FREE(data);
return;
}
ms = station_meta_parse(file, verbose, &seed_cmpinc);
if(!ms) {
return;
}
for(size_t i = 0; i < xarray_length(files); i++) {
meta_data *m = NULL;
sac *s = files[i];
cprintf("black,bold", "Working on file: ");
cprintf("","%s ", s->m->filename);
if(!(m = meta_data_find_match(ms, s))) {
no_meta_data(s, verbose);
continue;
}
IS_OK(verbose);
for(size_t k = 4; k < sizeof(fid)/sizeof(int); k++) {
int f = fid[k];
if(strlen(m->key[k]) > 0) {
if(f == SAC_KINST) {
sac_set_string(s, f, m->key[k]);
if(verbose) {
sac_show_string(s, fields[k], f);
}
} else {
double v = 0.0;
v = strtod(m->key[k], &endptr);
if(f == SAC_CMPINC) {
v += seed_cmpinc;
}
sac_set_float(s, f, v);
if(verbose) {
sac_show_float(s, fields[k], f);
}
}
}
}
}
for(size_t i = 0; i < xarray_length(ms); i++) {
FREE(ms[i]);
}
xarray_free(ms);
ms = NULL;
}
void
show_stations_error(result *r) {
int code = result_http_code(r);
if(code == 404 || code == 204) {
printf("No stations found (%d)\n", code);
} else {
printf("%s\n", result_error_msg(r));
}
}
xml *
xml_merge_results(result *r1, result *r2, char *path) {
xml *x1 = NULL;
xml *x2 = NULL;
// If neither request is ok, return with error
if(!result_is_ok(r1) && !result_is_ok(r2)) {
printf("Error getting station data: ");
if(r1) {
show_stations_error(r1);
} else if(r2) {
show_stations_error(r2);
}
goto done;
}
// Convert requested data to xml
if(r1 && result_is_ok(r1) && !(x1 = xml_new(result_data(r1), result_len(r1)))) {
printf("Error parsing xml\n");
goto done;
}
if(r2 && result_is_ok(r2) && !(x2 = xml_new(result_data(r2), result_len(r2)))) {
printf("Error parsing xml\n");
xml_free(x1);
goto done;
}
if(!x1 && !x2) {
// If neither xml exists, return
return NULL;
} else if(x1 && x2 == NULL) {
// Do nothing, if only 1st xml exists
} else if(x1 == NULL && x2) {
// Shift the 2nd xml then 1st, if it only exists
x1 = x2;
x2 = NULL;
} else if(x1 && x2) {
// Merge if both xml exist into the 1st, at network level
xml_merge(x1, x2, path);
}
done:
xml_free(x2);
return x1;
}
/**
* @brief Fill meta data for a collection of sac files by request
*
* @memberof meta_data
* @ingroup meta
*
* @param files collection of sac files, must be enclosed in a \ref xarray
* @param verbose be verbose when setting meta data
* @param ph5 get data also from ph5 web service
*
* @return 1 always
*
* @note meta data is requested using IRIS Station request, level = channel
*
*/
int
sac_array_fill_meta_data(sac **files, int verbose, int ph5) {
sac *s = NULL;
request *sm = NULL;
result *r[2] = {NULL,NULL};
char *data = NULL;
size_t nalloc = 2048;
size_t n = 0;
char line[2048] = {0};
xml *x = NULL;
// Station Meta Request Build
data = str_grow(data, &nalloc, n, strlen("level=channel\n"));
n = fern_strlcat(data, "level=channel\n", nalloc);
for(size_t i = 0; i < xarray_length(files); i++) {
s = files[i];
if(! sac_hdr_defined(s, SAC_NET, SAC_STA, SAC_CHA, NULL) ) {
printf("Insufficient net,sta,cha,time to retrieve station meta data\n");
continue;
}
sac_fmt(line, sizeof(line), "%R\n", s);
data = str_grow(data, &nalloc, n, strlen(line));
n = fern_strlcat(data, line, nalloc);
}
// Request Station Meta Data
sm = request_new();
request_set_verbose(sm, verbose);
request_set_url(sm, STATION_IRIS);
r[0] = request_post(sm, data);
if(ph5) {
request_set_url(sm, STATION_IRIS_PH5);
r[1] = request_post(sm, data);
}
if(!(x = xml_merge_results(r[0], r[1], "//s:Network"))) {
goto error;
}
sac_fill_meta_data_from_xml(files, x, verbose);
error:
xml_free(x);
RESULT_FREE(r[0]);
RESULT_FREE(r[1]);
REQUEST_FREE(sm);
FREE(data);
return 1;
}
/**
* @brief Fill meta data associated with an event in multiple sac file
*
* @memberof meta_data
* @ingroup meta
*
* @param s sac files, encolsed in an xarray
* @param ev event data, if NULL, nothing is done
* @param verbose be verbose when setting meta data
*
* @note Fields set are
* - evla - Event Latitude
* - evla - Event Longitude
* - evla - Event Depth
* - evla - Event Elevation - Always 0.0
* - kevnm - Event ID - Only if <= 16 characters
* - kzdate and kztime - Event Origin time
* - iztype = IO (Origin)
*
* @note All time picks are shifted so the origin time is 0.0, See sac_set_time()
*/
void
sac_array_fill_meta_data_from_event(sac **s, Event *ev, int verbose) {
for(size_t i = 0; i < xarray_length(s); i++) {
sac_fill_meta_data_from_event(s[i], ev, verbose);
}
}
/**
* @brief Fill meta data associated with an event in a sac file
*
* @memberof meta_data
* @ingroup meta
*
* @param s sac file
* @param ev event data, if NULL, nothing is done
* @param verbose be verbose when setting meta data
*
* @note Field set are
* - evla - Event Latitude
* - evla - Event Longitude
* - evla - Event Depth
* - evla - Event Elevation - Always 0.0
* - kevnm - Event ID - Only if <= 16 characters
* - kzdate and kztime - Event Origin time
* - iztype = IO (Origin)
*
* @note All time picks are shifted so the origin time is 0.0, See sac_set_time()
*/
void
sac_fill_meta_data_from_event(sac *s, Event *ev, int verbose) {
if(ev) {
if(verbose) {
printf("Setting event parameters from '%s'\n", event_id(ev));
}
sac_set_float(s, SAC_EVLA, event_lat(ev));
sac_set_float(s, SAC_EVLO, event_lon(ev));
sac_set_float(s, SAC_EVDP, event_depth(ev));
sac_set_float(s, SAC_EVEL, 0.0);
if(strlen(event_id(ev)) <= 16) {
sac_set_string(s, SAC_EVENT, event_id(ev));
} else {
cprintf("red,bold",
"Warning: eventid too long (%lu) to store in header\n",
strlen(event_id(ev)));
fern_strlcpy(s->h->kevnm, SAC_CHAR_UNDEFINED_2, strlen(s->h->kevnm));
}
sac_set_time(s, event_time(ev));
if(verbose) {
char *ekey[] = {"evla","evlo","evdp","evel","kevnm"};
int eid[] = {SAC_EVLA, SAC_EVLO, SAC_EVDP, SAC_EVEL, SAC_EVENT};
for(int j = 0; j < 4; j++) {
sac_show_float(s, ekey[j], eid[j]);
}
sac_show_string(s, ekey[4], eid[4]);
char dst[128] = {0};
timespec64 t = event_time(ev);
strftime64t(dst, sizeof(dst), "%FT%T.%3f", &t);
printf("\t%8s %s\n", "origin", dst);
}
}
}