File size: 5,968 Bytes
60cca7c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
"""
Export utilities for HVAC Load Calculator

This module provides functions for exporting data from the HVAC Load Calculator.
"""

import json
import csv
import io
import pandas as pd
from datetime import datetime


def export_data(form_data, results, format='json'):
    """
    Export form data and calculation results.
    
    Args:
        form_data (dict): Form input data
        results (dict): Calculation results
        format (str): Export format ('json' or 'csv')
        
    Returns:
        str: Exported data as string
    """
    if format == 'json':
        return export_as_json(form_data, results)
    elif format == 'csv':
        return export_as_csv(form_data, results)
    else:
        raise ValueError(f"Unsupported export format: {format}")


def export_as_json(form_data, results):
    """
    Export data as JSON.
    
    Args:
        form_data (dict): Form input data
        results (dict): Calculation results
        
    Returns:
        str: JSON string
    """
    # Combine form data and results
    export_data = {
        'form_data': form_data,
        'results': results,
        'export_timestamp': datetime.now().isoformat()
    }
    
    # Convert to JSON string
    return json.dumps(export_data, indent=2)


def export_as_csv(form_data, results):
    """
    Export data as CSV.
    
    Args:
        form_data (dict): Form input data
        results (dict): Calculation results
        
    Returns:
        str: CSV string
    """
    # Create a buffer for CSV data
    output = io.StringIO()
    writer = csv.writer(output)
    
    # Write header
    writer.writerow(['HVAC Load Calculator Results', datetime.now().isoformat()])
    writer.writerow([])
    
    # Write building information
    writer.writerow(['Building Information'])
    building_info = form_data.get('building_info', {})
    for key, value in building_info.items():
        writer.writerow([key, value])
    writer.writerow([])
    
    # Write calculation results
    writer.writerow(['Calculation Results'])
    for key, value in results.items():
        if key not in ['building_info', 'timestamp'] and not isinstance(value, dict):
            writer.writerow([key, value])
    writer.writerow([])
    
    # Write load components
    writer.writerow(['Load Components'])
    writer.writerow(['Component', 'Load (W)', 'Percentage (%)'])
    
    # Calculate percentages
    sensible_load = results.get('sensible_load', 1)  # Avoid division by zero
    
    components = {
        'Conduction (Opaque Surfaces)': results.get('conduction_gain', 0),
        'Conduction (Windows)': results.get('window_conduction_gain', 0),
        'Solar Radiation (Windows)': results.get('window_solar_gain', 0),
        'Infiltration & Ventilation': results.get('infiltration_gain', 0),
        'Internal Gains': results.get('internal_gain', 0)
    }
    
    for component, load in components.items():
        percentage = (load / sensible_load) * 100 if sensible_load > 0 else 0
        writer.writerow([component, f"{load:.2f}", f"{percentage:.2f}"])
    
    # Get CSV content
    return output.getvalue()


def generate_report(form_data, results, calculation_type='cooling'):
    """
    Generate a formatted report of calculation results.
    
    Args:
        form_data (dict): Form input data
        results (dict): Calculation results
        calculation_type (str): Type of calculation ('cooling' or 'heating')
        
    Returns:
        str: Formatted report as HTML
    """
    # Create a DataFrame for the report
    report_data = []
    
    # Add building information
    building_info = form_data.get('building_info', {})
    report_data.append({
        'Section': 'Building Information',
        'Item': 'Building Name',
        'Value': building_info.get('building_name', 'N/A')
    })
    report_data.append({
        'Section': 'Building Information',
        'Item': 'Location',
        'Value': building_info.get('location_name', 'N/A')
    })
    report_data.append({
        'Section': 'Building Information',
        'Item': 'Floor Area',
        'Value': f"{building_info.get('floor_area', 0):.2f} m²"
    })
    report_data.append({
        'Section': 'Building Information',
        'Item': 'Volume',
        'Value': f"{building_info.get('volume', 0):.2f} m³"
    })
    
    # Add calculation results
    if calculation_type == 'cooling':
        report_data.append({
            'Section': 'Results',
            'Item': 'Sensible Cooling Load',
            'Value': f"{results.get('sensible_load', 0):.2f} W"
        })
        report_data.append({
            'Section': 'Results',
            'Item': 'Latent Cooling Load',
            'Value': f"{results.get('latent_load', 0):.2f} W"
        })
        report_data.append({
            'Section': 'Results',
            'Item': 'Total Cooling Load',
            'Value': f"{results.get('total_load', 0):.2f} W"
        })
        report_data.append({
            'Section': 'Results',
            'Item': 'Cooling Load per Area',
            'Value': f"{results.get('total_load', 0) / building_info.get('floor_area', 1):.2f} W/m²"
        })
    else:  # heating
        report_data.append({
            'Section': 'Results',
            'Item': 'Total Heating Load',
            'Value': f"{results.get('total_load', 0):.2f} W"
        })
        report_data.append({
            'Section': 'Results',
            'Item': 'Heating Load per Area',
            'Value': f"{results.get('total_load', 0) / building_info.get('floor_area', 1):.2f} W/m²"
        })
        if 'annual_energy_kwh' in results:
            report_data.append({
                'Section': 'Results',
                'Item': 'Annual Heating Energy',
                'Value': f"{results.get('annual_energy_kwh', 0):.2f} kWh"
            })
    
    # Create DataFrame
    df = pd.DataFrame(report_data)
    
    # Convert to HTML
    html = df.to_html(index=False)
    
    return html