コード例 #1
0
ファイル: main.c プロジェクト: deadbok/aMOS
int main(void)
{
	printstr("OK\n");
	if (init_video())
	{
		return(1);
	}

	printstr("    Resetting disk...");
	if (print_error(disk_reset(loader_setup.boot_drive), disk_errors, N_DISK_ERRORS, TRUE))
	{
		return(1);
	}

	printstr("    Getting disk info...");
	if (print_error(disk_info_standard(loader_setup.boot_drive,
									   &disk_geometry.cylinder,
									   &disk_geometry.head,
									   &disk_geometry.sector),
					 disk_errors, N_DISK_ERRORS, TRUE))
	{
		return(1);
	}

	printstr("...OK\n");

	printstr("Loading HIKernel (");
	putl(loader_setup.kernel_sectors, 10);
	printstr(" sectors)...\n");

	if (print_error(read_kernel(), disk_errors, N_DISK_ERRORS, TRUE))
	{
		return(1);
	}

	printstr("Killing floppy motor...\n");
	stop_floppy();

	printstr("Starting the BOSS...");

	engage();

	update_cursor();
	printstr("\nBack in the aMOS loader\n");
	printstr("Can't play that funky music, giving it up\n");
	printstr("Let down and hanging around\n");
	for (;;) {}

	return(0);
}
コード例 #2
0
ファイル: read_xml.c プロジェクト: UPD-RTS/TiCOS-CodeGen
// read the xml file
int read_xml(){
	
	init_config();

	debug_printf ("Reading hardware element...\n");	

	// read the hardaware element
	if (read_hardware() != 0){
		return -1;	
	}
	debug_printf ("OK\n");	

	// read the kernel element	
	if (read_kernel() != 0){
		return -1;	
	}

	// read the ports element
	if (read_ports() != 0){
		return -1;	
	}

	// read the partitions element
	if (read_partitions() != 0){
		return -1;	
	}

	// check if every port has been assigned to a partition
	int i;
	for (i=0; i<nb_ports; i++){
		if (ports[i].partition_id == -1){
			printf("WARNING: port %d has not been assigned to any partition!\n",ports[i].user_port_id);
		}	
	}

	return 0;

} 
コード例 #3
0
int
main (int argc, char* argv[]) 
{
	int fd, kernel_fd = -1;
	
	/* scanning whether there's -h or --help in the options */
	char c, *unit;
	while ((c = getopt_long(argc, argv, ":skmdx:", longopts, 0)) != -1) {
		switch (c) {
		case 'h':
			printf ("  -d <path>, --disk=<path>    specify normal disk\n"
					"  -k <path>, --kernel=<path>  specify kernel file\n"
					"  -m NUM   , --memsize=NUM    specify memory size\n"
					"  -s <path>, --swap=<path>    specify swap disk\n"
					"  -h, --help                  print this message and exit\n");
			return 0;
			break;
		case 'k':
			if ((kernel_fd = open (optarg, O_RDONLY)) < 0)
				return 1;
			break;
		case 'm':
			memsize = strtol (optarg, &unit, 10);
			if (*unit == 'K' || *unit == 'k')
				memsize <<= 10;
			else if (*unit == 'M' || *unit == 'm')
				memsize <<= 20;
			break;
		default:
			/* ignore other options now */
			break;
		}
	}
	if (kernel_fd < 0) {
		printf ("No kernel file specified.\n");
		return 1;
	}
	
	struct stat buf;
	fstat (kernel_fd, &buf);
	
	fd = create_mem_file (memsize);

	/* map the temp file & copy boot and kernel code to the memory */
	int brk_end = (int)sbrk (0);
	mmap ((void*)brk_end, memsize, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_SHARED, fd, 0);

	read_kernel (kernel_fd, brk_end - KERNBASE);

	/* map the reserved part to where it really should be */
	mmap ((void*)KERNBASE, RESERVED_SIZE, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_SHARED, fd, 0);

	/* Now we have access to ginfo normally. */
	ginfo->mem_fd = fd;
	e820_init (kernel_fd);
	parse_opts (argc, argv);

	/* unmap useless page table entries */
	memcpy ((void*)(BOOT_CODE), (void*)&__boot_start, PGSIZE);
	stk_init (brk_end, fd);
	
	void (*boot_start) (void);
	boot_start = __pa (__boot_entry);
	boot_start ();

	return 0;
}
コード例 #4
0
ファイル: kdump.c プロジェクト: ant4g0nist/ios-kern-utils
int main()
{
    kern_return_t ret;
    task_t kernel_task;
    vm_address_t kbase;
    unsigned char buf[HEADER_SIZE];      // will hold the original mach-o header and load commands
    unsigned char header[HEADER_SIZE];   // header for the new mach-o file
    unsigned char* binary;               // mach-o will be reconstructed in here
    FILE* f;
    size_t filesize = 0;
#if __LP64__
    struct segment_command_64* seg;
    struct mach_header_64* orig_hdr = (struct mach_header_64*)buf;
    struct mach_header_64* hdr = (struct mach_header_64*)header;
#else
    struct segment_command* seg;
    struct mach_header* orig_hdr = (struct mach_header*)buf;
    struct mach_header* hdr = (struct mach_header*)header;
#endif

    memset(header, 0, HEADER_SIZE);

    ret = task_for_pid(mach_task_self(), 0, &kernel_task);
    if (ret != KERN_SUCCESS) {
        printf("[!] failed to get access to the kernel task");
        return -1;
    }

    if ((kbase = get_kernel_base()) == 0) {
        printf("[!] could not find kernel base address\n");
        return -1;
    }
    printf("[*] found kernel base at address 0x" ADDR "\n", kbase);

    f = fopen("kernel.bin", "wb");
    binary = calloc(1, KERNEL_SIZE);            // too large for the stack

    printf("[*] reading kernel header...\n");
    read_kernel(kbase, HEADER_SIZE, buf);
    memcpy(hdr, orig_hdr, sizeof(*hdr));
    hdr->ncmds = 0;
    hdr->sizeofcmds = 0;

    /*
     * We now have the mach-o header with the LC_SEGMENT
     * load commands in it.
     * Next we are going to redo the loading process,
     * parse each load command and read the data from
     * vmaddr into fileoff.
     * Some parts of the mach-o can not be restored (e.g. LC_SYMTAB).
     * The load commands for these parts will be removed from the final
     * executable.
     */
    printf("[*] restoring segments...\n");
    CMD_ITERATE(orig_hdr, cmd) {
        switch(cmd->cmd) {
        case LC_SEGMENT:
        case LC_SEGMENT_64: {
            #if __LP64__
            seg = (struct segment_command_64*)cmd;
            #else
            seg = (struct segment_command*)cmd;
            #endif
            printf("[+] found segment %s\n", seg->segname);
            read_kernel(seg->vmaddr, seg->filesize, binary + seg->fileoff);
            filesize = max(filesize, seg->fileoff + seg->filesize);
        }
        case LC_UUID:
        case LC_UNIXTHREAD:
        case 0x25:
        case 0x2a:
        case 0x26:
            memcpy(header + sizeof(*hdr) + hdr->sizeofcmds, cmd, cmd->cmdsize);
            hdr->sizeofcmds += cmd->cmdsize;
            hdr->ncmds++;
            break;
        }
    }

    // now replace the old header with the new one ...
    memcpy(binary, header, sizeof(*hdr) + orig_hdr->sizeofcmds);

    // ... and write the final binary to file
    fwrite(binary, filesize, 1, f);

    printf("[*] done, wrote 0x%lx bytes\n", filesize);
    fclose(f);
    free(binary);
    return 0;
}
コード例 #5
0
ファイル: main.c プロジェクト: dlwh/cs252
int main (int argc, const char * argv[]) {
/*	for(int single=-1; single<3; single++){
		for(int pair=-2; pair<4; pair++){
			measure_loop(4, 4, 100, 300, single, pair);
		}
	}
*/	
/*	for(int k=3; k<500; k++){
		printf("%d\n", k);
		measure_loop(k, k, 10, 1000, 2, 3);
		measure_loop(k, k, 10, 1000, 2, -2);
		measure_loop(k, k, 10, 1000, -1, 1);
	}
*/	
//	measure_loop(630, 630, 10, 300, 2, -2);
	
	int gpu = 1;
	/*------------------------------------------------------------------------------------------*/
	cl_device_id device_id;
	
	int err = clGetDeviceIDs(NULL, gpu ? CL_DEVICE_TYPE_GPU : CL_DEVICE_TYPE_CPU, 1, &device_id, NULL);
	if (err != CL_SUCCESS){
		printf("Error: Failed to create a device group!\n");
		return EXIT_FAILURE;
	}
	
	cl_context context = clCreateContext(0, 1, &device_id, NULL, NULL, &err);
	if (!context){
		printf("Error: Failed to create a compute context!\n");
		return EXIT_FAILURE;
	}
	
	char* KernelSource=read_kernel("kernel.cl");
	
	
	cl_command_queue commands = clCreateCommandQueue(context, device_id, 0, &err);
	if (!commands){
		printf("Error: Failed to create a command commands!\n");
		printf("%i %i\n", CL_INVALID_VALUE, err);
		return EXIT_FAILURE;
	}
	
	
	cl_program program = clCreateProgramWithSource(context, 1, (const char **) & KernelSource, NULL, &err);
	if (!program){
		printf("Error: Failed to create compute program!\n");
		return EXIT_FAILURE;
	}
	
	err = clBuildProgram(program, 0, NULL, NULL, NULL, NULL);
	if (err != CL_SUCCESS){
		size_t len;
		char buffer[2048];
		
		printf("Error: Failed to build program executable!\n");
		clGetProgramBuildInfo(program, device_id, CL_PROGRAM_BUILD_LOG, sizeof(buffer), buffer, &len);
		printf("%s\n", buffer);
		return EXIT_FAILURE;
	}
	
	cl_kernel kernelInf = clCreateKernel(program, "updateFactor", &err);
	if (!kernelInf || err != CL_SUCCESS){
		printf("Error: Failed to create compute kernel!\n");
		return 1;
	}
	cl_kernel kernelMarg = clCreateKernel(program, "updateMarginals", &err);
	if (!kernelMarg || err != CL_SUCCESS){
		printf("Error: Failed to create compute kernel marg!\n");
		return 1;
	}
	/*---------------------------------------------------------------------------------------*/
	
	
	for(int i=3; i<500; i++){
		//int s = (int) 100*pow(10, i/25.);
		//int s=i;
		
		//printf("%d %d\n", i, s);
		//measure_loop(s, s, 10, 100, 0, 0);
		//measure_loop(s, s, 10, 100, 2, -2);
		
		printf("[%d, ", i);
		measure_time(kernelInf, kernelMarg, commands, context, device_id, i, i, 2+ceil(900/(i*i)));
	}
}
コード例 #6
0
ファイル: phot.c プロジェクト: krzul/dia
/*--------------------------------------------------------*/
int main(int argc, char *argv[])
{
        char        **newheader,
                    *imffname, *parfname, *psffname, *reffname,
                    *instrname,
                    *fieldname,
                    *outfname,
                    *coofname,
                    **diffiles,
                    **imfiles,
                    **kerfiles;
        int         nx0, ny0, k, *sector, nim, iim, npsf, ipsf, hsize,
                    isec_x, isec_y, *cx, *cy, psfn, kern, irad,
//                  ofs,
                    *vnum,
                    i;
        float       *im, *difim, *refim;
        double      **wxy, *x, *y, *xs, *ys, **psfs, *psfim, *kerim, ratio;
        STAR        **obj, *objp;
        PSF_STRUCT  psf;
        KER_STRUCT  ker;
        PAR_STRUCT  par;

/*** IO stuff ***/

  if (argc != 7)
  {
    printf("\n\tUSAGE: phot  parameter_file instrument_file");
    printf(" ref_image psf_fit_file image_data_list field_name\n");
    exit(1);
  }

  parfname= argv[1];
  instrname=argv[2];
  reffname= argv[3];
  psffname= argv[4];
  imffname= argv[5];
  fieldname=argv[6];

  get_params(parfname, instrname, &par);

  if (par.verbose)
    printf("\n\n*** Profile photometry with variable PSF and kernel ***\n\n");

  if (!(outfname=(char *)calloc(strlen(fieldname)+5, sizeof(char))))
    errmess("calloc(outfname)");
  strcpy(outfname, fieldname);
  strcat(outfname, ".db");

  if (!(coofname=(char *)calloc(strlen(fieldname)+5, sizeof(char))))
    errmess("calloc(coofname)");
  strcpy(coofname, fieldname);
  strcat(coofname, ".coo");


/***  read filenames for difference images, images and kernel fits  ***/
  nim=readfnames(imffname, &diffiles, &imfiles, &kerfiles);
  if (par.verbose)  printf("%d images to process\n", nim);

/***  read coordinates of variables  ***/
  npsf=readcoo(coofname, &vnum, &x, &y);
  if (par.verbose)  printf("%d variables to measure\n\n", npsf);

/***  read in psf fit and get a sample kernel from the first image  ***/
  read_psf(psffname, &psf);
  read_kernel(kerfiles[0], &ker, 1, par.verbose);

  psf.normrad = par.normrad;
  psf.hw += ker.hw;

  psfn = 2*psf.hw + 1;
  kern = 2*ker.hw + 1;

/*** get memory ***/
  if (!(ker.vecs = (double **)malloc(ker.nvecs*sizeof(double *))))
    errmess("malloc(ker.vecs)");
  for (k=0; k<ker.nvecs; k++)
    if (!(ker.vecs[k] = (double *)malloc(kern*kern*sizeof(double))))
      errmess("malloc(ker.vecs[k])");

  if (!(sector=(int *)malloc(npsf*sizeof(int)))) errmess("malloc(sector)");
  if (!(cx= (int *)malloc(npsf*sizeof(int))))          errmess("malloc(cx)");
  if (!(cy= (int *)malloc(npsf*sizeof(int))))          errmess("malloc(cy)");
  if (!(xs= (double *)malloc(npsf*sizeof(double))))    errmess("malloc(xs)");
  if (!(ys= (double *)malloc(npsf*sizeof(double))))    errmess("malloc(ys)");
  if (!(wxy=(double **)malloc(npsf*sizeof(double *)))) errmess("malloc(wxy)");

  if (!(psfs=(double **)malloc(npsf*sizeof(double *))))
    errmess("malloc(psfs)");
  if (!(kerim=(double *)malloc(kern*kern*sizeof(double))))
    errmess("malloc(kerim)");
  if (!(psfim=(double *)malloc(psfn*psfn*sizeof(double))))
    errmess("malloc(psfim)");

  if (!(obj=(STAR **)malloc(npsf*sizeof(STAR *)))) errmess("malloc(obj)");

/***********************************************************************/
/** get things that can be done once for all: spatial coeffs and psfs **/
/***********************************************************************/

  for (ipsf=0; ipsf<npsf; ipsf++)
  {
    if (!(obj[ipsf]=(STAR *)malloc(nim*sizeof(STAR))))
      errmess("malloc(obj[ipsf])");
    if (!(wxy[ipsf]=(double *)malloc(ker.nwxy*sizeof(double))))
      errmess("malloc(wxy[ipsf])");
    if (!(psfs[ipsf]=(double *)malloc(psfn*psfn*sizeof(double))))
      errmess("malloc(psfs[ipsf])");

/* offsets for image sectors from kernel fit */

    cx[ipsf]=(int)floor(x[ipsf]+0.5);
    cy[ipsf]=(int)floor(y[ipsf]+0.5);

    isec_x=(cx[ipsf] - ker.hw)/(ker.nx - 2*ker.hw);
    isec_y=(cy[ipsf] - ker.hw)/(ker.ny - 2*ker.hw);

    xs[ipsf]=x[ipsf] - isec_x*(ker.nx - 2*ker.hw);
    ys[ipsf]=y[ipsf] - isec_y*(ker.ny - 2*ker.hw);

    sector[ipsf]=isec_y + ker.nsec_y*isec_x;

    spatial_coeffs(&ker, xs[ipsf], ys[ipsf], wxy[ipsf]);

    init_psf(&psf, x[ipsf], y[ipsf]);
    make_psf(&psf, x[ipsf], y[ipsf], cx[ipsf], cy[ipsf], psfs[ipsf]);
  }

  refim=read_FITS_2D1file(reffname, 's', &hsize, &newheader, &nx0, &ny0);
  for (i=0; i<hsize; i++) free(newheader[i]);
  free(newheader);

  make_vectors(&ker);

  par.nx0=nx0;
  par.ny0=ny0;
  par.psfhw=psf.hw;
  par.psfn=psfn;

  irad=(int)par.anrad2 + 2;

/*******************************/
/***  main loop over images  ***/
/*******************************/

  for (iim=0; iim<nim; iim++)
  {
    if (par.verbose > 2) printf("%d: %s\n", iim, diffiles[iim]);

    difim=read_FITS_2D1file(diffiles[iim], 's', &hsize, &newheader, &nx0, &ny0);

    for (i=0; i<hsize; i++) free(newheader[i]);
    free(newheader);

    if ((nx0 != par.nx0) || (ny0 != par.ny0))
    {
      printf("ERROR! phot: wrong size of the image %s\n", diffiles[iim]);
      exit(2);
    }

    im=read_FITS_2D1file(imfiles[iim], 's', &hsize, &newheader, &nx0, &ny0);
    for (i=0; i<hsize; i++) free(newheader[i]);
    free(newheader);

    if ((nx0 != par.nx0) || (ny0 != par.ny0))
    {
      printf("ERROR! phot: wrong size of the image %s\n", imfiles[iim]);
      exit(3);
    }


/* read kernel into tables allocated before */
    read_kernel(kerfiles[iim], &ker, 0, par.verbose);


/*** loop over stars ***/
    for (ipsf=0; ipsf<npsf; ipsf++)
    {
      objp = &obj[ipsf][iim];

      if ((cx[ipsf] < irad) || (cy[ipsf] < irad) ||
          (cx[ipsf] >= nx0-irad) || (cy[ipsf] >= ny0-irad))
      {
        if (par.verbose)
          printf("%s warning: object %4d too close to edge: ignored!\n",
                  diffiles[iim], ipsf);

        objp->a_flux   = par.bad_value;
        objp->a_err    = par.bad_value;
        objp->p_flux   = par.bad_value;
        objp->p_err    = par.bad_value;
        objp->chi2_n   = par.bad_value;
        objp->ker_chi2 = par.bad_value;
        objp->corr     = par.bad_value;
        objp->nbad     = -1;

        continue;
      }

/*** prepare local psf ***/
      make_kernel(&ker, wxy[ipsf], kerim, sector[ipsf]);

      im_convolve(psfs[ipsf], psfim, psfn, psfn, kerim, ker.hw);

      objp->ker_chi2=(float)ker.chi2_n[sector[ipsf]];

      objp->fwhm=(float)get_fwhm(psfim, x[ipsf], y[ipsf], cx[ipsf], cy[ipsf],
                                 &par, &ratio);

      if (par.bkg_mode) objp->bg = bkg(difim, cx[ipsf], cy[ipsf], &par);
      else              objp->bg = 0.0;

/*** actual profile and aperture photometry ***/
      get_phot(difim, im, refim, x[ipsf], y[ipsf], cx[ipsf], cy[ipsf], psfim,
               objp, &par);

      if (par.verbose > 1)
        printf("%s  star: %5d  flux= %9g +- %8g   nbad: %d\n",
                diffiles[iim], ipsf, objp->p_flux, objp->p_err, objp->nbad);
    }

    free(difim);
    free(im);
  }

/*** write photometry to the output file  ***/
  if (par.verbose) printf("\nWriting photometry to:  %s\n", outfname);

  if (par.dbf == 'A')
    writedba(outfname, diffiles, nim, npsf, vnum, obj);
  else
    writedbb(outfname, diffiles, nim, npsf, vnum, obj);

  if (par.verbose)  printf("\nPhotometry done!\n");

  return(0);
}
コード例 #7
0
ファイル: getvar.c プロジェクト: krzul/dia
/*--------------------------------------------------------*/
int main(int argc, char *argv[])
{
        char        **header,
                    *imffname, *parfname, *psffname, *reffname,
                    *instrname,
                    *fieldname,
                    *catname,
                    *coofname,
                    record[RECORD_LEN],
                    **diffiles,
                    **imfiles,
                    **kerfiles;
        int         nx0, ny0, k, nim, iim, hsize,
                    cx, cy, psfn, kern, irad, ofs, nobj1, nobj1_max, nobj2,
                    nobj2_max, i, j, flag, *index,
                    nvar;
        float       x0_off, y0_off, x_tmp, y_tmp, fwhm_limit, *fwhm,
                    **im, **difim, *refim, *varim1, *varim2, *corrim,
                    *tmpim;
        double      *wxy, x, y, *psfs, *psfim, *kerim, ratio;
        FILE        *outfcat, *outfcoo;
        STAR        *obj1, *obj2, *objp;
        PSF_STRUCT  psf;
        KER_STRUCT  ker;
        PAR_STRUCT  par;

/*** IO stuff ***/

  if (argc != 9) usage();

  parfname = argv[1];
  instrname= argv[2];
  reffname = argv[3];
  psffname = argv[4];
  imffname = argv[5];
  fieldname = argv[6]; 
  x0_off = atof(argv[7]);
  y0_off = atof(argv[8]);

    puts("sssssss");
  get_params(parfname, instrname, &par);
    puts("sssssss");

  if (!(catname=(char *)calloc(strlen(fieldname)+5, sizeof(char))))
    errmess("calloc(catname)");
  strcpy(catname, fieldname);   strcat(catname, ".cat");

  if (!(coofname=(char *)calloc(strlen(fieldname)+5, sizeof(char))))
    errmess("calloc(coofname)");
  strcpy(coofname, fieldname);  strcat(coofname, ".coo");

  if (par.verbose > 2)
  {
    printf("parfname = %s\n", parfname);
    printf("instrname= %s\n", instrname);
    printf("reffname = %s\n", reffname);
    printf("psffname = %s\n", psffname);
    printf("imffname = %s\n", imffname);
    printf("fieldname= %s\n", fieldname);
    printf("x0_off   = %g\n", x0_off);
    printf("y0_off   = %g\n", y0_off);
    printf("--------\n");
    printf("catname  = %s\n", catname);
    printf("coofname = %s\n", coofname);
    printf("--------\n");
  }

  nim=read_inp_list(imffname, &diffiles, &imfiles, &kerfiles);
  if (par.verbose)  printf("%s: %d images\n", imffname, nim);


//    printf("%s %s %s\n", diffiles[0],imfiles[0],kerfiles[0]);
//    printf("%s %s %s\n", diffiles[1],imfiles[1],kerfiles[1]);
//getchar();


/**********************************************************************/
/***  read in psf fit and get a sample kernel from the first image  ***/
/**********************************************************************/

  read_psf(psffname, &psf, par.verbose);

printf("%f %f %f\n",psf.ax, psf.ay, psf.vec[0]);

  read_kernel(kerfiles[0], &ker, 1, par.verbose);

printf("%f %d %f\n",*ker.sig, *ker.deg, *ker.vec[0]);


//getchar();


  psf.normrad = par.normrad;
  psf.hw += ker.hw;

  psfn = 2*psf.hw + 1;
  kern = 2*ker.hw + 1;

/*** get memory ***/
  if (!(ker.vecs = (double **)malloc(ker.nvecs*sizeof(double *))))
    errmess("malloc(ker.vecs)");
  for (k=0; k<ker.nvecs; k++)
    if (!(ker.vecs[k] = (double *)malloc(kern*kern*sizeof(double))))
      errmess("malloc(ker.vecs[k])");

  if (!(kerim=(double *)malloc(kern*kern*sizeof(double))))
    errmess("malloc(kerim)");
  if (!(psfim=(double *)malloc(psfn*psfn*sizeof(double))))
    errmess("malloc(psfim)");

  if (!(fwhm  = (float *)malloc(nim*sizeof(float)))) errmess("malloc(fwhm)");
  if (!(index = (int   *)malloc(nim*sizeof(int)))) errmess("malloc(index)");

/***********************************************************************/
/** get things that can be done once for all: spatial coeffs and psfs **/
/***********************************************************************/

  refim=read_FITS_2D1file(reffname, 's', &hsize, &header, &nx0, &ny0);
  for (i=0; i<hsize; i++) free(header[i]);
  free(header);

  par.nx0 = nx0;
  par.ny0 = ny0;
  par.psfhw = psf.hw;
  par.psfn  = psfn;

    printf("nx0: %d %d %d %d\n",nx0,ny0,psf.hw,psfn);

  irad  = (int)par.anrad2 + 2;

/*** get even more memory ***/
  if (!(psfs=(double *)malloc(psfn*psfn*sizeof(double))))
    errmess("malloc(psfs)");
  if (!(wxy=(double *)malloc(ker.nwxy*sizeof(double)))) errmess("malloc(wxy)");

  if (!(im   =(float **)malloc(nim*sizeof(float *)))) errmess("malloc(im)");
  if (!(difim=(float **)malloc(nim*sizeof(float *)))) errmess("malloc(difim)");

  if (!(varim1=(float *)malloc(nx0*ny0*sizeof(float))))
    errmess("malloc(varim1)");
  if (!(varim2=(float *)malloc(nx0*ny0*sizeof(float))))
    errmess("malloc(varim2)");
  if (!(corrim=(float *)malloc(nx0*ny0*sizeof(float))))
    errmess("malloc(corrim)");

  for (i=0; i<nx0*ny0; i++) varim1[i] = varim2[i] = 0.0;

/*** make reference psf and spatial coeffs for kernel ***/
  init_psf(&psf, (double)(nx0/2), (double)(ny0/2));
  make_psf(&psf, (double)(nx0/2), (double)(ny0/2), nx0/2, ny0/2, psfs);

  make_vectors(&ker);

  

  spatial_coeffs(&ker, (double)(ker.nx/2), (double)(ker.ny/2), wxy);

/*******************************/
/***  main loop over images  ***/
/*******************************/
    printf("MAIN:\n");

  for (iim=0; iim<nim; iim++)
  {
    read_kernel(kerfiles[iim], &ker, 0, par.verbose);

    make_kernel(&ker, wxy, kerim, 0);
    
    im_convolve(psfs, psfim, psfn, psfn, kerim, ker.hw);
    fwhm[iim] = get_fwhm(psfim, 0.0, 0.0, 0, 0, &par, &ratio);
  }

/*** reject bad seeing frames ***/

  quick_sort(fwhm, index, nim);

    printf("fwhm = %f\n",fwhm[0]);
    printf("fwhm = %f\n",fwhm[1]);

  fwhm_limit = fwhm[index[(int)(nim*par.fwhm_frac)]];

    printf("fwhmlim = %f\n",fwhm_limit);

  j = 0;
  for (iim=0; iim<nim; iim++)
  {
    if (fwhm[iim] <= fwhm_limit)
    {
      if (j != iim)
      {
        strcpy(diffiles[j], diffiles[iim]);
        strcpy( imfiles[j], imfiles[iim]);
        strcpy(kerfiles[j], kerfiles[iim]);
      }
      j++;
    }
  }

  nim = j;

  if (nim == 0)
  {
    printf("no images left ! (exit)\n");
    exit(2);
  }

/*** get all variability data at once ***/

  for (iim=0; iim<nim; iim++)
  {
    difim[iim]=read_FITS_2D1file(diffiles[iim], 's',
                                  &hsize, &header, &nx0, &ny0);

    //printf("difim = %f\n",difim[iim]);
                                  
    for (i=0; i<hsize; i++) free(header[i]);
    free(header);

    if ((nx0 != par.nx0) || (ny0 != par.ny0))
    {
      printf("ERROR! getvar: image %s has wrong size\n", diffiles[iim]);
      exit(3);
    }

    im[iim]=read_FITS_2D1file(imfiles[iim], 's', &hsize, &header, &nx0, &ny0);
    for (i=0; i<hsize; i++) free(header[i]);
    free(header);

    if ((nx0 != par.nx0) || (ny0 != par.ny0))
    {
      printf("ERROR! getvar: image %s has wrong size\n", imfiles[iim]);
      exit(4);
    }
  }

/*** prepare variability likelihood image ***/
  get_repeaters(difim, im, varim1, varim2, &par, nim);

/*** save variability images ***/
  if (!(tmpim=(float *)malloc(nx0*ny0*sizeof(float)))) errmess("malloc(tmpim)");

  memcpy(tmpim, varim1, nx0*ny0*sizeof(float));
  writevar2fits("var1.fits", nx0, ny0, tmpim);

  memcpy(tmpim, varim2, nx0*ny0*sizeof(float));
  writevar2fits("var2.fits", nx0, ny0, tmpim);

  free(tmpim);

/*** find stellar looking things ***/
  covar_sig(varim1, corrim, nx0, ny0, 0.0, par.mohw, psfs, par.psfhw);

  obj1 = find_stars(corrim, nx0, ny0, &nobj1, &nobj1_max, &par);
  if (par.verbose > 1) printf("find_stars -> %d variables of type 1\n", nobj1);

  center_stars(obj1, nobj1, difim, im, &par, nim);

/***/
  covar_sig(varim2, corrim, nx0, ny0, 0.0, par.mohw, psfs, par.psfhw);

  obj2 = find_stars(corrim, nx0, ny0, &nobj2, &nobj2_max, &par);
  if (par.verbose > 1) printf("find_stars -> %d variables of type 2\n", nobj2);

  center_stars(obj2, nobj2, difim, im, &par, nim);

/*** some stars may be found in both: take only one type and flag ***/
  cross_id(obj1, nobj1, obj2, nobj2, par.id_rad);

  if (par.verbose)
    printf("\nFound %d and %d candidates for variables of both types\n\n",
     nobj1, nobj2);

/*********************************************************************/
/***   write results to a binary file and x,y to a temporary file  ***/
/*********************************************************************/

  if (!(outfcoo = fopen(coofname, "w"))) errmess(coofname);
  if (!(outfcat = fopen(catname, "w"))) errmess(catname);

  free(coofname);
  free(catname);

  fseek(outfcat, 0, SEEK_END);

  ofs = sizeof(float);

/*** get sinusoidal variables out ***/
  if (par.verbose) printf("var1:\n num     X       Y      flux      flag\n");
  nvar=0;

  for (i=0; i<nobj1; i++)
  {
    if (par.verbose > 1) printf("i= %d\n", i);

    objp = &(obj1[i]);

    x = (double)(objp->x);
    y = (double)(objp->y);
    if (par.verbose > 1) printf("x= %g   y= %g\n", x, y);
    if ((x < par.bad_margin) || (x >= par.nx0 - par.bad_margin)) continue;
    if ((y < par.bad_margin) || (y >= par.ny0 - par.bad_margin)) continue;

    cx = objp->cx;
    cy = objp->cy;
    if (par.verbose > 1) printf("cx= %d   cy= %d\n", cx, cy);

/*** append to a binary file ***/
    if (objp->nframes > 0)
    {
      fprintf(outfcoo, "%5d  %11.5f  %11.5f\n", nvar, x, y);
      nvar++;

/*** get template photometry ***/
      init_psf(&psf, x, y);

      make_psf(&psf, x, y, cx, cy, psfs);

      objp->bg = bkg(refim, cx, cy, &par);

      get_phot(refim, refim, refim, x, y, cx, cy, psfs, objp, &par);

      flag = neighbor(refim, (int)x, (int)y, &par, objp->bg);

      if (par.verbose)
        printf("%4d  %9.3f  %9.3f  %9.1f  %5d\n", i, x, y, objp->p_flux, flag);

      x_tmp = (float)(x + x0_off);
      y_tmp = (float)(y + y0_off);

      if (par.verbose > 2)
      {
        printf("x_tmp= %g   y_tmp= %g\n", x_tmp, y_tmp);
        printf("ofs= %d   13*ofs= %d\n", ofs, 13*ofs);
      }

      memcpy(&record[ 0*ofs], &x_tmp        , ofs);
      memcpy(&record[ 1*ofs], &y_tmp        , ofs);
      memcpy(&record[ 2*ofs], &objp->p_flux , ofs);
      memcpy(&record[ 3*ofs], &objp->p_err  , ofs);
      memcpy(&record[ 4*ofs], &objp->a_flux , ofs);
      memcpy(&record[ 5*ofs], &objp->a_err  , ofs);
      memcpy(&record[ 6*ofs], &objp->bg     , ofs);
      memcpy(&record[ 7*ofs], &objp->chi2_n , ofs);
      memcpy(&record[ 8*ofs], &objp->corr   , ofs);
      memcpy(&record[ 9*ofs], &objp->nbad   , sizeof(int));
      memcpy(&record[10*ofs], &objp->vtype  , sizeof(int));
      memcpy(&record[11*ofs], &objp->nframes, sizeof(int));
      memcpy(&record[12*ofs], &flag         , sizeof(int));

      if (par.verbose > 2) printf("record prepared - writting...\n");
      fwrite(record, 1, sizeof(record), outfcat);
    }
  }

/*** the same for transients ***/
  if (par.verbose) printf("var2:\n num     X       Y      flux      flag\n");

  for (i=0; i<nobj2; i++)
  {
    if (par.verbose > 1) printf("i= %d\n", i);

    objp = &obj2[i];

    x = (double)(objp->x);
    y = (double)(objp->y);
    if (par.verbose > 1) printf("x= %g   y= %g\n", x, y);
    if ((x < par.bad_margin) || (x >= par.nx0 - par.bad_margin)) continue;
    if ((y < par.bad_margin) || (y >= par.ny0 - par.bad_margin)) continue;

    cx = objp->cx;
    cy = objp->cy;
    if (par.verbose > 1) printf("cx= %d   cy= %d\n", cx, cy);

/*** append to a binary file ***/
    if (objp->nframes > 0)
    {
      fprintf(outfcoo, "%5d  %11.5f  %11.5f\n", nvar, x, y);
      nvar++;

/*** get template photometry ***/
      init_psf(&psf, x, y);

      make_psf(&psf, x, y, cx, cy, psfs);

      obj2[i].bg = bkg(refim, cx, cy, &par);

      get_phot(refim, refim, refim, x, y, cx, cy, psfs, objp, &par);

      flag = neighbor(refim, (int)x, (int)y, &par, objp->bg);

      if (par.verbose)
        printf("%4d  %9.3f  %9.3f  %9.1f  %5d\n",
                i+nobj1, x, y, objp->p_flux, flag);

      x_tmp = (float)(x + x0_off);
      y_tmp = (float)(y + y0_off);

      if (par.verbose > 2)
      {
        printf("x_tmp= %g   y_tmp= %g\n", x_tmp, y_tmp);
        printf("ofs= %d   13*ofs= %d\n", ofs, 13*ofs);
      }

      memcpy(&record[ 0*ofs], &x_tmp        , ofs);
      memcpy(&record[ 1*ofs], &y_tmp        , ofs);
      memcpy(&record[ 2*ofs], &objp->p_flux , ofs);
      memcpy(&record[ 3*ofs], &objp->p_err  , ofs);
      memcpy(&record[ 4*ofs], &objp->a_flux , ofs);
      memcpy(&record[ 5*ofs], &objp->a_err  , ofs);
      memcpy(&record[ 6*ofs], &objp->bg     , ofs);
      memcpy(&record[ 7*ofs], &objp->chi2_n , ofs);
      memcpy(&record[ 8*ofs], &objp->corr   , ofs);
      memcpy(&record[ 9*ofs], &objp->nbad   , sizeof(int));
      memcpy(&record[10*ofs], &objp->vtype  , sizeof(int));
      memcpy(&record[11*ofs], &objp->nframes, sizeof(int));
      memcpy(&record[12*ofs], &flag         , sizeof(int));

      if (par.verbose > 2) printf("record prepared - writting...\n");
      fwrite(record, 1, sizeof(record), outfcat);
    }
  }

  fclose(outfcoo);
  fclose(outfcat);

  if (par.verbose)  printf("\nVariability seach fihished!\n");

  return(0);
}