int log2orbpkt (char *comment, char **packet, int *nbytes, int *bufsize) { int comment_length; unsigned short hdrsiz; unsigned short pktsiz; int bsize; unsigned short int usi; char *ptr; comment_length = strlen(comment); hdrsiz = 2 + 2 + 2 + 2; pktsiz = hdrsiz + comment_length + 1; *nbytes = pktsiz; bsize = *nbytes + 1; if (*packet == NULL) { *packet = (char *) malloc (bsize); if (*packet == NULL) { register_error (1, "log2orbpkt: malloc() error.\n"); return (-1); } *bufsize = bsize; } else if (bsize > *bufsize) { *packet = (char *) realloc (*packet, bsize); if (*packet == NULL) { register_error (1, "log2orbpkt: realloc() error.\n"); return (-1); } *bufsize = bsize; } ptr = *packet; H2N2 (ptr, &hdrsiz, 1); ptr += 2; H2N2 (ptr, &pktsiz, 1); ptr += 2; usi = (unsigned short int)QUANTERRA_LOG_HDR_TYPE; H2N2 (ptr, &usi, 1); ptr += 2; usi = (unsigned short int)QUANTERRA_LOG_PKT_TYPE; H2N2 (ptr, &usi, 1); ptr += 2; memcpy (ptr, comment, (int)comment_length + 1); /* normal exit */ return (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); }
void local22k2 (unsigned char **k2_pointer, void *data_pointer) { H2N2 (*k2_pointer, data_pointer, 1); *k2_pointer += 2; }