static void swap_hydrol_lft (HYDROL_LFT *data) { swap_int (&data->id); swap_short (&data->accuracy_category); swap_float (&data->contour_value); swap_short (&data->hypsography_portrayal_category); }
static void swap_markerp_pft (MARKERP_PFT *data) { swap_int (&data->id); swap_short (&data->usage); swap_short (&data->tile_reference_identifier); swap_int (&data->entity_node_key); }
static void swap_hydline_lft (HYDLINE_LFT *data) { swap_int (&data->id); swap_short (&data->accuracy_category); swap_float (&data->contour_value); swap_short (&data->tile_reference_identifier); swap_int (&data->edge_primitive_key); }
unsigned short vax_to_local_short(const unsigned short *s) { #if defined(WORDS_BIGENDIAN) return swap_short(*s); #else return *s; #endif /* WORDS_BIGENDIAN */ }
int16_t le_to_native_short(int16_t s) { if(set_endian() == BIG_ENDIAN) return swap_short(s); return s; }
void CNB::PopulateIPLength(IPv4Header *IpHeader, USHORT IpLength) const { if ((IpHeader->ip_verlen & 0xF0) == 0x40) { if (!IpHeader->ip_length) { IpHeader->ip_length = swap_short(IpLength); } } }
void vax_to_local_shorts(unsigned short *sa, const int *n) { #if defined(WORDS_BIGENDIAN) int i; for (i = 0; i < *n; i++) { sa[i] = swap_short(sa[i]); } #endif /* WORDS_BIGENDIAN */ (void)sa; (void)n; // Avoid compiler warning return; }
/*--------------------------------------------------------------------------*/ int writeShort(short _val, FILE* _pF, int _iEndian) { //reverse _val bytes and write short newVal = _val; if (_iEndian == BIG_ENDIAN) { newVal = swap_short(_val); } return fwrite(&newVal, sizeof(short), 1, _pF) != 1 ? -1 : 0; }
void CNB::PopulateIPLength(IPHeader *IpHeader, USHORT IpLength) const { if ((IpHeader->v4.ip_verlen & 0xF0) == 0x40) { if (!IpHeader->v4.ip_length) { IpHeader->v4.ip_length = swap_short(IpLength); } } else if ((IpHeader->v6.ip6_ver_tc & 0xF0) == 0x60) { if (!IpHeader->v6.ip6_payload_len) { IpHeader->v6.ip6_payload_len = swap_short(IpLength - IPV6_HEADER_MIN_SIZE); } } else { DPrintf(0, ("[%s] ERROR: Bad version of IP header!\n", __FUNCTION__)); } }
void swap_img(char *img, AnalyzeHeader hdr) { int i,sz=hdr.dim[1]*hdr.dim[2]*hdr.dim[3]; switch(hdr.datatype) { case UCHAR: break; case SHORT: for(i=0;i<sz;i++) swap_short(&((short*)img)[i]); break; case INT: for(i=0;i<sz;i++) swap_int(&((int*)img)[i]); break; case FLOAT: for(i=0;i<sz;i++) swap_float(&((float*)img)[i]); break; case RGBFLOAT: for(i=0;i<sz;i++) swap_rgbfloat(&((float*)img)[i]); break; } }
int aud_readinit (char *filename, int *sample_rate, int *sample_size, int *channels ) { /* Sets up a descriptor to read from a wave (RIFF). * Returns file descriptor if successful*/ int fd; WaveHeader wh; if (0 > (fd = open (filename, O_RDONLY))){ fprintf(stderr,"unable to open the audiofile\n"); return -1; } read (fd, &wh, sizeof(wh)); if (0 != bcmp(wh.main_chunk, RIFF, sizeof(wh.main_chunk)) || 0 != bcmp(wh.chunk_type, WAVEFMT, sizeof(wh.chunk_type)) ) { fprintf (stderr, "not a WAVE-file\n"); errno = 3;// EFTYPE; return -1; } if (swap_short(wh.format) != PCM_CODE) { fprintf (stderr, "can't play non PCM WAVE-files\n"); errno = 5;//EFTYPE; return -1; } if (swap_short(wh.chans) > 2) { fprintf (stderr, "can't play WAVE-files with %d tracks\n", wh.chans); return -1; } *sample_rate = (unsigned int) swap_long(wh.sample_fq); *sample_size = (unsigned int) swap_short(wh.bit_p_spl); *channels = (unsigned int) swap_short(wh.chans); fprintf (stderr, "%s chan=%u, freq=%u bitrate=%u format=%hu\n", filename, *channels, *sample_rate, *sample_size, wh.format); return fd; }
/*--------------------------------------------------------------------------*/ int readShort(FILE* _pF, int _iEndian, unsigned short* val) { if (fread(val, sizeof(short), 1, _pF) != 1) { return -1; } if (_iEndian == BIG_ENDIAN) { *val = swap_short(*val); } return 0; }
static void swap_dangera_aft (DANGERA_AFT *data) { swap_int (&data->id); swap_short (&data->accuracy_category); swap_short (&data->date); swap_short (&data->existence_category); swap_short (&data->hydrographic_depth_height_info); swap_float (&data->hydrographic_depth); swap_short (&data->value); swap_short (&data->vertical_reference_category); swap_short (&data->tile_reference_identifier); swap_int (&data->face_primitive_key); }
long sp_ntohl(long t){ static enum SP_sample_byte_fmt sbf = SP_sbf_null; if (sbf == SP_sbf_null) sbf = get_natural_sbf(4); if (sbf == SP_sbf_3210) return(t); if (sbf == SP_sbf_1032){ swap_short(t); return(t); } fprintf(stderr, "Error: sp_ntohl(%lx) Failed, Contact the SPHERE maintainer\n", t); exit(-1); }
void swap_triplet (TRIPLET *triplet) { NV_INT32 i, type[3]; NV_INT16 type2; /* The first byte of a triplet defines how much data follows. The byte is broken up into four, two-bit sections. The first two bits define how many bytes will be in the first section of the triplet. The next two bits, the second. The third two bits, the third. The last two bits are TBD. If the value in a two bit section is 0 there will be no bytes in the corresponding data section. If it is 1 there will be one byte, 2 for two, 3 for four. Some examples: type byte = 1000 1 byte value size = 2 bytes type byte = 3000 4 byte value size = 5 bytes type byte = 0230 2 byte value then a 4 byte value size = 7 bytes type byte = 3230 4 byte value, 2 byte value, 4 byte value size = 11 bytes type byte = 3330 4 byte value, 4 byte value, 4 byte value size = 13 bytes */ type[0] = (triplet->type & 0xc0) > 6; type[1] = (triplet->type & 0x30) > 4; type[2] = (triplet->type & 0x0c) > 2; for (i = 0 ; i < 3 ; i++) { switch (type[i]) { case 0: case 1: break; case 2: type2 = triplet->field[i]; swap_short (&type2); triplet->field[i] = type2; break; case 3: swap_int (&triplet->field[i]); break; } } }
static void swap_pipelinel_lft (PIPELINEL_LFT *data) { swap_int (&data->id); swap_float (&data->depth_below_surface_level); swap_short (&data->existence_category); swap_float (&data->height_above_sea_bottom); swap_short (&data->location_category); swap_float (&data->overhead_clearance_category); swap_short (&data->over_water_obstruction); swap_short (&data->pipeline_type); swap_short (&data->product_category); swap_short (&data->tile_reference_identifier); swap_int (&data->edge_primitive_key); }
void flip_ucac2_star( UCAC2_STAR *star) { swap_long( &star->ra); swap_long( &star->dec); swap_short( &star->mag); swap_short( &star->epoch_ra); swap_short( &star->epoch_dec); swap_long( &star->pm_ra); swap_long( &star->pm_dec); swap_long( &star->twomass_id); swap_short( &star->j_mag); swap_short( &star->h_mag); swap_short( &star->ks_mag); }
void got_new_packet(ecb_header * ecb) { ipx_packet * p; int id; unsigned short datasize; datasize = 0; last_ecb = ecb; p = (ipx_packet *)ecb; if (p->ecb.in_use) { neterrors++; return; } if (p->ecb.completion_code) { neterrors++; return; } // Error( "Recieve error %d for completion code", p->ecb.completion_code ); if (memcmp(&p->ipx.source.node_id, &ipx_my_node, 6)) { datasize = swap_short(p->ipx.length); lastlen = datasize; datasize -= sizeof(ipx_header); // Find slot to put packet in... if (datasize > 0 && datasize <= sizeof(packet_data)) { if (num_packets >= MAX_PACKETS) { //printf( 1, "IPX: Packet buffer overrun!!!\n" ); neterrors++; return; } id = packet_free_list[num_packets++]; if (id > largest_packet_index) largest_packet_index = id; packet_size[id] = datasize - sizeof(int); packet_buffers[id].packetnum = p->pd.packetnum; if (packet_buffers[id].packetnum < 0) { neterrors++; return; } memcpy(packet_buffers[id].data, p->pd.data, packet_size[id]); } else { neterrors++; return; } } // Repost the ecb p->ecb.in_use = 0; //ipx_listen_for_packet(&p->ecb); }
void swap_hdr(AnalyzeHeader *hdr) { int i; // header key swap_int(&(*hdr).sizeof_hdr); swap_int(&(*hdr).extents); swap_short(&(*hdr).session_error); // image dimension for(i=0;i<8;i++) swap_short(&(*hdr).dim[i]); for(i=0;i<7;i++) swap_short(&(*hdr).unused[i]); swap_short(&(*hdr).datatype); swap_short(&(*hdr).bitpix); swap_short(&(*hdr).dim_un0); for(i=0;i<8;i++) swap_float(&(*hdr).pixdim[i]); swap_float(&(*hdr).vox_offset); for(i=0;i<3;i++) swap_float(&(*hdr).funused[i]); swap_float(&(*hdr).cal_max); swap_float(&(*hdr).cal_min); swap_float(&(*hdr).compressed); swap_float(&(*hdr).verified); swap_int(&(*hdr).glmax); swap_int(&(*hdr).glmin); // data history for(i=0;i<3;i++) swap_short(&(*hdr).orig[i]); swap_int(&(*hdr).views); swap_int(&(*hdr).vols_added); swap_int(&(*hdr).start_field); swap_int(&(*hdr).field_skip); swap_int(&(*hdr).omax); swap_int(&(*hdr).omin); swap_int(&(*hdr).smax); swap_int(&(*hdr).smin); }
void bpp2fb(FILE *input, FILE *output) /* includefile */ { FILE *fpou; int np,ns,nc,i,c,b,s,nchars,idump,doit,opened,nsblk,i1,i2,*chtab,blocksize; float *tempblock, *datablock,realtime=0.0; unsigned char *charblock,sample; static unsigned char *charcarry; unsigned short *shortblock; char string[80]; int nshift[8] = {28,24,20,16,12,8,4,0}; static int ncarryover; static int first = 1; static int fileidx_start=1,file_count=1; static double end_time; double scantime,sample_start,sample_final; int sample_skip,bytestart; int bytefinal,fileidx_final,sample_diff,byte_diff=0; int bpp_headersize = 32768; int rd_jnq=0; double sample_end,byte_end; np=idump=opened=0; ns=512; blocksize=ns*nchans; nc=nchans; if(first) { charcarry = (unsigned char *) malloc(sizeof(unsigned char)*blocksize); ncarryover = 0; first = 0; scantime = (double)((double)bpp_search.file_size)/((double)(nchans/2))*bpp_search.samp_rate*1.e-6; if(start_time){ fileidx_start = ceil(start_time/scantime); file_count = fileidx_start; sample_skip = floor(start_time/tsamp); sample_start = sample_skip - (double)((fileidx_start-1)*(scantime/(double)tsamp)); bytestart = (sample_start*(double)nchans)/2.; if(bytestart<blocksize/2){ bytestart+=(double)bpp_search.file_size-blocksize/2; fileidx_start-=1; file_count -=1; sample_skip -= ns; sample_start = sample_skip - (double)((fileidx_start-1)*(scantime/(double)tsamp)); } realtime += sample_skip*(double)tsamp - scantime*(fileidx_start-1); if((rd_jnq =fseek(input,bytestart,SEEK_CUR)< 0)) fprintf(stderr,"Error seeking to data byte %d in file\n",bytestart); } if(final_time){ sample_end = ceil(final_time/(double)tsamp); fileidx_final = ceil(final_time/scantime); sample_final = sample_end-(double)((fileidx_final-1)*scantime/tsamp); byte_end = ceil(final_time/(double)tsamp)*(double)nchans/2; bytefinal = (double)sample_final*(double)nchans/2; end_time = (double)(byte_end/((double)nchans/2)*(double)tsamp); fprintf(stderr,"Ending Time: \n"); fprintf(stderr," End File # %2d\n",fileidx_final); fprintf(stderr," End Sample # %12.3f\n",sample_final); fprintf(stderr," End Time (s) %12.5f (w.r.t. File # 1)\n",end_time); if(start_time) { end_time -= (double)((fileidx_start-1)*scantime); fprintf(stderr," End Time (s) %12.5f (w.r.t. File # %d)\n",end_time,fileidx_start); } } if (!final_time) end_time = 1000*scantime; } tempblock = (float *) malloc(sizeof(float)*blocksize); datablock = (float *) malloc(sizeof(float)*blocksize); charblock = (unsigned char *) malloc(sizeof(unsigned char)*blocksize); shortblock = (unsigned short *) malloc(sizeof(unsigned short)*blocksize); if (headerfile) { /* write output ASCII header file */ fpou=open_file("head","w"); fprintf(fpou,"Original BPP file: %s\n",inpfile); fprintf(fpou,"Sample time (us): %f\n",tsamp*1.0e6); fprintf(fpou,"Number of samples/record: %d\n",ns); fprintf(fpou,"Center freq (MHz): %f\n",fch1+(float)nchans*foff/2.0); fprintf(fpou,"Channel band (kHz): %f\n",fabs(foff)*1000.0); fprintf(fpou,"Number of channels/record: %d\n",nc); fprintf(fpou,"Time stamp (MJD): %18.12f\n",tstart); fprintf(fpou,"AZ at start: %f\n",az_start); fprintf(fpou,"ZA at start: %f\n",za_start); fprintf(fpou,"RA (J2000): %f\n",src_raj); fprintf(fpou,"DEC (J2000): %f\n",src_dej); fclose(fpou); } chtab=bpp_chans(bpp_search.bandwidth,bpp_search.mb_start_address, bpp_search.mb_end_address,bpp_search.mb_start_board, bpp_search.mb_end_board,bpp_search.cb_id, bpp_search.aib_los,bpp_search.dfb_sram_freqs, bpp_search.rf_lo); /************************************************/ /* main loop over each record of input data file*/ /************************************************/ while (!feof(input)&& realtime<=end_time) { /* read in a record */ if(!ncarryover) /* No data left from previous file */ nchars=fread(charblock,sizeof(unsigned char),blocksize/2,input); if(ncarryover>0) { /* Add to partial block left from previous file */ nchars=fread(charblock,sizeof(unsigned char),(blocksize/2-ncarryover),input); for(c=0;c<nchars;c++) charblock[c+ncarryover] = charblock[c]; for(c=0;c<ncarryover;c++) charblock[c] = charcarry[c]; ncarryover = 0; nchars = blocksize/2; fprintf(stderr," Starting at beginning of File # %d\n",file_count); } if(!ncarryover && nchars<blocksize/2) { /* Don't process, just keep */ ncarryover = nchars; for(c=0;c<nchars;c++) charcarry[c] = charblock[c]; file_count++; if(final_time) end_time = end_time - (double)tsamp*(int)(scantime/(double)tsamp); fprintf(stderr,"Advancing to file # %d\n",file_count); /* if(final_time) fprintf(stderr," End time = %f (w.r.t. file # %d)\n",end_time,file_count);*/ /* fprintf(stderr," Realtime is %f\n",realtime);*/ } else { /* decide whether to process it */ if ( (doit=process(realtime,sample_skip*(double)tsamp,end_time)) == -1) { fprintf(stderr,"realtime at time of break = %f (s)\n ",realtime); break; } doit = 1; if (doit) { /* about to process this record, update log */ np++; if (np%10 == 0) { if (!opened) { open_log("filterbank.monitor"); opened=1; } sprintf(string,"time:%.1fs",realtime); update_log(string); } /* unscramble the 4-bit data into the float tempblock */ nsblk=0; for (c=0;c<nchars;c++) { char2ints(charblock[c],&i1,&i2); tempblock[nsblk++] = (float) i2; tempblock[nsblk++] = (float) i1; } /* update wallclock time */ realtime+=(float) (nsblk/nchans/nifs) * (float) tsamp; if (sumifs) for (s=0;s<nsblk;s++) datablock[s]=0.0; s=i1=i2=0; /* loop over all samples, summing IFs 1+2 -> total power if neccessary */ while (s<nsblk) { for (i=0;i<nifs;i++) { for (c=0;c<nchans;c++) { if (sumifs) { if (i<2) datablock[i1+c]+=tempblock[i2+chtab[i*nchans+c]]; } else { datablock[i1+i*nchans+c]=tempblock[i2+chtab[i*nchans+c]]; } s++; } } /* update internal counters */ i2+=nifs*nchans; if (sumifs) { i1+=nchans; } else { i1+=nifs*nchans; } } /* divide by 2 to in sumif mode to allow for 4-bit packing */ if (sumifs) { nsblk/=nifs; for (s=0;s<nsblk;s++) datablock[s]/=2.0; } /* decide on how to write out data */ if (obits==32) { /* user has requested floating point numbers in binary file */ if (swapout) for (s=0;s<nsblk;s++) swap_float(&datablock[s]); fwrite(datablock,sizeof(float),nsblk,output); } else if (obits==16) { /* user has requested unsigned shorts in binary file */ float2short(datablock,nsblk,0.0,15.0,shortblock); if (swapout) for (s=0;s<nsblk;s++) swap_short(&shortblock[s]); fwrite(shortblock,sizeof(unsigned short),nsblk,output); } else if (obits==8) { /* user has requested unsigned chars in binary file */ float2char(datablock,nsblk,0.0,15.0,charblock); fwrite(charblock,sizeof(unsigned char),nsblk,output); } else if (obits==4) { /* default is to write data out packed into character format */ float2four(datablock,nsblk,0.0,15.0,charblock); fwrite(charblock,sizeof(unsigned char),nsblk/2,output); } else { error_message("unknown bit format for writing"); } } /* update dumps read/processed */ idump+=ns; } } }
void wapp2fb(FILE *input, FILE *output) /* includefile */ { FILE *bptr, *fpou, *alfa[2]; int pixel[2]; double pra, pdec; double bw, bandwidth, scale, power, hweight, tsamp_us, crate, lmst; double *lag, *sum, *acf, *window, jan1, days, epoch, ras,des,rahr,dede; float zerolag,*block,smin,smax; int doit,i,j,k,two_nlags,nlags,stat,rec_size,idump,swap_bytes,ifnum,opened; int filesize,headersize,beam,utsecs,iymdf[4],rah,ram,ded,dem; unsigned char *cblock, zuc; unsigned short *sblock, zus; unsigned int zul; char message[80], outfile[80]; void *dump; static float realtime=0.0; #ifdef FFTW fftw_plan fftplan; #endif /* establish whether we need to swap bytes (WAPP is little endian) */ swap_bytes=big_endian(); /* initialise correlator parameters used below */ nlags=nchans; two_nlags=2*nlags; bandwidth=foff*nlags; tsamp_us=tsamp*1.0e6; if (bandwidth<0.0) bandwidth *= -1.0; #ifdef FFTW acf = fftw_malloc(sizeof(double) * two_nlags); lag = fftw_malloc(sizeof(double) * two_nlags); /* set up fftw table and acf array when computing power spectra */ fftplan=fftw_plan_r2r_1d(two_nlags,acf,lag,FFTW_R2HC,FFTW_PATIENT); #endif #ifndef FFTW /* set up acf array when computing power spectra */ acf = (double *) malloc(two_nlags * sizeof(double)); lag = (double *) malloc(two_nlags * sizeof(double)); #endif if (compute_spectra) { /* ranges for scaling spectra */ smin=0.0;smax=3.0; } else { /* ranges for scaling correlation functions */ smin=-0.5;smax=1.0; } /* set up the weights for windowing of ACF to monimize FFT leakage */ if (hanning) { /* Hanning window */ hweight=0.50; } else if (hamming) { /* Hamming window */ hweight=0.54; } else { /* no window (default) */ hweight=1.00; } /* define the smoothing window to be applied base on the above weight */ window = (double *) malloc(nlags * sizeof(double)); for (j=0; j<nlags; j++) window[j]=(hweight+(1.0-hweight)*cos(PI*j/nlags)); /* work out number of IFs to loop over */ if (sumifs && (nifs>1)) { smin*=2.0; smax*=2.0; ifnum=2; } else { sumifs=0; ifnum=nifs; } /* calculate required record size for reading - i.e. number of bytes/dump */ rec_size = nifs*nlags*(nbits/8); dump = malloc(rec_size); /* pointer to the correlator dump */ /* correlator data rate */ crate = 1.0/(tsamp_us-WAPP_DEAD_TIME); /* scale factor to normalize correlation functions */ if (bandwidth < 50.0) bw=50.0; /* correct scaling for narrow-band use */ else bw=bandwidth; scale = crate/bw; if (wapp_level==9) scale/=16.0; /* 9-level sampling */ if (wapp_sum) scale/=2.0; /* summed IFs (search mode) */ scale*=pow(2.0,(double)wapp_lagtrunc); /* needed for truncation modes */ /* now define a number of working arrays to store lags and spectra */ block = (float *) malloc(nlags * sizeof(float)); cblock = (unsigned char *) malloc(nlags * sizeof(unsigned char)); sblock = (unsigned short *) malloc(nlags * sizeof(unsigned short)); /* if the file is ALFA data --- do the demultiplexing to two files */ if (wapp_isalfa) { angle_split(src_raj,&rah,&ram,&ras); rahr=(double)rah+(double)ram/60.0+(double)ras/3600.0; angle_split(src_dej,&ded,&dem,&des); if (ded>0) dede=(double)ded+(double)dem/60.0+(double)des/3600.0; else dede=(double)ded-(double)dem/60.0-(double)des/3600.0; /* calculate local sidereal time in hours */ lmst=slaGmst(tstart)*12.0/4.0/atan(1.0)-4.4502051459439667; if (lmst<0.0) lmst+=24.0; slaDjcal(5,tstart,iymdf,&stat); slaCaldj(iymdf[0],1,1,&jan1,&stat); days=tstart-jan1+1.0; epoch=(double)iymdf[0]+days/365.25; utsecs=86400*(tstart-floor(tstart)); pixel[0]=(wapp_number-1)*2; pixel[1]=pixel[0]+1; puts("opening output files for demultiplexed ALFA data..."); for (i=0; i<2; i++) { if (alfa_raj[pixel[i]] == 0.0) { alfa_position(rahr,dede,lmst,epoch,alfa_ang,0.0,0.0,pixel[i],&pra,&pdec); src_raj=h2hms(pra); src_dej=deg2dms(pdec); } else { src_raj=h2hms(alfa_raj[pixel[i]]); src_dej=deg2dms(alfa_dej[pixel[i]]); } sprintf(outfile,"%s_%.0f_%05d_%04d_%s_%d.fil", project,floor(tstart),utsecs, scan_number,source_name,pixel[i]); alfa[i]=open_file(outfile,"wb"); puts(outfile); filterbank_header(alfa[i]); } beam=0; } if (headerfile) { /* write output ASCII header file */ fpou=open_file("head","w"); fprintf(fpou,"Original WAPP file: %s\n",inpfile); fprintf(fpou,"Sample time (us): %f\n",tsamp_us); fprintf(fpou,"Observation time (s): %f\n",wapp_obstime); fprintf(fpou,"Time stamp (MJD): %18.12f\n",tstart); fprintf(fpou,"Number of samples/record: %d\n",512); fprintf(fpou,"Center freq (MHz): %f\n",fch1+(float)nlags*foff/2.0); fprintf(fpou,"Channel band (kHz): %f\n",bandwidth*1000.0/nlags); fprintf(fpou,"Number of channels/record: %d\n",nlags); fprintf(fpou,"Nifs: %d\n",ifnum); fprintf(fpou,"RA (J2000): %f\n",src_raj); fprintf(fpou,"DEC (J2000): %f\n",src_dej); fprintf(fpou,"Gal l: %.4f\n",srcl); fprintf(fpou,"Gal b: %.4f\n",srcb); fprintf(fpou,"Name: %s\n",source_name); fprintf(fpou,"Lagformat: %d\n",wapp_lagformat); fprintf(fpou,"Sum: %d\n",wapp_sum); fprintf(fpou,"Level: %d\n",wapp_level); fprintf(fpou,"AZ at start: %f\n",az_start); fprintf(fpou,"ZA at start: %f\n",za_start); fprintf(fpou,"AST at start: %f\n",ast0); fprintf(fpou,"LST at start: %f\n",lst0); fprintf(fpou,"Project ID: %s\n",project); fprintf(fpou,"Observers: %s\n",culprits); filesize=sizeof_file(inpfile); fprintf(fpou,"File size (bytes): %d\n",filesize); headersize=wapp_header_size+wapp_incfile_length; fprintf(fpou,"Data size (bytes): %d\n",filesize-headersize); fprintf(fpou,"Number of samples: %d\n",nsamples(inpfile,headersize,nbits,nifs,nchans)); fclose(fpou); } /* initialise various counters and flags */ opened=idump=i=j=0; /* main loop reading data from infile until no more left to read */ while( (stat=read(wapp_file,dump,rec_size)) == rec_size) { /* calculate elapsed time and determine whether we process this record */ realtime += (float) tsamp; if ( (doit=process(realtime,start_time,final_time)) == -1) break; if (doit) { /* set ALFA beam output if necessary */ if (wapp_isalfa) { output=alfa[beam]; /* set output file for this loop */ beam=!(beam); /* flip file for next iteration */ } /* clear zerolag and blocksum arrays */ zerolag=0.0; for (j=0; j<nlags; j++) block[j]=0.0; /* loop over the IFs */ for (i=0; i<ifnum; i++) { if (ifstream[i]=='Y') { if (zerolagdump) { /* only interested in the zero lag term for each IF */ switch (nbits) { case 8: zuc = *(((unsigned char *)dump)+i*nlags); zerolag+=zuc; break; case 16: zus = *(((unsigned short *)dump)+i*nlags); if (swap_bytes) swap_short(&zus); zerolag+=zus; break; case 32: zul = *(((unsigned int *)dump)+i*nlags); if (swap_bytes) swap_int(&zul); zerolag+=zul; break; } /* write out the data checking IF number for summed mode */ if ( (sumifs && (i==1)) || (!sumifs) ) { if (obits==32) { if (swapout) swap_float(&zerolag); fwrite(&zerolag,sizeof(float),1,output); } else { sprintf(message,"cannot write %d bits in zerolag mode",obits); error_message(message); } } } else { /* fill lag array with scaled CFs */ for (j=0; j<nlags; j++) { switch (nbits) { case 8: zuc = *(((unsigned char *)dump)+j+i*nlags); lag[j] = scale * (double) zuc - 1.0; break; case 16: zus = *(((unsigned short *)dump)+j+i*nlags); if (swap_bytes) swap_short(&zus); lag[j] = scale * (double) zus - 1.0; break; case 32: zul = *(((unsigned int *)dump)+j+i*nlags); if (swap_bytes) swap_int(&zul); lag[j] = scale * (double) zul - 1.0; break; } } /* calculate power and correct for finite level quantization */ power = inv_cerf(lag[0]); power = 0.1872721836/power/power; if (i<2) { if (do_vanvleck) { if (wapp_level==3) { /* apply standard 3-level van vleck correction */ vanvleck3lev(lag,nlags); } else if (wapp_level==9) { /* apply 9-level van vleck correction */ vanvleck9lev(lag,nlags); } } } if (compute_spectra) { /* form windowed even ACF in array */ for(j=1; j<nlags; j++) { acf[j]=window[j]*lag[j]*power; acf[two_nlags-j]=acf[j]; } acf[nlags]=0.0; acf[0]=lag[0]*power; /* FFT the ACF (which is real and even) -> real and even FFT */ #ifdef FFTW fftw_execute(fftplan); #endif #ifndef FFTW rfft(two_nlags,acf,lag); #endif /* if the band needs to be flipped --- do it here */ if (wapp_flip) { /* use acf as temporary array */ for (j=0;j<nlags;j++) acf[j]=lag[j]; k=nlags-1; for (j=0;j<nlags;j++) { lag[k]=acf[j]; k--; } } /* add lags to block array */ for (j=0; j<nlags; j++) block[j]+=lag[j]; } else { /* just copy correlation functions into block */ for (j=0; j<nlags; j++) block[j]=lag[j]; } /* write out data block checking IF number for summed mode */ if ( (sumifs && (i==1)) || (!sumifs) ) { if (obits==32) { if (swapout) for (j=0; j<nlags; j++) swap_float(&block[j]); fwrite(block,sizeof(float),nlags,output); } else if (obits==16) { float2short(block,nlags,smin,smax,sblock); if (swapout) for (j=0; j<nlags; j++) swap_short(&sblock[j]); fwrite(sblock,sizeof(unsigned short),nlags,output); } else if (obits==8) { float2char(block,nlags,smin,smax,cblock); fwrite(cblock,sizeof(unsigned char),nlags,output); } else if (obits==4) { float2four(block,nlags,smin,smax,cblock); fwrite(cblock,sizeof(unsigned char),nlags/2,output); } else { sprintf(message,"cannot write %d bits in wapp2fb",obits); error_message(message); } } } /* end of zerolagdump if */ if (!sumifs) { /* reset block and zerolag if not summing */ zerolag=0.0; for (j=0; j<nlags; j++) block[j]=0.0; } } /* end of IFstream if */ } /* end of loop over IFs */ } /* end of processing if */ /* increment dump counter and update logfile every 512 dumps */ idump++; if (idump%512 == 0) { if (!opened) { /* open up logfile */ open_log("filterbank.monitor"); opened=1; } sprintf(message,"time:%.1fs",realtime); update_log(message); } } /* end of main read loop*/ /* job done - free up remaining arrays */ free(dump);free(block);free(sblock);free(cblock);free(window); #ifdef FFTW fftw_destroy_plan(fftplan); fftw_free(acf); fftw_free(lag); #endif #ifndef FFTW free(acf); free(lag); #endif }
void process_xif_ifd(IFD_Type *ifd_ptr, TIFF_Hdr_Type *tiff_hdr, unsigned short segment_size, unsigned char *endofsegment_ptr) { int i; unsigned short entry_count; unsigned short tag; unsigned short type; unsigned count; unsigned value; unsigned int remaining_size; remaining_size = endofsegment_ptr - (unsigned char *)ifd_ptr; if (remaining_size > segment_size) _terminate(-1); entry_count=swap_short(ifd_ptr->Count); if (entry_count *12 + sizeof(entry_count) > remaining_size) { printf("Invalid IFD count value\n"); _terminate(-1); } printf("# of arrays: @d\n", entry_count); for (i=0; i< entry_count; ++i) { tag = swap_short(ifd_ptr->Entry[i].Tag); type = swap_short(ifd_ptr->Entry[i].Type); count = swap_word(ifd_ptr->Entry[i].Count); value = swap_word(ifd_ptr->Entry[i].Value); printf("Tag: @x (", tag); print_xif_tag_text(tag); printf(")\n"); printf("Type: @x (", type); print_type(type); printf(")\n"); printf("Count: @d\n", count); #ifdef PATCHED if (type==2) { if ( ((char *)ifd_ptr+value > (char *)tiff_hdr+segment_size) || ((char *)ifd_ptr+value < (char *)ifd_ptr )) { printf("Value: 0\n"); return; } } #endif if (type == 2) { #ifdef PATCHED if ( strlen( (char*)(ifd_ptr) + value ) > 2048 ) { ((char*)(ifd_ptr))[value+2048] = '\x00'; } #endif printf("Value: @s\n", (char *)(ifd_ptr) + value); } else { printf("Value: @u\n", value); } } }
void swapEndianess() { swap_long(reinterpret_cast<unsigned char*>(&sizeof_hdr)); swap_long(reinterpret_cast<unsigned char*>(&extents)); swap_short(reinterpret_cast<unsigned char*>(&session_error)); swap_short(reinterpret_cast<unsigned char*>(&dim[0])); swap_short(reinterpret_cast<unsigned char*>(&dim[1])); swap_short(reinterpret_cast<unsigned char*>(&dim[2])); swap_short(reinterpret_cast<unsigned char*>(&dim[3])); swap_short(reinterpret_cast<unsigned char*>(&dim[4])); swap_short(reinterpret_cast<unsigned char*>(&dim[5])); swap_short(reinterpret_cast<unsigned char*>(&dim[6])); swap_short(reinterpret_cast<unsigned char*>(&dim[7])); swap_long(reinterpret_cast<unsigned char*>(&intent_p1)); swap_long(reinterpret_cast<unsigned char*>(&intent_p2)); swap_long(reinterpret_cast<unsigned char*>(&intent_p3)); swap_short(reinterpret_cast<unsigned char*>(&intent_code)); swap_short(reinterpret_cast<unsigned char*>(&datatype)); swap_short(reinterpret_cast<unsigned char*>(&bitpix)); swap_short(reinterpret_cast<unsigned char*>(&slice_start)); swap_long(reinterpret_cast<unsigned char*>(&pixdim[0])); swap_long(reinterpret_cast<unsigned char*>(&pixdim[1])); swap_long(reinterpret_cast<unsigned char*>(&pixdim[2])); swap_long(reinterpret_cast<unsigned char*>(&pixdim[3])); swap_long(reinterpret_cast<unsigned char*>(&pixdim[4])); swap_long(reinterpret_cast<unsigned char*>(&pixdim[5])); swap_long(reinterpret_cast<unsigned char*>(&pixdim[6])); swap_long(reinterpret_cast<unsigned char*>(&pixdim[7])); swap_long(reinterpret_cast<unsigned char*>(&vox_offset)); swap_long(reinterpret_cast<unsigned char*>(&scl_slope)); swap_long(reinterpret_cast<unsigned char*>(&scl_inter)); swap_short(reinterpret_cast<unsigned char*>(&slice_end)); swap_long(reinterpret_cast<unsigned char*>(&cal_max)); swap_long(reinterpret_cast<unsigned char*>(&cal_min)); swap_long(reinterpret_cast<unsigned char*>(&slice_duration)); swap_long(reinterpret_cast<unsigned char*>(&toffset)); swap_long(reinterpret_cast<unsigned char*>(&glmax)); swap_long(reinterpret_cast<unsigned char*>(&glmin)); swap_short(reinterpret_cast<unsigned char*>(&qform_code)); swap_short(reinterpret_cast<unsigned char*>(&sform_code)); swap_long(reinterpret_cast<unsigned char*>(&quatern_b)); swap_long(reinterpret_cast<unsigned char*>(&quatern_c)); swap_long(reinterpret_cast<unsigned char*>(&quatern_d)); swap_long(reinterpret_cast<unsigned char*>(&qoffset_x)); swap_long(reinterpret_cast<unsigned char*>(&qoffset_y)); swap_long(reinterpret_cast<unsigned char*>(&qoffset_z)); swap_long(reinterpret_cast<unsigned char*>(&srow_x[0])); swap_long(reinterpret_cast<unsigned char*>(&srow_x[1])); swap_long(reinterpret_cast<unsigned char*>(&srow_x[2])); swap_long(reinterpret_cast<unsigned char*>(&srow_x[3])); swap_long(reinterpret_cast<unsigned char*>(&srow_y[0])); swap_long(reinterpret_cast<unsigned char*>(&srow_y[1])); swap_long(reinterpret_cast<unsigned char*>(&srow_y[2])); swap_long(reinterpret_cast<unsigned char*>(&srow_y[3])); swap_long(reinterpret_cast<unsigned char*>(&srow_z[0])); swap_long(reinterpret_cast<unsigned char*>(&srow_z[1])); swap_long(reinterpret_cast<unsigned char*>(&srow_z[2])); swap_long(reinterpret_cast<unsigned char*>(&srow_z[3])); }
static void swap_ecrarea_aft (ECRAREA_AFT *data) { swap_int (&data->id); swap_short (&data->tile_reference_identifier); swap_int (&data->face_primitive_key); }
int main(int cgc_argc, char *cgc_argv[]) { unsigned ret_code; unsigned short SOM; unsigned short marker; unsigned short segment_size; unsigned short tagMark; unsigned short byte_align; unsigned char *tiff_header; unsigned char *xif_data; void *tmp_ptr; void *gps_info_ptr; unsigned offset; TIFF_Hdr_Type *tiff_hdr; IFD_Type *IFD; IFD_Type *xif_ifd_ptr; unsigned char *endofsegment_ptr; int i; swap_short = 0; swap_word = 0; // cgc_read the first two bytes and check for the Start of Message marker ret_code = receive_bytes((unsigned char *)&SOM, sizeof(SOM)); if (ret_code == -1 ) { cgc_printf("did not receive bytes\n"); _terminate(-1); } if (SOM != 0xFFF8) { puts("Did not find SOM marker"); _terminate(-1); } else { puts("SOM marker found"); } // Now look for the next marker, which can be SAP0 or SAP1 ret_code = receive_bytes((unsigned char *)&marker, sizeof(marker)); if (ret_code == -1 ) { cgc_printf("did not receive bytes\n"); _terminate(-1); } // SAP0 if (marker == 0xFFF0) { cgc_printf("SAP0 marker found\n"); ret_code = receive_bytes((unsigned char *)&segment_size, sizeof(segment_size)); if (ret_code == -1 ) { cgc_printf("did not receive bytes\n"); _terminate(-1); } if (segment_size <=2) { cgc_printf("Invalid segment size\n", segment_size); _terminate(-1); } // now cgc_read and discard the rest of the SAP0 header xif_data = malloc(segment_size-sizeof(segment_size)); if ((int)xif_data == 0) { cgc_printf("Unable to allocate memory\n"); _terminate(-1); } // cgc_read the rest of SAP0 and discard ret_code=receive_bytes((unsigned char *)xif_data, segment_size-sizeof(segment_size)); if (ret_code == -1) { cgc_printf("unable to read SAP0 segment\n"); _terminate(-1); } free(xif_data); xif_data = 0; // now cgc_read the next marker, which should be SAP1 ret_code = receive_bytes((unsigned char *)&marker, sizeof(marker)); if (ret_code == -1 ) { cgc_printf("did not receive bytes\n"); _terminate(-1); } } // SAP0 header // look for the SAP1 marker now if (marker != 0xffF1) { cgc_printf("Did not find SAP1 marker\n"); _terminate(-1); } else { cgc_printf("SAP1 marker found\n"); } // cgc_read the length of the overall segment ret_code = receive_bytes((unsigned char *)&segment_size, sizeof(segment_size)); if (ret_code == -1 ) { cgc_printf("did not receive bytes\n"); _terminate(-1); } cgc_printf("sizeof section is @d\n", segment_size); if (segment_size <= 0) { cgc_printf("Invalid segment size\n"); _terminate(-1); } xif_data = malloc(segment_size); if ((int)xif_data == 0) { cgc_printf("Unable to allocate memory\n"); _terminate(-1); } ret_code=receive_bytes((unsigned char *)xif_data, segment_size); if (ret_code == -1) { cgc_printf("unable to read SAP1 segment\n"); _terminate(-1); } // tiff header + xif header + the count of IFD segments if (segment_size < sizeof(TIFF_Hdr_Type) + 6 + 2) { cgc_printf("not enough data received\n"); _terminate(-1); } tiff_hdr = (TIFF_Hdr_Type *)(xif_data+6); endofsegment_ptr = (unsigned char *)xif_data + segment_size; if (tiff_hdr->Byte_Order == 0x4949) { cgc_printf("Intel formatted integers\n"); swap_short = intel_swap_short; swap_word = intel_swap_word; } else if (tiff_hdr->Byte_Order == 0x4d4d) { cgc_printf("Motorola formatted integers\n"); swap_short = motorola_swap_short; swap_word = motorola_swap_word; } #ifdef PATCHED else { cgc_printf("Invalid header values\n"); _terminate(-1); } #endif cgc_printf("TagMark = @x\n", swap_short(tiff_hdr->Fixed)); offset = swap_word(tiff_hdr->Offset_to_IFD); cgc_printf("Offset = @x\n", swap_word(tiff_hdr->Offset_to_IFD)); if (offset > segment_size) { cgc_printf("Invalid offset\n"); _terminate(-1); } IFD = (void *)(tiff_hdr) + swap_word(tiff_hdr->Offset_to_IFD); // how many array entries are there IFD->Count = swap_short(IFD->Count); cgc_printf("# of compatility arrays: @d\n", IFD->Count); if (IFD->Count * 12 > segment_size - sizeof(TIFF_Hdr_Type) - 6 - 2) { cgc_printf("Invalid number of IFD entries\n"); _terminate(-1); } // loop through all of the entries in the array for (i=0; i< (IFD->Count); ++i) { cgc_printf("Tag: @x (", swap_short(IFD->Entry[i].Tag)); print_tag_text(swap_short(IFD->Entry[i].Tag)); cgc_printf(")\n"); cgc_printf("Type: @x (", swap_short(IFD->Entry[i].Type)); print_type(swap_short(IFD->Entry[i].Type)); cgc_printf(")\n"); cgc_printf("Count: @d\n", swap_word(IFD->Entry[i].Count)); if (swap_short(IFD->Entry[i].Type) == 2) { if (swap_word(IFD->Entry[i].Value) < segment_size - 8 && swap_word(IFD->Entry[i].Value) > 0) #ifdef PATCHED { if ( cgc_strlen( (char *)(tiff_hdr) + swap_word(IFD->Entry[i].Value)) > 2048) { ((char *)(tiff_hdr))[swap_word(IFD->Entry[i].Value)+2048] = '\x00'; } #endif cgc_printf("Value: @s\n", (char *)(tiff_hdr) + swap_word(IFD->Entry[i].Value)); #ifdef PATCHED } #endif else cgc_printf("Value: 0\n"); } else { cgc_printf("Value: @u\n", swap_word(IFD->Entry[i].Value)); } if (swap_short(IFD->Entry[i].Tag) == 0x8825) { gps_info_ptr = (void *)tiff_hdr + swap_word(IFD->Entry[i].Value); process_gps_ifd(gps_info_ptr, tiff_hdr, segment_size, endofsegment_ptr); } else if (swap_short(IFD->Entry[i].Tag) == 0x8769) { xif_ifd_ptr = (void *)tiff_hdr + swap_word(IFD->Entry[i].Value); process_xif_ifd(xif_ifd_ptr, tiff_hdr, segment_size, endofsegment_ptr); } } puts("Finished processing"); } // main
void swapEndianess() { swap_long(reinterpret_cast<unsigned char*>(&sizeof_hdr)); swap_long(reinterpret_cast<unsigned char*>(&extents)); swap_short(reinterpret_cast<unsigned char*>(&session_error)); }
void fileHandler::swapHdr() { swap_long(mHdr.hk.sizeof_hdr); swap_long(mHdr.hk.extents); swap_short(mHdr.hk.session_error); swap_short(mHdr.dime.dim[0]); swap_short(mHdr.dime.dim[1]); swap_short(mHdr.dime.dim[2]); swap_short(mHdr.dime.dim[3]); swap_short(mHdr.dime.dim[4]); swap_short(mHdr.dime.dim[5]); swap_short(mHdr.dime.dim[6]); swap_short(mHdr.dime.dim[7]); swap_short((short)mHdr.dime.funused1); swap_short(mHdr.dime.datatype); swap_short(mHdr.dime.bitpix); swap_long((int)mHdr.dime.pixdim[0]); swap_long((int)mHdr.dime.pixdim[1]); swap_long((int)mHdr.dime.pixdim[2]); swap_long((int)mHdr.dime.pixdim[3]); swap_long((int)mHdr.dime.pixdim[4]); swap_long((int)mHdr.dime.pixdim[5]); swap_long((int)mHdr.dime.pixdim[6]); swap_long((int)mHdr.dime.pixdim[7]); swap_long((int)mHdr.dime.vox_offset); swap_long((int)mHdr.dime.funused1); swap_long((int)mHdr.dime.funused2); swap_long((int)mHdr.dime.cal_max); swap_long((int)mHdr.dime.cal_min); swap_long((int)mHdr.dime.compressed); swap_long((int)mHdr.dime.verified); swap_short(mHdr.dime.dim_un0); swap_long(mHdr.dime.glmax); swap_long(mHdr.dime.glmin); }
void swapEndianess() { swap_short(reinterpret_cast<unsigned char*>(&dim[0])); swap_short(reinterpret_cast<unsigned char*>(&dim[1])); swap_short(reinterpret_cast<unsigned char*>(&dim[2])); swap_short(reinterpret_cast<unsigned char*>(&dim[3])); swap_short(reinterpret_cast<unsigned char*>(&dim[4])); swap_short(reinterpret_cast<unsigned char*>(&dim[5])); swap_short(reinterpret_cast<unsigned char*>(&dim[6])); swap_short(reinterpret_cast<unsigned char*>(&dim[7])); swap_short(reinterpret_cast<unsigned char*>(&unused8)); swap_short(reinterpret_cast<unsigned char*>(&unused9)); swap_short(reinterpret_cast<unsigned char*>(&unused10)); swap_short(reinterpret_cast<unsigned char*>(&unused11)); swap_short(reinterpret_cast<unsigned char*>(&unused12)); swap_short(reinterpret_cast<unsigned char*>(&unused13)); swap_short(reinterpret_cast<unsigned char*>(&unused14)); swap_short(reinterpret_cast<unsigned char*>(&datatype)); swap_short(reinterpret_cast<unsigned char*>(&bitpix)); swap_long(reinterpret_cast<unsigned char*>(&pixdim[0])); swap_long(reinterpret_cast<unsigned char*>(&pixdim[1])); swap_long(reinterpret_cast<unsigned char*>(&pixdim[2])); swap_long(reinterpret_cast<unsigned char*>(&pixdim[3])); swap_long(reinterpret_cast<unsigned char*>(&pixdim[4])); swap_long(reinterpret_cast<unsigned char*>(&pixdim[5])); swap_long(reinterpret_cast<unsigned char*>(&pixdim[5])); swap_long(reinterpret_cast<unsigned char*>(&pixdim[6])); swap_long(reinterpret_cast<unsigned char*>(&pixdim[7])); swap_long(reinterpret_cast<unsigned char*>(&vox_offset)); swap_long(reinterpret_cast<unsigned char*>(&funused1)); swap_long(reinterpret_cast<unsigned char*>(&funused2)); swap_long(reinterpret_cast<unsigned char*>(&cal_max)); swap_long(reinterpret_cast<unsigned char*>(&cal_min)); swap_long(reinterpret_cast<unsigned char*>(&compressed)); swap_long(reinterpret_cast<unsigned char*>(&verified)); swap_short(reinterpret_cast<unsigned char*>(&dim_un0)); swap_long(reinterpret_cast<unsigned char*>(&glmax)); swap_long(reinterpret_cast<unsigned char*>(&glmin)); }
static void swap_buoybcnp_pft (BUOYBCNP_PFT *data) { swap_int (&data->id); swap_short (&data->accuracy_category); swap_int (&data->broadcast_frequency[0]); swap_int (&data->broadcast_frequency[1]); swap_short (&data->buoy_beacon_type); swap_short (&data->color_code); swap_short (&data->elevation); swap_short (&data->nominal_light_range); swap_short (&data->nav_system_type[0]); swap_short (&data->nav_system_type[1]); swap_short (&data->operating_range[0]); swap_short (&data->operating_range[1]); swap_float (&data->light_period); swap_short (&data->radar_reflector_attr); swap_short (&data->structure_shape_category); swap_short (&data->sound_signal_type); swap_short (&data->topmark_characteristic); swap_short (&data->tile_reference_identifier); swap_int (&data->entity_node_key); }
static void swap_lightsp_pft (LIGHTSP_PFT *data) { swap_int (&data->id); swap_short (&data->accuracy_category); swap_int (&data->broadcast_frequency[0]); swap_int (&data->broadcast_frequency[1]); swap_short (&data->color_code); swap_short (&data->elevation); swap_short (&data->hydrographic_light_type); swap_short (&data->IALA_aid_category); swap_short (&data->nominal_light_range); swap_short (&data->nav_system_type[0]); swap_short (&data->nav_system_type[1]); swap_float (&data->light_period); swap_short (&data->radar_reflector_attr); swap_short (&data->sound_signal_type); swap_short (&data->topmark_characteristic); swap_short (&data->tile_reference_identifier); swap_int (&data->entity_node_key); }