Skip to content

Commit

Permalink
Basic working something
Browse files Browse the repository at this point in the history
  • Loading branch information
phocks committed Oct 30, 2019
1 parent d6fad27 commit 85892e3
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 2 deletions.
16 changes: 16 additions & 0 deletions .gcloudignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# This file specifies files that are *not* uploaded to Google Cloud Platform
# using gcloud. It follows the same syntax as .gitignore, with the addition of
# "#!include" directives (which insert the entries of the given .gitignore-style
# file at that point).
#
# For more information, run:
# $ gcloud topic gcloudignore
#
.gcloudignore
# If you would like to upload your .git directory, .gitignore file or files
# from your .gitignore file, remove the corresponding line
# below:
.git
.gitignore

node_modules
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
1 change: 1 addition & 0 deletions .pylintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
generated-members=netCDF4.*
Binary file added data/CCRC_NARCliM_YEA_1950-2009_HWN_EHF_NF13.nc
Binary file not shown.
62 changes: 60 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,70 @@
import numpy as np # numpy module
import netCDF4 as nc # netcdf module
from flask import jsonify


def heatwave_api(request):
return 'Hello, World!\n'
request_json = request.get_json()
print(request_json)

in_nc = nc.Dataset("./data/CCRC_NARCliM_YEA_1950-2009_HWN_EHF_NF13.nc")
print(in_nc)

temps = in_nc.variables['HWN_EHF']
print(temps) # print the variable attributes

print(temps.dimensions)
print(temps.shape)

mt = in_nc.variables['time'] # read time variable
print(mt) # print the variable attributes

time = mt[:] # Reads the netCDF variable MT, array of one element
print(time)
time_unit = in_nc.variables["time"].getncattr('units')
time_cal = in_nc.variables["time"].getncattr(
'calendar') # read calendar type
local_time = nc.num2date(time, units=time_unit,
calendar=time_cal) # convert time
print("Original time %s is now converted as %s" %
(time[0], local_time[0])) # check conversion

lat, lon = in_nc.variables['lat'], in_nc.variables['lon']

target_lat = -27.4698
target_lon = 153.0251

latvals = lat[:]
lonvals = lon[:] # extract lat/lon values (in degrees) to numpy arrays
import numpy as np

def getclosest_ij(lats, lons, latpt, lonpt):
# find squared distance of every point on grid
dist_sq = (lats-latpt)**2 + (lons-lonpt)**2
minindex_flattened = dist_sq.argmin() # 1D index of minimum dist_sq element
# Get 2D index for latvals and lonvals arrays from 1D index
return np.unravel_index(minindex_flattened, latvals.shape)

iy_min, ix_min = getclosest_ij(latvals, lonvals, target_lat, target_lon)
print(iy_min, ix_min)

temps_vals = temps[:]
location_data = []

for year in range(19):
print(temps[year, iy_min, ix_min])
location_data.append(int(temps_vals[year, iy_min, ix_min]))

print(location_data)

return jsonify({"1960": location_data})


if __name__ == "__main__":
from flask import Flask, request
app = Flask(__name__)

@app.route('/')
@app.route('/', methods=["GET", "POST"])
def index():
return heatwave_api(request)

Expand Down
18 changes: 18 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Function dependencies, for example:
# package>=version

# Pre-installed packages
click==6.7
Flask==1.0.2
itsdangerous==0.24
Jinja2==2.10
MarkupSafe==1.0
pip==18.0
requests==2.21.0
setuptools==40.2.0
Werkzeug==0.14.1
wheel==0.31.1

# Custom packages
numpy==1.17.3
netCDF4===1.5.3

0 comments on commit 85892e3

Please sign in to comment.