diff --git a/README.md b/README.md index 1be26e55..7853027d 100644 --- a/README.md +++ b/README.md @@ -58,19 +58,6 @@ CARA has not undergone review, approval or certification by competent authoritie The software is provided "as is", without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose and non-infringement. In no event shall the authors or copyright holders be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with the software or the use or other dealings in the software. -## Adapting CARA to your location - -The default weather data (average hourly outdoor temperature in Celcius for each month of the year) used in CARA is for Geneva, Switzerland. -In order for the natural ventilation option to work correctly for other geographic locations, the outdoor temperatures must be updated. -There are some scripts to help download and process the temperature data from your nearest weather station in the https://gitlab.cern.ch/cara/climatology-data repository. -Once you have used the scripts, the hourly temperature data for your location should be added to the file `data.py` in place of the default values for Geneva. The temperature values for your locations should be pasted into the `Geneva_hourly_temperatures_celsius_per_hour` variable, **without changing the variable name** in the following format: - - `'Jan': [0.2, -0.3, -0.5, -0.9, -1.1, -1.4, -1.5, -1.5, -1.1, 0.1, 1.5, - 2.8, 3.8, 4.4, 4.5, 4.4, 4.4, 3.9, 3.1, 2.7, 2.2, 1.7, 1.5, 1.1], - 'Feb': [0.9, 0.3, 0.0, -0.5, -0.7, -1.1, -1.2, -1.1, -0.7, 0.8, 2.5, - 4.2, 5.4, 6.2, 6.3, 6.2, 6.1, 5.5, 4.5, 4.1, 3.5, 2.8, 2.5, 2.0],...` - -CARA currently supports **only one geographic location for weather data per instance**. ## Running CARA locally @@ -85,10 +72,20 @@ This will start a local version of CARA, which can be visited at http://localhos ## Development guide +The CARA repository makes use of Git's Large File Storage (LFS) feature. +You will need a working installation of git-lfs in order to run CARA in development mode. +See https://git-lfs.github.com/ for installation instructions. + +### Installing CARA in editable mode + +``` +git lfs pull # Fetch the data from LFS +pip install -e . # At the root of the repository +``` + ### Running the COVID calculator app in development mode ``` -pip install -e . # At the root of the repository python -m cara.apps.calculator ``` @@ -107,7 +104,6 @@ python -m cara.apps.calculator --prefix=/mycalc ### Running the CARA Expert-App app in development mode ``` -pip install -e . # At the root of the repository voila cara/apps/expert/cara.ipynb --port=8080 ``` diff --git a/cara/.gitattributes b/cara/.gitattributes new file mode 100644 index 00000000..c1609315 --- /dev/null +++ b/cara/.gitattributes @@ -0,0 +1,2 @@ +global_weather_set.json filter=lfs diff=lfs merge=lfs -text +hadisd_station_fullinfo_v311_202001p.txt filter=lfs diff=lfs merge=lfs -text diff --git a/cara/apps/calculator/__init__.py b/cara/apps/calculator/__init__.py index dbfa15ef..1688d494 100644 --- a/cara/apps/calculator/__init__.py +++ b/cara/apps/calculator/__init__.py @@ -33,7 +33,7 @@ from .user import AuthenticatedUser, AnonymousUser # calculator version. If the calculator needs to make breaking changes (e.g. change # form attributes) then it can also increase its MAJOR version without needing to # increase the overall CARA version (found at ``cara.__version__``). -__version__ = "2.1.0" +__version__ = "3.0.0" class BaseRequestHandler(RequestHandler): diff --git a/cara/apps/calculator/model_generator.py b/cara/apps/calculator/model_generator.py index a4b139e7..a6a41fab 100644 --- a/cara/apps/calculator/model_generator.py +++ b/cara/apps/calculator/model_generator.py @@ -1,5 +1,5 @@ import dataclasses -from dataclasses import dataclass +import datetime import html import logging import typing @@ -8,6 +8,7 @@ import numpy as np from cara import models from cara import data +import cara.data.weather import cara.monte_carlo as mc from .. import calculator from cara.monte_carlo.data import activity_distributions, virus_distributions @@ -25,7 +26,7 @@ _NO_DEFAULT = object() _DEFAULT_MC_SAMPLE_SIZE = 50000 -@dataclass +@dataclasses.dataclass class FormData: activity_type: str air_changes: float @@ -50,6 +51,9 @@ class FormData: infected_lunch_start: minutes_since_midnight #Used if infected_dont_have_breaks_with_exposed infected_people: int infected_start: minutes_since_midnight + location_name: str + location_latitude: float + location_longitude: float mask_type: str mask_wearing_option: str mechanical_ventilation_type: str @@ -100,6 +104,9 @@ class FormData: 'infected_lunch_start': '12:30', 'infected_people': _NO_DEFAULT, 'infected_start': '08:30', + 'location_latitude': _NO_DEFAULT, + 'location_longitude': _NO_DEFAULT, + 'location_name': _NO_DEFAULT, 'mask_type': 'Type I', 'mask_wearing_option': 'mask_off', 'mechanical_ventilation_type': 'not-applicable', @@ -197,7 +204,8 @@ class FormData: ('virus_type', VIRUS_TYPES), ('volume_type', VOLUME_TYPES), ('window_opening_regime', WINDOWS_OPENING_REGIMES), - ('window_type', WINDOWS_TYPES)] + ('window_type', WINDOWS_TYPES), + ('event_month', MONTH_NAMES)] for attr_name, valid_set in validation_tuples: if getattr(self, attr_name) not in valid_set: raise ValueError(f"{getattr(self, attr_name)} is not a valid value for {attr_name}") @@ -244,6 +252,52 @@ class FormData: def build_model(self, sample_size=_DEFAULT_MC_SAMPLE_SIZE) -> models.ExposureModel: return self.build_mc_model().build_model(size=sample_size) + def tz_name_and_utc_offset(self) -> typing.Tuple[str, float]: + """ + Return the timezone name (e.g. CET), and offset, in hours, that need to + be *added* to UTC to convert to the form location's timezone. + + """ + month = MONTH_NAMES.index(self.event_month) + 1 + timezone = cara.data.weather.timezone_at( + latitude=self.location_latitude, longitude=self.location_longitude, + ) + # We choose the first of the month for the current year. + date = datetime.datetime(datetime.datetime.now().year, month, 1) + name = timezone.tzname(date) + assert isinstance(name, str) + utc_offset_td = timezone.utcoffset(date) + assert isinstance(utc_offset_td, datetime.timedelta) + utc_offset_hours = utc_offset_td.total_seconds() / 60 / 60 + return name, utc_offset_hours + + def outside_temp(self) -> models.PiecewiseConstant: + """ + Return the outside temperature as a PiecewiseConstant in the destination + timezone. + + """ + month = MONTH_NAMES.index(self.event_month) + 1 + + wx_station = self.nearest_weather_station() + temp_profile = cara.data.weather.mean_hourly_temperatures(wx_station[0], month) + + _, utc_offset = self.tz_name_and_utc_offset() + + # Offset the source times according to the difference from UTC (as a + # result the first data value may no longer be a midnight, and the hours + # no longer ordered modulo 24). + source_times = np.arange(24) + utc_offset + times, temp_profile = cara.data.weather.refine_hourly_data( + source_times, + temp_profile, + npts=24*10, # 10 steps per hour => 6 min steps + ) + outside_temp = models.PiecewiseConstant( + tuple(float(t) for t in times), tuple(float(t) for t in temp_profile), + ) + return outside_temp + def ventilation(self) -> models._VentilationBase: always_on = models.PeriodicInterval(period=120, duration=120) # Initializes a ventilation instance as a window if 'natural_ventilation' is selected, or as a HEPA-filter otherwise @@ -253,10 +307,8 @@ class FormData: else: window_interval = always_on - month = self.event_month[:3] - + outside_temp = self.outside_temp() inside_temp = models.PiecewiseConstant((0, 24), (293,)) - outside_temp = data.GenevaTemperatures[month] ventilation: models.Ventilation if self.window_type == 'window_sliding': @@ -298,6 +350,12 @@ class FormData: else: return models.MultipleVentilation((ventilation, infiltration_ventilation)) + def nearest_weather_station(self) -> cara.data.weather.WxStationRecordType: + """Return the nearest weather station (which has valid data) for this form""" + return cara.data.weather.nearest_wx_station( + longitude=self.location_longitude, latitude=self.location_latitude + ) + def mask(self) -> models.Mask: # Initializes the mask type if mask wearing is "continuous", otherwise instantiates the mask attribute as # the "No mask"-mask @@ -601,6 +659,9 @@ def baseline_raw_form_data(): 'infected_lunch_start': '12:30', 'infected_people': '1', 'infected_start': '09:00', + 'location_latitude': 46.20833, + 'location_longitude': 6.14275, + 'location_name': 'Geneva', 'mask_type': 'Type I', 'mask_wearing_option': 'mask_off', 'mechanical_ventilation_type': '', @@ -637,6 +698,11 @@ WINDOWS_TYPES = {'window_sliding', 'window_hinged', 'not-applicable'} COFFEE_OPTIONS_INT = {'coffee_break_0': 0, 'coffee_break_1': 1, 'coffee_break_2': 2, 'coffee_break_4': 4} +MONTH_NAMES = [ + 'January', 'February', 'March', 'April', 'May', 'June', 'July', + 'August', 'September', 'October', 'November', 'December', +] + def _hours2timestring(hours: float): # Convert times like 14.5 to strings, like "14:30" @@ -692,4 +758,3 @@ for _field in dataclasses.fields(FormData): elif _field.type is bool: _CAST_RULES_FORM_ARG_TO_NATIVE[_field.name] = lambda v: v == '1' _CAST_RULES_NATIVE_TO_FORM_ARG[_field.name] = int - diff --git a/cara/apps/calculator/static/js/form.js b/cara/apps/calculator/static/js/form.js index b15f2fc6..abb888c5 100644 --- a/cara/apps/calculator/static/js/form.js +++ b/cara/apps/calculator/static/js/form.js @@ -311,7 +311,7 @@ function validate_form(form) { var lunch_finish = document.getElementById(activity+"_lunch_finish"); lunch_mins = parseTimeToMins(lunch_finish.value) - parseTimeToMins(lunch_start.value); } - + var coffee_breaks = parseInt(document.querySelector('input[name="'+activity+'_coffee_break_option"]:checked').value); var coffee_duration = parseInt(document.getElementById(activity+"_coffee_duration").value); var coffee_mins = coffee_breaks * coffee_duration; @@ -328,6 +328,24 @@ function validate_form(form) { }); } + // Validate location input. + if (submit) { + // We make the non-visible location inputs mandatory, without marking them as "required" inputs. + // See https://stackoverflow.com/q/22148080/741316 for motivation. + var locationSelectObj= document.getElementById("location_select"); + removeErrorFor(locationSelectObj); + $("input[name*='location']").each(function() { + el = $(this); + if ($.trim(el.val()) == ''){ + submit = false; + } + }); + + if (!submit) { + insertErrorFor(locationSelectObj, "Please select a location"); + } + } + //Validate all non zero values $("input[required].non_zero").each(function() { if (!validateValue(this)) { @@ -335,7 +353,6 @@ function validate_form(form) { } }); - //Validate window venting duration < venting frequency if (!$("#windows_duration").hasClass("disabled")) { var windowsDurationObj = document.getElementById("windows_duration"); @@ -464,9 +481,9 @@ function parseTimeToMins(cTime) { /* -------On Load------- */ $(document).ready(function () { - + var url = new URL(decodeURIComponent(window.location.href)); //Pre-fill form with known values - (new URL(decodeURIComponent(window.location.href))).searchParams.forEach((value, name) => { + url.searchParams.forEach((value, name) => { //If element exists if(document.getElementsByName(name).length > 0) { @@ -484,6 +501,7 @@ $(document).ready(function () { else if (elemObj.type === 'checkbox') { elemObj.checked = (value==1); } + //Ignore 0 (default) values from server side else if (!(elemObj.classList.contains("non_zero") || elemObj.classList.contains("remove_zero")) || (value != "0.0" && value != "0")) { elemObj.value = value; @@ -492,6 +510,21 @@ $(document).ready(function () { } }); + // Handle default URL values if they are not explicitly defined. + if (Array.from(url.searchParams).length > 0) { + if (!url.searchParams.has('location_name')) { + $('[name="location_name"]').val('Geneva') + $('[name="location_select"]').val('Geneva') + } + if (!url.searchParams.has('location_latitude')) { + $('[name="location_latitude"]').val('46.20833') + } + if (!url.searchParams.has('location_longitude')) { + $('[name="location_longitude"]').val('6.14275') + } + } + + // When the document is ready, deal with the fact that we may be here // as a result of a forward/back browser action. If that is the case, update // the visibility of some of our inputs. @@ -531,8 +564,102 @@ $(document).ready(function () { $(".start_time[data-lunch-for]").each(function() {validateLunchBreak($(this).data('time-group'))}); $("[data-lunch-for]").change(function() {validateLunchBreak($(this).data('time-group'))}); $("[data-lunch-break]").change(function() {validateLunchBreak($(this).data('lunch-break'))}); + + $("#location_select").select2({ + ajax: { + // Docs for the geocoding service at: + // https://developers.arcgis.com/rest/geocode/api-reference/geocoding-service-output.htm + url: "https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/suggest", + dataType: 'json', + delay: 250, + data: function(params) { + return { + text: params.term, // search term + f: 'json', + page: params.page, + maxSuggestions: 20, + }; + }, + processResults: function(data, params) { + // Enable infinite scrolling + params.page = params.page || 1; + return { + results: data.suggestions.map(function(suggestion) { + return { + id: suggestion.magicKey, // The unique reference to this result. + text: suggestion.text, + magicKey: suggestion.magicKey + } + }), + pagination: { + more: (params.page * 10) < data.suggestions.length + } + }; + }, + cache: true + }, + placeholder: 'Search for a location', + minimumInputLength: 1, + templateResult: formatlocation, + templateSelection: formatLocationSelection + }); + + function formatlocation(suggestedLocation) { + // Function is called for each location from the geocoding API. + + if (suggestedLocation.loading) { + // Update the first message in the search results to show the + // "Searching..." message. + return suggestedLocation.text; + } + + // Create a container for this location (to be added to the DOM by the select2 + // library when returned). + // This will become one of many search results in the dropdown. + var $container = $( + "
" + + "
" + + "
" + suggestedLocation.text + "
" + + "
" + + "
" + ); + return $container; + } + + function formatLocationSelection(selectedSuggestion) { + // Function is called when a selection is made in the search result dropdown. + + // ID may be empty, for example when the page is refreshed or back button pressed. + if (selectedSuggestion.id != "") { + + // Turn the suggestion into a proper location (so that we can get its latitude & longitude). + $.ajax({ + dataType: "json", + url: 'https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/findAddressCandidates', + data: { + magicKey: selectedSuggestion.magicKey, + outFields: 'country, location', + f: "json" + }, + success: function (locations) { + // If there isn't precisely one result something is very wrong. + geocoded_loc = locations.candidates[0]; + $('input[name="location_name"]').val(selectedSuggestion.text); + $('input[name="location_latitude"]').val(geocoded_loc.location.y.toPrecision(7)); + $('input[name="location_longitude"]').val(geocoded_loc.location.x.toPrecision(7)); + } + }); + + } else if ($('input[name="location_name"]').val() != "") { + // If we have no selection AND the location_name is available, use that in the search bar. + // This means that we preserve the location through refresh/back button. + return $('input[name="location_name"]').val(); + } + return selectedSuggestion.text; + } }); + /* -------Debugging------- */ function debug_submit(form) { diff --git a/cara/apps/calculator/templates/base/calculator.report.html.j2 b/cara/apps/calculator/templates/base/calculator.report.html.j2 index 8a36f52b..ad04ffae 100644 --- a/cara/apps/calculator/templates/base/calculator.report.html.j2 +++ b/cara/apps/calculator/templates/base/calculator.report.html.j2 @@ -199,6 +199,10 @@

  • Room Volume: {{ model.concentration_model.room.volume }} m³

  • Room Central Heating: {{ "On" if form.room_heating_option else "Off" }}

  • +
  • Geographic Location: {{ form.location_name }}

  • + {% if form.ventilation_type == "natural_ventilation" %} +
  • Nearest weather station: {{ form.nearest_weather_station()[1].strip().title() }}

  • + {% endif %} @@ -245,7 +249,7 @@ {% endif %}

    -

    When using the natural ventilation option, air flows are calculated using averaged hourly temperatures for the Geneva region, based on historical data for the month selected.

    +

    When using the natural ventilation option, air flows are calculated using averaged hourly temperatures for the region {{ form.location_name }}, based on historical data for the month selected.

    {% else %} No

    {% endif %} diff --git a/cara/apps/calculator/templates/calculator.form.html.j2 b/cara/apps/calculator/templates/calculator.form.html.j2 index 712432fb..c119d4e5 100644 --- a/cara/apps/calculator/templates/calculator.form.html.j2 +++ b/cara/apps/calculator/templates/calculator.form.html.j2 @@ -6,11 +6,13 @@ {% block extra_headers %} + {% endblock extra_headers %} {% block body_scripts %} + {% endblock body_scripts %} @@ -102,6 +104,23 @@ v{{ calculator_version }} Please sen       + +
    + +
    + ? +
    + +
    + + + + +
    +

    @@ -206,10 +225,10 @@ v{{ calculator_version }} Please sen
    - Exposed person(s) presence:
    + Exposed person(s) presence:
    Start:    Finish:
    - Infected person(s) presence:
    + Infected person(s) presence:
    Start:    Finish:

    diff --git a/cara/data.py b/cara/data/__init__.py similarity index 96% rename from cara/data.py rename to cara/data/__init__.py index 8e844dae..353992e6 100644 --- a/cara/data.py +++ b/cara/data/__init__.py @@ -1,6 +1,8 @@ import numpy as np from cara import models +# TODO: The values in this module to be removed and instead use the cara.data.weather functionality. + # average temperature of each month, hour per hour (from midnight to 11 pm) Geneva_hourly_temperatures_celsius_per_hour = { 'Jan': [0.2, -0.3, -0.5, -0.9, -1.1, -1.4, -1.5, -1.5, -1.1, 0.1, 1.5, diff --git a/cara/data/global_weather_set.json b/cara/data/global_weather_set.json new file mode 100644 index 00000000..ba1765ee --- /dev/null +++ b/cara/data/global_weather_set.json @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c6b41b08c350c543bced4d15b670851042cf1ca9135551b2ed2afb2d99ec63e8 +size 13803155 diff --git a/cara/data/hadisd_station_fullinfo_v311_202001p.txt b/cara/data/hadisd_station_fullinfo_v311_202001p.txt new file mode 100644 index 00000000..1747636a --- /dev/null +++ b/cara/data/hadisd_station_fullinfo_v311_202001p.txt @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4843d34b6e4c26d4382860e011451d5f32157b9a3660830f8d2894a11d022298 +size 772370 diff --git a/cara/data/weather.py b/cara/data/weather.py new file mode 100644 index 00000000..af5f27ec --- /dev/null +++ b/cara/data/weather.py @@ -0,0 +1,149 @@ +import datetime +import functools +import json +from pathlib import Path +import typing + +import dateutil.tz +import numpy as np +from scipy.spatial import cKDTree +from timezonefinder import TimezoneFinder + + +WX_DATA_LOCATION = Path(__file__).absolute().parent +WxStationIdType = str +MonthType = str +# HourlyTempType - 24 temperatures, one for each hour of the day (the average for the given month). +HourlyTempType = typing.List[float] +WxStationRecordType = typing.Tuple[WxStationIdType, str, float, float] + + +@functools.lru_cache() +def wx_data() -> typing.Dict[WxStationIdType, typing.Dict[MonthType, HourlyTempType]]: + """ + Load the weather data (temperature in kelvin). + + The data is structured by station location, and for each station location, by month. + + """ + with (WX_DATA_LOCATION / 'global_weather_set.json').open("r") as json_file: + data = json.load(json_file) + + for station in list(data.keys()): + for month in list(data[station].keys()): + if not np.any(np.isnan(data[station][month])): + data[station][month] = tuple( + 273.15 + np.array(data[station][month])) + return data + + +@functools.lru_cache() +def wx_station_data() -> typing.Dict[WxStationIdType, WxStationRecordType]: + """ + Return a dictionary of ``station-id: station records``, where station records + are of the form ``(station-id, station-name, station-latitude, station-longitude)``. + + The stations returned are guaranteed to have valid weather data. + + """ + weather_data = wx_data() + station_data = {} + fixed_delimits = [0, 12, 13, 44, 51, 60, 69, 90, 91] + station_file = WX_DATA_LOCATION / 'hadisd_station_fullinfo_v311_202001p.txt' + + for line in station_file.open('rt'): + start_end_positions = zip(fixed_delimits[:-1], fixed_delimits[1:]) + split_vals = [line[start:end] for start, end in start_end_positions] + station_location = ( + split_vals[0], split_vals[2], float(split_vals[3]), float(split_vals[4]), + ) + # We only consider stations with weather data, don't include the rest. + if split_vals[0] in weather_data: + station_data[split_vals[0]] = station_location + return station_data + + +@functools.lru_cache() +def _wx_station_kdtree() -> cKDTree: + """Build a kd-tree of wx station longitude & latitudes (note the coordinate order)""" + station_data = wx_station_data().values() + coords = np.array([(stn_record[3], stn_record[2]) + for stn_record in station_data]) + return cKDTree(coords) + + +def mean_hourly_temperatures(wx_station: str, month: int) -> HourlyTempType: + """ + Return the mean monthly temperature for the given weather station and month. + + Returns + ------- + + temperatures: List[24 floats] + A list containing 24 temperature values, one for each hour, in kelvin. + Index 0 of the result corresponds to hour 00:00 (UTC), and index 23 (the last) to 23:00 (UTC). + + """ + # Note that the current dataset encodes month number as a string. + return wx_data()[wx_station][str(month)] + + +def timezone_at(*, latitude: float, longitude: float) -> datetime.tzinfo: + """Find a timezone for the given location, or raise.""" + tf = TimezoneFinder() + tz_name = tf.timezone_at(lat=latitude, lng=longitude) + tz = dateutil.tz.gettz(tz_name) + if tz_name is None or tz is None: + raise ValueError( + f"Unable to determine the timezone of given location " + f"(lat={latitude}, lng={longitude})" + ) + return tz + + +def refine_hourly_data(source_times, hourly_data, npts): + """ + Given times (in hours), where each data point is on the hour, + interpolate the data to mid-point of the returned boundaries. + + For example: + + >>> time_bounds, data = refine_hourly_data(list(range(24)), list(range(24)), 24) + >>> len(time_bounds), len(data) + (25, 24) + >>> time_bounds + array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12., + 13., 14., 15., 16., 17., 18., 19., 20., 21., 22., 23., 24.]) + >>> data + array([ 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5, + 11.5, 12.5, 13.5, 14.5, 15.5, 16.5, 17.5, 18.5, 19.5, 20.5, 21.5, + 22.5, 11.5]) + + The source times need not be monotonic, which allows for data to be + time-offset shifted. For example: + + >>> time_bounds, data = refine_hourly_data( + ... list(range(6, 28)) + [4, 5], list(range(24)), 24) + >>> data + array([18.5, 19.5, 20.5, 21.5, 22.5, 11.5, 0.5, 1.5, 2.5, 3.5, 4.5, + 5.5, 6.5, 7.5, 8.5, 9.5, 10.5, 11.5, 12.5, 13.5, 14.5, 15.5, + 16.5, 17.5]) + + """ + target_time_boundaries, step = np.linspace( + 0, 24, npts + 1, retstep=True, endpoint=True, + ) + target_times = target_time_boundaries[:-1] + step / 2 + data = np.interp(target_times, np.array(source_times), hourly_data, period=24) + return target_time_boundaries, data + + +def nearest_wx_station(*, longitude: float, latitude: float) -> WxStationRecordType: + """ + Given a latitude & longitude, return the nearest station with valid weather data. + + """ + ktree = _wx_station_kdtree() + station_data = list(wx_station_data().values()) + dd, ii = ktree.query((longitude, latitude), k=[1]) + return station_data[ii[0]] diff --git a/cara/tests/apps/calculator/test_model_generator.py b/cara/tests/apps/calculator/test_model_generator.py index 53045564..6ee821c0 100644 --- a/cara/tests/apps/calculator/test_model_generator.py +++ b/cara/tests/apps/calculator/test_model_generator.py @@ -1,14 +1,14 @@ import dataclasses +import typing +import numpy as np +import numpy.testing as npt import pytest from cara.apps.calculator import model_generator from cara.apps.calculator.model_generator import _hours2timestring from cara.apps.calculator.model_generator import minutes_since_midnight from cara import models -from cara import data -import numpy as np -import numpy.testing as npt def test_model_from_dict(baseline_form_data): @@ -33,13 +33,6 @@ def test_blend_expiration(): def test_ventilation_slidingwindow(baseline_form: model_generator.FormData): - room = models.Room(75) - window = models.SlidingWindow( - active=models.PeriodicInterval(period=120, duration=10), - inside_temp=models.PiecewiseConstant((0, 24), (293,)), - outside_temp=data.GenevaTemperatures['Dec'], - window_height=1.6, opening_length=0.6, - ) baseline_form.ventilation_type = 'natural_ventilation' baseline_form.windows_duration = 10 baseline_form.windows_frequency = 120 @@ -49,19 +42,28 @@ def test_ventilation_slidingwindow(baseline_form: model_generator.FormData): baseline_form.window_height = 1.6 baseline_form.opening_distance = 0.6 - ts = np.linspace(8, 16, 100) - np.testing.assert_allclose([window.air_exchange(room, t)+0.25 for t in ts], - [baseline_form.ventilation().air_exchange(room, t) for t in ts]) + baseline_vent = baseline_form.ventilation() + assert isinstance(baseline_vent, models.MultipleVentilation) + baseline_window = baseline_vent.ventilations[0] + assert isinstance(baseline_window, models.SlidingWindow) + + window = models.SlidingWindow( + active=models.PeriodicInterval(period=120, duration=10), + inside_temp=models.PiecewiseConstant((0, 24), (293,)), + outside_temp=baseline_window.outside_temp, + window_height=1.6, opening_length=0.6, + ) + + ach = models.AirChange( + active=models.PeriodicInterval(period=120, duration=120), + air_exch=0.25, + ) + ventilation = models.MultipleVentilation((window, ach)) + + assert ventilation == baseline_vent def test_ventilation_hingedwindow(baseline_form: model_generator.FormData): - room = models.Room(75) - window = models.HingedWindow( - active=models.PeriodicInterval(period=120, duration=10), - inside_temp=models.PiecewiseConstant((0, 24), (293,)), - outside_temp=data.GenevaTemperatures['Dec'], - window_height=1.6, window_width=1., opening_length=0.6, - ) baseline_form.ventilation_type = 'natural_ventilation' baseline_form.windows_duration = 10 baseline_form.windows_frequency = 120 @@ -72,9 +74,24 @@ def test_ventilation_hingedwindow(baseline_form: model_generator.FormData): baseline_form.window_width = 1. baseline_form.opening_distance = 0.6 - ts = np.linspace(8, 16, 100) - np.testing.assert_allclose([window.air_exchange(room, t)+0.25 for t in ts], - [baseline_form.ventilation().air_exchange(room, t) for t in ts]) + baseline_vent = baseline_form.ventilation() + assert isinstance(baseline_vent, models.MultipleVentilation) + baseline_window = baseline_vent.ventilations[0] + assert isinstance(baseline_window, models.HingedWindow) + + window = models.HingedWindow( + active=models.PeriodicInterval(period=120, duration=10), + inside_temp=models.PiecewiseConstant((0, 24), (293,)), + outside_temp=baseline_window.outside_temp, + window_height=1.6, window_width=1., opening_length=0.6, + ) + ach = models.AirChange( + active=models.PeriodicInterval(period=120, duration=120), + air_exch=0.25, + ) + ventilation = models.MultipleVentilation((window, ach)) + + assert ventilation == baseline_vent def test_ventilation_mechanical(baseline_form: model_generator.FormData): @@ -108,19 +125,6 @@ def test_ventilation_airchanges(baseline_form: model_generator.FormData): def test_ventilation_window_hepa(baseline_form: model_generator.FormData): - room = models.Room(75) - window = models.SlidingWindow( - active=models.PeriodicInterval(period=120, duration=10), - inside_temp=models.PiecewiseConstant((0, 24), (293,)), - outside_temp=data.GenevaTemperatures['Dec'], - window_height=1.6, opening_length=0.6, - ) - hepa = models.HEPAFilter( - active=models.PeriodicInterval(period=120, duration=120), - q_air_mech=250., - ) - ventilation = models.MultipleVentilation((window,hepa)) - baseline_form.ventilation_type = 'natural_ventilation' baseline_form.windows_duration = 10 baseline_form.windows_frequency = 120 @@ -130,9 +134,29 @@ def test_ventilation_window_hepa(baseline_form: model_generator.FormData): baseline_form.opening_distance = 0.6 baseline_form.hepa_option = True - ts = np.linspace(9, 17, 100) - np.testing.assert_allclose([ventilation.air_exchange(room, t)+0.25 for t in ts], - [baseline_form.ventilation().air_exchange(room, t) for t in ts]) + baseline_vent = baseline_form.ventilation() + assert isinstance(baseline_vent, models.MultipleVentilation) + baseline_window = baseline_vent.ventilations[0] + assert isinstance(baseline_window, models.SlidingWindow) + + # Now build the equivalent ventilation instance directly, and compare. + window = models.SlidingWindow( + active=models.PeriodicInterval(period=120, duration=10), + inside_temp=models.PiecewiseConstant((0, 24), (293,)), + outside_temp=baseline_window.outside_temp, + window_height=1.6, opening_length=0.6, + ) + hepa = models.HEPAFilter( + active=models.PeriodicInterval(period=120, duration=120), + q_air_mech=250., + ) + ach = models.AirChange( + active=models.PeriodicInterval(period=120, duration=120), + air_exch=0.25, + ) + ventilation = models.MultipleVentilation((window, hepa, ach)) + + assert ventilation == baseline_vent def present_times(interval: models.Interval) -> models.BoundarySequence_t: @@ -410,6 +434,12 @@ def test_key_validation_mech_ventilation_type_na(baseline_form_data): model_generator.FormData.from_dict(baseline_form_data) +def test_key_validation_event_month(baseline_form_data): + baseline_form_data['event_month'] = 'invalid month' + with pytest.raises(ValueError, match='invalid month is not a valid value for event_month'): + model_generator.FormData.from_dict(baseline_form_data) + + def test_default_types(): # Validate that FormData._DEFAULTS are complete and of the correct type. # Validate that we have the right types and matching attributes to the DEFAULTS. @@ -444,3 +474,23 @@ def test_form_to_dict(baseline_form): # If we set the value to the default one, it should no longer turn up in the dictionary. baseline_form.exposed_coffee_break_option = model_generator.FormData._DEFAULTS['exposed_coffee_break_option'] assert 'exposed_coffee_break_option' not in baseline_form.to_dict(baseline_form, strip_defaults=True) + + +@pytest.mark.parametrize( + ["longitude", "latitude", "month", "expected_tz_name", "expected_offset"], + [ + [6.14275, 46.20833, "January", 'CET', 1], # Geneva, winter + [6.14275, 46.20833, "May", 'CEST', 2], # Geneva, summer + [144.96751, -37.81739, "January", 'AEDT', 11], # Melbourne, summer time + [144.96751, -37.81739, "June", 'AEST', 10], # Melbourne, winter time + [-176.433333, -44.033333, 'August', '+1245', 12.75], # Chatham Islands + ] +) +def test_form_timezone(baseline_form_data, longitude, latitude, month, expected_tz_name, expected_offset): + baseline_form_data['location_latitude'] = latitude + baseline_form_data['location_longitude'] = longitude + baseline_form_data['event_month'] = month + form = model_generator.FormData.from_dict(baseline_form_data) + name, offset = form.tz_name_and_utc_offset() + assert name == expected_tz_name + assert offset == expected_offset diff --git a/cara/tests/data/__init__.py b/cara/tests/data/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/cara/tests/data/test_weather.py b/cara/tests/data/test_weather.py new file mode 100644 index 00000000..2a667df8 --- /dev/null +++ b/cara/tests/data/test_weather.py @@ -0,0 +1,74 @@ +import datetime + +import dateutil.tz +import numpy as np +import numpy.testing +import pytest + +import cara.data.weather as wx + + +def test_nearest_wx_station(): + melbourne_lat, melbourne_lon = -37.81739, 144.96751 + station_rec = wx.nearest_wx_station(longitude=melbourne_lon, latitude=melbourne_lat) + + station_name = station_rec[1].strip() + # Note: For Melbourne, the nearest station is 'MELBOURNE REGIONAL OFFICE', + # but the nearest location with suitable wx data is 'MELBOURNE ESSENDON' + assert station_name == 'MELBOURNE ESSENDON' + + +def test_refine(): + source_times = [0, 3, 6, 9, 12, 15, 18, 21] + data = [0, 30, 60, 90, 120, 90, 60, 30] + + time_bounds, data = wx.refine_hourly_data(source_times, data, 4) + + # Notice that the expected data falls in the mid-point of the + # expected time bounds. + np.testing.assert_array_equal(time_bounds, [0., 6., 12., 18., 24.]) + np.testing.assert_array_equal(data, [30., 90., 90., 30.]) + + +def test_refine_offset(): + source_times = [14, 20, 26, 32] + data = [200., 182, 168, 192] + + time_bounds, data = wx.refine_hourly_data(source_times, data, 6) + + # Notice that the expected data falls in the mid-point of the + # expected time bounds. + np.testing.assert_array_equal(time_bounds, [0., 4., 8., 12., 16., 20., 24.]) + np.testing.assert_array_almost_equal(data, [168., 184., 194.666667, 200., 188., 177.333333]) + + +def test_refine_non_monotonic(): + source_times = [14, 20, 2, 8] + data = [200., 182, 168, 192] + + time_bounds, data = wx.refine_hourly_data(source_times, data, 6) + + # Notice that the expected data falls in the mid-point of the + # expected time bounds. + np.testing.assert_array_equal(time_bounds, [0., 4., 8., 12., 16., 20., 24.]) + np.testing.assert_array_almost_equal(data, [168., 184., 194.666667, 200., 188., 177.333333]) + + + +def test_timezone_at__out_of_range(): + with pytest.raises(ValueError, match='out of bounds'): + wx.timezone_at(latitude=88, longitude=181) + + +@pytest.mark.parametrize( + ["latitude", "longitude", "expected_tz_name"], + [ + [6.14275, 46.20833, 'Europe/Zurich'], # Geneva + [144.96751, -37.81739, "Australia/Melbourne"], # Melbourne + [-176.433333, -44.033333, 'Pacific/Chatham'], # Chatham Islands + ] +) +def test_timezone_at__expected(latitude, longitude, expected_tz_name): + assert wx.timezone_at(latitude=longitude, longitude=latitude) == dateutil.tz.gettz(expected_tz_name) + assert wx.timezone_at(latitude=0, longitude=-175) == dateutil.tz.gettz('Etc/GMT+12') + assert wx.timezone_at(latitude=89.8, longitude=-170) == dateutil.tz.gettz('Etc/GMT+11') diff --git a/requirements.txt b/requirements.txt index 302de847..457a69b7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -74,6 +74,7 @@ sniffio==1.2.0 terminado==0.10.1 testpath==0.5.0 threadpoolctl==2.2.0 +timezonefinder==5.2.0 tornado==6.1 traitlets==5.0.5 urllib3==1.26.6 diff --git a/setup.cfg b/setup.cfg index 21bbf8d4..c2f968b6 100644 --- a/setup.cfg +++ b/setup.cfg @@ -25,3 +25,7 @@ ignore_missing_imports = True [mypy-scipy.*] ignore_missing_imports = True + +[mypy-timezonefinder.*] +ignore_missing_imports = True + diff --git a/setup.py b/setup.py index a70d5237..9e5eb596 100644 --- a/setup.py +++ b/setup.py @@ -29,9 +29,11 @@ REQUIREMENTS: dict = { 'mistune', 'numpy', 'psutil', + 'python-dateutil', 'qrcode[pil]', 'scipy', 'sklearn', + 'timezonefinder', 'tornado', 'voila >=0.2.4', ], @@ -42,6 +44,7 @@ REQUIREMENTS: dict = { 'pytest-tornasync', 'numpy-stubs @ git+https://github.com/numpy/numpy-stubs.git', 'types-dataclasses', + 'types-python-dateutil', ], 'dev': [ 'jupyterlab', @@ -84,5 +87,7 @@ setup( 'apps/*/*/*', 'apps/*/*/*/*', 'apps/*/*/*/*/*', + 'data/*.json', + 'data/*.txt', ]}, )