You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Creating arrays based on NetCDF files
6 views (last 30 days)
Show older comments
I'm working on a code that creates an array type (nlines, ncolumns, nfiles) with nfiles equal to the number of grids that it has ( k = 1:nfiles), after that I need to calculate the estatistics for each grid, for each value of K, and the statistics for each pixel. Then I need to calculate the average to each pixel e then create a grid to represent the averages of the values.
21 Comments
Star Strider
on 13 Aug 2023
My impression is that OP is informing us of his objective, and will let us know how it turns out!
Vasco
on 14 Aug 2023
English is not my first language so I'm having trouble putting into words what I want.
I made an array based on longitude, latitude and time if I post it can you give some pointers or indicate me on whether the array was created correctly or not?
dpb
on 14 Aug 2023
We can try, certainly. But, we need as precise a description of the result you're looking for and what you have to start with. If that is a NetCDF file, then we need you to attach it (preferably it is a small(ish) example that illustrates, not a huge one).
Then, probably the easiest way to combat the language barrier is to actually show us from the input what it is that you're trying to create as output, and more importantly, the "HOW/WHY" of why the result is what it is.
Vasco
on 14 Aug 2023
I´ll try to sumarise what I need
I'm doing a study on the GRACE mission from NASA and I'm studying the water thickness on a global scale, basically Im researching the changes in water mass on land of the entire time period for the mentioned mission (from 2002-2017) and I was tasked with doing an array or by my coordinator words a "cube" which involves the lon, lat, time encompassing the the average for lwe(water thickness) for each pixel throughout the entire duration of the mission.
dpb
on 14 Aug 2023
Edited: dpb
on 14 Aug 2023
load pixel_media.mat
whos
Name Size Bytes Class Attributes
ans 1x79 158 char
cmdout 1x33 66 char
pixel_media 1x360x180 518400 double
pixel_media=squeeze(pixel_media);
imagesc(pixel_media)

figure
imagesc(pixel_media.')

Well, that's not what you're wanting, either, it would seem! :)
figure
imagesc(pixel_media.')
hAx=gca; hAx.YDir='normal';

Something like that, maybe?
To compute statistics, presuming all the files are over the same coordinates and the same resolution, you'd just create the 3D array of each observation a plane (how many of them are there, you may run into memory issues and need to see about memory saving or other techniques than holding everything in memory at one time); then to compute statistics simply use the optional 'dim' argument for the various statistical functions like mean to operate over the 3rd dimension. "Piece o' cake!" :)
BTW, your English comprehension and grammar seem just fine to me -- far better than my abilities in your native language would be, for sure!
dpb
on 15 Aug 2023
Edited: dpb
on 15 Aug 2023
NOTA BENE: In MATLAB images go from top LH corner down so to flip the display vertically, set the y-axis direction from bottom up as for an "ordinary" graph instead.
For real, you'll undoubtedly want to use the imagesc(x,y,C) form where x,y are the latitude/longitude associated with the image in order to display on actual coordinates instead of against pixel number.
Vasco
on 15 Aug 2023
Edited: dpb
on 15 Aug 2023
figure(2);
x_valor = 1:size(pixel_media, 2);
y_valor = 1:size(pixel_media, 3);
clevv=-0.5:0.1:0.2;
v = imagesc(x_valor, y_valor, squeeze(pixel_media(1, :, :)));
hAx=gca;
hAx.YDir='normal';
clim([min(clevv), max(clevv)]);
colorbar('eastoutside');
colormap(winter(length(clevv)-1));
figura_1 = fullfile(output, strcat(nome_final, '.png'));
saveas(gca, figura_1);
tried this code but the image still doesnt rotate
and also I want values that are not representated in the clevv to have another color
more especificaly the values for the sea water and NaN
dpb
on 15 Aug 2023
Edited: dpb
on 15 Aug 2023
_"... the coordinates are correct, ..."
You sure about that?
load pixel_media.mat
pixel_media=squeeze(pixel_media).'; % go ahead and transpose the data
imagesc(pixel_media)
hAx=gca; hAx.YDir='normal';

[xlim ylim]
ans = 1×4
0.5000 360.5000 0.5000 180.5000
You observe that imagesc scales the image to a midpoint between each pixel, not the actual latitude/longitude of the data points in the array. That the array size happens to be 180 x 360 is just fortuitous matchup to the numbers you expected although the latitude not being centered is unusual representation I'd think in any presentation.
Try
pixel_media=flipud(pixel_media); % flip the latitude direction to match MATLAB image
lat=linspace(-180,180,size(pixel_media,1));
lon=linspace(0,360,size(pixel_media,2));
imagesc(lon,lat,pixel_media)
xlim([lon(1) lon(end)]), ylim([lat(1) lat(end)])
xticks(linspace(lon(1),lon(end),9)), yticks(linspace(lat(1),lat(end),7))

dpb
on 15 Aug 2023
Yeah, my foohpah on the latitude range; was just typing, not thinking.
I dunno where you want the origin to be for longitude; is the present 0 on Greenwich, maybe? Looks like it could be chosen that way is why I left it at 0.
I hadn't seen the plaint about colormap until now; there's where I'm at the limits of my image processing; I've never fully figured out how to neatly manipulate them to do special things; the default with imagesc scales the data over the range linearly.
I think to change the sea color you need to change its value if stay with indexed colors. Limiting the colormap to a range just causes points outside those to be max'ed out at one or the other end.
Vasco
on 15 Aug 2023
i got the image with the ocean color being null basically the only problem that im having is merging the code you made with mine so that the xticks works ill send the my base code
I managed to found by chance something thats transforms the values to NaN values but to be honest I still dont understand how it fully works.
Answers (0)
See Also
Categories
Find more on Orange in Help Center and File Exchange
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)