2023.0403
近期在国内数据中心网页上批量下载数据,还在摸索批量快速下载的方法;
想起来之前批量下载NASA数据,补充一下wget批量下载merra-2数据的方法;
博主讲得很详细:
NASA气候气象资料Merra2数据下载教程_哔哩哔哩_bilibili
(cmd里操作)
cd E:\north china\datapre\AAOD\2005-2008\nc05-08\(自己存放文件的路径)
NUL >.urs_cookies
(lujing)dir
wget --load-cookies E:\north china\datapre\AAOD\2005-2008\nc05-08\.urs_cookies --save-cookies E:\north china\datapre\AAOD\2005-2008\nc05-08\.urs_cookies --auth-no-challenge=on --keep-session-cookies --user=chenxilu --ask-password --content-disposition -i subset_OMAEROe_003_20220518_081006(自己输.txt)
(再输密码)
2022.0106
了解merra2气溶胶数据集;了解了冬小麦、夏玉米物候信息(ps:中国农业观测站数据没找到)
2022.0107
用arcgis求了一年12个月的AOD均值,进行了可视化;批量nc格式转tif,求均值,剪裁,转点,值转至点导出了excel表;
还在探索中,如何批量处理nc数据,matlab方法没尝试出来;明天试arcpy;
如何批量处理数据后转为excel表,还在探索中~~~
2022.0108-matlab批量nc转tif
在matlab里打开了nc数据,变量数据是二维数据,
想按自己要求导出为excel表格,比如
没有做到,唉~~~~~
matlab批量打开NC文件_批量转tif_批量nc转tif,matlab批量读取nc文件-其它代码类资源-CSDN下载
InPath = 'E:\MERRA2_R\1989.10-1994.09\nc\;
OutPath = 'E:\MERRA2_R\1989.10-1994.09\tif\';
filelist=dir([InPath,'*.nc']);
a=filelist(1).name;
b=filelist(2).name;
k=length(filelist);
for i=1:k
InFile = strcat(InPath,filelist(i).name)
ncdisp(InFile)
filename=[InPath,filelist(i).name]
RAD=ncread(filename,'SWGDN')
rad=rot90(RAD,1)
lon=ncread(InFile,'lon')
lat=ncread(InFile,'lat')
GeoRef = georasterref('Rastersize',size(rad),'Lonlim',[double(min(lon)),double(max(lon))],'Latlim',[double(min(lat)),double(max(lat))])
rad_Tif = strcat(OutPath,filelist(i).name,'.tif')
geotiffwrite(rad_Tif,flip(rad),GeoRef)
end
代码里有几个函数不明白,可查看官方帮助以及博客
打开 NetCDF 数据源 - MATLAB netcdf.open- MathWorks 中国
matlab 中 strcat函数的用法_md_555的专栏-CSDN博客_matlab strcat
对栅格数据批处理,准备用arcpy
代码没试出来~~~~明天再试
20220109—arcpy批量裁剪栅格数据
利用arcpy批量裁剪栅格数据,参考了其他博主按掩膜裁剪
Arcgis中利用python语言批量裁剪栅格影像数据_arcgis裁剪tif-Python文档类资源-CSDN下载
自己摸索着利用的clip_management裁剪的,代码尝试成功;
想继续批量将裁剪后的栅格数据转为点,代码没有尝试成功~~~
可以print三个dot文件,但是最终只呈现出一个文件里的值,~~~
目前暂定,matlab批量转nc数据为tif数据,然后利用arcpy批量裁剪后进行栅格数据转点再进一步值提取至点,再接着将属性表转为excel,再利用matlab对多个excel进行整合;
今日疑问,按掩膜裁剪后像元位置和原来无法重合,但是在clip里设置环境,按要素裁剪后好像可以重合,还是不太明白两者的区别;
明天也要加油呀,希望搞定批量将栅格转点!!!
20220110——arcpy批量栅格转点
arcpy批量栅格转点成功!!!(上代码)
重点是输出路径里的名称,之前一直没有设置好导致名称输出一样,所以最后的输出会覆盖前面的内容;此处有借鉴以下资料
python批处理系列代码-栅格转点_liudan19940618的博客-CSDN博客_python 栅格转点
Python学习:split()方法以及关于str.split()[0]等形式内容的详细讲解_do_you_ac_today的博客-CSDN博客_split()[0]方法
尝试读取点要素并写入txt文件;参考以下博客:
计算要素数量、将python结果写入到TXT中 - gis木棉鱼 - 博客园
Python(arcpy) 批量读取点元素文件(shp)属性表的字段_天外来客的花田-CSDN博客_python 遍历shp
有点子问题,可以输出TXT文件,会报错;
格式也感觉怪怪的,明天继续研究!加油!!!
20220111
前面的txt文本转换中,文本格式非常乱,希望是数值型的并且保证在一列,不知道怎么可以输出这种形式;
参考下面博客运行了一遍,只是可以print属性值,想试着写入txt文本里
python/arcpy提取shp文件属性表中的字段值_Lianj.Qin-CSDN博客_python读取shp的属性表
20220112
借鉴以下博文以及哔哩哔哩,批量处理成功,只不过数据混在一起的;
python教程之ArcPy从入门到精通_哔哩哔哩_bilibili
Python(arcpy) 批量读取点元素文件(shp)属性表的字段_天外来客的花田-CSDN博客_python 遍历shp
>>> arcpy.env.workspace=r"E:\MERRA2\dot"
>>> import os
>>> pathfile=r"E:\MERRA2\dot"
>>> ls=os.listdir(pathfile)
>>> outfile=open(pathfile+'sw2.txt',"a")
>>> for i in ls:
... if os.path.splitext(i)[1]==".shp":
... shppath=("E:/MERRA2/dot/"+i)
... with arcpy.da.SearchCursor(shppath,["grid_code"]) as cursor:
... for row in cursor:
... g=row[0]
... result=str(g)+"\n"
... outfile.write(result)
... outfile.close()
对于混在一起的数据,在matlab中每隔82行取一列
a=textread('');
a1=reshape(a,[ ])
将matlab数据保存为excel文件_奔流聚海-CSDN博客_matlab导出excel数据
批量将matlab转换的Tif文件利用arcpy裁剪,再进行栅格转点:
arcpy.env.workspace=r'E:\MERRA2_R\2009.10-2014.09\tif'
... rasters=arcpy.ListRasters("*",'tif')
... mask=r'E:\不过尔尔\毕业设计\数据\区划\京津冀县域.shp'
... outpath=r'E:\MERRA2_R\2009.10-2014.09\cj'
... for raster in rasters:
... out=outpath+'\\'+raster.split(".")[2]+".tif"
... outcj=arcpy.Clip_management(raster,"113.45486338 36.0487175250001 119.848790435 42.6154530900001",out,"E:/不过尔尔/毕业设计/数据/区划/京津冀县域.shp","-3.402823e+038","ClippingGeometry","NO_MAINTAIN_EXTENT")
... outsec="E:/MERRA2_R/2009.10-2014.09/sgd/"+raster.split(".")[2]+".shp"
... arcpy.RasterToPoint_conversion(outcj,outsec,"VALUE")
arcpy.env.workspace=r"E:\MERRA2_R\2009.10-2014.09\sgd"
>>> import os
>>> pathfile=r"E:\MERRA2_R\2009.10-2014.09\sgd"
>>> ls=os.listdir(pathfile)
>>> outfile=open(pathfile+'2009.10-2014.09.txt',"a")
>>> for i in ls:
... if os.path.splitext(i)[1]==".shp":
... shppath=("E:/MERRA2_R/2009.10-2014.09/sgd/"+i)
... with arcpy.da.SearchCursor(shppath,["grid_code"]) as cursor:
... for row in cursor:
... g=row[0]
... result=str(g)+"\n"
... outfile.write(result)
... outfile.close()
今晚的代码历程比较顺利!明天加油呀!!!
20220113—aod数据整合
把前几天的工作进行了整合:wget批量下载—matlab将nc转Tif—批量Tif裁剪以及栅格转点——批量将点属性值写入txt文本,最终aod数据汇总进excel如下:
20220117—产量数据网格化
放假啦,歇了四天,有点不想动~~~
借鉴下面博文,给研究区地方批量找了经纬度坐标;目的是想把各县域的作物单产数据按面积加权汇总在前面空气污染指标网格内,还得摸索摸索!
arcgis人口空间化_基于GIS的人口普查数据的空间可视化_weixin_39640573的博客-CSDN博客
按照步骤可完成以下博文操作
在ArcGIS中利用“行政单元面积权重法”实现人口数据格网化_水滴石穿,绳锯木断-CSDN博客_人口数据格网化
产量数据添加到行政区划图中,才能与渔网相关联,将产量数据网格化;
20220119—产量数据网格化
将excel表格与gis属性表联合;有些计算可在excel中完成,最后再导入gis里;
如何通过excel表格链接赋值arcgis属性表,并制作入库文件_哔哩哔哩_bilibili
了解了根据不同经度选择投影带;
arcgis面积统计计算(包含投影转换、excel转换、属性表生成等)(小白直接上手)_哔哩哔哩_bilibili
日常学习小技巧:index 和match函数
https://jingyan.baidu.com/article/54b6b9c0d8426f2d583b478d.html
冬小麦产量整理为前述空气污染物指标相应格点数据:
步骤:行政区相应产量数据利用表格联合汇入京津冀县域shp属性表中——创建渔网——裁剪渔网——渔网与京津冀县域union——计算各块面积——计算各块面积所占渔网面积比例,面积加权分配作物单产得到渔网所对应的作物单产——渔网面顺序与前述栅格数据点顺序对应——最终得到栅格点上的作物单产数据;
20220121—返工的一天!!!
发现致命错误!!!前述matlab批量nc转栅格的时候应该旋转图像,否则值对应不上!!!
返工吧!!!
InPath='E:\MERRA2_ozone\1989.10-1994.09\nc\';
OutPath='E:\MERRA2_ozone\1989.10-1994.09\tif\';
filelist=dir([InPath,'*.nc']);
k=length(filelist);
for i=1:k
InFile = strcat(InPath,filelist(i).name)
ncdisp(InFile)
filename=[InPath,filelist(i).name]
ozone=ncread(filename,'TO3')
ozo=rot90(ozone,1)
lon=ncread(InFile,'lon')
lat=ncread(InFile,'lat')
GeoRef = georasterref('Rastersize',size(ozo),'Lonlim',[double(min(lon)),double(max(lon))],'Latlim',[double(min(lat)),double(max(lat))])
ozo_Tif = strcat(OutPath,filelist(i).name,'.tif')
geotiffwrite(ozo_Tif,flip(ozo),GeoRef)
20220506——matlab将计算好的nc数据重新写入新的nc文件
时隔三个月,开始一项新任务!!!又是从小白开始学起
目前,对于找到的OMI臭氧产品,时间分辨率是1天,但是还是想汇总成月均值,目前想了一个办法是通过matlab先计算为月均值后,再进行后续的转格式操作
原来的Nc文件中的数据(提取经纬度信息):
lo=ncread('OMI-Aura_L3-OMAEROe_2004m1001_v003-2011m1109t080411.he5.SUB.nc4','lon');
la=ncread('OMI-Aura_L3-OMAEROe_2004m1001_v003-2011m1109t080411.he5.SUB.nc4','lat');
创建新的nc文件并且导入原来的经纬度信息
nccreate('myncfile1.nc','lon','Dimensions',{'x',42});
nccreate('myncfile1.nc','lat','Dimensions',{'y',49});
ncwrite('myncfile1.nc','lon',lo);
ncwrite('myncfile1.nc','lat',la);
创建变量并导入计算好的数据
nccreate('myncfile1.nc','aodd',...
'Dimensions', {'x',42,'y',49},...
'FillValue','disable');
ncwrite('myncfile1.nc','aodd',aodm);
20220511——MATLAB一年循环12个月处理每月nc日文件为月均值导出tif
get 到了几个知识点
1.字符串的截取(在此处没用),文件名中特定部分提取以便循环;2.NAN值转化为0
% 一年12个月的循环
for j=1:12
filelist=dir(strcat('OMI-Aura_L3-OMAEROe_2004m',num2str(j),'*.nc4'));
k=length(filelist);
ozom=ones(42,49);
for i=1:k
InFile = strcat(filelist(i).name)
ncdisp(InFile)
filename=[filelist(i).name]
ozo=ncread(filename,'AbsorbingAerosolOpticalThicknessMW');
ozo2=ozo(:,:,2);
ozo2(find(isnan(ozo2)==1)) = 0;
ozom=ozom+ozo2;
end
ozomm=ozom/k;
lon=ncread(InFile,'lon');
lat=ncread(InFile,'lat');
InPath=strcat('OMI-Aura_L3-OMAEROe_2004',num2str(j),'.nc');
OutPath=strcat('OMI-Aura_L3-OMAEROe_2004',num2str(j),'.tif');
% 创建nc文件并导入经纬度信息
nccreate(InPath,'lon','Dimensions',{'x',42});
nccreate(InPath,'lat','Dimensions',{'y',49});
ncwrite(InPath,'lon',lon);
ncwrite(InPath,'lat',lat);
% 创建变量并导入计算好的数据
nccreate(InPath,'aodd',...
'Dimensions', {'x',42,'y',49},...
'FillValue','disable');
ncwrite(InPath,'aodd',ozomm);
ozo3=ncread(InPath,'aodd');
ozommm=rot90(ozo3,1);
GeoRef = georasterref('Rastersize',size(ozommm),'Lonlim',[double(min(lon)),double(max(lon))],'Latlim',[double(min(lat)),double(max(lat))])
ozo_Tif =OutPath
geotiffwrite(ozo_Tif,flip(ozommm),GeoRef)
end
3.每月最终的nc文件降尺度(0.25°——0.01°)处理尝试!!!好复杂,明天再试吧!
20220514——matlab双线性插值降尺度
1.双线性插值了解:
双线性插值(超级易懂的)_简单说鸭的博客-CSDN博客_双线性插值
对于一年12个月循环处理,代码:
% 三年一循环
for y=2005:2007
% 一年12个月的循环,为方便循环分两次
for j=1:9
filelist=dir(strcat('OMI-Aura_L3-OMAEROe_',num2str(y),'m0',num2str(j),'*.nc4'));
k=length(filelist);
ozom=zeros(42,49);
index=zeros(42,49);
for i=1:k
InFile = strcat(filelist(i).name);
ncdisp(InFile);
filename=[filelist(i).name];
ozo=ncread(filename,'AbsorbingAerosolOpticalThicknessMW');
ozo2=ozo(:,:,2);
ozo2(find(isnan(ozo2)==1)) = 0;
ind=ozo2~=0;
index=index+ind;
ozom=ozom+ozo2;
end
ozomm=ozom./index/1000;
% 由于一些天数里部分像元缺失影像,应该除以有值的天数而不是每月的天数,按下载说明将原值缩小1000倍
ozomm(find(isnan(ozomm)==1)) = 0;
% 降尺度双线性插值处理
lon=ncread(InFile,'lon');
lat=ncread(InFile,'lat');
[xx,yy]=meshgrid(lat,lon);
[xi,yi]=meshgrid(31:0.01:43,110:0.01:123);
c=interp2(xx,yy,ozomm,xi,yi);
latn=xi(1,:);
lonn=yi(:,1);
InPath=strcat('OMI-Aura_L3-OMAEROe_',num2str(y),num2str(j),'.nc');
out='E:\north china\datapre\AAOD\2005-2008\tif05-08\';
OutPath=strcat(out,'OMI-Aura_L3-OMAEROe_',num2str(y),num2str(j),'.tif');
% 创建nc文件并导入经纬度信息
nccreate(InPath,'lon','Dimensions',{'x',1301});
nccreate(InPath,'lat','Dimensions',{'y',1201});
ncwrite(InPath,'lon',lonn);
ncwrite(InPath,'lat',latn);
% 创建变量并导入计算好的数据
nccreate(InPath,'aaod',...
'Dimensions', {'x',1301,'y',1201},...
'FillValue','disable');
ncwrite(InPath,'aaod',c);
ozo3=ncread(InPath,'aaod');
ozommm=rot90(ozo3,1);
GeoRef = georasterref('Rastersize',size(ozommm),'Lonlim',[double(min(lonn)),double(max(lonn))],'Latlim',[double(min(latn)),double(max(latn))])
ozo_Tif =OutPath
geotiffwrite(ozo_Tif,flip(ozommm),GeoRef)
end
% 10-12月
for j=10:12
filelist=dir(strcat('OMI-Aura_L3-OMAEROe_',num2str(y),'m',num2str(j),'*.nc4'));
k=length(filelist);
ozom=zeros(42,49);
index=zeros(42,49);
for i=1:k
InFile = strcat(filelist(i).name);
ncdisp(InFile);
filename=[filelist(i).name];
ozo=ncread(filename,'AbsorbingAerosolOpticalThicknessMW');
ozo2=ozo(:,:,2);
ozo2(find(isnan(ozo2)==1)) = 0;
ind=ozo2~=0;
index=index+ind;
ozom=ozom+ozo2;
end
ozomm=ozom./index/1000;
ozomm(find(isnan(ozomm)==1)) = 0;
% 降尺度双线性插值处理
lon=ncread(InFile,'lon');
lat=ncread(InFile,'lat');
[xx,yy]=meshgrid(lat,lon);
[xi,yi]=meshgrid(31:0.01:43,110:0.01:123);
c=interp2(xx,yy,ozomm,xi,yi);
latn=xi(1,:);
lonn=yi(:,1);
InPath=strcat('OMI-Aura_L3-OMAEROe_',num2str(y),num2str(j),'.nc');
out='E:\north china\datapre\AAOD\2005-2008\tif05-08\';
OutPath=strcat(out,'OMI-Aura_L3-OMAEROe_',num2str(y),num2str(j),'.tif');
% 创建nc文件并导入经纬度信息
nccreate(InPath,'lon','Dimensions',{'x',1301});
nccreate(InPath,'lat','Dimensions',{'y',1201});
ncwrite(InPath,'lon',lonn);
ncwrite(InPath,'lat',latn);
% 创建变量并导入计算好的数据
nccreate(InPath,'aaod',...
'Dimensions', {'x',1301,'y',1201},...
'FillValue','disable');
ncwrite(InPath,'aaod',c);
ozo3=ncread(InPath,'aaod');
ozommm=rot90(ozo3,1);
GeoRef = georasterref('Rastersize',size(ozommm),'Lonlim',[double(min(lonn)),double(max(lonn))],'Latlim',[double(min(latn)),double(max(latn))])
ozo_Tif =OutPath
geotiffwrite(ozo_Tif,flip(ozommm),GeoRef)
end
end
2.空间降尺度
青藏高原地区微波土壤水分产品降尺度及其时空变化分析 - 中国知网
(空间降尺度,别人的算法可能不仅仅是一个简单的插值降尺度,例如看到的土壤水分产品的降尺度,会结合蒸散发模型利用高分辨率的土壤温度来反演相应的土壤水分数据;对于空气污染物数据来讲,单从简单的插值降尺度的角度是否会误差偏大?)
青藏高原地区卫星降水数据空间降尺度研究及时空变化分析 - 中国知网
(3)ArcGIS批量分区统计以及表转EXCEL
代码参考官方示例
批量分区统计处理以及表转EXCEL
import arcpy
from arcpy.sa import * (#这步不可省略)
arcpy.env.workspace=r'E:\north china\datapre\AAOD\2005-2008\tif05-08'
... rasters=arcpy.ListRasters("*",'tif')
... outpath=r'E:\north china\datapre\AAOD\2005-2008\table05-08'
outpathb=r'E:\north china\datapre\AAOD\2005-2008\excel05-08'
... for raster in rasters:
inzonedata=r'E:\north china\region\ncc.shp'
outtable=outpath+'\\'+raster.split("_")[2].split(".")[0]+".dbf"
arcpy.CheckOutExtension("Spatial") (#这步也不可省略)
outzsat=ZonalStatisticsAsTable(inzonedata, "PAC", raster,outtable, "NODATA", "MEAN")
out_xls = outpathb+'\\'+raster.split("_")[2].split(".")[0]+".xls"
arcpy.TableToExcel_conversion(outzsat, out_xls)
20220518——数据预处理
(1)matlab批量读取EXCEL文件重新写入
aa=[];
for y=2005:2007
for i=1:12
list=strcat(num2str(y),num2str(i),'.xls');
biaoming=strcat(num2str(y),num2str(i));
a=xlsread(list,biaoming,'E2:E496');
aa=[aa a];
end
end
b=xlswrite('05-07.xls',aa)
20220705——又一轮的数据预处理
与之前的网格数据处理不同,本次的数据预处理,基于站点数据插值为网格数据后再分区统计至县域;格点气候数据和污染物数据汇总至县域数据应仅考虑有耕地的栅格点,考虑用耕地面积数据掩膜提取气候数据和污染物数据后,再分区统计至各县域;
批量excel转表,插值处理以及分区统计后转EXCEL
import arcpy
from arcpy.sa import *
arcpy.env.workspace=r'D:\ncc_datas\datasorting\pre\excel'
... rasters=arcpy.ListRasters("*",'xlsx')
... outpath=r'D:\ncc_datas\datapre\dbf'
outpathb=r'E:\north china\datapre\AAOD\2005-2008\excel05-08'
... for raster in rasters:
outtable=outpath+'\\'+raster.split("-")[2].split(".")[0]+".dbf"
arcpy.ExcelToTable_conversion(raster, outtable, "Sheet1")
在GIS里,excel转表后,属性表转点数据的操作只能手动,目前我好像没有找到批量此操作的过程,所以,中间卡住了,后续的批量点插值无法进行;;;
尝试用R试试;在网上也发现了QGIS,两个都试试吧,哎一古~~~