HDF读写
HDF5
hdf5info()
hdf5read()
HDF4 和 HDF EOS(地理数据)
hdfinfo()
hdfread()import matlab.io.hdfeos.*
%% TIFF NC HDF数据的正反问题
clc
clear alllat1=zeros(360,1);
for i=1:360lat1(i,1)=90+90.0/360.0-(180.0/360.0)*i;
end% 数组为正,从上到下为90:-90,从左至右为-180:180
data = repmat(lat1,1,720);
data(:,1) = -180;
data(:,end) = 180;%% TIFF
% write 保证数组为正,lat*lon
R=georasterref('RasterSize', [360,720], 'Latlim', [-90, 90],...'Lonlim', [-180, 180], 'ColumnsStartFrom', 'north');
outpath = 'D:\BaiduNetdiskDownload\test.tif';
geotiffwrite(outpath,data,R)% read
data_read = geotiffread(outpath);% re_write
outpath = 'D:\BaiduNetdiskDownload\test_rewrite.tif';
geotiffwrite(outpath,data,R)%% HDF EOS % 数组是正的,保存的时候需要转置,hdfread读出来是正的。
import matlab.io.hdfeos.*
output=['D:\BaiduNetdiskDownload\test','.hdf'];
data1 = data'; % 720*360
gfid = gd.open(output,'create');
uplft = [-180000000 90000000];%修改左上,全球范围是固定值
lowrgt = [180000000 -90000000];%修改右下
gridID = gd.create(gfid,'GPP_grid',720,360,uplft,lowrgt);
%投影信息
gd.defProj(gridID,'geo',[],[],[]);%defProj(gridID,projCode,zoneCode,sphereCode,projParm)
gd.defDim(gridID,'XDim',720);
gd.defDim(gridID,'YDim',360);
dimlist = {
'XDim','YDim'};
gd.defComp(gridID,'deflate',4);%gd.defComp(gridID,'rle');%压缩不好
gd.defField(gridID,'GPP_005D',dimlist,'double');%
gd.writeField(gridID,'GPP_005D',data1);
gd.detach(gridID);
gd.close(gfid);% read
data_hdf = hdfread(output,'GPP_005D'); % 360*720% re_write
output=['D:\BaiduNetdiskDownload\test_rewrite','.hdf'];
gfid = gd.open(output,'create');
uplft = [-180000000 90000000];%修改左上,全球范围是固定值
lowrgt = [180000000 -90000000];%修改右下
gridID = gd.create(gfid,'GPP_grid',720,360,uplft,lowrgt);
%投影信息
gd.defProj(gridID,'geo',[],[],[]);%defProj(gridID,projCode,zoneCode,sphereCode,projParm)
gd.defDim(gridID,'XDim',720);
gd.defDim(gridID,'YDim',360);
dimlist = {
'XDim','YDim'};
gd.defComp(gridID,'deflate',4);%gd.defComp(gridID,'rle');%压缩不好
gd.defField(gridID,'GPP_005D',dimlist,'double');%
gd.writeField(gridID,'GPP_005D',data_hdf');
gd.detach(gridID);
gd.close(gfid);%% NC 暂时放一边
ncid =netcdf.create('D:\BaiduNetdiskDownload\test_2.nc','NETCDF4')% Define dimension
dimidx = netcdf.defDim(ncid,'lat',360);
dimidy = netcdf.defDim(ncid,'lon',720);% Define variables
latid = netcdf.defVar(ncid,'lat','nc_double',dimidx);
lonid = netcdf.defVar(ncid,'lon','nc_double',dimidy);
varid = netcdf.defVar(ncid,'ET','nc_double',[dimidx dimidy]);% Define attributes of the new variables
netcdf.putAtt(ncid,lonid,'units','degrees_east');%单位信息和long_name,其它的信息可依此定义
netcdf.putAtt(ncid,lonid,'long_name','longitude');netcdf.putAtt(ncid,latid,'units','degrees_north');
netcdf.putAtt(ncid,latid,'long_name','latitude');netcdf.putAtt(ncid,varid,'units','g C m-2');
netcdf.putAtt(ncid,varid,'long_name','Total Soil Respiration');
netcdf.defVarFill(ncid,varid,false,-9999);netcdf.endDef(ncid);%--------------------------给新变量赋值-------------------------------------------%
lonv = zeros(720,1);
latv = zeros(360,1);for i = 1: 720lonv(i,1) = -179.75 + (i - 1) * 0.5;
end
netcdf.putVar(ncid,lonid,lonv); for j = 1: 360latv(j,1) = -(-89.75 + (j - 1) * 0.5);
end
netcdf.putVar(ncid,latid,latv); netcdf.putVar(ncid,varid,flip(data));netcdf.close(ncid);data1 = rot90(data);
data2 = flip(data);