Skip to content

Commit

Permalink
push version 1.7
Browse files Browse the repository at this point in the history
  • Loading branch information
Mario Hafer authored and Weathermann committed Nov 19, 2023
1 parent 6157a39 commit a775436
Show file tree
Hide file tree
Showing 25 changed files with 2,771 additions and 1,980 deletions.
130 changes: 82 additions & 48 deletions classes/ASCIIGridWriter.py
Original file line number Diff line number Diff line change
@@ -1,50 +1,57 @@
'''
ASCIIGridWriter
Creates a ESRI ASCII GRID (to convert this to GeoTIFF afterwards)
from original binary RADOLAN file
Created on 06.12.2020
@author: Weatherman
'''

import sys
from pathlib import Path
import numpy as np

NODATA = -1.0
from NumpyRadolanReader import NumpyRadolanReader

default_nodata_value = -1.0


class ASCIIGridWriter:
'''
classdocs
'''
"""
ASCIIGridWriter
Creates a ESRI ASCII GRID (to convert this to GeoTIFF afterwards)
from original binary RADOLAN file
Created on 06.12.2020
@author: Weatherman
"""


def __init__(self, np_2Ddata, precision, asc_filename_path):
'''
Constructor
'''

def __init__(self, np_2Ddata, precision, asc_filename_path, nodata_value=default_nodata_value):
"""
:param np_2Ddata:
:param precision:
:param asc_filename_path:
:param nodata_value: possibility to specify another missing value,
e.g. for dBZ products (e.g. -50.0; -32.5 = neg. maximum)
"""
print(self)

self._np_2Ddata = np_2Ddata
self._asc_filename_path = asc_filename_path
self._prec = precision # precision: 1.0, 0.1, 0.01


# catch 'None':
if nodata_value is None:
nodata_value = default_nodata_value

self._nodata_value = nodata_value

if nodata_value != default_nodata_value:
self.out(f"nodata_value: {nodata_value}")


def __str__(self):
return self.__class__.__name__



def out(self, s, ok=True):
if ok:
print("{}: {}".format(self, s))
print(f"{self}: {s}")
else:
print("{}: {}".format(self, s), file=sys.stderr)



print(f"{self}: {s}", file=sys.stderr)


def write(self):
"""
Expand All @@ -55,32 +62,29 @@ def write(self):
nrows, ncols = self._np_2Ddata.shape

d_projected_meters = {
# key: row, value: tuple of projected meters
# key: row, value: tuple of projected meters: x0, y0
1500: (-673462, -5008645), # central europe composite, 1500x1400
1200: (-542962, -3609145), # WN, 1200x1100
1200: (-543197, -4822589), # WN, HG, 1200x1100 *)
1100: (-443462, -4758645), # extended national composite / RADKLIM: 1100x900
900: (-523462, -4658645) # national composite: 900x900
}

# *) taken y0 from HG format description and minus 1200 kilometers.
# They specify the _upper_ left corner there.

try:
llcorner = d_projected_meters[nrows]
except KeyError:
raise NotImplementedError


gis_header_template = '''\
ncols {}
nrows {}
xllcorner {}
yllcorner {}
cellsize 1000
nodata_value {}'''

# The * before 'tup' tells Python to unpack the tuple into separate arguments,
# as if you called s.format(tup[0], tup[1], tup[2]) instead.
#header = gis_header_template.format(*dim, *llcorner, NODATA)
# -> geht nur Python3
gis_header = gis_header_template.format(ncols, nrows, llcorner[0], llcorner[1], NODATA)
l_gis_header_template = []
l_gis_header_template.append(f"ncols {ncols}")
l_gis_header_template.append(f"nrows {nrows}")
l_gis_header_template.append(f"xllcorner {llcorner[0]}")
l_gis_header_template.append(f"yllcorner {llcorner[1]}")
l_gis_header_template.append("cellsize 1000")
l_gis_header_template.append(f"nodata_value {self._nodata_value}")

gis_header = "\n".join(l_gis_header_template)


# precision: 1.0, 0.1, 0.01
Expand All @@ -97,13 +101,13 @@ def write(self):
#mask = np.isnan(np_data)
#np_data[mask] = NODATA
# -> ok
np_data[np.isnan(np_data)] = NODATA # set np.nan to ASCII value
np_data[np.isnan(np_data)] = self._nodata_value # set np.nan to ASCII value

# np.flipud(): Flip array in the up/down direction.
np.savetxt(self._asc_filename_path, np.flipud(np_data), fmt=fmt,
delimiter=' ', newline='\n', header=gis_header, comments='') # footer=''

self.out("write(): -> {}".format(self._asc_filename_path))
self.out(f"write(): -> {self._asc_filename_path}")

"""
if np.amax(data_clutter) > 0.0:
Expand All @@ -119,5 +123,35 @@ def write(self):
self._write_ascii_grid(data_nodata, gis_header, save_file)
"""




def test_hg():
print("*** Test HG ***\n")

hg_path = Path("/run/media/loki/ungesichert/Testdaten/HG")

for hg in hg_path.glob("HG*_???"):
_to_asc(hg)

def test_wn():
print("*** Test WN ***\n")
wn_path = Path("/run/media/loki/ungesichert/Testdaten/WN")
wn1 = wn_path / "20221224-2200/WN2212242200_000"
wn2 = wn_path / "20221226-1300/WN2212261300_000"

for wn in (wn1, wn2):
_to_asc(wn, -50.0)

def _to_asc(bin, nodata_value=None):
nrr = NumpyRadolanReader(bin) # FileNotFoundError
nrr.read()

asc_file = Path(bin.parent, bin.name + ".asc")

ascii_writer = ASCIIGridWriter(nrr.data, nrr.precision, asc_file, nodata_value)
ascii_writer.write()

if __name__ == "__main__":
""" Test ASCII convert """

#test_hg()
test_wn()
Loading

0 comments on commit a775436

Please sign in to comment.