Converting NC to TIF Using Matlab for Remote Sensing Data

In remote sensing and GIS data processing, the NetCDF (.nc) format is a commonly used data format in fields such as meteorology, hydrology, and ecology. It efficiently stores multidimensional raster data (such as time series and latitude-longitude grids), but many GIS software (like ArcGIS and QGIS) have better compatibility with the TIFF format. Today, I will guide you step-by-step on how to convert NC to TIF using Matlab, easily breaking down the data format barriers!

1. Why Convert NC Format to TIF?

The NC format excels at storing multidimensional data, but for visualization and spatial analysis, the TIFF format is more compatible with mainstream GIS tools. For example, NC files like PET (Potential Evapotranspiration) and meteorological reanalysis data can be directly overlaid with terrain and administrative boundary layers for spatial analysis after conversion to TIF.

2. Complete Process of NC to TIF Conversion in Matlab

Below, we will use PET data (pet_2020.nc) as an example, providing reusable code along with detailed explanations:

% ---------------------- 1. Set File Paths ----------------------input_nc_file = 'E:\Matlab\nc转tif\pet_2020.nc'; % Your NC file pathoutput_tif_file = 'E:\Matlab\nc转tif\pet_2020.tif'; % Output TIF path% ---------------------- 2. Read NC File Information ----------------------info = ncinfo(input_nc_file);disp('Variable information in the file:');disp({info.Variables.Name}); % Print all variable names (e.g., etp, lat, lon)% ---------------------- 3. Read Core Data and Geographic Information ----------------------variable_name = 'etp'; % Replace with your NC file's core variable name (critical!)data = ncread(input_nc_file, variable_name); % Read target variable datalatitude = ncread(input_nc_file, 'lat'); % Read latitudelongitude = ncread(input_nc_file, 'lon'); % Read longitude% ---------------------- 4. Create Geographic Reference Object ----------------------R = georasterref('RasterSize', size(data), ...                 'LatitudeLimits', [min(latitude), max(latitude)], ...                 'LongitudeLimits', [min(longitude), max(longitude)]);% ---------------------- 5. Export as GeoTIFF ----------------------geotiffwrite(output_tif_file, data, R);disp(['Conversion completed! File saved to: ' output_tif_file]);

3. Key Steps to Note!

  1. Confirm Variable Name: Use <span>ncinfo</span> to check the variable list in the NC file, and make sure to replace <span>variable_name</span> with the actual variable name (for example, some NC files may have variables named <span>pet</span> or <span>precip</span>, do not make mistakes!).

  2. Do Not Omit Geographic Reference: <span>georasterref</span> is crucial for ensuring the TIF has coordinates — if latitude and longitude information is missing, the resulting TIF will be “coordinate-less raw data” and cannot be located in GIS.

  3. Avoid Path Pitfalls: Ensure that the path does not contain Chinese characters or special symbols (for example, <span>E:\NC转TIF\数据.nc</span> may cause errors), it is recommended to use English paths.

4. Practical Tips

  • If the NC data is multidimensional (for example, containing a time dimension): first use <span>size(data)</span> to check the dimensions (for example, <span>[lon, lat, time]</span>), take a single time slice (like <span>data(:,:,1)</span>) before exporting.
  • If the data is flipped: use <span>flipud(data)</span> to adjust the latitude direction to avoid the TIF displaying “upside down”.
  • Verify results: Open the TIF in ArcGIS/QGIS to check if the coordinate range and values match those in the NC file.

Leave a Comment