Esempio n. 1
0
void *otffree(otf *otf) {
	if (otf != NULL) {
		if (otf->filename != NULL) free(otf->filename);
		otf->filename = NULL;
		otf->data  = io_freeData(otf->data);
		otf->dataf = io_freeData(otf->dataf);
		otf->head  = io_freeData(otf->head);
	}
	free(otf);
	return NULL;
}
Esempio n. 2
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. 3
0
void plot(g_ctl * ctl, float *y, int npts, float delta, float b,
		  int overlay, int color, float zoom)
{
	float *x = NULL;
	int i;
	int oldcolor;

	cpgqci(&oldcolor);

	x = malloc(sizeof(float) * npts);
	for (i = 0; i < npts; i++)
		x[i] = i * delta + b;

	if (!overlay) {
		ctl_yupdate_ndb(ctl, y, npts, delta, b);

		ctl->ymin /= zoom;
		ctl->ymax /= zoom;
		ctl->h = ctl->ymax - ctl->ymin;

		ctl_resizeview(ctl);
		ctl_drawaxis(ctl);
	}

	cpgsci(color);
	cpgline(npts, x, y);
	cpgsci(oldcolor);

	x = io_freeData(x);
}
Esempio n. 4
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. 5
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. 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
int findHas(glob_t *glb) {
	int has = 0;
	
	if (glb == NULL)
		return has;
	
	if (glb->gl_pathc == 0)
		return has;
	
	SACHEAD *head = io_readSacHead(glb->gl_pathv[0]);
	if (head != NULL){
		if (head->unused27 == 1) 
			has = 1;
		head = io_freeData(head);
	}
	
	return has;
}
Esempio n. 8
0
int findFilters(glob_t *glb, float *lp, float *hp) {
	int state = 0;
	
	// Set default values
	*lp = getConfigAsNumber(config, NAME_LP, DEFAULT_LP);
	*hp = getConfigAsNumber(config, NAME_HP, DEFAULT_HP);

	if (glb == NULL)
		return state;

	if (glb->gl_pathc == 0)
		return state;

	SACHEAD *head = io_readSacHead(glb->gl_pathv[0]);
	if (head == NULL)
		return state;

	//P phase
	if (getConfigAsNumber(config, NAME_PICK, DEFAULT_PICK) == P){
		if (head->unused11 != -12345.0 && head->unused12 != -12345.0 && head->unused11 > head->unused12) {
			*lp = head->unused11;
			*hp = head->unused12;
			state = (head->unused11 > head->unused12);
		}

	} else if (getConfigAsNumber(config, NAME_PICK, DEFAULT_PICK) == S){ //Phase S
		if (head->unused6 != -12345.0 && head->unused7 != -12345.0 && head->unused6 > head->unused7) {
			*lp = head->unused6;
			*hp = head->unused7;
			state = (head->unused6 > head->unused7);
		}

	} else
		sprintf(message,"Something is not right\n");


	head = io_freeData(head);

	return state;
}
Esempio n. 9
0
/* Other io help methods */
void filtertf(tf * f, defs * d)
{
	if (f->current->data == NULL) {
		f->current->dataf = NULL;
		return;
	}

	f->current->dataf = io_freeData(f->current->dataf);

	if (d->filter) {
		yu_rtrend(f->current->data, f->current->head->npts);
		f->current->dataf = iir(f->current->data, f->current->head->npts, f->current->head->delta,
					(d->hp > 0.0) ? 2 : 0, d->hp, (d->lp > 0.0) ? 2 : 0,
					d->lp);

		if (f->current->dataf == NULL) {
			sprintf(message, "Something wrong with the filter %f %f.", d->hp, d->lp);
			alert(WARNING);
		}
	}

	return;
}
Esempio n. 10
0
void io_loadR (glob_t *glb, tf *files, int nfiles){
	int argc = glb->gl_pathc;
	char **argv = glb->gl_pathv;
	int i;
	
	// 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;
		}

		// Search for the correct Z component file
		int j = findpair(files, nfiles, head);
		if (j == -1) {
			head = io_freeData(head);
			data = io_freeData(data);
			head = NULL;
			data = NULL; 
			fprintf(stderr,"File %s not paired.", argv[i]);
			continue;
		}
		
		// Check that we can assign a new data to component r
		if (files[j].r != NULL) {
			head = io_freeData(head);
			data = io_freeData(data);
			head = NULL;
			data = NULL; 
			fprintf(stderr,"Trace already assigned.");
			continue;
		}
		
		// Prepare space for new otf
		files[j].r = malloc(sizeof(otf));
		if (files[j].r == NULL){
			fprintf(stderr,"Failed to allocate more memory");
			continue;
		}
		
		// We mark this file as visited on load
		head->unused27 = 1;
		
		// Attach Data
		files[j].r->data = data;
		
		// Attach Head
		files[j].r->head= head;
		
		// Clear filter data space
		files[j].r->dataf = NULL;
		
		// Save a copy of the filename
		files[j].r->filename = malloc(sizeof(char) * (strlen(argv[i]) + 1));
		strcpy(files[j].r->filename, argv[i]);
	}
}
Esempio n. 11
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);
}