% % This example code illustrates how to access and visualize SMAP L2 file % in MATLAB. % % If you have any questions, suggestions, comments on this example, please % use the HDF-EOS Forum (http://hdfeos.org/forums). % % If you would like to see an example of any other NASA HDF/HDF-EOS data % product that is not listed in the HDF-EOS Comprehensive Examples page % (http://hdfeos.org/zoo), feel free to contact us at eoshelp@hdfgroup.org % or post it at the HDF-EOS Forum (http://hdfeos.org/forums). % % Usage:save this script and run (without .m at the end) % % % $matlab -nosplash -nodesktop -r SMAP_L2_SM_SP_1AIWDV_20200209T061215_20200208T183607_005W11N_h5 % % Tested under: MATLAB R2020a % Last updated: 2020-09-18 % Open the HDF5 File. FILE_NAME = 'SMAP_L2_SM_SP_1AIWDV_20200209T061215_20200208T183607_005W11N_R16515_001.h5'; file_id = H5F.open (FILE_NAME, 'H5F_ACC_RDONLY', 'H5P_DEFAULT'); % Open the dataset. DATAFIELD_NAME = 'Soil_Moisture_Retrieval_Data_1km/soil_moisture_1km'; data_id = H5D.open (file_id, DATAFIELD_NAME); Lat_NAME='Soil_Moisture_Retrieval_Data_1km/latitude_1km'; lat_id=H5D.open(file_id, Lat_NAME); Lon_NAME='Soil_Moisture_Retrieval_Data_1km/longitude_1km'; lon_id=H5D.open(file_id, Lon_NAME); % Read the dataset. data=H5D.read (data_id,'H5T_NATIVE_DOUBLE', 'H5S_ALL', 'H5S_ALL', 'H5P_DEFAULT'); lat=H5D.read(lat_id,'H5T_NATIVE_DOUBLE', 'H5S_ALL', 'H5S_ALL', 'H5P_DEFAULT'); lon=H5D.read(lon_id,'H5T_NATIVE_DOUBLE', 'H5S_ALL', 'H5S_ALL', 'H5P_DEFAULT'); % Read the fill value. ATTRIBUTE = '_FillValue'; attr_id = H5A.open_name (data_id, ATTRIBUTE); fillvalue=H5A.read (attr_id, 'H5T_NATIVE_DOUBLE'); % Read the units. ATTRIBUTE = 'units'; attr_id = H5A.open_name (data_id, ATTRIBUTE); units = H5A.read(attr_id, 'H5ML_DEFAULT'); % Read the valid_max. ATTRIBUTE = 'valid_max'; attr_id = H5A.open_name (data_id, ATTRIBUTE); valid_max = H5A.read(attr_id, 'H5ML_DEFAULT'); % Read the valid_min. ATTRIBUTE = 'valid_min'; attr_id = H5A.open_name (data_id, ATTRIBUTE); valid_min = H5A.read(attr_id, 'H5ML_DEFAULT'); % Read title attribute. ATTRIBUTE = 'long_name'; attr_id = H5A.open_name (data_id, ATTRIBUTE); long_name=H5A.read (attr_id, 'H5ML_DEFAULT'); % Close and release resources. H5A.close (attr_id) H5D.close (data_id); H5F.close (file_id); % Replace the fill value with NaN. data(data==double(fillvalue)) = NaN; % Replace the invalid range values with NaN. data(data < double(valid_min)) = NaN; data(data > double(valid_max)) = NaN; % Set map boundary limits. latlim=[floor(min(min(lat)))-1, ceil(max(max(lat)))+1]; lonlim=[floor(min(min(lon)))-1, ceil(max(max(lon)))+1]; f=figure('Name', FILE_NAME, 'visible', 'off'); % Create the plot. axesm('MapProjection','eqdcylin',... 'Frame','on','Grid','on', ... 'MapLatLimit',latlim,'MapLonLimit',lonlim, ... 'MLabelLocation', 1, 'PLabelLocation', 1, ... 'MeridianLabel','on','ParallelLabel','on','MLabelParallel', ... 'south') % Plot the data. cm = colormap('Jet'); min_data=min(min(data)); max_data=max(max(data)); caxis([min_data max_data]); k = size(data); [count, n] = size(cm); datap=data(~isnan(data)); latp=lat(~isnan(data)); lonp=lon(~isnan(data)); scatterm(latp, lonp, 1, datap); coast = load('coast.mat'); plotm(coast.lat,coast.long,'k'); tightmap; h = colorbar(); % An HDF5 string attribute is an array of characters. % Without the following conversion, the characters in unit will appear % in a vertical direction. units1 = sprintf('%s', char(units)); % units is pretty long so use a small font. set (get(h, 'title'), 'string', units1, 'FontSize', 4, ... 'Interpreter', 'None', ... 'FontWeight','bold'); name = sprintf('%s', long_name); % long_name is also long so we use a small font. title({FILE_NAME; name}, ... 'Interpreter', 'None', 'FontSize', 7,'FontWeight','bold'); saveas(f, [FILE_NAME '.m.png']); exit;