⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 packdata.c

📁 C编写的格式转换程序
💻 C
📖 第 1 页 / 共 2 页
字号:
  XN = data[ns-1];  if ( swapflag ) ms_gswap4 (&XN);  dframes->f[0].ctrl = (dframes->f[0].ctrl<<2) | STEIM1_SPECIAL_MASK;    while (points_remaining > 0)    {      points_packed = 0;            /* Pack the next available data into the most compact form */      if (BYTEPACK(0,points_remaining))	{	  mask = STEIM1_BYTE_MASK;	  for (j=0; j<4; j++) { dframes->f[fn].w[wn].byte[j] = diff[j]; }	  points_packed = 4;	}      else if (HALFPACK(0,points_remaining))	{	  mask = STEIM1_HALFWORD_MASK;	  for (j=0; j<2; j++)	    {	      stmp = diff[j];	      if ( swapflag ) ms_gswap2 (&stmp);	      dframes->f[fn].w[wn].hw[j] = stmp;	    }	  points_packed = 2;	}      else	{	  mask = STEIM1_FULLWORD_MASK;	  itmp = diff[0];	  if ( swapflag ) ms_gswap4 (&itmp);	  dframes->f[fn].w[wn].fw = itmp;	  points_packed = 1;	}            /* Append mask for this word to current mask */      dframes->f[fn].ctrl = (dframes->f[fn].ctrl<<2) | mask;            points_remaining -= points_packed;      ipt += points_packed;            /* Check for full frame or full block */      if (++wn >= VALS_PER_FRAME)	{	  if ( swapflag ) ms_gswap4 (&dframes->f[fn].ctrl);	  /* Reset output index to beginning of frame */	  wn = 0;	  /* If block is full, output block and reinitialize */	  if (++fn >= nf) break;	  dframes->f[fn].ctrl = 0;	}            /* Shift and re-fill difference and minbits buffers */      for ( i=points_packed; i < 4; i++ )	{	  /* Shift remaining buffer entries */	  diff[i-points_packed] = diff[i];	  minbits[i-points_packed] = minbits[i];	}      for ( i=4-points_packed,j=ipt+(4-points_packed); i < 4 && j < ns; i++,j++ )	{	  /* Re-fill entries */	  diff[i] = data[j] - data[j-1];	  MINBITS(diff[i],minbits[i]);	}    }    /* Update XN value in first frame */  XN = data[(ns-1)-points_remaining];  if ( swapflag ) ms_gswap4 (&XN);    /* End of data.  Pad current frame and optionally rest of block */  /* Do not pad and output a completely empty block */  if ( ! EMPTY_BLOCK(fn,wn) )    {      *pnframes = pad_steim_frame (dframes, fn, wn, nf, swapflag, pad);    }  else    {      *pnframes = 0;    }    *pnsamples = ns - points_remaining;    return 0;}/************************************************************************ *  msr_pack_steim2:							* *	Pack data into STEIM1 data frames.				* *  return:								* *	0 on success.							* *	-1 on error.                                                    * ************************************************************************/int msr_pack_steim2 (DFRAMES      *dframes,	/* ptr to data frames                   */  int32_t      *data,		/* ptr to unpacked data array           */  int32_t       d0,		/* first difference value               */  int		ns,		/* number of samples to pack            */  int		nf,		/* total number of data frames to pack  */  int		pad,		/* flag to specify padding to nf        */  int	       *pnframes,	/* number of frames actually packed     */  int	       *pnsamples,	/* number of samples actually packed    */  int           swapflag)       /* if data should be swapped            */{  int		points_remaining = ns;  int           points_packed = 0;  int32_t       diff[7];        /* array of differences                 */  uint8_t       minbits[7];     /* array of minimum bits for diffs      */  int		i, j;  int		mask;  int		ipt = 0;	/* index of initial data to pack.	*/  int		fn = 0;		/* index of initial frame to pack.	*/  int		wn = 2;		/* index of initial word to pack.	*/    /* Calculate initial difference and minbits buffers */  diff[0] = d0;  MINBITS(diff[0],minbits[0]);  for (i=1; i < 7 && i < ns; i++)    {      diff[i] = data[i] - data[i-1];      MINBITS(diff[i],minbits[i]);    }    dframes->f[fn].ctrl = 0;    /* Set X0 and XN values in first frame */  X0 = data[0];  if ( swapflag ) ms_gswap4 (&X0);  dframes->f[0].ctrl = (dframes->f[0].ctrl<<2) | STEIM2_SPECIAL_MASK;  XN = data[ns-1];  if ( swapflag ) ms_gswap4 (&XN);  dframes->f[0].ctrl = (dframes->f[0].ctrl<<2) | STEIM2_SPECIAL_MASK;    while (points_remaining > 0)    {      points_packed = 0;            /* Pack the next available datapoints into the most compact form */      if (BIT4PACK(0,points_remaining))	{	  PACK(4,7,0x0000000f,02)	  if ( swapflag ) ms_gswap4 (&dframes->f[fn].w[wn].fw);	  mask = STEIM2_567_MASK;	  points_packed = 7;	}      else if (BIT5PACK(0,points_remaining))	{	  PACK(5,6,0x0000001f,01)	    if ( swapflag ) ms_gswap4 (&dframes->f[fn].w[wn].fw);	  mask = STEIM2_567_MASK;	  points_packed = 6;	}      else if (BIT6PACK(0,points_remaining))	{	  PACK(6,5,0x0000003f,00)	  if ( swapflag ) ms_gswap4 (&dframes->f[fn].w[wn].fw);	  mask = STEIM2_567_MASK;	  points_packed = 5;	}      else if (BYTEPACK(0,points_remaining))	{	  mask = STEIM2_BYTE_MASK;	  for (j=0; j<4; j++) dframes->f[fn].w[wn].byte[j] = diff[j];	  points_packed = 4;	}      else if (BIT10PACK(0,points_remaining))	{	  PACK(10,3,0x000003ff,03)	  if ( swapflag ) ms_gswap4 (&dframes->f[fn].w[wn].fw);	  mask = STEIM2_123_MASK;	  points_packed = 3;	}      else if (BIT15PACK(0,points_remaining))	{	  PACK(15,2,0x00007fff,02)	  if ( swapflag ) ms_gswap4 (&dframes->f[fn].w[wn].fw);	  mask = STEIM2_123_MASK;	  points_packed = 2;	}      else if (BIT30PACK(0,points_remaining))	{	  PACK(30,1,0x3fffffff,01)	  if ( swapflag ) ms_gswap4 (&dframes->f[fn].w[wn].fw);	  mask = STEIM2_123_MASK;	  points_packed = 1;	}      else	{	  ms_log (2, "msr_pack_steim2(%s): Unable to represent difference in <= 30 bits\n",		  PACK_SRCNAME);	  return -1;	}            /* Append mask for this word to current mask */      dframes->f[fn].ctrl = (dframes->f[fn].ctrl<<2) | mask;            points_remaining -= points_packed;      ipt += points_packed;            /* Check for full frame or full block */      if (++wn >= VALS_PER_FRAME)	{	  if ( swapflag ) ms_gswap4 (&dframes->f[fn].ctrl);	  /* Reset output index to beginning of frame */	  wn = 0;	  /* If block is full, output block and reinitialize */	  if (++fn >= nf) break;	  dframes->f[fn].ctrl = 0;	}            /* Shift and re-fill difference and minbits buffers */      for ( i=points_packed; i < 7; i++ )	{	  /* Shift remaining buffer entries */	  diff[i-points_packed] = diff[i];	  minbits[i-points_packed] = minbits[i];	}      for ( i=7-points_packed,j=ipt+(7-points_packed); i < 7 && j < ns; i++,j++ )	{	  /* Re-fill entries */	  diff[i] = data[j] - data[j-1];	  MINBITS(diff[i],minbits[i]);	}    }    /* Update XN value in first frame */  XN = data[(ns-1)-points_remaining];  if ( swapflag ) ms_gswap4 (&XN);    /* End of data.  Pad current frame and optionally rest of block */  /* Do not pad and output a completely empty block */  if ( ! EMPTY_BLOCK(fn,wn) )    {      *pnframes = pad_steim_frame (dframes, fn, wn, nf, swapflag, pad);    }  else    {      *pnframes = 0;    }    *pnsamples = ns - points_remaining;    return 0;}/************************************************************************ *  pad_steim_frame:							* *	Pad the rest of the data record with null values,		* *	and optionally the rest of the total number of frames.		* *  return:								* *	total number of frames in record.				* ************************************************************************/static int pad_steim_frame (DFRAMES      *dframes,  int		fn,	    	/* current frame number.		*/  int	    	wn,		/* current work number.			*/  int	    	nf,		/* total number of data frames.		*/  int		swapflag,	/* flag to swap byte order of data.	*/  int	    	pad)		/* flag to pad # frames to nf.		*/{  /* Finish off the current frame */  if (wn < VALS_PER_FRAME && fn < nf)    {      for (; wn < VALS_PER_FRAME; wn++)	{	  dframes->f[fn].w[wn].fw = 0;	  dframes->f[fn].ctrl = (dframes->f[fn].ctrl<<2) | STEIM1_SPECIAL_MASK;	}      if ( swapflag ) ms_gswap4 (&dframes->f[fn].ctrl);      fn++;    }    /* Fill the remaining frames in the block */  if (pad)    {      for (; fn<nf; fn++)	{	  dframes->f[fn].ctrl = STEIM1_SPECIAL_MASK;	/* mask for ctrl */	  for (wn=0; wn<VALS_PER_FRAME; wn++)	    {	      dframes->f[fn].w[wn].fw = 0;	      dframes->f[fn].ctrl = (dframes->f[fn].ctrl<<2) | STEIM1_SPECIAL_MASK;	    }	  	  if ( swapflag ) ms_gswap4 (&dframes->f[fn].ctrl);	}    }    return fn;}/************************************************************************ *  msr_pack_text:						       	* *	Pack text data into text format.  Split input data on line	**	breaks so as to not split lines between records.		* *	Return: 0 on success, -1 on error.				* ************************************************************************/int msr_pack_text (char 	       *packed,         /* output data array - packed.		*/  char	       *data,		/* input data array - unpacked.		*/  int		ns,		/* desired number of samples to pack.	*/  int		max_bytes,	/* max # of bytes for output buffer.	*/  int		pad,		/* flag to specify padding to max_bytes.*/  int	       *pnbytes,	/* number of bytes actually packed.	*/  int	       *pnsamples)	/* number of samples actually packed.	*/{  int points_remaining = ns;	/* number of samples remaining to pack.	*/  int		last = -1;  int		nbytes;  int		i;    /* Split lines only if a single line will not fit in 1 record */  if (points_remaining > max_bytes)    {      /* Look for the last newline that will fit in output buffer */      for (i=points_remaining-1; i>=0; i--)	{	  if (data[i] == '\n') {	    last = i;	    break;	  }	}      if (last < 0) last = max_bytes - 1;    }    if (last < 0) last = points_remaining - 1;  nbytes = last + 1;  memcpy (packed, data, nbytes);  packed += nbytes;  max_bytes -= nbytes;  *pnbytes = nbytes;  *pnsamples = nbytes;  points_remaining -= nbytes;    /* Pad miniSEED block if necessary */  if (pad)    {      memset ((void *)packed, 0, max_bytes);      *pnbytes += max_bytes;    }    *pnsamples = ns - points_remaining;    return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -