/*This sketch imports xml data generated by a php script that reads weather data from the victoria school weather network, and assigns colors to the different stations */ import proxml.*; //to store the background after painting an ellipse PImage back; //xml element to store and load the drawn ellipses XMLElement wxobs; XMLInOut xmlInOut; int xPos = 0; int yPos = 0; int[] stationArr = new int[100]; float[] temperatureArr = new float[100]; int[] winddirArr = new int[100]; //int[] stationXArr = new int[100]; //int[] stationYArr = new int[100]; float[] stationXArr = new float[100]; float[] stationYArr = new float[100]; float[] windSpeedArr = new float[100]; int stationCount = 0; int hval = 0; int sval = 100; int bval = 100; //int ypos = 40; //int xpos = 0; float yposf = 40; float xposf = 0; int blobsize = 40; int movestep = 3; float minVal = 40; float maxVal = 0; float hfactor = 0; void setup(){ size(400,400); frameRate(24); smooth(); //noStroke(); background(000); colorMode(HSB, 100); PFont font; font = loadFont("arial-black.vlw"); textFont(font, 14); textAlign(CENTER); //load ellipses from file if it exists try{ xmlInOut = new XMLInOut(this); //wxobs = xmlInOut.loadElementFrom("http://www.alphapure.net/wxcontest/test.php"); wxobs = xmlInOut.loadElementFrom("wxobs_old.xml"); initWx(); } catch(InvalidDocumentException ide){ println("File does not exist"); } //initialise PImage for background back = new PImage(width,height); loadPixels(); back.pixels = pixels; //noLoop(); //populate initial station position arrays for (int i = 0; i < stationCount-1; i++){ xposf += 40; if (xposf>380){ xposf = 40; yposf+=40; } stationXArr[i] = xposf; stationYArr[i] = yposf; //stationXArr[i] = 200; //stationYArr[i] = 200; } for (int i = 0; i < stationCount; i++){ if (temperatureArr[i] > maxVal){ maxVal = temperatureArr[i]; } if (temperatureArr[i] < minVal){ minVal = temperatureArr[i]; } } float gap = maxVal - minVal; float hfactor = 88/gap; } void draw(){ //background(0); fill(0,0,0,10); rect(0,0,400,400); float minVal = 40; float maxVal = 0; for (int i = 0; i < stationCount; i++){ if (temperatureArr[i] > maxVal){ maxVal = temperatureArr[i]; } if (temperatureArr[i] < minVal){ minVal = temperatureArr[i]; } } float gap = maxVal - minVal; float hfactor = 88/gap; String theTemp = "0"; for (int i = 0; i < stationCount-1; i++){ hval = int((temperatureArr[i]-minVal)*hfactor); hval = 88-hval; //get the current xy xposf = stationXArr[i]; yposf = stationYArr[i]; //get the wind speed float windSpeed = windSpeedArr[i]; //calculate new xy based on wind value // Convert polar to cartesian float winddirRadians = radians(winddirArr[i]+90); //float x = movestep * cos(winddirRadians); //float y = movestep * sin(winddirRadians); float x = windSpeed * cos(winddirRadians); float y = windSpeed * sin(winddirRadians); //println(x+"; "+y); xposf = xposf + x; yposf = yposf + y; if(xposf > 420) { xposf = -20;} if(yposf > 420) { yposf = -20;} if(xposf < -20) { xposf = 420;} if(yposf < -20) { yposf = 420;} //if((xposf > 420)|| (yposf > 420) || (xposf < -20) || (yposf < -20)){ // xposf = 200; // yposf = 200; //} //stationXArr[i] = int(xposf); //stationYArr[i] = int(yposf); stationXArr[i] = xposf; stationYArr[i] = yposf; //println(stationArr[i]+","+winddirRadians+": "+xposf+"; "+yposf+"| "+x+"; "+y); //fill(hval,sval,bval); stroke(hval,sval,bval); if (windSpeed == 0){ ellipse(xposf, yposf, blobsize, blobsize); }else{ arc(xposf, yposf, blobsize, blobsize, winddirRadians-PI/2, winddirRadians+PI/2); } //fill(hval,70,30); //text(winddirArr[i], xpos, ypos+5); //theTemp = nf(temperatureArr[i],1,1); //text(theTemp, xposf, yposf+5); } } // void initWx(){ //wxobs.printElementTree(" "); XMLElement readings; XMLElement station; XMLElement temperature; XMLElement winddir; XMLElement windspeed; //stationCount = wxobs.countChildren(); stationCount = 100; //debug println(stationCount+" stations"); for(int i = 0; i < stationCount-1; i++){ readings = wxobs.getChild(i); station = readings.getChild(0); temperature = readings.getChild(1); winddir = readings.getChild(2); windspeed = readings.getChild(3); String s = station.getChild(0).toString(); String t = temperature.getChild(0).toString(); String wd = winddir.getChild(0).toString(); String ws = windspeed.getChild(0).toString(); int s_int = int(s); float t_float = float(t); int wd_int = int(wd); float ws_float = float(ws); stationArr[i] = s_int; //temperature = readings.getChild(1); temperatureArr[i] = t_float; //println(station.getChild(0)+": "+temperature.getChild(0)); winddirArr[i] = wd_int; //println(stationArr[i]+": "+temperatureArr[i]+", "+winddirArr[i]); //println(si+": "+ti); windSpeedArr[i] = ws_float; println(stationArr[i]+": "+temperatureArr[i]+", "+winddirArr[i]+", "+windSpeedArr[i]); //println(si+": "+ti); } }