python对netCDF气象文件中所有数据解析成CSV
本次实验目标是:将nc或者grib中的数据解析成csv格式的文件,不管有多少个属性都可以顺利解析出。
1.函数调用
1.1将grib文件转换成nc文件
import os
os.chdir(r'F:\weather_Data\wgrib2-master')
os.system(r"wgrib2 F:\weather_Data\TIGGE20220306\_taiyuan_data.grib -netcdf F:\weather_Data\TIGGE20220306\_taiyuan_data.nc")#将太原气象文件grib格式转换成nc格式文件
以上代码将太原气象文件grib格式转换成nc格式文件,这么做的原因是,便于解析nc文件的所有数据
1.2查看nc文件中有哪些气象属性
from netCDF4 import Dataset
nc_obj = Dataset('F:\weather_Data\TIGGE20220306\_taiyuan_data.nc')
#查看nc文件中的变量
print(nc_obj.variables.keys())
"__________输出结果为____________"
dict_keys(['latitude', 'longitude', 'time', 'UGRD_10maboveground', 'VGRD_10maboveground', 'TMP_2maboveground', 'DPT_2maboveground'])
1.3查看nc文件气象属性信息
from netCDF4 import Dataset
nc_obj = Dataset('F:\weather_Data\TIGGE20220306\_taiyuan_data.nc')
#查看nc文件中的变量
print(nc_obj.variables)
"__________输出结果为____________"
{'latitude': <class 'netCDF4._netCDF4.Variable'>
float64 latitude(latitude)
units: degrees_north
long_name: latitude
unlimited dimensions:
#current shape 意思是latitude这个变量是一维度,并且有三个值
current shape = (3,)
filling on, default _FillValue of 9.969209968386869e+36 used, 'longitude': <class 'netCDF4._netCDF4.Variable'>
float64 longitude(longitude)
units: degrees_east
long_name: longitude
unlimited dimensions:
current shape = (5,)
filling on, default _FillValue of 9.969209968386869e+36 used, 'time': <class 'netCDF4._netCDF4.Variable'>
float64 time(time)
units: seconds since 1970-01-01 00:00:00.0 0:00
long_name: verification time generated by wgrib2 function verftime()
reference_time: 1546300800.0
reference_time_type: 0
reference_date: 2019.01.01 00:00:00 UTC
reference_time_description: kind of product unclear, reference date is variable, min found reference date is given
time_step_setting: auto
time_step: 86400.0
unlimited dimensions: time
current shape = (31,)
filling on, default _FillValue of 9.969209968386869e+36 used, 'UGRD_10maboveground': <class 'netCDF4._netCDF4.Variable'>
float32 UGRD_10maboveground(time, latitude, longitude)
_FillValue: 9.999e+20
short_name: UGRD_10maboveground
long_name: U-Component of Wind
level: 10 m above ground
units: m/s
unlimited dimensions: time
current shape = (31, 3, 5)
filling on, ........
进程已结束,退出代码0
1.4查看每个气象属性存储的信息
nc_obj = Dataset('F:\weather_Data\TIGGE20220306\_taiyuan_data.nc')
print(nc_obj.variables["DPT_2maboveground"][:])
"__________输出结果为____________"
[[[258.79907 257.64478 258.44458 257.92798 258.6775 ]
[258.71997 257.4231 256.4807 256.0266 257.69897]
[258.33667 256.7185 255.2776 254.90553 255.98024]]
[[261.82712 259.89548 260.3027 259.8486 260.2607 ]
[261.50388 260.73825 257.9033 258.7109 259.374 ]
[261.84274 260.5439 257.2822 256.67087 258.74118]]
.......
1.5查看气象属性的纬度
nc_obj = Dataset('F:\weather_Data\TIGGE20220306\_taiyuan_data.nc')
print(nc_obj.variables["DPT_2maboveground"].shape)
"__________输出DPT_2maboveground纬度结果为____________"
(31, 3, 5)
这里的(31, 3, 5)表示的含义是:31表示时间属性有多少个值,3代表的是经度(或者是纬度)有多少个值,5代表的是纬度(或者是经度)有多少个值,总体来说表示一个DPT_2maboveground(Dew Point Temperature 2 m above ground)”地上两米露点温度“三维数据值由时间,经度,纬度三个属性来确定。
2.解析nc文件
由前面普及的代码,现在应该可以看懂以下的代码
from netCDF4 import Dataset,num2date
import csv
import os
import sys
print("______________________________________")
def ncConvert(tailname,filename,csv1):
# nc_obj = Dataset('F:\weather_Data\wgrib2-master\\' + era-1.nc)
if(tailname == "grib" or tailname == "grib2"):
nc_obj = Dataset('F:\weather_Data\wgrib2-master\\' + filename)
if (tailname == "nc"):
nc_obj = Dataset('D:\convertdocument\\' + filename)
ncdata = list(range(len(nc_obj.variables)))
i = 0
timetail = nc_obj.variables["time"]
for x in nc_obj.variables.keys():
if (x == "longitude"):
ncdata[1] = nc_obj.variables[x][:]
if (x == "latitude"):
ncdata[2] = nc_obj.variables[x][:]
if (x == "time"):
ncdata[0] = nc_obj.variables[x][:]
if (i > 2):
ncdata[i] = nc_obj.variables[x][:]
i += 1
print(len(ncdata[2]))
print(len(ncdata[1]))
print(len(ncdata[0]))
test_list = list(nc_obj.variables.keys())
test_list[0] = "time"
test_list[1] = "longitude"
test_list[2] = "latitude"
print(ncdata)
try:
# 打开目标csv文件
# with open("F:\weather_Data\wgrib2-master\\" + csv1 + '.csv', 'a', newline='') as targetFile:
with open(csv1, 'a', newline='') as targetFile:
# 创建写入流
writer = csv.writer(targetFile)
# 写入表头
writer.writerow((test_list
))
# 写入数据
for z in range(len(ncdata[0])): # z为时间
for j in range(len(ncdata[2])): # j为纬度
for k in range(len(ncdata[1])): # k为经度
a = list(range(len(nc_obj.variables)))
count = 0
while (count < len(nc_obj.variables)):
if (count == 0):
time1 = num2date(ncdata[0][z], units=timetail.units)
a[count] = (time1)
if (count == 1):
a[count] = (ncdata[1][k])
if (count == 2):
a[count] = (ncdata[2][j])
if (count > 2):
a[count] = (ncdata[count][z][j][k])
count += 1
writer.writerow(a)
targetFile.close() # 关闭文件
print('Get' + csv1 + '.csv Success!')
except Exception as e: # 抛异常
print('Error :' + str(e))
def convert(file,csv1):
subfix = 0
if(file.split(".")[1]=="grib" or file.split(".")[1]=="grib2"):
print("!!!!!!!!")
os.chdir(r'F:\weather_Data\wgrib2-master')
subfix = file.split("\\")[-1].split(".")[0]
subfix = subfix + ".nc"
s = "wgrib2" + " " + file + " " + "-netcdf" + " " + subfix;
os.system(s)
if(file.split(".")[1]=="nc"):
subfix = file.split("\\")[-1].split(".")[0]
subfix = subfix + ".nc"
ncConvert(file.split(".")[1],subfix,csv1)
if __name__ == '__main__':
convert("D:\convertdocument\_taiyuan_data.nc","F:\weather_Data\wgrib2-master\\xxxxxxxxxxxxxxxxxxxxxx.csv")
- 解析结果如下: