RWEL HW- rideshare data (also messing around with sound)
playing around with python’s ability to write .wav file – I’d like to redo my midterm in python, writing two different sin waves, as well as reading through a text’s binary data and having a tone play on 1 and nothing on 0… here’s the start
import sys import math import wave import struct freq = 440.0 data_size = 40000 fname = "WaveTest.wav" frate = 11025.0 # framerate as a float amp = 64000.0 # multiplier for amplitude sine_list_x = [] for x in range(data_size): sine_list_x.append(math.sin(2*math.pi*freq*(x/frate))) wav_file = wave.open(fname, "w") nchannels = 1 sampwidth = 2 framerate = int(frate) nframes = data_size comptype = "NONE" compname = "not compressed" wav_file.setparams((nchannels, sampwidth, framerate, nframes, comptype, compname)) for s in sine_list_x: # write the audio frames to file wav_file.writeframes(struct.pack('h', int(s*amp/2))) wav_file.close()
Additionally, I’m doing more with the code to extract zip code data: I had not been printing the city and state in my processing sketch earlier, but now I am-
[‘43068’, ‘43215’] (zip codes, origin and destination of rideshare requests)
82.803454 39.955145 83.004383 39.967106 (data of gps lats / longs from the zip codes – I had been using after cleaning it with python)
80.281567 40.604424 MACARTHUR PA 80.073393 40.438045 CRAFTON PA (data I will now use, using the new code – see below)
where the first zip code is the origin of a rideshare request, and the latter is a destination point: gps lat., gps long., city name, state name, from a zip code
Now I just need to figure out how to do this through processing (in other words, I don’t want to have to run this code to pull the data from online in order to generate the text file that processing can use)
import sys import urllib #define a function def len(array): i=0 for entry in array: i=i+1 return i def clean(line): line = line.replace("\'", "") line = line.replace(" ", "") line = line.replace(",", ".") line = line.replace("(", "") line = line.replace(")", "") line = line.replace("...", ",") line = line.strip("(),'\"") return line def zipToLat(origin, destination, zipCodeMap): #for each origin and destination zipcode that exists in the zipCodeMap, print it out in the format : (latOrigin, lonOrigin; latDestination, lonDestination) if zipCodeMap.has_key(origin) and zipCodeMap.has_key(destination): #print zipCodeMap[origin][0], zipCodeMap[origin][1], zipCodeMap[destination][0], zipCodeMap[destination][1] print zipCodeMap[origin][0],",", zipCodeMap[origin][1],",", zipCodeMap[origin][2], zipCodeMap[origin][3],",", zipCodeMap[destination][0],",", zipCodeMap[destination][1],",",zipCodeMap[destination][2], zipCodeMap[destination][3] #print "yes" #print zipCodeMap[origin]+zipCodeMap[destination] #print origin def maxloc(locationsDict,zipmap): #return string of both values - location and how many times it appears tmp_max_count = -1 tmp_max_value = '' zipMapRF={} for entry in locationsDict: if len(entry)>1: zipcode = entry[0] count = entry[1] if zipmap.has_key(zipcode): #print zipmap[zipcode] + " , " , count #print "%s %s" % (zipmap[entry[0]],entry[1]) zipMapRF[entry[0]] = entry[1] if tmp_max_count < count: tmp_max_count = count tmp_max_value = zipmap[zipcode] #entry 1 num times apprs, 0 is name of location #chek beforehand - does zipmap have this key.. # print locationsDict print "popular location is " + tmp_max_value + " which was visited " , tmp_max_count , " times." return tmp_max_value #now get value and the map zipNames = {} zipCoordinates = {} starts = {} destinations= {} locations={} for line in open('zips.txt'):#can i just load it from http://www.census.gov/tiger/tms/gazetteer/zips.txt tup = line.split(",") #print tup if len(tup)>=4: zip=tup[1][1:-1] state=tup[2][1:-1] town=tup[3][1:-1] lon=tup[4] lat=tup[5] point = lon,lat point = lon,lat, town, state #print point #zipmap[zip]=pair #print zip + " (" + pair[0] + "," + pair[1] + ")" zipNames[zip] = town + ", " + state zipCoordinates[zip] = point #print zipCoordinates #this Now stores most of what I need #print zipmap #print zipNames #print zipCoordinates #f = urllib.urlopen('http://www.erideshare.com/!!.php') #s = f.read() #f.close() f = urllib.urlopen('http://www.erideshare.com/!!.php') lines = f.readlines() f.close() for line in lines:#open('phpErid.txt'):#figure this out for pointPair in line.split("|"): #build concordence pointPair = pointPair.strip() points = pointPair.split(",") #print points #print "points!" #print points if len(points)>=2: dest = points[1]#the second zip code is the dest loc = points[0]#first zip code of the pair destinations[dest]=destinations.get(dest,0) +1 starts[loc] = starts.get(loc,0) +1 locations[dest] = locations.get(dest,0) +1 locations[loc] = locations.get(loc,0) +1 zipToLatValue = zipToLat(loc,dest, zipCoordinates) l=len(locations) topZipCode = maxloc(locations,zipNames) locations = locations.items() maxloc(locations,zipNames) locations.sort #print locations if len(locations)>1: locations.sort() # print locations[0] # print locations[l-1] maxloc(locations,zipNames) #{'M': 1, 's': 4, 'p': 2, 'i': 4} #print zipNames #print zipmap #60647,60120| 60647,60120| L1X2W8,M5S1N5| L1X2W8,M5S1N5| 22601,20041| 22601,20041| 19446,19477| 19446,19477| 19446,19477| 19446,19477| 21224,20019| 90840,92660| 02114,02132| 17047,17128| 94612,94903| 94612,94903| 94612,94903| 62034,62035| 62034,62035| 94110,94403| 94110,94403| 07306,07059|
So here is an image of the processing sketch I have when I write this text file inside of the processing folder
(and here is the sketch in early phases, running sort of slowly)