// From an IDL_StructDefPtr char* IDLStruct::MakeStruct( IDL_StructDefPtr defptr, IDL_MEMINT len, IDL_VPTR* vptr) { char* result; result = IDL_MakeTempStructVector( defptr, len, vptr, IDL_TRUE); return(result); }
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); }
int pgsql_query_send(char *query, char *connect_info, IDL_VPTR *resultVptr) { /* connection info */ PGconn *conn=NULL; PGresult *res=NULL; int query_status; /* Infor about each field */ field_info *fi; /* Structure definition info */ idl_tag_info *ti; //UCHAR *dataPtr; char *dataPtr; /* temporary pointer to tag data */ UCHAR *tptr; /* loop variables */ long long row; int tag; /* binary or ascii? Only need ascii for file output */ int binary; int verbose=0; /* Attempt to establish the connection */ conn = PQconnectdb(connect_info); if (PQstatus(conn) != CONNECTION_OK) { pgsql_query_error("Could not establish connection", PQerrorMessage(conn)); PQfinish(conn); return(MYPG_CONNECT_FAILURE); } /* send the query and return the results */ if (kw.file_there) binary = 0; else binary = 1; if (kw.verbose_there) if (kw.verbose) verbose = 1; if (verbose) IDL_Message(IDL_M_NAMED_GENERIC, IDL_MSG_INFO, "Querying database non-asynchronously: you may not cancel. For requests you may cancel don't send the /no_async keyword"); res = PQexecParams(conn, query, 0, /* number of parameters (none) */ NULL, /* see doc for the parameter info */ NULL, NULL, NULL, binary); /* 0 for text, 1 for binary (network order) */ /* Success? */ query_status = pgsql_query_checkstatus(res); if (query_status != MYPG_SUCCESS) { prepExit(conn, res); return(query_status); } /* See if the user input a file to write to */ if (kw.file_there) { int write_status; write_status = pgsql_write_file(res); prepExit(conn, res); return(write_status); } /* Get information for each returned field */ fi = pgsql_get_field_info(res); /* Copy into output keywords, if they exist */ pgsql_copy_info(fi); /* Get info to make struct and copy data */ ti = pgsql_get_idl_tag_info(fi->tagdefs); /* Create the output structure */ if (verbose) IDL_Message(IDL_M_NAMED_GENERIC, IDL_MSG_INFO, "Creating output struct"); dataPtr = IDL_MakeTempStructVector(ti->sdef, (IDL_MEMINT) fi->nTuples, resultVptr, IDL_TRUE); /* Copy into output variable */ if (verbose) IDL_Message(IDL_M_NAMED_GENERIC, IDL_MSG_INFO, "Copying data"); for (row=0; row< fi->nTuples; row++) for (tag = 0; tag < fi->nFields; tag++) { tptr = ( (*resultVptr)->value.s.arr->data + row*( (*resultVptr)->value.arr->elt_len) + ti->tagOffsets[tag]); pgsql_store_binary(ti->tagDesc[tag]->type, fi->field_isarray[tag], PQgetvalue(res, row, tag), tptr); } if (verbose) IDL_Message(IDL_M_NAMED_GENERIC, IDL_MSG_INFO, "Cleaning up"); pgsql_freemem(fi, ti); PQclear(res); PQfinish(conn); if (verbose) IDL_Message(IDL_M_NAMED_GENERIC, IDL_MSG_INFO, "Done"); return(MYPG_SUCCESS); }
int pgsql_query_send_async(char *query, char *connect_info, IDL_VPTR *resultVptr) { /* connection info */ int query_status; PGconn *conn=NULL; PGresult *res=NULL; PGcancel *cancel_obj; /* Information about each field */ field_info *fi; /* Structure definition info */ idl_tag_info *ti; //UCHAR *dataPtr; char *dataPtr; /* temporary pointer to tag data */ UCHAR *tptr; /* loop variables */ long long row; int tag; /* binary or ascii? Only need ascii for file output */ int binary; int estatus; int verbose=0; /* We must reset this each time */ cancel_query = 0; /* Attempt to establish the connection */ conn = PQconnectdb(connect_info); if (PQstatus(conn) != CONNECTION_OK) { pgsql_query_error("Could not establish connection", PQerrorMessage(conn)); PQfinish(conn); return(MYPG_CONNECT_FAILURE); } /* send the query and return the results */ if (kw.file_there) binary = 0; else binary = 1; if (kw.verbose_there) if (kw.verbose) verbose = 1; if (verbose) IDL_Message(IDL_M_NAMED_GENERIC, IDL_MSG_INFO, "Querying database (^C to cancel)"); if (! PQsendQueryParams(conn, query, 0, NULL, NULL, NULL, NULL, binary) ) { prepExit(conn, res); return(MYPG_DISPATCH_ERROR); } if (! (cancel_obj = PQgetCancel(conn)) ) { IDL_Message(IDL_M_NAMED_GENERIC, IDL_MSG_INFO, "Cancel object is NULL"); return(MYPG_CANCEL_FAILURE); } /* Only allow SIGINT after this point, since it calls cancel */ pgsql_sigint_register(); /* note this is a busy loop. I tried sleeping, but it really slows down the job */ PQconsumeInput(conn); //Try to collect the results while (PQisBusy(conn)) // while not ready ... { if (cancel_query) { char errbuf[256]; IDL_Message(IDL_M_NAMED_GENERIC, IDL_MSG_INFO, "Canceling query at user request"); if (!PQcancel(cancel_obj, errbuf, 256) ) { estatus = MYPG_CANCEL_FAILURE; IDL_Message(IDL_M_NAMED_GENERIC, IDL_MSG_INFO, errbuf); } else estatus = MYPG_QUERY_CANCELLED; pgsql_sigint_unregister(); /* This will call PQfinish and PQclear clear the memory */ prepExit(conn, res); return(estatus); } PQconsumeInput(conn); //...retry } /* No signal handling beyond this point */ pgsql_sigint_unregister(); if (verbose) IDL_Message(IDL_M_NAMED_GENERIC, IDL_MSG_INFO, "Getting result"); res = PQgetResult(conn); /* Success? */ query_status = pgsql_query_checkstatus(res); if (query_status != MYPG_SUCCESS) { prepExit(conn, res); return(query_status); } /* See if the user input a file to write to */ if (kw.file_there) { int write_status; write_status = pgsql_write_file(res); prepExit(conn, res); return(write_status); } /* Get information for each returned field */ fi = pgsql_get_field_info(res); /* Copy into output keywords, if they exist */ pgsql_copy_info(fi); /* Get info to make struct and copy data */ ti = pgsql_get_idl_tag_info(fi->tagdefs); /* Create the output structure */ if (verbose) IDL_Message(IDL_M_NAMED_GENERIC, IDL_MSG_INFO, "Creating output struct"); dataPtr = IDL_MakeTempStructVector(ti->sdef, (IDL_MEMINT) fi->nTuples, resultVptr, IDL_TRUE); /* Copy into output variable */ if (verbose) IDL_Message(IDL_M_NAMED_GENERIC, IDL_MSG_INFO, "Copying data"); for (row=0; row< fi->nTuples; row++) for (tag = 0; tag < fi->nFields; tag++) { tptr = ( (*resultVptr)->value.s.arr->data + row*( (*resultVptr)->value.arr->elt_len) + ti->tagOffsets[tag]); pgsql_store_binary(ti->tagDesc[tag]->type, fi->field_isarray[tag], PQgetvalue(res, row, tag), tptr); } if (verbose) IDL_Message(IDL_M_NAMED_GENERIC, IDL_MSG_INFO, "Cleaning up"); pgsql_freemem(fi, ti); PQclear(res); PQfinish(conn); if (verbose) IDL_Message(IDL_M_NAMED_GENERIC, IDL_MSG_INFO, "Done"); return(MYPG_SUCCESS); }