def main(): ## get metadata file file (ghcnd-stations.txt) click.secho('>>> Get metadata file (ghcnd-stations.txt)', fg='green') gp.get_ghcnd_stations() ## get inventory file (ghcnd-inventory.txt) click.secho('>>> Get inventory file (ghcnd-inventory.txt)', fg='green') gp.get_ghcnd_inventory() ## find stations click.secho('>>> Find stations... Example: "NEW ORLEANS"', fg='green') gp.find_station("NEW ORLEANS") ## download click.secho( '>>> Download data in csv format after finding the target station ID... Example: "NEW ORLEANS LAKEFRONT AP" ', fg='green') gp.output_to_csv("USW00053917") ## read data click.secho('>>> Read data... Example: "NEW ORLEANS LAKEFRONT AP"', fg='green') data = pd.read_csv('USW00053917.csv') print(data.head()) print(data.tail()) # return return None
def plot_spatial_freeze(year,element,lower_lon=-125,upper_lon=-65,lower_lat=25,upper_lat=50,dpi=200,proj='merc'): print("\nPLOT SPATIAL FREEZE") print("year: ",year) print("element: ",element) days_in_year=(datetime(year, 12, 31)-datetime(year, 1, 1)).days+1 divisor=10. if element != "LAST" and element != "FIRST": print("Only Derived Elements Available: LAST/FIRST") return None if element == "LAST": # Initialize Colormap Values cbar_hex = ['#072F6B', '#08529C', '#2171B5', '#176fc1', '#0b8ed8', '#04a1e6', '#19b5f1', '#33bccf', '#66ccce', '#99dbb8', '#c0e588', '#cce64b' ] cbar_vals = np.array([1,69,79,89,99,109,119,129,139,149,159,169], dtype='f') if element == "FIRST": # Initialize Colormap Values cbar_hex = ['#993399', '#CC0099', '#FF33CC', '#333399', '#3366FF', '#33CCFF', '#006600', '#339933', '#00CC00', '#CC9900', '#FF9900', '#FFCC00' ] cbar_vals = np.array([222,232,242,252,262,272,282,292,302,312,322,332], dtype='f') # Set Up Colormap based element-based values cmap=mcolors.ListedColormap(cbar_hex, N=len(cbar_vals)) norm = mcolors.BoundaryNorm(cbar_vals, cmap.N) minimum = min(cbar_vals); maximum = max(cbar_vals) # set range. cmap.set_under("#ffffff") cmap.set_over("#ffffff") extend="both" ################################################# # Read in GHCND-D Inventory File to get # stations that have element requested print("GETTING STATIONS") ghcnd_stations=gp.get_ghcnd_inventory() num_stns=ghcnd_stations.shape[0] ghcnd_valid_id = np.empty(num_stns,dtype='S11') ghcnd_freeze_day = np.zeros((num_stns),dtype='f') ghcnd_values = np.zeros((num_stns),dtype='f')-(9999.0) ghcnd_lats = np.zeros((num_stns),dtype='f')-(9999.0) ghcnd_lons = np.zeros((num_stns),dtype='f')-(9999.0) ################################################# # Read in GHCN-D data print("READING IN DATA") gp.get_data_year(str(year)) infile = str(year)+".csv.gz" file_handle = gzip.open(infile, 'rb') ghcnd_contents = file_handle.readlines() file_handle.close() print("SORTING") ghcnd_contents=np.sort(ghcnd_contents) print("GOING THROUGH DATA") # Go through GHCN-D Data station_counter=-1 old_id = "XXXXXXXXXXX" num_valid=0 for counter in xrange(len(ghcnd_contents)): ghcnd_line = ghcnd_contents[counter].split(',') if ghcnd_line[2] == "TMIN": if old_id != ghcnd_line[0]: station_counter = station_counter+1 num_valid=num_valid+1 ghcnd_valid_id[station_counter] = ghcnd_line[0] day_in_year=datetime(int(ghcnd_line[1][0:4]),int(ghcnd_line[1][4:6]),int(ghcnd_line[1][6:8])).timetuple().tm_yday if ghcnd_line[3] != "-9999" and ghcnd_line[5].strip()=='': tmin_value = (float(ghcnd_line[3])/divisor) if element == "LAST": if tmin_value <=0 and day_in_year <= 169: ghcnd_freeze_day[station_counter]=day_in_year if element == "FIRST": if tmin_value <=0 and day_in_year >= 222 and ghcnd_freeze_day[station_counter] == 0: ghcnd_freeze_day[station_counter]=day_in_year old_id=ghcnd_line[0] ghcnd_derived_lats = np.zeros((num_valid),dtype='f')-(9999.0) ghcnd_derived_lons = np.zeros((num_valid),dtype='f')-(9999.0) ghcnd_derived_values = np.zeros((num_valid),dtype='f')-(9999.0) for station_counter in xrange(0,num_valid): ghcnd_meta = ghcnd_stations[ghcnd_stations[:,0] == ghcnd_valid_id[station_counter]] ghcnd_derived_lats[station_counter]=float(ghcnd_meta[0][1]) ghcnd_derived_lons[station_counter]=float(ghcnd_meta[0][2]) ghcnd_derived_values[station_counter]=ghcnd_freeze_day[station_counter] # Sort from lowest to highest (so higher values are plotted over lower values) sorted=np.ma.argsort(ghcnd_derived_values) ghcnd_derived_values=ghcnd_derived_values[sorted] ghcnd_derived_lats=ghcnd_derived_lats[sorted] ghcnd_derived_lons=ghcnd_derived_lons[sorted] ################# # PLOT print("PLOTTING (POINT DATA)") plt.figure(num=1, figsize=(10, 6), dpi=dpi, facecolor='w', edgecolor='k') # Create Mercator Basemaps map_points = Basemap(projection=proj,llcrnrlon=lower_lon,llcrnrlat=lower_lat, urcrnrlon=upper_lon,urcrnrlat=upper_lat, resolution='h') # draw coastlines, country boundaries, fill continents. map_points.drawcoastlines(linewidth=0.25) map_points.drawcountries(linewidth=0.25) map_points.drawstates(linewidth=0.25) # Plot Data x, y = map_points(ghcnd_derived_lons,ghcnd_derived_lats) map_points.scatter(x,y,c=ghcnd_derived_values,s=10,cmap=cmap,vmin=minimum,vmax=maximum,linewidths=.1) # Adds the colormap legend cmleg = np.zeros((1,len(cbar_vals)),dtype='f') for i in xrange(0,(len(cbar_vals))): cmleg[0,i] = float(cbar_vals[i]) # Add Colorbar cmap_legend=plt.imshow(cmleg, cmap=cmap, norm=norm) cbar = map_points.colorbar(location='bottom',pad="5%",ticks=cbar_vals,extend=extend) cbar.ax.tick_params(labelsize=10) cbar.set_label('Day of Year') # Set Labels Based on Element if element == "LAST": cbar.ax.set_xticklabels(['Jan-1', 'Mar-10', 'Mar-20', 'Mar-30', 'Apr-10', 'Apr-20', 'Apr-30', 'May-10', 'May-20', 'May-30', 'Jun-10', 'Jun-20'], rotation=30) # add title plt.title('Last Freeze date for '+str(year)) # Save to file plt.savefig('POINT_LAST-FRZ_'+str(year)+'.png', format='png', dpi=dpi) if element == "FIRST": cbar.ax.set_xticklabels(['Aug-10', 'Aug-20', 'Aug-31', 'Sep-10', 'Sep-20', 'Sep-30', 'Oct-10', 'Oct-20', 'Oct-31', 'Nov-10', 'Nov-20', 'Nov-30'], rotation=30) # add title plt.title('First Freeze date for '+str(year)) # Save to file plt.savefig('POINT_FIRST-FRZ_'+str(year)+'.png', format='png', dpi=dpi) plt.clf() return None
def plot_spatial_derived(year,element,lower_lon=-125,upper_lon=-65,lower_lat=25,upper_lat=50,dpi=200,proj='merc'): print("\nPLOT SPATIAL DERIVED") print("year: ",year) print("element: ",element) days_in_year=(datetime(year, 12, 31)-datetime(year, 1, 1)).days+1 if element != "GDD" and element != "HDD" and element != "CDD": print("Only Derived Elements Available: GDD/HDD/CDD") return None # Set info based on Element if element == "GDD" or element == "HDD" or element == "CDD": divisor=10. if element == "HDD": # Initialize Colormap Values (Reds to Blues) cbar_hex = ['#A60F14', '#a4262c', '#db1e26', '#f1471c', '#f57215', '#f89d0e', '#fcc707', '#fede27', '#f3f01d', '#cce64b', '#c0e588', '#99dbb8', '#66ccce', '#33bccf', '#19b5f1', '#04a1e6', '#0b8ed8', '#176fc1', '#2171B5', '#08529C', '#072F6B' ] if element == "CDD" or element == "GDD": # Initialize Colormap Values (Blues to Reds) cbar_hex = ['#072F6B', '#08529C', '#2171B5', '#176fc1', '#0b8ed8', '#04a1e6', '#19b5f1', '#33bccf', '#66ccce', '#99dbb8', '#c0e588', '#cce64b', '#f3f01d', '#fede27', '#fcc707', '#f89d0e', '#f57215', '#f1471c', '#db1e26', '#a4262c', '#A60F14' ] cbar_vals = np.array([0,100,200,300,400,500,600,700,800,900,1000,1100,1200,1300,1400,1500,1600,1700,1800,1900,2000], dtype='f') # Set Up Colormap based element-based values cmap=mcolors.ListedColormap(cbar_hex, N=len(cbar_vals)) norm = mcolors.BoundaryNorm(cbar_vals, cmap.N) minimum = min(cbar_vals); maximum = max(cbar_vals) # set range. cmap.set_under("#ffffff") cmap.set_over("#5F0000") extend="max" ################################################# # Read in GHCND-D Inventory File to get # stations that have element requested print("GETTING STATIONS") ghcnd_stations=gp.get_ghcnd_inventory() num_stns=ghcnd_stations.shape[0] ghcnd_valid_id = np.empty(num_stns,dtype='S11') ghcnd_tmax = np.zeros((num_stns,days_in_year),dtype='f')-(9999.0) ghcnd_tmin = np.zeros((num_stns,days_in_year),dtype='f')-(9999.0) ghcnd_lats = np.zeros((num_stns,days_in_year),dtype='f')-(9999.0) ghcnd_lons = np.zeros((num_stns,days_in_year),dtype='f')-(9999.0) ################################################# # Read in GHCN-D data print("READING IN DATA") gp.get_data_year(str(year)) infile = str(year)+".csv.gz" file_handle = gzip.open(infile, 'rb') ghcnd_contents = file_handle.readlines() file_handle.close() print("SORTING") ghcnd_contents=np.sort(ghcnd_contents) print("GOING THROUGH DATA") # Go through GHCN-D Data station_counter=-1 old_id = "XXXXXXXXXXX" num_valid=0 for counter in xrange(len(ghcnd_contents)): ghcnd_line = ghcnd_contents[counter].split(',') if ghcnd_line[2] == "TMAX" or ghcnd_line[2] == "TMIN": if old_id != ghcnd_line[0]: station_counter = station_counter+1 num_valid=num_valid+1 ghcnd_valid_id[station_counter] = ghcnd_line[0] #print(station_counter,ghcnd_valid_id[station_counter]) day_in_year=datetime(int(ghcnd_line[1][0:4]),int(ghcnd_line[1][4:6]),int(ghcnd_line[1][6:8])).timetuple().tm_yday-1 # Get TMAX/TMIN and its associated lats/lons if ghcnd_line[2] == "TMAX" and ghcnd_line[3] != "-9999" and ghcnd_line[5].strip()=='': ghcnd_tmax[station_counter,day_in_year] = (float(ghcnd_line[3])/divisor) if ghcnd_line[2] == "TMIN" and ghcnd_line[3] != "-9999" and ghcnd_line[5].strip()=='': ghcnd_tmin[station_counter,day_in_year] = (float(ghcnd_line[3])/divisor) old_id=ghcnd_line[0] print("CREATING ACCUMULATIONS") ghcnd_derived_lats = np.zeros((num_valid),dtype='f')-(9999.0) ghcnd_derived_lons = np.zeros((num_valid),dtype='f')-(9999.0) ghcnd_derived_values = np.zeros((num_valid),dtype='f')-(9999.0) for station_counter in xrange(0,num_valid): ghcnd_meta = ghcnd_stations[ghcnd_stations[:,0] == ghcnd_valid_id[station_counter]] ghcnd_derived_lats[station_counter]=float(ghcnd_meta[0][1]) ghcnd_derived_lons[station_counter]=float(ghcnd_meta[0][2]) ghcnd_value = np.zeros((days_in_year),dtype='f')-(9999.0) non_missing=np.where(np.logical_and(ghcnd_tmax[station_counter] != -9999, ghcnd_tmin[station_counter] != -9999)) ghcnd_value[non_missing] = (ghcnd_tmax[station_counter,non_missing] + ghcnd_tmin[station_counter,non_missing]) / 2. if element == "GDD": base=10.000 ghcnd_value[non_missing] = ghcnd_value[non_missing] - base if element == "HDD": base=18.333 ghcnd_value[non_missing] = base - ghcnd_value[non_missing] if element == "CDD": base=18.333 ghcnd_value[non_missing] = ghcnd_value[non_missing] - base non_zero=np.where(ghcnd_value > 0) ghcnd_derived_values[station_counter]=np.sum(ghcnd_value[non_zero]) # Sort from lowest to highest (so higher values are plotted over lower values) sorted=np.ma.argsort(ghcnd_derived_values) ghcnd_derived_values=ghcnd_derived_values[sorted] ghcnd_derived_lats=ghcnd_derived_lats[sorted] ghcnd_derived_lons=ghcnd_derived_lons[sorted] ################# # PLOT print("PLOTTING (POINT DATA)") plt.figure(num=1, figsize=(10, 6), dpi=dpi, facecolor='w', edgecolor='k') # Create Mercator Basemaps map_points = Basemap(projection=proj,llcrnrlon=lower_lon,llcrnrlat=lower_lat, urcrnrlon=upper_lon,urcrnrlat=upper_lat, resolution='h') # draw coastlines, country boundaries, fill continents. map_points.drawcoastlines(linewidth=0.25) map_points.drawcountries(linewidth=0.25) map_points.drawstates(linewidth=0.25) # Plot Data x, y = map_points(ghcnd_derived_lons,ghcnd_derived_lats) map_points.scatter(x,y,c=ghcnd_derived_values,s=10,cmap=cmap,vmin=minimum,vmax=maximum,linewidths=.1) # Adds the colormap legend cmleg = np.zeros((1,len(cbar_vals)),dtype='f') for i in xrange(0,(len(cbar_vals))): cmleg[0,i] = float(cbar_vals[i]) # Add Colorbar cmap_legend=plt.imshow(cmleg, cmap=cmap, norm=norm) cbar = map_points.colorbar(location='bottom',pad="5%",ticks=cbar_vals,extend=extend) cbar.ax.tick_params(labelsize=10) cbar.set_label('Degree Days ('+str(base)+' $^\circ$C)') # add title plt.title(element+' data for '+str(year)) # Save to file plt.savefig('POINT_'+element+'_'+str(year)+'.png', format='png', dpi=dpi) plt.clf() return None
def plot_spatial(year,month,day,element,lower_lon=-125,upper_lon=-65,lower_lat=25,upper_lat=50,dpi=200,proj='merc'): print("\nPLOT SPATIAL") if month < 10: month="0"+str(month) if day < 10: day="0"+str(day) print("year: ",year) print("month: ",month) print("day: ",day) print("element: ",element) plot_date=str(year)+str(month)+str(day) if element != "TMAX" and element != "TMIN" and element != "TAVG" and element != "PRCP" and element != "SNOW" and element != "SNWD": print("Only Elements Available: TMAX/TMIN/TAVG/PRCP/SNOW/SNWD") return None # Set info based on Element if element == "TMAX" or element == "TMIN" or element == "TAVG": divisor=10. unit='$^\circ$F' # Initialize Colormap Values cbar_hex = ['#072F6B', '#08529C', '#2171B5', '#176fc1', '#0b8ed8', '#04a1e6', '#19b5f1', '#33bccf', '#66ccce', '#99dbb8', '#c0e588', '#cce64b', '#f3f01d', '#fede27', '#fcc707', '#f89d0e', '#f57215', '#f1471c', '#db1e26', '#a4262c', '#A60F14' ] cbar_vals = np.array([0,5,10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,85,90,95,100], dtype='f') # Set Up Colormap based element-based values cmap=mcolors.ListedColormap(cbar_hex, N=len(cbar_vals)) norm = mcolors.BoundaryNorm(cbar_vals, cmap.N) minimum = min(cbar_vals); maximum = max(cbar_vals) # set range. cmap.set_under("#071E46") cmap.set_over("#5F0000") extend="both" if element == "PRCP": divisor=10. unit='inches' # Initialize Colormap Values cbar_hex = [ '#33ffff', '#0099ff', '#0033cc', '#33ff00', '#33cc00','#336600', '#ffff33', '#cc9900', '#ff9900', '#ff0000','#cc0000', '#990000', '#ff00ff', '#9900ff', '#9900ff' ] cbar_vals = np.array([0.01,0.10,0.25,0.50,0.75,1.0,1.5,2.0,2.5,3.0,4.0,5.0,6.0,8.0,10.0], dtype='f') # Set Up Colormap based element-based values cmap=mcolors.ListedColormap(cbar_hex, N=len(cbar_vals)) norm = mcolors.BoundaryNorm(cbar_vals, cmap.N) minimum = min(cbar_vals); maximum = max(cbar_vals) # set range. cmap.set_over("#ffffff") extend="max" if element == "SNOW" or element == "SNWD": divisor=1. unit='inches' # Initialize Colormap Values cbar_hex = [ '#EDFAC2', '#CDFFCD', '#99ff99', '#53bd9f', '#32a696', '#3296b4', '#0570b0', '#05508c', '#0a1f96', '#6a2c5a', '#6a2c5a' ] cbar_vals = np.array([0.01,1.,2.,3.,4.,5.,10.,15.,20.,30.,40.], dtype='f') # Set Up Colormap based element-based values cmap=mcolors.ListedColormap(cbar_hex, N=len(cbar_vals)) norm = mcolors.BoundaryNorm(cbar_vals, cmap.N) minimum = min(cbar_vals); maximum = max(cbar_vals) # set range. cmap.set_over("#2c0246") extend="max" ################################################# # Read in GHCND-D Inventory File to get # stations that have element requested print("GETTING STATIONS THAT MATCH ELEMENT: ",element) ghcnd_inventory=gp.get_ghcnd_inventory() ghcnd_stations = ghcnd_inventory[ghcnd_inventory[:,3] == element] num_stns=ghcnd_stations.shape[0] ghcnd_values = np.zeros((num_stns),dtype='f')-(9999.0) ghcnd_lats = np.zeros((num_stns),dtype='f')-(9999.0) ghcnd_lons = np.zeros((num_stns),dtype='f')-(9999.0) ################################################# # Read in GHCN-D data print("READING IN DATA") gp.get_data_year(str(year)) infile = str(year)+".csv.gz" file_handle = gzip.open(infile, 'rb') ghcnd_contents = file_handle.readlines() file_handle.close() valid_stns=0 for counter in xrange(len(ghcnd_contents)): ghcnd_line = ghcnd_contents[counter].split(',') if ghcnd_line[1] == plot_date and ghcnd_line[2] == element: # Get Data Value ghcnd_values[valid_stns] = (float(ghcnd_line[3])/divisor) # Get Lat/Lon ghcnd_meta = ghcnd_stations[ghcnd_stations[:,0] == ghcnd_line[0]] ghcnd_lats[valid_stns]=float(ghcnd_meta[0][1]) ghcnd_lons[valid_stns]=float(ghcnd_meta[0][2]) valid_stns=valid_stns+1 # Sort from lowest to highest (so higher values are plotted over lower values) sorted=np.ma.argsort(ghcnd_values) ghcnd_values=ghcnd_values[sorted] ghcnd_lats=ghcnd_lats[sorted] ghcnd_lons=ghcnd_lons[sorted] # Convert to Imperial Units and mask data where needed if element == "TMAX" or element == "TMIN" or element == "TAVG": ghcnd_values = (ghcnd_values * 1.8) + 32. # C to F else: ghcnd_values = (ghcnd_values * 0.03937) # mm to inch ghcnd_values = ma.masked_where(ghcnd_values < 0.01, ghcnd_values) # Don't Plot Missing Data valid=np.where(np.logical_and(ghcnd_lats!=-9999,ghcnd_lons!=-9999)) ################# # PLOT print("PLOTTING (POINT DATA)") plt.figure(num=1, figsize=(10, 6), dpi=dpi, facecolor='w', edgecolor='k') # Create Mercator Basemaps map_points = Basemap(projection=proj,llcrnrlon=lower_lon,llcrnrlat=lower_lat, urcrnrlon=upper_lon,urcrnrlat=upper_lat, resolution='h') # draw coastlines, country boundaries, fill continents. map_points.drawcoastlines(linewidth=0.25) map_points.drawcountries(linewidth=0.25) map_points.drawstates(linewidth=0.25) # Plot Data x, y = map_points(ghcnd_lons[valid],ghcnd_lats[valid]) map_points.scatter(x,y,c=ghcnd_values[valid],s=10,cmap=cmap,vmin=minimum,vmax=maximum,linewidths=0.1) # Adds the colormap legend cmleg = np.zeros((1,len(cbar_vals)),dtype='f') for i in xrange(0,(len(cbar_vals))): cmleg[0,i] = float(cbar_vals[i]) # Add Colorbar cmap_legend=plt.imshow(cmleg, cmap=cmap, norm=norm) cbar = map_points.colorbar(location='bottom',pad="5%",ticks=cbar_vals,extend=extend) cbar.ax.tick_params(labelsize=10) cbar.set_label(unit) # add title plt.title(element+' data for '+str(year)+' '+str(month)+' '+str(day)) # Save to file plt.savefig('POINT_'+str(element)+'_'+str(plot_date)+'.png', format='png', dpi=dpi) plt.clf() return None