Prepare and Fit Spatial Regression Models 20190222




Pay Notebook Creator: Roy Hyunjin Han0
Set Container: Numerical CPU with TINY Memory for 10 Minutes 0
Total0

Animate Air Pollution

In [ ]:
# CrossCompute
target_folder = '/tmp'
url = 'https://data.cityofnewyork.us/download/q68s-8qxv/application%2Fzip'
color_map_select = """
    viridis

    viridis
    plasma
    inferno
    magma
    cividis
"""
In [ ]:
color_map = color_map_select.strip().splitlines()[0]
color_map
In [ ]:
import geotable
import numpy as np
import pandas as pd
In [ ]:
import gzip
from invisibleroads_macros.disk import uncompress
from os.path import exists
from urllib.request import urlretrieve

def download(target_path, source_url):
    if not exists(target_path):
        urlretrieve(source_url, target_path)    
    return target_path

def download_zip(target_folder, source_url):
    archive_path = download(target_folder + '.zip', source_url)
    return uncompress(archive_path, target_folder)
            
def download_gz(target_path, source_url):
    archive_path = download(target_path + '.gz', source_url)
    with gzip.open(archive_path, 'rb') as f:
        open(target_path, 'wb').write(f.read())
    return target_path
In [ ]:
# Load air pollution raster
source_url = (
    'https://data.cityofnewyork.us/download/'
    'q68s-8qxv/application%2Fzip')
source_folder = '/tmp/pollution'
download_zip(source_folder, source_url)
In [ ]:
# Show nitrogen dioxide images
from glob import glob
sorted(glob('/tmp/pollution/*no2*.ovr'))
In [ ]:
# Install package
import subprocess
subprocess.call('pip install -U rasterio'.split())
In [ ]:
# Load image
import rasterio
pollution_raster = rasterio.open('/tmp/pollution/aa2_no2300m/prj.adf')
In [ ]:
# Load image array
x = pollution_raster.read()
In [ ]:
array = x[0]
array.shape
In [ ]:
# Enable inline plots
%matplotlib inline
In [ ]:
import matplotlib.pyplot as plt
plt.imshow(array, vmin=0);
In [ ]:
img = plt.imshow(array, vmin=0)
plt.axis('off')
plt.savefig('/tmp/test.png', bbox_inches='tight')
In [ ]:
from os.path import join
image_paths = []
for index, folder in enumerate(sorted(glob('/tmp/pollution/*_no2300m'))):
    with rasterio.open(join(folder, 'prj.adf')) as pollution_raster:
        x = pollution_raster.read()
        array = x[0]
        img = plt.imshow(array, vmin=0);
        img.set_cmap(color_map)
        plt.axis('off')
        target_path = join(target_folder, 'image%s.png' % index)
        plt.savefig(target_path, bbox_inches='tight')
        image_paths.append(target_path)
In [ ]:
image_paths
In [ ]:
import subprocess
subprocess.call('pip install imageio'.split())
In [ ]:
import imageio
images = []
for path in image_paths:
    images.append(imageio.imread(path))
target_path = join(target_folder, 'movie.gif')
imageio.mimsave(target_path, images)
In [ ]:
print('pollution_image_path = %s' % target_path)