// on success, swi.data points to malloced data. int seti_parse_data(FILE* f, ANALYSIS_STATE& state) { unsigned long nbytes, nsamples,samples_per_byte; sah_complex *data; unsigned long i; char *p, buf[256]; sah_complex *bin_data=0; int retval=0; FORCE_FRAME_POINTER; nsamples = swi.nsamples; samples_per_byte=(8/swi.bits_per_sample); data = (sah_complex *)malloc_a(nsamples*sizeof(sah_complex), MEM_ALIGN); bin_data = (sah_complex *)malloc_a(nsamples*sizeof(sah_complex), MEM_ALIGN); if (!data) SETIERROR(MALLOC_FAILED, "!data"); if (!bin_data) SETIERROR(MALLOC_FAILED, "!bin_data"); switch(swi.data_type) { case DATA_ASCII: for (i=0; i<nsamples; i++) { p = fgets(buf, 256, f); if (!p) { SETIERROR(READ_FAILED,"in seti_parse_data"); } sscanf(buf, "%f%f", &data[i][0], &data[i][1]); } break; case DATA_ENCODED: case DATA_SUN_BINARY: try { int nread; std::string tmpbuf(""); fseek(f,0,SEEK_SET); nbytes = (nsamples/samples_per_byte); tmpbuf.reserve(nbytes*3/2); while ((nread=(int)fread(buf,1,sizeof(buf),f))) { tmpbuf+=std::string(&(buf[0]),nread); } std::vector<unsigned char> datav( xml_decode_field<unsigned char>(tmpbuf,"data") ); memcpy(bin_data,&(datav[0]),datav.size()); if (datav.size() < nbytes) throw BAD_DECODE; } catch (int i) { retval=i; if (data) free_a(data); if (bin_data) free_a(bin_data); SETIERROR(i,"in seti_parse_data()"); } bits_to_floats((unsigned char *)bin_data, data, nsamples); memcpy(bin_data,data,nsamples*sizeof(sah_complex)); state.savedWUData = bin_data; break; /* #if 0 nbytes = (nsamples/4); bin_data = (unsigned char*)malloc_a((nbytes+2)*sizeof(unsigned char), MEM_ALIGN); if (!bin_data) SETIERROR(MALLOC_FAILED, "!bin_data"); retval = read_bin_data(bin_data, nbytes, f); if (retval) { if (data) free_a(data); if (bin_data) free_a(bin_data); SETIERROR(retval,"from read_bin_data()"); } bits_to_floats(bin_data, data, nsamples); state.savedWUData = bin_data; break; #endif */ } state.npoints = nsamples; state.data = data; #ifdef BOINC_APP_GRAPHICS if (sah_graphics) { strlcpy(sah_graphics->wu.receiver_name,swi.receiver_cfg.name,255); sah_graphics->wu.s4_id = swi.receiver_cfg.s4_id; sah_graphics->wu.time_recorded = swi.time_recorded; sah_graphics->wu.subband_base = swi.subband_base; sah_graphics->wu.start_ra = swi.start_ra; sah_graphics->wu.start_dec = swi.start_dec; sah_graphics->wu.subband_sample_rate = swi.subband_sample_rate; sah_graphics->ready = true; } #endif return 0; }
int main(int argc, char *argv[]) { char *outfile=NULL, buf[256]; struct stat statbuf; int nbytes,nread,nsamples; std::string tmpbuf(""); int i=0,j; if ((argc < 2) || (argc > 3)) { fprintf(stderr,"%s infile [outfile]\n",argv[0]); exit(1); } char *infile=argv[1]; if (argc==3) { outfile=argv[2]; } FILE *in=fopen(infile,"r"); FILE *out; if (outfile) { out=fopen(outfile,"w"); } else { out=stdout; } stat(infile,&statbuf); nbytes=statbuf.st_size; fseek(in,0,SEEK_SET); tmpbuf.reserve(nbytes); // read entire file into a buffer. while ((nread=(int)fread(buf,1,sizeof(buf),in))) { tmpbuf+=std::string(&(buf[0]),nread); } // parse the header header.parse_xml(tmpbuf); // decode the data std::vector<unsigned char> datav( xml_decode_field<unsigned char>(tmpbuf,"data") ); tmpbuf.clear(); nsamples=header.group_info->data_desc.nsamples; nbytes=nsamples*header.group_info->recorder_cfg->bits_per_sample/8; if (datav.size() < nbytes) { fprintf(stderr,"Data size does not match number of samples\n"); exit(1); } // convert the data to floating point sah_complex *fpdata=(sah_complex *)calloc(nsamples,sizeof(sah_complex)); sah_complex *fpout=(sah_complex *)calloc(2048,sizeof(sah_complex)); sah_complex *tmpb=(sah_complex *)calloc(1024,sizeof(sah_complex)); if (!fpdata || !fpout) { fprintf(stderr,"Memory allocation failure\n"); exit(1); } bits_to_floats(&(datav[0]),fpdata,nsamples); datav.clear(); sah_complex workbuf[2048]; fftwf_plan reverse=fftwf_plan_dft_1d(2048,workbuf,fpout,FFTW_BACKWARD,FFTW_MEASURE); fftwf_plan forward=fftwf_plan_dft_1d(1024,tmpb,workbuf,FFTW_FORWARD,FFTW_MEASURE|FFTW_PRESERVE_INPUT); while (i<nsamples) { // Do the forward transform. fftwf_execute_dft(forward, fpdata+i, workbuf); // shift 64 frequency bins memmove((void *)(workbuf+64), (void *)workbuf, 1024*sizeof(sah_complex)); // now move the upper 64 into the low bins memmove((void *)workbuf,(void *)(workbuf+1024),64*sizeof(sah_complex)); // clear the upper range memset((void *)(workbuf+1024),0,64*sizeof(sah_complex)); // Do the reverse transform fftwf_execute_dft(reverse,workbuf,fpout); // for (j=0; j<2048; j++) { fprintf(out,"%f\n",fpout[j][0]/1024.0); } i+=1024; } exit(0); }
IDL_VPTR readwu(int argc, IDL_VPTR argv[], char *argk) { IDL_VPTR filename=NULL; static IDL_VARIABLE rv; rv.type=IDL_TYP_INT; rv.flags=IDL_V_CONST|IDL_V_NULL; rv.value.i=-1; char *outfile=NULL, buf[256]; struct stat statbuf; int nbytes,nread,nsamples; std::string tmpbuf(""); int i=0,j; if (argc != 1) { fprintf(stderr,"argc=%d\n",argc); fprintf(stderr,"array=readwu(wufile_name)\n"); return &rv; } IDL_STRING *infile=NULL; if (argv[0]->type != IDL_TYP_STRING) { IDL_MessageFromBlock(readwu_msg_block,0,IDL_MSG_RET,"Parameter 1 must be type STRING"); } else { infile=(IDL_STRING *)(&argv[0]->value.s); } FILE *in=fopen(infile->s,"r"); if (!in) { IDL_MessageFromBlock(readwu_msg_block,0,IDL_MSG_RET,"File not found"); return &rv; } stat(infile->s,&statbuf); nbytes=statbuf.st_size; fseek(in,0,SEEK_SET); tmpbuf.reserve(nbytes); // read entire file into a buffer. while ((nread=(int)fread(buf,1,sizeof(buf),in))) { tmpbuf+=std::string(&(buf[0]),nread); } // parse the header header.parse_xml(tmpbuf); // decode the data std::vector<unsigned char> datav( xml_decode_field<unsigned char>(tmpbuf,"data") ); tmpbuf.clear(); nsamples=header.group_info->data_desc.nsamples; nbytes=nsamples*header.group_info->recorder_cfg->bits_per_sample/8; if (datav.size() < nbytes) { fprintf(stderr,"Data size does not match number of samples\n"); return &rv; } // convert the data to floating point sah_complex *fpdata=(sah_complex *)IDL_MemAlloc(nsamples*sizeof(sah_complex),0,IDL_MSG_RET); if (!fpdata) { fprintf(stderr,"Unable to allocate memory!\r\n"); return &rv; } bits_to_floats(&(datav[0]),fpdata,nsamples); datav.clear(); IDL_MEMINT dims[]={nsamples}; return IDL_ImportArray(1,dims,IDL_TYP_COMPLEX,(UCHAR *)fpdata,NULL,NULL); }