Example #1
0
/*
  nbytes = MG_NET_SENDTO(socket, variable, host, port)

  Sends the raw byte data from the IDL variable on the socket. Returns the
  number of bytes sent or -1 for error. Note: no byteswapping is performed.
*/
static IDL_VPTR IDL_CDECL mg_net_sendto(int argc, IDL_VPTR argv[], char *argk) {
  IDL_LONG i, iNum, iRet;
  struct sockaddr_in sin;
  IDL_VPTR vpTmp;
  char *pbuffer;
  short	port;
  int host, addr_len;

  i = IDL_LongScalar(argv[0]);
  if ((i < 0) || (i >= MAX_SOCKETS)) return (IDL_GettmpLong(-1));
  if (net_list[i].iState != NET_IO) return (IDL_GettmpLong(-1));
  IDL_ENSURE_SIMPLE(argv[1]);
  vpTmp = argv[1];
  port = (short) IDL_LongScalar(argv[3]);
  host = IDL_ULongScalar(argv[2]);

  if (vpTmp->type == IDL_TYP_STRING) {
    vpTmp  = IDL_CvtByte(1, &vpTmp);
  }

  IDL_VarGetData(vpTmp, &iNum, &pbuffer, 1);
  iNum = iNum * IDL_TypeSizeFunc(vpTmp->type);

  sin.sin_addr.s_addr = host;
  sin.sin_family = AF_INET;
  sin.sin_port = htons(port);
  addr_len = sizeof(struct sockaddr_in);

  iRet = sendto(net_list[i].socket, pbuffer, iNum, 0, (struct sockaddr *) &sin, addr_len);

  if (vpTmp != argv[1]) IDL_Deltmp(vpTmp);

  return(IDL_GettmpLong(iRet));
}
Example #2
0
/*
  nbytes = MG_NET_SEND(socket, variable [, host] [, port])

  Sends the raw byte data from the IDL variable on the socket. Returns the
  number of bytes sent or -1 for error. Note: no byteswapping is performed.

	When sending data from a UDP socket, you must specify the remote host and
	port arguments where host is the value returned from the MG_NET_NAME2HOST
	function.
*/
static IDL_VPTR IDL_CDECL mg_net_send(int argc, IDL_VPTR argv[], char *argk) {
  IDL_LONG i, iNum, iRet;
  IDL_VPTR vpTmp;
  char *pbuffer;

  i = IDL_LongScalar(argv[0]);
  if ((i < 0) || (i >= MAX_SOCKETS)) return(IDL_GettmpLong(-1));
  if ((net_list[i].iState != NET_IO) || (net_list[i].iType != NET_UDP_PEER))
    return(IDL_GettmpLong(-1));
  IDL_ENSURE_SIMPLE(argv[1]);
  vpTmp = argv[1];

  if (vpTmp->type == IDL_TYP_STRING) {
    vpTmp  = IDL_CvtByte(1, &vpTmp);
  }

  IDL_VarGetData(vpTmp, &iNum, &pbuffer, 1);
  iNum = iNum * IDL_TypeSizeFunc(vpTmp->type);

  iRet = send(net_list[i].socket, pbuffer, iNum, 0);

  if (vpTmp != argv[1]) IDL_Deltmp(vpTmp);

  return(IDL_GettmpLong(iRet));
}
Example #3
0
// Convert the input IDL_VPTR of IDL_STRINGs to a vector<string> all uppercase
int IDLStruct::IDL_STRING2StringVecUpper(
        IDL_VPTR svptr, vector<string>& tagnames)
{
    int status=0;
    IDL_MEMINT num=0;
    IDL_STRING* ptr;


    // Checking here, but better to check outside of this in case of memory
    // issues
    IDL_ENSURE_SIMPLE(svptr);

    if (svptr->type != IDL_TYP_STRING)
    {
        IDL_Message(IDL_M_NAMED_GENERIC, IDL_MSG_INFO, 
                "IDL_STRING2StringVecUpper: Input must be of type IDL_STRING");
        return(status);
    }

    IDL_VarGetData(svptr, &num, (char **) &ptr, IDL_TRUE);

    tagnames.resize(num);
    for (IDL_MEMINT i=0; i<num; i++)
    {
        string name=ptr[i].s;
        // Make upper case
        std::transform(name.begin(), name.end(), 
                name.begin(), (int(*)(int)) toupper);
        tagnames[i] = name;
    }
    status=1;
    return(status);
}
Example #4
0
// Convert the input numerical variable to a vector<IDL_MEMINT>
int IDLStruct::IDL_VAR2IDL_MEMINTVec(
        IDL_VPTR vptr, vector<IDL_MEMINT>& memintvec)
{
    int status=0;

    // Checking here, but better to check outside of this in case of memory
    // issues
    IDL_ENSURE_SIMPLE(vptr);

    int converted = 0;
    if (vptr->type != IDL_TYP_MEMINT)
        converted = 1;

    // Will return same variable if it is already type IDL_MEMINT
    // We have to explicitly release this mem; but at least IDL
    // will warn us if we forget
    IDL_VPTR vptr_use = IDL_CvtMEMINT(1, &vptr);


    // The true will ensure simple, which we have already done 
    IDL_MEMINT num;
    IDL_MEMINT* ptr;
    IDL_VarGetData(vptr_use, &num, (char **) &ptr, IDL_TRUE);

    memintvec.clear();

    if (num > 0)
    {
        memintvec.resize(num);
        for (IDL_MEMINT i=0; i<num; i++)
        {
            memintvec[i] = ptr[i];
        }

    }

    if (converted)
        IDL_Deltmp(vptr_use);

    status=1;
    return(status);

}
Example #5
0
IDL_VPTR rdObjmask(int argc, IDL_VPTR *argv, char *argk) {

    /* the desired atlas image. */
    ATLAS_IMAGE *ai;

    /* The fits file */
    FITS *fits;

    char *atlasFileName;  // The input file name

    IDL_VPTR idlistVptr; // optional idlist input keyword
    IDL_MEMINT* idlist;
    IDL_MEMINT nid=0;
    int idiscopy=0;

    IDL_MEMINT nrows=0;  // actual number of rows in file
    IDL_MEMINT i=0, j=0;
  
    IDL_VPTR result;  // The result and temporary copies
    char* resptr;
    char* rowptr;
    char* elptr;
    IDL_LONG elval;
    int elsize;

    // Structure definition
    void* sdef;

    // offsets structure
    offsetstruct os;



    // First make sure IDL_LONG is 4-byte int
    elsize=sizeof(IDL_LONG);
    if (elsize != 4)
    {
        IDL_Message(IDL_M_NAMED_GENERIC, IDL_MSG_LONGJMP, "sizeof(IDL_LONG) must be 4");
    }


    /* Get the keywords */
    (void) IDL_KWProcessByOffset(argc, argv, argk, kw_pars, 
            (IDL_VPTR *) 0, 1, &kw);

    /* Check arguments */
    if (nParams(argc) < 1)
    {
        IDL_Message(IDL_M_NAMED_GENERIC, IDL_MSG_INFO,
                "Syntax: struct = rdObjmask(atlasFile, idlist=, status=)");
        setStatus(FAILURE);
        return(IDL_GettmpInt(-1));   
    }




    /* get file name and open fits file */
    atlasFileName = getFileNameVal(argv[0]);

    if((fits = open_fits_table(atlasFileName, 1)) == NULL) {
        /* Memory is cleaned up in open_fits_table() */
        setStatus(FAILURE);
        return(IDL_GettmpInt(-1));   
    }
    nrows = fits->naxis2;




    // See if idlist was sent, else generate a list of all
    if (kw.idlist_there)
    {
        /* make sure it is a MEMINT. Copy if needed */
        idlistVptr = getAsMEMINT(argv[1], &idiscopy);
        IDL_VarGetData(idlistVptr, &nid, (char **) &idlist, IDL_TRUE);
    } 
    else
    {
        idiscopy=1;
        nid=nrows;
        idlist = (IDL_MEMINT *) calloc(nrows, sizeof(IDL_MEMINT));
        for (i=1;i<=nrows;i++)
            idlist[i-1] = i;
    }



    /* structure definition and create output */ 
    sdef = IDL_MakeStruct(0, stags);
    resptr = IDL_MakeTempStructVector(sdef, nid, &result, IDL_TRUE); 
    os = GetOffsets(sdef);




    /* Copy in the info */
    for (i=0;i<nid;i++)
    {
        // Point to the appropriate row in output data
        rowptr = (char *) result->value.s.arr->data + i*( result->value.arr->elt_len );

        // Copy id.  Do it here since if the object has no atlas image
        // it wouldn't be copied
        elval = (IDL_LONG) idlist[i];
        elptr = rowptr + os.id;
        memcpy(elptr, &elval, elsize);


        // Read the atlas image
        ai = read_atlas_image(fits, (int) idlist[i]);


        // In what follows, copy -1 if the ai is invalid
        if (ai != NULL)
        {
            // Copy run
            elval = (IDL_LONG) ai->run;
            elptr = rowptr + os.run;
            memcpy(elptr, &elval, elsize);
            // Copy rerun
            elval = (IDL_LONG) ai->rerun;
            elptr = rowptr + os.rerun;
            memcpy(elptr, &elval, elsize);
            // Copy camcol 
            elval = (IDL_LONG) ai->camCol;
            elptr = rowptr + os.camcol;
            memcpy(elptr, &elval, elsize);
            // Copy field
            elval = (IDL_LONG) ai->field;
            elptr = rowptr + os.field;
            memcpy(elptr, &elval, elsize);
            // Copy id
            //elval = (IDL_LONG) ai->id;
            //elptr = rowptr + os.id;
            //memcpy(elptr, &elval, elsize);
            // Copy parent 
            elval = (IDL_LONG) ai->parent;
            elptr = rowptr + os.parent;
            memcpy(elptr, &elval, elsize);


            // copy row0 
            elptr = rowptr + os.row0;
            for (j=0;j<5;j++)
            {
                if (ai->id > 0)
                    elval = (IDL_LONG) (ai->master_mask->rmin + ai->drow[j]);
                else
                    elval=-1;
                memcpy(elptr, &elval, elsize);
                elptr+=elsize;
            }
            // copy col0 
            elptr = rowptr + os.col0;
            for (j=0;j<5;j++)
            {
                if (ai->id > 0)
                    elval = (IDL_LONG) (ai->master_mask->cmin + ai->dcol[j]);
                else
                    elval=-1;
                memcpy(elptr, &elval, elsize);
                elptr+=elsize;
            }
            // copy rowmax
            elptr = rowptr + os.rowmax;
            for (j=0;j<5;j++)
            {
                if (ai->id > 0)
                    elval = (IDL_LONG) (ai->master_mask->rmax + ai->drow[j]);
                else
                    elval=-1;
                memcpy(elptr, &elval, elsize);
                elptr+=elsize;
            }
            // copy colmax 
            elptr = rowptr + os.colmax;
            for (j=0;j<5;j++)
            {
                if (ai->id > 0)
                    elval = (IDL_LONG) (ai->master_mask->cmax + ai->dcol[j]);
                else
                    elval=-1;
                memcpy(elptr, &elval, elsize);
                elptr+=elsize;
            }


            // copy drow
            elptr = rowptr + os.drow;
            for (j=0;j<5;j++)
            {
                if (ai->id > 0)
                    elval = (IDL_LONG) (ai->drow[j]);
                else
                    elval = -1;
                memcpy(elptr, &elval, elsize);
                elptr+=elsize;
            }
            // copy dcol 
            elptr = rowptr + os.dcol;
            for (j=0;j<5;j++)
            {
                if (ai->id > 0)
                    elval = (IDL_LONG) (ai->dcol[j]);
                else
                    elval = -1;
                memcpy(elptr, &elval, elsize);
                elptr+=elsize;
            }

            phAtlasImageDel(ai,1);
        }
    }

    /* clean up */
    phFitsDel(fits);  

    if (kw.idlist_there & idiscopy)
        IDL_Deltmp(idlistVptr);
    else
        free(idlist);

    /* Clean up the keyword info */
    IDL_KW_FREE;

    setStatus(SUCCESS);
    //return(IDL_GettmpInt(-1));   
    return(result);

}
Example #6
0
/*
  out = MG_NET_SELECT(sockets[], timeout)

  Checks to see if there is data waiting to be read or a connection has been
  requested for a list of sockets. The return value is -1 on error, scalar 0
  if no sockets are ready or returns a list of the sockets which are ready.
  The routine waits the number of seconds specified by the timeout argument
  for sockets to become ready. A timeout value of 0 results in a poll of the
  sockets.
*/
static IDL_VPTR IDL_CDECL mg_net_select(int argc, IDL_VPTR argv[], char *argk) {
  struct timeval timeval;
  fd_set rfds;

  IDL_LONG i, j;
  IDL_LONG n, num;

  float	fWait;
  IDL_LONG *piSocks,iNum;
  IDL_VPTR vpSocks;

  vpSocks = IDL_CvtLng(1, &(argv[0]));
  IDL_VarGetData(vpSocks, &iNum, (char **) &piSocks, 1);
  fWait = (float) IDL_DoubleScalar(argv[1]);

  num = -1;
  FD_ZERO(&rfds);

  for (j = 0; j < iNum; j++) {
    i = piSocks[j];
    if ((i < 0) || (i >= MAX_SOCKETS)) {
      if (vpSocks != argv[0]) IDL_Deltmp(vpSocks);
      return (IDL_GettmpLong(-1));
    }
    if (net_list[i].iState != NET_UNUSED) {
      FD_SET(net_list[i].socket, &rfds);
      if (net_list[i].socket > (SOCKET) num) num = net_list[i].socket;
    }
  }
  while (fWait >= 0.0) {
    if (fWait >= 2.0) {
      timeval.tv_sec = 2;
      timeval.tv_usec = 0;
    } else {
      timeval.tv_sec = (long) fWait;
      fWait = fWait - timeval.tv_sec;
      timeval.tv_usec = (long) (fWait * 1000000);
    }
    n = select(num + 1, &rfds, NULL, NULL, &timeval);
    if (n == -1) fWait = -1.0;
    if (n > 0) fWait = -1.0;
    fWait -= 2.0;
    if (IDL_BailOut(IDL_FALSE)) {
      n = -1;
      fWait = -1.0;
    }
  }

  if (n > 0) {
    IDL_LONG *pOut;
    IDL_VPTR vpTmp;

    pOut = (IDL_LONG *) IDL_MakeTempVector(IDL_TYP_LONG,
					   n, IDL_ARR_INI_NOP, &vpTmp);
    for (j = 0; j < iNum; j++) {
      i = piSocks[j];
      if (net_list[i].iState != NET_UNUSED) {
	if (FD_ISSET(net_list[i].socket, &rfds)){
	  *pOut++ = i;
	}
      }
    }
    if (vpSocks != argv[0]) IDL_Deltmp(vpSocks);
    return (vpTmp);
  }

  if (vpSocks != argv[0]) IDL_Deltmp(vpSocks);

  return (IDL_GettmpLong(n));
}
Example #7
0
void crehds( int argc, IDL_VPTR argv[] ) {
/*
** Declare variables
*/
char *hdsname;    /* The name of the HDS container file to create */
char *strucname;  /* The name of the top-level structure */
IDL_VPTR var;

int ndims;
hdsdim dims[DAT__MXDIM];

int status;        /* Starlink status */

HDSLoc* toploc = NULL;      /* Locator to top object */
int numtags;                /* Number of tags = 0 if not a structure */
char hdstype[DAT__SZTYP+1]; /* corresponding HDS type */
int fstat;                  /* Final status (before emsEload) */
char param[EMS__SZPAR+1];     /* Error message parameter name */
int parlen;                 /* Length of error message parameter name */
char opstr[EMS__SZMSG+1];     /* Error message */
int oplen;                  /* Length of error message */
char **taglist;             /* Pointer to taglist */
char *data;                 /* Pointer to data */
IDL_LONG nvals;             /* Number of values in var */
int elt_len;                /* Length of element of structure */
IDL_STRING *IDL_tags;
hdsdim IDL_ntags;

char defname[12]="IDL2HDS_OUT";

/* Start Error context */
   status = SAI__OK;
   emsMark();

/* Check that the correct number of arguments were passed in */
   if ( ( argc == 2 ) | (argc == 3 ) ) {
      var = argv[0];

      if ( argv[1]->flags & IDL_V_ARR ) {
         IDL_tags = (IDL_STRING *)argv[1]->value.arr->data;
         IDL_ntags = (hdsdim)argv[1]->value.arr->n_elts;
      } else {
         IDL_tags = &argv[1]->value.str;
         IDL_ntags = 1;
      }
      taglist = getstringarray( 1, &IDL_ntags, IDL_tags );

      if ( argc == 3 )
         hdsname = argv[2]->value.str.s;
      else
         hdsname = defname;
      strucname = hdsname;

      IDL_VarGetData( var, &nvals, &data, 0 );

      getobjectdetails( var, data, taglist,
         hdstype, &numtags, &ndims, dims, &elt_len, &status );
      hdsNew( hdsname, strucname, hdstype, ndims, dims, &toploc, &status );
      if ( numtags ) {
/* is a structure - invoke the structure handler */
         hdsstructwrite(
            toploc, data, taglist, numtags, ndims, dims, var, &status );
         retstringarray( taglist );
      } else {
         hdsprimwrite( toploc, hdstype, ndims, dims, data, &status );
      }

      datAnnul( &toploc, &status );

   } else {
      status = SAI__ERROR;
      emsRep( " ", "crehds: Incorrect number of arguments", &status );
   }

/*  Report any error messages */
/*  Adding Starlink-style !! and ! prefix */
   if ( status != SAI__OK ) {
      fstat = status;
      while ( status != SAI__OK ) {
         emsEload(
            param, &parlen, opstr, &oplen, &status );
         if ( status != SAI__OK )
            IDL_Message( IDL_M_NAMED_GENERIC, IDL_MSG_INFO, opstr );
      }
   }
   emsRlse();

/*  That's it, return to the calling routine */
   return;
}