void switch_output_file(struct lib_ccx_ctx *ctx, struct encoder_ctx *enc_ctx, int track_id) { if (enc_ctx->out->filename != NULL) { // Close and release the previous handle free(enc_ctx->out->filename); close(enc_ctx->out->fh); } char *ext = get_file_extension(ctx->write_format); char suffix[32]; sprintf(suffix, "_%d", track_id); enc_ctx->out->filename = create_outfilename(get_basename(enc_ctx->first_input_file), suffix, ext); enc_ctx->out->fh = open(enc_ctx->out->filename, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, S_IREAD | S_IWRITE); // Reset counters as we switch output file. enc_ctx->cea_708_counter = 0; enc_ctx->srt_counter = 0; }
int main(int argc, char * argv[]) { header_param_list Ihpar, Qhpar, Uhpar, Vhpar; header_param_list Iouthpar, Qouthpar, Uouthpar, Vouthpar; FILE *Ifile, *Qfile, *Ufile, *Vfile, *datfile; FILE *Ioutfile, *Qoutfile, *Uoutfile, *Voutfile; char Ioutfilename[128], Qoutfilename[128], Uoutfilename[128], Voutfilename[128]; char *Ifilename, *Qfilename, *Ufilename, *Vfilename; int num_planes, num_points; int i; char *datfilename; float *Iplane, *Qplane, *Uplane, *Vplane; if (argc != 6) { printf("usage: %s <I.fits> <Q.fits> <U.fits> <V.fits> <empirical.dat>\n", argv[0]); return EXIT_FAILURE; } Ifilename = argv[1]; Qfilename = argv[2]; Ufilename = argv[3]; Vfilename = argv[4]; datfilename = argv[5]; //open files for reading Ifile = fopen(Ifilename, "r"); Qfile = fopen(Qfilename, "r"); Ufile = fopen(Ufilename, "r"); Vfile = fopen(Vfilename, "r"); datfile = fopen(datfilename, "r"); if (Ifile == NULL || Qfile == NULL || Ufile == NULL || Vfile == NULL || datfile == NULL) { printf("ERROR: unable to open input file '%s' for reading\n", Ifilename); return EXIT_FAILURE; } //open files for writing create_outfilename(Ioutfilename, Ifilename); Ioutfile = fopen(Ioutfilename, "w"); create_outfilename(Qoutfilename, Qfilename); Qoutfile = fopen(Qoutfilename, "w"); create_outfilename(Uoutfilename, Ufilename); Uoutfile = fopen(Uoutfilename, "w"); create_outfilename(Voutfilename, Vfilename); Voutfile = fopen(Voutfilename, "w"); if (Ioutfile == NULL || Qoutfile == NULL || Uoutfile == NULL || Voutfile == NULL) { printf("ERROR: unable to open output file '%s' for writing\n", Ioutfilename); return EXIT_FAILURE; } //read the headers readfits_header (Ifile, &Ihpar); readfits_header (Qfile, &Qhpar); readfits_header (Ufile, &Uhpar); readfits_header (Vfile, &Vhpar); if ((memcmp(Ihpar.naxis, Qhpar.naxis, 3) != 0) || (memcmp(Ihpar.naxis, Uhpar.naxis, 3) != 0) || (memcmp(Ihpar.naxis, Vhpar.naxis, 3) != 0) ) { printf("ERROR: naxis size mismatch between fits headers\n"); return EXIT_FAILURE; } num_points = Ihpar.naxis[0] * Ihpar.naxis[1]; num_planes = Ihpar.naxis[2]; printf("num_points: %i\n", num_points); printf("num_planes: %i\n", num_planes); Iplane = malloc(sizeof(float) * num_points); Qplane = malloc(sizeof(float) * num_points); Uplane = malloc(sizeof(float) * num_points); Vplane = malloc(sizeof(float) * num_points); Iouthpar = Ihpar; Qouthpar = Qhpar; Uouthpar = Uhpar; Vouthpar = Vhpar; strcat(Iouthpar.object, " Empirical"); strcat(Qouthpar.object, " Empirical"); strcat(Uouthpar.object, " Empirical"); strcat(Vouthpar.object, " Empirical"); writefits_header(Ioutfile, Iouthpar); writefits_header(Qoutfile, Qouthpar); writefits_header(Uoutfile, Uouthpar); writefits_header(Voutfile, Vouthpar); for (i=0; i<num_planes; i++) { int j, ix; float deltaInot, deltaQnot, deltaUnot, deltaVnot; float Inot, Qnot, Unot, Vnot; float mI, mQ, mU, mV; float Imax, Qmax, Umax, Vmax; //read a line from the empirical.dat file fscanf(datfile, "%d %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f\n", &ix, &mQ, &mU, &mV, &Inot, &Qnot, &Unot, &Vnot, &deltaInot, &deltaQnot, &deltaUnot, &deltaVnot, &Imax, &Qmax, &Umax, &Vmax); printf("mQ: %f mU: %f mV: %f\n", mQ, mU, mV); readfits_plane (Ifile, Iplane, &Ihpar); readfits_plane (Qfile, Qplane, &Qhpar); readfits_plane (Ufile, Uplane, &Uhpar); readfits_plane (Vfile, Vplane, &Vhpar); //do the actual corrections for (j=0; j<num_points; j++) { Iplane[j] = Iplane[j] - Inot; Qplane[j] = Qplane[j] - Qnot - (mQ * Iplane[j]); Uplane[j] = Uplane[j] - Unot - (mU * Iplane[j]); Vplane[j] = Vplane[j] - Vnot - (mV * Iplane[j]); } writefits_plane(Ioutfile, Iplane, &Iouthpar); writefits_plane(Qoutfile, Qplane, &Qouthpar); writefits_plane(Uoutfile, Uplane, &Uouthpar); writefits_plane(Voutfile, Vplane, &Vouthpar); } fclose(Ifile); fclose(Qfile); fclose(Ufile); fclose(Vfile); fclose(datfile); free(Iplane); free(Qplane); free(Uplane); free(Vplane); writefits_pad_end(Ioutfile, Iouthpar); writefits_pad_end(Qoutfile, Qouthpar); writefits_pad_end(Uoutfile, Uouthpar); writefits_pad_end(Voutfile, Vouthpar); fclose(Ioutfile); fclose(Qoutfile); fclose(Uoutfile); fclose(Voutfile); }
static int init_output_ctx(struct encoder_ctx *ctx, struct encoder_cfg *cfg) { int ret = EXIT_OK; int nb_lang; char *basefilename = NULL; // Input filename without the extension char *extension = NULL; // Input filename without the extension #define check_ret(filename) if (ret != EXIT_OK) \ { \ print_error(cfg->gui_mode_reports,"Failed %s\n", filename); \ return ret; \ } if (cfg->cc_to_stdout == CCX_FALSE && cfg->send_to_srv == CCX_FALSE && cfg->extract == 12) nb_lang = 2; else nb_lang = 1; ctx->out = malloc(sizeof(struct ccx_s_write) * nb_lang); if(!ctx->out) return -1; ctx->nb_out = nb_lang; if(cfg->cc_to_stdout == CCX_FALSE && cfg->send_to_srv == CCX_FALSE) { if (cfg->output_filename != NULL) { // Use the given output file name for the field specified by // the -1, -2 switch. If -12 is used, the filename is used for // field 1. if (cfg->extract == 12) { basefilename = get_basename(cfg->output_filename); extension = get_file_extension(cfg->write_format); ret = init_write(&ctx->out[0], strdup(cfg->output_filename)); check_ret(cfg->output_filename); ret = init_write(&ctx->out[1], create_outfilename(basefilename, "_2", extension)); check_ret(ctx->out[1].filename); } else if (cfg->extract == 1) { ret = init_write(ctx->out, strdup(cfg->output_filename)); check_ret(cfg->output_filename); } else { ret = init_write(ctx->out, strdup(cfg->output_filename)); check_ret(cfg->output_filename); } } else if (cfg->write_format != CCX_OF_NULL) { basefilename = get_basename(ctx->first_input_file); extension = get_file_extension(cfg->write_format); if (cfg->extract == 12) { ret = init_write(&ctx->out[0], create_outfilename(basefilename, "_1", extension)); check_ret(ctx->out[0].filename); ret = init_write(&ctx->out[1], create_outfilename(basefilename, "_2", extension)); check_ret(ctx->out[1].filename); } else if (cfg->extract == 1) { ret = init_write(ctx->out, create_outfilename(basefilename, NULL, extension)); check_ret(ctx->out->filename); } else { ret = init_write(ctx->out, create_outfilename(basefilename, NULL, extension)); check_ret(ctx->out->filename); } } freep(&basefilename); freep(&extension); } if (cfg->cc_to_stdout == CCX_TRUE) { ctx->out[0].fh = STDOUT_FILENO; ctx->out[0].filename = NULL; mprint ("Sending captions to stdout.\n"); } if (cfg->send_to_srv == CCX_TRUE) { ctx->out[0].fh = -1; ctx->out[0].filename = NULL; connect_to_srv(ccx_options.srv_addr, ccx_options.srv_port, ccx_options.tcp_desc, ccx_options.tcp_password); } if (cfg->dtvcc_extract) { for (int i = 0; i < CCX_DTVCC_MAX_SERVICES; i++) { if (!cfg->services_enabled[i]) { ctx->dtvcc_writers[i].fd = -1; ctx->dtvcc_writers[i].filename = NULL; ctx->dtvcc_writers[i].cd = (iconv_t) -1; continue; } if (cfg->cc_to_stdout) { ctx->dtvcc_writers[i].fd = STDOUT_FILENO; ctx->dtvcc_writers[i].filename = NULL; ctx->dtvcc_writers[i].cd = (iconv_t) -1; } else { if (cfg->output_filename) basefilename = get_basename(cfg->output_filename); else basefilename = get_basename(ctx->first_input_file); ccx_dtvcc_writer_init(&ctx->dtvcc_writers[i], basefilename, ctx->program_number, i + 1, cfg->write_format, cfg); free(basefilename); } } } if(ret) { print_error(cfg->gui_mode_reports, "Output filename is same as one of input filenames. Check output parameters.\n"); return CCX_COMMON_EXIT_FILE_CREATION_FAILED; } return EXIT_OK; }