dimesões de uma matriz

Olá pessoal,

Estou trabalhando com matrizes multidimensionais e surgiu uma dúvida que não consigo resolver. Vou tentar resumir o que já fiz e com o problema que me deparei. Primeiro trabalho com o IDL e JAVA. IDL é uma linguagem de descrição de interface.

No IDL precisei realizar o seguinte:

Ler dois arquivos .sav e cnj, onde esses arquivos são matrizes do tipo DOUBLE = Array[4, 16, 16, 16] . Precisei colocar esses dois arquivos em um único, no caso um arquivo binário. (ok - feito)

Depois no JAVA leio esse binário e reestruturo em uma nova matriz também do tipo [4, 16, 16, 16] (ok - feito)

No entanto, agora me deparei com o seguinte problema a matriz [4, 16, 16, 16] possui dimensões variaveis de acordo com o arquivo lido lá no IDL, ou seja, a matriz ficaria assim [4, X, X, Y], onde X e Y são as dimensões que podem variar.

O que eu fiz foi o seguinte, no IDL eu gravo no arquivo binário as posições dessas dimensões para depois receber lá no JAVA, para que vocês possam ver segue meu código no IDL:

PRO READ_FILE

; ---------------------
; Lendo o arquivo .sav
; ---------------------
OpenR, lun, 'C:\Documents and Settings\Projeto_Estudo\I71B16T020.0_B0061_drat0.518_Dlon012.6_Cen0.500_h0.22_base1.00_beta041.9_i021.8_Nel15.00_Blat035_Blong336_fr00001.00_fas15.sav', /Get_lun
file_sav = strarr(file_lines('C:\Documents and Settings\Projeto_Estudo\I71B16T020.0_B0061_drat0.518_Dlon012.6_Cen0.500_h0.22_base1.00_beta041.9_i021.8_Nel15.00_Blat035_Blong336_fr00001.00_fas15.sav')) 
ReadF, lun, file_sav
close, lun

; --------------------- 
; Lendo o arquivo .cnj
; ---------------------
OpenR, lun, 'C:\Documents and Settings\Projeto_Estudo\I71B16T020.0_B0061_drat0.518_Dlon012.6_Cen0.500_h0.22_base1.00_beta041.9_i021.8_Nel15.00_Blat035_Blong336_fr00001.00_fas15.cnj', /Get_lun
file_cnj = strarr(file_lines('C:\Documents and Settings\Projeto_Estudo\I71B16T020.0_B0061_drat0.518_Dlon012.6_Cen0.500_h0.22_base1.00_beta041.9_i021.8_Nel15.00_Blat035_Blong336_fr00001.00_fas15.cnj')) 
ReadF, lun,file_cnj
close, lun

; ------------------------------------
; Restaurando os arquivos .sav e .cnj
; ------------------------------------
restore, 'C:\Documents and Settings\Projeto_Estudo\I71B16T020.0_B0061_drat0.518_Dlon012.6_Cen0.500_h0.22_base1.00_beta041.9_i021.8_Nel15.00_Blat035_Blong336_fr00001.00_fas15.sav'
restore, 'C:\Documents and Settings\Projeto_Estudo\I71B16T020.0_B0061_drat0.518_Dlon012.6_Cen0.500_h0.22_base1.00_beta041.9_i021.8_Nel15.00_Blat035_Blong336_fr00001.00_fas15.cnj'

; ------------------------------------------------------------------
; Salvando em novo arquivo, criando o arquivo binário
; ------------------------------------------------------------------
OpenW, Unit, 'C:\Documents and Settings\Projeto_Estudo\data.bin', /Get_lun, /SWAP_IF_LITTLE_ENDIAN



;-----------------------------------------------------------------------------------------------------------------
; Comparando dimensões e salvando as posições mod_imag[1], mod_imag[3] no arquivo data.bin
;----------------------------------------------------------------------------------------------------------------- 
    conj_imag = size(conjugado.imag, /dim)
    mod_imag = size(modelo.imag, /dim)
    if ((mod_imag[1] eq mod_imag[3]) and (conj_imag[1] eq conj_imag[3])) then begin writeu, unit, mod_imag[1], mod_imag[3] 
    endif else begin 
        print, 'Error: Dimensao de .sav e cnj'
        stop
    endelse


writeu, unit, modelo.imag
close, unit

END

E meu código JAVA:


import java.io.DataInputStream;

import java.io.File;
import java.io.FileInputStream;

public class LeituraArquivo {
  public static void main(String args[]) {

    // Declarando a matriz mat
    double mat[][][][] =  new double[4][16][16][16];


	
    // Chama o metodo lerArquivoBin()
     mat=lerArquivoBin("C:/Documents and Settings/Web/IDLWorkspace/Projeto_Estudo/data.dtb");


    // Imprimindo a matriz mat
    for(int i=0; i< 3; i++) {
             for(int j=0; j< 15; j++) {
            	 for(int k=0; k< 15; k++){
            		 for(int l=0; l< 15; l++){

            	 	 }
            	 }
             }
            
    }
    
 }
  
  
  public static double[][][][] lerArquivoBin(String origem)
  {
     double aux[][][][] = new double[4][16][16][16];

     try{
         File fileOrigin = new File(origem);
         DataInputStream in = new DataInputStream(new FileInputStream(fileOrigin));
         

         
         for(int l=0; l< 15; l++) {
             for(int k=0; k< 15; k++) {
            	 for(int j=0; j< 15; j++){
            		 for(int i=0; i< 4; i++){
            			 aux[i][j][k][l] = (double)in.readDouble();
            			 System.out.print(" " + aux[i][j][k][l]);
            			 
            		 }
            	 }
             }
         }
        
         
         in.close();
     }catch(Exception e){
         System.out.println("Erro ao ler arquivo: "+origem);
         e.printStackTrace();
         aux = null;
     }
     return(aux);
     

  }
}

Daí pensei em fazer algo assim, ler o binário no Java, pegar o valor das posições mod_imag[1], mod_imag[3] e guardar em uma variavel, e depois usar no for

Exemplo: [code]
X= mod_imag[1];
Y = mod_imag[3];

double mat[][][][] = new double[4][X][X][Y];

[/code]

Só que sei também que as posições mod_imag[1], mod_imag[3] são do tipo LONG no IDL.

Alguém pode me ajudar?
Obrigada

Olá carol,

se você garante que consegue capturar o valor de X e Y, você pode fazer o seguinte:


    // x e y como atributos
    private int X;
    private int Y;

    .
    .    
    .
    LeituraArquivo leitura = new LeituraArquivo ();

   .
   .
   .

   mat=lerArquivoBin("C:/Documents and Settings/Web/IDLWorkspace/Projeto_Estudo/data.dtb");

    // impressão
    for(int i=0; i< 3 ; i++) {  
             for(int j=0; j< leitura.x ; j++) {  
                 for(int k=0; k< leitura.x; k++){  
                     for(int l=0; l< leitura.y; l++){  
  
                     }  
                 }  
             }  
    }

    // seta x e y
    leitura.x= mod_imag[1];  
    leitura.y = mod_imag[3];  

    // leitura
    for(int l=0; l< leitura.y; l++) {  
             for(int k=0; k< leitura.x; k++) {  
                 for(int j=0; j< leitura.x; j++){  
                     for(int i=0; i< 4; i++){  
                         aux[i][j][k][l] = (double)in.readDouble();  
                         System.out.print(" " + aux[i][j][k][l]);  
                           
                     }  
                 }  
             }  
         }

Veja se isso te ajuda! :slight_smile:

Olá carol,

se você garante que consegue capturar o valor de X e Y, você pode fazer o seguinte:


    // x e y como atributos
    private int x;
    private int y;

    .
    .    
    .
    LeituraArquivo leitura = new LeituraArquivo ();

   .
   .
   .

   mat=lerArquivoBin("C:/Documents and Settings/Web/IDLWorkspace/Projeto_Estudo/data.dtb");

    // impressão
    for(int i=0; i< 3 ; i++) {  
             for(int j=0; j< leitura.x ; j++) {  
                 for(int k=0; k< leitura.x; k++){  
                     for(int l=0; l< leitura.y; l++){  
  
                     }  
                 }  
             }  
    }

    // seta x e y
    leitura.x = mod_imag[1];  
    leitura.y = mod_imag[3];  

    // leitura
    for(int l=0; l< leitura.y; l++) {  
             for(int k=0; k< leitura.x; k++) {  
                 for(int j=0; j< leitura.x; j++){  
                     for(int i=0; i< 4; i++){  
                         aux[i][j][k][l] = (double)in.readDouble();  
                         System.out.print(" " + aux[i][j][k][l]);  
                           
                     }  
                 }  
             }  
         }

Veja se isso te ajuda! :slight_smile:

|

Não, eu não disse que garanto que consigo capturar o valor de X e Y, disse que pensei em fazer dessa forma…no entanto não sei se daria certo…mais pelo q estou vendo não vai dar certo…pq esta dando alertas ao passar mod_imag[1]. E uma outra coisa, gravo essas duas posições no binário, então ele não grava dessa forma no binário. No caso ele vai gravar o valor de mod_imag[1] e mod_imag[3] .

Apareceu os seguinte alertas nas linhas que você disse para inserir:

  • Illegal modifier for parameter y; only final is permitted
  • x cannot be resolved or is not a field
  • y cannot be resolved or is not a field
    para
// x e y como atributos   
private int x;   
private int y;  


// leitura   
for(int l=0; l< leitura.y; l++) {     
         for(int k=0; k< leitura.x; k++) {     
             for(int j=0; j< leitura.x; j++){     
                 for(int i=0; i< 4; i++){     
                     aux[i][j][k][l] = (double)in.readDouble();     
                     System.out.print(" " + aux[i][j][k][l]);     
                         
                 }     
             }     
         }     
     }  

E se fizer algo assim, só que também estou um pouco perdidada nessa:


import java.io.DataInputStream;

import java.io.File;
import java.io.FileInputStream;

public class LeituraArquivo {
  public static void main(String args[]) {

    // Declarando a matriz mat

	double[][][][] mat; 
	  

	  LeituraArquivo leitura = new LeituraArquivo (); 

	
    // Chama o metodo lerArquivoBin()
     mat=lerArquivoBin("C:/Documents and Settings/Web/IDLWorkspace/Projeto_Estudo/data.dtb");


    // Imprimindo a matriz mat
    for(int i=0; i< 3; i++) {
             for(int j=0; j< leitura.x; j++) {
            	 for(int k=0; k< leitura.x; k++){
            		 for(int l=0; l< leitura.y; l++){

            	 	 }
            	 }
             }
            
    }
    
 }
  
  
  public static double[][][][] lerArquivoBin(String origem)
  {
	 double[][][][] aux;
     try{
         File fileOrigin = new File(origem);
         DataInputStream in = new DataInputStream(new FileInputStream(fileOrigin));
         
         final int x = in.readInt();  
         final int y = in.readInt();  
         aux = new double[4][x][x][y];
         
         for(int l=0; l< y; l++) {
             for(int k=0; k< x; k++) {
            	 for(int j=0; j< x; j++){
            		 for(int i=0; i< 4; i++){
            			 aux[i][j][k][l] = (double)in.readDouble();
            			 System.out.print(" " + aux[i][j][k][l]);
            			 
            		 }
            	 }
             }
         }
         System.out.println("O valor da  "+ aux[3][3][10][13]);
         
         in.close();
     }catch(Exception e){
         System.out.println("Erro ao ler arquivo: "+origem);
         aux = null;
     }
     return(aux);
     

  }
}

O meu problema é como pegar esses valores que chamo de X e Y lá no binário.

Por que essa matriz que tenho de [4, 16, 16, 16] pode ter variações como por exemplo: [4, 14, 14, 17], ou ainda [4, 17, 17, 12]. Ou seja, sempre irá variar dessa forma