Esempio n. 1
0
/* cc -g -xF -xsb -DDBXTESTING asmfuncs.c */
main()
{
    int len;
    struct sample_info      s;
    SAMPLE_INFO ns;
    FILE *info_file;
    char info[120];

    read_info_file(".");
    len = entryQ_length(".","GregAutoEntry");
    printf("len: %d\n",len);
    len = entryQ_length(".","/vxwks/greg/inova/nautoproc/GregAutoEntry");
    printf("len: %d\n",len);
     info_file = fopen("./GregAutoEntry","r");
    read_sample_info(info_file,&ns);
    fclose(info_file);
     info_file = fopen("./tmpentry","w");
    write_sample_info(info_file,&ns);
    fclose(info_file);
    len = get_sample_info (&ns,"DATA:",info,120,NULL);
    printf("DATA: '%s', len: %d\n",info,len);
    len = get_sample_info (&ns,"USERDIR:",info,120,NULL);
    printf("USERDIR: '%s', len: %d\n",info,len);
    len = get_sample_info (&ns,"STATUS:",info,120,NULL);
    printf("STATUS: '%s', len: %d\n",info,len);
    len = get_sample_info (&ns,"SAMPLE#:",info,120,NULL);
    printf("SAMPLE: '%s', len: %d\n",info,len);
    len = get_sample_info (&ns,"EOE:",info,120,NULL);
    printf("EOE: '%s', len: %d\n",info,len);

    update_sample_info(".","GregAutoEntry",
                            "DATA:", "/userdir/greg/auto/file1234" ,
                            "STATUS:", "Complete" );

    update_sample_info(".","GregAutoEntry",
                            "DATA:", "/userdir/greg/auto/file1236" ,
                            "STATUS:", "Active" );

    getPsgQentry("./psgQ",&ns);
    deletePsgQentry("./psgQ",&ns);

    deleteEnterQentry(".","enterQ");

}
Esempio n. 2
0
getPsgQentry(char *filename,SAMPLE_INFO *s)
{
    char eolch;
    char textline[256];
    FILE *stream;
    int stat;

    stream = fopen(filename,"r");

    if (stream == NULL)  /* does file exist? */
    {
        return(-1);
    }

    if (fscanf(stream,"%[^\n]%c", textline, &eolch) <= 0)
    {
      fclose(stream);
      return(ERROR);
    }
    else
    {
       char *sptr,*token;
       sptr = textline;
       

       token = (char*) strtok(sptr," ");
       token = (char*) strtok(NULL,"\n");
    }
    stat = read_sample_info(stream,s);

    if ((stat == ENDOFFILE) || (stat == ERROR))
    {
        fclose(stream);
        return(-1);
    }

    fclose(stream);

    return(0);
}
/**@brief Main routine of L-Galaxies*/
int main(int argc, char **argv)
{
  int filenr, *FileToProcess, *TaskToProcess, nfiles;
  char buf[1000];
  time_t start, current;


#ifdef PARALLEL
  MPI_Init(&argc, &argv);
  MPI_Comm_rank(MPI_COMM_WORLD, &ThisTask);
  MPI_Comm_size(MPI_COMM_WORLD, &NTask);
#else
  NTask = 1;
  ThisTask = 0;
#endif //PARALLEL

#ifdef MCMC
  time(&global_starting_time);
#endif


  if(ThisTask==0)
    {
 	  printf("\n\n\n");
 	  printf("**************************************************************************\n");
 	  printf("*                                                                        *\n");
 	  printf("*                   Copyright (C) <2016>  <L-Galaxies>                   *\n");
 	  printf("*                                                                        *\n");
 	  printf("*  This program is free software: you can redistribute it and/or modify  *\n");
 	  printf("*  it under the terms of the GNU General Public License as published by  *\n");
 	  printf("*  the Free Software Foundation, either version 3 of the License, or     *\n");
 	  printf("*  (at your option) any later version.                                   *\n");
 	  printf("*                                                                        *\n");
 	  printf("*  This program is distributed in the hope that it will be useful,       *\n");
 	  printf("*  but WITHOUT ANY WARRANTY; without even the implied warranty of        *\n");
 	  printf("*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *\n");
 	  printf("*  GNU General Public License for more details.                          *\n");
 	  printf("*                                                                        *\n");
 	  printf("*  You should have received a copy of the GNU General Public License     *\n");
 	  printf("*  along with this program.  If not, see <http://www.gnu.org/licenses/>  *\n");
 	  printf("*                                                                        *\n");
 	  printf("**************************************************************************\n\n\n");
    }

  if(argc > 3 || argc<2)
    {
	  printf("\n  Wrong number of runtime arguments\n\n");
      printf("\n  usage: ./L-Galaxies <parameterfile>\n\n");
      endrun(0);
    }

 if (ThisTask == 0)
   printf("%s\n",COMPILETIMESETTINGS); 

 /* check compatibility of some Makefile Options*/
  check_options();

  /*Reads the parameter file, given as an argument at run time. */
  read_parameter_file(argv[1]);

#ifdef MR_PLUS_MRII
  //Start with MR files and later change to MRII
  LastDarkMatterSnapShot=LastDarkMatterSnapShot_MR;
  sprintf(FileWithZList, "%s", FileWithZList_MR);
  sprintf(FileWithZList_OriginalCosm, "%s", FileWithZList_OriginalCosm_MR);
#endif

  mymalloc_init();

  sprintf(FinalOutputDir, "%s", OutputDir);
#ifndef MCMC
  if(argc == 3)
	sprintf(OutputDir, "%s", argv[2]);
#else
  FirstChainNumber=0;
  if(argc == 3)
  	FirstChainNumber=atoi(argv[2]);
#endif

  //time(&start);


#ifdef COMPUTE_SPECPHOT_PROPERTIES
  //for dust_model
  mu_seed = -150;
#endif

  init();

#ifdef STAR_FORMATION_HISTORY
#ifdef PARALLEL
  if(ThisTask == 0)
#endif
    write_sfh_bins();
#endif



#ifndef MCMC
  nfiles=get_nr_files_to_process(ThisTask);
  FileToProcess=mymalloc("FileToProcess", sizeof(int) * nfiles);
  TaskToProcess=mymalloc("TaskToProcess", sizeof(int) * nfiles);
  assign_files_to_tasks(FileToProcess, TaskToProcess, ThisTask, NTask, nfiles);

  int file;
  for(file = 0; file < nfiles; file++)
    {
	  if(ThisTask==TaskToProcess[file])
		  filenr=FileToProcess[file];
	  else
		  continue;
#else //MCMC
  /* In MCMC mode only one file is loaded into memory
   * and the sampling for all the steps is done on it */
	sprintf(SimulationDir, "%s/", SimulationDir);
  for(filenr = MCMCTreeSampleFile; filenr <= MCMCTreeSampleFile; filenr++)
	{
#endif //MCMC
	  time(&start);

	  #ifdef PARALLEL
#ifndef MCMC
	  time_t current;
	  do
	  	time(&current);
	  //while(difftime(current, start) < 5.0 * ThisTask);
	  while(difftime(current, start) < 1.0 * ThisTask);

#endif
#endif

	  load_tree_table(filenr);
#ifdef MCMC
      Senna(); // run the model in MCMC MODE
#else
      SAM(filenr); // run the model in NORMAL MODE
#endif

#ifdef MCMC
      break;	//break loop on files since the MCMC is done on a single file
#else
      time(&current);
      printf("\ndone tree file %d in %ldmin and %lds\n\n", filenr, (current - start)/60, (current - start)%60);

#endif //MCMC
      free_tree_table();
      //if temporary directory given as argument
       if(argc == 3)
         {
#ifdef GALAXYTREE
    	   sprintf(buf, "mv %s/%s_galtree_%d %s", OutputDir,FileNameGalaxies, filenr, FinalOutputDir);
#else
    	   sprintf(buf, "mv %s/%s_z*_%d %s", OutputDir,FileNameGalaxies, filenr, FinalOutputDir);
#endif
    	   system(buf);
         }
    }

#ifndef MCMC
  myfree(TaskToProcess);
  myfree(FileToProcess);
#endif

#ifdef PARALLEL
  MPI_Finalize();
#endif
  return 0;

}

/**@brief SAM() loops on trees and calls construct_galaxies.*/
#ifdef MCMC
double SAM(int filenr)
#else
void SAM(int filenr)
#endif
{
  int treenr, halonr;

#ifdef MCMC
  int ii;
  MCMC_GAL = mymalloc("MCMC_Gal", sizeof(struct MCMC_GALAXY) * MCMCAllocFactor);
  for(ii=0;ii<NOUT;ii++)
  	  TotMCMCGals[ii] = 0;

#ifdef MR_PLUS_MRII
  change_dark_matter_sim("MR");
#else
  if(CurrentMCMCStep==1)
  	read_sample_info();
#ifdef HALOMODEL
  else
    {
      int snap, ii;
      for(snap=0;snap<NOUT;snap++)
	for(ii=0;ii<NFofsInSample[snap];ii++)
	  MCMC_FOF[ii].NGalsInFoF[snap]=0;
    }
#endif //HALOMODEL
#endif //MR_PLUS_MRII
#endif //MCMC

  //to be used when we have tables for the scaling in any cosmology
  //read_scaling_parameters();

#ifndef MCMC
#ifdef GALAXYTREE
  create_galaxy_tree_file(filenr);
#else
  create_galaxy_files(filenr);
#endif
#endif

#ifdef GALAXYTREE
  FILE *fdg = fopen("treengal.dat", "w");
#endif

//***************************************************************************************
//***************************************************************************************

  //for(treenr = 0; treenr < NTrees_Switch_MR_MRII; treenr++)
  for(treenr = 0; treenr < Ntrees; treenr++)
  {
  //printf("doing tree %d of %d\n", treenr, Ntrees);
#ifdef MR_PLUS_MRII
  	if(treenr == NTrees_Switch_MR_MRII)
  		change_dark_matter_sim("MRII");
#endif

  	load_tree(treenr);
#ifdef MCMC
#ifdef PRELOAD_TREES
      if(CurrentMCMCStep==1)
#endif
#endif
        scale_cosmology(TreeNHalos[treenr]);

      gsl_rng_set(random_generator, filenr * 100000 + treenr);
      NumMergers = 0;
      NHaloGal = 0;
#ifdef GALAXYTREE
      NGalTree = 0;
      IndexStored = 0;
#endif
      int snapnum;
      //LastSnapShotNr is the highest output snapshot
      /* we process the snapshots now in temporal order 
       * (as a means to reduce peak memory usage) */
      for(snapnum = 0; snapnum <= LastSnapShotNr; snapnum++)
      {
#ifdef MCMC
    	  /* read the appropriate parameter list for current snapnum
    	   * into the parameter variables to be used in construct_galaxies */
    	  read_mcmc_par(snapnum);
#ifdef HALOMODEL
          //because we need halo masses even for FOFs
          //with no galaxies it needs to be done here
          assign_FOF_masses(snapnum, treenr);
#endif
#endif
    	  for(halonr = 0; halonr < TreeNHalos[treenr]; halonr++)
    	  	if(HaloAux[halonr].DoneFlag == 0 && Halo[halonr].SnapNum == snapnum)
    	  		construct_galaxies(filenr, treenr, halonr);
      }

      /* output remaining galaxies as needed */
      while(NHaloGal)
      	output_galaxy(treenr, 0);


#ifndef MCMC
#ifdef GALAXYTREE
      save_galaxy_tree_finalize(filenr, treenr);
#ifndef PARALLEL
      if((treenr/100)*100==treenr) printf("treenr=%d  TotGalCount=%d\n", treenr, TotGalCount);
#endif
      fflush(stdout);
      fprintf(fdg, "%d\n", NGalTree);
#endif
#else//ifdef MCMC
#endif
      free_galaxies_and_tree();
  }//loop on trees

#ifdef MCMC
  double lhood = get_likelihood();

#ifdef MR_PLUS_MRII
  free(MCMC_FOF);
#else
  if(CurrentMCMCStep==ChainLength)
  	free(MCMC_FOF);
#endif

  myfree(MCMC_GAL);
  return lhood;

#else //MCMC

#ifdef GALAXYTREE
  close_galaxy_tree_file();
#else
  close_galaxy_files();
#endif

  return;
#endif
}
Esempio n. 4
0
/*---------------------------------------------------------------------------
|  update_sample_info()/5
|
|  update_sample_info - 
|	Takes a automation directory and filename of sample_info structure (e.g., doneQ,enterq,sampleinfo)
|	Searches through the file for a match between the match_target value
|	  and the match_value;
|       Upon finding the match it then updates the update_target with the
|	  update_value;
|       For example: You want to update the doneQ  'STATUS' entry to Complete,
|		     for the experiment inwhich the DATA entry matches with
|		     '/vnmr/auto/greg.1201' (i.e. DATA is a unique identifier).
|       The call would be:
|	  update_sample_info("systemdir/auto","doneQ",
|			     "DATA:","/vnmr/auto/greg.1201",
|			     "STATUS","Complete");
|
|				Author:  Greg Brissey 1/28/88
+--------------------------------------------------------------------------*/
int update_sample_info(char *autodir, char *filename, char *match_target,
                       char *match_value, char *update_target, char *update_value)
{
    FILE *update_file,*tmp_file;
    char filepath[128],value[MAX_TEXT_LEN];
    struct sample_info	sample_entry;
    int stat,entryline,foundit;
    
    strncpy(filepath,autodir,110);
    strcat(filepath,"/");
    strcat(filepath,filename);

    /* if autoinfo mapping file hase not be read do so now */
    if (infomap.mapped != 1)		/* true if autoinfo has been read for mapping */
    {
	read_info_file(autodir);
    }


    if (lockfile(filepath) == ERROR) /* lock file for excusive use */
    {
        fprintf(stderr,"update_sample_info: could not lock '%s' file.\n",filepath);
    }
 
    update_file = fopen(filepath,"r");
    if (update_file == NULL)  /* does file exist? */
    {
        unlockfile(filepath);   /* unlock file */
        fprintf(stderr,"update_sample_info: '%s' file is not present for reading.\n",
			filepath);
        return(ERROR);
    }

    tmp_file = fopen("/tmp/update_sample_info.tmp","w");
    if (tmp_file == NULL)  /* does file exist? */
    {
        unlockfile(filepath);   /* unlock file */
        fclose(update_file);
        fprintf(stderr,
	    "update_sample_info: temp file '/tmp/update_sample_info.tmp' could not be created.\n");
        return(ERROR);
    }
 
    /* read through the the file writing to a tmp file as changes are made */
    foundit = 0;
    while ( (stat = read_sample_info(update_file,&sample_entry)) != ENDOFFILE)
    {
	if (stat == ERROR)
	{
           unlockfile(filepath);   /* unlock file */
    	   fclose(update_file);
    	   fclose(tmp_file);
           fprintf(stderr,
	    "update_sample_info: read error on file '%s'.\n",filepath);
           return(ERROR);
	}

        /* Once found and changed we can skip all this */
        if (!foundit)
        {
	  get_sample_info(&sample_entry,match_target,value,MAX_TEXT_LEN,&entryline);
          if (strncmp(value,match_value,MAX_TEXT_LEN) == 0)
          {
	     get_sample_info(&sample_entry,update_target,value,MAX_TEXT_LEN,&entryline);

	     /* If updating STATUS and STATUS: Shimming, then remove this entry */
             if ( (strcmp(infomap.status,update_target) == 0) && 
	          ( (strcmp(update_value,"Complete") == 0) || (strcmp(update_value,"Error") == 0)) &&
		  (strcmp(sample_entry.prompt_entry[entryline].etext,"Shimming") == 0) )
             {
		/* elimenate Shimming Experiment from doneQ */
	        foundit = 1;
		continue;	/* don't update, don't write it out */
             }

	     strncpy(sample_entry.prompt_entry[entryline].etext,update_value,MAX_TEXT_LEN);
	     sample_entry.prompt_entry[entryline].etext[MAX_TEXT_LEN-1] = '\0';
	     foundit = 1;
          }
        }

	if (write_sample_info(tmp_file,&sample_entry) == ERROR)
	{
           unlockfile(filepath);   /* unlock file */
    	   fclose(update_file);
    	   fclose(tmp_file);
           fprintf(stderr,
	    "update_sample_info: write error to temp file '/tmp/update_sample_info.tmp'.\n");
           return(ERROR);
	}
    }

    fclose(update_file);
    fclose(tmp_file);

    /* Changes are complete in the tmp file, now copy the tmp file into the orginal file */
    tmp_file = fopen("/tmp/update_sample_info.tmp","r");
    if (tmp_file == NULL)  /* does file exist? */
    {
        unlockfile(filepath);   /* unlock file */
        fprintf(stderr,
          "update_sample_info: temp file '/tmp/update_sample_info.tmp' could not be opened to be read.\n");
        return(ERROR);
    }

    update_file = fopen(filepath,"w");
    if (update_file == NULL)  /* does file exist? */
    {
        unlockfile(filepath);   /* unlock file */
        fclose(tmp_file);
        fprintf(stderr,"update_sample_info: '%s' file is not present for writing.\n",
			filepath);
        return(ERROR);
    }

    while ( ( stat = read_sample_info(tmp_file,&sample_entry)) != ENDOFFILE)
    {
	if (stat == ERROR)
	{
           unlockfile(filepath);   /* unlock file */
    	   fclose(update_file);
    	   fclose(tmp_file);
           fprintf(stderr,
	    "update_sample_info: read error on file '/tmp/update_sample_info.tmp'.\n");
           return(ERROR);
	}
	if (write_sample_info(update_file,&sample_entry) == ERROR)
	{
           unlockfile(filepath);   /* unlock file */
    	   fclose(update_file);
    	   fclose(tmp_file);
           fprintf(stderr,
	    "update_sample_info: write error to file '%s'.\n",filepath);
           return(ERROR);
	}
    }

    fclose(update_file);
    fclose(tmp_file);

    unlockfile(filepath);   /* unlock file */
    return(OK); 
}
Esempio n. 5
0
/**@brief Main routine of L-Galaxies*/
int main(int argc, char **argv)
{
  int filenr, *FileToProcess, *TaskToProcess, nfiles;
  char buf[1000];
  time_t start, current;

  //Catch floating point exceptions
#ifdef DEBUG
#endif

#ifdef PARALLEL
  MPI_Init(&argc, &argv);
  MPI_Comm_rank(MPI_COMM_WORLD, &ThisTask);
  MPI_Comm_size(MPI_COMM_WORLD, &NTask);
#else
  NTask = 1;
  ThisTask = 0;
#endif //PARALLEL

#ifdef MCMC
  time(&global_starting_time);
#endif

  if(argc > 3)
    {
      printf("\n  usage: L-Galaxies <parameterfile>\n\n");
      endrun(0);
    }

 if (ThisTask == 0)
   printf("%s\n",COMPILETIMESETTINGS); 

  /*Reads the parameter file, given as an argument at run time. */
  read_parameter_file(argv[1]);

#ifdef MR_PLUS_MRII
  //Start with MR files and later change to MRII
  LastDarkMatterSnapShot=LastDarkMatterSnapShot_MR;
  sprintf(FileWithZList, "%s", FileWithZList_MR);
  sprintf(FileWithZList_OriginalCosm, "%s", FileWithZList_OriginalCosm_MR);
#endif

  mymalloc_init();

  sprintf(FinalOutputDir, "%s", OutputDir);
  if(argc == 3)
	sprintf(OutputDir, "%s", argv[2]);

  //time(&start);

  /* check compatibility of some Makefile Options*/
  check_options();

#ifdef COMPUTE_SPECPHOT_PROPERTIES
  //for dust_model
  mu_seed = -150;
#endif
  init();

#ifdef STAR_FORMATION_HISTORY
#ifdef PARALLEL
  if(ThisTask == 0)
#endif
    write_sfh_bins();
#endif

#ifdef MCMC
#ifdef PARALLEL
 /* a small delay to avoid all processors reading trees at the same time*/
  time(&start);
  do
    time(&current);
  while(difftime(current, start) < 10.0 * ThisTask);
#endif
#endif

#ifndef MCMC
  nfiles=get_nr_files_to_process(ThisTask);
  FileToProcess=mymalloc("FileToProcess", sizeof(int) * nfiles);
  TaskToProcess=mymalloc("TaskToProcess", sizeof(int) * nfiles);
  assign_files_to_tasks(FileToProcess, TaskToProcess, ThisTask, NTask, nfiles);

  int file;
  for(file = 0; file < nfiles; file++)
    {
	  if(ThisTask==TaskToProcess[file])
		  filenr=FileToProcess[file];
	  else
		  continue;
#else //MCMC
  /* In MCMC mode only one file is loaded into memory
   * and the sampling for all the steps is done on it */
	sprintf(SimulationDir, "%s/MergerTrees_%d/", SimulationDir, ThisTask);
  for(filenr = MCMCTreeSampleFile; filenr <= MCMCTreeSampleFile; filenr++)
	{
#endif //MCMC
	  time(&start);

#ifdef READXFRAC
      get_xfrac_mesh();
#endif 

      load_tree_table(filenr);

      /* Read in mesh dimensions */

#ifdef MCMC
#ifdef PARALLEL
      time_t start, start2;
 /* a small delay to reset processors to the same time*/
      time(&start2);
      do
      	time(&current);
      while(difftime(current, start2) < 10.0 * (NTask-ThisTask));
#endif
      Senna(); // run the model in MCMC MODE
#else
      SAM(filenr); // run the model in NORMAL MODE
#endif



#ifdef MCMC
      break;	//break loop on files since the MCMC is done on a single file
#else
      time(&current);
      printf("\ndone tree file %d in %ldmin and %lds\n\n", filenr, (current - start)/60, (current - start)%60);

#endif //MCMC
      free_tree_table();
      //if temporary directory given as argument
       if(argc == 3)
         {
#ifdef GALAXYTREE
    	   sprintf(buf, "mv %s/%s_galtree_%d %s", OutputDir,FileNameGalaxies, filenr, FinalOutputDir);
#else
    	   sprintf(buf, "mv %s/%s_z*_%d %s", OutputDir,FileNameGalaxies, filenr, FinalOutputDir);
#endif
    	   system(buf);
         }
    }

#ifndef MCMC
  myfree(TaskToProcess);
  myfree(FileToProcess);
#endif

#ifdef PARALLEL
  MPI_Barrier(MPI_COMM_WORLD);  
  MPI_Finalize();
#endif
  return 0;

}

/**@brief SAM() loops on trees and calls construct_galaxies.*/
#ifdef MCMC
  double SAM(int filenr)
#else
  void SAM(int filenr)
#endif
  {
    int treenr, halonr;
    time_t t_mark_a, t_mark_b;
#ifdef MCMC
    int ii;
    MCMC_GAL = mymalloc("MCMC_Gal", sizeof(struct MCMC_GALAXY) * MCMCAllocFactor);

    for(ii=0;ii<NOUT;ii++)
      TotMCMCGals[ii] = 0;

    if(Sample_Cosmological_Parameters==1)
      {
	reset_cosmology ();
#ifdef HALOMODEL
	initialize_halomodel();
#endif
      }

#ifdef MR_PLUS_MRII
    change_dark_matter_sim("MR");
#else
    if(Sample_Cosmological_Parameters==1 || CurrentMCMCStep==1)
      read_sample_info();
    else
      {
        int snap, ii;
        for(snap=0;snap<NOUT;snap++)
	  for(ii=0;ii<NFofsInSample[snap];ii++)
	    MCMC_FOF[ii].NGalsInFoF[snap]=0;
      }
#endif
#endif

    //to be used when we have tables for the scaling in any cosmology
    //read_scaling_parameters();

#ifndef MCMC
#ifdef GALAXYTREE
    create_galaxy_tree_file(filenr);
#else
    create_galaxy_files(filenr);
#endif
#ifdef ALL_SKY_LIGHTCONE
    int nr;
    for ( nr = 0; nr < NCONES; nr++)
      create_galaxy_lightcone_files(filenr, nr);
#endif
#endif

#ifdef GALAXYTREE
    FILE *fdg = fopen("treengal.dat", "w");
#endif

    //***************************************************************************************
    //***************************************************************************************


    //for(treenr = 0; treenr < NTrees_Switch_MR_MRII; treenr++)
    //for(treenr = NTrees_Switch_MR_MRII; treenr < Ntrees; treenr++)

    /* Scan through all trees snapshot by snapshot */
    int snapnum;


    //for(treenr = 0; treenr < NTrees_Switch_MR_MRII; treenr++)
    //for(treenr = NTrees_Switch_MR_MRII; treenr < Ntrees; treenr++)
    for(treenr = 0; treenr < Ntrees; treenr++)
      //for(treenr = 0; treenr < 1;treenr++)
      {
	//printf("doing tree %d of %d\n", treenr, Ntrees);

#ifdef MR_PLUS_MRII
	if(treenr == NTrees_Switch_MR_MRII)
	  change_dark_matter_sim("MRII");
#endif

	load_tree(treenr);

#ifdef MCMC
#ifdef PRELOAD_TREES
	if(Sample_Cosmological_Parameters==1 || CurrentMCMCStep==0)
#endif
#endif
	  scale_cosmology(TreeNHalos[treenr]);

	gsl_rng_set(random_generator, filenr * 100000 + treenr);
	NumMergers = 0;
	NHaloGal = 0;

#ifdef GALAXYTREE
	NGalTree = 0;
	IndexStored = 0;
#endif
	//LastSnapShotNr is the highest output snapshot
	/* we process the snapshots now in temporal order
	 * (as a means to reduce peak memory usage) */
	for(snapnum = 0; snapnum <= LastSnapShotNr; snapnum++)
	  //for(snapnum = 0; snapnum <= 30; snapnum++)
	  {
#ifdef MCMC
	    /* read the appropriate parameter list for current snapnum
	     * into the parameter variables to be used in construct_galaxies */
	    read_mcmc_par(snapnum);
#else
	    //used to allow parameter values to vary with redshift
	    //re_set_parameters(snapnum);
#endif
	    //printf("doing snap=%d\n",snapnum);
	    for(halonr = 0; halonr < TreeNHalos[treenr]; halonr++)
	      if(HaloAux[halonr].DoneFlag == 0 && Halo[halonr].SnapNum == snapnum)
		construct_galaxies(filenr, treenr, halonr);
	  }

	/* output remaining galaxies as needed */
	while(NHaloGal)
	  output_galaxy(treenr, 0);


#ifndef MCMC
#ifdef GALAXYTREE
	save_galaxy_tree_finalize(filenr, treenr);
#ifndef PARALLEL
	if((treenr/100)*100==treenr) printf("treenr=%d TotGalCount=%d\n", treenr, TotGalCount);
#endif
	fflush(stdout);
	fprintf(fdg, "%d\n", NGalTree);
#endif
#else//ifdef MCMC
#ifdef PRELOAD_TREES
	if(Sample_Cosmological_Parameters==1)
	  un_scale_cosmology(TreeNHalos[treenr]);
#endif
#endif
	free_galaxies_and_tree();
      }





#ifdef MCMC
    double lhood = get_likelihood();

#ifdef MR_PLUS_MRII
    free(MCMC_FOF);
#else
    if(Sample_Cosmological_Parameters==1 ||  CurrentMCMCStep==ChainLength)
      free(MCMC_FOF);
#endif

#ifdef HALOMODEL
    if (Sample_Cosmological_Parameters==1) {
      gsl_spline_free(FofSpline);
      gsl_interp_accel_free(FofAcc);
      gsl_spline_free(SigmaSpline);
      gsl_interp_accel_free(SigmaAcc);
      gsl_spline_free(ellipSpline);
      gsl_interp_accel_free(ellipAcc);
      gsl_spline_free(PowSpline);
    } //if
#endif

    myfree(MCMC_GAL);
    return lhood;

#else //MCMC

#ifdef GALAXYTREE
    close_galaxy_tree_file();
#else
    close_galaxy_files();
#endif
#ifdef ALL_SKY_LIGHTCONE
    for (nr = 0; nr < NCONES; nr++)
      close_galaxy_lightcone_files(nr);
#endif

    return;
#endif
  }