1717plt .ion ()
1818
1919
20- def get_filtered_isd (data_dir , s_date = None , f_date = None ,
21- allow_download = True ):
22- fname = 'isd-history.csv'
20+ def get_filtered_isd (data_dir , s_date = None , f_date = None , allow_download = True ):
21+ fname = "isd-history.csv"
2322 target_file = os .path .join (data_dir , fname )
2423
2524 os .makedirs (data_dir , exist_ok = True )
2625 if not os .path .exists (target_file ) and allow_download :
27- url_target = ' ftp://ftp.ncdc.noaa.gov/pub/data/noaa/isd-history.csv'
28- with open (target_file , 'wb' ) as fout :
26+ url_target = " ftp://ftp.ncdc.noaa.gov/pub/data/noaa/isd-history.csv"
27+ with open (target_file , "wb" ) as fout :
2928 print (url_target )
3029 fout .write (urlopen (url_target ).read ())
3130
3231 isd_history = pd .read_csv (target_file )
3332 if s_date is not None :
34- isd_history = isd_history [isd_history [' BEGIN' ] < s_date ]
33+ isd_history = isd_history [isd_history [" BEGIN" ] < s_date ]
3534 if f_date is not None :
36- isd_history = isd_history [isd_history [' END' ] > f_date ]
35+ isd_history = isd_history [isd_history [" END" ] > f_date ]
3736
3837 return isd_history
3938
4039
4140def extract_date_time (row ):
42- '''
43- '''
44- fmt_str = '%Y%m%d%H%M'
41+ """"""
42+ fmt_str = "%Y%m%d%H%M"
4543 dt = datetime .datetime .strptime (row [15 :27 ], fmt_str )
4644 return dt , dt .year , dt .month , dt .day , dt .hour
4745
4846
4947def extract_temperature (row ):
5048 t = int (row [87 :92 ])
5149 if t == 9999 :
52- return (np .nan , )
50+ return (np .nan ,)
5351 return (t / 10 ,)
5452
5553
5654def injest_file (fname ):
57- with gzip .open (fname , 'rt' , encoding = 'ascii' ) as f :
58- data = [extract_date_time (ln ) + extract_temperature (ln )
59- for ln in f ]
55+ with gzip .open (fname , "rt" , encoding = "ascii" ) as f :
56+ data = [extract_date_time (ln ) + extract_temperature (ln ) for ln in f ]
6057
61- return pd .DataFrame (data ,
62- columns = (' datetime' , ' year' , ' month' ,
63- 'day' , 'hour' , 'T' ) ).dropna ()
58+ return pd .DataFrame (
59+ data , columns = (" datetime" , " year" , " month" , "day" , "hour" , "T" )
60+ ).dropna ()
6461
6562
66- def get_hourly_data (data_dir , template , years , allow_download = True ,
67- urlbase = 'ftp://ftp.ncdc.noaa.gov/pub/data/noaa/{year}' ):
63+ def get_hourly_data (
64+ data_dir ,
65+ template ,
66+ years ,
67+ allow_download = True ,
68+ urlbase = "ftp://ftp.ncdc.noaa.gov/pub/data/noaa/{year}" ,
69+ ):
6870
69- data_dir_template = os .path .join (data_dir , ' {year}' )
71+ data_dir_template = os .path .join (data_dir , " {year}" )
7072 target_template = os .path .join (data_dir_template , template )
71- url_template = '/' .join ((urlbase , template ))
73+ url_template = "/" .join ((urlbase , template ))
7274 data = []
7375
7476 for year in years :
@@ -78,31 +80,32 @@ def get_hourly_data(data_dir, template, years, allow_download=True,
7880
7981 if not os .path .exists (target_file ) and allow_download :
8082 url_target = url_template .format (year = year )
81- with open (target_file , 'wb' ) as fout :
83+ with open (target_file , "wb" ) as fout :
8284 print (url_target )
8385 fout .write (urlopen (url_target ).read ())
8486
8587 data .append (injest_file (target_file ))
8688 data = pd .concat (data )
87- data .set_index (' datetime' , inplace = True )
89+ data .set_index (" datetime" , inplace = True )
8890 return data
8991
9092
9193class StationPicker :
9294 def __init__ (self , station_artist , data , data_path = None ):
9395 if data_path is None :
94- data_path = os .path .expanduser (' ~/data_cache' )
96+ data_path = os .path .expanduser (" ~/data_cache" )
9597 self .data_path = data_path
9698 self .event = None
9799 self .data = data
98100 self .station_artist = station_artist
99- self .cid = station_artist .figure .canvas .mpl_connect ('pick_event' ,
100- self ._id_station )
101+ self .cid = station_artist .figure .canvas .mpl_connect (
102+ "pick_event" , self ._id_station
103+ )
101104 self .station_templates = {}
102105 self .station_rows = {}
103106
104107 def _id_station (self , event ):
105- print (' HIT' )
108+ print (" HIT" )
106109 if event .artist is not self .station_artist :
107110 return True
108111
@@ -111,18 +114,18 @@ def _id_station(self, event):
111114 return True
112115 for i in event .ind :
113116 row = self .data .iloc [i ]
114- label = row [' STATION NAME' ]
115- tmplate = ' {USAF:05d}-{WBAN:05d}-{{year}}.gz' .format (** row )
117+ label = row [" STATION NAME" ]
118+ tmplate = " {USAF:05d}-{WBAN:05d}-{{year}}.gz" .format (** row )
116119 self .station_rows [label ] = row
117120 self .station_templates [label ] = tmplate
118- print (' {!r}: {!r}' .format (label , tmplate ))
121+ print (" {!r}: {!r}" .format (label , tmplate ))
119122
120123 def remove (self ):
121124 self .station_artist .figure .canvas .mpl_disconnect (self .cid )
122125 self .cid = None
123126
124127 def get_station_data (self , station_name , years ):
125- ''' Get data from NOAA
128+ """ Get data from NOAA
126129
127130 Parameters
128131 ----------
@@ -136,57 +139,58 @@ def get_station_data(self, station_name, years):
136139 -------
137140 DataFrame
138141 Only extracts the temperature, year, month, day, and hour
139- '''
140- return get_hourly_data (self . data_path ,
141- self .station_templates [station_name ],
142- years )
142+ """
143+ return get_hourly_data (
144+ self . data_path , self .station_templates [station_name ], years
145+ )
143146
144147
145148def plot_station_locations (fig , fih , pick_radius = 10 ):
146149 fig .clf ()
147150
148151 countries = cfeature .NaturalEarthFeature (
149- category = 'cultural' ,
150- name = 'admin_0_countries' ,
151- scale = '50m' ,
152- facecolor = 'none' ,
153- edgecolor = 'gray' )
152+ category = "cultural" ,
153+ name = "admin_0_countries" ,
154+ scale = "50m" ,
155+ facecolor = "none" ,
156+ edgecolor = "gray" ,
157+ )
154158
155159 states_provinces = cfeature .NaturalEarthFeature (
156- category = 'cultural' ,
157- name = 'admin_1_states_provinces_lines' ,
158- scale = '50m' ,
159- facecolor = 'none' ,
160- edgecolor = 'gray' )
160+ category = "cultural" ,
161+ name = "admin_1_states_provinces_lines" ,
162+ scale = "50m" ,
163+ facecolor = "none" ,
164+ edgecolor = "gray" ,
165+ )
161166
162167 land = cfeature .NaturalEarthFeature (
163- category = 'physical' ,
164- name = 'land' ,
165- scale = '50m' ,
166- facecolor = cfeature .COLORS ['land' ])
168+ category = "physical" , name = "land" , scale = "50m" , facecolor = cfeature .COLORS ["land" ]
169+ )
167170
168171 lakes = cfeature .NaturalEarthFeature (
169- category = 'physical' ,
170- name = 'lakes' ,
171- scale = '50m' ,
172- facecolor = cfeature .COLORS ['water' ])
172+ category = "physical" ,
173+ name = "lakes" ,
174+ scale = "50m" ,
175+ facecolor = cfeature .COLORS ["water" ],
176+ )
173177
174178 ax = plt .axes (projection = cartopy .crs .PlateCarree ())
175179 ax .set_xlim (- 80.5 , - 71 )
176180 ax .set_ylim (36 , 45 )
177181
178182 ax .add_feature (land )
179183 ax .add_feature (lakes )
180- ax .add_feature (states_provinces , edgecolor = ' gray' )
181- ax .add_feature (countries , edgecolor = ' gray' )
182- art , = ax .plot (' LON' , ' LAT' , 'o' , data = fih , ms = 5 , picker = 10 )
184+ ax .add_feature (states_provinces , edgecolor = " gray" )
185+ ax .add_feature (countries , edgecolor = " gray" )
186+ ( art ,) = ax .plot (" LON" , " LAT" , "o" , data = fih , ms = 5 , picker = 10 )
183187
184188 sp = StationPicker (art , fih )
185189
186190 return ax , art , sp
187191
188192
189- data_path = os .path .expanduser (' ~/data_cache' )
193+ data_path = os .path .expanduser (" ~/data_cache" )
190194fih = get_filtered_isd (data_path )
191195fig = plt .figure ()
192196ax , art , sp = plot_station_locations (fig , fih )
0 commit comments