Skip to main content
ClimApp uses two lightweight Python classes to represent its core domain objects. RegistroClimatico captures a single meteorological observation from any source (manual entry or AEMET live feed), while JSONRepository handles all persistence to the local JSON file. Together they form a clean separation between the data shape and storage concerns.

RegistroClimatico

RegistroClimatico is the primary domain model. It wraps one climate reading and guarantees that numeric fields are stored as floats, even if a controller passes string values from a form submission.

Constructor

All four metric arguments are cast to float inside __init__. This means the constructor will raise a ValueError if a non-numeric string (e.g. "N/A") is passed — validate inputs before instantiating.
string
required
Identifier of the weather station or municipality that produced the reading.
string
required
Date string for the observation. No format is enforced by the model itself; use validar_fecha() upstream.
number
required
Air temperature in °C. Cast to float automatically.
number
required
Relative humidity as a percentage (0–100). Cast to float automatically.
number
required
Wind speed in km/h. Cast to float automatically.
number
required
Rainfall in mm. Cast to float automatically.

to_dict()

Returns a plain dictionary suitable for JSON serialisation. The JSONRepository calls this before writing to disk; the Flask routes return it directly to the frontend.
string
Station or municipality identifier, unchanged from construction.
string
Observation date as stored at construction time.
number
Temperature in °C (float).
number
Relative humidity percentage (float).
number
Wind speed in km/h (float).
number
Rainfall in mm (float).

Full class source


JSONRepository

JSONRepository manages the flat JSON file that stores all climate records. It is instantiated once at module level and exposed through three convenience functions.

Constructor

string
default:"data/registros_climaticos.json"
Path to the JSON storage file. When None, defaults to data/registros_climaticos.json relative to the working directory. The constructor creates the file (and any missing parent directories) automatically if it does not exist.

guardar(registro_dict)

Appends a single record dictionary to the JSON array on disk. Reads the entire file, appends, then writes back with indent=4. Returns True on success, False if any exception is raised.
object
required
A plain dictionary representing one climate record, typically produced by RegistroClimatico.to_dict().

filtrar_registros(municipio, fuente, fecha)

Returns a filtered list of records from the JSON file. All parameters are optional; omitting one skips that filter.
string
Municipality name to filter by. Comparison is case-insensitive and uses substring matching (via in).
string
Data source to filter by. Accepted values in practice are "manual" and "aemet". Uses exact match.
string
Date string to filter by. Uses exact string match against the fecha field.

obtener_ultimo_registro(municipio, fuente)

Scans the JSON array in reverse and returns the first record whose municipio field matches (case-insensitive, exact). Returns None if no match is found or if the file cannot be read.
The fuente parameter is accepted in the method signature but the current implementation only filters by municipio. The source filter is not applied in this method.

Module-level convenience functions

A shared _repo = JSONRepository() instance is created at import time. Use these functions instead of instantiating the class directly:

Zona

Zona maps a Spanish municipality to its corresponding AEMET reference station. It is used alongside estacion_por_municipio.json to resolve municipality names and station identifiers.

Constructor

string
required
The human-readable municipality name (e.g. "Madrid").
string
required
The INE (Spanish National Statistics Institute) code for the municipality.
string
required
The AEMET station identifier linked to this municipality.
string
required
The human-readable name of the AEMET reference station.

Zona.from_dict(data)

Static factory method. Creates a Zona from a single entry in estacion_por_municipio.json:

to_dict()

Serializes the zone back to a plain dictionary with keys municipio, cod_ine, id_estacion, and estacion_referencia. Useful for returning municipality lists through a future API endpoint.