Class: ClimateTracker::NOAA_Data

Inherits:
Object
  • Object
show all
Defined in:
lib/climate_tracker/noaa_data.rb

Constant Summary collapse

@@states =
{}
@@header =
{ "token" => "JPhvnfSrGIAesNPlFwRxKFsZTwPuYoum" }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeNOAA_Data

Returns a new instance of NOAA_Data.



7
8
9
10
11
12
13
14
# File 'lib/climate_tracker/noaa_data.rb', line 7

def initialize
	@data_type = "MNTM"
	@pull_count = 0
	@re_pull = true
	#Possible Data Types
	# > MMNT, MMXT, MNTM (Min T, Max T, and Avg T monthly)
	# > TCPC (Total Precip) TSNW (Total snowfall)
end

Instance Attribute Details

#data_avgObject

Returns the value of attribute data_avg.



2
3
4
# File 'lib/climate_tracker/noaa_data.rb', line 2

def data_avg
  @data_avg
end

#data_dumpObject

Returns the value of attribute data_dump.



2
3
4
# File 'lib/climate_tracker/noaa_data.rb', line 2

def data_dump
  @data_dump
end

#data_typeObject

Returns the value of attribute data_type.



2
3
4
# File 'lib/climate_tracker/noaa_data.rb', line 2

def data_type
  @data_type
end

#delta_tempObject

Returns the value of attribute delta_temp.



2
3
4
# File 'lib/climate_tracker/noaa_data.rb', line 2

def delta_temp
  @delta_temp
end

#pull_countObject

Returns the value of attribute pull_count.



2
3
4
# File 'lib/climate_tracker/noaa_data.rb', line 2

def pull_count
  @pull_count
end

#re_pullObject

Returns the value of attribute re_pull.



2
3
4
# File 'lib/climate_tracker/noaa_data.rb', line 2

def re_pull
  @re_pull
end

#year1_avgsObject

Returns the value of attribute year1_avgs.



2
3
4
# File 'lib/climate_tracker/noaa_data.rb', line 2

def year1_avgs
  @year1_avgs
end

#year2_avgsObject

Returns the value of attribute year2_avgs.



2
3
4
# File 'lib/climate_tracker/noaa_data.rb', line 2

def year2_avgs
  @year2_avgs
end

Class Method Details

.statesObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/climate_tracker/noaa_data.rb', line 16

def self.states
	if @@states.empty?
		#populate available states
		uri = URI.parse("http://www.ncdc.noaa.gov/cdo-web/api/v2/locations?locationcategoryid=ST&limit=52")
		request = Net::HTTP::Get.new(uri.request_uri, initheader = @@header)
		http = Net::HTTP.new(uri.host, uri.port).start 
		response = http.request(request)
		data = JSON.parse(response.body) 
		data["results"].collect do |result|
			st_name = result["name"].upcase
			st_id = result["id"]
			@@states[st_name] = st_id
		end
	end
	@@states.keys
end

Instance Method Details

#gather_valuesObject



56
57
58
59
60
61
62
63
64
65
# File 'lib/climate_tracker/noaa_data.rb', line 56

def gather_values
	total_values = 0.000
	@data_dump["results"].each do |result| 
		total_values += result["value"].to_f
	end

	@data_avg = (((total_values / @data_dump["results"].size)*(9.0/5.0))+32.0)
	
	@data_avg #float
end

#pull_data(date, state) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/climate_tracker/noaa_data.rb', line 33

def pull_data(date, state)
	#NOAA program requires range of 1 year for dataset ANNUAL. Create 1 year ago range from given date
	date_array = date.split("-")
	date_array[0] = date_array[0].to_i-1
	last_year_date = date_array.join("-")
	year_date = date

	#retrieve appropriate state_code and download temperatures for above range
	if @@states.empty?
		self.class.states
	end
	state_code = @@states[state]
	uri = URI.parse("http://www.ncdc.noaa.gov/cdo-web/api/v2/data?datasetid=ANNUAL&datatypeid=#{@data_type}&locationid=#{state_code}&startdate=#{last_year_date}&enddate=#{year_date}&units=metric&limit=1000")
	request = Net::HTTP::Get.new(uri.request_uri, initheader = @@header)
	http = Net::HTTP.new(uri.host, uri.port).start 
	response = http.request(request)
	@data_dump = JSON.parse(response.body) #returns are only for the month of the years in which this were called.  (ie. startdate XXXX-02-01 will only display February) ur
	
	@pull_count += 1
	@re_pull = false
	self
end

#temp_difference(year1, year2, state) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/climate_tracker/noaa_data.rb', line 67

def temp_difference(year1, year2, state)
	if self.re_pull == true 
		@year1_avgs = self.pull_data(year1, state).gather_values 
	elsif @year1_avgs == nil
		@year1_avgs = @data_avg
	elsif @year2_avgs != @data_avg
		@year1_avgs = @data_avg
	end

	@year2_avgs = self.pull_data(year2, state).gather_values
	delta_temp = (@year2_avgs - @year1_avgs).round(2) 
	delta_percent = ((@year2_avgs/@year1_avgs)*100).round(2)
	if delta_temp > 0 #if delta_temp is positive than temp went up
		delta_descr = "warmer"
		delta_descr_2 = "increase"
	else 
		delta_descr = "colder"
		delta_descr_2 = "decrease"
	end

	@delta_temp = [delta_temp, delta_percent, delta_descr, delta_descr_2] #hash[state] = [temp change, %, warmer/colder, increase/decrease]

	@delta_temp
end