# include <stdio.h>

int nims_expand_hrs(work,ibkg,idata)
/******************************************************************************
*_Title  NIMS_EXPAND_HRS  Expand NIMS HRS (background & data) into 16-bit words

*_Args	Type                Variable	   I/O Description		      */
	unsigned char       work[90];    /* !I  Buffer of 72 10-bit data items*/
	unsigned short int  ibkg[4];     /* !O  Buffer of 4 background items  */
	unsigned short int  idata[68];   /* !O  Buffer of 17*4 sensor items   */
/*
*_Descr Expand NIMS HRS background and sensor data from packed 10-bit form
*	to 16-bit signed integers: 4 background items and 17*4=68 sensor
*	items.  Each contiguous 5-byte segment containing 4 10-bit numbers
*	is expanded to 4 16-bit numbers.

*_Calls NIMS_EXPAND4 (not generally portable)

*_Keys  NIMS, STRING

*_Hist	16feb90  R Mehlman UCLA/IGPP Original Version (VAX/VMS Fortran)
*	26may92  RMX  Fixed incorrect WORK dimension: (18.5) should have been
*			(5,18) but didn't actually matter.  Made it WORK(90).
*	02nov92  RMX  Renamed from HRS_EXPAND.  NIMEXP changed to NIMS_EXPAND4.
*	28feb93  RMX  Portable C version (calls C or Assembler version of
*			 NIMS_EXPAND4, which is not generally portable)
*_End
*******************************************************************************/
  {
    unsigned char *ip;		/* Address of current 5-byte input set */
    unsigned short int *jp;	/* Address of current 4-word output set */
    long k;			/* Index of 4-word output set */

    nims_expand4(work, ibkg);	/* Expand 4 background items */

    ip = work+5;		/* Point to start of input sensor items */
    jp = idata;			/* Point to start of output buffer */
    for (k = 0; k < 17; k++) {  /* Loop on 17 sets of 4 sensor items */
      nims_expand4(ip, jp);	/*  Expand set */
      ip = ip + 5;		/*  Increment input pointer */
      jp = jp + 4;		/*  Increment output pointer */
    }      			/* End loop on 17 sets of 4 sensor items */
    return (0);
  }
/*******************************************************************************
* In SunOS systems, the fortran compiler will insert an underscore character (_)
* at the end of all subroutine names. The code enclosed by the "ifdef sun"
* preprocesser statement will fortran routines to also call the
* nims_expand routine.
*******************************************************************************/
#ifdef sun
int nims_expand_hrs_(work,ibkg,idata)
	unsigned char       work[90];    /* !I  Buffer of 72 10-bit data items*/
	unsigned short int  ibkg[4];     /* !O  Buffer of 4 background items  */
	unsigned short int  idata[68];   /* !O  Buffer of 17*4 sensor items   */
{
   nims_expand_hrs(work,ibkg,idata);
   return(0);
}
#endif
# include <stdio.h>

int nims_expand4(ibuf,obuf)
/******************************************************************************
* The definition of these sub-scripts takes care of the byte swapping
* required on VAX systems.
******************************************************************************/
#ifdef vax
#define SCP1 0
#define SCP2 1
#else
#define SCP1 1
#define SCP2 0
#endif
/*******************************************************************************
*_Title NIMS_EXPAND4  Expand NIMS data: 5-byte input to 4 INT*2 words

*_Args	Type                Variable   I/O  Description			      */
	unsigned char       ibuf[5]; /* !I  Input buffer of 4 10-bit items    */
	unsigned short int  obuf[4]; /* !O  Output buffer of 4 16-bit items   */
/*
*_Desc	Expand 5-byte data or background segment of NIMS HRS packet from
*	Galileo/NIMS EDR:  4 10-bit items into 4 16-bit (INTEGER*2) items.

*_Lims  VAX-specific version

*_Keys	NIMS, STRING

*_Hist	17oct83  R. Mehlman UCLA/IGPP ORIGINAL VAX ASSEMBLER VERSION
*			Used by NIMS SE programs.
*			Recoded for VAX from WDS PDP-11 XPND rtne in DESE.MAC
*	16feb90  RMX  NIMS EDR processing version:  documented
*	02nov92  RMX  Renamed from NIMEXP.
*	26feb93  RMX  VAX C version (not generally portable)
*       18mar93  EME  Make routine compatible with SunOS systems
*_End
******************************************************************************/
  {
    unsigned short int mask10 = 0x03ff;
    union
      {
	unsigned char work1[4][2];
	unsigned short int work2[4];
      } work;

    work.work1[0][SCP1] = ibuf[1];
    work.work1[0][SCP2] = ibuf[0];
    obuf[0] = (work.work2[0] >> 6) & mask10;

    work.work1[1][SCP1] = ibuf[2];
    work.work1[1][SCP2] = ibuf[1];
    obuf[1] = (work.work2[1] >> 4) & mask10;

    work.work1[2][SCP1] = ibuf[3];
    work.work1[2][SCP2] = ibuf[2];
    obuf[2] = (work.work2[2] >> 2) & mask10;

    work.work1[3][SCP1] = ibuf[4];
    work.work1[3][SCP2] = ibuf[3];
    obuf[3] = work.work2[3] & mask10;

    return (0);
  }

/*******************************************************************************
* In SunOS systems, the fortran compiler will insert an underscore character (_)
* at the end of all subroutine names. The code enclosed by the "ifdef sun"
* preprocesser statement will fortran routines to also call the
* nims_expand routine.
*******************************************************************************/
#ifdef sun
int nims_expand4_(ibuf,obuf)
   unsigned char       ibuf[5]; /* !I  Input buffer of 4 10-bit items    */
   unsigned short int  obuf[4]; /* !O  Output buffer of 4 16-bit items   */
{
   nims_expand4(ibuf,obuf);
   return(0);
}
#endif
