package ipp.imse.fusionOptics.imse.old;

import ipp.imse.fusionOptics.mse.AugMSESystem;

import java.util.List;

import cache.common.Cache;
import cache.randomAccessCache.RACache;
import cache.randomAccessCache.RACacheService;
import cache.randomAccessCache.RACacheSet;
import fusionOptics.MinervaOpticsSettings;
import fusionOptics.Util;
import fusionOptics.interfaces.Absorber;
import fusionOptics.optics.Box;
import fusionOptics.pointSpread.DualGaussianPSF;
import fusionOptics.pointSpread.PSFGrid;
import fusionOptics.pointSpread.PSFStatsSourceInterpolation;
import fusionOptics.pointSpread.PointSpreadFunction;

import binaryMatrixFile.BinaryMatrixFile;


import oneLiners.OneLiners;

/** Generate images from PSF interpolation */

public class ChangeCacheIndexing {
	
	final static String outPath = MinervaOpticsSettings.getAppsOutputPath() + "/rayTracing/augImse/psfCollect";
	
	//Grids as used in SimpleImaging
	//final static String cacheSet = "augImse-full"; 
	//final static String cacheSet2 = "augImse-gridKeyedData-full"; 
	final static String cacheSet = "augImse-large-1000x1000";
	final static String cacheSet2 = "augImse-gridKeyedData-large-1000x1000";
	
	final static double imageX[] = OneLiners.linSpace(-0.01, 0.01, 500);
	final static double imageY[] = OneLiners.linSpace(-0.01, 0.01, 500);
	
	final static AugMSESystem sys = new AugMSESystem();
	
	static PSFStatsSourceInterpolation psfInterp;
	static PointSpreadCollectorOld psfCollOld;
	static PSFGrid psfGrid;
	
	public static void main(String[] args) {
		psfCollOld = new PointSpreadCollectorOld(cacheSet, null);
		double g[][] = psfCollOld.getGridDefinition();
		double gridDef[][] = new double[][]{
				{ g[0][0], g[0][g[0].length-1], g[0].length },
				{ g[1][0], g[1][g[1].length-1], g[1].length },
				{ g[2][0], g[2][g[2].length-1], g[2].length },
			};
		
		RACache cache = (RACache) RACacheService.instance().getCache("optics.PSF");
		
		//cache.getTaggedSet(cacheSet, "localID-1011-iZ-100").cleanCache(true);
		//cache.cleanAllSets(cacheSet, true);
		//if(true)return;
		
		//used to do this, but now invalid:
		//cache.getTaggedSet(cacheSet2, null).singleAccess = true;
		
		psfGrid = new PSFGrid(cacheSet2, gridDef, DualGaussianPSF.class);
		
		double sourcePoses[][] = psfCollOld.getSourcePositions();
		int n = sourcePoses.length;
		
		long t0 = System.currentTimeMillis(), updatePeriod = MinervaOpticsSettings.getUserAttentionSpan();
		for(int i=0; i < n; i++){
			if(psfGrid.get(sourcePoses[i]) != null) 
				continue;
			
			PointSpreadFunction psf = psfCollOld.getPSFExact(sourcePoses[i]);
			
			if(psf == null)
				throw new RuntimeException("cache get failed for one of the known cache keys");
			
			psfGrid.put(sourcePoses[i], psf);
			
			if(psfGrid.get(sourcePoses[i]) == null)
				throw new RuntimeException("today, we are made of the fail");
				
			
			if((System.currentTimeMillis() - t0) > updatePeriod){ 
				System.out.print("\n" + i + " / " + n + " = " + (i*100.0/n) + "%");
				t0 = System.currentTimeMillis();
			}
		}
		
		
		if(true)return;
	}
}
