📄 pack.c
字号:
if ( msr->fsdh == NULL ) { ms_log (2, "msr_pack_header_raw(%s): Cannot allocate memory\n", PACK_SRCNAME); return -1; } } /* Update the SEED structures associated with the MSRecord */ if ( normalize ) if ( msr_normalize_header (msr, verbose) < 0 ) { ms_log (2, "msr_pack_header_raw(%s): error normalizing header values\n", PACK_SRCNAME); return -1; } if ( verbose > 2 ) ms_log (1, "%s: Packing fixed section of data header\n", PACK_SRCNAME); if ( maxheaderlen > msr->reclen ) { ms_log (2, "msr_pack_header_raw(%s): maxheaderlen of %d is beyond record length of %d\n", PACK_SRCNAME, maxheaderlen, msr->reclen); return -1; } if ( maxheaderlen < sizeof(struct fsdh_s) ) { ms_log (2, "msr_pack_header_raw(%s): maxheaderlen of %d is too small, must be >= %d\n", PACK_SRCNAME, maxheaderlen, sizeof(struct fsdh_s)); return -1; } fsdh = (struct fsdh_s *) rawrec; offset = 48; /* Roll-over sequence number if necessary */ if ( msr->sequence_number > 999999 ) msr->sequence_number = 1; /* Copy FSDH associated with the MSRecord into the record */ memcpy (fsdh, msr->fsdh, sizeof(struct fsdh_s)); /* Swap byte order? */ if ( swapflag ) { MS_SWAPBTIME (&fsdh->start_time); ms_gswap2 (&fsdh->numsamples); ms_gswap2 (&fsdh->samprate_fact); ms_gswap2 (&fsdh->samprate_mult); ms_gswap4 (&fsdh->time_correct); ms_gswap2 (&fsdh->data_offset); ms_gswap2 (&fsdh->blockette_offset); } /* Traverse blockette chain and pack blockettes at 'offset' */ cur_blkt = msr->blkts; while ( cur_blkt && offset < maxheaderlen ) { /* Check that the blockette fits */ if ( (offset + 4 + cur_blkt->blktdatalen) > maxheaderlen ) { ms_log (2, "msr_pack_header_raw(%s): header exceeds maxheaderlen of %d\n", PACK_SRCNAME, maxheaderlen); break; } /* Pack blockette type and leave space for next offset */ memcpy (rawrec + offset, &cur_blkt->blkt_type, 2); if ( swapflag ) ms_gswap2 (rawrec + offset); nextoffset = offset + 2; offset += 4; if ( cur_blkt->blkt_type == 100 ) { struct blkt_100_s *blkt_100 = (struct blkt_100_s *) (rawrec + offset); memcpy (blkt_100, cur_blkt->blktdata, sizeof (struct blkt_100_s)); offset += sizeof (struct blkt_100_s); if ( swapflag ) { ms_gswap4 (&blkt_100->samprate); } } else if ( cur_blkt->blkt_type == 200 ) { struct blkt_200_s *blkt_200 = (struct blkt_200_s *) (rawrec + offset); memcpy (blkt_200, cur_blkt->blktdata, sizeof (struct blkt_200_s)); offset += sizeof (struct blkt_200_s); if ( swapflag ) { ms_gswap4 (&blkt_200->amplitude); ms_gswap4 (&blkt_200->period); ms_gswap4 (&blkt_200->background_estimate); MS_SWAPBTIME (&blkt_200->time); } } else if ( cur_blkt->blkt_type == 201 ) { struct blkt_201_s *blkt_201 = (struct blkt_201_s *) (rawrec + offset); memcpy (blkt_201, cur_blkt->blktdata, sizeof (struct blkt_201_s)); offset += sizeof (struct blkt_201_s); if ( swapflag ) { ms_gswap4 (&blkt_201->amplitude); ms_gswap4 (&blkt_201->period); ms_gswap4 (&blkt_201->background_estimate); MS_SWAPBTIME (&blkt_201->time); } } else if ( cur_blkt->blkt_type == 300 ) { struct blkt_300_s *blkt_300 = (struct blkt_300_s *) (rawrec + offset); memcpy (blkt_300, cur_blkt->blktdata, sizeof (struct blkt_300_s)); offset += sizeof (struct blkt_300_s); if ( swapflag ) { MS_SWAPBTIME (&blkt_300->time); ms_gswap4 (&blkt_300->step_duration); ms_gswap4 (&blkt_300->interval_duration); ms_gswap4 (&blkt_300->amplitude); ms_gswap4 (&blkt_300->reference_amplitude); } } else if ( cur_blkt->blkt_type == 310 ) { struct blkt_310_s *blkt_310 = (struct blkt_310_s *) (rawrec + offset); memcpy (blkt_310, cur_blkt->blktdata, sizeof (struct blkt_310_s)); offset += sizeof (struct blkt_310_s); if ( swapflag ) { MS_SWAPBTIME (&blkt_310->time); ms_gswap4 (&blkt_310->duration); ms_gswap4 (&blkt_310->period); ms_gswap4 (&blkt_310->amplitude); ms_gswap4 (&blkt_310->reference_amplitude); } } else if ( cur_blkt->blkt_type == 320 ) { struct blkt_320_s *blkt_320 = (struct blkt_320_s *) (rawrec + offset); memcpy (blkt_320, cur_blkt->blktdata, sizeof (struct blkt_320_s)); offset += sizeof (struct blkt_320_s); if ( swapflag ) { MS_SWAPBTIME (&blkt_320->time); ms_gswap4 (&blkt_320->duration); ms_gswap4 (&blkt_320->ptp_amplitude); ms_gswap4 (&blkt_320->reference_amplitude); } } else if ( cur_blkt->blkt_type == 390 ) { struct blkt_390_s *blkt_390 = (struct blkt_390_s *) (rawrec + offset); memcpy (blkt_390, cur_blkt->blktdata, sizeof (struct blkt_390_s)); offset += sizeof (struct blkt_390_s); if ( swapflag ) { MS_SWAPBTIME (&blkt_390->time); ms_gswap4 (&blkt_390->duration); ms_gswap4 (&blkt_390->amplitude); } } else if ( cur_blkt->blkt_type == 395 ) { struct blkt_395_s *blkt_395 = (struct blkt_395_s *) (rawrec + offset); memcpy (blkt_395, cur_blkt->blktdata, sizeof (struct blkt_395_s)); offset += sizeof (struct blkt_395_s); if ( swapflag ) { MS_SWAPBTIME (&blkt_395->time); } } else if ( cur_blkt->blkt_type == 400 ) { struct blkt_400_s *blkt_400 = (struct blkt_400_s *) (rawrec + offset); memcpy (blkt_400, cur_blkt->blktdata, sizeof (struct blkt_400_s)); offset += sizeof (struct blkt_400_s); if ( swapflag ) { ms_gswap4 (&blkt_400->azimuth); ms_gswap4 (&blkt_400->slowness); ms_gswap2 (&blkt_400->configuration); } } else if ( cur_blkt->blkt_type == 405 ) { struct blkt_405_s *blkt_405 = (struct blkt_405_s *) (rawrec + offset); memcpy (blkt_405, cur_blkt->blktdata, sizeof (struct blkt_405_s)); offset += sizeof (struct blkt_405_s); if ( swapflag ) { ms_gswap2 (&blkt_405->delay_values); } if ( verbose > 0 ) { ms_log (1, "msr_pack_header_raw(%s): WARNING Blockette 405 cannot be fully supported\n", PACK_SRCNAME); } } else if ( cur_blkt->blkt_type == 500 ) { struct blkt_500_s *blkt_500 = (struct blkt_500_s *) (rawrec + offset); memcpy (blkt_500, cur_blkt->blktdata, sizeof (struct blkt_500_s)); offset += sizeof (struct blkt_500_s); if ( swapflag ) { ms_gswap4 (&blkt_500->vco_correction); MS_SWAPBTIME (&blkt_500->time); ms_gswap4 (&blkt_500->exception_count); } } else if ( cur_blkt->blkt_type == 1000 ) { struct blkt_1000_s *blkt_1000 = (struct blkt_1000_s *) (rawrec + offset); memcpy (blkt_1000, cur_blkt->blktdata, sizeof (struct blkt_1000_s)); offset += sizeof (struct blkt_1000_s); /* This guarantees that the byte order is in sync with msr_pack() */ if ( packdatabyteorder >= 0 ) blkt_1000->byteorder = packdatabyteorder; } else if ( cur_blkt->blkt_type == 1001 ) { struct blkt_1001_s *blkt_1001 = (struct blkt_1001_s *) (rawrec + offset); memcpy (blkt_1001, cur_blkt->blktdata, sizeof (struct blkt_1001_s)); offset += sizeof (struct blkt_1001_s); } else if ( cur_blkt->blkt_type == 2000 ) { struct blkt_2000_s *blkt_2000 = (struct blkt_2000_s *) (rawrec + offset); memcpy (blkt_2000, cur_blkt->blktdata, cur_blkt->blktdatalen); offset += cur_blkt->blktdatalen; if ( swapflag ) { ms_gswap2 (&blkt_2000->length); ms_gswap2 (&blkt_2000->data_offset); ms_gswap4 (&blkt_2000->recnum); } /* Nothing done to pack the opaque headers and data, they should already be packed into the blockette payload */ } else { memcpy (rawrec + offset, cur_blkt->blktdata, cur_blkt->blktdatalen); offset += cur_blkt->blktdatalen; } /* Pack the offset to the next blockette */ if ( cur_blkt->next ) { memcpy (rawrec + nextoffset, &offset, 2); if ( swapflag ) ms_gswap2 (rawrec + nextoffset); } else { memset (rawrec + nextoffset, 0, 2); } blktcnt++; cur_blkt = cur_blkt->next; } fsdh->numblockettes = blktcnt; if ( verbose > 2 ) ms_log (1, "%s: Packed %d blockettes\n", PACK_SRCNAME, blktcnt); return offset;} /* End of msr_pack_header_raw() *//*************************************************************************** * msr_update_header: * * Update the header values that change between records: start time, * sequence number, etc. * * Returns 0 on success or -1 on error. ***************************************************************************/static intmsr_update_header ( MSRecord *msr, char *rawrec, flag swapflag, flag verbose ){ struct fsdh_s *fsdh; char seqnum[7]; if ( ! msr || ! rawrec ) return -1; if ( verbose > 2 ) ms_log (1, "%s: Updating fixed section of data header\n", PACK_SRCNAME); fsdh = (struct fsdh_s *) rawrec; /* Pack values into the fixed section of header */ snprintf (seqnum, 7, "%06d", msr->sequence_number); memcpy (fsdh->sequence_number, seqnum, 6); ms_hptime2btime (msr->starttime, &(fsdh->start_time)); /* Swap byte order? */ if ( swapflag ) { MS_SWAPBTIME (&fsdh->start_time); } return 0;} /* End of msr_update_header() *//************************************************************************ * msr_pack_data: * * Pack Mini-SEED data samples. The input data samples specified as * 'src' will be packed with 'encoding' format and placed in 'dest'. * * If a pointer to a 32-bit integer sample is provided in the * argument 'lastintsample' and 'comphistory' is true the sample * value will be used to seed the difference buffer for Steim1/2 * encoding and provide a compression history. It will also be * updated with the last sample packed in order to be used with a * subsequent call to this routine. * * The number of samples packed will be placed in 'packsamples' and * the number of bytes packed will be placed in 'packbytes'. * * Return 0 on success and a negative number on error. ************************************************************************/static intmsr_pack_data (void *dest, void *src, int maxsamples, int maxdatabytes, int *packsamples, int32_t *lastintsample, flag comphistory, char sampletype, flag encoding, flag swapflag, flag verbose){ int retval; int nframes; int npacked; int32_t *intbuff; int32_t d0; /* Decide if this is a format that we can decode */ switch (encoding) { case DE_ASCII: if ( sampletype != 'a' ) { ms_log (2, "%s: Sample type must be ascii (a) for ASCII encoding not '%c'\n", PACK_SRCNAME, sampletype); return -1; } if ( verbose > 1 ) ms_log (1, "%s: Packing ASCII data\n", PACK_SRCNAME); retval = msr_pack_text (dest, src, maxsamples, maxdatabytes, 1, &npacked, packsamples); break; case DE_INT16: if ( sampletype != 'i' ) { ms_log (2, "%s: Sample type must be integer (i) for integer-16 encoding not '%c'\n", PACK_SRCNAME, sampletype); return -1; } if ( verbose > 1 ) ms_log (1, "%s: Packing INT-16 data samples\n", PACK_SRCNAME); retval = msr_pack_int_16 (dest, src, maxsamples, maxdatabytes, 1, &npacked, packsamples, swapflag); break; case DE_INT32: if ( sampletype != 'i' ) { ms_log (2, "%s: Sample type must be integer (i) for integer-32 encoding not '%c'\n", PACK_SRCNAME, sampletype); return -1; } if ( verbose > 1 ) ms_log (1, "%s: Packing INT-32 data samples\n", PACK_SRCNAME); retval = msr_pack_int_32 (dest, src, maxsamples, maxdatabytes, 1, &npacked, packsamples, swapflag); break; case DE_FLOAT32: if ( sampletype != 'f' ) { ms_log (2, "%s: Sample type must be float (f) for float-32 encoding not '%c'\n", PACK_SRCNAME, sampletype); return -1; } if ( verbose > 1 ) ms_log (1, "%s: Packing FLOAT-32 data samples\n", PACK_SRCNAME); retval = msr_pack_float_32 (dest, src, maxsamples, maxdatabytes, 1, &npacked, packsamples, swapflag); break; case DE_FLOAT64: if ( sampletype != 'd' ) { ms_log (2, "%s: Sample type must be double (d) for float-64 encoding not '%c'\n", PACK_SRCNAME, sampletype); return -1; } if ( verbose > 1 ) ms_log (1, "%s: Packing FLOAT-64 data samples\n", PACK_SRCNAME); retval = msr_pack_float_64 (dest, src, maxsamples, maxdatabytes, 1, &npacked, packsamples, swapflag); break; case DE_STEIM1: if ( sampletype != 'i' ) { ms_log (2, "%s: Sample type must be integer (i) for Steim-1 compression not '%c'\n", PACK_SRCNAME, sampletype); return -1; } intbuff = (int32_t *) src; /* If a previous sample is supplied use it for compression history otherwise cold-start */ d0 = ( lastintsample && comphistory ) ? (intbuff[0] - *lastintsample) : 0; if ( verbose > 1 ) ms_log (1, "%s: Packing Steim-1 data frames\n", PACK_SRCNAME); nframes = maxdatabytes / 64; retval = msr_pack_steim1 (dest, src, d0, maxsamples, nframes, 1, &npacked, packsamples, swapflag); /* If a previous sample is supplied update it with the last sample value */ if ( lastintsample && retval == 0 ) *lastintsample = intbuff[*packsamples-1]; break; case DE_STEIM2: if ( sampletype != 'i' ) { ms_log (2, "%s: Sample type must be integer (i) for Steim-2 compression not '%c'\n", PACK_SRCNAME, sampletype); return -1; } intbuff = (int32_t *) src; /* If a previous sample is supplied use it for compression history otherwise cold-start */ d0 = ( lastintsample && comphistory ) ? (intbuff[0] - *lastintsample) : 0; if ( verbose > 1 ) ms_log (1, "%s: Packing Steim-2 data frames\n", PACK_SRCNAME); nframes = maxdatabytes / 64; retval = msr_pack_steim2 (dest, src, d0, maxsamples, nframes, 1, &npacked, packsamples, swapflag); /* If a previous sample is supplied update it with the last sample value */ if ( lastintsample && retval == 0 ) *lastintsample = intbuff[*packsamples-1]; break; default: ms_log (2, "%s: Unable to pack format %d\n", PACK_SRCNAME, encoding); return -1; } return retval;} /* End of msr_pack_data() */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -