首先需要安装micaps的读取库
读取文件
无论文件是如何存放的,读取出来都是从地理的坐下角的数组,如果需要可进行翻转**
读取方法
from nmc_met_io import read_micaps
import os
import numpy as np
import writeFile
#按月读取加写文件
arrMonth=["09","10","11","12"]
path="E://Pic/不同高度风数据/micapse/10m/"
files = os.listdir(path)
for strmonth in arrMonth:
#提前知道是多少个文件数组,这个求平均速度很快,如果使用的是list,然后在asarray进行转换,数据量大的时候,转换特别慢
arrayU = np.zeros((39, 81,121), dtype=float)
arrayV = np.zeros((39, 81,121), dtype=float)
i=0
for file in files:
if os.path.isfile(path+ "/"+file) and file[4:6] == strmonth:
data = read_micaps.read_micaps_11(path+"/"+file)
arrayU[i,]=np.flip(data.variables["uwind"][0],0)
arrayV[i,]=np.flip(data.variables["vwind"][0],0)
i=i+1
dataU = arrayU.mean(0)#求平均
dataV = arrayV.mean(0)
writeFile.write11("E://Pic/不同高度风数据/micapse/123/", strmonth + ".txt",dataU, dataV,strmonth + "_month_avg_wind", 2022, 8, 1, 1, 0, 1, 0.125,-0.125, 110.0000, 125.0000, 45.0000,35.0000, 121, 81)
写micapse格式文件
写micapse11和micapse4文件
import numpy as np
def write11(output_dir,fileName, dataU, dataV, description, year, month, day, hour, period, level, lonInterval, latInterval, beginLon,
endLon, beginLat, endLat, lonGridCount, latGridCount):
with open(output_dir + fileName, 'w+') as writer:
eachline = "diamond 11 " + description
writer.write(eachline + "\n")
eachline = str(year) + "\t" + str(month) + "\t" + str(day) + "\t" + str(
hour) + "\t" + str(period) + "\t" + str(int(level))
writer.write(eachline + "\n")
eachline = str(lonInterval) + "\t" + str(latInterval) + "\t" + str(
round(beginLon, 2)) + "\t" \
+ str(round(endLon, 2)) + "\t" + str(
round(beginLat, 2)) + "\t" + str(
round(endLat, 2)) + "\t" + str(
lonGridCount) + "\t" + str(
latGridCount)
writer.write(eachline + "\n")
np.savetxt(writer, np.vstack((dataU, dataV)),fmt='%.2f', delimiter='\t')
def write4(output_dir, fileName, data, description, y, m, d, h, period, level, lonInterval,
latInterval, startLon, endLon, startLat, endLat, lonGridCount,latGridCount):
with open(output_dir + fileName, 'w') as writer:
eachline = "diamond 4 " + description
writer.write(eachline + "\n")
eachline = str(y) + "\t" + str(m) + "\t" + str(d) + "\t" + str(
h) + "\t" + str(
period) + "\t" + str(int(level))
writer.write(eachline + "\n")
eachline = str(lonInterval) + "\t" + str(latInterval) + "\t" + str(
round(startLon, 2)) + "\t" \
+ str(endLon) + "\t" + str(
round(startLat, 2)) + "\t" + str(
round(endLat, 2)) + \
"\t" + str(lonGridCount) + "\t" + str(
latGridCount) + " 0 0 0 3 0"
writer.write(eachline + "\n")
np.savetxt(writer, data, fmt='%.2f', delimiter='\t')