-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from airavata-courses/assignment_1_fixes
Assignment 1 fixes
- Loading branch information
Showing
25 changed files
with
128 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,92 @@ | ||
|
||
import matplotlib.pyplot as plt | ||
import matplotlib | ||
import pyart | ||
import numpy as np | ||
matplotlib.use('Agg') | ||
from PIL import Image | ||
import base64 | ||
import matplotlib.image as mpimg | ||
import json | ||
import pandas as pd | ||
import numpy as np | ||
import pyimgur | ||
from kafka import KafkaProducer | ||
producer = KafkaProducer(bootstrap_servers='localhost:9092') | ||
|
||
from pykafka import KafkaClient | ||
from metpy.cbook import get_test_data | ||
from metpy.io import Level2File | ||
from metpy.plots import add_metpy_logo, add_timestamp | ||
CLIENT_ID = "78adc2281c1c838" | ||
|
||
im = pyimgur.Imgur(CLIENT_ID) | ||
|
||
|
||
|
||
def produce(data,conn): | ||
def produce(data,conn,client): | ||
Year=data['inputData']['Year'] | ||
Month=data['inputData']['Month'] | ||
Day=data['inputData']['Day'] | ||
Radar=data['inputData']['Radar'] | ||
scans = conn.get_avail_scans(Year,Month,Day,Radar) # year, month and day | ||
results = conn.download(scans[0], 'templocation') | ||
uid=data['uid'] | ||
inputData=data['inputData'] | ||
userID=data["userID"] | ||
|
||
fig = plt.figure(figsize=(16,12)) | ||
|
||
numberOfPlots=1 | ||
scans = conn.get_avail_scans(Year,Month,Day,Radar) # year, month and day | ||
results = conn.download(scans[numberOfPlots-1], 'templocation') | ||
|
||
# fig = plt.figure(figsize=(16,12)) | ||
for i,scan in enumerate(results.iter_success(),start=1): | ||
ax = fig.add_subplot(2,2,i) | ||
radar = scan.open_pyart() | ||
# ax = fig.add_subplot(1,1,i) | ||
# radar = scan.open_pyart() | ||
|
||
# display = pyart.graph.RadarDisplay(radar) | ||
# display.plot('reflectivity',0,ax=ax,title="{} {}".format(scan.radar_id,scan.scan_time)) | ||
# display.set_limits((-150, 150), (-150, 150), ax=ax) | ||
|
||
sweep = 0 | ||
name = scan.open() | ||
f = Level2File(name) | ||
# First item in ray is header, which has azimuth angle | ||
az = np.array([ray[0].az_angle for ray in f.sweeps[sweep]]) | ||
|
||
# 5th item is a dict mapping a var name (byte string) to a tuple | ||
# of (header, data array) | ||
ref_hdr = f.sweeps[sweep][0][4][b'REF'][0] | ||
ref_range = np.arange(ref_hdr.num_gates) * ref_hdr.gate_width + ref_hdr.first_gate | ||
ref = np.array([ray[4][b'REF'][1] for ray in f.sweeps[sweep]]) | ||
|
||
rho_hdr = f.sweeps[sweep][0][4][b'RHO'][0] | ||
rho_range = (np.arange(rho_hdr.num_gates + 1) - 0.5) * rho_hdr.gate_width + rho_hdr.first_gate | ||
rho = np.array([ray[4][b'RHO'][1] for ray in f.sweeps[sweep]]) | ||
|
||
fig, axes = plt.subplots(1, 2, figsize=(15, 8)) | ||
add_metpy_logo(fig, 190, 85, size='large') | ||
for var_data, var_range, ax in zip((ref, rho), (ref_range, rho_range), axes): | ||
# Turn into an array, then mask | ||
data = np.ma.array(var_data) | ||
data[np.isnan(data)] = np.ma.masked | ||
|
||
# Convert az,range to x,y | ||
xlocs = var_range * np.sin(np.deg2rad(az[:, np.newaxis])) | ||
ylocs = var_range * np.cos(np.deg2rad(az[:, np.newaxis])) | ||
|
||
# Plot the data | ||
ax.pcolormesh(xlocs, ylocs, data, cmap='viridis') | ||
ax.set_aspect('equal', 'datalim') | ||
ax.set_xlim(-40, 20) | ||
ax.set_ylim(-30, 30) | ||
add_timestamp(ax, f.dt, y=0.02, high_contrast=True) | ||
|
||
|
||
|
||
pltName='images/'+str(uid+str(i)+'.png') | ||
plt.savefig(pltName) | ||
plt.close(fig) | ||
uploadImage = im.upload_image(pltName, title="Uploaded with PyImgur") | ||
link=str(uploadImage.link) | ||
|
||
body={ "inputData":inputData, | ||
"outputData":link, | ||
"uid":uid, | ||
"userID":userID | ||
} | ||
|
||
with (client.topics['dataAnalysisConsumerF']).get_sync_producer() as producer: | ||
producer.produce(bytes(json.dumps(body),'utf-8')) | ||
|
||
display = pyart.graph.RadarDisplay(radar) | ||
display.plot('reflectivity',0,ax=ax,title="{} {}".format(scan.radar_id,scan.scan_time)) | ||
display.set_limits((-150, 150), (-150, 150), ax=ax) | ||
plotName=data["uid"]+'.png' | ||
plt.savefig(plotName) | ||
|
||
plt.close(fig) | ||
|
||
uploadImage = im.upload_image(plotName, title="Uploaded with PyImgur") | ||
link=str(uploadImage.link) | ||
# fig.canvas.draw ( ) | ||
# w,h = fig.canvas.get_width_height() | ||
# buf = np.frombuffer ( fig.canvas.tostring_argb(), dtype=np.uint8 ) | ||
# buf.shape = ( w, h,4 ) | ||
# buf = np.roll ( buf, 3, axis = 2 ) | ||
# w, h, d = buf.shape | ||
# res=Image.frombytes( "RGBA", ( w ,h ), buf.tostring( ) ) | ||
# res=res.resize((960,720),Image.ANTIALIAS) | ||
body={ "inputData":data["inputData"], | ||
"outputData":link, | ||
"uid":data["uid"], | ||
"userID":data["userID"] | ||
} | ||
# body='{"resultPlot":"0","uid":"8","userID":"2"}' | ||
|
||
print(bytes(json.dumps(body),'utf-8')) | ||
producer.send('dataAnalysisConsumerF', bytes(json.dumps(body),'utf-8')) | ||
|
||
|
||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.