Writing lat and lon attributes in a NetCDF file

17 views (last 30 days)
Hello!
I'm here to ask help for a matlab scrip. I have a matrix of data that covers a part of europe (it's a tile of a MODIS product) and I want to write it in a netcdf file in order to represent the map with QGIS. I write the information of latitude and longitude with the function ncwriteatt with the following script. However, when I use the command ncinfo the attributes are empty and when I represent it of QGIS the file is not georeferenced.
Can anyone help me? Thanks in advance,
Margherita
xmin=0.003241+(0.004464/2);
xmax=xmin+2399*0.004464;
ymin=40.002083+(0.004464/2);
ymax=ymin+2399*0.004464;
cx=[xmin:0.004464:xmax]';
cy=flip([ymin:0.004464:ymax]');
coord_x=zeros(length(cy(:,1)),length(cx(:,1)));
coord_y=zeros(length(cy(:,1)),length(cx(:,1)));
for i=1:length(cy)
coord_y(i,:)=cy(i);
end
for i=1:length(cx)
coord_x(:,i)=cx(i);
end
dim_lon=length(cx);
dim_lat=length(cy);
nccreate('MYD16A2_2021.nc','ET_500m', 'Dimensions',{'lon',dim_lon,'lat',dim_lat}, 'FillValue','disable');
nccreate('MYD16A2_2021.nc','lon','Dimensions', {'lon',dim_lon})
nccreate('MYD16A2_2021.nc','lat','Dimensions', {'lat',dim_lat})
ncwrite('MYD16A2_2021.nc','ET_500m', averageData)
ncwrite('MYD16A2_2021.nc','lat',cy)
ncwrite('MYD16A2_2021.nc','lon',cx)
ncwriteatt('MYD16A2_2021.nc', 'lon', 'standard_name', 'longitude');
ncwriteatt('MYD16A2_2021.nc', 'lon', 'units', 'degrees_east');
ncwriteatt('MYD16A2_2021.nc', 'lon', 'axis', 'X');
ncwriteatt('MYD16A2_2021.nc', 'lat', 'standard_name', 'latitude');
ncwriteatt('MYD16A2_2021.nc', 'lat', 'units', 'degrees_north');
ncwriteatt('MYD16A2_2021.nc', 'lat', 'axis', 'Y');
ncwriteatt('MYD16A2_2021.nc', 'ET_500m', 'coordinate_system', 'WGS84')
ncwriteatt('MYD16A2_2021.nc', 'ET_500m','long_name','Evapotranspiration from MODIS data')
ncwriteatt('MYD16A2_2021.nc', 'ET_500m','units','mm/8days')
ncwriteatt('MYD16A2_2021.nc', 'ET_500m','missing_value',-9999)

Answers (1)

Balaji
Balaji on 12 Sep 2023
Hi Margherita
I understand you are facing issue in the netcdf file you have created.
Based on the provided script, it seems like you are correctly creating the netcdf file and writing the data and attributes. However, there are a few modifications you can make to ensure proper georeferencing and attribute assignment. Here are the suggested changes:
Specify the coordinate variables lon and lat as 1D arrays instead of 2D arrays. This will make it easier to assign the coordinate values using ncwrite.
nccreate('MYD16A2_2021.nc','lon','Dimensions', {'lon',dim_lon})
nccreate('MYD16A2_2021.nc','lat','Dimensions', {'lat',dim_lat})
ncwrite('MYD16A2_2021.nc','lon',cx)
ncwrite('MYD16A2_2021.nc','lat',cy)
ncwriteatt('MYD16A2_2021.nc', 'lon', 'standard_name', 'longitude');
ncwriteatt('MYD16A2_2021.nc', 'lon', 'units', 'degrees_east');
ncwriteatt('MYD16A2_2021.nc', 'lon', 'axis', 'X');
ncwriteatt('MYD16A2_2021.nc', 'lat', 'standard_name', 'latitude');
ncwriteatt('MYD16A2_2021.nc', 'lat', 'units', 'degrees_north');
ncwriteatt('MYD16A2_2021.nc', 'lat', 'axis', 'Y');
averageData = ... % Define or load the data matrix here
ncwrite('MYD16A2_2021.nc','ET_500m', averageData)
Hope this helps!
Thanks
Balaji

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!