Skip to content
Snippets Groups Projects
Commit 5e1f7cb3 authored by Tor-Einar Skog's avatar Tor-Einar Skog
Browse files

Generate mapfile using Jinja2

parent 364d1dfb
No related branches found
No related tags found
No related merge requests found
*.nc
*.tif
*.tif.aux.xml
\ No newline at end of file
*.tif.aux.xml
mapfile/SEPTREFHUM.map
......@@ -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
......@@ -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
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment