Python篇:读写micaps文件

首先需要安装micaps的读取库

nmc-met-io资源下载

读取文件

无论文件是如何存放的,读取出来都是从地理的坐下角的数组,如果需要可进行翻转**
读取方法

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')

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Micaps数据是一种气象科学中广泛使用的数据格式,主要用于气象预报和气象研究。Python是一种功能强大、易于学习和使用的编程语言,已经成为气象数据分析和处理的常用工具之一。下面是Python打开Micaps数据的方法。 首先,Python可以使用numpy和xarray库来处理Micaps数据。在使用这两个库之前,需要先实现Micaps文件读取Micaps格式经常改变,因此在进行数据读取之前需要确认数据的格式和版本。 Micaps数据中经常包含ASCII码表示的头信息和表格数据。可以使用Python文件操作函数打开Micaps文件,并将文件头和数据分别存储在两个变量中。然后,将数据格式化成可以被numpy或xarray解析的格式,可以是列表或数组。通过xarray或numpy的功能,可以将处理结果存储为新文件或以其他格式保存。 其次,除了numpy和xarray,Python还可以使用Open CV、GeoPandas和GDAL这些专门用于空间数据处理的库来处理Micaps数据。 OpenCV是一个用于图像处理和计算机视觉的库,可以处理从雷达或卫星收集的Micaps数据。GeoPandas是一个与地理信息系统(GIS)相关的Python库,可以处理Micaps数据中的GIS格式。GDAL (Geospatial Data Abstraction Library)是一个开源 GIS 库,可以用于读写Micaps数据和多种其他空间数据格式。 综上所述,Python可以使用多个方法来打开Micaps数据,如利用numpy和xarray库、OpenCV、GeoPandas和GDAL等专业库,根据使用的方法进行相应的数据处理,并将数据保存为新的文件格式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

肆意飞扬

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值