Creating multiple arrays from a txt file

2»

Answers

  • float P = 0;
    float A = 0;
    float spe[];
    float den[];
    float temp[];
    int DC = 0;
    
    import ddf.minim.*;
    import ddf.minim.ugens.*;
    
    Record[] records;
    
    Minim minim;
    AudioOutput out;
    Oscil wave;
    
    //======================================
    //SETUP
    //======================================
    
    void setup() {
    
      println("Setup");
      size(1887, 1000); 
      println("Loading Data");
      //======================================
      //SOUND DATA
      //======================================
      minim = new Minim(this);
      // use the getLineOut method of the Minim object to get an AudioOutput object
      out = minim.getLineOut();
      //======================================
      //VISUAL DATA
      //====================================== 
      println("");
      //======================================
      String[] rawData =
        loadStrings("http://" + "services.swpc.noaa.gov/text/ace-swepam.txt"); //data
      records = getRecordsFromRawData(rawData);
    }
    
    void draw() {
      showSomeData();
    }
    
    void stop() {
      minim.stop();
      super.stop();
    }
    
    //======================================
    
    void showSomeData() {
    
      float H = 100; //GRID VAR
      float W = 1920; //LENGTH VAR
      // println("Data Loaded");
    
      //======================================
      // DEBUG
      //======================================
      // println("Debug;");
      //    float A = r.temperature/10000;
      //    float B = r.speed/100;
      //    float C = r.density;
      //    println(A);
      //    println(B);
      //    println(C);
      //======================================
      // println("Debug End");
      println("Table Start");
      //======================================
      //grid
      //====================================== 
      fill(175, 175, 175); //Gray
      stroke(175, 175, 175); //Gray outline
      rect(32*3, 0, 0, 1920); //side
      //======================================
      //HOR
      //======================================
      stroke(170, 170, 170);
      rect(101+(15*3), 0, 0, W);
      rect(101+(15*7), 0, 0, W);
      rect(101+(15*11), 0, 0, W);
      rect(101+(15*15), 0, 0, W);
      rect(101+(15*19), 0, 0, W);
      rect(101+(15*23), 0, 0, W);
      rect(101+(15*27), 0, 0, W);
      rect(101+(15*31), 0, 0, W);
      rect(101+(15*35), 0, 0, W);
      rect(101+(15*39), 0, 0, W);
      rect(101+(15*43), 0, 0, W);
      rect(101+(15*47), 0, 0, W);
      rect(101+(15*51), 0, 0, W);
      rect(101+(15*55), 0, 0, W);
      rect(101+(15*59), 0, 0, W);
      rect(101+(15*63), 0, 0, W);
      rect(101+(15*67), 0, 0, W);
      rect(101+(15*71), 0, 0, W);
      rect(101+(15*75), 0, 0, W);
      rect(101+(15*79), 0, 0, W);
      rect(101+(15*83), 0, 0, W);
      rect(101+(15*87), 0, 0, W);
      rect(101+(15*91), 0, 0, W);
      rect(101+(15*95), 0, 0, W);
      rect(101+(15*99), 0, 0, W);
      rect(101+(15*103), 0, 0, W);
      rect(101+(15*107), 0, 0, W);
      rect(101+(15*111), 0, 0, W);
      rect(101+(15*115), 0, 0, W);
      rect(101+(15*119), 0, 0, W);
      rect(101+(15*121), 0, 0, W);
      //======================================
      //VER
      //======================================
      rect(32*3, H*1, W, 0);
      rect(32*3, H*2, W, 0);
      rect(32*3, H*3, W, 0);
      rect(32*3, H*4, W, 0);
      rect(32*3, H*5, W, 0);
      rect(32*3, H*6, W, 0);
      rect(32*3, H*7, W, 0);
      rect(32*3, H*8, W, 0);
      rect(32*3, H*9, W, 0);
      //======================================
      rect(32*3, H*10, W, 0);
      rect(32*3, H*11, W, 0);
      rect(32*3, H*12, W, 0);
      rect(32*3, H*13, W, 0);
      rect(32*3, H*14, W, 0);
      rect(32*3, H*15, W, 0);
      rect(32*3, H*16, W, 0);
      rect(32*3, H*17, W, 0);
      rect(32*3, H*18, W, 0);
      rect(32*3, H*19, W, 0);
      rect(32*3, H*20, W, 0); 
      println("");
      println("Done");
      //======================================
      //Lines Default
      //======================================
      int i=32; //Data offset
      println("");
      println("Loop Start");
    
      for (Record r : records) {
        // println(r.density, r.speed, r.temperature, r.time); //Debug Storage
        //======================================
        // Bottom Half
        if (r.temperature > 0) { //filter 1
          if (r.speed > 0) { //filter 2
            stroke(255, 0, 255);
            line(i*3+5, r.temperature * .001 + 500, i*3+5, r.speed*1-100); //Line Calc
          }
        }
        //======================================
        // Top Half
        if (r.density > 0) { //filter 1
          if (r.speed > 0) { //filter 2
            stroke(0, 255, 255);
            line(i*3+5, r.density * 100 + 100, i*3+5, r.speed*1-100); //Line Calc
          }
        }
        //======================================
        //Time Overlay
        //======================================
        if (P < 3) {
          P = P + 1;
        } else {
          fill(100, 100, 100);
          text(r.time, (i*3)+8, 994);
          P = 0;
        }   
        //======================================
        //Line Corrections
        //======================================
        // Overlay 1A
        if ((r.density * 100 + 100) > (r.temperature * .001 + 400)) {
          stroke(255, 0, 255);
          line(i*3+5, r.speed*1-100, i*3+5, r.temperature * .001 + 500); //Line Calc
          //println("< H1 CY >");
        }
        // Overlay 1B
        if ((r.temperature * .001 + 500) > (r.density * 100 + 100)) {
          stroke(0, 255, 255);
          line(i*3+5, r.speed*1-100, i*3+5, r.density * 100 + 100); //Line Calc
          //println("< H1 MA >");
        }
        // Overlay 2
        if ((r.temperature * .001 + 500) < (r.speed*1-100)) {
          stroke(255, 0, 255);
          line(i*3+5, r.speed*1-100, i*3+5, r.temperature * .001 + 500); //Line Calc
          //println("< H2 CY >");
        }
        line(i*3+5, 900, i*3+5, 900); //seperator
    
        float cor = r.temperature;
        if (cor < 0) {
          stroke(255, 100, 100);
          fill(255, 100, 100);
          rect(i*3, 0, 10, 980);
        }
        //======================================
        //CALCULATIONS
        //====================================== 
        if (r.speed > -1) {
          fill(100, 100, 100); // white
          //    stroke(100, 100, 100); // white
          stroke(100, 100, 100); // Gray
          ellipse(5 + i*3, (r.speed*1-100), 4, 4); //SPEED
          // filter
        } else {
          ellipse(5 + i*3, 400, 4, 4);
          println("Invalid data (SPEED)");
        } //debug
        //======================================
        if (r.temperature > -1) {
          fill(255, 0, 250); // magenta
          //    stroke(255, 0, 250); // magenta
          stroke(100, 100, 100); // Gray
          ellipse(5 + i*3, (r.temperature * .001 + 500), 4, 4); // TEMP
          // filter
        } else {
          ellipse(5 + i*3, 400, 4, 4);
          println("Invalid data (TEMP)");
        } //debug
    
        if (r.temperature < 59000) {
          r.temperature = 59000;
        }
        //======================================
        if (r.density > -1) {
          fill(0, 255, 255); // cyan
          //    stroke(0, 255, 255); // cyan
          stroke(100, 100, 100); // Gray
          ellipse(5 + i*3, (r.density * 100 + 100), 4, 4); //DESNITY
          // filter
        } else {
          ellipse(5 + i*3, 400, 4, 4);
          println("Invalid data (DENSITY)");
        } //debug
        //======================================
        i+=5; // loop increase
        //====================================== 
        //OVERLAY
        //====================================== 
        // etc
        fill(150, 150, 150); // gray
        text("+", 4, 995);
    
        text("h h m m", 100, 983); //bottom left
        text("T I M E:", 100, 995);
        //======================================
        stroke(100, 100, 100); // cyan
        fill(100, 100, 100); // black
        text("W I N D  S P E E D  D A T A", 4, 14); //title
        //======================================
        //SIDEBAR
        //======================================
        text("0 . 0  p / c c", 4, 128);
        text("1 . 0  p / c c", 4, 228);
        text("2 . 0  p / c c", 4, 328);
        text("3 . 0  p / c c", 4, 428);
        text("4 . 0  p / c c", 4, 528);
        text("5 . 0  p / c c", 4, 628);
        text("6 . 0  p / c c", 4, 728);
        text("7 . 0  p / c c", 4, 828);
        text("2 0 0  k m / s", 4, 114);
        text("3 0 0  k m / s", 4, 214);
        text("4 0 0  k m / s", 4, 314);
        text("5 0 0  k m / s", 4, 414);
        text("6 0 0  k m / s", 4, 514);
        text("2 0 0 0 0 0  k", 4, 642);
        text("-2 0 0 0 0 k", 4, 342);
        text("-1 0 0 0 0 k ", 4, 442);
        text("1 0 0 0 0  k  ", 4, 542);
        text("7 0 0  k m / s", 4, 614);
        text("3 0 0 0 0 0  k", 4, 714);
        text("4 0 0 0 0 0  k", 4, 814);
        text("5 0 0 0 0 0  k", 4, 914);
        //======================================
        //DESNITY
        //====================================== 
        // cyan
        fill(0, 255, 255);  
        stroke(100, 100, 100); // gray outline
        rect(80, 118, 12, 12);
        rect(80, 218, 12, 12);
        rect(80, 318, 12, 12);
        rect(80, 418, 12, 12);
        rect(80, 518, 12, 12);
        rect(80, 618, 12, 12);
        rect(80, 718, 12, 12);
        rect(80, 818, 12, 12);
        //key
        rect(80, 956, 12, 12);
        text("D e n s i t y", 12, 966);
        //======================================
        //speed
        //====================================== 
        // gray
        fill(100, 100, 100); 
        rect(80, 104, 12, 12);
        rect(80, 204, 12, 12);
        rect(80, 304, 12, 12);
        rect(80, 404, 12, 12);
        rect(80, 504, 12, 12);
        rect(80, 604, 12, 12);
        //key
        rect(80, 970, 12, 12);
        text("S p e e d", 25, 978);
        //======================================
        //temp
        //====================================== 
        // magenta
        fill(255, 0, 255); 
        rect(80, 332, 12, 12);
        rect(80, 432, 12, 12);
        rect(80, 532, 12, 12);
        rect(80, 632, 12, 12);
        rect(80, 704, 12, 12);
        rect(80, 804, 12, 12);
        rect(80, 904, 12, 12);
        //key
        rect(80, 984, 12, 12);
        text("T e m p ", 32, 990);
        //======================================
        //Error Count
        //====================================== 
        if (r.speed < -1) {
          DC = DC + 1 ;
        }
      }//for 
      //======================================
    
      // noLoop(); // loop end
      println("");
      println("Loop End");
      println("Error Count : ", DC);
      println("");
      println("=============== debug ===============");
    }
    
    //======================================
    //storage setup A
    //======================================
    
    Record[] getRecordsFromRawData(String[] data) {
      ArrayList<Record> r = new ArrayList<Record>();
      for (String line : data) { //var
        char c = line.charAt(0); // data line
        if (c >= '0' && c <= '9') {
          r.add(new Record(line));
        }
      }
      return r.toArray(new Record[r.size()]);
    }
    
    //======================================
    //Storage setup B
    //======================================
    
    class Record {
      float density, speed, temperature; //var
      int time;
      //======================================
      public Record(String lineOfData) {
        String[] items = lineOfData.split("\\s+");
        //======================================
        density = float(items[7]); //cyan
        speed = float(items[8]); //gray
        temperature = float(items[9]); //magenta
        int hhmm = int(items[3]);
        time = hhmm;
        //println(hhmm);
      }
    }
    
    //======================================
    //Mouse 
    //======================================
    
    void mousePressed() {
    
      int X = mouseX;
      int Y = mouseY;
    
      //======================================
      int s = 15;
      int l = 0;
      //println(X);
      //======================================
      //Sound Playback
      //======================================
    
      int i=32; //Data offset
      for ( Record r : records) {
    
        if (dist(X, 0, i*3+5, 0) < 8) {
    
          //println("");
          //println(r);
          //======================================
          float S = records[l].speed;
          println("Speed : ", S);
          float D = records[l].density;
          println("Density : ", D);
          float T = records[l].temperature;
          println("Temperature : ", T);
          //======================================
          int A = ((int)r.speed/100)+100;
          //println("Speed Playback Tone : ", A);
          //======================================
          if (wave!=null&&out!=null) {
            wave.unpatch(out);
          }
          //======================================
          if (A < 10000) {
            if (wave!=null&&out!=null) {
              wave.unpatch(out);
            }
            wave = new Oscil((int)map(r.speed, 220, 500, 440, 1000), 0.5f, Waves.SINE );
            wave.patch(out);
            println(r.speed); 
            // delay(s*100);
          } else {
            println("Invalid data set");
          }
          break;
          //======================================
        }
        l = l + 1;
        i=i+5;
      }//for
    }//func 
    
    //======================================
    //END
    //======================================
    
Sign In or Register to comment.