Ejemplo n.º 1
0
long writePolygonData(Dbptr db, Point *poly, long npoints, char *pname, int closed, int level, char *ptype, char *auth, char *dir, char *dfile, int pcode) {
	/* writes poygon data into file
   		if dir and o dfile are NULL, use defaults
		returns record number if successful,dbINVALID else
	 */		
	long i;
	char datafilename[PATH_MAX];
	FILE *dfh;
	int foff;
	char *ftype;
	int int1,int2;
	float float1,float2;
	double north=-360.0,east=-360.0,west=360.0,south=360.0;
	double lat,lon;
	int pid;
	int free_dir=0;
	int free_dfile=0;
	char closed_flag[2];

	if (closed) {
		strcpy(closed_flag,"y");
	} else {
		strcpy(closed_flag,"n");
	}

	pid=dbnextid(db,"pid");
	
	db=dblookup(db, 0, "polygon", 0, 0);
	if ( (db.record=dbaddnull(db) ) ==dbINVALID) {
		elog_log(0,"writePolygonData: problem adding row...");
		return dbINVALID;
	}
	
	if (dir == NULL) {
		dir = strdup(default_dir);
		free_dir=1;
	}
	if (dfile == NULL) {
		dfile = strdup(default_dfile);
		free_dfile=0;
	}

	if (dbputv(db,0,"pid",pid,"dir",dir,"dfile",dfile,NULL ) == dbINVALID) {
		elog_log(0,"writePolygonData: error putting dir %s & dfile %s",dir,dfile);
		if (free_dir) free(dir);
		if (free_dfile) free(dfile);
		return dbINVALID;
	}
	/* some fix needed here when in "relative" directories...*/
	switch (dbfilename(db,datafilename)) {
		case -1:
			if (makedir(dir)<0) {
				elog_log(0,"writePolygonData: dir %s is NOT writable!",dir);
				if (free_dir) free(dir);
				if (free_dfile) free(dfile);
				return dbINVALID;
			}
			if ((dfh= fopen(datafilename,"w"))==NULL) {
				elog_log(0,"writePolygondata: error crating %s/%s",dir,dfile);
				if (free_dir) free(dir);
				if (free_dfile) free(dfile);
				return dbINVALID;
				}
			foff= 0;
			break;
		case 0:
			/*make file*/
			if (makedir(dir)<0) {
				elog_log(0,"problem creating directory %s",dir);
				if (free_dir) free(dir);
				if (free_dfile) free(dfile);
				return dbINVALID;
			}
			if ((dfh= fopen(datafilename,"w"))==NULL) {
				elog_log(0,"problem creating file %s",dir);
				if (free_dir) free(dir);
				if (free_dfile) free(dfile);
				return dbINVALID;
			}
			foff= 0;
			break;
		case 1:
			/*append*/	
			dfh= fopen(datafilename,"a");
			fseek(dfh,0,2);
			foff= ftell(dfh);
			break;
		case 2:
			/*.Z exists....*/
			elog_log(0,"writePolygonData: compression not yet supported...");
			if (free_dir) free(dir);
			if (free_dfile) free(dfile);
			return dbINVALID;
			break;
		default:
			elog_log(0,"writePolygondata: unknown return value from dbfilename, giving up...");
			if (free_dir) free(dir);
			if (free_dfile) free(dfile);
			return dbINVALID;	
			
	}
	ftype = polytype(pcode);
	
	switch (pcode) {
		case polyINT:
			for (i=0;i < npoints;i++) {
				lon=poly[i].lon;
				west = (lon<west) ? lon : west;
				east = (lon>east) ? lon : east;
				int1=lon * 1e6;
				H2N4((char *)(&int2),(char *)(&int1),1);
				fwrite(&int2,sizeof(int),1,dfh);

				lat=poly[i].lat;
				south = (lat < south) ? lat : south;
				north = (lat > north) ? lat : north;
				int1=lat * 1e6;
				H2N4((char *)(&int2),(char *)(&int1),1);
				fwrite(&int2,sizeof(int),1,dfh);
			}
			fclose(dfh);
			break;
		case polyINTELINT:
			for (i=0;i < npoints;i++) {
				lon=poly[i].lon;
				west = (lon<west) ? lon : west;
				east = (lon>east) ? lon : east;
				int1=lon * 1e6;
				#ifdef WORDS_BIGENDIAN
					swap4(&int1,&int2,1);
				#else
					int2=int1;
				#endif
				fwrite(&int2,sizeof(int),1,dfh);

				lat=poly[i].lat;
				south = (lat < south) ? lat : south;
				north = (lat > north) ? lat : north;
				int1=lat * 1e6;
				#ifdef WORDS_BIGENDIAN
					swap4(&int1,&int2,1);
				#else
					int2=int1;
				#endif
				fwrite(&int2,sizeof(int),1,dfh);
			}
			fclose(dfh);
			break;
		case polyFLOAT:
			for (i=0;i < npoints;i++) {
				lon=poly[i].lon;
				west = (lon<west) ? lon : west;
				east = (lon>east) ? lon : east;
				float1=lon;
				H2N4((char *)(&float2),(char *)(&float1),1);
				fwrite(&float2,sizeof(float),1,dfh);

				lat=poly[i].lat;
				south = (lat < south) ? lat : south;
				north = (lat > north) ? lat : north;
				float1=lat;
				H2N4((char *)(&float2),(char *)(&float1),1);
				fwrite(&float2,sizeof(float),1,dfh);
			}
			fclose(dfh);
			break;	
		case polyINTELFLOAT:
			for (i=0;i < npoints;i++) {
				lon= poly[i].lon;
				west= (lon<west) ? lon : west;
				east= (lon>east) ? lon : east;
				float1=lon;
				#ifdef WORDS_BIGENDIAN
					swap4(&float1,&float2,1);
				#else	
					float2= float1;
				#endif
				fwrite(&float2,sizeof(float),1,dfh);
				lat= poly[i].lat;
				south= (lat < south) ? lat : south;
				north= (lat > north) ? lat : north;
				float1=lat;
				#ifdef WORDS_BIGENDIAN
					swap4(&float1,&float2,1);
				#else	
					float2= float1;
				#endif
				fwrite(&float2,sizeof(float),1,dfh);
			}
			fclose(dfh);
			break;	
		case polyGSHHS:
			elog_log(0,"writePolygonData: storage type %s not yet implemented... ",ftype);
			if (free_dir) free(dir);
			if (free_dfile) free(dfile);
			return dbINVALID;
			break;
		default:
			elog_log(0,"writePolygonData: unknown storage type pcode, giving up");
			if (free_dir) free(dir);
			if (free_dfile) free(dfile);
			return dbINVALID;
	}	
	if (dbputv(db,0,"north",north, "east",east, "south",south, "west",west,
			"pname",pname,"closed",closed_flag,"level",level,"ptype",ptype, "auth",auth,"npoints",npoints, "ftype",ftype,"foff",foff,NULL ) == dbINVALID) {
		elog_log(0,"writePolygonData: error putting values!");
		//free (ftype);
		if (free_dir) free(dir);
		if (free_dfile) free(dfile);
		return dbINVALID;
	} else {
		//free (ftype);
		if (free_dir) free(dir);
		if (free_dfile) free(dfile);
		return db.record;
	}
}
Ejemplo n.º 2
0
int 
qdata2qorbpkt (unsigned char *qdata_pkt, int ndata,
			double timein, double calib, double calper,
			char *netsta, double *time, char *srcname,
			char **packet, int *nbytes, int *bufsize, char *segtype)

{
	int *idata, npts;
	unsigned short level;
	int i, n;
	char *ptr;
	int bsize;
	unsigned char msg_type;
	unsigned char soh_flags;
	unsigned char clock_flags;
	unsigned char *data;
	int nsamp;
	char sampratec;
	float samprate;
	char station[8];
	char channel[8];
	char network[8];
	int year, month, day, doy, hour, minute, isec, millisec, microsec;
	double second;
	unsigned short hdrsiz;
	unsigned short pktsiz;
	short int si;
	unsigned short usi;
	float flt;

	/* grok the quanterra data packet */

	msg_type = qdata_pkt[6];
	switch (msg_type) {
	case 0:
		level = 0;
		break;
	case 1:
		level = 1;
		break;
	case 2:
		level = 2;
		break;
	default:
		register_error (0, "qdata2qorbpkt: Unknown Quanterra message or compression level (%d).\n", 
						msg_type);
		return (-1);
	}
	soh_flags = qdata_pkt[9];
	clock_flags = qdata_pkt[49];
	data = &(qdata_pkt[66]);
	N2H2(&usi, &(qdata_pkt[24]), 1);
	nsamp = usi;
	sampratec = qdata_pkt[26];
	if (nsamp == 0) return (1);
	if (sampratec == 0) return (1);
	if (sampratec > 0) {
		samprate = (float)sampratec;
	} else {
		samprate = 1.0/(float)(-sampratec);
	}
	for (i=0; i<2; i++) if (qdata_pkt[50+i] != ' ' && qdata_pkt[50+i] != '\0') break;
	n = 2-i;
	memcpy (network, &(qdata_pkt[50+i]), n);
	for (i=0; i<n; i++) if (network[i] == ' ' || network[i] == '\0') break;
	network[i] = '\0';
	for (i=0; i<4; i++) if (qdata_pkt[10+i] != ' ' && qdata_pkt[10+i] != '\0') break;
	n = 4-i;
	memcpy (station, &(qdata_pkt[10+i]), n);
	for (i=0; i<n; i++) if (station[i] == ' ' || station[i] == '\0') break;
	station[i] = '\0';
	for (i=0; i<3; i++) if (qdata_pkt[46+i] != ' ' && qdata_pkt[46+i] != '\0') break;
	n = 3-i;
	memcpy (channel, &(qdata_pkt[46+i]), n);
	for (i=0; i<n; i++) if (channel[i] == ' ' || channel[i] == '\0') break;
	channel[i] = '\0';
	sprintf (srcname, "%s_%s/QCDAT", netsta, channel);
	if (timein == 0.0) {
		year = qdata_pkt[28];
		month = qdata_pkt[29];
		day = qdata_pkt[30];
		hour = qdata_pkt[31];
		minute = qdata_pkt[32];
		isec = qdata_pkt[33];
		N2H2(&si, &(qdata_pkt[14]), 1);
		millisec = si;
		N2H2(&si, &(qdata_pkt[56]), 1);
		microsec = si;
		if (year < 50) year += 2000; else year += 1900;
		doy = mday2doy (year, month, day);
		second = (double)isec + 0.001*millisec + 0.000001*microsec;
		*time = h2e (year, doy, hour, minute, second);
	} else {
		*time = timein;
	}

	/* allocate the output packet */

	hdrsiz = 36;
	pktsiz = hdrsiz + ndata;
	*nbytes = pktsiz;
	bsize = *nbytes + 1;
	if (*packet == NULL) {
		*packet = (char *) malloc (bsize);
		if (*packet == NULL) {
			register_error (1, "qdata2qorbpkt: malloc() error.\n");
			return (-1);
		}
		*bufsize = bsize;
	} else if (bsize > *bufsize) {
		*packet = (char *) realloc (*packet, bsize);
		if (*packet == NULL) {
			register_error (1, "qdata2qorbpkt: realloc() error.\n");
			return (-1);
		}
		*bufsize = bsize;
	}

	/* fill the output packet */

	ptr = *packet;
	H2N2 (ptr, &hdrsiz, 1);
	ptr += 2;
	H2N2 (ptr, &pktsiz, 1);
	ptr += 2;
	usi = (unsigned short int)QUANTERRA_DATA_HDR_TYPE2;
	H2N2 (ptr, &usi, 1);
	ptr += 2;
	usi = (unsigned short int)QUANTERRA_DATA_PKT_TYPE2;
	H2N2 (ptr, &usi, 1);
	ptr += 2;
	memcpy (ptr, &soh_flags, 1);
	ptr += 1;
	memcpy (ptr, &clock_flags, 1);
	ptr += 1;
	H2N2 (ptr, &level, 1);
	ptr += 2;
	H2N4 (ptr, &nsamp, 1);
	ptr += 4;
	H2N4 (ptr, &samprate, 1);
	ptr += 4;
	flt = calib;
	H2N4 (ptr, &flt, 1);
	ptr += 4;
	flt = calper;
	H2N4 (ptr, &flt, 1);
	ptr += 4;
	memcpy (ptr, station, 4);
	ptr += 4;
	memcpy (ptr, segtype, 4);
	ptr += 4;
	memcpy (ptr, &(qdata_pkt[66]), ndata);

	/* normal exit */

	return (0);
}
Ejemplo n.º 3
0
void local42k2 (unsigned char **k2_pointer, void *data_pointer) {
	H2N4 (*k2_pointer, data_pointer, 1);
	*k2_pointer += 4;
}