/*
 * Created on 14 December 2005, 16:23
 * (C) 2005 Copyright ANSTO. All rights reserved.
 */

package au.gov.ansto.bragg.nexus.test;

import au.gov.ansto.bragg.nexus.auto.NXaperture;
import au.gov.ansto.bragg.nexus.auto.NXcrystal;
import au.gov.ansto.bragg.nexus.auto.NXcrystal.Polar_angle;
import au.gov.ansto.bragg.nexus.auto.NXcrystal.Type;
import au.gov.ansto.bragg.nexus.auto.NXdetector;
import au.gov.ansto.bragg.nexus.auto.NXdetector.Azimuthal_angle;
import au.gov.ansto.bragg.nexus.auto.NXentry;
import au.gov.ansto.bragg.nexus.auto.NXentry.Definition;
import au.gov.ansto.bragg.nexus.auto.NXentry.Start_time;
import au.gov.ansto.bragg.nexus.auto.NXentry.Title;
import au.gov.ansto.bragg.nexus.auto.NXinstrument;
import au.gov.ansto.bragg.nexus.auto.NXmonitor;
import au.gov.ansto.bragg.nexus.auto.NXmonitor.Preset;
import au.gov.ansto.bragg.nexus.auto.NXroot;
import au.gov.ansto.bragg.nexus.auto.NXsample;
import au.gov.ansto.bragg.nexus.auto.NXsample.Orientation_matrix;
import au.gov.ansto.bragg.nexus.auto.NXuser;
import au.gov.ansto.bragg.nexus.auto.NXuser.Address;
import au.gov.ansto.bragg.nexus.base.ExtraName_;
import au.gov.ansto.bragg.nexus.base.NXgroup_;
import au.gov.ansto.bragg.nexus.base.NXtypes;

/**
 *
 * @author dke
 */
public class Test_NeXus
// extends Test  //TODO
{
    
    /** Creates a new instance of Test_NeXus */
    public Test_NeXus() {
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        
        NXroot rita2 = testNXrootRITA2();
        System.out.print(rita2);
    }
    
    static public NXroot testNXrootRITA2() {
        try {
            
            /*
        NXaperture a = new NXaperture();
        NXaperture.Material m = new NXaperture.Material();
        m.setType(1);
        m.setUnits("cm");
        a.setMaterial(m);
        return a;       */
            
            NXentry entry = new NXentry();
            NXentry.Title title = new NXentry.Title("title of an experiment");
            entry.setTitle(title);
            entry.setStart_time(new NXentry.Start_time("2005-10-11 13:41:44"));
            
            NXentry.Definition definition = new NXentry.Definition("NXmonotas");
            definition.setURL("http://www.nexus.anl.gov/instruments/xml/NXmonotas.xml");
            definition.setVersion("1.0");
            entry.setDefinition(definition);
            
            
            NXuser user  = new NXuser();
            user.setName("user");
            user.setExtraName(new NXuser.ExtraName("UNKNOWN"));
            user.setAffiliation(new NXuser.Affiliation("UNKNOWN"));
            user.setAddress(new NXuser.Address("UNKNOWN"));
            user.setEmail(new NXuser.Email("unknown@nowhere"));
            entry.addNXuser(user);
            
            NXinstrument instrument = new NXinstrument();
            instrument.setName("RITA-2");
            
            NXcrystal crystal_m = new NXcrystal();
            crystal_m.setName("monochromator");
            //crystal_m.setType(new NXcrystal.Type("Pyrolytic Graphite"));
            
            /* unsupported
          <energy NAPItype="NX_FLOAT32[3]" units="mev"
target="/entry1/RITA-2/monochromator/energy">
                  3.8016       3.8016       3.8016
          </energy>
          <rotation_angle NAPItype="NX_FLOAT32[3]" units="degree">
                 58.5000      58.5000      58.5000
          </rotation_angle>
          <d_spacing NAPItype="NX_FLOAT32" units="Angstroem">
                  3.3540
          </d_spacing>
             */
            instrument.addNXcrystal(crystal_m);
            
            NXcrystal crystal_a = new NXcrystal();
            crystal_a.setName("analyzer");
            //crystal_a.setType(new NXcrystal.Type("Pyrolytic Graphite"));
                /*
          <energy NAPItype="NX_FLOAT32[3]" units="mev"
target="/entry1/RITA-2/analyzer/energy">
                138.7144     138.7144     138.7144
          </energy>
          <rotation_angle NAPItype="NX_FLOAT32[3]" units\="degree">
                 45.0000      45.0000      45.0000
          </rotation_angle>
          <d_spacing NAPItype="NX_FLOAT32" units="Angstroem">
                  3.3540
          </d_spacing>
                 */
            NXcrystal.Polar_angle polar_angle =
                    new NXcrystal.Polar_angle(new float[]{16.9000f,17.0000f,17.1000f});
            
            polar_angle.setUnits("degree");
            
            crystal_a.setPolar_angle(polar_angle);
                    /*
          <polar_angle NAPItype="NX_FLOAT32[3]" units="degree">
                 16.9000      17.0000      17.1000
          </polar_angle>
          <azimuthal_angle NAPItype="NX_FLOAT32" units="degree">
                  0.0000
          </azimuthal_angle>
          <blade_angle_1 NAPItype="NX_FLOAT32" units="degree">
                  0.0000
          </blade_angle_1>
          <blade_angle_2 NAPItype="NX_FLOAT32" units="degree">
                  0.0000
          </blade_angle_2>
          <blade_angle_3 NAPItype="NX_FLOAT32" units="degree">
                  0.0000
          </blade_angle_3>
          <blade_angle_4 NAPItype="NX_FLOAT32" units="degree">
                  0.0000
          </blade_angle_4>
          <blade_angle_5 NAPItype="NX_FLOAT32" units="degree">
                  0.0000
          </blade_angle_5>
          <blade_angle_6 NAPItype="NX_FLOAT32" units="degree">
                  0.0000
          </blade_angle_6>
          <blade_angle_7 NAPItype="NX_FLOAT32" units="degree">
                  0.0000
          </blade_angle_7>
          <blade_angle_8 NAPItype="NX_FLOAT32" units="degree">
                  0.0000
          </blade_angle_8>
          <blade_angle_9 NAPItype="NX_FLOAT32" units="degree">
                  0.0000
          </blade_angle_9>
        </NXcrystal>
                     */
            instrument.addNXcrystal(crystal_a);
            
            
            NXdetector detector = new NXdetector();
            detector.setName("detector");
            NXdetector.Polar_angle polar_angle_d = new NXdetector.Polar_angle(new float[]{13.1470f, 13.1470f, 13.1470f});
            polar_angle_d.setUnits("degree");
            detector.setPolar_angle(polar_angle_d);
            /* UNSUPPORTED
          <summed_counts NAPItype="NX_INT32[3]" signal="1">
              1804289383   1221918175    187389957
          </summed_counts>
          <azimuthal_angle NAPItype="NX_FLOAT32" units="degree">
             
          </azimuthal_angle>
             */
            NXdetector.Azimuthal_angle azimuthal_angle = new NXdetector.Azimuthal_angle(180.0000);
            azimuthal_angle.setUnits("degree");
            detector.setAzimuthal_angle(azimuthal_angle);
            
            instrument.addNXdetector(detector);
            entry.setNXinstrument(instrument);
            
            NXmonitor monitor = new NXmonitor();
            monitor.setName("control");
            entry.addNXmonitor(monitor);
            
            NXmonitor.Preset preset = new NXmonitor.Preset(1.0000f);
            preset.setNXtype(NXtypes.NX_FLOAT);
            monitor.setPreset(preset);
            
            NXmonitor.Mode mode = new NXmonitor.Mode("timer");
            monitor.setMode(mode);
            
            NXsample sample = new NXsample();
            sample.setName("sample");
            //sample.setUnit_cell();
            sample.setOrientation_matrix(new NXsample.Orientation_matrix(
                    new float[][] {{-0.0051f, -0.1426f, 0.0679f},
                    {0.0002f, -0.0323f, -0.3005f},
                    {0.1678f, -0.0043f,  0.0024f}}));
            
            
            entry.setNXsample(sample);
            
            NXroot root = new NXroot();
            
            root.addNXentry(entry);
            
            return root;
        } catch (Exception e) {e.printStackTrace(); return null;}
    }
    
    static public NXgroup_ testNXgroup() {
        try {
            NXaperture a = new NXaperture();
            NXaperture.Material m = new NXaperture.Material();
            m.setNXtype(1);
            m.setUnits("cm");
            a.setMaterial(m);
            return a;
        } catch (Exception e) {e.printStackTrace();return null;}
    }
    
}
