static void free_memory() { IDL_Deltmp(vxx2); xx2=NULL; IDL_Deltmp(vyy2); yy2=NULL; IDL_Deltmp(vzz2); zz2=NULL; if(nchunk2!=NULL) unassignchunks(&nchunk2,&chunklist2,nra,ndec); if(rabounds!=NULL) unsetchunks(&rabounds,&decbounds,&nra,&ndec); nchunk2=NULL; chunklist2=NULL; rabounds=NULL; decbounds=NULL; nra=NULL; }
/* 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)); }
/* 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)); }
// 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); }
static void IDL_CDECL IDL_mg_print(int argc, IDL_VPTR *argv, char *argk) { int nargs; char *format, *cformat; IDL_VPTR origFormat, vcformat; typedef struct { IDL_KW_RESULT_FIRST_FIELD; IDL_VPTR format; int format_present; } KW_RESULT; static IDL_KW_PAR kw_pars[] = { { "FORMAT", IDL_TYP_STRING, 1, IDL_KW_VIN, IDL_KW_OFFSETOF(format_present), IDL_KW_OFFSETOF(format) }, { NULL } }; KW_RESULT kw; nargs = IDL_KWProcessByOffset(argc, argv, argk, kw_pars, NULL, 1, &kw); if (kw.format_present) { origFormat = argv[argc - 1]; format = IDL_VarGetString(origFormat); cformat = (char *) calloc(strlen(format) + 5 + 1, sizeof(char)); sprintf(cformat, "(%%\"%s\")", format); vcformat = IDL_StrToSTRING(cformat); argv[argc - 1] = vcformat; } IDL_Print(argc, argv, argk); if (kw.format_present) { argv[argc - 1] = origFormat; IDL_Deltmp(vcformat); free(cformat); } IDL_KW_FREE; }
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); }
/* 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)); }
void idlprimfill( HDSLoc *cloc, IDL_VPTR datav, void *datptr, int *status ) { int j; /* loop counters */ UCHAR idltype; /* The IDL type */ char type[DAT__SZTYP+1]; /* Type in which to to map HDS data */ int bpix; /* Number of bytes/value */ int defined; /* If HDS value defined */ void *cpntr; /* True C pointer to mapped data */ size_t nels; /* Number of mapped elements */ int nels_i; size_t nbytes; /* Number of bytes in array */ int flen; /* length of HDS strings */ int clen; /* length of corresponding C string */ char *chars; /* pointer to imported characters */ IDL_STRING *strings; /* pointer to array of string structures */ IDL_VPTR chptr; /* Scratch variable pointer */ if ( *status != SAI__OK ) return; /* check type compatibility */ /* Get the number of bytes per element */ /* and the HDS type in which to map the data */ idltype = datav->type; switch (idltype) { case IDL_TYP_FLOAT: strcpy( type, "_REAL" ); bpix = 4; break; case IDL_TYP_LONG: strcpy( type, "_INTEGER" ); bpix = 4; break; case IDL_TYP_INT: strcpy( type, "_WORD" ); bpix = 2; break; case IDL_TYP_DOUBLE: strcpy( type, "_DOUBLE" ); bpix = 8; break; case IDL_TYP_BYTE: strcpy( type, "_UBYTE" ); bpix = 1; break; case IDL_TYP_STRING: datType( cloc, type, status); bpix = 1; break; default: /* flag no data to copy */ bpix = 0; *status = SAI__ERROR; emsSeti( "TYPE", idltype ); emsRep( " ", "Illegal IDL type ^TYPE", status ); break; } /* end of case */ if ( (*status == SAI__OK ) && bpix ) { /* Map the data as if a vector - provided it is defined */ datState( cloc, &defined, status ); if ( defined ) { datMapV( cloc, type, "READ", &cpntr, &nels, status ); if ( *status != SAI__OK ) { emsRep(" ", "Failed to map HDS component", status ); } else { if ( idltype == IDL_TYP_STRING ) { flen = atoi( type + 6 ); clen = flen + 1; /* Import the Fortran strings to C */ nels_i = (int)nels; chars = IDL_GetScratch( &chptr, nels_i, clen ); cnfImprta( cpntr, flen, chars, clen, 1, &nels_i ); /* set strings to be a pointer to the IDL_STRING structure(s) */ strings = (IDL_STRING *)datptr; /* store the imported strings into the STRING structures */ for ( j=0; j<nels; j++ ) IDL_StrStore( strings+j, &chars[j*clen] ); IDL_Deltmp( chptr ); } else { /* Type other than string */ if ( datav->flags & IDL_V_ARR ) { /* Number Array */ /* copy the data to the array */ nbytes = bpix * nels; memcpy( datptr, cpntr, nbytes ); } else { /* Number Scalar */ switch (idltype) { case IDL_TYP_FLOAT: ((IDL_ALLTYPES *)datptr)->f = *(float *)cpntr; break; case IDL_TYP_LONG: ((IDL_ALLTYPES *)datptr)->l = *(int *)cpntr; break; case IDL_TYP_INT: ((IDL_ALLTYPES *)datptr)->i = *(short *)cpntr; break; case IDL_TYP_DOUBLE: ((IDL_ALLTYPES *)datptr)->d = *(double *)cpntr; break; case IDL_TYP_BYTE: ((IDL_ALLTYPES *)datptr)->c = *(UCHAR *)cpntr; break; } /* end of case */ } /* end of if array */ } /* end if string */ datUnmap( cloc, status ); } /* end of mapped data */ } /* end of if defined */ } /* end of bpix non-zero */ return; }
static IDL_VPTR IDLOldRawOpen(int argc,IDL_VPTR *argv) { IDL_VPTR vrawfp; struct OldRawIDLFp *irawfp; struct OldRawFp *rawfp; void *s; int st; static IDL_MEMINT hdim[]={1,80}; static IDL_MEMINT ddim[]={1,32}; static IDL_MEMINT edim[]={1,256}; static IDL_STRUCT_TAG_DEF trawfp[]={ {"RAWUNIT",0,(void *) IDL_TYP_LONG}, {"INXUNIT",0,(void *) IDL_TYP_LONG}, {"RAW_RECL",0,(void *) IDL_TYP_LONG}, {"INX_RECL",0,(void *) IDL_TYP_LONG}, {"BLEN",0,(void *) IDL_TYP_LONG}, {"INX_SREC",0,(void *) IDL_TYP_LONG}, {"INX_EREC",0,(void *) IDL_TYP_LONG}, {"CTIME",0,(void *) IDL_TYP_DOUBLE}, {"STIME",0,(void *) IDL_TYP_DOUBLE}, {"ETIME",0,(void *) IDL_TYP_DOUBLE}, {"TIME",0,(void *) IDL_TYP_LONG}, {"HEADER",hdim,(void *) IDL_TYP_BYTE}, {"DATE",ddim,(void *) IDL_TYP_BYTE}, {"EXTRA",edim,(void *) IDL_TYP_BYTE}, {"MAJOR_REV",0,(void *) IDL_TYP_BYTE}, {"MINOR_REV",0,(void *) IDL_TYP_BYTE}, {"RAWREAD",0,(void *) IDL_TYP_LONG}, {"BNUM",0,(void *) IDL_TYP_LONG}, 0}; static IDL_MEMINT ilDims[IDL_MAX_ARRAY_DIM]; IDL_VARIABLE unit; IDL_VPTR fargv[2]; IDL_FILE_STAT stat; int ffd=-1,ifd=-1; IDL_ENSURE_STRING(argv[0]); if (argc>1) IDL_ENSURE_STRING(argv[1]); s=IDL_MakeStruct("RAWFP",trawfp); ilDims[0]=1; irawfp=(struct OldRawIDLFp *) IDL_MakeTempStruct(s,1,ilDims,&vrawfp,TRUE); unit.type=IDL_TYP_LONG; unit.flags=0; fargv[0]=&unit; fargv[1]=argv[0]; IDL_FileGetUnit(1,fargv); IDL_FileOpen(2,fargv,NULL,IDL_OPEN_R,IDL_F_STDIO,1,0); irawfp->rawunit=IDL_LongScalar(&unit); irawfp->inxunit=-1; st=IDL_FileEnsureStatus(IDL_MSG_RET,irawfp->rawunit,IDL_EFS_USER); if (st==FALSE) { st=0; IDL_Deltmp(vrawfp); return (IDL_GettmpLong(st)); } if (argc>1) { fargv[1]=argv[1]; IDL_FileGetUnit(1,fargv); IDL_FileOpen(2,fargv,NULL,IDL_OPEN_R,IDL_F_STDIO,1,0); irawfp->inxunit=IDL_LongScalar(&unit); st=IDL_FileEnsureStatus(IDL_MSG_RET,irawfp->rawunit,IDL_EFS_USER); if (st==FALSE) { /* free unit for index but continue */ IDL_FileFreeUnit(1,argv); irawfp->inxunit=-1; } } IDL_FileStat(irawfp->rawunit,&stat); ffd=fileno(stat.fptr); if (irawfp->inxunit !=-1) { IDL_FileStat(irawfp->inxunit,&stat); ifd=fileno(stat.fptr); } rawfp=OldRawOpenFd(ffd,ifd); OldRawRawFpToIDL(rawfp,irawfp); free(rawfp); return vrawfp; }