diff --git a/.gitignore b/.gitignore index 60e0b3efb905e9e28235bb57fb8069e1111fb052..2a12b1dfb20cecf3cd2db7b3a5b0ca433a302226 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *.nc *.tif -*.tif.aux.xml \ No newline at end of file +*.tif.aux.xml +mapfile/SEPTREFHUM.map diff --git a/README.md b/README.md index 1eb6514a52d667f04d8e7f3a448a428c19638ce7..36dc584320898a9b246c65ddb25dfcf6b72b3450 100644 --- a/README.md +++ b/README.md @@ -54,4 +54,3 @@ gdal_merge.py -separate in/test_3.tif in/test_4.tif in/test_5.tif in/test_6.tif **Could we live with x GeoTIFF files per timestamp, where x is the number of variables?** -### \ No newline at end of file diff --git a/SEPTREFHUM.py b/SEPTREFHUM.py index ae5f60f85a62f7bf1250a6c7ff3b8be854f994ac..57658d69dcf6ad481fb04f1a8a96b43e97ceb798 100755 --- a/SEPTREFHUM.py +++ b/SEPTREFHUM.py @@ -7,6 +7,7 @@ import subprocess,glob from datetime import datetime, timezone +from jinja2 import Environment, FileSystemLoader import pytz import netCDF4 as nc @@ -100,9 +101,11 @@ subprocess.run('cdo -aexpr,"WARNING_STATUS = WHS < 20 ? 2 : -1; WARNING_STATUS = wh_3daysum = nc.Dataset('%swh_3daysum.nc' % tmpfile_path, 'r') timesteps = wh_3daysum.variables["time"][:] timestep_index = 1 +timestep_dates = [] # Used in the mapfile template for timestep in timesteps: timestep_date = datetime.fromtimestamp(timestep) file_date = timestep_date.astimezone(local_timezone).strftime("%Y-%m-%d") + timestep_dates.append(file_date) # Create NetCDF result file subprocess.run('cdo -seltimestep,%s/%s %sresult.nc %sresult_%s.nc' % (timestep_index, timestep_index, tmpfile_path, tmpfile_path, file_date), shell=True) # Split all variables into separate GeoTIFF files, using GDAL @@ -116,8 +119,17 @@ for timestep in timesteps: timestep_index = timestep_index + 1 -# Remove all temporary/intermediary files -subprocess.run("rm %s*" % tmpfile_path, shell=True) +# Generate mapfile +env = Environment(loader=FileSystemLoader('.')) +template = env.get_template("mapfile/template.j2") +output = template.render({ + "timestep_dates": timestep_dates +}) +with open("mapfile/SEPTREFHUM.map", 'w') as f: + f.write(output) + +# Remove all temporary/intermediary files +subprocess.run("rm %s*" % tmpfile_path, shell=True) \ No newline at end of file diff --git a/mapfile/template.j2 b/mapfile/template.j2 new file mode 100644 index 0000000000000000000000000000000000000000..57fbda9f58ee885422b28d9e02983e67437c7670 --- /dev/null +++ b/mapfile/template.j2 @@ -0,0 +1,130 @@ +MAP + +NAME "VIPS Spatial models SEPTREFHUM" + +# UTM33 +EXTENT -75000 6450000 1120000 8000000 +UNITS METERS +MAXSIZE 5120 + +CONFIG "MS_ERRORFILE" "/disks/data01/mapserver/log/SEPTREFHUM.log" + +# Projection is UTM33 +PROJECTION + "init=epsg:25833" +END + + +WEB + TEMPLATE "/disks/data01/mapserver/wms/SEPTREFHUM/query_template_SEPTREFHUM.html" + IMAGEPATH "/disks/data01/mapserver/tmp/" + IMAGEURL "/tmp/" + METADATA + "wms_keywordlist" "VIPS model Septoria Reference Humidity Model (SEPTREFHUM)" + "wms_abstract" "VIPS model SEPTREFHUM" + "wms_enable_request" "*" + "wms_title" "VIPS model SEPTREFHUM" + "wms_onlineresource" "https://testvips.nibio.no/cgi-bin/SEPTREFHUM" + "wms_getfeatureinfo_formatlist" "text/plain,text/html,text/xml" + "wms_accessconstraints" "none" + "wms_addresstype" "" + "wms_address" "Høgskoleveien 7" + "wms_city" "Ås" + "wms_stateorprovince" "Ås" + "wms_postcode" "1430" + "wms_country" "Norway" + "wms_contactelectronicmailaddress" "vips@nibio.no" + "wms_feature_info_mime_type" "text/html" + "wms_contactperson" "Berit Nordskog" + "wms_contactposition" "owner" + "wms_contactorganization" "Norsk institutt for bioøkonomi (NIBIO)" + END +END #web + +############################# Start of legend ######################################################### +# The legend configuration uses the CLASSITEM information in each layer to generate colour icons and labels +LEGEND + STATUS ON + KEYSIZE 16 12 + LABEL + TYPE TRUETYPE + SIZE 10 + COLOR 0 0 0 + OFFSET 0 -3 + END +END + +{% for timestep_date in timestep_dates %} +LAYER + NAME "SEPTREFHUM.WARNING_STATUS {{ timestep_date }}" + DATA "/disks/data01/mapserver/data/SEPTREFHUM/result_{{ timestep_date }}.tif" + TYPE RASTER + PROCESSING "BANDS=1" + + + STATUS ON + METADATA + "wms_title" "Reference humidity model {{ timestep_date }}" + END + CLASSITEM "[pixel]" + + # class using simple string comparison, equivalent to ([pixel] = 0) + + CLASS + NAME "Varsel ferdig" + EXPRESSION ([pixel] < 1) + STYLE + COLOR 160 160 160 + END + END + CLASS + NAME "Data mangler" + EXPRESSION ([pixel] >= 1 AND [pixel] < 2) + STYLE + COLOR 0 102 255 + END + END + CLASS + NAME "Ingen risiko" + EXPRESSION ([pixel] >= 2 AND [pixel] < 3) + STYLE + COLOR 51 204 51 + END + END + CLASS + NAME "Mulig risiko" + EXPRESSION ([pixel] >= 3 AND [pixel] < 4) + STYLE + COLOR 255 204 0 + END + END + CLASS + NAME "Høy risiko" + EXPRESSION ([pixel] >= 4) + STYLE + COLOR 255 0 0 + END + END +END # Layer + +LAYER + NAME "SEPTREFHUM.WH {{ timestep_date }}" + DATA "/disks/data01/mapserver/data/SEPTREFHUM/result_{{ timestep_date }}.tif" + TYPE RASTER + PROCESSING "BANDS=2" + PROCESSING "SCALE=AUTO" + + + STATUS ON + METADATA + "wms_title" "Reference humidity model WHS {{ timestep_date }}" + END + CLASSITEM "[pixel]" + +END # Layer + +{% endfor %} + +END #map + + \ No newline at end of file