Esempio n. 1
0
// Create a station from a sacfile
station *loadStation(char *filename)
{
	station *s = NULL;

	SACHEAD *h = io_readSacHead(filename);
	if (h == NULL) {
		fprintf(stderr, "On Station file %s not found.\n", filename);
		return s;
	}


	s = malloc(sizeof(station));

	s->lat = h->stla;
	s->lon = h->stlo;
	// Elevation should be in KM and + is down !
	s->el = -h->stel / 1000.0;
	s->name = NULL;
	s->net = NULL;

	//  fprintf(stderr, "File: %s ", filename);
	hdu_getValueFromChar("kstnm", h, NULL, NULL, &s->name);
	hdu_getValueFromChar("knetwk", h, NULL, NULL, &s->net);

	h = io_freeData(h);

	return s;
}
Esempio n. 2
0
// Create a pick from a sacfile
pick *loadPick(char *filename, stations * ss)
{
	char *name = NULL;
	char *net = NULL;
	int Id;

	pick *p = NULL;

	SACHEAD *h = io_readSacHead(filename);
	if (h == NULL) {
		fprintf(stderr, "On pick file %s not found.\n", filename);
		return p;
	}

	/* Check if the file is picked */
	if (h->f == -12345. || h->a == -12345.) {
		h = io_freeData(h);
		return p;
	}

	hdu_getValueFromChar("KSTNM", h, NULL, NULL, &name);
	hdu_getValueFromChar("KNETWK", h, NULL, NULL, &net);

	Id = getStationId(ss, name, net);

	if (Id == -1) {
		fprintf(stderr, "Station for pick %s not found.\n", filename);
		h = io_freeData(h);
		name = io_freeData(name);
		net = io_freeData(net);
		return p;
	}

	p = malloc(sizeof(pick));
	p->station = Id;
	p->phase = 1;
	p->tobs = h->f - h->o;
	p->tref = h->a - h->o;
	p->difference = p->tobs - p->tref;
	p->residual = -999.9;

	p->gcarc = h->gcarc;
	p->baz = h->baz;

	h = io_freeData(h);
	name = io_freeData(name);
	net = io_freeData(net);

	return p;
}
Esempio n. 3
0
void checkTREF(tf * files, int nfiles)
{
	int has = 1;
	char phase[4];
	char kphase[10];
	int i;

	for (i = 0; i < nfiles; i++)
		if (files[i].z->head->a == SAC_HEADER_FLOAT_UNDEFINED)
			has = 0;
	
	if (has == 1)
		return;

	lerchar
			("Not all your files has a pick, enter the name of header to import picks or none to quit?",
			 phase, 4);
	
	SACHEADDEF *hdef = getSacHeadDefFromChar(phase);
	if (hdef == NULL || hdef->isMark != 1) {
		strcpy(message, "Okay. Skipping the import of reference phases.");
		alert(WARNING);
		return;
	}
	
	sprintf(kphase, "K%s", phase);
	
	for (i = 0; i < nfiles; i++) {
		float val;
		char *name = NULL;
		
		// Get
		hdu_getValueFromChar(phase, files[i].z->head, &val, NULL, NULL);
		hdu_getValueFromChar(kphase, files[i].z->head, NULL, NULL, &name);

		// Check
		if (val == SAC_HEADER_FLOAT_UNDEFINED) {
			name = io_freeData(name);
			strcpy(message,
				   "The desired header variable is also undefined.");
			alert(WARNING);
			continue;
		}
		// Set
		files[i].z->head->a = val;
		changeCharValueFromChar(files[i].z->head, kphase, name);
		name = io_freeData(name);
	}
}
Esempio n. 4
0
char * pickL(pdefs *pick, SACHEAD *head, int i) {
	if (i < 0 || i >= pick->nPhase) return NULL;
	
	char *label;
	char lvar[128];
	sprintf(lvar,"k%s", pick->markPhase[i]);
	hdu_getValueFromChar(lvar, head, NULL, NULL, &label);
	return label;
}
Esempio n. 5
0
int findpair(tf *files, int nfiles, SACHEAD *head) {
	int i;
	
	//	Loop thr the files and search for the matching:
	//	 * Station code
	//	 * Network code
	for(i=0; i < nfiles; i++) {
		// Station Code
		char *scode = NULL;
		hdu_getValueFromChar("KSTNM", head, NULL, NULL, &scode);
		if ((strlen(scode) != strlen(files[i].station))  || strcmp(scode, files[i].station)) {
			if (scode != NULL) free(scode);
			scode = NULL;
			continue;
		}
		
		// Network code
		char *ncode;
		hdu_getValueFromChar("KNETWK", head, NULL, NULL, &ncode);
		if ((strlen(ncode) != strlen(files[i].net))  || strcmp(ncode, files[i].net)) {
			if (scode != NULL) free(scode);
			if (ncode != NULL) free(ncode);
			scode = NULL;
			ncode = NULL;
			continue;
		}
		
		// Clean up
		if (scode != NULL) free(scode);
		if (ncode != NULL) free(ncode);
		scode = NULL;
		ncode = NULL;
		
		// return index that match
		return i;
	}
	
	return -1;
}
Esempio n. 6
0
// Create one event from a sac filename
event *loadEvent(char *sacFilename)
{
	event *e = NULL;
	int i;
	
	SACHEAD *h = io_readSacHead(sacFilename);
	if (h == NULL) {
		fprintf(stderr, "On Event file %s not found.\n", sacFilename);
		return e;
	}

	e = malloc(sizeof(event));

	e->Id = NULL;
	hdu_getValueFromChar("kevnm", h, NULL, NULL, &e->Id);
	// if no id use the folder name
	if ((i = strncmp(e->Id, SAC_HEADER_CHAR_UNDEFINED, strlen(SAC_HEADER_CHAR_UNDEFINED)) == 0)) {
		char *filenamecopy = malloc(sizeof(char) * (strlen(sacFilename)+1));
		strcpy(filenamecopy, sacFilename);

		char *basefolder;
		basefolder = dirname(filenamecopy);
		
		free(e->Id);
		e->Id = malloc(sizeof(char)*(strlen(basefolder)+1));
		strcpy(e->Id, basefolder);

		free(filenamecopy);
		filenamecopy = NULL;
		fprintf(stderr, "Warning, no kevnm defined using the folder name.\n");
	}

	e->lat = h->evla;
	e->lon = h->evlo;
	e->depth = h->evdp;
	e->magnitude = h->mag;
	e->n = 0;
	e->resMean = 0.0;
	e->resStd = 0.0;
	e->hasMean = 0;
	e->picks = NULL;

	h = io_freeData(h);

	return e;
}
Esempio n. 7
0
tf * io_loadZ (glob_t *glb, int *pnfiles) {
	tf *files = NULL;
	int nfiles = 0;
	
	int i;
	int argc = glb->gl_pathc;
	char **argv = glb->gl_pathv;
	
	// Loop thru all and load
	for (i = 0; i < argc; i++) {
		SACHEAD *head = NULL;
		float * data = NULL;
		
		// Load SAC
		data = io_readSac(argv[i], &head);
		
		// Check that is loaded
		if (head == NULL || data == NULL) {
			fprintf(stderr,"Error loading file: %s", argv[i]);
			continue;
		}
		
		// Check that this is a Z file
		if (head->cmpinc != 0.0) {
			fprintf(stderr,"Error loading file: %s (not Z component)", argv[i]);
			continue;
		}
		
		// Get more memory
		files = realloc(files, sizeof(tf) * (nfiles + 1));
		if (files == NULL){
			fprintf(stderr,"Failed to allocate more memory");
			continue;
		}
		
		// Zero all
		files[nfiles].net = NULL;
		files[nfiles].station = NULL;
		files[nfiles].z = NULL;
		files[nfiles].n = NULL;
		files[nfiles].e = NULL;
		files[nfiles].t = NULL;
		files[nfiles].r = NULL;

		
		// Allocated space for Z
		files[nfiles].z = malloc(sizeof(otf));
		if (files[nfiles].z == NULL){
			fprintf(stderr,"Failed to allocate more memory");
			continue;
		}
		
		// Filename
		files[nfiles].z->filename = NULL;
		
		// data
		files[nfiles].z->data = NULL;
		
		// filtered data
		files[nfiles].z->dataf = NULL;
		
		// header file
		files[nfiles].z->head = NULL;
		
		// Set no default data source
		files[nfiles].current = NULL;
		
		// Save a copy of the filename
		files[nfiles].z->filename = malloc(sizeof(char) * (strlen(argv[i]) + 1));
		strcpy(files[nfiles].z->filename, argv[i]);
		
		// Copy the station & network name
		hdu_getValueFromChar("KSTNM", head, NULL, NULL, &files[nfiles].station);
		hdu_getValueFromChar("KNETWK", head, NULL, NULL, &files[nfiles].net);
		
		// We mark this file as visited on load
		head->unused27 = 1;
		
		// Set not selected
		files[nfiles].selected = 0;
		
		// Set the reference to 0
		files[nfiles].z->reference = 0;
		
		// Re-adjust the pointers
		files[nfiles].z->data = data;
		files[nfiles].z->head = head;
		
		// Increament and go to next file
		nfiles++;
	}
	
	*pnfiles = nfiles;
	return files;
}
Esempio n. 8
0
void multitraceplot(defs * d)
{
	int j, k;
	float start;
	char string[1024];
	char stringaux[1024];
	g_ctl **ctl = d->ctl;
	int color = 1;
	char aligChar;
	float x1, x2;
	int lastFile;
	pdefs *pick = d->pickRules[(int)getConfigAsNumber(config, NAME_PICK, DEFAULT_PICK)];
	
	ctl_clean(NULL);

	ctl_resizeview(ctl[0]);
	plothelp(d);

	// If no traces found give warning and return
	if (d->nfiles <= 0) {
		sprintf(string,
				"No folders matching pattern found. Use ? to change folder pattern configuration !");
		cpgmtxt("B", 0.5, 0.5, 0.5, string);
		return;
	}

	lastFile = ((d->offset + d->max) >=
				d->nfiles) ? (d->nfiles - 1) : (d->offset + d->max);

	// Those are used for min and max of the ALIGO mode
	if (d->files[d->offset].current) {
		x1 = pickR(pick, d->files[d->offset].current->head) + d->files[d->offset].current->reference - 2.5;
		x2 = pickR(pick, d->files[lastFile].current->head) + d->files[lastFile].current->reference +
				d->postphase;
	} else {
		x1 = 0.0;
		x2 = 10.0;
	}
	
	for (j = 0; j < d->max && (j + d->offset) < d->nfiles; j++) {

		/* *********************************************************** */
		/* Prepare the file to plot                                    */
		/* *********************************************************** */
		tf *thistrace = &d->files[j + d->offset];
		if (thistrace->current == NULL) {
			continue;
		}
		if (d->onlyselected && !thistrace->selected)
			continue;

		if (d->filter && d->needfilter) {
			filtertf(thistrace, d);
		}

		float Fmark = pickD(pick, thistrace->current->head);
		float Amark = pickR(pick, thistrace->current->head);
		
		/* *********************************************************** */
		/* Switch Aligment mode                                        */
		/* *********************************************************** */

		(j == 0) ? ctl_axisbottom(ctl[j]) : ctl_axisnone(ctl[j]);
		switch (d->alig) {
		case (ALIGO):
			aligChar = 'O';
			if (d->files[0].current->head->o != SAC_HEADER_FLOAT_UNDEFINED) {
				start = thistrace->current->reference - d->files[0].current->head->o;
				ctl_xreset_mm(ctl[j], x1 - d->files[d->offset].current->head->o,
							  x2 - d->files[d->offset].current->head->o);
			} else {
				start = thistrace->current->reference;
				ctl_xreset_mm(ctl[j], x1, x2);
				aligChar = '!';

				SACTIME *time = getTimeStructFromSAC(d->files[0].current->head);
				char *stime = print_time(time, TIME_ISO);
				sprintf(d->lastaction, "Reference is: %s", stime);
				stime = io_freeData(stime);
				time = io_freeData(time);
			}
			break;

		case (ALIGF):
			aligChar = 'F';
			ctl_xreset_mm(ctl[j], -d->prephase, +d->postphase);
			if (Fmark != SAC_HEADER_FLOAT_UNDEFINED) {
				start = thistrace->current->head->b - Fmark;
			} else {
				start = thistrace->current->head->b;
			}
			break;

		case (ALIGA):
			aligChar = 'A';
			ctl_xreset_mm(ctl[j], -d->prephase, +d->postphase);
			if (Amark != SAC_HEADER_FLOAT_UNDEFINED) {
				start = thistrace->current->head->b - Amark;
			} else {
				start = thistrace->current->head->b;
			}
			break;
		}

		/* *********************************************************** */
		/* Plot the trace (DATA)                                       */
		/* *********************************************************** */

		// Selected color
		color = 14;				// Default trace color
		if (thistrace->selected == 1)
			color = 2;			// If event Selected Color = Red
		// if (thistrace->current->head->d>unused25 != 1) color = 14; // If we are in the restricted mode and trace is locked Color = Gray
		if (d->overlay)
			color = j + 1;

		// Finally Plot
		plot(ctl[j],
			 (d->filter
			  && thistrace->current->dataf != NULL) ? thistrace->current->dataf : thistrace->current->data,
			 thistrace->current->head->npts, thistrace->current->head->delta, start, 0, color,
			 d->zoom);
		/* *********************************************************** */

		/* *********************************************************** */
		/* Plot MARKS & Names                                          */
		/* *********************************************************** */
		for(k=0;k<pick->nPhase;k++) {
			if (strcmp(pick->referencePhase, pick->markPhase[k]) == 0 && d->hidephase) continue;

			float value = pickO(pick, thistrace->current->head, k);
			if (value != SAC_HEADER_FLOAT_UNDEFINED) {
				char *label = pickL(pick, thistrace->current->head, k);
				mark(ctl[j], value + start - thistrace->current->head->b, label, 2);
				if (label != NULL) free(label);
				label = NULL;
			}
		}
		
		// IF OVERLAY MODE JUST PLOT THE TRACE AND RETURN.
		if (d->overlay == 1)
			continue;

		// Mark the Window we perform the Min/Max Search in the trace
		if (d->plotsearchsize) {
			mark(ctl[j],
				 Amark + start - thistrace->current->head->b +
				 d->searchsize, "", 3);
			mark(ctl[j],
				 Amark + start - thistrace->current->head->b -
				 d->insetsearchsize, "", 3);
		}

		sprintf(string, "B: %.0f D: %.2f", thistrace->current->head->baz,
				thistrace->current->head->gcarc);
		if (d->putname >= 3)
			cpgmtxt("B", -2.00, 0.0, 0.0, string);
		else
			cpgmtxt("B", -0.70, 0.0, 0.0, string);

		// Add the name of the trace
		if (d->putname > 0) {
			float pos = 1.2;

			cpgsci(2);
			cpgmtxt("R", pos, .5, .5, thistrace->net);
			pos +=  1.0;
			
			if (d->max <= 20) {
				cpgmtxt("R", pos, .5, .5, thistrace->station);
				pos +=  1.0;
			}
			
			if (d->putname >= 2) {
				char *cmp;
				hdu_getValueFromChar("KCMPNM",thistrace->current->head,NULL,NULL, &cmp);
				cpgmtxt("R", pos, .5, .5, cmp);
				cmp = io_freeData(cmp);
				pos +=  1.0;
			}
			
			if (d->putname >= 3) {
				sprintf(string, "%02d] %s", d->offset + j,
						thistrace->current->filename);
				cpgmtxt("B", -0.70, 0.0, 0.0, string);
			}
			
			cpgsci(1);
		}

	}							// End of trace for loop

	// basic information on the foot
	ctl_resizeview(ctl[0]);
	
	// components information
	// green  (should load and loaded)
	// gray   (should not load)
	// red    (should load but not loaded)
	// the < mark the current displayed component
	strcpy(stringaux,"");
	if (d->nfiles > 0) {
		cpgsci((d->files[0].z != NULL)?3:2);
		cpgsch(1.);
		cpgmtxt("B",1.25, -0.05, 1.0, (d->zne == 0)?"Z":"Z");
		
		cpgsci((d->has3 && d->files[0].n != NULL)?3:(d->has3)?2:14);
		cpgmtxt("B",1.25, -0.04, 1.0, (d->zne == 1)?"N":"N");
		
		cpgsci((d->has3 && d->files[0].e != NULL)?3:(d->has3)?2:14);
		cpgmtxt("B",1.25, -0.03, 1.0, (d->zne == 2)?"E":"E");

		cpgsci((d->has3 && d->files[0].r != NULL)?3:(d->has3)?2:14);
		cpgmtxt("B",1.25, -0.02, 1.0, (d->zne == 3)?"R":"R");
		
		cpgsci((d->has3 && d->files[0].t != NULL)?3:(d->has3)?2:14);
		cpgmtxt("B",1.25, -0.01, 1.0, (d->zne == 4)?"T":"T");

		cpgsci(1);

		cpgsch(.65);
		int phaseid = (int)getConfigAsNumber(config, NAME_PICK, DEFAULT_PICK);
		cpgsci((phaseid == 1)?1:14);
		cpgmtxt("B", 1.25, -0.065, 1.0, PickTypesNames[1]);
		cpgsci((phaseid == 2)?1:14);
		cpgmtxt("B", 2.35, -0.065, 1.0, PickTypesNames[2]);

		cpgsch(1.);
		cpgsci(1);
		cpgmtxt("B", 2.0, -0.05 + d->zne * 0.01, 1.75, "\\m17");

		// Reset to leave
		cpgsch(0.65);
	}
	
	sprintf(string, "[%03d-%03d/%03d] [Filter %s (%.2f to %.2f hz)]",
			d->offset + 1, d->offset + d->max, d->nfiles,
			(d->filter) ? "ON" : "OFF", d->hp, d->lp);
	cpgmtxt("B", 2.5, 0.0, 0.0, string);

	sprintf(string, "[%c] [PSw: %.1f ISw: %.1f Tr/S: %d] [%s]", aligChar,
			d->searchsize, d->insetsearchsize, d->max,
			(d->sortmode == 0) ? "DZ" : "BAZ");
	cpgmtxt("B", 2.5, 1.0, 1.0, string);

	ctl_resizeview(ctl[d->max - 1]);
	sprintf(string, "%11s",
			(d->currentdir - 1 >=
			 0) ? d->glb->gl_pathv[d->currentdir - 1] : "First Directory");
	cpgmtxt("T", 1.0, 0.35, 1.0, string);

	sprintf(string, "%11s",
			(d->currentdir + 1 <
			 d->glb->gl_pathc) ? d->glb->gl_pathv[d->currentdir +
												  1] : "Last Directory");
	cpgmtxt("T", 1.0, 0.65, 0.0, string);

	sprintf(string, "%11s", d->glb->gl_pathv[d->currentdir]);
	if (d->needsave == 1) {
		cpgsci(2);
	} else if (d->has == 1) {
		cpgsci(3);
	} else {
		cpgsci(7);
	}

	cpgmtxt("T", 1.0, 0.5, 0.5, string);

	//plot if the traces are correlated(green) or not (red)
	sprintf(string, "Correlated");
	if (d->files->current->head->unused26 == 1){
		cpgsci(3);
	} else{
		cpgsci(2);
	}

	cpgmtxt("T", 1.0, 0.1, 0.5, string);

	cpgsci(1);

	sprintf(string, "Mag = %.1f", d->files->current->head->mag);
	cpgmtxt("T", 1.0, 0.9, 0.5, string);
}
Esempio n. 9
0
float pickR(pdefs *pick, SACHEAD *head){
	float value;
	hdu_getValueFromChar(pick->referencePhase, head, &value, NULL, NULL);
	return value;
}
Esempio n. 10
0
float pickD(pdefs *pick, SACHEAD *head){
	float value;
	hdu_getValueFromChar(pick->destinationPhase, head, &value, NULL, NULL);
	return value;
}
Esempio n. 11
0
float pickO(pdefs *pick, SACHEAD *head, int i) {
	float value;
	if (i < 0 || i >= pick->nPhase) return SAC_HEADER_FLOAT_UNDEFINED;
	hdu_getValueFromChar(pick->markPhase[i], head, &value, NULL, NULL);
	return value;
}