0 Replies Latest reply on Oct 24, 2008 2:50 PM by 843785

    copy to arrays instead to a file..

    843785
      here in the below code I just copied data from one file to another file(after some modifications)...but now I would like to copy into a single double 2darray ....I tried my level best to do that but cannot succeed...so can any one help...
       import java.io.*;
      public class window_averager
      {
      
              public void calc_window_means(String read_file, int window_size)
              {
                      final int kPosition_index = 0;
                      final int kRun_value_index = 1;
                      final int kMaxLines = 60000;
                      String newfilename =  read_file + "_" + window_size;
                      System.out.println(newfilename);
                      try{
                        FileInputStream fstream = new FileInputStream(read_file);
                        FileInputStream fstream1 = new FileInputStream(read_file);
                      DataInputStream in = new DataInputStream(fstream);
                      DataInputStream in1 = new DataInputStream(fstream1);
                      BufferedReader br = new BufferedReader(new InputStreamReader(in));
                      BufferedReader br1 = new BufferedReader(new InputStreamReader(in1));
                      BufferedWriter bufferedWriter = null;
                      bufferedWriter = new BufferedWriter(new FileWriter(newfilename+".txt"));
      
                      // DO NOT WRITE TO A FILE. WRITE TO YOUR VALUES ARRAYS. BEGIN BY DETERMINING THE NUMBER OF WINDOWS.
                      // DO THIS BY FINDING THE FIRST AND LAST VALUES IN THE FILE AND DIVIDING THEIR DIFFERENCE BY THE WINDOW SIZE.
                      String temp_strs[] = new String[2];              
                          double positions[]=new double[kMaxLines];
                          double means[]=new double[kMaxLines];
      
                          double cumul_in_cur_window = 0;
                      int window_index = 1;
                      int first_index_of_next_window = window_size;
                      int prev_index = 0;
                      double prev_run_value = 0;
                      String strLine;
                      String strLines;
                      int lastLineNo = 0;
      
                      while ((strLine = br1.readLine()) != null) {
                          lastLineNo++;
                      }
                      System.out.println(lastLineNo);   
      
      
                      for(int i = 1; i < lastLineNo; i++)
                      {
                                      strLines = br.readLine();
                                      temp_strs = strLines.split("\t");
                                      int current_pos_index = Integer.parseInt(temp_strs[kPosition_index]);
                                      double run_value = Double.parseDouble(temp_strs[kRun_value_index]);
                                      if (current_pos_index < first_index_of_next_window)
                                        {
                                                      cumul_in_cur_window += (current_pos_index - prev_index) * prev_run_value;
                                        }
                                      else
                                        {  
                                        // first case: pos index is in the next window. Write average for prev window,
                                        // initialize cumul for new window.
                                                 double closing_total = (first_index_of_next_window - prev_index) * prev_run_value;
                                          cumul_in_cur_window += closing_total;
                                          double mean_value = cumul_in_cur_window / window_size;
                                          positions[window_index - 1] = window_index; // The -1 is to make the indices zero-offset
                                          means[window_index - 1] = mean_value;
      
                                          bufferedWriter.append(""+window_index+"");
                                          bufferedWriter.append("\t");
                                          bufferedWriter.append(""+mean_value+"");
                                          bufferedWriter.append("\n");
      
      
                                          //results[1]=mean_value;
      window_index++;
      prev_index = first_index_of_next_window;
      first_index_of_next_window += window_size;
      while (first_index_of_next_window < current_pos_index)
      {
      positions[window_index - 1] = window_index; // The -1 is to make the indices zero-offset
      means[window_index - 1] = prev_run_value;
      bufferedWriter.append(""+window_index+"");
      bufferedWriter.append("\t");
      bufferedWriter.append(""+prev_run_value+"");
      bufferedWriter.append("\n");
      // results[0][i] = prev_run_value;
      window_index++;
      prev_index = first_index_of_next_window;
      first_index_of_next_window += window_size;
      }
      cumul_in_cur_window = (current_pos_index - prev_index) * prev_run_value;
      }
      prev_index = current_pos_index;
      prev_run_value = run_value;


      }

      br.close();
      bufferedWriter.close();
      }
      catch (Exception e){//Catch exception if any
      e.printStackTrace();
      }
      }
      /*
      public static void main(String args[])
      {
      window_averager wa= new window_averager();
      wa.calc_window_means("d:/java_files/allSB2_window_10000.txt",50);
      }*/

      }