Skip to content
Snippets Groups Projects
Commit e62cefc0 authored by leufen1's avatar leufen1
Browse files

night lights: added plot routine to check if data is loaded properly. also added flip check.

parent 2afd8737
No related branches found
No related tags found
2 merge requests!5New App; stable_night_lights,!4Lukas issue004 feat new app stable night lights
......@@ -42,3 +42,7 @@ Thumbs.db
# data folder #
###############
/data/
# check plot folder #
#####################
/plots/
\ No newline at end of file
......@@ -11,4 +11,4 @@ requests>=2.19.1
pip>=10.0.1
setuptools>=39.1.0
wheel>=0.32.2
matplotlib>=3.1.0
......@@ -10,6 +10,9 @@ import numpy as np
import os
from toar_location_services.settings import DATA_DIR, DEBUG, USE_DUMMY_STABLE_NIGHT_LIGHTS_DATA
from django.contrib.gis.gdal import GDALRaster
import matplotlib.pyplot as plt
# import gdal
# from gdalconst import GA_ReadOnly
def read_proxydata(filename, dummy=DEBUG and USE_DUMMY_STABLE_NIGHT_LIGHTS_DATA):
......@@ -27,21 +30,31 @@ def read_proxydata(filename, dummy=DEBUG and USE_DUMMY_STABLE_NIGHT_LIGHTS_DATA)
print("Opening ", NIGHTLIGHTS_FILE, "...")
rst = GDALRaster(NIGHTLIGHTS_FILE, write=False)
lon0, lat0 = rst.origin # top left corner
xmin, ymin, xmax, ymax = rst.extent # xmin/lon0, ymin, xmax, ymax/lat0
xmin, ymin, xmax, ymax = rst.extent # xmin=lon0, ymin, xmax, ymax=lat0
cols = rst.width
rows = rst.height
dlon, dlat = rst.scale # xscale/dlon, yscale/dlat
# rst.width*xscale + xmin == xmax
dlon, dlat = rst.scale # xscale=dlon, yscale=dlat
# construct data array and lonvec, latvec
data = rst.bands[0].data()
lonvec = np.linspace(xmin, xmax, cols)
latvec = np.linspace(ymin, ymax, rows)
# check if data is flipped
lat1 = lat0 + dlat * rows
if lat1 < lat0:
lat0, lat1 = lat1, lat0
latvec = np.flipud(latvec)
# set metadata
boundingbox = [lon0, latvec.min(), lonvec.max(), lat0]
# plot data
if DEBUG:
plt.contourf(lonvec, latvec, data)
plt.savefig('../plots/global_nighttime_lights.png')
plt.close()
#
datainfo = {'size': (rows, cols), 'resolution': (np.abs(dlon), np.abs(dlat)),
'boundingbox': boundingbox}
......
from django.test import TestCase
# Create your tests here.
from nightlights_file_extraction import read_proxydata
FILENAME = "stable_lights_europe.tif"
read_proxydata(FILENAME, dummy=False)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment