// Intialize options then parse the cmd line arguments Options::Options( int argc, char **argv ) { center = false; pdbfile = NULL; outputfile = NULL; failure = false; sameChain = false; threshold = 7.0; numLigands = 0; gamessfolder = NULL; outputGamessINP = false; pdblist = NULL; chain_list = NULL; extension = ".pdb.gz"; resolution = 99999.0; findCarbonRings = false; parseCmdline( argc, argv ); }
int main(int argc, char* argv[]) { std::string input, output; OptionsMap options = create_map<OptionsMap::key_type, OptionsMap::mapped_type> ("d", Option())("s", Option()); try { auto lefovers = parseCmdline(argc, argv, options); if (lefovers.size() != 2) throw std::runtime_error("Missing leftover args"); input = lefovers[0]; output = lefovers[1]; } catch (std::exception& e) { std::cerr << "Error: " << e.what() << std::endl; printUsage(); return 2; } std::ifstream ifile(input.c_str(), std::ios_base::binary); if (!ifile) { std::cerr << "Error: Unable to open input file: " << input << std::endl; return 1; } std::ofstream ofile(output.c_str(), std::ios_base::binary); if (!ofile) { std::cerr << "Error: Unable to open output file: " << output << std::endl; return 1; } try { if (options["d"].isPresent) { decompress(ifile, ofile); } else { if (options["s"].isPresent) compressStaticly(ifile, ofile); else compressAdaptive(ifile, ofile); } } catch (std::exception& e) { std::cerr << "Error: " << e.what() << std::endl; return 1; } return 0; }
int main(int argc, char *argv[]) { struct Config_t config; GDFS_MAPDATA *Source_MapHead_p, *Source_MapTail_p; uint32 Noof_Source_Parameters = 0; int result = 0; memset(&config, 0x00, sizeof(struct Config_t)); /* Parse the input argument and if not correct print info and exit */ if (parseCmdline(argc, argv, &config) == -1) { result = -1; goto exit; } /* First we open the CSPSA area */ if( InitCSPSA(&config) != 0) { result = -1; goto exit; } if(!ReadMapFile( config.MAP_FileName, &Noof_Source_Parameters, &Source_MapHead_p, &Source_MapTail_p)){ fprintf(stderr, "CSPSA2MAP: Failed to open input map file.\n"); result = -1; goto exit; } if(0 != write_parameters_to_cspsa(Source_MapHead_p, &config)){ fprintf(stderr, "CSPSA2MAP: Failed to write parameters to CSPSA.\n"); result = -1; goto exit; } exit: /* Clean up and close any open files etc.. */ if(config.CSPSA_Handle != 0x00){ UnInit(&config); } return result; }
int main(int argc, char *argv[]) /* dftfold: Does complex plane vector addition of a DFT freq */ /* Written by Scott Ransom on 31 Aug 00 based on Ransom and */ /* Eikenberry paper I (to be completed sometime...). */ { FILE *infile; char infilenm[200], outfilenm[200]; int dataperread; unsigned long N; double T, rr = 0.0, norm = 1.0; dftvector dftvec; infodata idata; Cmdline *cmd; /* Call usage() if we have no command line arguments */ if (argc == 1) { Program = argv[0]; usage(); exit(1); } /* Parse the command line using the excellent program Clig */ cmd = parseCmdline(argc, argv); #ifdef DEBUG showOptionValues(); #endif printf("\n\n"); printf(" DFT Vector Folding Routine\n"); printf(" by Scott M. Ransom\n"); printf(" 31 August, 2000\n\n"); /* Open the datafile and read the info file */ sprintf(infilenm, "%s.dat", cmd->argv[0]); infile = chkfopen(infilenm, "rb"); readinf(&idata, cmd->argv[0]); /* The number of points in datafile */ N = chkfilelen(infile, sizeof(float)); dataperread = N / cmd->numvect; /* N = cmd->numvect * dataperread; */ T = N * idata.dt; /* Calculate the Fourier frequency */ if (!cmd->rrP) { if (cmd->ffP) rr = cmd->ff; else if (cmd->ppP) rr = T / cmd->pp; else { printf("\n You must specify a frequency to fold! Exiting.\n\n"); } } else rr = cmd->rr; /* Calculate the amplitude normalization if required */ if (cmd->normP) norm = 1.0 / sqrt(cmd->norm); else if (cmd->fftnormP) { FILE *fftfile; int kern_half_width, fftdatalen, startbin; double rrfrac, rrint; char fftfilenm[200]; fcomplex *fftdata; sprintf(fftfilenm, "%s.fft", cmd->argv[0]); fftfile = chkfopen(fftfilenm, "rb"); kern_half_width = r_resp_halfwidth(HIGHACC); fftdatalen = 2 * kern_half_width + 10; rrfrac = modf(rr, &rrint); startbin = (int) rrint - fftdatalen / 2; fftdata = read_fcomplex_file(fftfile, startbin, fftdatalen); norm = 1.0 / sqrt(get_localpower3d(fftdata, fftdatalen, rrfrac + fftdatalen / 2, 0.0, 0.0)); vect_free(fftdata); fclose(fftfile); } /* Initialize the dftvector */ init_dftvector(&dftvec, dataperread, cmd->numvect, idata.dt, rr, norm, T); /* Show our folding values */ printf("\nFolding data from '%s':\n", infilenm); printf(" Folding Fourier Freq = %.5f\n", rr); printf(" Folding Freq (Hz) = %-.11f\n", rr / T); printf(" Folding Period (s) = %-.14f\n", T / rr); printf(" Points per sub-vector = %d\n", dftvec.n); printf(" Number of sub-vectors = %d\n", dftvec.numvect); printf(" Normalization constant = %g\n", norm * norm); /* Perform the actual vector addition */ { int ii, jj; float *data; double real, imag, sumreal = 0.0, sumimag = 0.0; double theta, aa, bb, cc, ss, dtmp; double powargr, powargi, phsargr, phsargi, phstmp; data = gen_fvect(dftvec.n); theta = -TWOPI * rr / (double) N; dtmp = sin(0.5 * theta); aa = -2.0 * dtmp * dtmp; bb = sin(theta); cc = 1.0; ss = 0.0; for (ii = 0; ii < dftvec.numvect; ii++) { chkfread(data, sizeof(float), dftvec.n, infile); real = 0.0; imag = 0.0; for (jj = 0; jj < dftvec.n; jj++) { real += data[jj] * cc; imag += data[jj] * ss; cc = aa * (dtmp = cc) - bb * ss + cc; ss = aa * ss + bb * dtmp + ss; } dftvec.vector[ii].r = norm * real; dftvec.vector[ii].i = norm * imag; sumreal += dftvec.vector[ii].r; sumimag += dftvec.vector[ii].i; } vect_free(data); printf("\nDone:\n"); printf(" Vector sum = %.3f + %.3fi\n", sumreal, sumimag); printf(" Total phase (deg) = %.2f\n", PHASE(sumreal, sumimag)); printf(" Total power = %.2f\n", POWER(sumreal, sumimag)); printf("\n"); } fclose(infile); /* Write the output structure */ sprintf(outfilenm, "%s_%.3f.dftvec", cmd->argv[0], rr); write_dftvector(&dftvec, outfilenm); /* Free our vector and return */ free_dftvector(&dftvec); return (0); }
int main (int argc, char **argv) { char *xmalloc (); char *xrealloc (); char *xstrdup (); int infpop; double nb; /* Check parameters */ Cmdline *cmd = parseCmdline (argc, argv); if ((cmd->show_helpP) | (argc == 1)) usage (); if (cmd->show_versionP) { printf ("%s %s\n", argv[0], VERSION); exit (0); } check_param (cmd); infpop = (cmd->pop == 0) ? 1 : 0; cmd->precision /= PC; cmd->prevalence /= PC; cmd->level /= PC; cmd->alpha /= PC; cmd->power /= PC; cmd->exposed /= PC; if (cmd->observedP) { nb = small_sampsi (cmd); display_small (cmd, nb); } else if (cmd->odds_ratioP && !cmd->sampleP) { cmd->ratio = floor (cmd->ratio); if (cmd->ratio < 1) sperror ("option -c should be >= 1"); case_control (cmd); } /* Absolute precision then sample size equals population size */ else if (cmd->precision == 0 && cmd->pop > 0) { nb = cmd->pop; display_surv (cmd, nb, infpop); } else if (cmd->precisionP) { nb = sampsi (cmd); display_surv (cmd, nb, infpop); } else if (cmd->binomialP) binom_ci (cmd); else if (cmd->compP && !cmd->sampleP && !cmd->deltaP) comp (cmd); else if (cmd->meansP && !cmd->sampleP && !cmd->deltaP) means (cmd); else if (cmd->sampleP && cmd->exposedP && !cmd->odds_ratioP && cmd->powerP && !cmd->matchedP) ccmin (cmd); else if (cmd->sampleP && cmd->compP && !cmd->deltaP) ppower (cmd); else if (cmd->sampleP && cmd->meansP && !cmd->deltaP) mpower (cmd); else if (cmd->sampleP && cmd->odds_ratioP && cmd->exposedP && !cmd->matchedP) ccpower (cmd); else if (cmd->sampleP && cmd->matchedP && cmd->odds_ratioP && cmd->exposedP) mccpower (cmd); else if (cmd->deltaP && cmd->compP && !cmd->sampleP) nequivp (cmd); else if (cmd->deltaP && cmd->meansP && !cmd->sampleP) nequivm (cmd); else sperror ("wrong combination of options, or missing options"); exit (0); }
int main(int argc, char *argv[]) { /* Any variable that begins with 't' means topocentric */ /* Any variable that begins with 'b' means barycentric */ FILE **outfiles = NULL; float **outdata; double dtmp, *dms, avgdm = 0.0, dsdt = 0, maxdm; double *dispdt, tlotoa = 0.0, blotoa = 0.0, BW_ddelay = 0.0; double max = -9.9E30, min = 9.9E30, var = 0.0, avg = 0.0; double *btoa = NULL, *ttoa = NULL, avgvoverc = 0.0; char obs[3], ephem[10], rastring[50], decstring[50]; long totnumtowrite, totwrote = 0, padwrote = 0, datawrote = 0; int *idispdt, **offsets; int ii, jj, numadded = 0, numremoved = 0, padding = 0, good_inputs = 1; int numbarypts = 0, numread = 0, numtowrite = 0; int padtowrite = 0, statnum = 0; int numdiffbins = 0, *diffbins = NULL, *diffbinptr = NULL, good_padvals = 0; double local_lodm; char *datafilenm, *outpath, *outfilenm, *hostname; struct spectra_info s; infodata idata; mask obsmask; MPI_Init(&argc, &argv); MPI_Comm_size(MPI_COMM_WORLD, &numprocs); MPI_Comm_rank(MPI_COMM_WORLD, &myid); #ifdef _OPENMP omp_set_num_threads(1); // Explicitly turn off OpenMP #endif set_using_MPI(); { FILE *hostfile; char tmpname[100]; int retval; hostfile = chkfopen("/etc/hostname", "r"); retval = fscanf(hostfile, "%s\n", tmpname); if (retval==0) { printf("Warning: error reading /etc/hostname on proc %d\n", myid); } hostname = (char *) calloc(strlen(tmpname) + 1, 1); memcpy(hostname, tmpname, strlen(tmpname)); fclose(hostfile); } /* Call usage() if we have no command line arguments */ if (argc == 1) { if (myid == 0) { Program = argv[0]; usage(); } MPI_Finalize(); exit(1); } make_maskbase_struct(); make_spectra_info_struct(); /* Parse the command line using the excellent program Clig */ cmd = parseCmdline(argc, argv); spectra_info_set_defaults(&s); // If we are zeroDMing, make sure that clipping is off. if (cmd->zerodmP) cmd->noclipP = 1; s.clip_sigma = cmd->clip; if (cmd->noclipP) { cmd->clip = 0.0; s.clip_sigma = 0.0; } if (cmd->ifsP) { // 0 = default or summed, 1-4 are possible also s.use_poln = cmd->ifs + 1; } if (!cmd->numoutP) cmd->numout = LONG_MAX; #ifdef DEBUG showOptionValues(); #endif if (myid == 0) { /* Master node only */ printf("\n\n"); printf(" Parallel Pulsar Subband De-dispersion Routine\n"); printf(" by Scott M. Ransom\n\n"); s.filenames = cmd->argv; s.num_files = cmd->argc; s.clip_sigma = cmd->clip; // -1 causes the data to determine if we use weights, scales, & // offsets for PSRFITS or flip the band for any data type where // we can figure that out with the data s.apply_flipband = (cmd->invertP) ? 1 : -1; s.apply_weight = (cmd->noweightsP) ? 0 : -1; s.apply_scale = (cmd->noscalesP) ? 0 : -1; s.apply_offset = (cmd->nooffsetsP) ? 0 : -1; s.remove_zerodm = (cmd->zerodmP) ? 1 : 0; if (RAWDATA) { if (cmd->filterbankP) s.datatype = SIGPROCFB; else if (cmd->psrfitsP) s.datatype = PSRFITS; else if (cmd->pkmbP) s.datatype = SCAMP; else if (cmd->bcpmP) s.datatype = BPP; else if (cmd->wappP) s.datatype = WAPP; else if (cmd->spigotP) s.datatype = SPIGOT; } else { // Attempt to auto-identify the data identify_psrdatatype(&s, 1); if (s.datatype==SIGPROCFB) cmd->filterbankP = 1; else if (s.datatype==PSRFITS) cmd->psrfitsP = 1; else if (s.datatype==SCAMP) cmd->pkmbP = 1; else if (s.datatype==BPP) cmd->bcpmP = 1; else if (s.datatype==WAPP) cmd->wappP = 1; else if (s.datatype==SPIGOT) cmd->spigotP = 1; else if (s.datatype==SUBBAND) insubs = 1; else { printf("\nError: Unable to identify input data files. Please specify type.\n\n"); good_inputs = 0; } } // So far we can only handle PSRFITS, filterbank, and subbands if (s.datatype!=PSRFITS && s.datatype!=SIGPROCFB && s.datatype!=SUBBAND) good_inputs = 0; // For subbanded data if (!RAWDATA) s.files = (FILE **)malloc(sizeof(FILE *) * s.num_files); if (good_inputs && (RAWDATA || insubs)) { char description[40]; psrdatatype_description(description, s.datatype); if (s.num_files > 1) printf("Reading %s data from %d files:\n", description, s.num_files); else printf("Reading %s data from 1 file:\n", description); for (ii = 0; ii < s.num_files; ii++) { printf(" '%s'\n", cmd->argv[ii]); if (insubs) s.files[ii] = chkfopen(s.filenames[ii], "rb"); } printf("\n"); if (RAWDATA) { read_rawdata_files(&s); print_spectra_info_summary(&s); spectra_info_to_inf(&s, &idata); } else { // insubs char *root, *suffix; cmd->nsub = s.num_files; s.N = chkfilelen(s.files[0], sizeof(short)); s.start_subint = gen_ivect(1); s.num_subint = gen_ivect(1); s.start_MJD = (long double *)malloc(sizeof(long double)); s.start_spec = (long long *)malloc(sizeof(long long)); s.num_spec = (long long *)malloc(sizeof(long long)); s.num_pad = (long long *)malloc(sizeof(long long)); s.start_spec[0] = 0L; s.start_subint[0] = 0; s.num_spec[0] = s.N; s.num_subint[0] = s.N / SUBSBLOCKLEN; s.num_pad[0] = 0L; s.padvals = gen_fvect(s.num_files); for (ii = 0 ; ii < ii ; ii++) s.padvals[ii] = 0.0; if (split_root_suffix(s.filenames[0], &root, &suffix) == 0) { printf("\nError: The input filename (%s) must have a suffix!\n\n", s.filenames[0]); exit(1); } if (strncmp(suffix, "sub", 3) == 0) { char *tmpname; tmpname = calloc(strlen(root) + 10, 1); sprintf(tmpname, "%s.sub", root); readinf(&idata, tmpname); free(tmpname); strncpy(s.telescope, idata.telescope, 40); strncpy(s.backend, idata.instrument, 40); strncpy(s.observer, idata.observer, 40); strncpy(s.source, idata.object, 40); s.ra2000 = hms2rad(idata.ra_h, idata.ra_m, idata.ra_s) * RADTODEG; s.dec2000 = dms2rad(idata.dec_d, idata.dec_m, idata.dec_s) * RADTODEG; ra_dec_to_string(s.ra_str, idata.ra_h, idata.ra_m, idata.ra_s); ra_dec_to_string(s.dec_str, idata.dec_d, idata.dec_m, idata.dec_s); s.num_channels = idata.num_chan; s.start_MJD[0] = idata.mjd_i + idata.mjd_f; s.dt = idata.dt; s.T = s.N * s.dt; s.lo_freq = idata.freq; s.df = idata.chan_wid; s.hi_freq = s.lo_freq + (s.num_channels - 1.0) * s.df; s.BW = s.num_channels * s.df; s.fctr = s.lo_freq - 0.5 * s.df + 0.5 * s.BW; s.beam_FWHM = idata.fov / 3600.0; s.spectra_per_subint = SUBSBLOCKLEN; print_spectra_info_summary(&s); } else { printf("\nThe input files (%s) must be subbands! (i.e. *.sub##)\n\n", cmd->argv[0]); MPI_Finalize(); exit(1); } free(root); free(suffix); } } } // If we don't have good input data, exit MPI_Bcast(&good_inputs, 1, MPI_INT, 0, MPI_COMM_WORLD); if (!good_inputs) { MPI_Finalize(); exit(1); } MPI_Bcast(&insubs, 1, MPI_INT, 0, MPI_COMM_WORLD); if (insubs) cmd->nsub = cmd->argc; /* Determine the output file names and open them */ local_numdms = cmd->numdms / (numprocs - 1); dms = gen_dvect(local_numdms); if (cmd->numdms % (numprocs - 1)) { if (myid == 0) printf ("\nThe number of DMs must be divisible by (the number of processors - 1).\n\n"); MPI_Finalize(); exit(1); } local_lodm = cmd->lodm + (myid - 1) * local_numdms * cmd->dmstep; split_path_file(cmd->outfile, &outpath, &outfilenm); datafilenm = (char *) calloc(strlen(outfilenm) + 20, 1); if (myid > 0) { if (chdir(outpath) == -1) { printf("\nProcess %d on %s cannot chdir() to '%s'. Exiting.\n\n", myid, hostname, outpath); MPI_Finalize(); exit(1); } outfiles = (FILE **) malloc(local_numdms * sizeof(FILE *)); for (ii = 0; ii < local_numdms; ii++) { dms[ii] = local_lodm + ii * cmd->dmstep; avgdm += dms[ii]; sprintf(datafilenm, "%s_DM%.2f.dat", outfilenm, dms[ii]); outfiles[ii] = chkfopen(datafilenm, "wb"); } avgdm /= local_numdms; } // Broadcast the raw data information broadcast_spectra_info(&s, myid); if (myid > 0) { spectra_info_to_inf(&s, &idata); if (s.datatype==SIGPROCFB) cmd->filterbankP = 1; else if (s.datatype==PSRFITS) cmd->psrfitsP = 1; else if (s.datatype==SCAMP) cmd->pkmbP = 1; else if (s.datatype==BPP) cmd->bcpmP = 1; else if (s.datatype==WAPP) cmd->wappP = 1; else if (s.datatype==SPIGOT) cmd->spigotP = 1; else if (s.datatype==SUBBAND) insubs = 1; } s.filenames = cmd->argv; /* Read an input mask if wanted */ if (myid > 0) { int numpad = s.num_channels; if (insubs) numpad = s.num_files; s.padvals = gen_fvect(numpad); for (ii = 0 ; ii < numpad ; ii++) s.padvals[ii] = 0.0; } if (cmd->maskfileP) { if (myid == 0) { read_mask(cmd->maskfile, &obsmask); printf("Read mask information from '%s'\n\n", cmd->maskfile); good_padvals = determine_padvals(cmd->maskfile, &obsmask, s.padvals); } broadcast_mask(&obsmask, myid); MPI_Bcast(&good_padvals, 1, MPI_INT, 0, MPI_COMM_WORLD); MPI_Bcast(s.padvals, obsmask.numchan, MPI_FLOAT, 0, MPI_COMM_WORLD); } else { obsmask.numchan = obsmask.numint = 0; MPI_Bcast(&good_padvals, 1, MPI_INT, 0, MPI_COMM_WORLD); } // The number of topo to bary time points to generate with TEMPO numbarypts = (int) (s.T * 1.1 / TDT + 5.5) + 1; // Identify the TEMPO observatory code { char *outscope = (char *) calloc(40, sizeof(char)); telescope_to_tempocode(idata.telescope, outscope, obs); free(outscope); } // Broadcast or calculate a few extra important values if (insubs) avgdm = idata.dm; idata.dm = avgdm; dsdt = cmd->downsamp * idata.dt; maxdm = cmd->lodm + cmd->numdms * cmd->dmstep; BW_ddelay = delay_from_dm(maxdm, idata.freq) - delay_from_dm(maxdm, idata.freq + (idata.num_chan-1) * idata.chan_wid); blocksperread = ((int) (BW_ddelay / idata.dt) / s.spectra_per_subint + 1); worklen = s.spectra_per_subint * blocksperread; if (cmd->nsub > s.num_channels) { printf ("Warning: The number of requested subbands (%d) is larger than the number of channels (%d).\n", cmd->nsub, s.num_channels); printf(" Re-setting the number of subbands to %d.\n\n", s.num_channels); cmd->nsub = s.num_channels; } if (s.spectra_per_subint % cmd->downsamp) { if (myid == 0) { printf ("\nError: The downsample factor (%d) must be a factor of the\n", cmd->downsamp); printf(" blocklength (%d). Exiting.\n\n", s.spectra_per_subint); } MPI_Finalize(); exit(1); } tlotoa = idata.mjd_i + idata.mjd_f; /* Topocentric epoch */ if (cmd->numoutP) totnumtowrite = cmd->numout; else totnumtowrite = (long) idata.N / cmd->downsamp; if (cmd->nobaryP) { /* Main loop if we are not barycentering... */ /* Dispersion delays (in bins). The high freq gets no delay */ /* All other delays are positive fractions of bin length (dt) */ dispdt = subband_search_delays(s.num_channels, cmd->nsub, avgdm, idata.freq, idata.chan_wid, 0.0); idispdt = gen_ivect(s.num_channels); for (ii = 0; ii < s.num_channels; ii++) idispdt[ii] = NEAREST_LONG(dispdt[ii] / idata.dt); vect_free(dispdt); /* The subband dispersion delays (see note above) */ offsets = gen_imatrix(local_numdms, cmd->nsub); for (ii = 0; ii < local_numdms; ii++) { double *subdispdt; subdispdt = subband_delays(s.num_channels, cmd->nsub, dms[ii], idata.freq, idata.chan_wid, 0.0); dtmp = subdispdt[cmd->nsub - 1]; for (jj = 0; jj < cmd->nsub; jj++) offsets[ii][jj] = NEAREST_LONG((subdispdt[jj] - dtmp) / dsdt); vect_free(subdispdt); } /* Allocate our data array and start getting data */ if (myid == 0) { printf("De-dispersing using %d subbands.\n", cmd->nsub); if (cmd->downsamp > 1) printf("Downsampling by a factor of %d (new dt = %.10g)\n", cmd->downsamp, dsdt); printf("\n"); } /* Print the nodes and the DMs they are handling */ print_dms(hostname, myid, numprocs, local_numdms, dms); outdata = gen_fmatrix(local_numdms, worklen / cmd->downsamp); numread = get_data(outdata, blocksperread, &s, &obsmask, idispdt, offsets, &padding); while (numread == worklen) { numread /= cmd->downsamp; if (myid == 0) print_percent_complete(totwrote, totnumtowrite); /* Write the latest chunk of data, but don't */ /* write more than cmd->numout points. */ numtowrite = numread; if (cmd->numoutP && (totwrote + numtowrite) > cmd->numout) numtowrite = cmd->numout - totwrote; if (myid > 0) { write_data(outfiles, local_numdms, outdata, 0, numtowrite); /* Update the statistics */ if (!padding) { for (ii = 0; ii < numtowrite; ii++) update_stats(statnum + ii, outdata[0][ii], &min, &max, &avg, &var); statnum += numtowrite; } } totwrote += numtowrite; /* Stop if we have written out all the data we need to */ if (cmd->numoutP && (totwrote == cmd->numout)) break; numread = get_data(outdata, blocksperread, &s, &obsmask, idispdt, offsets, &padding); } datawrote = totwrote; } else { /* Main loop if we are barycentering... */ /* What ephemeris will we use? (Default is DE405) */ strcpy(ephem, "DE405"); /* Define the RA and DEC of the observation */ ra_dec_to_string(rastring, idata.ra_h, idata.ra_m, idata.ra_s); ra_dec_to_string(decstring, idata.dec_d, idata.dec_m, idata.dec_s); /* Allocate some arrays */ btoa = gen_dvect(numbarypts); ttoa = gen_dvect(numbarypts); for (ii = 0; ii < numbarypts; ii++) ttoa[ii] = tlotoa + TDT * ii / SECPERDAY; /* Call TEMPO for the barycentering */ if (myid == 0) { double maxvoverc = -1.0, minvoverc = 1.0, *voverc = NULL; printf("\nGenerating barycentric corrections...\n"); voverc = gen_dvect(numbarypts); barycenter(ttoa, btoa, voverc, numbarypts, rastring, decstring, obs, ephem); for (ii = 0; ii < numbarypts; ii++) { if (voverc[ii] > maxvoverc) maxvoverc = voverc[ii]; if (voverc[ii] < minvoverc) minvoverc = voverc[ii]; avgvoverc += voverc[ii]; } avgvoverc /= numbarypts; vect_free(voverc); printf(" Average topocentric velocity (c) = %.7g\n", avgvoverc); printf(" Maximum topocentric velocity (c) = %.7g\n", maxvoverc); printf(" Minimum topocentric velocity (c) = %.7g\n\n", minvoverc); printf("De-dispersing using %d subbands.\n", cmd->nsub); if (cmd->downsamp > 1) { printf(" Downsample = %d\n", cmd->downsamp); printf(" New sample dt = %.10g\n", dsdt); } printf("\n"); } /* Print the nodes and the DMs they are handling */ print_dms(hostname, myid, numprocs, local_numdms, dms); MPI_Bcast(btoa, numbarypts, MPI_DOUBLE, 0, MPI_COMM_WORLD); MPI_Bcast(&avgvoverc, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD); blotoa = btoa[0]; /* Dispersion delays (in bins). The high freq gets no delay */ /* All other delays are positive fractions of bin length (dt) */ dispdt = subband_search_delays(s.num_channels, cmd->nsub, avgdm, idata.freq, idata.chan_wid, avgvoverc); idispdt = gen_ivect(s.num_channels); for (ii = 0; ii < s.num_channels; ii++) idispdt[ii] = NEAREST_LONG(dispdt[ii] / idata.dt); vect_free(dispdt); /* The subband dispersion delays (see note above) */ offsets = gen_imatrix(local_numdms, cmd->nsub); for (ii = 0; ii < local_numdms; ii++) { double *subdispdt; subdispdt = subband_delays(s.num_channels, cmd->nsub, dms[ii], idata.freq, idata.chan_wid, avgvoverc); dtmp = subdispdt[cmd->nsub - 1]; for (jj = 0; jj < cmd->nsub; jj++) offsets[ii][jj] = NEAREST_LONG((subdispdt[jj] - dtmp) / dsdt); vect_free(subdispdt); } /* Convert the bary TOAs to differences from the topo TOAs in */ /* units of bin length (dt) rounded to the nearest integer. */ dtmp = (btoa[0] - ttoa[0]); for (ii = 0; ii < numbarypts; ii++) btoa[ii] = ((btoa[ii] - ttoa[ii]) - dtmp) * SECPERDAY / dsdt; /* Find the points where we need to add or remove bins */ { int oldbin = 0, currentbin; double lobin, hibin, calcpt; numdiffbins = abs(NEAREST_LONG(btoa[numbarypts - 1])) + 1; diffbins = gen_ivect(numdiffbins); diffbinptr = diffbins; for (ii = 1; ii < numbarypts; ii++) { currentbin = NEAREST_LONG(btoa[ii]); if (currentbin != oldbin) { if (currentbin > 0) { calcpt = oldbin + 0.5; lobin = (ii - 1) * TDT / dsdt; hibin = ii * TDT / dsdt; } else { calcpt = oldbin - 0.5; lobin = -((ii - 1) * TDT / dsdt); hibin = -(ii * TDT / dsdt); } while (fabs(calcpt) < fabs(btoa[ii])) { /* Negative bin number means remove that bin */ /* Positive bin number means add a bin there */ *diffbinptr = NEAREST_LONG(LININTERP (calcpt, btoa[ii - 1], btoa[ii], lobin, hibin)); diffbinptr++; calcpt = (currentbin > 0) ? calcpt + 1.0 : calcpt - 1.0; } oldbin = currentbin; } } *diffbinptr = cmd->numout; /* Used as a marker */ } diffbinptr = diffbins; /* Now perform the barycentering */ outdata = gen_fmatrix(local_numdms, worklen / cmd->downsamp); numread = get_data(outdata, blocksperread, &s, &obsmask, idispdt, offsets, &padding); while (numread == worklen) { /* Loop to read and write the data */ int numwritten = 0; double block_avg, block_var; numread /= cmd->downsamp; /* Determine the approximate local average */ avg_var(outdata[0], numread, &block_avg, &block_var); if (myid == 0) print_percent_complete(totwrote, totnumtowrite); /* Simply write the data if we don't have to add or */ /* remove any bins from this batch. */ /* OR write the amount of data up to cmd->numout or */ /* the next bin that will be added or removed. */ numtowrite = abs(*diffbinptr) - datawrote; if (cmd->numoutP && (totwrote + numtowrite) > cmd->numout) numtowrite = cmd->numout - totwrote; if (numtowrite > numread) numtowrite = numread; if (myid > 0) { write_data(outfiles, local_numdms, outdata, 0, numtowrite); /* Update the statistics */ if (!padding) { for (ii = 0; ii < numtowrite; ii++) update_stats(statnum + ii, outdata[0][ii], &min, &max, &avg, &var); statnum += numtowrite; } } datawrote += numtowrite; totwrote += numtowrite; numwritten += numtowrite; if ((datawrote == abs(*diffbinptr)) && (numwritten != numread) && (totwrote < cmd->numout)) { /* Add/remove a bin */ int skip, nextdiffbin; skip = numtowrite; /* Write the rest of the data after adding/removing a bin */ do { if (*diffbinptr > 0) { /* Add a bin */ if (myid > 0) write_padding(outfiles, local_numdms, block_avg, 1); numadded++; totwrote++; } else { /* Remove a bin */ numremoved++; datawrote++; numwritten++; skip++; } diffbinptr++; /* Write the part after the diffbin */ numtowrite = numread - numwritten; if (cmd->numoutP && (totwrote + numtowrite) > cmd->numout) numtowrite = cmd->numout - totwrote; nextdiffbin = abs(*diffbinptr) - datawrote; if (numtowrite > nextdiffbin) numtowrite = nextdiffbin; if (myid > 0) { write_data(outfiles, local_numdms, outdata, skip, numtowrite); /* Update the statistics and counters */ if (!padding) { for (ii = 0; ii < numtowrite; ii++) update_stats(statnum + ii, outdata[0][skip + ii], &min, &max, &avg, &var); statnum += numtowrite; } } numwritten += numtowrite; datawrote += numtowrite; totwrote += numtowrite; skip += numtowrite; /* Stop if we have written out all the data we need to */ if (cmd->numoutP && (totwrote == cmd->numout)) break; } while (numwritten < numread); } /* Stop if we have written out all the data we need to */ if (cmd->numoutP && (totwrote == cmd->numout)) break; numread = get_data(outdata, blocksperread, &s, &obsmask, idispdt, offsets, &padding); } } if (myid > 0) { /* Calculate the amount of padding we need */ if (cmd->numoutP && (cmd->numout > totwrote)) padwrote = padtowrite = cmd->numout - totwrote; /* Write the new info file for the output data */ idata.dt = dsdt; update_infodata(&idata, totwrote, padtowrite, diffbins, numdiffbins, cmd->downsamp); for (ii = 0; ii < local_numdms; ii++) { idata.dm = dms[ii]; if (!cmd->nobaryP) { double baryepoch, barydispdt, baryhifreq; baryhifreq = idata.freq + (s.num_channels - 1) * idata.chan_wid; barydispdt = delay_from_dm(dms[ii], doppler(baryhifreq, avgvoverc)); baryepoch = blotoa - (barydispdt / SECPERDAY); idata.bary = 1; idata.mjd_i = (int) floor(baryepoch); idata.mjd_f = baryepoch - idata.mjd_i; } sprintf(idata.name, "%s_DM%.2f", outfilenm, dms[ii]); writeinf(&idata); } /* Set the padded points equal to the average data point */ if (idata.numonoff >= 1) { int index, startpad, endpad; for (ii = 0; ii < local_numdms; ii++) { fclose(outfiles[ii]); sprintf(datafilenm, "%s_DM%.2f.dat", outfilenm, dms[ii]); outfiles[ii] = chkfopen(datafilenm, "rb+"); } for (ii = 0; ii < idata.numonoff; ii++) { index = 2 * ii; startpad = idata.onoff[index + 1]; if (ii == idata.numonoff - 1) endpad = idata.N - 1; else endpad = idata.onoff[index + 2]; for (jj = 0; jj < local_numdms; jj++) chkfseek(outfiles[jj], (startpad + 1) * sizeof(float), SEEK_SET); padtowrite = endpad - startpad; write_padding(outfiles, local_numdms, avg, padtowrite); } } } /* Print simple stats and results */ var /= (datawrote - 1); if (myid == 0) print_percent_complete(1, 1); if (myid == 1) { printf("\n\nDone.\n\nSimple statistics of the output data:\n"); printf(" Data points written: %ld\n", totwrote); if (padwrote) printf(" Padding points written: %ld\n", padwrote); if (!cmd->nobaryP) { if (numadded) printf(" Bins added for barycentering: %d\n", numadded); if (numremoved) printf(" Bins removed for barycentering: %d\n", numremoved); } printf(" Maximum value of data: %.2f\n", max); printf(" Minimum value of data: %.2f\n", min); printf(" Data average value: %.2f\n", avg); printf(" Data standard deviation: %.2f\n", sqrt(var)); printf("\n"); } /* Close the files and cleanup */ if (cmd->maskfileP) free_mask(obsmask); if (myid > 0) { for (ii = 0; ii < local_numdms; ii++) fclose(outfiles[ii]); free(outfiles); } vect_free(outdata[0]); vect_free(outdata); vect_free(dms); free(hostname); vect_free(idispdt); vect_free(offsets[0]); vect_free(offsets); free(datafilenm); free(outfilenm); free(outpath); if (!cmd->nobaryP) { vect_free(btoa); vect_free(ttoa); vect_free(diffbins); } MPI_Finalize(); return (0); }
int main(int argc, char **argv) { int i_file,i_scan,i_chan,i_bin,n_omit,status=0; int NFirstTable, NumHDU, NDumps, TotDumps=0, hdutype; // int OutChans; int spk; int got_bins=0, got_mjd1=0; //, zeroed_outprofs=0; long NPtsProf=0, FirstNPtsProf=0; float Weight=1.0, TotWeight=0.; // int ProfSum=0; double x, ptype; double MJD_first=0., MJD_last=0., MJD_mid; double IMJDMid, MJDSecsMid; double SBase,Srms,Duty,SPeak,FinalMask[NBINMAX]; double OutFreq; char ProgName[32]; char Outfile[128]; char Header[256]; struct ASPHdr *Hdr; struct SubHdr Subhdr; struct StdProfs *InProfile, OutProfile; struct RunVars RunMode; fitsfile **Fin; FILE *Fout, *Fcheck; Cmdline *Cmd; /* Get command line variables */ Cmd = parseCmdline(argc, argv); /* Normally use this somewhere, and not showOptionValues */ Cmd->tool = Cmd->tool; strcpy(ProgName, argv[0]); Fin = (fitsfile **)malloc(Cmd->InfileC*sizeof(fitsfile)); Hdr = (struct ASPHdr *)malloc(Cmd->InfileC*sizeof(struct ASPHdr)); /* Dynamically allocate RunMode variables */ if (AllocRunMode(&RunMode) < 0){ printf("Could not allocate RunMode structure. Exiting...\n"); exit(2); } strcpy(RunMode.Infile,Cmd->Infile); // if(!zeroed_outprofs) { /* if(Cmd->SortChansP){ OutChans=Hdr[0].obs.NChan; } else{ */ // OutChans=1; // } // OutProfile=(struct StdProfs *)malloc(OutChans*sizeof(struct StdProfs)); // TotWeight=(float *)malloc(OutChans*sizeof(float)); /* Zero out profiles */ // for(i_chan=0;i_chan<OutChans;i_chan++){ FZero(OutProfile.rstds,NBINMAX); FZero(OutProfile.rstdq,NBINMAX); FZero(OutProfile.rstdu,NBINMAX); FZero(OutProfile.rstdv,NBINMAX); // } //zeroed_outprofs=1; // } /* Create an output file to check omissions if in vebose mode */ if (Cmd->VerboseP || Cmd->CheckOmitP){ if((Fcheck = fopen("check_omit.dat","w")) == 0) { printf("Cannot open %s. Exiting...\n",Outfile); exit(1); } } /* read in all input files and add each to the final profile */ /* read in all input file headers */ NPtsProf=0; for (i_file=0;i_file<Cmd->InfileC;i_file++){ n_omit=0; status=0; if(fits_open_file(&Fin[i_file], Cmd->Infile[i_file], READONLY, &status)){ printf("Error opening FITS file %s !!!\n", Cmd->Infile[i_file]); exit(1); } if(ReadASPHdr(&Hdr[i_file], Fin[i_file]) < 0){ printf("%s> Unable to read Header from file %s. Exiting...\n", ProgName,Cmd->Infile[i_file]); exit(1); } /* Write file name in verbose-mode omit check file */ if(Cmd->VerboseP || Cmd->CheckOmitP) fprintf(Fcheck, "\n%s\n",Cmd->Infile[i_file]); /* for now just check if all files have same number of channels */ /* if(Cmd->SortChansP) if(i_file>0 && Hdr[i_file].obs.NChan!=Hdr[0].obs.NChan){ fprintf(stderr,"%s> Different numbers of channels in different files:\n\n", ProgName); fprintf(stderr,"%s: %d channels, %s: %d channels\n", Cmd->Infile[0],Hdr[0].obs.NChan, Cmd->Infile[i_file],Hdr[i_file].obs.NChan); } */ /* now find the number of dumps in the file */ fits_get_num_hdus(Fin[i_file], &NumHDU, &status); if(!strcmp(Hdr[i_file].gen.HdrVer,"Ver1.0")){ NDumps = NumHDU-3; /* the "3" is temporary, depending on how many non-data tables we will be using */ } else if(!strcmp(Hdr[i_file].gen.HdrVer,"Ver1.0.1")){ NDumps = (NumHDU-3)/2; } else{ fprintf(stderr,"%s> Do not recognize FITS file version in header.\n", ProgName); fprintf(stderr,"This header is %s. Exiting...\n",Hdr[i_file].gen.HdrVer); exit(1); } printf("File %s:\n",Cmd->Infile[i_file]); printf(" Number of channels: %d\n",Hdr[i_file].obs.NChan) ; printf(" Number of dumps: %d\n",NDumps); /* Move to the first data table HDU in the fits file */ if(!strcmp(Hdr[i_file].gen.HdrVer,"Ver1.0")) fits_movnam_hdu(Fin[i_file], BINARY_TBL, "STOKES0", 0, &status); else if (!strcmp(Hdr[i_file].gen.HdrVer,"Ver1.0.1")) fits_movnam_hdu(Fin[i_file], ASCII_TBL, "DUMPREF0", 0, &status); /* Get the current HDU number */ fits_get_hdu_num(Fin[i_file], &NFirstTable); /* Set up Profile structure size */ for(i_scan=0;i_scan<NDumps;i_scan++){ InProfile=(struct StdProfs *)malloc(Hdr[i_file].obs.NChan* sizeof(struct StdProfs)); /* move to next dump's data */ if(!strcmp(Hdr[i_file].gen.HdrVer,"Ver1.0")){ fits_movabs_hdu(Fin[i_file], NFirstTable+i_scan, &hdutype, &status); } else if(!strcmp(Hdr[i_file].gen.HdrVer,"Ver1.0.1")){ /* if we've reached the end of the FITS file then increase FileNo */ fits_movabs_hdu(Fin[i_file],NFirstTable+(i_scan%MAXDUMPS)*2+1,&hdutype, &status); fits_get_num_rows(Fin[i_file], &NPtsProf, &status);status=0; fits_movrel_hdu(Fin[i_file], -1, NULL, &status); } /* IF not done so, use number of bins from first file to compare to the rest of the files */ if(got_bins==0){ FirstNPtsProf=NPtsProf; got_bins=1; } /********** FIX: SKIP THIS WITHOUT DOING THE OMIT THING ***********/ /********** AND DON'T ADD TO NUMBER COUNT ************/ if(NPtsProf != FirstNPtsProf) { fprintf(stderr,"Warning: Skipping scan %d (%ld bins ", i_scan,NPtsProf); fprintf(stderr,"vs. %ld bins in others).\n",FirstNPtsProf); n_omit += Hdr[i_file].obs.NChan; } else{ /* find NPtsProf */ ReadASPStokes(&Hdr[i_file], &Subhdr, Fin[i_file], NPtsProf, InProfile, i_scan, Cmd->VerboseP); /* Add this profile onto running output profile */ for(i_chan=0;i_chan<Hdr[i_file].obs.NChan;i_chan++){ /* Bad scans are zeroed so if summ of the profile is zero, it's not to be used in summation */ // ProfSum = FSum(&InProfile[i_chan].rstds[0], NPtsProf); // ProfSum = 0; // ProfSum = ArrayZero(InProfile[i_chan].rstds, NPtsProf); // if(ProfSum != 0.0) { // i.e. good data /* Test that all bins in current profile are not zeroed */ if(!ArrayZero(InProfile[i_chan].rstds, NPtsProf)) { // i.e. good data // if(InProfile[i_chan].rstds[0] > -99998.) { // i.e. good data /* If first MJD has not been registered, then do so since this would be the first non-omitted scan */ if (got_mjd1==0) { MJD_first = (double)Hdr[i_file].obs.IMJDStart + Subhdr.DumpMiddleSecs/86400.; got_mjd1=1; } if (i_scan==NDumps-1) { /* Just keep overwriting MJD_last every i_file -- that way we ensure getting the last MJD of the FINAL non-omitted scan used */ MJD_last = (double)Hdr[i_file].obs.IMJDStart + Subhdr.DumpMiddleSecs/86400.; } /* Get SNR for each Profile if we want to use weighting; otherwise weights will all be 1.0 */ if(Cmd->WeightP) { Duty = DutyLookup(Hdr[i_file].target.PSRName); BMask(InProfile[i_chan].rstds,&Hdr[i_file].redn.RNBinTimeDump, &Duty,FinalMask); Baseline(InProfile[i_chan].rstds,FinalMask, &Hdr[i_file].redn.RNBinTimeDump,&SBase,&Srms); SPeak = FindPeak(InProfile[i_chan].rstds, &Hdr[i_file].redn.RNBinTimeDump,&spk); InProfile[i_chan].SNR = SPeak*Srms; // Weight = InProfile[i_chan].SNR; Weight = Srms; // which is actually 1/RMS. } // printf("SNR %d = %lf\n",i_chan,InProfile[i_chan].SNR); /* Need to figure out how to organize input channels to match * output channels */ /* if(Cmd->SortChansP){ for(i_bin=0;i_bin<NPtsProf;i_bin++) { OutProfile[i_chan].rstds[i_bin] += Weight*InProfile[i_chan].rstds[i_bin]; OutProfile[i_chan].rstdq[i_bin] += Weight*InProfile[i_chan].rstdq[i_bin]; OutProfile[i_chan].rstdu[i_bin] += Weight*InProfile[i_chan].rstdu[i_bin]; OutProfile[i_chan].rstdv[i_bin] += Weight*InProfile[i_chan].rstdv[i_bin]; } } else{ */ for(i_bin=0;i_bin<NPtsProf;i_bin++) { OutProfile.rstds[i_bin] += Weight*InProfile[i_chan].rstds[i_bin]; OutProfile.rstdq[i_bin] += Weight*InProfile[i_chan].rstdq[i_bin]; OutProfile.rstdu[i_bin] += Weight*InProfile[i_chan].rstdu[i_bin]; OutProfile.rstdv[i_bin] += Weight*InProfile[i_chan].rstdv[i_bin]; // printf("%f\n",OutProfile[0].rstds[i_bin]);fflush(stdout); } // } TotWeight += Weight; // for now keep at zero index /* Print profile weights for each scan, for each channel */ if(RunMode.Verbose) { if(i_chan==0) printf("Profile weights -- scan %d: \n ",i_scan); printf("%6.2f ",Weight); if(i_chan==Hdr[i_file].obs.NChan-1) printf("\n");fflush(stdout); } } else { n_omit++; if(Cmd->VerboseP || Cmd->CheckOmitP){ fprintf(Fcheck, "%6d %.1lf\n", i_scan,Hdr[i_file].obs.ChanFreq[i_chan]); /* printf("File %d, Dump %d, Channel %d (%lf MHz) were found to be\n", i_file,i_scan,i_chan,Hdr[i_file].obs.ChanFreq[i_chan]); printf(" zeroed and so are not included.\n");fflush(stdout); */ } } // if(i_chan==0) {for(i=0;i<50;i++) printf("%lf ",OutProfile[0].rstds[i]);printf("\n\n");fflush(stdout);}; } /*****************/ /* } */ } /* else from positive check on NPtsProf */ free(InProfile); } /****** maybe bring this inside the ELSE where entire scans aren't being omitted ******/ /* if (Cmd->SortChansP) TotDumps += (NDumps - n_omit); else */ TotDumps += (NDumps*Hdr[i_file].obs.NChan - n_omit); //free(InProfile); printf("Reading of file %s complete and successful.\n", Cmd->Infile[i_file]); printf("%d scans omitted.\n\n",n_omit);fflush(stdout); } if(Cmd->VerboseP || Cmd->CheckOmitP) fclose(Fcheck); /* Appease the format of the MakePol routine by making up these RunMode structure members */ strcpy(RunMode.Source,Hdr[0].target.PSRName); RunMode.Verbose = Cmd->VerboseP; RunMode.FlipPA = 0; RunMode.NoBase = Cmd->NoBaseP; /* divide out total number of dumps to get the average */ // for(i_chan=0;i_chan<OutChans;i_chan++){ printf("Totdumps = %d\n",TotDumps); fflush(stdout); for(i_bin=0;i_bin<NPtsProf;i_bin++) { OutProfile.rstds[i_bin] /= TotWeight; OutProfile.rstdq[i_bin] /= TotWeight; OutProfile.rstdu[i_bin] /= TotWeight; OutProfile.rstdv[i_bin] /= TotWeight; } MakePol(&RunMode, (int)NPtsProf, &OutProfile); /* Open file for writing */ sprintf(Outfile,"AddProf.out"); /* now write the output ascii added profile */ if ((Fout = fopen(Outfile,"w")) == 0) { printf("Cannot open %s. Exiting...\n",Outfile); exit(1); } /* take average MJD of first to last scan */ MJD_mid = (MJD_first + MJD_last)/2.; IMJDMid = floor(MJD_mid); MJDSecsMid = (MJD_mid - IMJDMid)*86400.; printf("MJD_mid = %lf, IMJDMid = %lf, MJDSecsMid = %lf\n",MJD_mid,IMJDMid,MJDSecsMid);fflush(stdout); /* to choose a channel to put in the header for now, ust use the average of the first datafile's channels */ OutFreq=0.; for(i_chan=0; i_chan<Hdr[0].obs.NChan; i_chan++){ OutFreq += Hdr[0].obs.ChanFreq[i_chan]; } OutFreq /= Hdr[0].obs.NChan; /* Create and print header line for output file(s) */ sprintf(Header,"# %.1f %.7f %.10f %ld %.3f %.3f %d %s %d %9s %.10f", IMJDMid, MJDSecsMid, // Subhdr.DumpRefPeriod[i_chan], 0., (long)1, OutFreq, // Hdr[0].obs.DM, Hdr[i_file].redn.RNBinTimeDump, Hdr[0].obs.DM, (int)NPtsProf, Hdr[0].obs.ObsvtyCode, 1, Hdr[0].target.PSRName, 0.); // Subhdr.DumpRefPhase[i_chan]); fprintf(Fout,"%s\n",Header); for(i_bin=0;i_bin<NPtsProf;i_bin++) { /* see how strong the linear polarization is */ x = OutProfile.stdlin[i_bin]*OutProfile.Srms; ptype = 43.1; if (x > 1.) ptype=43.2; if (x > 2.) ptype=43.3; if (x > 3.) ptype=43.4; if (x > 4.) ptype=43.5; if (x > 5.) ptype=43.6; fprintf(Fout,"%5d%15.7f%15.7f%15.7f%15.7f%15.7f%15.7f%15.7f%6.1f\n",i_bin, OutProfile.rstds[i_bin],OutProfile.rstdq[i_bin], OutProfile.rstdu[i_bin], OutProfile.rstdv[i_bin], /* phi in degrees */ OutProfile.stdlin[i_bin], OutProfile.stdphi[i_bin]*180.0/TWOPI, OutProfile.stdphierr[i_bin]*180.0/TWOPI,ptype); } printf("Created output file %s\n",Outfile); fclose(Fout); // } /* Write all this to file */ printf("\nCompleted successfully.\n\n");fflush(stdout); exit(0); }
int main(int argc, char **argv) { void *retval; int engine, evaltype, record, errorcode, usevaluefile = 1; Board *state; threadParameter *threadparameter; char *errorstring; char *tempcheck; FILE *logfile, *gamefile; pthread_t inputThread, outputThread; /* thread that takes care of input/output to user/front end */ prodcons *input, *output; /* communication buffers for iothread */ Cmdline *cmd; logfile = gamefile = NULL; #ifdef WIN32 /* copied from Dann Corbit to use under windows environment */ /* No buffering, please... */ setbuf(stdout, NULL); setbuf(stdin, NULL); /* and I *really* mean it, too! */ setvbuf(stdout, NULL, _IONBF, 0); setvbuf(stdin, NULL, _IONBF, 0); fflush(NULL); #endif srandom((unsigned int) time(NULL)); state = (Board *) xmalloc(sizeof(Board)); threadparameter = (threadParameter *) xmalloc(sizeof(threadParameter)); cmd = parseCmdline(argc, argv); if (cmd->show_helpP) usage(); if (cmd->show_versionP) { printf("%s %s\n", argv[0], VERSION); exit(0); } if (cmd->configfileP) { processConfigFile(cmd->configfile); } else { processConfigFile(NULL); } if ((cmd->verboseP) && (cmd->verbose > 0)){ configvalues.verbose = cmd->verbose; } if (configvalues.verbose > 0) { if (cmd->logfileP) { logfile = fopen(cmd->logfile, "w"); } else if (configvalues.logfilename) { logfile = fopen(configvalues.logfilename, "w"); } else { logfile = NULL; configvalues.verbose = 0; } if (logfile) { /* Make log file line buffered*/ setlinebuf(logfile); } state->logfile = logfile; } else { state->logfile = NULL; } state->verbosity = configvalues.verbose; record = 0; gamefile = NULL; if ((cmd->recordP) || (configvalues.record)) { if (!cmd->norecordP) { record = 1; if (cmd->gamefileP) { gamefile = fopen(cmd->gamefile, "a"); } else if (configvalues.gamefilename) { gamefile = fopen(configvalues.gamefilename, "a"); } else { gamefile = NULL; record = 0; } } } state->record = record; state->gamefile = gamefile; if (cmd->randomvarianceP) { state->random = cmd->randomvariance; } else { state->random = configvalues.random; } engine = configvalues.engine; if (cmd->randomP) { engine = RANDOM_ENGINE; } if (cmd->onestepP) { engine = ONESTEP_ENGINE; } if (cmd->completeP) { engine = COMPLETENEGAMAX_ENGINE; } if (cmd->multistepP) { engine = MULTISTEP_ENGINE; } if (cmd->negascoutP) { engine = NEGASCOUT_ENGINE; } evaltype = configvalues.evaltype; usevaluefile = configvalues.usevaluefile; if (cmd->fixedevalP) { usevaluefile = 0; } if (cmd->relativeevalP) { evaltype = RELATIVEPOSITIONPIECE_EVAL; } else if (cmd->positionevalP) { evaltype = POSITIONPIECE_EVAL; } switch (engine) { case RANDOM_ENGINE: state->engine = randomPlay; break; case ONESTEP_ENGINE: state->engine = oneStep; break; case COMPLETENEGAMAX_ENGINE: state->engine = multiNegamax; break; case MULTISTEP_ENGINE: state->engine = multiStep; break; case NEGASCOUT_ENGINE: state->engine = timedIterative; state->searcher = negaScoutDriver; break; default: state->engine = NULL; } if (cmd->resignP) { configvalues.resign = - ((int) (cmd->resign * 100)); } switch (evaltype) { case POSITIONPIECE_EVAL: if (cmd->noresignP) { state->resign = MINVALUE - 1; } else { state->resign = configvalues.resign; } state->evalboard = positionheuri; break; case RELATIVEPOSITIONPIECE_EVAL: state->evalboard = relativepositionheuri; state->resign = MINVALUE - 1; break; default: state->evalboard = NULL; } if (!cmd->nobookP) { if (cmd->bookfileP) { errorcode = initialiseOpeningBook(cmd->bookfile); if (errorcode) { errorstring = bookErrorToString(errorcode); fprintf(stderr,"%s\n",errorstring); free(errorstring); } } else if ((configvalues.usebook) && (configvalues.bookfilename)) { errorcode = initialiseOpeningBook(configvalues.bookfilename); if (errorcode) { errorstring = bookErrorToString(errorcode); fprintf(stderr,"%s\n",errorstring); free(errorstring); } } } if (usevaluefile) { if (cmd->valuefileP) { initialisePieceValues(cmd->valuefile); configvalues.valuefilename = cmd->valuefile; } else if (configvalues.valuefilename) { initialisePieceValues(configvalues.valuefilename); } else { initialisePieceValues(NULL); } } else { initialisePieceValues(NULL); } if (cmd->computernameP) { state->name = xstrdup(cmd->computername); } else if (configvalues.computername) { state->name = xstrdup(configvalues.computername); } else { if (configvalues.valuefilename) { state->name = (char *) xmalloc(strlen(PACKAGE_STRING) + strlen(configvalues.valuefilename) + 2); tempcheck = state->name; state->name = strcpy(state->name, PACKAGE_STRING); state->name = strcat(state->name, " "); state->name = strcat(state->name, configvalues.valuefilename); if (tempcheck != state->name) { printf("f****d\n"); } } else { state->name = (char *) xstrdup (PACKAGE_STRING); } } if (cmd->dP) { configvalues.maxdepth = cmd->d; } state->depth = configvalues.maxdepth; if (cmd->epdmindepthP) { configvalues.epdmindepth = cmd->epdmindepth; } state->epdmindepth = configvalues.epdmindepth; if (cmd->drawvalueP) { configvalues.drawvalue = cmd->drawvalue; } state->drawvalue = configvalues.drawvalue; state->timeaggression = configvalues.timeaggression; if (cmd->hashsizeP) { state->hashtable = initialiseHashTable(cmd->hashsize); } else if (cmd->hashkilobytesP) { state->hashtable = initialiseHashTableBySize(cmd->hashkilobytes); } else if (configvalues.hashentries) { state->hashtable = initialiseHashTable(configvalues.hashentries); } else if (configvalues.hashkilobytes) { state->hashtable = initialiseHashTableBySize(configvalues.hashkilobytes); } else { state->hashtable = initialiseHashTable(0); } initialiseMoveLists(); initialiseGenerateFunctions(); input = (prodcons *) xmalloc(sizeof(prodcons)); output = (prodcons *) xmalloc(sizeof(prodcons)); initComms(input); initComms(output); state->input = input; state->output = output; threadparameter->input = input; threadparameter->output = output; threadparameter->name = state->name; pthread_create(&inputThread, NULL, doInput, (void *) threadparameter); pthread_create(&outputThread, NULL, doOutput, (void *) threadparameter); play(state); pthread_cancel(inputThread); pthread_join(outputThread, &retval); finaliseComms(input); finaliseComms(output); closeOpeningBook(); free(state); free(input); free(output); if (configvalues.verbose) { fflush(logfile); fclose(logfile); } if (record) { fflush(gamefile); fclose(gamefile); } exit(0); }
int main(int argc, char **argv) { int index = -1, need_type = 0; int objs_read, objs_to_read, has_suffix; long i, j, ct; char *cptr, *data, *short_filenm, *extension, key = '\n'; FILE *infile; Cmdline *cmd; infodata inf; /* Call usage() if we have no command line arguments */ if (argc == 1) { Program = argv[0]; usage(); exit(0); } /* Parse the command line using the excellent program Clig */ cmd = parseCmdline(argc, argv); #ifdef DEBUG showOptionValues(); #endif //fprintf(stdout, "\n\n PRESTO Binary File Reader\n"); //fprintf(stdout, " by Scott M. Ransom\n\n"); /* Set our index value */ if (cmd->bytP || cmd->sbytP) index = BYTE; else if (cmd->fltP || cmd->sfltP) index = FLOAT; else if (cmd->dblP || cmd->sdblP) index = DOUBLE; else if (cmd->fcxP || cmd->sfcxP) index = FCPLEX; else if (cmd->dcxP || cmd->sdcxP) index = DCPLEX; else if (cmd->shtP || cmd->sshtP) index = SHORT; else if (cmd->igrP || cmd->sigrP) index = INT; else if (cmd->lngP || cmd->slngP) index = LONG; else if (cmd->rzwP || cmd->srzwP) index = RZWCAND; else if (cmd->binP || cmd->sbinP) index = BINCAND; else if (cmd->posP || cmd->sposP) index = POSITION; else if (cmd->pkmbP) index = PKMBHDR; else if (cmd->bcpmP) index = BCPMHDR; else if (cmd->wappP) index = WAPPHDR; else if (cmd->spigotP) index = SPIGOTHDR; else if (cmd->filterbankP) index = SPECTRAINFO; #ifdef USELOFAR else if (cmd->lofarhdf5P) index = SPECTRAINFO; #endif else if (cmd->psrfitsP) index = SPECTRAINFO; /* Try to determine the data type from the file name */ if (index == -1) { has_suffix = split_root_suffix(cmd->argv[0], &short_filenm, &extension); if (!has_suffix) { need_type = 1; } else { if (strlen(extension) < 2) { need_type = 1; } else { if (0 == strcmp(extension, "dat")) { index = FLOAT; fprintf(stdout, "Assuming the data is floating point.\n\n"); } else if (0 == strcmp(extension, "sdat")) { index = SHORT; fprintf(stdout, "Assuming the data is short integers.\n\n"); } else if (0 == strcmp(extension, "fft")) { index = FCPLEX; fprintf(stdout, "Assuming the data is single precision complex.\n\n"); } else if ((0 == strcmp(extension, "fits")) || (0 == strcmp(extension, "sf"))) { if (strstr(short_filenm, "spigot_5") != NULL) { cmd->spigotP = 1; index = SPIGOTHDR; fprintf(stdout, "Assuming the data is from the Caltech/NRAO Spigot.\n\n"); } else if (is_PSRFITS(cmd->argv[0])) { cmd->psrfitsP = 1; index = SPECTRAINFO; fprintf(stdout, "Assuming the data is in PSRFITS format.\n\n"); } } else if (0 == strcmp(extension, "bcpm1") || 0 == strcmp(extension, "bcpm2")) { cmd->bcpmP = 1; index = BCPMHDR; fprintf(stdout, "Assuming the data is from a BCPM machine.\n\n"); } else if (0 == strcmp(extension, "pkmb")) { cmd->pkmbP = 1; index = PKMBHDR; fprintf(stdout, "Assuming the data is from the Parkes Multibeam machine.\n\n"); } else if (0 == strcmp(extension, "fil") || 0 == strcmp(extension, "fb")) { cmd->filterbankP = 1; index = SPECTRAINFO; fprintf(stdout, "Assuming the data is a SIGPROC filterbank file.\n\n"); } else if (0 == strcmp(extension, "h5")) { cmd->lofarhdf5P = 1; index = SPECTRAINFO; fprintf(stdout, "Assuming the data is a LOFAR HDF5 file.\n\n"); } else if (isdigit(extension[0]) && isdigit(extension[1]) && isdigit(extension[2])) { cmd->wappP = 1; index = WAPPHDR; fprintf(stdout, "Assuming the data is from a WAPP machine.\n\n"); } else if (0 == strcmp(extension, "pos")) { index = POSITION; fprintf(stdout, "Assuming the data contains 'position' structures.\n\n"); } else if (0 == strcmp(extension, "cand")) { /* A binary or RZW search file? */ if (NULL != (cptr = strstr(cmd->argv[0], "_bin"))) { index = BINCAND; fprintf(stdout, "Assuming the file contains binary candidates.\n\n"); } else if (NULL != (cptr = strstr(cmd->argv[0], "_rzw"))) { index = RZWCAND; ct = (long) (cptr - cmd->argv[0]); fprintf(stdout, "Assuming the file contains 'RZW' candidates.\n"); free(short_filenm); short_filenm = (char *) malloc(ct + 1); short_filenm[ct] = '\0'; strncpy(short_filenm, cmd->argv[0], ct); fprintf(stdout, "\nAttempting to read '%s.inf'. ", short_filenm); readinf(&inf, short_filenm); fprintf(stdout, "Successful.\n"); N = (long) (inf.N + DBLCORRECT); dt = inf.dt; if (cmd->nphP) nph = cmd->nph; else nph = 1.0; fprintf(stdout, "\nUsing N = %ld, dt = %g, and DC Power = %f\n\n", N, dt, nph); } else if (NULL != (cptr = strstr(cmd->argv[0], "_ACCEL"))) { index = RZWCAND; ct = (long) (cptr - cmd->argv[0]); fprintf(stdout, "Assuming the file contains 'RZW' candidates.\n"); free(short_filenm); short_filenm = (char *) malloc(ct + 1); short_filenm[ct] = '\0'; strncpy(short_filenm, cmd->argv[0], ct); fprintf(stdout, "\nAttempting to read '%s.inf'. ", short_filenm); readinf(&inf, short_filenm); fprintf(stdout, "Successful.\n"); N = (long) (inf.N + DBLCORRECT); dt = inf.dt; if (cmd->nphP) nph = cmd->nph; else nph = 1.0; fprintf(stdout, "\nUsing N = %ld, dt = %g, and DC Power = %f\n\n", N, dt, nph); } else need_type = 1; } else need_type = 1; } } /* If no file extension or if we don't understand the extension, exit */ if (need_type) { fprintf(stdout, "You must specify a data type for this file.\n\n"); free(short_filenm); exit(-1); } free(short_filenm); if (has_suffix) free(extension); } if (cmd->index[1] == -1 || cmd->index[1] == 0) cmd->index[1] = INT_MAX; if (cmd->index[1] < cmd->index[0]) { fprintf(stdout, "\nThe high index must be >= the low index."); fprintf(stdout, " Exiting.\n\n"); exit(-1); } // Use new-style backend reading stuff if (cmd->psrfitsP || cmd->filterbankP || cmd->lofarhdf5P) { struct spectra_info s; // Eventually we should use this... // identify_psrdatatype(struct spectra_info *s, int output); spectra_info_set_defaults(&s); if (cmd->psrfitsP) s.datatype=PSRFITS; if (cmd->filterbankP) s.datatype=SIGPROCFB; if (cmd->lofarhdf5P) s.datatype=LOFARHDF5; s.filenames = cmd->argv; s.num_files = cmd->argc; s.clip_sigma = 0.0; s.apply_flipband = s.apply_weight = s.apply_scale = s.apply_offset = -1; s.remove_zerodm = 0; read_rawdata_files(&s); SPECTRAINFO_print(0, (char *)(&s)); printf("\n"); exit(0); } if (cmd->spigotP) { SPIGOT_INFO spigot; if (read_SPIGOT_header(cmd->argv[0], &spigot)) { print_SPIGOT_header(&spigot); printf("\n"); } else { printf("\n Error reading spigot file!\n\n"); } exit(0); } if (cmd->wappP) { struct HEADERP *hdr = NULL; infile = chkfopen(cmd->argv[0], "rb"); hdr = head_parse(infile); set_WAPP_HEADER_version(hdr); if (hdr) { print_WAPP_hdr(hdr); printf("\n"); } else { printf("\n Error reading WAPP file!\n\n"); } exit(0); } /* Open the file */ infile = chkfopen(cmd->argv[0], "rb"); if (cmd->fortranP) { chkfileseek(infile, 1, sizeof(long), SEEK_SET); } /* Skip to the correct first object */ if (cmd->index[0] > 0) { chkfileseek(infile, (long) (cmd->index[0]), type_sizes[index], SEEK_CUR); } /* Read the file */ objs_to_read = objs_at_a_time[index]; data = (char *) malloc(type_sizes[index] * objs_at_a_time[index]); i = cmd->index[0]; do { if (objs_to_read > cmd->index[1] - i) objs_to_read = cmd->index[1] - i; objs_read = chkfread(data, type_sizes[index], objs_to_read, infile); for (j = 0; j < objs_read; j++) print_funct_ptrs[index] (i + j, data + j * type_sizes[index]); /* Just print 1 header for BCPM and WAPP files */ if (index == BCPMHDR || index == WAPPHDR || index == SPIGOTHDR) break; i += objs_read; if (cmd->pageP) { fflush(NULL); fprintf(stdout, "\nPress ENTER for next page, or any other key and "); fprintf(stdout, "then ENTER to exit.\n\n"); key = getchar(); } } while (!feof(infile) && i < cmd->index[1] && key == '\n'); fflush(NULL); if (feof(infile)) { fprintf(stdout, "\nEnd of file.\n\n"); } free(data); fclose(infile); exit(0); }
int main(int argc, char *argv[]) { /* Any variable that begins with 't' means topocentric */ /* Any variable that begins with 'b' means barycentric */ FILE *outfile; float *outdata = NULL; double tdf = 0.0, dtmp = 0.0, barydispdt = 0.0, dsdt = 0.0; double *dispdt, *tobsf = NULL, tlotoa = 0.0, blotoa = 0.0; double max = -9.9E30, min = 9.9E30, var = 0.0, avg = 0.0; char obs[3], ephem[10], *datafilenm, *outinfonm; char rastring[50], decstring[50]; int numchan = 1, newper = 0, oldper = 0, nummasked = 0, useshorts = 0; int numadded = 0, numremoved = 0, padding = 0, *maskchans = NULL, offset = 0; long slen, ii, numbarypts = 0, worklen = 65536; long numread = 0, numtowrite = 0, totwrote = 0, datawrote = 0; long padwrote = 0, padtowrite = 0, statnum = 0; int numdiffbins = 0, *diffbins = NULL, *diffbinptr = NULL, good_padvals = 0; int *idispdt; struct spectra_info s; infodata idata; Cmdline *cmd; mask obsmask; /* Call usage() if we have no command line arguments */ if (argc == 1) { Program = argv[0]; printf("\n"); usage(); exit(0); } /* Parse the command line using the excellent program Clig */ cmd = parseCmdline(argc, argv); spectra_info_set_defaults(&s); s.filenames = cmd->argv; s.num_files = cmd->argc; // If we are zeroDMing, make sure that clipping is off. if (cmd->zerodmP) cmd->noclipP = 1; s.clip_sigma = cmd->clip; // -1 causes the data to determine if we use weights, scales, & // offsets for PSRFITS or flip the band for any data type where // we can figure that out with the data s.apply_flipband = (cmd->invertP) ? 1 : -1; s.apply_weight = (cmd->noweightsP) ? 0 : -1; s.apply_scale = (cmd->noscalesP) ? 0 : -1; s.apply_offset = (cmd->nooffsetsP) ? 0 : -1; s.remove_zerodm = (cmd->zerodmP) ? 1 : 0; if (cmd->noclipP) { cmd->clip = 0.0; s.clip_sigma = 0.0; } if (cmd->ifsP) { // 0 = default or summed, 1-4 are possible also s.use_poln = cmd->ifs + 1; } if (cmd->ncpus > 1) { #ifdef _OPENMP int maxcpus = omp_get_num_procs(); int openmp_numthreads = (cmd->ncpus <= maxcpus) ? cmd->ncpus : maxcpus; // Make sure we are not dynamically setting the number of threads omp_set_dynamic(0); omp_set_num_threads(openmp_numthreads); printf("Using %d threads with OpenMP\n\n", openmp_numthreads); #endif } else { #ifdef _OPENMP omp_set_num_threads(1); // Explicitly turn off OpenMP #endif } #ifdef DEBUG showOptionValues(); #endif printf("\n\n"); printf(" Pulsar Data Preparation Routine\n"); printf(" Type conversion, de-dispersion, barycentering.\n"); printf(" by Scott M. Ransom\n\n"); if (RAWDATA) { if (cmd->filterbankP) s.datatype = SIGPROCFB; else if (cmd->psrfitsP) s.datatype = PSRFITS; } else { // Attempt to auto-identify the data identify_psrdatatype(&s, 1); if (s.datatype == SIGPROCFB) cmd->filterbankP = 1; else if (s.datatype == PSRFITS) cmd->psrfitsP = 1; else if (s.datatype == SDAT) useshorts = 1; else if (s.datatype != DAT) { printf ("Error: Unable to identify input data files. Please specify type.\n\n"); exit(1); } } if (!RAWDATA) { char *root, *suffix; /* Split the filename into a rootname and a suffix */ if (split_root_suffix(s.filenames[0], &root, &suffix) == 0) { printf("\nThe input filename (%s) must have a suffix!\n\n", s.filenames[0]); exit(1); } printf("Reading input data from '%s'.\n", s.filenames[0]); printf("Reading information from '%s.inf'.\n\n", root); /* Read the info file if available */ readinf(&idata, root); free(root); free(suffix); s.files = (FILE **) malloc(sizeof(FILE *)); s.files[0] = chkfopen(s.filenames[0], "rb"); } else { char description[40]; psrdatatype_description(description, s.datatype); if (s.num_files > 1) printf("Reading %s data from %d files:\n", description, s.num_files); else printf("Reading %s data from 1 file:\n", description); for (ii = 0; ii < s.num_files; ii++) { printf(" '%s'\n", cmd->argv[ii]); } printf("\n"); } /* Determine the other file names and open the output data file */ slen = strlen(cmd->outfile) + 8; datafilenm = (char *) calloc(slen, 1); sprintf(datafilenm, "%s.dat", cmd->outfile); outfile = chkfopen(datafilenm, "wb"); sprintf(idata.name, "%s", cmd->outfile); outinfonm = (char *) calloc(slen, 1); sprintf(outinfonm, "%s.inf", cmd->outfile); if (RAWDATA) { read_rawdata_files(&s); if (cmd->ignorechanstrP) { s.ignorechans = get_ignorechans(cmd->ignorechanstr, 0, s.num_channels-1, &s.num_ignorechans, &s.ignorechans_str); if (s.ignorechans_str==NULL) { s.ignorechans_str = (char *)malloc(strlen(cmd->ignorechanstr)+1); strcpy(s.ignorechans_str, cmd->ignorechanstr); } } print_spectra_info_summary(&s); spectra_info_to_inf(&s, &idata); /* Finish setting up stuff common to all raw formats */ idata.dm = cmd->dm; worklen = s.spectra_per_subint; /* If we are offsetting into the file, change inf file start time */ if (cmd->start > 0.0 || cmd->offset > 0) { if (cmd->start > 0.0) /* Offset in units of worklen */ cmd->offset = (long) (cmd->start * idata.N / worklen) * worklen; add_to_inf_epoch(&idata, cmd->offset * idata.dt); offset_to_spectra(cmd->offset, &s); printf("Offsetting into the input files by %ld spectra (%.6g sec)\n", cmd->offset, cmd->offset * idata.dt); } if (cmd->maskfileP) maskchans = gen_ivect(idata.num_chan); /* Compare the size of the data to the size of output we request */ if (cmd->numoutP) { dtmp = idata.N; idata.N = cmd->numout; writeinf(&idata); idata.N = dtmp; } else { /* Set the output length to a good number if it wasn't requested */ cmd->numoutP = 1; cmd->numout = choose_good_N((long long)(idata.N/cmd->downsamp)); writeinf(&idata); printf("Setting a 'good' output length of %ld samples\n", cmd->numout); } /* The number of topo to bary time points to generate with TEMPO */ numbarypts = (long) (idata.dt * idata.N * 1.1 / TDT + 5.5) + 1; // Identify the TEMPO observatory code { char *outscope = (char *) calloc(40, sizeof(char)); telescope_to_tempocode(idata.telescope, outscope, obs); free(outscope); } } /* Read an input mask if wanted */ if (cmd->maskfileP) { read_mask(cmd->maskfile, &obsmask); printf("Read mask information from '%s'\n\n", cmd->maskfile); good_padvals = determine_padvals(cmd->maskfile, &obsmask, s.padvals); } else { obsmask.numchan = obsmask.numint = 0; } /* Determine our initialization data if we do _not_ have Parkes, */ /* Green Bank BCPM, or Arecibo WAPP data sets. */ if (!RAWDATA) { /* If we will be barycentering... */ if (!cmd->nobaryP) { /* The number of topo to bary time points to generate with TEMPO */ numbarypts = (long) (idata.dt * idata.N * 1.1 / TDT + 5.5) + 1; // Identify the TEMPO observatory code { char *outscope = (char *) calloc(40, sizeof(char)); telescope_to_tempocode(idata.telescope, outscope, obs); free(outscope); } } /* The number of data points to work with at a time */ if (worklen > idata.N) worklen = idata.N; worklen = (long) (worklen / 1024) * 1024; /* If we are offsetting into the file, change inf file start time */ if (cmd->start > 0.0 || cmd->offset > 0) { if (cmd->start > 0.0) /* Offset in units of worklen */ cmd->offset = (long) (cmd->start * idata.N / worklen) * worklen; add_to_inf_epoch(&idata, cmd->offset * idata.dt); printf("Offsetting into the input files by %ld samples (%.6g sec)\n", cmd->offset, cmd->offset * idata.dt); if (useshorts) { chkfileseek(s.files[0], cmd->offset, sizeof(short), SEEK_SET); } else { chkfileseek(s.files[0], cmd->offset, sizeof(float), SEEK_SET); } } /* Set the output length to a good number if it wasn't requested */ if (!cmd->numoutP) { cmd->numoutP = 1; cmd->numout = choose_good_N((long long)(idata.N/cmd->downsamp)); printf("Setting a 'good' output length of %ld samples\n", cmd->numout); } } /* Check if we are downsampling */ dsdt = idata.dt * cmd->downsamp; if (cmd->downsamp > 1) { printf("Downsampling by a factor of %d\n", cmd->downsamp); printf("New sample dt = %.10g\n\n", dsdt); if (worklen % cmd->downsamp) { printf("Error: The downsample factor (%d) must be a factor of the\n", cmd->downsamp); printf(" worklength (%ld). Exiting.\n\n", worklen); exit(1); } } printf("Writing output data to '%s'.\n", datafilenm); printf("Writing information to '%s'.\n\n", outinfonm); /* The topocentric epoch of the start of the data */ tlotoa = (double) idata.mjd_i + idata.mjd_f; if (!strcmp(idata.band, "Radio") && RAWDATA) { /* The topocentric spacing between channels */ tdf = idata.chan_wid; numchan = idata.num_chan; /* The topocentric observation frequencies */ tobsf = gen_dvect(numchan); tobsf[0] = idata.freq; for (ii = 0; ii < numchan; ii++) tobsf[ii] = tobsf[0] + ii * tdf; /* The dispersion delays (in time bins) */ dispdt = gen_dvect(numchan); // full float bins idispdt = gen_ivect(numchan); // nearest integer bins if (cmd->nobaryP) { /* Determine our dispersion time delays for each channel */ for (ii = 0; ii < numchan; ii++) dispdt[ii] = delay_from_dm(cmd->dm, tobsf[ii]); /* The highest frequency channel gets no delay */ /* All other delays are positive fractions of bin length (dt) */ dtmp = dispdt[numchan - 1]; for (ii = 0; ii < numchan; ii++) { dispdt[ii] = (dispdt[ii] - dtmp) / idata.dt; idispdt[ii] = (int) (dispdt[ii] + 0.5); } worklen *= ((int) (fabs(dispdt[0])) / worklen) + 1; } } else { /* For unknown radio raw data (Why is this here?) */ tobsf = gen_dvect(numchan); dispdt = gen_dvect(numchan); idispdt = gen_ivect(numchan); dispdt[0] = 0.0; idispdt[0] = 0; if (!strcmp(idata.band, "Radio")) { tobsf[0] = idata.freq + (idata.num_chan - 1) * idata.chan_wid; cmd->dm = idata.dm; } else { tobsf[0] = 0.0; cmd->dm = 0.0; } } if (cmd->nobaryP) { /* Main loop if we are not barycentering... */ /* Allocate our data array */ outdata = gen_fvect(worklen); printf("Massaging the data ...\n\n"); printf("Amount Complete = 0%%"); do { if (RAWDATA) numread = read_psrdata(outdata, worklen, &s, idispdt, &padding, maskchans, &nummasked, &obsmask); else if (useshorts) numread = read_shorts(s.files[0], outdata, worklen, numchan); else numread = read_floats(s.files[0], outdata, worklen, numchan); if (numread == 0) break; /* Downsample if requested */ if (cmd->downsamp > 1) numread = downsample(outdata, numread, cmd->downsamp); /* Print percent complete */ newper = (int) ((float) totwrote / cmd->numout * 100.0) + 1; if (newper > oldper) { printf("\rAmount Complete = %3d%%", newper); fflush(stdout); oldper = newper; } /* Write the latest chunk of data, but don't */ /* write more than cmd->numout points. */ numtowrite = numread; if ((totwrote + numtowrite) > cmd->numout) numtowrite = cmd->numout - totwrote; chkfwrite(outdata, sizeof(float), numtowrite, outfile); totwrote += numtowrite; /* Update the statistics */ if (!padding) { for (ii = 0; ii < numtowrite; ii++) update_stats(statnum + ii, outdata[ii], &min, &max, &avg, &var); statnum += numtowrite; } /* Stop if we have written out all the data we need to */ if (totwrote == cmd->numout) break; } while (numread); datawrote = totwrote; } else { /* Main loop if we are barycentering... */ double avgvoverc = 0.0, maxvoverc = -1.0, minvoverc = 1.0, *voverc = NULL; double *bobsf = NULL, *btoa = NULL, *ttoa = NULL; /* What ephemeris will we use? (Default is DE405) */ strcpy(ephem, "DE405"); /* Define the RA and DEC of the observation */ ra_dec_to_string(rastring, idata.ra_h, idata.ra_m, idata.ra_s); ra_dec_to_string(decstring, idata.dec_d, idata.dec_m, idata.dec_s); /* Allocate some arrays */ bobsf = gen_dvect(numchan); btoa = gen_dvect(numbarypts); ttoa = gen_dvect(numbarypts); voverc = gen_dvect(numbarypts); for (ii = 0; ii < numbarypts; ii++) ttoa[ii] = tlotoa + TDT * ii / SECPERDAY; /* Call TEMPO for the barycentering */ printf("Generating barycentric corrections...\n"); barycenter(ttoa, btoa, voverc, numbarypts, rastring, decstring, obs, ephem); for (ii = 0; ii < numbarypts; ii++) { if (voverc[ii] > maxvoverc) maxvoverc = voverc[ii]; if (voverc[ii] < minvoverc) minvoverc = voverc[ii]; avgvoverc += voverc[ii]; } avgvoverc /= numbarypts; vect_free(voverc); blotoa = btoa[0]; printf(" Average topocentric velocity (c) = %.7g\n", avgvoverc); printf(" Maximum topocentric velocity (c) = %.7g\n", maxvoverc); printf(" Minimum topocentric velocity (c) = %.7g\n\n", minvoverc); printf("Collecting and barycentering %s...\n\n", cmd->argv[0]); /* Determine the initial dispersion time delays for each channel */ for (ii = 0; ii < numchan; ii++) { bobsf[ii] = doppler(tobsf[ii], avgvoverc); dispdt[ii] = delay_from_dm(cmd->dm, bobsf[ii]); } /* The highest frequency channel gets no delay */ /* All other delays are positive fractions of bin length (dt) */ barydispdt = dispdt[numchan - 1]; for (ii = 0; ii < numchan; ii++) { dispdt[ii] = (dispdt[ii] - barydispdt) / idata.dt; idispdt[ii] = (int) (dispdt[ii] + 0.5); } if (RAWDATA) worklen *= ((int) (dispdt[0]) / worklen) + 1; /* If the data is de-dispersed radio data... */ if (!strcmp(idata.band, "Radio")) { printf("The DM of %.2f at the barycentric observing freq of %.3f MHz\n", idata.dm, bobsf[numchan - 1]); printf(" causes a delay of %f seconds compared to infinite freq.\n", barydispdt); printf(" This delay is removed from the barycented times.\n\n"); } printf("Topocentric epoch (at data start) is:\n"); printf(" %17.11f\n\n", tlotoa); printf("Barycentric epoch (infinite obs freq at data start) is:\n"); printf(" %17.11f\n\n", blotoa - (barydispdt / SECPERDAY)); /* Convert the bary TOAs to differences from the topo TOAs in */ /* units of bin length (dsdt) rounded to the nearest integer. */ dtmp = (btoa[0] - ttoa[0]); for (ii = 0; ii < numbarypts; ii++) btoa[ii] = ((btoa[ii] - ttoa[ii]) - dtmp) * SECPERDAY / dsdt; { /* Find the points where we need to add or remove bins */ int oldbin = 0, currentbin; double lobin, hibin, calcpt; numdiffbins = abs(NEAREST_LONG(btoa[numbarypts - 1])) + 1; diffbins = gen_ivect(numdiffbins); diffbinptr = diffbins; for (ii = 1; ii < numbarypts; ii++) { currentbin = NEAREST_LONG(btoa[ii]); if (currentbin != oldbin) { if (currentbin > 0) { calcpt = oldbin + 0.5; lobin = (ii - 1) * TDT / dsdt; hibin = ii * TDT / dsdt; } else { calcpt = oldbin - 0.5; lobin = -((ii - 1) * TDT / dsdt); hibin = -(ii * TDT / dsdt); } while (fabs(calcpt) < fabs(btoa[ii])) { /* Negative bin number means remove that bin */ /* Positive bin number means add a bin there */ *diffbinptr = NEAREST_LONG(LININTERP(calcpt, btoa[ii - 1], btoa[ii], lobin, hibin)); diffbinptr++; calcpt = (currentbin > 0) ? calcpt + 1.0 : calcpt - 1.0; } oldbin = currentbin; } } *diffbinptr = cmd->numout; /* Used as a marker */ } diffbinptr = diffbins; /* Now perform the barycentering */ printf("Massaging the data...\n\n"); printf("Amount Complete = 0%%"); /* Allocate our data array */ outdata = gen_fvect(worklen); do { /* Loop to read and write the data */ int numwritten = 0; double block_avg, block_var; if (RAWDATA) numread = read_psrdata(outdata, worklen, &s, idispdt, &padding, maskchans, &nummasked, &obsmask); else if (useshorts) numread = read_shorts(s.files[0], outdata, worklen, numchan); else numread = read_floats(s.files[0], outdata, worklen, numchan); if (numread == 0) break; /* Downsample if requested */ if (cmd->downsamp > 1) numread = downsample(outdata, numread, cmd->downsamp); /* Determine the approximate local average */ avg_var(outdata, numread, &block_avg, &block_var); /* Print percent complete */ newper = (int) ((float) totwrote / cmd->numout * 100.0) + 1; if (newper > oldper) { printf("\rAmount Complete = %3d%%", newper); fflush(stdout); oldper = newper; } /* Simply write the data if we don't have to add or */ /* remove any bins from this batch. */ /* OR write the amount of data up to cmd->numout or */ /* the next bin that will be added or removed. */ numtowrite = abs(*diffbinptr) - datawrote; /* FIXME: numtowrite+totwrote can wrap! */ if ((totwrote + numtowrite) > cmd->numout) numtowrite = cmd->numout - totwrote; if (numtowrite > numread) numtowrite = numread; chkfwrite(outdata, sizeof(float), numtowrite, outfile); datawrote += numtowrite; totwrote += numtowrite; numwritten += numtowrite; /* Update the statistics */ if (!padding) { for (ii = 0; ii < numtowrite; ii++) update_stats(statnum + ii, outdata[ii], &min, &max, &avg, &var); statnum += numtowrite; } if ((datawrote == abs(*diffbinptr)) && (numwritten != numread) && (totwrote < cmd->numout)) { /* Add/remove a bin */ float favg; int skip, nextdiffbin; skip = numtowrite; do { /* Write the rest of the data after adding/removing a bin */ if (*diffbinptr > 0) { /* Add a bin */ favg = (float) block_avg; chkfwrite(&favg, sizeof(float), 1, outfile); numadded++; totwrote++; } else { /* Remove a bin */ numremoved++; datawrote++; numwritten++; skip++; } diffbinptr++; /* Write the part after the diffbin */ numtowrite = numread - numwritten; if ((totwrote + numtowrite) > cmd->numout) numtowrite = cmd->numout - totwrote; nextdiffbin = abs(*diffbinptr) - datawrote; if (numtowrite > nextdiffbin) numtowrite = nextdiffbin; chkfwrite(outdata + skip, sizeof(float), numtowrite, outfile); numwritten += numtowrite; datawrote += numtowrite; totwrote += numtowrite; /* Update the statistics and counters */ if (!padding) { for (ii = 0; ii < numtowrite; ii++) update_stats(statnum + ii, outdata[skip + ii], &min, &max, &avg, &var); statnum += numtowrite; } skip += numtowrite; /* Stop if we have written out all the data we need to */ if (totwrote == cmd->numout) break; } while (numwritten < numread); } /* Stop if we have written out all the data we need to */ if (totwrote == cmd->numout) break; } while (numread); /* Free the arrays used in barycentering */ vect_free(bobsf); vect_free(btoa); vect_free(ttoa); } /* Calculate what the amount of padding we need */ if (cmd->numout > totwrote) padwrote = padtowrite = cmd->numout - totwrote; /* Write the new info file for the output data */ if (!cmd->nobaryP) { idata.bary = 1; idata.mjd_i = (int) floor(blotoa - (barydispdt / SECPERDAY)); idata.mjd_f = blotoa - (barydispdt / SECPERDAY) - idata.mjd_i; } if (cmd->downsamp > 1) idata.dt = dsdt; update_infodata(&idata, totwrote, padtowrite, diffbins, numdiffbins); writeinf(&idata); /* Set the padded points equal to the average data point */ if (idata.numonoff >= 1) { int jj, index, startpad, endpad; for (ii = 0; ii < worklen; ii++) outdata[ii] = avg; fclose(outfile); outfile = chkfopen(datafilenm, "rb+"); for (ii = 0; ii < idata.numonoff; ii++) { index = 2 * ii; startpad = idata.onoff[index + 1]; if (ii == idata.numonoff - 1) endpad = idata.N - 1; else endpad = idata.onoff[index + 2]; chkfseek(outfile, (startpad + 1) * sizeof(float), SEEK_SET); padtowrite = endpad - startpad; for (jj = 0; jj < padtowrite / worklen; jj++) chkfwrite(outdata, sizeof(float), worklen, outfile); chkfwrite(outdata, sizeof(float), padtowrite % worklen, outfile); } } vect_free(outdata); // Close all the raw files and free their vectors close_rawfiles(&s); /* Print simple stats and results */ var /= (datawrote - 1); /* Conver the '.dat' file to '.sdat' if requested */ if (cmd->shortsP) { FILE *infile; int safe_convert = 1, bufflen = 65536; char *sdatafilenm; float *fbuffer; short *sbuffer; offset = (int) (floor(avg)); if ((max - min) > (SHRT_MAX - SHRT_MIN)) { if ((max - min) < 1.5 * (SHRT_MAX - SHRT_MIN)) { printf("Warning: There is more dynamic range in the data\n" " than can be handled perfectly:\n" " max - min = %.2f - %.2f = %.2f\n" " Clipping the low values...\n\n", max, min, max - min); offset = max - SHRT_MAX; } else { printf("Error: There is way too much dynamic range in the data:\n" " max - min = %.2f - %.2f = %.2f\n" " Not converting to shorts.\n\n", max, min, max - min); safe_convert = 0; } } if (safe_convert) { fbuffer = gen_fvect(bufflen); sbuffer = gen_svect(bufflen); sdatafilenm = (char *) calloc(slen, 1); sprintf(sdatafilenm, "%s.sdat", cmd->outfile); printf("\n\nConverting floats in '%s' to shorts in '%s'.", datafilenm, sdatafilenm); fflush(NULL); infile = chkfopen(datafilenm, "rb"); outfile = chkfopen(sdatafilenm, "wb"); while ((numread = chkfread(fbuffer, sizeof(float), bufflen, infile))) { for (ii = 0; ii < numread; ii++) sbuffer[ii] = (short) (fbuffer[ii] + 1e-20 - offset); chkfwrite(sbuffer, sizeof(short), numread, outfile); } fclose(infile); fclose(outfile); remove(datafilenm); vect_free(fbuffer); vect_free(sbuffer); } } printf("\n\nDone.\n\nSimple statistics of the output data:\n"); printf(" Data points written: %ld\n", totwrote); if (padwrote) printf(" Padding points written: %ld\n", padwrote); if (!cmd->nobaryP) { if (numadded) printf(" Bins added for barycentering: %d\n", numadded); if (numremoved) printf(" Bins removed for barycentering: %d\n", numremoved); } printf(" Maximum value of data: %.2f\n", max); printf(" Minimum value of data: %.2f\n", min); printf(" Data average value: %.2f\n", avg); printf(" Data standard deviation: %.2f\n", sqrt(var)); if (cmd->shortsP && offset != 0) printf(" Offset applied to data: %d\n", -offset); printf("\n"); /* Cleanup */ if (cmd->maskfileP) { free_mask(obsmask); vect_free(maskchans); } vect_free(tobsf); vect_free(dispdt); vect_free(idispdt); free(outinfonm); free(datafilenm); if (!cmd->nobaryP) vect_free(diffbins); return (0); }
int main(int argc, char *argv[]) { prepfoldinfo search; Cmdline *cmd; plotflags flags; /* Call usage() if we have no command line arguments */ if (argc == 1) { Program = argv[0]; usage(); exit(0); } /* Parse the command line using the excellent program Clig */ cmd = parseCmdline(argc, argv); flags.events = cmd->eventsP; flags.scaleparts = cmd->scalepartsP; flags.justprofs = cmd->justprofsP; flags.allgrey = cmd->allgreyP; flags.nosearch = 1; /* * Read the raw prepfoldinfo structure */ read_prepfoldinfo(&search, argv[1]); /* * Print the main prepfoldinfo structure values */ print_prepfoldinfo(&search); if(cmd->infoonlyP) return 0; /* * Zap requested subbands or intervals */ { int *killparts, *killsubs, ii, jj, kk, index; int numkillparts = 0, numkillsubs = 0; if (cmd->killpartsstrP) { killparts = ranges_to_ivect(cmd->killpartsstr, 0, search.npart - 1, &numkillparts); for (ii = 0; ii < numkillparts; ii++) { if ((killparts[ii] >= 0) && (killparts[ii] < search.npart)) { index = killparts[ii] * search.proflen * search.nsub; for (jj = 0; jj < search.nsub; jj++) { search.stats[killparts[ii] * search.nsub + jj].prof_var = 0.0; search.stats[killparts[ii] * search.nsub + jj].prof_avg = 0.0; for (kk = 0; kk < search.proflen ; kk++) search.rawfolds[index + kk] = 0.0; index += search.proflen; } } } free(killparts); } if (cmd->killsubsstrP) { killsubs = ranges_to_ivect(cmd->killsubsstr, 0, search.nsub - 1, &numkillsubs); for (ii = 0; ii < numkillsubs; ii++) { if ((killsubs[ii] >= 0) && (killsubs[ii] < search.nsub)) { for (jj = 0; jj < search.npart; jj++) { index = search.proflen * (jj * search.nsub + killsubs[ii]); search.stats[jj * search.nsub + killsubs[ii]].prof_var = 0.0; search.stats[jj * search.nsub + killsubs[ii]].prof_avg = 0.0; for (kk = 0; kk < search.proflen ; kk++) search.rawfolds[index + kk] = 0.0; } } } free(killsubs); } } /* Switch to portrait mode */ if (cmd->portraitP) { int goodlen; char *substr, *tmpdev; substr = strstr(search.pgdev, "/CPS"); goodlen = substr - search.pgdev; *substr = '\0'; tmpdev = calloc(goodlen + 6, sizeof(char)); sprintf(tmpdev, "%s/VCPS", search.pgdev); free(search.pgdev); search.pgdev = calloc(goodlen + 6, sizeof(char)); strncpy(search.pgdev, tmpdev, strlen(tmpdev)); free(tmpdev); printf("New device is '%s'\n", search.pgdev); } if (0) { int goodlen; char *substr, *tmpdev; substr = strstr(search.pgdev, "ps/CPS"); goodlen = substr - search.pgdev; *substr = '\0'; tmpdev = calloc(goodlen + 9, sizeof(char)); strncpy(tmpdev, search.pgdev, goodlen); strcpy(tmpdev + goodlen, "png/TPNG"); free(search.pgdev); search.pgdev = calloc(goodlen + 9, sizeof(char)); strncpy(search.pgdev, tmpdev, strlen(tmpdev)); free(tmpdev); printf("New device is '%s'\n", search.pgdev); } /* * Plot our results */ prepfold_plot(&search, &flags, !cmd->noxwinP, NULL); /* Free our memory */ delete_prepfoldinfo(&search); return (0); }
int main(int argc, char *argv[]) { Cmdline *cmd; struct psrfits pfupper, pflower, pfo; fitsfile *infits, *outfits; char *pc1, *pc2; char outfilename[200]; //Name of outfile if not specified on command line int stat = 0, padding = 0, userN = 0, status; // Call usage() if we have no command line arguments if (argc == 1) { Program = argv[0]; usage(); exit(0); } // Parse the command line using the excellent program Clig cmd = parseCmdline(argc, argv); pfupper.tot_rows = pfupper.N = pfupper.T = pfupper.status = 0; //Initialize upper band pflower.tot_rows = pflower.N = pflower.T = pflower.status = 0; //Initialize lower band pfupper.filenum = pflower.filenum = 1; pfo.tot_rows = pfo.N = pfo.T = pfo.status = pfo.multifile = 0; //Initialize output sprintf(pfupper.filename, cmd->argv[0]); //Copy filename specified on command line to sprintf(pflower.filename, cmd->argv[0]); //upper and lower bands, will correct filenames shortly if ((pc2 = strstr(pfupper.filename, "s1")) != NULL) //Upper contains s1, change to s0 strncpy(pc2, "s0", 2); else if ((pc2 = strstr(pflower.filename, "s0")) != NULL) //Lower contains s0, change to s1 strncpy(pc2, "s1", 2); else { printf("Unable to determine which sideband is which\n"); exit(EXIT_FAILURE); } //Setting the name of the output file, setting as same name as input file, but removing s0/s1. pc1 = strstr(pflower.filename, "s1"); pc2 = strrchr(pflower.filename, '.'); //At '.fits' pc2--; while ((pc2 >= pflower.filename) && isdigit(*pc2)) //Move through the digits to the separation char. pc2--; strncpy(outfilename, pflower.filename, pc1 - pflower.filename); //Copy everything up to s1 into outfilename strncpy(outfilename + (pc1 - pflower.filename), pc1 + 2, pc2 - pc1 - 2); //Concatenate from after s1 to char before the separation char. pc1 = outfilename + (pc2 - pflower.filename - 2); *pc1 = 0; int rv = psrfits_open(&pfupper); //Open upper band if (rv) { fits_report_error(stderr, rv); exit(1); } rv = psrfits_open(&pflower); //Open lower band if (rv) { fits_report_error(stderr, rv); exit(1); } pfo = pflower; //Copy all lower band variables into the output struct if (!cmd->outputbasenameP) sprintf(pfo.basefilename, basename(outfilename)); else sprintf(pfo.basefilename, cmd->outputbasename); pfo.filenum = 0; sprintf(pfo.filename, "\0"); //Set filename to null so psrfits_open will create the filename for me pfo.rownum = 1; pfo.tot_rows = 0; pfo.N = 0; printf("lower rows_per_file=%d\n",pflower.rows_per_file); printf("upper rows_per_file=%d\n",pfupper.rows_per_file); if (pfupper.rows_per_file != pflower.rows_per_file) { //Sanity check for the two input frequency bands fprintf(stderr, "rows_per_file in input files do not match!\n"); exit(1); } double upperfreqoflower, nextfromlower, lowerfreqofupper, numchandiff; //Used to find which frequencies to take from each band double offsetfactor, scalefactor; //Factors which will be applied to offsets and scales int upchanskip, lowchanskip; //Number of channels to skip in each banda //Variables used to make code cleaner int extrachanoffset, outoffset, upperoffset, numtocopyupper, loweroffset_skip, loweroffset, numtocopylower, newuppernchan, newlowernchan; double df = pflower.hdr.df; int nchan = pflower.hdr.nchan; int outnchan; int npol = pflower.hdr.npol; int nbits = pflower.hdr.nbits; int nsblk = pflower.hdr.nsblk; //Allocate memory for all upper and lower data pflower.sub.dat_freqs = (double *) malloc(sizeof(double) * nchan); pflower.sub.dat_weights = (float *) malloc(sizeof(float) * nchan); pflower.sub.dat_offsets = (float *) malloc(sizeof(float) * nchan * npol); pflower.sub.dat_scales = (float *) malloc(sizeof(float) * nchan * npol); pflower.sub.rawdata = (unsigned char *) malloc(pflower.sub.bytes_per_subint); pflower.sub.data = (unsigned char *) malloc(pflower.sub.bytes_per_subint*2); pfupper.sub.dat_freqs = (double *) malloc(sizeof(double) * nchan); pfupper.sub.dat_weights = (float *) malloc(sizeof(float) * nchan); pfupper.sub.dat_offsets = (float *) malloc(sizeof(float) * nchan * npol); pfupper.sub.dat_scales = (float *) malloc(sizeof(float) * nchan * npol); pfupper.sub.rawdata = (unsigned char *) malloc(pfupper.sub.bytes_per_subint); pfupper.sub.data = (unsigned char *) malloc(pfupper.sub.bytes_per_subint*2); int firsttime = 1; //First time through do while loop do { print_percent_complete(pflower.rownum, pflower.rows_per_file, pflower.rownum == 1 ? 1 : 0); psrfits_read_subint(&pflower); psrfits_read_subint(&pfupper); if (firsttime) { //First time through loop, calculate factors for scales and offsets and number of channels to skip firsttime = 0; //Find the number of channels in the upper band which will be skipped if (df < 0) { //Find channel order, low to high or high to low upperfreqoflower = pflower.sub.dat_freqs[0]; //Highest frequency channel in lower band lowerfreqofupper = pfupper.sub.dat_freqs[nchan - 1]; //Lowest frequency channel in upper band } else { upperfreqoflower = pflower.sub.dat_freqs[nchan - 1]; //Highest frequency channel in lower band lowerfreqofupper = pfupper.sub.dat_freqs[0]; //Lowest frequency channel in upper band } nextfromlower = upperfreqoflower + fabs(df); //Second highest channel in lower band numchandiff = (nextfromlower - lowerfreqofupper) / fabs(df); //Number of channels to skip in float form int chanskip; if (numchandiff > 0) { //Make sure there are channels which need to be skipped if (numchandiff - (double) ((int) numchandiff) > .5) // See whether we need to round up integer channels to skip chanskip = (int) numchandiff + 1; else chanskip = (int) numchandiff; } else chanskip = 0; //No need to skip any channels if (chanskip % 2 == 1) { //Odd number of channels, give lower band the extra channel upchanskip = chanskip / 2; lowchanskip = chanskip / 2 + 1; } else //Even number of channels to skip upchanskip = lowchanskip = chanskip / 2; if (upchanskip % 2 == 1) { //We want an even number of channels in upper band for 4-bit data to get copied correctly ++lowchanskip; --upchanskip; } //Find new values given the number of channels skipped pfo.hdr.nchan = outnchan = nchan + nchan - chanskip + 2; //New number of channels, plus 2 to make nchan=960 (many factors of 2) pfo.hdr.BW = (double) outnchan *fabs(df); //New bandwidth pfo.hdr.fctr = //New center frequency (pflower.hdr.fctr - (double) (nchan / 2) * fabs(df)) + pfo.hdr.BW / 2.0; pfo.sub.bytes_per_subint = //Calculate new number of bytes in each subint outnchan * nsblk * nbits / 8 * npol; //Allocate space for output data now that we know the new number of channels pfo.sub.dat_freqs = (double *) malloc(sizeof(double) * outnchan); pfo.sub.dat_weights = (float *) malloc(sizeof(float) * outnchan); pfo.sub.dat_offsets = (float *) malloc(sizeof(float) * outnchan * npol); pfo.sub.dat_scales = (float *) malloc(sizeof(float) * outnchan * npol); pfo.sub.rawdata = (unsigned char *) malloc(pfo.sub.bytes_per_subint); pfo.sub.data = (unsigned char *) malloc(pfo.sub.bytes_per_subint*2); newuppernchan = nchan - upchanskip; //The number of channels to copy from the upper sideband. newlowernchan = nchan - lowchanskip; //The number of channels to copy from the lower sideband. extrachanoffset = 2; //Offset for 2 extra freq channels making nchan 960 in bytes outoffset = (outnchan * npol); //Offset in each loop due to previously written data upperoffset = (nchan * npol); //Offset in loop for upper band numtocopyupper = (newuppernchan * npol); //Number of bytes to copy from upper band loweroffset_skip = (lowchanskip * npol); //Number of bytes to skip when copying lower band due to //having written upper band loweroffset = //Number of bytes to skip due to having written previous lower band data (nchan * npol); numtocopylower = (newlowernchan * npol); //Number of bytes to copy from lower band float upmean, upvar, lowmean, lowvar; avg_var(pfupper.sub.dat_offsets + (nchan - upchanskip), //Find the mean and variance of the upper band's offsets upchanskip, &upmean, &upvar); printf("Upper offset stats: mean=%f variance=%f\n", upmean, upvar); avg_var(pflower.sub.dat_offsets, lowchanskip, &lowmean, &lowvar); //Find the mean and variance of the lower band's offsets printf("Lower offset stats: mean=%f variance=%f\n", lowmean, lowvar); printf("Applying factor of %f to upper offsets\n", (lowmean / upmean)); offsetfactor = lowmean / upmean; //Set offset factor used to correct variance differences in the two bands avg_var(pfupper.sub.dat_scales + (nchan - upchanskip), //Find the mean and var. of the upper band's scales upchanskip, &upmean, &upvar); printf("Upper scales stats: mean=%f variance=%f\n", upmean, upvar); avg_var(pflower.sub.dat_scales, lowchanskip, &lowmean, &lowvar); //Find the mean and var. of the lower band's scales printf("Lower scales stats: mean=%f variance=%f\n", lowmean, lowvar); printf("Applying factor of %f to upper scales\n", (lowmean / upmean)); scalefactor = lowmean / upmean; //Set scale factor used to correct variance differences in the two bands } if (pflower.status == 0 && pfupper.status == 0) { //Copy info from the lower band subint struct to the output file's subint struct pfo.sub.tsubint = pflower.sub.tsubint; pfo.sub.offs = pflower.sub.offs; pfo.sub.lst = pflower.sub.lst; pfo.sub.ra = pflower.sub.ra; pfo.sub.dec = pflower.sub.dec; pfo.sub.glon = pflower.sub.glon; pfo.sub.glat = pflower.sub.glat; pfo.sub.feed_ang = pflower.sub.feed_ang; pfo.sub.pos_ang = pflower.sub.pos_ang; pfo.sub.par_ang = pflower.sub.par_ang; pfo.sub.tel_az = pflower.sub.tel_az; pfo.sub.tel_zen = pflower.sub.tel_zen; pfo.sub.FITS_typecode = pflower.sub.FITS_typecode; //Create variables to reduce column width of lines below double *dat_freqs = pfo.sub.dat_freqs; double *udat_freqs = pfupper.sub.dat_freqs; double *ldat_freqs = pflower.sub.dat_freqs; float *dat_weights = pfo.sub.dat_weights; float *udat_weights = pfupper.sub.dat_weights; float *ldat_weights = pflower.sub.dat_weights; float *dat_offsets = pfo.sub.dat_offsets; float *udat_offsets = pfupper.sub.dat_offsets; float *ldat_offsets = pflower.sub.dat_offsets; float *dat_scales = pfo.sub.dat_scales; float *udat_scales = pfupper.sub.dat_scales; float *ldat_scales = pflower.sub.dat_scales; unsigned char *data = pfo.sub.data; unsigned char *udata = pfupper.sub.data; unsigned char *ldata = pflower.sub.data; if (df < 0) { //Copy frequency labels dat_freqs[1] = udat_freqs[0] + fabs(df); //Calculate the frequency labels dat_freqs[0] = dat_freqs[1] + fabs(df); //for our two empty frequency channels int newuppernchan = nchan - upchanskip; //The number of channels to copy from the upper band int newlowernchan = nchan - lowchanskip; //The number of channels to copy from the lower band memcpy(dat_freqs + 2, udat_freqs, sizeof(double) * newuppernchan); //Copy from the upper band, skipping first two chans. memcpy(dat_freqs + newuppernchan + 2, //Copy from the lower band ldat_freqs + lowchanskip, sizeof(double) * newlowernchan); //Copy weights dat_weights[0] = dat_weights[1] = 0; //Set the weights of first two channels to 0, so they shouldn't be used in calculations memcpy(dat_weights + 2, udat_weights, //Copy weights from the upper band sizeof(float) * newuppernchan); memcpy(dat_weights + 2 + newuppernchan, //Copy weights from the lower band ldat_weights + lowchanskip, sizeof(float) * newlowernchan); //Copy offsets dat_offsets[0] = dat_offsets[1] = //Set offsets of first two channels to the same as upper's first channel udat_offsets[0]; //(shouldn't matter since they should be ignored) int ii; for (ii = 0; ii < newuppernchan; ++ii) //Apply offset factor to upper band udat_offsets[ii] = udat_offsets[ii] * (offsetfactor); memcpy(dat_offsets + 2 * npol, udat_offsets, //Copy upper offsets sizeof(float) * newuppernchan * npol); memcpy(dat_offsets + (newuppernchan + 2) * npol, //Copy lower offsets ldat_offsets + lowchanskip, sizeof(float) * newlowernchan * npol); //Copy scales for (ii = 0; ii < newuppernchan; ++ii) //Apply scale factor to upper band udat_scales[ii] = udat_scales[ii] * (scalefactor); dat_scales[0] = dat_scales[1] = udat_scales[0]; memcpy(dat_scales + 2 * npol, udat_scales, //Copy upper scales sizeof(float) * newuppernchan * npol); memcpy(dat_scales + (newuppernchan + 2) * npol, //Copy lower scales ldat_scales + lowchanskip, sizeof(float) * newlowernchan * npol); //Copy the data for (ii = 0; ii < nsblk; ++ii) { //Loop through data copying into place memcpy(data + ii * outoffset + extrachanoffset, udata + ii * upperoffset, numtocopyupper); memcpy(data + ii * outoffset + extrachanoffset + numtocopyupper, ldata + ii * loweroffset + loweroffset_skip, numtocopylower); } psrfits_write_subint(&pfo); } else { } } } while (pfo.rownum <= pfo.rows_per_file && pfupper.status==0 && pflower.status==0); printf("Closing file '%s'\n", pflower.filename); fits_close_file(pfupper.fptr, &status); printf("Closing file '%s'\n", pfupper.filename); fits_close_file(pflower.fptr, &status); if(pflower.status!=0||pfupper.status!=0) { fprintf(stderr,"An error occurred when combining the two Mock files!\n"); if(pflower.status==108||pfupper.status==108) fprintf(stderr,"One or both of the files is incomplete.\n"); exit(1); } exit(0); }
int main(int argc, char *argv[]) { int ii, jj, numbirds; double lofreq, hifreq; char *rootfilenm; birdie *newbird; GSList *zapped = NULL; infodata idata; Cmdline *cmd; /* Call usage() if we have no command line arguments */ if (argc == 1) { Program = argv[0]; printf("\n"); usage(); exit(1); } /* Parse the command line using the excellent program Clig */ cmd = parseCmdline(argc, argv); #ifdef DEBUG showOptionValues(); #endif printf("\n\n"); printf(" Interactive/Automatic Birdie Zapping Program\n"); printf(" by Scott M. Ransom\n"); printf(" January, 2001\n\n"); if (!cmd->zapP && !cmd->inzapfileP && !cmd->outzapfileP) { printf("You must specify '-in' and '-out' if you are not\n"); printf("automatically zapping a file (with '-zap').\n\n"); exit(0); } { int hassuffix = 0; char *suffix; hassuffix = split_root_suffix(cmd->argv[0], &rootfilenm, &suffix); if (hassuffix) { if (strcmp(suffix, "fft") != 0) { printf("\nInput file ('%s') must be a FFT file ('.fft')!\n\n", cmd->argv[0]); free(suffix); exit(0); } free(suffix); } else { printf("\nInput file ('%s') must be a FFT file ('.fft')!\n\n", cmd->argv[0]); exit(0); } } /* Read the info file */ readinf(&idata, rootfilenm); if (idata.object) { printf("Examining %s data from '%s'.\n\n", remove_whitespace(idata.object), cmd->argv[0]); } else { printf("Examining data from '%s'.\n\n", cmd->argv[0]); } T = idata.dt * idata.N; dr = 1.0 / NUMBETWEEN; if (cmd->zapP) { /* Automatic */ double *bird_lobins, *bird_hibins, hibin; if (!cmd->zapfileP) { printf("You must specify a 'zapfile' containing freqs\n"); printf("and widths if you want to write to the FFT file.\n\n"); free(rootfilenm); exit(0); } hibin = idata.N / 2; /* Read the Standard bird list */ numbirds = get_birdies(cmd->zapfile, T, cmd->baryv, &bird_lobins, &bird_hibins); /* Zap the birdies */ fftfile = chkfopen(cmd->argv[0], "rb+"); for (ii = 0; ii < numbirds; ii++) { if (bird_lobins[ii] >= hibin) break; if (bird_hibins[ii] >= hibin) bird_hibins[ii] = hibin - 1; zapbirds(bird_lobins[ii], bird_hibins[ii], fftfile, NULL); } vect_free(bird_lobins); vect_free(bird_hibins); } else { /* Interactive */ int *bird_numharms; double *bird_basebins; /* Read the Standard bird list */ numbirds = get_std_birds(cmd->inzapfile, T, cmd->baryv, &bird_basebins, &bird_numharms); /* Create our correlation kernel */ { int numkern; fcomplex *resp; khw = r_resp_halfwidth(LOWACC); numkern = 2 * NUMBETWEEN * khw; resp = gen_r_response(0.0, NUMBETWEEN, numkern); kernel = gen_cvect(FFTLEN); place_complex_kernel(resp, numkern, kernel, FFTLEN); COMPLEXFFT(kernel, FFTLEN, -1); vect_free(resp); } /* Loop over the birdies */ fftfile = chkfopen(cmd->argv[0], "rb"); cpgstart_x("landscape"); cpgask(0); for (ii = 0; ii < numbirds; ii++) { for (jj = 0; jj < bird_numharms[ii]; jj++) { process_bird(bird_basebins[ii], jj + 1, &lofreq, &hifreq); if (lofreq && hifreq) { newbird = birdie_create(lofreq, hifreq, cmd->baryv); zapped = g_slist_insert_sorted(zapped, newbird, birdie_compare); } } } cpgclos(); /* Output the birdies */ { FILE *outfile; outfile = chkfopen(cmd->outzapfile, "w"); fprintf(outfile, "#\n"); fprintf(outfile, "# Topocentric birdies found using 'zapbirds' for '%s'\n", cmd->argv[0]); fprintf(outfile, "#\n"); fprintf(outfile, "# Frequency (Hz) Width (Hz)\n"); fprintf(outfile, "#\n"); g_slist_foreach(zapped, birdie_print, outfile); fclose(outfile); } printf("\nOutput birdie file is '%s'.\n\n", cmd->outzapfile); /* Free the memory */ g_slist_foreach(zapped, birdie_free, NULL); g_slist_free(zapped); vect_free(kernel); vect_free(bird_numharms); vect_free(bird_basebins); } fclose(fftfile); free(rootfilenm); printf("Done\n\n"); return 0; }
int main(int argc, char *argv[]) { FILE *fftfile, *candfile; float powargr, powargi, *powers = NULL, *minifft; float norm, numchunks, *powers_pos; int nbins, newncand, nfftsizes, fftlen, halffftlen, binsleft; int numtoread, filepos = 0, loopct = 0, powers_offset, ncand2; int ii, ct, newper = 0, oldper = 0, numsumpow = 1; double T, totnumsearched = 0.0, minsig = 0.0, min_orb_p, max_orb_p; char *rootfilenm, *notes; fcomplex *data = NULL; rawbincand tmplist[MININCANDS], *list; infodata idata; struct tms runtimes; double ttim, utim, stim, tott; Cmdline *cmd; fftwf_plan fftplan; /* Prep the timer */ tott = times(&runtimes) / (double) CLK_TCK; /* Call usage() if we have no command line arguments */ if (argc == 1) { Program = argv[0]; printf("\n"); usage(); exit(1); } /* Parse the command line using the excellent program Clig */ cmd = parseCmdline(argc, argv); #ifdef DEBUG showOptionValues(); #endif printf("\n\n"); printf(" Phase Modulation Pulsar Search Routine\n"); printf(" by Scott M. Ransom\n\n"); { int hassuffix = 0; char *suffix; hassuffix = split_root_suffix(cmd->argv[0], &rootfilenm, &suffix); if (hassuffix) { if (strcmp(suffix, "fft") != 0) { printf("\nInput file ('%s') must be a FFT file ('.fft')!\n\n", cmd->argv[0]); free(suffix); exit(0); } free(suffix); } else { printf("\nInput file ('%s') must be a FFT file ('.fft')!\n\n", cmd->argv[0]); exit(0); } } /* Read the info file */ readinf(&idata, rootfilenm); T = idata.N * idata.dt; if (strlen(remove_whitespace(idata.object)) > 0) { printf("Analyzing '%s' data from '%s'.\n\n", remove_whitespace(idata.object), cmd->argv[0]); } else { printf("Analyzing data from '%s'.\n\n", cmd->argv[0]); } min_orb_p = MINORBP; if (cmd->noaliasP) max_orb_p = T / 2.0; else max_orb_p = T / 1.2; /* open the FFT file and get its length */ fftfile = chkfopen(cmd->argv[0], "rb"); nbins = chkfilelen(fftfile, sizeof(fcomplex)); /* Check that cmd->maxfft is an acceptable power of 2 */ ct = 4; ii = 1; while (ct < MAXREALFFT || ii) { if (ct == cmd->maxfft) ii = 0; ct <<= 1; } if (ii) { printf("\n'maxfft' is out of range or not a power-of-2.\n\n"); exit(1); } /* Check that cmd->minfft is an acceptable power of 2 */ ct = 4; ii = 1; while (ct < MAXREALFFT || ii) { if (ct == cmd->minfft) ii = 0; ct <<= 1; } if (ii) { printf("\n'minfft' is out of range or not a power-of-2.\n\n"); exit(1); } /* Low and high Fourier freqs to check */ if (cmd->floP) { cmd->rlo = floor(cmd->flo * T); if (cmd->rlo < cmd->lobin) cmd->rlo = cmd->lobin; if (cmd->rlo > cmd->lobin + nbins - 1) { printf("\nLow frequency to search 'flo' is greater than\n"); printf(" the highest available frequency. Exiting.\n\n"); exit(1); } } else { cmd->rlo = 1.0; if (cmd->rlo < cmd->lobin) cmd->rlo = cmd->lobin; if (cmd->rlo > cmd->lobin + nbins - 1) { printf("\nLow frequency to search 'rlo' is greater than\n"); printf(" the available number of points. Exiting.\n\n"); exit(1); } } if (cmd->fhiP) { cmd->rhi = ceil(cmd->fhi * T); if (cmd->rhi > cmd->lobin + nbins - 1) cmd->rhi = cmd->lobin + nbins - 1; if (cmd->rhi < cmd->rlo) { printf("\nHigh frequency to search 'fhi' is less than\n"); printf(" the lowest frequency to search 'flo'. Exiting.\n\n"); exit(1); } } else if (cmd->rhiP) { if (cmd->rhi > cmd->lobin + nbins - 1) cmd->rhi = cmd->lobin + nbins - 1; if (cmd->rhi < cmd->rlo) { printf("\nHigh frequency to search 'rhi' is less than\n"); printf(" the lowest frequency to search 'rlo'. Exiting.\n\n"); exit(1); } } /* Determine how many different mini-fft sizes we will use */ nfftsizes = 1; ii = cmd->maxfft; while (ii > cmd->minfft) { ii >>= 1; nfftsizes++; } /* Allocate some memory and prep some variables. */ /* For numtoread, the 6 just lets us read extra data at once */ numtoread = 6 * cmd->maxfft; if (cmd->stack == 0) powers = gen_fvect(numtoread); minifft = (float *) fftwf_malloc(sizeof(float) * (cmd->maxfft * cmd->numbetween + 2)); ncand2 = 2 * cmd->ncand; list = (rawbincand *) malloc(sizeof(rawbincand) * ncand2); for (ii = 0; ii < ncand2; ii++) list[ii].mini_sigma = 0.0; for (ii = 0; ii < MININCANDS; ii++) tmplist[ii].mini_sigma = 0.0; filepos = cmd->rlo - cmd->lobin; numchunks = (float) (cmd->rhi - cmd->rlo) / numtoread; printf("Searching...\n"); printf(" Amount complete = %3d%%", 0); fflush(stdout); /* Prep FFTW */ read_wisdom(); /* Loop through fftfile */ while ((filepos + cmd->lobin) < cmd->rhi) { /* Calculate percentage complete */ newper = (int) (loopct / numchunks * 100.0); if (newper > oldper) { newper = (newper > 99) ? 100 : newper; printf("\r Amount complete = %3d%%", newper); oldper = newper; fflush(stdout); } /* Adjust our search parameters if close to end of zone to search */ binsleft = cmd->rhi - (filepos + cmd->lobin); if (binsleft < cmd->minfft) break; if (binsleft < numtoread) { /* Change numtoread */ numtoread = cmd->maxfft; while (binsleft < numtoread) { cmd->maxfft /= 2; numtoread = cmd->maxfft; } } fftlen = cmd->maxfft; /* Read from fftfile */ if (cmd->stack == 0) { data = read_fcomplex_file(fftfile, filepos, numtoread); for (ii = 0; ii < numtoread; ii++) powers[ii] = POWER(data[ii].r, data[ii].i); numsumpow = 1; } else { powers = read_float_file(fftfile, filepos, numtoread); numsumpow = cmd->stack; } if (filepos == 0) powers[0] = 1.0; /* Chop the powers that are way above the median level */ prune_powers(powers, numtoread, numsumpow); /* Loop through the different small FFT sizes */ while (fftlen >= cmd->minfft) { halffftlen = fftlen / 2; powers_pos = powers; powers_offset = 0; /* Create the appropriate FFT plan */ fftplan = fftwf_plan_dft_r2c_1d(cmd->interbinP ? fftlen : 2 * fftlen, minifft, (fftwf_complex *) minifft, FFTW_PATIENT); /* Perform miniffts at each section of the powers array */ while ((numtoread - powers_offset) > (int) ((1.0 - cmd->overlap) * cmd->maxfft + DBLCORRECT)) { /* Copy the proper amount and portion of powers into minifft */ memcpy(minifft, powers_pos, fftlen * sizeof(float)); /* For Fourier interpolation use a zeropadded FFT */ if (cmd->numbetween > 1 && !cmd->interbinP) { for (ii = fftlen; ii < cmd->numbetween * fftlen; ii++) minifft[ii] = 0.0; } /* Perform the minifft */ fftwf_execute(fftplan); /* Normalize and search the miniFFT */ norm = sqrt(fftlen * numsumpow) / minifft[0]; for (ii = 0; ii < (cmd->interbinP ? fftlen + 1 : 2 * fftlen + 1); ii++) minifft[ii] *= norm; search_minifft((fcomplex *) minifft, halffftlen, min_orb_p, max_orb_p, tmplist, MININCANDS, cmd->harmsum, cmd->numbetween, idata.N, T, (double) (powers_offset + filepos + cmd->lobin), cmd->interbinP ? INTERBIN : INTERPOLATE, cmd->noaliasP ? NO_CHECK_ALIASED : CHECK_ALIASED); /* Check if the new cands should go into the master cand list */ for (ii = 0; ii < MININCANDS; ii++) { if (tmplist[ii].mini_sigma > minsig) { /* Check to see if another candidate with these properties */ /* is already in the list. */ if (not_already_there_rawbin(tmplist[ii], list, ncand2)) { list[ncand2 - 1] = tmplist[ii]; minsig = percolate_rawbincands(list, ncand2); } } else { break; } /* Mini-fft search for loop */ } totnumsearched += fftlen; powers_pos += (int) (cmd->overlap * fftlen); powers_offset = powers_pos - powers; /* Position of mini-fft in data set while loop */ } fftwf_destroy_plan(fftplan); fftlen >>= 1; /* Size of mini-fft while loop */ } if (cmd->stack == 0) vect_free(data); else vect_free(powers); filepos += (numtoread - (int) ((1.0 - cmd->overlap) * cmd->maxfft)); loopct++; /* File position while loop */ } /* Print the final percentage update */ printf("\r Amount complete = %3d%%\n\n", 100); /* Print the number of frequencies searched */ printf("Searched %.0f pts (including interbins).\n\n", totnumsearched); printf("Timing summary:\n"); tott = times(&runtimes) / (double) CLK_TCK - tott; utim = runtimes.tms_utime / (double) CLK_TCK; stim = runtimes.tms_stime / (double) CLK_TCK; ttim = utim + stim; printf(" CPU time: %.3f sec (User: %.3f sec, System: %.3f sec)\n", ttim, utim, stim); printf(" Total time: %.3f sec\n\n", tott); printf("Writing result files and cleaning up.\n"); /* Count how many candidates we actually have */ ii = 0; while (ii < ncand2 && list[ii].mini_sigma != 0) ii++; newncand = (ii > cmd->ncand) ? cmd->ncand : ii; /* Set our candidate notes to all spaces */ notes = malloc(sizeof(char) * newncand * 18 + 1); for (ii = 0; ii < newncand; ii++) strncpy(notes + ii * 18, " ", 18); /* Check the database for possible known PSR detections */ if (idata.ra_h && idata.dec_d) { for (ii = 0; ii < newncand; ii++) { comp_rawbin_to_cand(&list[ii], &idata, notes + ii * 18, 0); } } /* Compare the candidates with each other */ compare_rawbin_cands(list, newncand, notes); /* Send the candidates to the text file */ file_rawbin_candidates(list, notes, newncand, cmd->harmsum, rootfilenm); /* Write the binary candidate file */ { char *candnm; candnm = (char *) calloc(strlen(rootfilenm) + 15, sizeof(char)); sprintf(candnm, "%s_bin%d.cand", rootfilenm, cmd->harmsum); candfile = chkfopen(candnm, "wb"); chkfwrite(list, sizeof(rawbincand), (unsigned long) newncand, candfile); fclose(candfile); free(candnm); } /* Free our arrays and close our files */ if (cmd->stack == 0) vect_free(powers); free(list); fftwf_free(minifft); free(notes); free(rootfilenm); fclose(fftfile); printf("Done.\n\n"); return (0); }
int main(int argc, char *argv[]) { FILE **infiles, *outfile = NULL, *scalingfile, *zerolagfile = NULL; int filenum, ptsperblock, numlags, numfiles, outnumlags; int bytes_per_read, scaling = 0, numscalings, firstfile, firstspec; int write_data = 1, update_posn = 0; long long N, numconverted = 0, numwritten = 0; char *path, *filenm, rawlags[4096]; char outfilenm[200], filenmbase[200], zerolagnm[200], scalingnm[200]; unsigned char output_samples[2048]; float *scalings = NULL, lags[2048], tmp_floats[2048]; double dt, T, time_offset; SPIGOT_INFO *spigots; sigprocfb fb; infodata idata; Cmdline *cmd; /* Call usage() if we have no command line arguments */ if (argc == 1) { Program = argv[0]; usage(); exit(0); } /* Parse the command line using the excellent program Clig */ cmd = parseCmdline(argc, argv); // showOptionValues(); numfiles = cmd->argc; /* If requesting that we skip values or only write */ /* a specific number of values, require an output file name */ if ((cmd->skip > 0) || cmd->numoutP){ if (!cmd->outfileP && !cmd->stdoutP) { fprintf(stderr, "\nspigot2filterbank ERROR: You need to specify an output\n" " filename (or -stdout) when using the -skip and/or \n" " -numout options!\n\n"); exit(1); } } /* Give an error if specifying stdout and an output file */ if (cmd->stdoutP && cmd->outfileP){ fprintf(stderr, "\nspigot2filterbank ERROR: You cannot specify both an output\n" " file and that the data should go to STDOUT!\n\n"); exit(1); } if (!cmd->stdoutP) printf("\nConverting SPIGOT FITs lags into SIGPROC filterbank format...\n\n"); /* Determine the filename base from the first spigot file */ split_path_file(cmd->argv[0], &path, &filenm); strncpy(filenmbase, filenm, strlen(filenm) - 5); filenmbase[strlen(filenm) - 5] = '\0'; free(path); free(filenm); /* Open a file to store the zerolags */ if (cmd->zerolagsP) { if (cmd->outfileP) { sprintf(zerolagnm, "%s.zerolags", cmd->outfile); } else { sprintf(zerolagnm, "%s.zerolags", filenmbase); } zerolagfile = chkfopen(zerolagnm, "wb"); } /* Attempt to read a file with lag scalings in it */ sprintf(scalingnm, "%s.scaling", filenmbase); if ((scalingfile = fopen(scalingnm, "rb"))) { /* Determine the length of the file */ numscalings = (int) chkfilelen(scalingfile, sizeof(float)); /* Create the array and read 'em */ scalings = gen_fvect(numscalings); chkfread(scalings, sizeof(float), numscalings, scalingfile); scaling = 1; /* close the scaling file */ fclose(scalingfile); if (!cmd->stdoutP) printf("Scaling the lags with the %d values found in '%s'\n\n", numscalings, scalingnm); } /* Read and convert the basic SPIGOT file information */ if (!cmd->stdoutP) printf("Spigot card input file information:\n"); spigots = (SPIGOT_INFO *) malloc(sizeof(SPIGOT_INFO) * numfiles); infiles = (FILE **) malloc(sizeof(FILE *) * numfiles); for (filenum = 0; filenum < numfiles; filenum++) { if (!cmd->stdoutP) printf(" '%s'\n", cmd->argv[filenum]); infiles[filenum] = chkfopen(cmd->argv[filenum], "rb"); read_SPIGOT_header(cmd->argv[filenum], spigots + filenum); rewind(infiles[filenum]); } if (!cmd->stdoutP) printf("\n"); /* The following is necessary in order to initialize all the */ /* static variables in spigot.c */ get_SPIGOT_file_info(infiles, spigots, numfiles, 0, 0, &N, &ptsperblock, &numlags, &dt, &T, &idata, !cmd->stdoutP); /* Compute the first file required */ firstfile = cmd->skip / spigots[0].samples_per_file; firstspec = cmd->skip % spigots[0].samples_per_file; if (!cmd->numoutP) { cmd->numout = (N - cmd->skip) / cmd->downsamp; } bytes_per_read = numlags * spigots[0].bits_per_lag / 8; outnumlags = numlags - cmd->lokill - cmd->hikill; /* Step through the SPIGOT files */ filenum = firstfile; while (numwritten < cmd->numout){ split_path_file(cmd->argv[filenum], &path, &filenm); strncpy(filenmbase, filenm, strlen(filenm) - 5); filenmbase[strlen(filenm) - 5] = '\0'; if (cmd->outfileP) { if (filenum==firstfile) { sprintf(outfilenm, "%s", cmd->outfile); outfile = chkfopen(outfilenm, "wb"); } } else { sprintf(outfilenm, "%s.fil", filenmbase); if (cmd->stdoutP) { outfile = stdout; } else { outfile = chkfopen(outfilenm, "wb"); } } if (!cmd->stdoutP) { if (filenum==firstfile) printf("Reading Spigot lags from '%s' (starting at sample %d)\n", cmd->argv[filenum], firstspec); else printf("Reading Spigot lags from '%s'\n", cmd->argv[filenum]); printf("Writing filterbank spectra to '%s'\n\n", outfilenm); } /* Update the filterbank header information for each file */ if (!spigots[filenum].tracking) { if (cmd->outfileP || cmd->stdoutP) { time_offset = 0.5 * cmd->numout * \ spigots[filenum].dt_us * 1e-6 * cmd->downsamp; } else { // Just normal files time_offset = 0.5 * spigots[filenum].file_duration; } update_posn = 1; } else { update_posn = 0; time_offset = 0.0; } /* Adjust the Spigot start time for the skip */ spigots[filenum].elapsed_time += cmd->skip * spigots[filenum].dt_us * 1e-6; /* Determine the SIGPROC header */ spigot2sigprocfb(&(spigots[filenum]), &fb, filenmbase, cmd->lokill, cmd->hikill, cmd->downsamp, update_posn, time_offset); /* Correct the structure if we are using floats */ if (cmd->floatsP) fb.nbits = 32; /* Write a filterbank header if we have not been told not to. */ /* Don't write it, though, if using stdio or a specified output */ /* file and the input file is not the first. */ if (!cmd->nohdrP) { if ((!cmd->stdoutP && !cmd->outfileP) || ((cmd->stdoutP || cmd->outfileP) && filenum==firstfile)) { write_filterbank_header(&fb, outfile); } } /* Go to the correct autocorrelation in the correct first FITs file */ chkfseek(infiles[filenum], spigots[filenum].header_len + bytes_per_read*firstspec, SEEK_SET); /* Loop over the samples in the file */ while ((numwritten < cmd->numout) && (chkfread(rawlags, bytes_per_read, 1, infiles[filenum]))) { if (cmd->zerolagsP) { /* Correct the lags so we can write the zerolag */ get_calibrated_lags(rawlags, lags); chkfwrite(lags, sizeof(float), 1, zerolagfile); } if (scaling) { convert_SPIGOT_point(rawlags, output_samples, SUMIFS, scalings[cmd->skip+numconverted]); } else { convert_SPIGOT_point(rawlags, output_samples, SUMIFS, 1.0); } /* If downsampling, average the current spectra */ if (cmd->downsamp > 1) { int ii; if (numconverted % cmd->downsamp == 0) { write_data = 0; /* Zero the array used for averaging */ for (ii = 0; ii < outnumlags; ii++) tmp_floats[ii] = 0.0; } else { /* Add the current data to the array used for averaging */ for (ii = 0; ii < outnumlags; ii++) tmp_floats[ii] += (float) output_samples[ii+cmd->lokill]; /* If that was the last sample to be added, average them */ /* and put them back into output_samples */ if (numconverted % cmd->downsamp == (cmd->downsamp-1)) { write_data = 1; for (ii = 0; ii < outnumlags; ii++) { tmp_floats[ii] /= (float) cmd->downsamp; output_samples[ii+cmd->lokill] = \ (unsigned char)(tmp_floats[ii]+0.5); } } } } numconverted++; if (write_data) { int ii; /* Invert the band so that the high freqs are first */ /* This is how SIGPROC stores its data. */ if (cmd->floatsP) { float tempzz = 0.0, *loptr, *hiptr; loptr = tmp_floats; // killed channels are already gone hiptr = tmp_floats + outnumlags - 1; for (ii = 0; ii < outnumlags / 2; ii++, loptr++, hiptr--) { SWAP(*loptr, *hiptr); } } else { unsigned char tempzz = 0.0, *loptr, *hiptr; loptr = output_samples + cmd->lokill; hiptr = output_samples + cmd->lokill + outnumlags - 1; for (ii = 0; ii < outnumlags / 2; ii++, loptr++, hiptr--) { SWAP(*loptr, *hiptr); } } /* Now actually write the data */ if (cmd->floatsP) { /* Copy the bytes to floats */ for (ii = 0; ii < outnumlags; ii++) tmp_floats[ii] = (float) output_samples[ii+cmd->lokill]; chkfwrite(tmp_floats, sizeof(float), fb.nchans, outfile); } else { chkfwrite(output_samples+cmd->lokill, sizeof(unsigned char), fb.nchans, outfile); } numwritten++; } } if ((!cmd->stdoutP) && (!cmd->outfileP)) fclose(outfile); fclose(infiles[filenum]); firstspec = 0; filenum++; free(path); free(filenm); } if ((!cmd->stdoutP) && (cmd->outfileP)) fclose(outfile); if (cmd->zerolagsP) fclose(zerolagfile); if (!cmd->stdoutP) fprintf(stderr, "Converted %lld samples and wrote %lld.\n\n", numconverted, numwritten); if (scaling) free(scalings); free(spigots); free(infiles); return 0; }
int main(int argc, char *argv[]) { int ii; double ttim, utim, stim, tott; struct tms runtimes; subharminfo **subharminfs; accelobs obs; infodata idata; GSList *cands = NULL; Cmdline *cmd; /* Prep the timer */ tott = times(&runtimes) / (double) CLK_TCK; /* Call usage() if we have no command line arguments */ if (argc == 1) { Program = argv[0]; printf("\n"); usage(); exit(1); } /* Parse the command line using the excellent program Clig */ cmd = parseCmdline(argc, argv); #ifdef DEBUG showOptionValues(); #endif printf("\n\n"); printf(" Fourier-Domain Acceleration Search Routine\n"); printf(" by Scott M. Ransom\n\n"); /* Create the accelobs structure */ create_accelobs(&obs, &idata, cmd, 1); /* Zap birdies if requested and if in memory */ if (cmd->zaplistP && !obs.mmap_file && obs.fft) { int numbirds; double *bird_lobins, *bird_hibins, hibin; /* Read the Standard bird list */ numbirds = get_birdies(cmd->zaplist, obs.T, cmd->baryv, &bird_lobins, &bird_hibins); /* Zap the birdies */ printf("Zapping them using a barycentric velocity of %.5gc.\n\n", cmd->baryv); hibin = obs.N / 2; for (ii = 0; ii < numbirds; ii++) { if (bird_lobins[ii] >= hibin) break; if (bird_hibins[ii] >= hibin) bird_hibins[ii] = hibin - 1; zapbirds(bird_lobins[ii], bird_hibins[ii], NULL, obs.fft); } free(bird_lobins); free(bird_hibins); } printf("Searching with up to %d harmonics summed:\n", 1 << (obs.numharmstages - 1)); printf(" f = %.1f to %.1f Hz\n", obs.rlo / obs.T, obs.rhi / obs.T); printf(" r = %.1f to %.1f Fourier bins\n", obs.rlo, obs.rhi); printf(" z = %.1f to %.1f Fourier bins drifted\n\n", obs.zlo, obs.zhi); /* Generate the correlation kernels */ printf("Generating correlation kernels:\n"); subharminfs = create_subharminfos(obs.numharmstages, (int) obs.zhi); printf("Done generating kernels.\n\n"); printf("Starting the search.\n"); /* Don't use the *.txtcand files on short in-memory searches */ if (!obs.dat_input) { printf(" Working candidates in a test format are in '%s'.\n\n", obs.workfilenm); } /* Start the main search loop */ { double startr = obs.rlo, lastr = 0, nextr = 0; ffdotpows *fundamental; while (startr + ACCEL_USELEN * ACCEL_DR < obs.highestbin) { /* Search the fundamental */ print_percent_complete(startr - obs.rlo, obs.highestbin - obs.rlo, "search", 0); nextr = startr + ACCEL_USELEN * ACCEL_DR; lastr = nextr - ACCEL_DR; fundamental = subharm_ffdot_plane(1, 1, startr, lastr, &subharminfs[0][0], &obs); cands = search_ffdotpows(fundamental, 1, &obs, cands); if (obs.numharmstages > 1) { /* Search the subharmonics */ int stage, harmtosum, harm; ffdotpows *subharmonic; for (stage = 1; stage < obs.numharmstages; stage++) { harmtosum = 1 << stage; for (harm = 1; harm < harmtosum; harm += 2) { subharmonic = subharm_ffdot_plane(harmtosum, harm, startr, lastr, &subharminfs[stage][harm - 1], &obs); add_ffdotpows(fundamental, subharmonic, harmtosum, harm); free_ffdotpows(subharmonic); } cands = search_ffdotpows(fundamental, harmtosum, &obs, cands); } } free_ffdotpows(fundamental); startr = nextr; } print_percent_complete(obs.highestbin - obs.rlo, obs.highestbin - obs.rlo, "search", 0); } printf("\n\nDone searching. Now optimizing each candidate.\n\n"); free_subharminfos(obs.numharmstages, subharminfs); { /* Candidate list trimming and optimization */ int numcands; GSList *listptr; accelcand *cand; fourierprops *props; numcands = g_slist_length(cands); if (numcands) { /* Sort the candidates according to the optimized sigmas */ cands = sort_accelcands(cands); /* Eliminate (most of) the harmonically related candidates */ if ((cmd->numharm > 1) && !(cmd->noharmremoveP)) eliminate_harmonics(cands, &numcands); /* Now optimize each candidate and its harmonics */ print_percent_complete(0, 0, NULL, 1); listptr = cands; for (ii = 0; ii < numcands; ii++) { print_percent_complete(ii, numcands, "optimization", 0); cand = (accelcand *) (listptr->data); optimize_accelcand(cand, &obs); listptr = listptr->next; } print_percent_complete(ii, numcands, "optimization", 0); /* Calculate the properties of the fundamentals */ props = (fourierprops *) malloc(sizeof(fourierprops) * numcands); listptr = cands; for (ii = 0; ii < numcands; ii++) { cand = (accelcand *) (listptr->data); /* In case the fundamental harmonic is not significant, */ /* send the originally determined r and z from the */ /* harmonic sum in the search. Note that the derivs are */ /* not used for the computations with the fundamental. */ calc_props(cand->derivs[0], cand->r, cand->z, 0.0, props + ii); /* Override the error estimates based on power */ props[ii].rerr = (float) (ACCEL_DR) / cand->numharm; props[ii].zerr = (float) (ACCEL_DZ) / cand->numharm; listptr = listptr->next; } /* Write the fundamentals to the output text file */ output_fundamentals(props, cands, &obs, &idata); /* Write the harmonics to the output text file */ output_harmonics(cands, &obs, &idata); /* Write the fundamental fourierprops to the cand file */ obs.workfile = chkfopen(obs.candnm, "wb"); chkfwrite(props, sizeof(fourierprops), numcands, obs.workfile); fclose(obs.workfile); free(props); printf("\n\n"); } else { printf("No candidates above sigma = %.2f were found.\n\n", obs.sigma); } } /* Finish up */ printf("Searched the following approx numbers of independent points:\n"); printf(" %d harmonic: %9lld\n", 1, obs.numindep[0]); for (ii = 1; ii < obs.numharmstages; ii++) printf(" %d harmonics: %9lld\n", 1 << ii, obs.numindep[ii]); printf("\nTiming summary:\n"); tott = times(&runtimes) / (double) CLK_TCK - tott; utim = runtimes.tms_utime / (double) CLK_TCK; stim = runtimes.tms_stime / (double) CLK_TCK; ttim = utim + stim; printf(" CPU time: %.3f sec (User: %.3f sec, System: %.3f sec)\n", ttim, utim, stim); printf(" Total time: %.3f sec\n\n", tott); printf("Final candidates in binary format are in '%s'.\n", obs.candnm); printf("Final Candidates in a text format are in '%s'.\n\n", obs.accelnm); free_accelobs(&obs); g_slist_foreach(cands, free_accelcand, NULL); g_slist_free(cands); return (0); }
int main(int argc, char *argv[]) { FILE *infile, *outfile; int ii, jj, bufflen = 10000, numread; long long N = 0; float *inbuffer = NULL, *outbuffer = NULL; short useshorts = 0, *sinbuffer = NULL, *soutbuffer = NULL; char *rootfilenm, *outname; infodata idata; Cmdline *cmd; /* Call usage() if we have no command line arguments */ if (argc == 1) { Program = argv[0]; printf("\n"); usage(); exit(1); } /* Parse the command line using the excellent program Clig */ cmd = parseCmdline(argc, argv); #ifdef DEBUG showOptionValues(); #endif printf("\n\n"); printf(" Time Series Downsampling Routine\n"); printf(" Sept, 2002\n\n"); { int hassuffix = 0; char *suffix; hassuffix = split_root_suffix(cmd->argv[0], &rootfilenm, &suffix); if (hassuffix) { if (strcmp(suffix, "sdat") == 0) useshorts = 1; if (strcmp(suffix, "dat") != 0 && strcmp(suffix, "sdat") != 0) { printf ("\nInput file ('%s') must be a time series ('.dat' or '.sdat')!\n\n", cmd->argv[0]); free(suffix); exit(0); } free(suffix); } else { printf("\nInput file ('%s') must be a time series ('.dat' or '.sdat')!\n\n", cmd->argv[0]); exit(0); } if (cmd->outfileP) { outname = cmd->outfile; } else { outname = (char *) calloc(strlen(rootfilenm) + 11, sizeof(char)); if (useshorts) sprintf(outname, "%s_D%d.sdat", rootfilenm, cmd->factor); else sprintf(outname, "%s_D%d.dat", rootfilenm, cmd->factor); } } /* Read the info file */ readinf(&idata, rootfilenm); if (idata.object) { printf("Downsampling %s data from '%s'.\n\n", remove_whitespace(idata.object), cmd->argv[0]); } else { printf("Downsampling data from '%s'.\n\n", cmd->argv[0]); } /* Open files and create arrays */ infile = chkfopen(argv[1], "rb"); outfile = chkfopen(outname, "wb"); /* Read and downsample */ if (useshorts) { sinbuffer = gen_svect(bufflen * cmd->factor); soutbuffer = gen_svect(bufflen); while ((numread = chkfread(sinbuffer, sizeof(short), bufflen * cmd->factor, infile))) { for (ii = 0; ii < numread / cmd->factor; ii++) { soutbuffer[ii] = 0; for (jj = 0; jj < cmd->factor; jj++) soutbuffer[ii] += sinbuffer[cmd->factor * ii + jj]; } chkfwrite(soutbuffer, sizeof(short), numread / cmd->factor, outfile); N += numread / cmd->factor; } vect_free(sinbuffer); vect_free(soutbuffer); } else { inbuffer = gen_fvect(bufflen * cmd->factor); outbuffer = gen_fvect(bufflen); while ((numread = chkfread(inbuffer, sizeof(float), bufflen * cmd->factor, infile))) { for (ii = 0; ii < numread / cmd->factor; ii++) { outbuffer[ii] = 0; for (jj = 0; jj < cmd->factor; jj++) outbuffer[ii] += inbuffer[cmd->factor * ii + jj]; } chkfwrite(outbuffer, sizeof(float), numread / cmd->factor, outfile); N += numread / cmd->factor; } vect_free(inbuffer); vect_free(outbuffer); } printf("Done. Wrote %lld points.\n\n", N); /* Write the new info file */ idata.dt = idata.dt * cmd->factor; idata.numonoff = 0; idata.N = (double) N; strncpy(idata.name, outname, strlen(outname) - 4); if (useshorts) idata.name[strlen(outname) - 5] = '\0'; else idata.name[strlen(outname) - 4] = '\0'; writeinf(&idata); fclose(infile); fclose(outfile); free(rootfilenm); if (!cmd->outfileP) free(outname); exit(0); }
int main(int argc, char **argv) { int i, j, indx, n_file, special_key, knul=0, anynull, hdutype, status=0; int LastSlashIndex; int NDump, NChan, NumHDU; long nrows=0; double ChanFreq, BW, CentFreq, DM; double TDump, StartTime, DumpMiddleSecs, ScanLen; char KeyValue[32], err_text[256]; char cur_file[256], cur_file_short[64]; char HdrVer[16]; char aspoutstr[11], dumprefstr[11]; fitsfile *Fin; Cmdline *Cmd; /* Get command line variables */ Cmd = parseCmdline(argc, argv); /* Normally use this somewhere, and not showOptionValues */ Cmd->tool = Cmd->tool; /* InfileP = (argc > 1); printf("InfileP = %d\n", InfileP); InfileC = (argc - 1); printf("InfileC = %d\n", InfileC); Infile = (char **)malloc(InfileC); for (i=0; i<InfileC; i++) { Infile[i] = (char *)malloc(256); if (strlen(argv[i+1]) > 255) { fprintf(stderr, "Error: argument %d is too long.\n", i+1); exit(1); } else{ strcpy(Infile[i], argv[i+1]); } printf("Argument %d: %s = %s\n", i+1, Infile[i], argv[i+1]); } */ /************* Keyword list output **************/ if (Cmd->ListP) { if (argc > 2) { fprintf(stderr,"\n-list option must be used alone. Exiting...\n"); exit(1); } else{ printf("\nSome available keywords/info options useable with the "); printf("-key flag:\n"); printf("\nObserving frequency-related options:\n"); printf("\nNCHAN: No. of channels\n"); printf("CFRQ: Centre frequecy\n"); printf("BW: Bandwidth\n"); printf("STARTCHAN: First (0th) frequency channel\n"); printf("ENDCHAN: Last (NCHAN-th) frequency channel\n"); printf("ALLCHAN: List all channels (ALLCHAN keyword output does\n"); printf(" not follow convention of -col flag)\n"); printf("\nIntegration-related options:\n"); printf("\nNDUMP: No. of integrations\n"); printf("TDUMP: Individual integration time (s)\n"); printf("SCANLEN: Full scan length of input file (s)\n"); printf("NPTSPROF: No. of bins in integrated profiles\n"); printf("\nOther options:\n"); printf("\nSRC_NAME: Source Name\n"); printf("DM: Dispersion measure\n"); printf("OBS_MODE: PSR/CAL -- normal or calibration scan\n"); printf("OBSVTY: Observatory code\n"); printf("FD_POLN: Polarisation basis (e.g. L or C for linear or\n"); printf(" circular, respectively)\n"); printf("RA: Right ascension of source\n"); printf("DEC: Declination of source\n"); printf("\nNote that all -key arguments are NOT case-sensitive.\n"); } } else if(Cmd->TDiffP) { if (Cmd->KeywordP) { fprintf(stderr,"\n-tdiff option must be used alone. Exiting...\n"); exit(1); } } else{ if(!Cmd->KeywordP){ printf("Must use -key option with appropriate arguments if not\n"); printf("using -list or -tdiff flag\n"); exit(1); } if(!Cmd->InfileP){ printf("Must use -infile option with input file arguments if not\n"); printf("using -list or -tdiff flag\n"); exit(1); } } /*************************************************/ /* Print header line */ if(Cmd->KeywordP && Cmd->ColumnP) { printf("%s ","File"); for(i=0;i<Cmd->KeywordC;i++) printf("%s ",Cmd->Keyword[i]); printf("\n"); } else if(Cmd->TDiffP){ // for Tdiff printf("File Source MJD TDIFF [ns]\n"); } for(n_file=0;n_file<Cmd->InfileC;n_file++){ strcpy(cur_file,Cmd->Infile[n_file]); /* Open fits file */ if(fits_open_file(&Fin, cur_file, READONLY, &status)){ printf("\nError opening FITS file %s\n", cur_file); fits_get_errstatus(status, err_text); printf("FITS Error status %d: %s\n",status,err_text); exit(1); } status=0; /* get rid of everthing before and including last forward slash in the filename for printing to screen: */ LastSlashIndex = -1; for(i=strlen(cur_file)-1;i>0;i--){ if(!strncasecmp(&cur_file[i],"/",1)){ LastSlashIndex = i; break; } } strncpy(cur_file_short,&cur_file[LastSlashIndex+1],strlen(cur_file)-LastSlashIndex); if(Cmd->TDiffP){ /******************** TDIFF ***********************/ int i_dump; int IMJDStart, SMJDStart; double StartMJD, MJD, TDiff; char ObsCode[4], psr[12]; /* Check this is Nancay data */ fits_read_key(Fin, TSTRING, "OBSVTY", ObsCode, NULL, &status); if(strncmp(ObsCode,"f",1)) { printf("\nError: File %s does not contain Nancay data (obs code = %s).", cur_file_short,ObsCode); printf(" Cannot extract TDIFF from this file. Exiting.\n"); exit(2); } /* Get pulsar name */ fits_read_key(Fin, TSTRING, "SRC_NAME", psr, NULL, &status); /* Calculate start MJD */ fits_read_key(Fin, TINT, "STT_IMJD", &(IMJDStart),NULL, &status); status = 0; fits_read_key(Fin, TINT, "STT_SMJD", &(SMJDStart),NULL, &status); status = 0; StartMJD = (double)IMJDStart + (double)SMJDStart/86400.; /* Find nuber of dumps in this file */ fits_get_num_hdus(Fin, &NumHDU, &status); NDump = NumHDU-3; // Nancay data is always Ver1.0 /* Loop through each dump and pick out time stamp and phase */ fits_movnam_hdu(Fin, BINARY_TBL, "ASPOUT0", 0, &status); for (i_dump=0;i_dump<NDump;i_dump++){ fits_movrel_hdu(Fin, 1, NULL, &status); fits_read_key(Fin, TDOUBLE, "DUMPMIDSECS", &(DumpMiddleSecs),NULL, &status); status = 0; fits_read_key(Fin, TDOUBLE, "DUMPTDIFF", &(TDiff),NULL, &status); status = 0; MJD = StartMJD + DumpMiddleSecs/86400.; printf("%s ",cur_file_short); printf("%s ",psr); printf("%14.8lf ",MJD); printf("%7.3lf \n",TDiff); } } // else if(Cmd->ColumnP) printf("%s ",cur_file_short); else printf("\nKeywords in %s:\n\n", cur_file_short); /* For each keyword requested... */ for(i=0;i<Cmd->KeywordC;i++){ special_key=0; /* Clear Keyword */ strcpy(KeyValue,"\0"); /**************** Channel-based keywords: *****************/ if(!strcasecmp(Cmd->Keyword[i],"NChan") || // # of channels !strncasecmp(Cmd->Keyword[i],"CFRQ",4) || // centre frequency !strncasecmp(Cmd->Keyword[i],"BW",2) || // bandwidth in MHz !strcasecmp(Cmd->Keyword[i],"StartChan") || // first channel (MHz) !strcasecmp(Cmd->Keyword[i],"EndChan") || // last channel (MHz) !strcasecmp(Cmd->Keyword[i],"AllChan")){ // all channels (MHz) /* move to channel table */ fits_movnam_hdu(Fin, ASCII_TBL, "BECONFIG", 0, &status); ffgnrw(Fin, &nrows, &status); indx = 1; fits_read_col(Fin, TINT, indx++, 1, 1, 1, &knul, &NChan, &anynull, &status); /* Write heading for AllChans keyword if requested */ if (!strcasecmp(Cmd->Keyword[i],"AllChan")) printf("\nAll Channels:\n"); /* Read NChans number, copy to keyword */ if (!strcasecmp(Cmd->Keyword[i],"NChan")) sprintf(KeyValue,"%d",NChan); /* Initialize BW and CentFreq variables */ BW=CentFreq=0.; for (j=0; j<NChan; j++) { /* read each channel in succession */ fits_read_col(Fin, TDOUBLE, indx++, 1, 1, 1, NULL, &ChanFreq, &anynull, &status); /* Take not of first channel if StartChan keyword is requested */ if (!strcasecmp(Cmd->Keyword[i],"StartChan") && j==0) sprintf(KeyValue,"%6.1lf",ChanFreq); /* Calculate bandwidth or centre frequency using first and last channels */ if((!strncasecmp(Cmd->Keyword[i],"BW",2) || !strncasecmp(Cmd->Keyword[i],"CFRQ",4))){ if (j==0) BW = CentFreq = ChanFreq; if (j==NChan-1) { /* +4 because we are subtracting centres of channels, so this includes the edges... */ BW = fabs(ChanFreq-BW)+4.; CentFreq = 0.5*(CentFreq + ChanFreq); } } /* Print each channel separately if AllChans keyword is requestes */ if (!strcasecmp(Cmd->Keyword[i],"AllChan")) printf("%6.1lf ",ChanFreq); } /* Take note of last channel if requested */ if (!strcasecmp(Cmd->Keyword[i],"EndChan")) sprintf(KeyValue,"%6.1lf",ChanFreq); /* Copy centre frequency and bandwidth keywords to keyword variable if requested */ if (!strncasecmp(Cmd->Keyword[i],"CFRQ",4)) sprintf(KeyValue,"%6.1lf",CentFreq); if(!strncasecmp(Cmd->Keyword[i],"BW",2)) sprintf(KeyValue,"%6.1lf",BW); /* If AllChans is requested, print newline */ if (!strcasecmp(Cmd->Keyword[i],"AllChan")) { special_key=1; printf("\n"); } /* Move file pointer back to beginning of file */ fits_movrel_hdu(Fin, -1, NULL, &status); /**************** End channel-based keywords: *****************/ } /**************** DM keyword: *****************/ else if(!strcasecmp(Cmd->Keyword[i],"DM")) { // special_key=1; fits_movnam_hdu(Fin, ASCII_TBL, "COHDDISP", 0, &status); ffgnrw(Fin, &nrows, &status); indx = 1; fits_read_col(Fin, TDOUBLE, indx, 1, 1, 1, NULL, &DM, &anynull, &status); sprintf(KeyValue,"%8.5lf",DM); fits_movrel_hdu(Fin, -2, NULL, &status); /**************** End DM keyword: *****************/ } /**************** NDUMPS or SCANLEN keyword: *****************/ else if(!strcasecmp(Cmd->Keyword[i],"NDUMP") || // # of dumps !strcasecmp(Cmd->Keyword[i],"NDUMPS") || !strcasecmp(Cmd->Keyword[i],"TDUMP") || // dump length in secs !strcasecmp(Cmd->Keyword[i],"SCANLEN")){ // length of scans in secs /* Get the number of dumps. this depensd on version of asp file code */ fits_get_num_hdus(Fin, &NumHDU, &status); fits_read_key(Fin, TSTRING, "HDRVER", HdrVer, NULL, &status); status=0; if(!strcasecmp(HdrVer,"Ver1.0")){ /* divided by 2 since half the tables are phase/period vs. freq before each data table */ NDump = NumHDU-3; } else if(!strcasecmp(HdrVer,"Ver1.0.1")){ /* the "3" is temporary, depending on how many non-data tables we will be using */ NDump = (NumHDU-3)/2; } else{ printf("Do not recognize FITS file version number in header.\n"); printf("This header %s. Exiting...\n",HdrVer); exit(3); } if (!strncasecmp(Cmd->Keyword[i],"NDUMP",4)) { // covers NDUMPS also... sprintf(KeyValue,"%d",NDump); } // if need TDUMP or Total Scan Length (SCANLEN): if (!strcasecmp(Cmd->Keyword[i],"TDUMP") || // dump length in secs !strcasecmp(Cmd->Keyword[i],"SCANLEN")) { /* Get time between successive scans */ /* Get start time */ fits_read_key(Fin, TDOUBLE, "STT_SMJD", &StartTime, NULL, &status); if (NDump > 1) { sprintf(aspoutstr,"ASPOUT%d",NDump-2); sprintf(dumprefstr,"DUMPREF%d",NDump-2); // printf ("\n%s %s\n",aspoutstr,dumprefstr); /* Move to first data table , get central time stamp */ if(!strcasecmp(HdrVer,"Ver1.0")) { fits_movnam_hdu(Fin, BINARY_TBL, "ASPOUT0", 0, &status); fits_read_key(Fin, TDOUBLE, "DUMPMIDSECS", &(DumpMiddleSecs),NULL, &status); status = 0; } else if (!strcasecmp(HdrVer,"Ver1.0.1")) { fits_movnam_hdu(Fin, ASCII_TBL, "DUMPREF0", 0, &status); fits_read_key(Fin, TDOUBLE, "MIDSECS", &(DumpMiddleSecs), NULL, &status); status = 0; } TDump = DumpMiddleSecs; /* Move to second data table , get central time stamp */ sprintf(aspoutstr,"ASPOUT%d",234); sprintf(dumprefstr,"DUMPREF%d",234); // printf ("\n%s %s\n",aspoutstr,dumprefstr); if(!strcasecmp(HdrVer,"Ver1.0")) { fits_movnam_hdu(Fin, BINARY_TBL, "ASPOUT1", 0, &status); fits_read_key(Fin, TDOUBLE, "DUMPMIDSECS", &(DumpMiddleSecs),NULL, &status); status = 0; } else if (!strcasecmp(HdrVer,"Ver1.0.1")) { fits_movnam_hdu(Fin, ASCII_TBL, "DUMPREF1", 0, &status); fits_read_key(Fin, TDOUBLE, "MIDSECS", &(DumpMiddleSecs), NULL, &status); status = 0; } if(DumpMiddleSecs < TDump) DumpMiddleSecs += 86400.; // printf("First = %lf, Second = %lf\n", TDump, DumpMiddleSecs); TDump = DumpMiddleSecs - TDump; } else { /* We may have switched over to the next MJD, so take care of this if it's the case */ if (DumpMiddleSecs < StartTime) DumpMiddleSecs += 86400.; /* TDump is twice the difference between the middle time stamp and the start time */ TDump = 2.* ((double)floor(DumpMiddleSecs) - StartTime); } if (!strncasecmp(Cmd->Keyword[i],"TDUMP",4)) { sprintf(KeyValue,"%9.3lf",TDump); } if (!strcasecmp(Cmd->Keyword[i],"SCANLEN")) { ScanLen = NDump*TDump; sprintf(KeyValue,"%7.1lf",ScanLen); } /* Move back to first HDU again */ fits_movabs_hdu(Fin,1,&hdutype,&status);status = 0; } } /**************** Other keywords: *****************/ else{ fits_read_key(Fin, TSTRING, Cmd->Keyword[i], KeyValue, NULL, &status); if(status == VALUE_UNDEFINED || !strcasecmp(KeyValue,"")) { sprintf(KeyValue,"<not_found>"); } /**************** End other keywords: *****************/ } /* The only special keyword is AllChans. If it's not, do the following */ if(!special_key){ /* Column format */ if(Cmd->ColumnP) { printf("%s ",KeyValue); if(i==Cmd->KeywordC-1 && n_file<Cmd->InfileC-1) printf("\n"); } /* Non-column format */ else { printf("%15s: %s\n",Cmd->Keyword[i],KeyValue); } } } status=0; fits_close_file(Fin, &status); } printf("\n"); fflush(stdout); exit(0); }
int main(int argc, char *argv[]) { Cmdline *cmd; struct psrfits pfi, pfo; // input and output struct subband_info si; int stat=0, padding=0, userN=0; // Call usage() if we have no command line arguments if (argc == 1) { Program = argv[0]; usage(); exit(0); } // Parse the command line using the excellent program Clig cmd = parseCmdline(argc, argv); // Open the input PSRFITs files psrfits_set_files(&pfi, cmd->argc, cmd->argv); // Use the dynamic filename allocation if (pfi.numfiles==0) pfi.filenum = cmd->startfile; pfi.tot_rows = pfi.N = pfi.T = pfi.status = 0; int rv = psrfits_open(&pfi); if (rv) { fits_report_error(stderr, rv); exit(1); } // Read the user weights if requested si.userwgts = NULL; if (cmd->wgtsfileP) { read_weights(cmd->wgtsfile, &userN, &si.userwgts); if (userN != pfi.hdr.nchan) { printf("Error!: Input data has %d channels, but '%s' contains only %d weights!\n", pfi.hdr.nchan, cmd->wgtsfile, userN); exit(0); } printf("Overriding input channel weights with those in '%s'\n", cmd->wgtsfile); } // Initialize the subbanding // (including reading the first row of data and // putting it in si->fbuffer) init_subbanding(&pfi, &pfo, &si, cmd); if (cmd->outputbasenameP) strcpy(pfo.basefilename, cmd->outputbasename); // Loop through the data do { // Put the overlapping parts from the next block into si->buffer float *ptr = pfi.sub.fdata + si.buflen * si.bufwid; if (padding==0) stat = psrfits_read_part_DATA(&pfi, si.max_overlap, si.numunsigned, ptr); if (stat || padding) { // Need to use padding since we ran out of data printf("Adding a missing row (#%d) of padding to the subbands.\n", pfi.tot_rows); // Now fill the last part of si->fbuffer with the chan_avgs so that // it acts like a correctly read block (or row) fill_chans_with_avgs(si.max_overlap, si.bufwid, ptr, si.chan_avgs); } //print_raw_chan_stats(pfi.sub.data, pfi.hdr.nsblk, // pfi.hdr.nchan, pfi.hdr.npol); // if the input data isn't 8 bit, unpack: if (pfi.hdr.nbits == 2) pf_unpack_2bit_to_8bit(&pfi, si.numunsigned); else if (pfi.hdr.nbits == 4) pf_unpack_4bit_to_8bit(&pfi, si.numunsigned); if ((pfo.hdr.ds_time_fact == 1) && (pfo.hdr.ds_freq_fact == 1)) { // No subbanding is needed, so just copy the float buffer // This is useful if we are just changing the number of bits // Could do it without a copy by simply exchanging pointers // to the fdata buffers in pfo and pfi... memcpy(pfo.sub.fdata, pfi.sub.fdata, pfi.hdr.nsblk * pfi.hdr.npol * pfi.hdr.nchan * sizeof(float)); } else { // Now create the subbanded row in the output buffer make_subbands(&pfi, &si); } // Output only Stokes I (in place via floats) if (pfo.hdr.onlyI && pfo.hdr.npol==4) get_stokes_I(&pfo); // Downsample in time (in place via floats) if (pfo.hdr.ds_time_fact > 1) downsample_time(&pfo); // Compute new scales and offsets so that we can pack // into 8-bits reliably if (pfo.rownum == 1) new_scales_and_offsets(&pfo, si.numunsigned, cmd); // Convert the floats back to bytes in the output array un_scale_and_offset_data(&pfo, si.numunsigned); //print_raw_chan_stats(pfo.sub.data, pfo.hdr.nsblk / pfo.hdr.ds_time_fact, // pfo.hdr.nchan / pfo.hdr.ds_freq_fact, pfo.hdr.npol); // pack into 2 or 4 bits if needed if (pfo.hdr.nbits == 2) pf_pack_8bit_to_2bit(&pfo, si.numunsigned); else if (pfo.hdr.nbits == 4) pf_pack_8bit_to_4bit(&pfo, si.numunsigned); // Write the new row to the output file pfo.sub.offs = (pfo.tot_rows+0.5) * pfo.sub.tsubint; psrfits_write_subint(&pfo); // Break out of the loop here if stat is set if (stat) break; // shift the last part of the current row into the "last-row" // part of the data buffer memcpy(si.fbuffer, si.fbuffer + si.buflen * si.bufwid, si.max_overlap * si.bufwid * sizeof(float)); // Read the next row (or padding) padding = get_current_row(&pfi, &si); // Set the new weights properly new_weights(&pfi, &pfo); } while (pfi.status == 0); print_clips(&pfo); rv = psrfits_close(&pfi); if (rv>100) { fits_report_error(stderr, rv); } rv = psrfits_close(&pfo); if (rv>100) { fits_report_error(stderr, rv); } exit(0); }
int main(int argc, char *argv[]) { int numfiles, ii, numrows, rownum, ichan, itsamp, datidx; int spec_per_row, status, maxrows; unsigned long int maxfilesize; float offset, scale, datum, packdatum, maxval, fulltsubint; float *datachunk; FILE **infiles; struct psrfits pfin, pfout; Cmdline *cmd; fitsfile *infits, *outfits; char outfilename[128], templatename[128], tform[8]; char *pc1, *pc2; int first = 1, dummy = 0, nclipped; short int *inrowdata; unsigned char *outrowdata; if (argc == 1) { Program = argv[0]; usage(); exit(1); } // Parse the command line using the excellent program Clig cmd = parseCmdline(argc, argv); numfiles = cmd->argc; infiles = (FILE **) malloc(numfiles * sizeof(FILE *)); //Set the max. total size (in bytes) of all rows in an output file, //leaving some room for PSRFITS header maxfilesize = (unsigned long int)(cmd->numgb * GB); maxfilesize = maxfilesize - 1000*KB; //fprintf(stderr,"cmd->numgb: %f maxfilesize: %ld\n",cmd->numgb,maxfilesize); #ifdef DEBUG showOptionValues(); #endif printf("\n PSRFITS 16-bit to 4-bit Conversion Code\n"); printf(" by J. Deneva, S. Ransom, & S. Chatterjee\n\n"); // Open the input files status = 0; //fits_close segfaults if this is not initialized printf("Reading input data from:\n"); for (ii = 0; ii < numfiles; ii++) { printf(" '%s'\n", cmd->argv[ii]); //Get the file basename and number from command-line argument //(code taken from psrfits2fil) pc2 = strrchr(cmd->argv[ii], '.'); // at .fits *pc2 = 0; // terminate string pc1 = pc2 - 1; while ((pc1 >= cmd->argv[ii]) && isdigit(*pc1)) pc1--; if (pc1 <= cmd->argv[ii]) { // need at least 1 char before filenum puts("Illegal input filename. must have chars before the filenumber"); exit(1); } pc1++; // we were sitting on "." move to first digit pfin.filenum = atoi(pc1); pfin.fnamedigits = pc2 - pc1; // how many digits in filenumbering scheme. *pc1 = 0; // null terminate the basefilename strcpy(pfin.basefilename, cmd->argv[ii]); pfin.initialized = 0; // set to 1 in psrfits_open() pfin.status = 0; //(end of code taken from psrfits2fil) //Open the existing psrfits file if (psrfits_open(&pfin, READONLY) != 0) { fprintf(stderr, "error opening file\n"); fits_report_error(stderr, pfin.status); exit(1); } // Create the subint arrays if (first) { pfin.sub.dat_freqs = (float *) malloc(sizeof(float) * pfin.hdr.nchan); pfin.sub.dat_weights = (float *) malloc(sizeof(float) * pfin.hdr.nchan); pfin.sub.dat_offsets = (float *) malloc(sizeof(float) * pfin.hdr.nchan * pfin.hdr.npol); pfin.sub.dat_scales = (float *) malloc(sizeof(float) * pfin.hdr.nchan * pfin.hdr.npol); //first is set to 0 after data buffer allocation further below } infits = pfin.fptr; spec_per_row = pfin.hdr.nsblk; fits_read_key(infits, TINT, "NAXIS2", &dummy, NULL, &status); pfin.tot_rows = dummy; numrows = dummy; //If dealing with 1st input file, create output template if (ii == 0) { sprintf(templatename, "%s.template.fits",cmd->outfile); fits_create_file(&outfits, templatename, &status); //fprintf(stderr,"pfin.basefilename: %s\n", pfin.basefilename); //fprintf(stderr,"status: %d\n", status); //Instead of copying HDUs one by one, can move to the SUBINT //HDU, and copy all the HDUs preceding it fits_movnam_hdu(infits, BINARY_TBL, "SUBINT", 0, &status); fits_copy_file(infits, outfits, 1, 0, 0, &status); //Copy the SUBINT table header fits_copy_header(infits, outfits, &status); fits_flush_buffer(outfits, 0, &status); //Set NAXIS2 in the output SUBINT table to 0 b/c we haven't //written any rows yet dummy = 0; fits_update_key(outfits, TINT, "NAXIS2", &dummy, NULL, &status); //Edit the NBITS key if (DEBUG) { dummy = 8; fits_update_key(outfits, TINT, "NBITS", &dummy, NULL, &status); } else { fits_update_key(outfits, TINT, "NBITS", &(cmd->numbits), NULL, &status); } //Edit the TFORM17 column: # of data bytes per row //fits_get_colnum(outfits,1,"DATA",&dummy,&status); if (DEBUG) sprintf(tform, "%dB", pfin.hdr.nsblk * pfin.hdr.nchan * pfin.hdr.npol); else sprintf(tform, "%dB", pfin.hdr.nsblk * pfin.hdr.nchan * pfin.hdr.npol * cmd->numbits / 8); fits_update_key(outfits, TSTRING, "TTYPE17", "DATA", NULL, &status); fits_update_key(outfits, TSTRING, "TFORM17", tform, NULL, &status); //Edit NAXIS1: row width in bytes fits_read_key(outfits, TINT, "NAXIS1", &dummy, NULL, &status); if (DEBUG) { dummy = dummy - pfin.hdr.nsblk * pfin.hdr.nchan * pfin.hdr.npol * (pfin.hdr.nbits - 8) / 8; } else { dummy = dummy - pfin.hdr.nsblk * pfin.hdr.nchan * pfin.hdr.npol * (pfin.hdr.nbits - cmd->numbits) / 8; } fits_update_key(outfits, TINT, "NAXIS1", &dummy, NULL, &status); //Set the max # of rows per file, based on the requested //output file size maxrows = maxfilesize / dummy; //fprintf(stderr,"maxrows: %d\n",maxrows); fits_close_file(outfits, &status); rownum = 0; } while (psrfits_read_subint(&pfin, first) == 0) { fprintf(stderr, "Working on row %d\n", ++rownum); //If this is the first row, store the length of a full subint if (ii == 0 && rownum == 1) fulltsubint = pfin.sub.tsubint; //If this is the last row and it's partial, drop it. //(It's pfin.rownum-1 below because the rownum member of the psrfits struct seems to be intended to indicate at the *start* of what row we are, i.e. a row that has not yet been read. In contrast, pfout.rownum indicates how many rows have been written, i.e. at the *end* of what row we are in the output.) if (pfin.rownum-1 == numrows && fabs(pfin.sub.tsubint - fulltsubint) > pfin.hdr.dt) { fprintf(stderr, "Dropping partial row of length %f s (full row is %f s)\n", pfin.sub.tsubint, fulltsubint); break; } //If we just read in the 1st row, or if we already wrote the last row in the current output file, create a new output file if ((ii == 0 && rownum == 1) || pfout.rownum == maxrows) { //Create new output file from the template pfout.fnamedigits = pfin.fnamedigits; if(ii == 0) pfout.filenum = pfin.filenum; else pfout.filenum++; sprintf(outfilename, "%s.%0*d.fits", cmd->outfile, pfout.fnamedigits, pfout.filenum); fits_create_template(&outfits, outfilename, templatename, &status); //fprintf(stderr,"After fits_create_template, status: %d\n",status); fits_close_file(outfits, &status); //Now reopen the file so that the pfout structure is initialized pfout.status = 0; pfout.initialized = 0; sprintf(pfout.basefilename, "%s.", cmd->outfile); if (psrfits_open(&pfout, READWRITE) != 0) { fprintf(stderr, "error opening file\n"); fits_report_error(stderr, pfout.status); exit(1); } outfits = pfout.fptr; maxval = pow(2, cmd->numbits) - 1; pfout.rows_per_file = maxrows; //fprintf(stderr, "maxval: %f\n", maxval); //fprintf(stderr, "pfout.rows_per_file: %d\n",pfout.rows_per_file); //These are not initialized in psrfits_open but are needed //in psrfits_write_subint (not obvious what are the corresponding //fields in any of the psrfits table headers) pfout.hdr.ds_freq_fact = 1; pfout.hdr.ds_time_fact = 1; } //Copy the subint struct from pfin to pfout, but correct //elements that are not the same pfout.sub = pfin.sub; //this copies array pointers too pfout.sub.bytes_per_subint = pfin.sub.bytes_per_subint * pfout.hdr.nbits / pfin.hdr.nbits; pfout.sub.dataBytesAlloced = pfout.sub.bytes_per_subint; pfout.sub.FITS_typecode = TBYTE; if (first) { //Allocate scaling buffer and output buffer datachunk = gen_fvect(spec_per_row); outrowdata = gen_bvect(pfout.sub.bytes_per_subint); first = 0; } pfout.sub.data = outrowdata; inrowdata = (short int *) pfin.sub.data; nclipped = 0; // Loop over all the channels: for (ichan = 0; ichan < pfout.hdr.nchan * pfout.hdr.npol; ichan++) { // Populate datachunk[] by picking out all time samples for ichan for (itsamp = 0; itsamp < spec_per_row; itsamp++) datachunk[itsamp] = (float) (inrowdata[ichan + itsamp * pfout.hdr.nchan * pfout.hdr.npol]); // Compute the statistics here, and put the offsets and scales in // pf.sub.dat_offsets[] and pf.sub.dat_scales[] if (rescale(datachunk, spec_per_row, cmd->numbits, &offset, &scale) != 0) { printf("Rescale routine failed!\n"); return (-1); } pfout.sub.dat_offsets[ichan] = offset; pfout.sub.dat_scales[ichan] = scale; // Since we have the offset and scale ready, rescale the data: for (itsamp = 0; itsamp < spec_per_row; itsamp++) { datum = (scale == 0.0) ? 0.0 : roundf((datachunk[itsamp] - offset) / scale); if (datum < 0.0) { datum = 0; nclipped++; } else if (datum > maxval) { datum = maxval; nclipped++; } inrowdata[ichan + itsamp * pfout.hdr.nchan * pfout.hdr.npol] = (short int) datum; } // Now inrowdata[ichan] contains rescaled ints. } // Then do the conversion and store the // results in pf.sub.data[] if (cmd->numbits == 8 || DEBUG) { for (itsamp = 0; itsamp < spec_per_row; itsamp++) { datidx = itsamp * pfout.hdr.nchan * pfout.hdr.npol; for (ichan = 0; ichan < pfout.hdr.nchan * pfout.hdr.npol; ichan++, datidx++) { pfout.sub.data[datidx] = (unsigned char) inrowdata[datidx]; } } } else if (cmd->numbits == 4) { for (itsamp = 0; itsamp < spec_per_row; itsamp++) { datidx = itsamp * pfout.hdr.nchan * pfout.hdr.npol; for (ichan = 0; ichan < pfout.hdr.nchan * pfout.hdr.npol; ichan += 2, datidx += 2) { packdatum = inrowdata[datidx] * 16 + inrowdata[datidx + 1]; pfout.sub.data[datidx / 2] = (unsigned char) packdatum; } } } else { fprintf(stderr, "Only 4 or 8-bit output formats supported.\n"); fprintf(stderr, "Bits per sample requested: %d\n", cmd->numbits); exit(1); } //pfout.sub.offs = (pfout.tot_rows+0.5) * pfout.sub.tsubint; fprintf(stderr, "nclipped: %d fraction clipped: %f\n", nclipped, (float) nclipped / (pfout.hdr.nchan * pfout.hdr.npol * pfout.hdr.nsblk)); // Now write the row. status = psrfits_write_subint(&pfout); if (status) { printf("\nError (%d) writing PSRFITS...\n\n", status); break; } //If current output file has reached the max # of rows, close it if (pfout.rownum == maxrows) fits_close_file(outfits, &status); } //Close the files fits_close_file(infits, &status); } fits_close_file(outfits, &status); // Free the structure arrays too... free(datachunk); free(infiles); free(pfin.sub.dat_freqs); free(pfin.sub.dat_weights); free(pfin.sub.dat_offsets); free(pfin.sub.dat_scales); free(pfin.sub.data); free(pfout.sub.data); free(pfin.sub.stat); return 0; }
int main(int argc, char *argv[]) { FILE *bytemaskfile; float **dataavg = NULL, **datastd = NULL, **datapow = NULL; float *chandata = NULL, powavg, powstd, powmax; float inttime, norm, fracterror = RFI_FRACTERROR; float *rawdata = NULL; unsigned char **bytemask = NULL; short *srawdata = NULL; char *outfilenm, *statsfilenm, *maskfilenm; char *bytemaskfilenm, *rfifilenm; int numchan = 0, numint = 0, newper = 0, oldper = 0, good_padvals = 0; int blocksperint, ptsperint = 0, ptsperblock = 0, padding = 0; int numcands, candnum, numrfi = 0, numrfivect = NUM_RFI_VECT; int ii, jj, kk, slen, numread = 0, insubs = 0; int harmsum = RFI_NUMHARMSUM, lobin = RFI_LOBIN, numbetween = RFI_NUMBETWEEN; double davg, dvar, freq; struct spectra_info s; presto_interptype interptype; rfi *rfivect = NULL; mask oldmask, newmask; fftcand *cands; infodata idata; Cmdline *cmd; /* Call usage() if we have no command line arguments */ if (argc == 1) { Program = argv[0]; printf("\n"); usage(); exit(0); } /* Parse the command line using the excellent program Clig */ cmd = parseCmdline(argc, argv); spectra_info_set_defaults(&s); s.filenames = cmd->argv; s.num_files = cmd->argc; s.clip_sigma = cmd->clip; // -1 causes the data to determine if we use weights, scales, & // offsets for PSRFITS or flip the band for any data type where // we can figure that out with the data s.apply_flipband = (cmd->invertP) ? 1 : -1; s.apply_weight = (cmd->noweightsP) ? 0 : -1; s.apply_scale = (cmd->noscalesP) ? 0 : -1; s.apply_offset = (cmd->nooffsetsP) ? 0 : -1; s.remove_zerodm = (cmd->zerodmP) ? 1 : 0; if (cmd->noclipP) { cmd->clip = 0.0; s.clip_sigma = 0.0; } if (cmd->ifsP) { // 0 = default or summed, 1-4 are possible also s.use_poln = cmd->ifs; } slen = strlen(cmd->outfile) + 20; #ifdef DEBUG showOptionValues(); #endif printf("\n\n"); printf(" Pulsar Data RFI Finder\n"); printf(" by Scott M. Ransom\n\n"); /* The following is the root of all the output files */ outfilenm = (char *) calloc(slen, sizeof(char)); sprintf(outfilenm, "%s_rfifind", cmd->outfile); /* And here are the output file names */ maskfilenm = (char *) calloc(slen, sizeof(char)); sprintf(maskfilenm, "%s.mask", outfilenm); bytemaskfilenm = (char *) calloc(slen, sizeof(char)); sprintf(bytemaskfilenm, "%s.bytemask", outfilenm); rfifilenm = (char *) calloc(slen, sizeof(char)); sprintf(rfifilenm, "%s.rfi", outfilenm); statsfilenm = (char *) calloc(slen, sizeof(char)); sprintf(statsfilenm, "%s.stats", outfilenm); sprintf(idata.name, "%s", outfilenm); if (RAWDATA) { if (cmd->filterbankP) s.datatype = SIGPROCFB; else if (cmd->psrfitsP) s.datatype = PSRFITS; else if (cmd->pkmbP) s.datatype = SCAMP; else if (cmd->bcpmP) s.datatype = BPP; else if (cmd->wappP) s.datatype = WAPP; else if (cmd->spigotP) s.datatype = SPIGOT; } else { // Attempt to auto-identify the data identify_psrdatatype(&s, 1); if (s.datatype==SIGPROCFB) cmd->filterbankP = 1; else if (s.datatype==PSRFITS) cmd->psrfitsP = 1; else if (s.datatype==SCAMP) cmd->pkmbP = 1; else if (s.datatype==BPP) cmd->bcpmP = 1; else if (s.datatype==WAPP) cmd->wappP = 1; else if (s.datatype==SPIGOT) cmd->spigotP = 1; else if (s.datatype==SUBBAND) insubs = 1; else { printf("Error: Unable to identify input data files. Please specify type.\n\n"); exit(1); } } if (!cmd->nocomputeP) { if (RAWDATA || insubs) { char description[40]; psrdatatype_description(description, s.datatype); if (s.num_files > 1) printf("Reading %s data from %d files:\n", description, s.num_files); else printf("Reading %s data from 1 file:\n", description); if (insubs) s.files = (FILE **)malloc(sizeof(FILE *) * s.num_files); for (ii = 0; ii < s.num_files; ii++) { printf(" '%s'\n", cmd->argv[ii]); if (insubs) s.files[ii] = chkfopen(cmd->argv[ii], "rb"); } printf("\n"); } if (RAWDATA) { read_rawdata_files(&s); print_spectra_info_summary(&s); spectra_info_to_inf(&s, &idata); ptsperblock = s.spectra_per_subint; numchan = s.num_channels; idata.dm = 0.0; } if (insubs) { /* Set-up values if we are using subbands */ char *tmpname, *root, *suffix; if (split_root_suffix(s.filenames[0], &root, &suffix) == 0) { printf("Error: The input filename (%s) must have a suffix!\n\n", s.filenames[0]); exit(1); } if (strncmp(suffix, "sub", 3) == 0) { tmpname = calloc(strlen(root) + 6, 1); sprintf(tmpname, "%s.sub", root); readinf(&idata, tmpname); free(tmpname); } else { printf("\nThe input files (%s) must be subbands! (i.e. *.sub##)\n\n", s.filenames[0]); exit(1); } free(root); free(suffix); ptsperblock = 1; /* Compensate for the fact that we have subbands and not channels */ idata.freq = idata.freq - 0.5 * idata.chan_wid + 0.5 * idata.chan_wid * (idata.num_chan / s.num_files); idata.chan_wid = idata.num_chan / s.num_files * idata.chan_wid; idata.num_chan = numchan = s.num_files; idata.dm = 0.0; sprintf(idata.name, "%s", outfilenm); writeinf(&idata); s.padvals = gen_fvect(s.num_files); for (ii = 0 ; ii < s.num_files ; ii++) s.padvals[ii] = 0.0; } /* Read an input mask if wanted */ if (cmd->maskfileP) { read_mask(cmd->maskfile, &oldmask); printf("Read old mask information from '%s'\n\n", cmd->maskfile); good_padvals = determine_padvals(cmd->maskfile, &oldmask, s.padvals); } else { oldmask.numchan = oldmask.numint = 0; } /* The number of data points and blocks to work with at a time */ if (cmd->blocksP) { blocksperint = cmd->blocks; cmd->time = blocksperint * ptsperblock * idata.dt; } else { blocksperint = (int) (cmd->time / (ptsperblock * idata.dt) + 0.5); } ptsperint = blocksperint * ptsperblock; numint = (long long) idata.N / ptsperint; if ((long long) idata.N % ptsperint) numint++; inttime = ptsperint * idata.dt; printf("Analyzing data sections of length %d points (%.6g sec).\n", ptsperint, inttime); { int *factors, numfactors; factors = get_prime_factors(ptsperint, &numfactors); printf(" Prime factors are: "); for (ii = 0; ii < numfactors; ii++) printf("%d ", factors[ii]); printf("\n"); if (factors[numfactors - 1] > 13) { printf(" WARNING: The largest prime factor is pretty big! This will\n" " cause the FFTs to take a long time to compute. I\n" " recommend choosing a different -time value.\n"); } printf("\n"); free(factors); } /* Allocate our workarrays */ if (RAWDATA) rawdata = gen_fvect(idata.num_chan * ptsperblock * blocksperint); else if (insubs) srawdata = gen_svect(idata.num_chan * ptsperblock * blocksperint); dataavg = gen_fmatrix(numint, numchan); datastd = gen_fmatrix(numint, numchan); datapow = gen_fmatrix(numint, numchan); chandata = gen_fvect(ptsperint); bytemask = gen_bmatrix(numint, numchan); for (ii = 0; ii < numint; ii++) for (jj = 0; jj < numchan; jj++) bytemask[ii][jj] = GOODDATA; rfivect = rfi_vector(rfivect, numchan, numint, 0, numrfivect); if (numbetween == 2) interptype = INTERBIN; else interptype = INTERPOLATE; /* Main loop */ printf("Writing mask data to '%s'.\n", maskfilenm); printf("Writing RFI data to '%s'.\n", rfifilenm); printf("Writing statistics to '%s'.\n\n", statsfilenm); printf("Massaging the data ...\n\n"); printf("Amount Complete = %3d%%", oldper); fflush(stdout); for (ii = 0; ii < numint; ii++) { /* Loop over the intervals */ newper = (int) ((float) ii / numint * 100.0 + 0.5); if (newper > oldper) { printf("\rAmount Complete = %3d%%", newper); fflush(stdout); oldper = newper; } /* Read a chunk of data */ if (RAWDATA) numread = read_rawblocks(rawdata, blocksperint, &s, &padding); else if (insubs) numread = read_subband_rawblocks(s.files, s.num_files, srawdata, blocksperint, &padding); if (padding) for (jj = 0; jj < numchan; jj++) bytemask[ii][jj] |= PADDING; for (jj = 0; jj < numchan; jj++) { /* Loop over the channels */ if (RAWDATA) get_channel(chandata, jj, blocksperint, rawdata, &s); else if (insubs) get_subband(jj, chandata, srawdata, blocksperint); /* Calculate the averages and standard deviations */ /* for each point in time. */ if (padding) { dataavg[ii][jj] = 0.0; datastd[ii][jj] = 0.0; datapow[ii][jj] = 1.0; } else { avg_var(chandata, ptsperint, &davg, &dvar); dataavg[ii][jj] = davg; datastd[ii][jj] = sqrt(dvar); realfft(chandata, ptsperint, -1); numcands = 0; norm = datastd[ii][jj] * datastd[ii][jj] * ptsperint; if (norm == 0.0) norm = (chandata[0] == 0.0) ? 1.0 : chandata[0]; cands = search_fft((fcomplex *) chandata, ptsperint / 2, lobin, ptsperint / 2, harmsum, numbetween, interptype, norm, cmd->freqsigma, &numcands, &powavg, &powstd, &powmax); datapow[ii][jj] = powmax; /* Record the birdies */ if (numcands) { for (kk = 0; kk < numcands; kk++) { freq = cands[kk].r / inttime; candnum = find_rfi(rfivect, numrfi, freq, RFI_FRACTERROR); if (candnum >= 0) { update_rfi(rfivect + candnum, freq, cands[kk].sig, jj, ii); } else { update_rfi(rfivect + numrfi, freq, cands[kk].sig, jj, ii); numrfi++; if (numrfi == numrfivect) { numrfivect *= 2; rfivect = rfi_vector(rfivect, numchan, numint, numrfivect / 2, numrfivect); } } } free(cands); } } } } printf("\rAmount Complete = 100%%\n"); /* Write the data to the output files */ write_rfifile(rfifilenm, rfivect, numrfi, numchan, numint, ptsperint, lobin, numbetween, harmsum, fracterror, cmd->freqsigma); write_statsfile(statsfilenm, datapow[0], dataavg[0], datastd[0], numchan, numint, ptsperint, lobin, numbetween); } else { /* If "-nocompute" */ float freqsigma; /* Read the data from the output files */ printf("Reading RFI data from '%s'.\n", rfifilenm); printf("Reading statistics from '%s'.\n", statsfilenm); readinf(&idata, outfilenm); read_rfifile(rfifilenm, &rfivect, &numrfi, &numchan, &numint, &ptsperint, &lobin, &numbetween, &harmsum, &fracterror, &freqsigma); numrfivect = numrfi; read_statsfile(statsfilenm, &datapow, &dataavg, &datastd, &numchan, &numint, &ptsperint, &lobin, &numbetween); bytemask = gen_bmatrix(numint, numchan); printf("Reading bytemask from '%s'.\n\n", bytemaskfilenm); bytemaskfile = chkfopen(bytemaskfilenm, "rb"); chkfread(bytemask[0], numint * numchan, 1, bytemaskfile); fclose(bytemaskfile); for (ii = 0; ii < numint; ii++) for (jj = 0; jj < numchan; jj++) bytemask[ii][jj] &= PADDING; /* Clear all but the PADDING bits */ inttime = ptsperint * idata.dt; } /* Make the plots and set the mask */ { int *zapints, *zapchan; int numzapints = 0, numzapchan = 0; if (cmd->zapintsstrP) { zapints = ranges_to_ivect(cmd->zapintsstr, 0, numint - 1, &numzapints); zapints = (int *) realloc(zapints, (size_t) (sizeof(int) * numint)); } else { zapints = gen_ivect(numint); } if (cmd->zapchanstrP) { zapchan = ranges_to_ivect(cmd->zapchanstr, 0, numchan - 1, &numzapchan); zapchan = (int *) realloc(zapchan, (size_t) (sizeof(int) * numchan)); } else { zapchan = gen_ivect(numchan); } rfifind_plot(numchan, numint, ptsperint, cmd->timesigma, cmd->freqsigma, cmd->inttrigfrac, cmd->chantrigfrac, dataavg, datastd, datapow, zapchan, numzapchan, zapints, numzapints, &idata, bytemask, &oldmask, &newmask, rfivect, numrfi, cmd->rfixwinP, cmd->rfipsP, cmd->xwinP); vect_free(zapints); vect_free(zapchan); } /* Write the new mask and bytemask to the file */ write_mask(maskfilenm, &newmask); bytemaskfile = chkfopen(bytemaskfilenm, "wb"); chkfwrite(bytemask[0], numint * numchan, 1, bytemaskfile); fclose(bytemaskfile); /* Determine the percent of good and bad data */ { int numpad = 0, numbad = 0, numgood = 0; for (ii = 0; ii < numint; ii++) { for (jj = 0; jj < numchan; jj++) { if (bytemask[ii][jj] == GOODDATA) { numgood++; } else { if (bytemask[ii][jj] & PADDING) numpad++; else numbad++; } } } printf("\nTotal number of intervals in the data: %d\n\n", numint * numchan); printf(" Number of padded intervals: %7d (%6.3f%%)\n", numpad, (float) numpad / (float) (numint * numchan) * 100.0); printf(" Number of good intervals: %7d (%6.3f%%)\n", numgood, (float) numgood / (float) (numint * numchan) * 100.0); printf(" Number of bad intervals: %7d (%6.3f%%)\n\n", numbad, (float) numbad / (float) (numint * numchan) * 100.0); qsort(rfivect, numrfi, sizeof(rfi), compare_rfi_sigma); printf(" Ten most significant birdies:\n"); printf("# Sigma Period(ms) Freq(Hz) Number \n"); printf("----------------------------------------------------\n"); for (ii = 0; ii < 10; ii++) { double pperr; char temp1[40], temp2[40]; if (rfivect[ii].freq_var == 0.0) { pperr = 0.0; sprintf(temp1, " %-14g", rfivect[ii].freq_avg); sprintf(temp2, " %-14g", 1000.0 / rfivect[ii].freq_avg); } else { pperr = 1000.0 * sqrt(rfivect[ii].freq_var) / (rfivect[ii].freq_avg * rfivect[ii].freq_avg); nice_output_2(temp1, rfivect[ii].freq_avg, sqrt(rfivect[ii].freq_var), -15); nice_output_2(temp2, 1000.0 / rfivect[ii].freq_avg, pperr, -15); } printf("%-2d %-8.2f %13s %13s %-8d\n", ii + 1, rfivect[ii].sigma_avg, temp2, temp1, rfivect[ii].numobs); } qsort(rfivect, numrfi, sizeof(rfi), compare_rfi_numobs); printf("\n Ten most numerous birdies:\n"); printf("# Number Period(ms) Freq(Hz) Sigma \n"); printf("----------------------------------------------------\n"); for (ii = 0; ii < 10; ii++) { double pperr; char temp1[40], temp2[40]; if (rfivect[ii].freq_var == 0.0) { pperr = 0.0; sprintf(temp1, " %-14g", rfivect[ii].freq_avg); sprintf(temp2, " %-14g", 1000.0 / rfivect[ii].freq_avg); } else { pperr = 1000.0 * sqrt(rfivect[ii].freq_var) / (rfivect[ii].freq_avg * rfivect[ii].freq_avg); nice_output_2(temp1, rfivect[ii].freq_avg, sqrt(rfivect[ii].freq_var), -15); nice_output_2(temp2, 1000.0 / rfivect[ii].freq_avg, pperr, -15); } printf("%-2d %-8d %13s %13s %-8.2f\n", ii + 1, rfivect[ii].numobs, temp2, temp1, rfivect[ii].sigma_avg); } printf("\nDone.\n\n"); } /* Close the files and cleanup */ free_rfi_vector(rfivect, numrfivect); free_mask(newmask); if (cmd->maskfileP) free_mask(oldmask); free(outfilenm); free(statsfilenm); free(bytemaskfilenm); free(maskfilenm); free(rfifilenm); vect_free(dataavg[0]); vect_free(dataavg); vect_free(datastd[0]); vect_free(datastd); vect_free(datapow[0]); vect_free(datapow); vect_free(bytemask[0]); vect_free(bytemask); if (!cmd->nocomputeP) { // Close all the raw files and free their vectors close_rawfiles(&s); vect_free(chandata); if (insubs) vect_free(srawdata); else vect_free(rawdata); } return (0); }
int main(int argc, char **argv) { int n_poly=0, poly_set_used; double mjd, HoursElapsed, RefPhase, RefFreq, RefPeriod; char tempo_cmd[256]; struct Polyco Polyco[MAX_PC_SETS]; Cmdline *Cmd; /* Get command line variables */ Cmd = parseCmdline(argc, argv); /* showOptionValues(); */ /* Normally use this somewhere, and not showOptionValues */ Cmd->tool = Cmd->tool; /* Store program name */ //strcpy(ProgName,argv[0]); /* Make sure parfile exists */ if(FileExists(Cmd->ParFile)){ if (Cmd->VerboseP) printf("Creating polycos for PSR %s from input parameter file %s.\n", Cmd->PSRName, Cmd->ParFile); } else{ fprintf(stderr,"Could not open file %s.\n", Cmd->ParFile); return -1; } /* If we made it this far, we have a par file we can now use to create a polyco file. */ /* Open final output file and close right away to create it -- sort of like a "touch" command, in effect */ /* if((Fpoly=fopen("poly_final.dat", "w"))==NULL){ fprintf(stderr,"Could not open file poly_final.dat. \n"); return -1; } fclose(Fpoly); */ /* Now create polycos for the observing times, timespan of validity, and observing frequency specified by user */ sprintf(tempo_cmd, "tempo -f %s -Zpsr=%s -Zfreq=%.3lf -Zstart=%.3lf -Ztobsh=%.2lf -Zspan=%d -Zsite=%s", Cmd->ParFile, Cmd->PSRName, Cmd->ObsFreq, /* Start polyco set to be 1 hour early, (and so end 1 hour later) */ Cmd->MJDStart-0.04 /* start one hour early */, /* allow an extra 2 hours of polycos */ Cmd->TObsHours + 2., /* End two hour later */ /* default at 30 minutes valid span */ Cmd->NSpan, Cmd->Site); if (Cmd->VerboseP) printf("tempo command:\n%s\n\n\n", tempo_cmd); system(tempo_cmd); /* Polyco file is now made. Now read it in and save into Polyco array*/ char testchar[16], testfile[32]; double testfreq, testmjd; strcpy(testfile, "polyco.dat"); strcpy(testchar, Cmd->PSRName); testfreq = Cmd->ObsFreq; testmjd = Cmd->MJDStart; if (Cmd->VerboseP) { printf("Time step = %.2lf minutes = %.4lf hours = %.5lf days\n", Cmd->TimeStep, Cmd->TimeStep/60.0, Cmd->TimeStep/1440.);fflush(stdout); printf("MJDStart = %.4lf, TObsHours = %.3lf\n", Cmd->MJDStart, Cmd->TObsHours);fflush(stdout); } if((n_poly=GetPoly("polyco.dat", Cmd->PSRName, &Polyco[0], Cmd->ObsFreq, Cmd->MJDStart)) < 1) { printf("Could not find polycos for all input parameters from \n"); printf("par file %s. Exiting...\n", Cmd->ParFile); exit(1); } printf("# MJD Hours RefPhase RefFreq RefPeriod\n"); /* Run loop over dates and get out PhaseCalc info */ for (mjd=Cmd->MJDStart; mjd<(Cmd->MJDStart + Cmd->TObsHours/24.0); mjd+=Cmd->TimeStep/1440.) { HoursElapsed = (mjd - Cmd->MJDStart)*24.0; /* Run PhaseCalc */ if((poly_set_used = PhaseCalc(Polyco, n_poly, floor(mjd), (mjd-floor(mjd)), &RefPhase, &RefFreq)) < 0){ printf("Could not find any polyco sets to match MJD. Exiting...\n"); exit(2); } if(Cmd->MilliSecsP) RefPeriod = 1000./RefFreq; else RefPeriod = 1.0/RefFreq; /* Display PhaseCalc output info */ printf("%.6lf %.4lf %lf %lf %.8lf\n", mjd, HoursElapsed, RefPhase, RefFreq, RefPeriod); } exit(0); }