Create and copy multi-dimensional arraysΒΆ

This example focuses on creating multi-dimensional arrays, using complex type and copying IDS structures.

See also

API documentation for validate

    public static void copyingAndValidatingIDS() throws Exception {
        
        try {

            // create empty gyrokinetics_local
            // NOTE: gyrokinetics_local is an alpha IDS
            imas.gyrokinetics_local emptyGyrokineticsLocal = new imas.gyrokinetics_local();

            // there is mandatory field <ids>/ids_properties/homogeneous_time
            emptyGyrokineticsLocal.ids_properties.homogeneous_time = LowLevel.IDS_TIME_MODE_HOMOGENEOUS;

            // some IDS fields contain multi-dimensional arrays.
            // first we have to create 2D array containing Complex values
            Complex[][] complexArray = new Complex[3][3];
            for (int x = 0; x < 3; x++){
                for (int y = 0; y < 3; y++){
                    complexArray[x][y] = new Complex (x,y);
                }
            }
            
            // then we create new object using Vect2DComplex method and set its dimensions to 3x3
            // after that we need to set previously created array to IDS field
            emptyGyrokineticsLocal.non_linear.fields_zonal_2d.phi_potential_perturbed_norm = new Vect2DComplex(3,3);
            emptyGyrokineticsLocal.non_linear.fields_zonal_2d.phi_potential_perturbed_norm.set(complexArray);

            System.out.println("\nFilled 2D array (gyrokinetics_local/non_linear/fields_zonal_2d/phi_potential_perturbed_norm): \n" + emptyGyrokineticsLocal.non_linear.fields_zonal_2d.phi_potential_perturbed_norm);

            // some fields have coordinates consistency. <isd>.validate() method checks for this consistency
            // example of field of this type is gyrokinetics_local/non_linear/fields_zonal_2d/phi_potential_perturbed_norm
            // it's first dimension size must be equal to non_linear/radial_wavevector_norm size and second dimension size equal to non_linear/time_norm
            try {
                emptyGyrokineticsLocal.validate();
            } catch (Exception e) {
                System.err.println("\nCaught exception (raised intentionally): " + e.getMessage());
            }

            // this will fix it
            double[] timeArray = {1.0, 2.0, 3.0};
            emptyGyrokineticsLocal.non_linear.radial_wavevector_norm = new Vect1DDouble(timeArray);
            emptyGyrokineticsLocal.non_linear.time_norm = new Vect1DDouble(timeArray);

            // gyrokinetics_local/linear.wavevector(i1)/eigenmode(i2)/fields.phi_potential_perturbed_norm has two dimensions and stores complex numbers
            emptyGyrokineticsLocal.linear.wavevector = new imas.gyrokinetics_local.linearClass.wavevectorClass[1];
            emptyGyrokineticsLocal.linear.wavevector[0] = new imas.gyrokinetics_local.linearClass.wavevectorClass();

            emptyGyrokineticsLocal.linear.wavevector[0].eigenmode = new imas.gyrokinetics_local.linearClass.wavevectorClass.eigenmodeClass[1];
            emptyGyrokineticsLocal.linear.wavevector[0].eigenmode[0] = new imas.gyrokinetics_local.linearClass.wavevectorClass.eigenmodeClass();

            double[] dimensionArray = {1.0,2.0,3.0};
            emptyGyrokineticsLocal.linear.wavevector[0].eigenmode[0].angle_pol = new Vect1DDouble(dimensionArray);
            emptyGyrokineticsLocal.linear.wavevector[0].eigenmode[0].time_norm = new Vect1DDouble(dimensionArray);

            emptyGyrokineticsLocal.linear.wavevector[0].eigenmode[0].fields.phi_potential_perturbed_norm = new Vect2DComplex(3,3);
            emptyGyrokineticsLocal.linear.wavevector[0].eigenmode[0].fields.phi_potential_perturbed_norm.set(complexArray);

            // right way of copying IDS in Java
            int dataEntry = imas.open("imas:memory?path=/", LowLevel.FORCE_CREATE_PULSE);
            emptyGyrokineticsLocal.time = new Vect1DDouble(timeArray);
            emptyGyrokineticsLocal.put(dataEntry, "gyrokinetics_local", emptyGyrokineticsLocal);
            imas.gyrokinetics_local emptyGyrokineticsLocalCopy = imas.gyrokinetics_local.get(dataEntry, "gyrokinetics_local");
            imas.close(dataEntry);

            System.out.println("\nOriginal value: \n" + emptyGyrokineticsLocal.linear.wavevector[0].eigenmode[0].fields.phi_potential_perturbed_norm);
            System.out.println("Copied value: \n" + emptyGyrokineticsLocalCopy.linear.wavevector[0].eigenmode[0].fields.phi_potential_perturbed_norm);

        } catch (Exception e) {
            System.err.println("Following exception occurred\n" + e.getMessage());
            throw e;       
        }
    }
}