这段MATLAB代码定义了一个名为 cutEnergyPlusData
的函数,用于从 EnergyPlus 数据中提取指定时间段的数据。EnergyPlus 数据通常是一整年的数据(1月1日至12月31日),而该函数可以根据用户指定的起始日期和时长,提取出所需的时间段数据。以下是代码的详细解读:
函数功能
- 目的:从 EnergyPlus 数据中提取指定时间段的数据。
- 特点:
- 支持跨年提取(例如从11月到次年3月)。
- 支持提取超过一年的数据(但天气数据会重复)。
- 适用场景:用于温室气候模拟、能源分析等领域。
代码解析
- 函数定义:
function season = cutEnergyPlusData(firstDay, seasonLength, path)
这行代码定义了一个名为
cutEnergyPlusData
的函数,接受三个输入参数:firstDay
:起始日期,表示从年初开始的天数(例如1表示1月1日)。seasonLength
:提取的时间段长度,单位为天(可以是小数)。path
:EnergyPlus 数据的文件路径(MAT 文件)。
返回一个输出参数season
,表示提取的时间段数据。
- 注释说明:
% CUTENERGYPLUSDATA Cut data from EnergyPlus to the required season segment % % The EnergyPlus data is a full year (Jan 1 - Dec 31). This function cuts % out a segment of the data according to the desired season. % Allows passing over the end of the year, e.g., a segment of November-March. % Also allows a season of more than a year if needed (but the years will % have identical weather). % % Usage: % season = loadEnergyPlusData(firstDay, seasonLength, path) % % Inputs: % firstDay Where to start looking at the data (days since start of % the year). Example: 1 (Jan 1) % seasonLength Length of the input in days (fractions accepted). % Example: 1 % path Path of a MAT file in the energyPlus format, created using saveWeatherMat % and has a variable named hiresWeather in the format as % below. Example: 'beiCSWD.mat' % % Column Description Unit % hiresWeather(:,1) timestamps of the input [datenum] % hiresWeather(:,2) outdoor global irradiation [W m^{-2}] % hiresWeather(:,3) outdoor air temperature [癈] % hiresWeather(:,4) outdoor vapor density [kg m^{-3}] % hiresWeather(:,5) outdoor CO2 density [kg{CO2} m^{-3}{air}] % hiresWeather(:,6) outdoor wind speed [m s^{-1}] % hiresWeather(:,7) sky temperature [癈] % hiresWeather(:,8) temperature of external soil layer [癈] % hiresWeather(:,9) daily radiation sum [MJ m^{-2} day^{-1}] % hiresWeather(:,10) elevation [m] % % % Output: % season A matrix with the same format of hiresWeather above, % cut so that it start and ends in the requested time % % Used to generate the data in: % Katzin, D., Marcelis, L. F. M., & van Mourik, S. (2021). % Energy savings in greenhouses by transition from high-pressure sodium % to LED lighting. Applied Energy, 281, 116019. % https://doi.org/10.1016/j.apenergy.2020.116019
这些注释提供了函数的功能描述、使用方法、输入输出参数的说明,以及相关论文的引用。
- 作者信息:
% David Katzin, Wageningen University % david.katzin@wur.nl % david.katzin1@gmail.com
这部分注释提供了作者的姓名、单位和联系方式。
- 定义常量:
SECONDS_IN_DAY = 24*60*60;
SECONDS_IN_DAY
:一天的总秒数(24小时 × 60分钟 × 60秒)。
- 加载数据:
load(path, 'hiresWeather'); input = hiresWeather;
- 从指定路径的 MAT 文件中加载
hiresWeather
数据,并将其存储在变量input
中。
- 从指定路径的 MAT 文件中加载
- 计算时间间隔:
interval = (input(2,1)-input(1,1))*SECONDS_IN_DAY;
- 计算数据的时间间隔(单位为秒),假设数据是等间隔的。
- 处理起始日期:
yearShift = floor(firstDay/365); % if firstDay is bigger than 365 firstDay = mod(firstDay,365); % in case value is bigger than 365
- 如果
firstDay
超过365天,则计算跨年的年数yearShift
,并将firstDay
调整为一年内的天数。
- 如果
- 计算起始点和结束点:
startPoint = 1+round((firstDay-1)*SECONDS_IN_DAY/interval); endPoint = startPoint-1+round(seasonLength*SECONDS_IN_DAY/interval);
startPoint
:数据中起始日期对应的索引。endPoint
:数据中结束日期对应的索引。
- 处理跨年数据:
dataLength = length(input(:,1)); newYears = (endPoint-mod(endPoint,dataLength))/dataLength;
- 计算数据跨越新年的次数
newYears
。
- 计算数据跨越新年的次数
- 提取数据:
if endPoint <= dataLength season = input(startPoint:endPoint,:); else % required season passes over end of year season = input(startPoint:end,:); resetDate = input(:,1)-input(1,1)+interval/SECONDS_IN_DAY; % dates from input but start at 0+interval for n=1:newYears-1 dateShift = resetDate+season(end,1); inputDateShift = [dateShift input(:,2:end)]; season = [season; inputDateShift]; end endPoint = mod(endPoint,dataLength); dateShift = resetDate+season(end,1); inputDateShift = [dateShift input(:,2:end)]; season = [season; inputDateShift(1:endPoint,:)]; end
- 如果时间段不跨年,则直接提取数据。
- 如果时间段跨年,则循环添加新年数据,直到覆盖整个时间段。
示例
假设:
firstDay = 300
(10月27日)。seasonLength = 100
(100天)。path = 'beiCSWD.mat'
(EnergyPlus 数据文件)。
- 计算起始点和结束点:
startPoint
对应10月27日的索引。endPoint
对应2月4日的索引(跨年)。
- 提取数据:
- 从10月27日到12月31日的数据。
- 添加1月1日到2月4日的数据。
总结
这段代码实现了一个从 EnergyPlus 数据中提取指定时间段的功能,支持跨年提取和超过一年的数据提取。适用于温室气候模拟、能源分析等领域。