how to make monthly graph from daily file data

5 views (last 30 days)
thank to per isakson
from this code (daily/1 file) can u make monthly graph (30file data)?? =====
function RainData = ReadManySoniData( folder_name, file_spec )
sad = dir( fullfile( folder_name, file_spec ) );
RainData = struct([]);
for sa = transpose( sad )
RainData = cat( 2, RainData, ReadOneSoniData( folder_name, sa.name ) );
end
[ dummy, ixs ] = sort( [ RainData(:).DayNumber ] );
RainData = RainData( ixs );
end
function rain_data = ReadOneSoniData( folder_name, file_name )
fid = fopen( fullfile( folder_name, file_name ), 'r' );
if not( fid >= 3 )
error( 'ReadOneSoniData:NoFileFound' ...
, 'Cannot find file "%s"' ...
, fullfile( folder_name, file_name ) )
end
cac = textscan( fid, '%s', 'Whitespace','', 'Delimiter','\n' );
fclose( fid );
cac = cac{:};
tmp = cellfun( 'isempty' ...
, regexp( cac, '\d{2}:\d{2}:\d{2}\s+C\s*$' ) );
isc = not( tmp );
tmp = cellfun( 'isempty' ...
, regexp( cac, '\d{2}:\d{2}:\d{2}\s+\*\*\s+----' ) );
iss = not( tmp );
cac( isc | iss ) = [];
str = transpose( char( cac ) );
nl = sprintf('\n');
str = cat( 1, str, repmat( nl(:), [length(nl),size(str,2)] ) );
cac = cell(1,9);
[cac{:}] = strread( str, '%8c%2c%4f%7f%4c%4u%4u%4u+%2u' ...
, 'delimiter', ' ', 'whitespace', '' );
try
date_vec = nan(1,3);
date_vec( [2,3,1] ) = sscanf( file_name, '%2u-%2u-%4u%*s' );
catch me
if strcmp( me.identifier, 'MATLAB:index_assign_element_count_mismatch' )
warning( 'ReadOneSoniData:CannotParseFileName' ...
, 'Cannot extract a date from file name: "%s"' ...
, file_name )
rain_data = struct([]);
return
else
rethrow( me )
end
end
str = transpose( char( cac{1} ) );
vec = nan( size(str,2), 3 );
[ vec(:,1), vec(:,2), vec(:,3) ] ...
= strread( str, '%2u:%2u:%2u', 'delimiter','','whitespace','' );
rain_data.Created = datestr( now, 'yyyy-mm-dd HH:MM:SS' );
rain_data.DataFile = fullfile( folder_name, file_name );
rain_data.Datevec = [ repmat( date_vec, [size(vec,1),1] ), vec ];
rain_data.DayNumber = datenum( date_vec );
rain_data.Rain = cac{3};
rain_data.DailyRain = sum( rain_data.Rain );
% and more as you see fit.
end
  2 Comments
Soni huu
Soni huu on 6 Jul 2012
Edited: Soni huu on 6 Jul 2012
this is sample yearly data (362 file data) (2.11m)
per isakson
per isakson on 6 Jul 2012
"from this code (daily/1 file) can u make monthly graph (30file data)??"
Put more effort in describing what you need!

Sign in to comment.

Accepted Answer

per isakson
per isakson on 6 Jul 2012
Edited: per isakson on 6 Jul 2012
Here is a function that returns total monthly rain. Try
>> mr = MonthlyRain( RainData );
>> plot( mr(1).Rain, 'd' );
>> bar( mr.Rain );
The values of the monthly rain could they be correct?
function monthly_rain = MonthlyRain( RainData )
day_number = [ RainData(:).DayNumber ];
month_number= month( day_number );
year_number = year( day_number );
year_list = unique( year_number );
monthly_rain = struct( 'Year', num2cell( year_list ), 'Rain', nan(12,1) );
ix_yy = 0;
for yy = year_list
is_yy = ( yy == year_number );
ix_yy = ix_yy + 1;
for mm = 1 : 12
is_mm = ( mm == month_number );
is_ym = ( is_yy & is_mm );
if any( is_ym )
monthly_rain(ix_yy).Rain(mm) = sum([RainData( is_ym ).DailyRain]);
end
end
end
end
  63 Comments
per isakson
per isakson on 13 Jul 2012
Do you have problem reading the file, '12-19-2010.dat'?
I have successfully red the 2011 data, which I downloaded some days ago. However, I will not download more data. I've run out of time.
Add these lines
fid = fopen( 'c:\temp\ReadRainDataFailures.log', 'a' );
if fid >= 3
fprintf(fid, '%s | %s | %s\n',datestr(now),folder_name, sa.name );
fclose( fid );
end
after
% disp( le.stack(1) )
That will give you a list of the files, which cannot be red. You might want to change the name and folder of the file. Do you have a "c:\temp"?
If you run into specific problems try make a question at Answers.
Soni huu
Soni huu on 13 Jul 2012
the code work now......... yes i have c:\temp

Sign in to comment.

More Answers (1)

per isakson
per isakson on 6 Jul 2012
Here is a function that collects total daily rain for one month at a time. Try
>> [ day_number, daily_rain ] = DailyRain( RainData, 2011, 11 );
>> plot( day_number, daily_rain )
>> figure, plot( day_number, daily_rain, '.' )
>> figure, plot( day_number, '.' )
As is and all that! You must check the the values. Missing data might cause surprises.
function [ day_number, daily_rain ] = DailyRain( RainData, year_number, month_number )
day_number = [ RainData(:).DayNumber ];
is_yy = ( year( day_number ) == year_number );
is_mm = ( month( day_number ) == month_number );
is_ym = ( is_yy & is_mm );
if any( is_ym )
daily_rain = [ RainData( is_ym ).DailyRain ];
day_number = [ RainData( is_ym ).DayNumber ];
else
daily_rain = [ ];
day_number = [ ];
end
end
  32 Comments
Soni huu
Soni huu on 24 Aug 2012
thanks per isakson... u save me... :)
per isakson
per isakson on 24 Aug 2012
Soni, good to hear that you succeeded to read the data

Sign in to comment.

Categories

Find more on Vehicle Scenarios in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!