How to create separate data for daylight hours and nighttime hours in MATLAB?

58 Views Asked by At

I have hourly surface temperature data from the model output. The data is a 3D matrix with a size of 303×185×1176 (lon×lat×time). Now, I have to create two matrixes from the existing data. One matrix will be for only daylight hours (6.30 AM-6.30 PM) and another matrix will be for only nighttime hours (6.30 PM-5.30 AM). Since the timestep of the model output is in UTC, I have to add 5.30 with the model hours to get the IST (Indian standard time). Thus, in my case, for the first day, daylight hours start from the timesteps 1 to 13, and nighttime hours start from the timesteps 13 to 24. So, we have to add 24 hours to the above timestep ranges to get the daytime and nighttime hours for the next day. Similarly, for the day after the next day, we have to add 48 hours with the first timestep ranges for getting the daytime and nighttime hours, and so on. I tried with the following MATLAB code:

num_hours_per_day = 24;
num_days = size(ST_data, 3) / num_hours_per_day; 
daytime_start = 1; % Initial daytime start timestep
nighttime_start = 13; % Initial nighttime start timestep
num_hours = size(ST_data, 3);
% Initialize daytime and nighttime matrices
daytime_matrix = [];
nighttime_matrix = [];
% Loop through each day
for day = 1:num_days
    % Calculate indices for daytime and nighttime hours
    daytime_indices = daytime_start:daytime_start+12; % Daytime hours (01:00 to 13:00)
    nighttime_indices = nighttime_start:nighttime_start+11; % Nighttime hours (14:00 to 24:00)
    
    % Update daytime and nighttime matrices
    daytime_matrix(:,:,daytime_indices) = ST_data(:,:,daytime_indices);
    nighttime_matrix(:,:,nighttime_indices) = ST_data(:,:,nighttime_indices);
    
    % Update start timesteps for the next day
    daytime_start = daytime_start + 24;
    nighttime_start = nighttime_start + 24;
end

But, I think that my code is incorrect. Because, after running the code, the size of daytime_matrix is 303×185×1165 and nighttime_matrix is 303×185×1176. If I am separating the total hours into daytime and nighttime, then the size of the third dimension of each matrix should be almost half of the total timestep.
For your reference, I am giving the lines that are printed after giving the command 'whos ST_data':

>> whos ST_data
  Name           Size                      Bytes  Class     Attributes

  ST_data      303x185x1176            527365440  double    

Also, the following lines are printed when I run 'ST_data(1, 1, 1:3)' in the command window:

>> ST_data(1, 1, 1:3)

ans(:,:,1) =

   15.2422


ans(:,:,2) =

   15.0156


ans(:,:,3) =

   14.7905

Note that the values of the surface temperature are in degrees Celsius. So, can anyone please help me to create the correct MATLAB code according to the above calculation? That will be very helpful for me. Thank you for your time and consideration.

With regards,

Ankan

2

There are 2 best solutions below

1
Mmohammad Karimi On
% Initialize daytime and nighttime matrices with the correct size
daytime_matrix = zeros(size(ST_data, 1), size(ST_data, 2), num_hours);
nighttime_matrix = zeros(size(ST_data, 1), size(ST_data, 2), num_hours);

% Loop through each day
for day = 1:num_days
    % Calculate indices for daytime and nighttime hours
    daytime_indices = mod(daytime_start-1, num_hours_per_day) + 1; % Wrap around to beginning of day if needed
    nighttime_indices = mod(nighttime_start-1, num_hours_per_day) + 1; % Wrap around to beginning of day if needed

    % Update daytime and nighttime matrices
    daytime_matrix(:,:,daytime_indices) = ST_data(:,:,daytime_indices);
    nighttime_matrix(:,:,nighttime_indices) = ST_data(:,:,nighttime_indices);

    % Update start timesteps for the next day
    daytime_start = daytime_start + num_hours_per_day;
    nighttime_start = nighttime_start + num_hours_per_day;
end

% Remove trailing zeros from matrices to ensure correct size
daytime_matrix = daytime_matrix(:,:,1:num_hours);
nighttime_matrix = nighttime_matrix(:,:,1:num_hours);
0
MH N On

It would be better if you provide us downloadable link of your mat file data using anykind of cloud storage service like google drive, dropbox, or OneDrive.

  1. Upload your file to your preferred cloud storage service.
  2. Make sure the file is set to public or anyone with the link can view it.
  3. Copy the shareable link provided by the service.
  4. Paste this link into your Stack Overflow post, providing context and detailing what help you need with this file.

It's a littile bit ambiguous with your explanations.

Since I couldn't see the exact data, I inferred the data based on your explanation and tested following code.

If this is what you want to do: Obtain the total number of days for one 3D-Data and classify it by date into two time zones (from 01:00 to 13:00 and 13:00 to 24:00 per day)

ST_data = rand(303, 185, 1176);

num_hours_per_day = 24;
num_days = size(ST_data, 3) / num_hours_per_day;
daytime_start = 1; % Initial daytime start timestep
nighttime_start = 13; % Initial nighttime start timestep

% Initialize structure array for storing each day's data
daily_data = struct();

% Loop through each day
for day = 1:num_days
    % Calculate indices for daytime and nighttime hours in ST_data
    daytime_indices = daytime_start:daytime_start+12; % Daytime hours (01:00 to 13:00)
    nighttime_indices = nighttime_start:nighttime_start+11; % Nighttime hours (13:00 to 24:00)
    
    % Extract daytime and nighttime data for the current day
    current_daytime_data = ST_data(:, :, daytime_indices);
    current_nighttime_data = ST_data(:, :, nighttime_indices);
    
    % Store the data for the current day in the structure array
    daily_data(day).daytime = current_daytime_data;
    daily_data(day).nighttime = current_nighttime_data;
    
    % Update start timesteps for the next day
    daytime_start = daytime_start + 24;
    nighttime_start = nighttime_start + 24;
end