package  ijDoc.plugs;

import java.io.*;
/**
 * This applies Normal Moveout to a 2D float array. 
 */
public class Nmo {
  float vel = 1.44f;

  public Nmo() { }

  /**
   * @param velocity (default 1.44 km/s)
   */
  public void setVelocity(float vel) { this.vel = vel; } 

  public static void main(String[] args) {
    int n1 = 400; 
    int n2 = 24;
    float[][] data = new float[n2][n1];
    DataInputStream  istr = new DataInputStream( System.in ); 
    DataOutputStream ostr = new DataOutputStream(System.out); 

    try {
      for (int i2=0; i2 < n2; i2++) 
      for (int i1=0; i1 < n1; i1++) 
	data[i2][i1] = istr.readFloat();
    }
    catch(Exception e) { System.err.println("Cannot read floats!"); }

    Nmo op = new Nmo();
    Float vel = new Float(args[0]); 
    op.setVelocity(vel.floatValue()); 
    data = op.apply(data);
      
    try {
      for (int i2=0; i2 < n2; i2++) 
      for (int i1=0; i1 < n1; i1++) 
        ostr.writeFloat(data[i2][i1]);
      ostr.flush();
    } 
    catch(Exception e) { System.err.println("Cannot read floats!"); }
  }

  public float[][] apply(float[][] iput) {
    int   nz = iput[0].length;
    float dz = 0.004f;
    float oz = 0.002f;

    int   nx = iput.length; 
    float dx = 0.067056f;
    float ox = 0.0670519f;

    float[][] oput = new float[nx][nz];
    for (int ix=0; ix < nx ; ix++) { float xs = (ox + dx * ix) * 1f/vel  ; 
    for (int iz=0; iz < nz ; iz++) { float zs =  oz + dz * iz; 
      float ts = (float) Math.sqrt(zs * zs + xs * xs) + 1.e-20f;
      float wt = (float) (zs/ts * (1./Math.sqrt(ts))); 
      int   it = (int) (.5 + (ts - oz) / dz);
      if (it < nz) {
	oput[ix][iz] += iput[ix][it] * wt;
      }
    }}
    return oput;
  }
}

