NC file reading and batch conversion to TIFF - the most detailed explanation in history - including code (ArcGIS/MATLAB)

What is an NC file, how to read it, and how to convert it to TIFF in batches (ArcGIS/MATLAB)


foreword

I believe that many students in remote sensing, geoinformation, and geography often use global monthly average precipitation data/temperature and other data, and this type of data is often saved in NC files. After getting it, everyone is often confused. What kind of data format is this? , how to read it, and how to convert it into the familiar raster data. Come to answer your questions today.

1. Introduction of NC files

The full name of NetCDF is network Common Data Format, and the Chinese translation is "network common data format"; the netcdf file was originally used to store data in meteorological science, and now it has become the format of files generated by many data acquisition software.

The structure of a Netcdf file includes the following objects:
● Variables: Variables correspond to real physical data;
● Dimensions: A dimension corresponds to an independent variable in a function, or a coordinate in a function image Axis;
● Attribute: Attribute is to comment on the specific physical meaning of the variable value and dimension.

Mathematically, the data stored by netcdf is a single-valued function with multiple arguments. In terms of the formula, it is f(x,y,z,…)=value;
The independent variables x, y, z are called dimensions (dimension) or coordinate axes (axix) in netcdf;
The value is called variable in netcdf ( Variables).

Second, view the NC structure

Use MATLAB to view
●Use the function ncdisp
to use:
ncFilePath = ['E:\Personal Document 1\Project\Data\2000-2020_tmp\tmp_2000_2002.nc']; %Set the NC path
ncdisp(ncFilePath);
●The reading result
is as follows It is the structure of the NC file, including type, dimension, and variables; among them, the variables include longitude, latitude, time, and pre (this is the variable we need); we can understand it visually as: latitude and longitude are its plane
coordinates , time is its dimension, forming a three-dimensional empty matrix, and then filling pre into the matrix respectively, forming such a data structure.
●Note: For this step, the straightforward point is to look at its data structure and the introduction of the file, and secondly, to determine the names of dimensions and variables, which will be used later.
Here, for example, the name of longitude is lon, the name of latitude is lat, the time is time, and the variable is tmp.

insert image description here

3. Read NC

Here are two softwares to explain. arcgis has the function of reading NC files, but it can only read a single file. If the number is large enough, it will undoubtedly consume a lot of time. Here we use MATLAB to read in batches and convert them to TIFF.

3.1 ArcGIS reading

1. Open ArcGIS, click Toolbox, find Multidimensional Tools, and click Create NetCDF Raster Layer below.

insert image description here

2. The following interface pops up, enter the netCDF file, and then the following variables will be automatically loaded. You can choose time as the dimension value, and then fill in the corresponding date in the value behind time in the table below, and then you can load the NC you want to read. If the data of a time dimension is not processed, the first value of the dimension is loaded by default. Then click OK.
Note: Note that the path cannot contain Chinese, otherwise the following variables cannot be automatically loaded, and an error will be reported.

insert image description here

  1. The result is as follows, that is, the reading is successful, and the raster layer can be saved.
    Note: But this method can only export raster data at one point in time. NC is generally multi-dimensional time, so how to export it in batches? Then look below.

insert image description here

3.2 Matlab read

  1. First read the data of any set of dimensions to see, use the code. set your own path
clc
clear
ncFilePath = ['E:\个人文档1\项目\数据\2000-2020_tmp\tmp_2000_2002.nc']; %设定NC路径
ncdisp(ncFilePath); %查看NC结构
data1=ncread(ncFilePath,'tmp'); 读取NC文件中的变量,注意要设置正确的变量名,该数据集变量名为tmp,怎么查看,参考上文开始的讲解
data3=data1(:,:,1); %读取该数据集维度中的第1个时间序列数据
  1. The results are as follows: It can be found that data1 shows 7680 4717 36, the first two are the latitude and longitude we are talking about, and the last 36 is the time series (this NC contains 3 years of data, 3*December = 36 months), data3 is to read The tmp data of January 2000.

insert image description here

  1. display data3. Select data3, click the drawing tool, and select imagesc to display the data. It is a bit strange when you look at it, but it is in the shape of a chicken (China), but it is displayed in the wrong direction. It should be rotated 90° counterclockwise first, then mirrored (flipped).

insert image description here

  1. We add the following codes, one is counterclockwise and the other is flipping. It is found that the terrain of China is normal.
 data4=rot90(data3); %逆时针旋转90°
 data5=flipud(data4); %矩阵的翻转
 imagesc(data5);

insert image description here

4. Read NC in batches and save them in TIFF format

  1. Saving data in TIFF format in MATLAB involves the problem of geographic coordinate system. There are two methods: one is to use the georasterref function in Matlab to create the geographic coordinate system of tif, and the other is to read a ready-made tiff file as a reference geographic coordinate system. Since the first method may be biased or wrong, in order to ensure the accuracy of the data, we use the second method.
  1. Same as the Arcgis reading operation in the third section, first read a certain period of data in NC. Then define the projection, and the projection operation is as follows.

insert image description here

  1. Check the properties. If there is a coordinate system in the spatial reference, the definition is successful. If there is still no coordinate system after the definition, go to the next step.

insert image description here

  1. Select the layer as shown in the figure, right-click and select Properties, and set the coordinate system of the layer.

insert image description here

  1. Click Export Data. Change the raster dataset (original) to data frame (current), you can find that there is a coordinate system in the spatial reference, set the save path, and click save.

insert image description here
insert image description here

  1. After saving successfully, the result is as follows.

insert image description here

4.2 Batch save as TIFF format

1. Don't talk nonsense, go directly to the code. Copy the following code and use it directly. The places that need to be adjusted have been marked in the text ( need to be adjusted ), just change the file path, and there is a place to loop, and adjust it according to your own needs.

clc
clear
%%  导入NC数据集
ncFilePath = ['E:\个人文档1\项目数据\2000-2020_pre\2000-2020\pre_2020.nc']; %设定NC路径**需要调整**
ncdisp(ncFilePath); %查看NC数据结构
%% 导入TIFF影像的坐标信息
[A,R]=geotiffread('E:\li\pre_2018.tif');%该处路径为上述Arcgis中导出带坐标系的TIFF文件**需要调整**
info=geotiffinfo('E:\li\pre_2018.tif'); %该处路径为上述Arcgis中导出带坐标系的TIFF文件 **需要调整**
%% 读取pre数据
data=ncread(ncFilePath,'pre');  
%% 批量处理
for year=2000:2020  %年循环**需要调整**
        data1=data(:,:,1+12*(year-2000):12*(year-1999)); %得到每年的12个月数据。注意,第一个减去的时间与循环的起始时间相同,第二个减去的时间在起始时间的基础上减1。如该处的20001999**需要调整**
        data3=sum(data1,3)/12; %表示沿X轴的第3维求和,求年平均数据
        data4=rot90(data3); %使矩阵逆时针旋转90°
        data5=flipud(data4); %矩阵的翻转
        filename=strcat('E:\个人文档1\项目\数据\NC_TO_TIFF\pre\year\中国',int2str(year),'年pre.tif'); %**需要调整**
        geotiffwrite(filename,data5,R,'GeoKeyDirectoryTag',info.GeoTIFFTags.GeoKeyDirectoryTag);
    for mon=1:12  %月循环
        data2=data1(:,:,mon);  %读取月数据
        data4=rot90(data2);%使矩阵逆时针旋转90°
        data5=flipud(data4); %矩阵的翻转
        filename=strcat('E:\个人文档1\项目数据\NC_TO_TIFF\pre\month\中国',int2str(year),'_',int2str(mon),'月pre.tif'); **需要调整**
        geotiffwrite(filename,data5,R,'GeoKeyDirectoryTag',info.GeoTIFFTags.GeoKeyDirectoryTag);
    end
end

2. The export is successful.

insert image description here

4.3 Export result verification

Select the results in January 2000, open it in ARCGIS, and find that the value is between -32768-2339; call up the original NC data of this period in MATLAB, extract the maximum and minimum values, and find that it is 99% close to the data. As for There is a slight single-digit error, which is due to the pyramid resampling of the TIFF image imported into ARcgis. It shows that the exported result is no problem.

insert image description here

insert image description here

Ending

Well, the above is the content of this explanation. I hope it can help you. If you have any questions, please leave a message below for discussion.

Guess you like

Origin blog.csdn.net/xiatiandexia123/article/details/127599217