解析
 
from datetime import *
import time
import calendar
import json
import numpy as np
from struct import *
import binascii
import netCDF4 file = open(r"D:/radarDataTest/Z_QPF_20140831203600.F030.bin", "rb")
data = file.read();
print(len(data))
file.close()
#
file = open(r"D:/radarDataTest/Z_QPF_20140831203600.F030.bin", "rb")
length = 0 zonName,dataName,flag,version, = unpack("12s38s8s8s", file.read(12+38+8+8))
zonName = zonName.decode("gbk").rstrip('\x00')
dataName = dataName.decode("gbk").rstrip('\x00')
flag = flag.decode("gbk").rstrip('\x00')
version = version.decode("gbk").rstrip('\x00')
length = length + 12+38+8+8
#
print(zonName)
print("数据说明: " + dataName)
print("文件标志: " + flag)
print("数据版本号: " + version) #
year,month,day,hour,minute,interval, = unpack("HHHHHH", file.read(2+2+2+2+2+2))
print("时间: "+str(year)+"-"+str(month)+"-"+str(day)+" "+str(hour)+":"+str(minute))
print("时段长: "+str(interval))
length = length + 2+2+2+2+2+2 #
XNumGrids,YNumGrids,ZNumGrids, = unpack("HHH", file.read(2+2+2))
print("X: " + str(XNumGrids)+" Y: "+str(YNumGrids)+" Z:"+str(ZNumGrids))
length = length + 2+2+2 #
RadarCount, = unpack("i", file.read(4))
print("拼图雷达数: " + str(RadarCount))
length = length + 4 #
StartLon,StartLat,CenterLon,CenterLat,XReso,YReso, = unpack("ffffff", file.read(4+4+4+4+4+4))
print("开始经度: "+str(StartLon)+" 开始纬度:"+str(StartLat)+" 中心经度:"+str(CenterLon)+" 中心纬度:"+str(CenterLat))
print("经度方向分辨率:"+str(XReso)+" 纬度方向分辨率:"+str(YReso))
length = length + 4+4+4+4+4+4 ZhighGrids=[]
for i in range(0, 40):
ZhighGrid, = unpack("f", file.read(4))
ZhighGrids.append(ZhighGrid)
print("垂直方向的高度:"+str(ZhighGrids))
length = length + 40*4 #
RadarStationNames=[]
for i in range(0, 20):
RadarStationName, = unpack("16s", file.read(16))
RadarStationName = RadarStationName.decode("gbk")
RadarStationNames.append(RadarStationName.rstrip('\x00'))
print("相关站点名称:"+str(RadarStationNames))
length = length + 20*16 #
RadarLongitudes=[]
for i in range(0, 20):
RadarLongitude, = unpack("f", file.read(4))
RadarLongitudes.append(RadarLongitude)
print("相关站点所在经度:"+str(RadarLongitudes))
length = length + 20*4 #
RadarLatitudes=[]
for i in range(0, 20):
RadarLatitude, = unpack("f", file.read(4))
RadarLatitudes.append(RadarLatitude)
print("相关站点所在纬度:"+str(RadarLatitudes))
length = length + 20*4 #
RadarAltitudes=[]
for i in range(0, 20):
RadarAltitude, = unpack("f", file.read(4))
RadarAltitudes.append(RadarAltitude)
print("相关站点所在海拔高度:"+str(RadarAltitudes))
length = length + 20*4 #
MosaicFlags=[]
for i in range(0, 20):
MosaicFlag, = unpack("B", file.read(1))
MosaicFlags.append(MosaicFlag)
print("该相关站点数据是否包含在本次拼图中:"+str(MosaicFlags))
length = length + 20*1 #
m_iDataType, = unpack("h", file.read(2))
print("数据类型定义:"+str(m_iDataType))
if m_iDataType==0:
print("unsigned char")
elif m_iDataType==1:
print("char")
elif m_iDataType==2:
print("unsigned short")
elif m_iDataType==3:
print("short")
elif m_iDataType==4:
print("unsigned short")
length = length + 2 #
m_iLevelDimension, = unpack("h", file.read(2))
print("每一层的向量数:"+str(m_iLevelDimension))
length = length + 2 #
Reserveds=[]
Reserveds, = unpack("168s", file.read(168))
Reserveds = Reserveds.decode("gbk").rstrip('\x00')
print("该相关站点数据是否包含在本次拼图中: "+Reserveds)
length = length + 168 #打印数据
valueZYX = []
for i in range(0, ZNumGrids):
valueYX = []
for j in range(0, YNumGrids):
valueX = []
for k in range(0, XNumGrids):
value, = unpack("h", file.read(2))
#value, = unpack("b", file.read(1))
'''
if value > 0:
print(value)
'''
valueX.append(value)
valueYX.append(valueX)
valueZYX.append(valueYX)
#
#print("数据:"+str(valueZYX))
length = length + ZNumGrids*YNumGrids*XNumGrids*2
print(length)
#
print("----------------------------数据----------------------------") file.close()
生成ASCII
import time
from struct import * start = time.clock()
file = open(r"D:/radarDataTest/Z_QPF_20140831203600.F030.bin", "rb")
#
zonName,dataName,flag,version, = unpack("12s38s8s8s", file.read(12+38+8+8))
zonName = zonName.decode("gbk").rstrip('\x00')
dataName = dataName.decode("gbk").rstrip('\x00')
flag = flag.decode("gbk").rstrip('\x00')
version = version.decode("gbk").rstrip('\x00') #
print(zonName)
print("数据说明: " + dataName)
print("文件标志: " + flag)
print("数据版本号: " + version)
#
length = 0
length = length + 2+2+2+2+2+2 # 时间说明
file.read(length) XNumGrids,YNumGrids,ZNumGrids, = unpack("HHH", file.read(2+2+2))
print("X: " + str(XNumGrids)+" Y: "+str(YNumGrids)+" Z:"+str(ZNumGrids)) length = 0
length = length + 4 # 拼图雷达数
file.read(length)
#
StartLon,StartLat,CenterLon,CenterLat,XReso,YReso, = unpack("ffffff", file.read(4+4+4+4+4+4))
print("开始经度: "+str(StartLon)+" 开始纬度:"+str(StartLat)+" 中心经度:"+str(CenterLon)+" 中心纬度:"+str(CenterLat))
print("经度方向分辨率:"+str(XReso)+" 纬度方向分辨率:"+str(YReso)) ZhighGrids=[]
for i in range(0, 40):
ZhighGrid, = unpack("f", file.read(4))
ZhighGrids.append(ZhighGrid)
print(" 垂直方向的高度:"+str(ZhighGrids)) #
length = 0
length = length + 20*16 # 相关站点名称
length = length + 20*4 # 相关站点所在经度
length = length + 20*4 # 相关站点所在纬度
length = length + 20*4 # 相关站点所在海拔高度
length = length + 20*1 # 该相关站点数据是否包含在本次拼图中
length = length + 2 # 数据类型定义
length = length + 2 # 每一层的向量数
length = length + 168 # 保留信息
file.read(length) textZYX = []
for i in range(0, ZNumGrids):
textYX = []
for j in range(0, YNumGrids):
textX = []
for k in range(0, XNumGrids):
value, = unpack("h", file.read(2))
textX.append(str(value))
textYX.append(' '.join(textX))
textZYX.append('\n'.join(textYX))
file.close() #
#------------------------------------------------------------------------------- file_object = open('ASCIIData.txt', 'w')
file_object.write("NCOLS " + str(XNumGrids) + "\n")
file_object.write("NROWS " + str(YNumGrids) + "\n")
file_object.write("XLLCENTER " + str(StartLon) + "\n")
file_object.write("YLLCENTER " + str(StartLat - YReso * (YNumGrids - 1)) + "\n") # round(YReso, 3) *
file_object.write("CELLSIZE " + str(XReso) + "\n")
file_object.write("NODATA_VALUE " + str(-9999) + "\n")
#
#
file_object.writelines(textZYX[0])
file_object.close()
end = time.clock()
print("read: %f s" % dateSpanTransfer)
dateSpanTransfer = end - start #-------------------------------------------------------------------------------
生成Image(.img)
import time
from struct import *
from osgeo import gdal, osr
from osgeo.gdalconst import *
import numpy start = time.clock()
file = open(r"D:/radarDataTest/Z_QPF_20140831203600.F030.bin", "rb")
#
zonName,dataName,flag,version, = unpack("12s38s8s8s", file.read(12+38+8+8))
zonName = zonName.decode("gbk").rstrip('\x00')
dataName = dataName.decode("gbk").rstrip('\x00')
flag = flag.decode("gbk").rstrip('\x00')
version = version.decode("gbk").rstrip('\x00') #
print(zonName)
print("数据说明: " + dataName)
print("文件标志: " + flag)
print("数据版本号: " + version)
#
length = 0
length = length + 2+2+2+2+2+2 # 时间说明
file.read(length) XNumGrids,YNumGrids,ZNumGrids, = unpack("HHH", file.read(2+2+2))
print("X: " + str(XNumGrids)+" Y: "+str(YNumGrids)+" Z:"+str(ZNumGrids)) length = 0
length = length + 4 # 拼图雷达数
file.read(length)
#
StartLon,StartLat,CenterLon,CenterLat,XReso,YReso, = unpack("ffffff", file.read(4+4+4+4+4+4))
print("开始经度: "+str(StartLon)+" 开始纬度:"+str(StartLat)+" 中心经度:"+str(CenterLon)+" 中心纬度:"+str(CenterLat))
print("经度方向分辨率:"+str(XReso)+" 纬度方向分辨率:"+str(YReso)) ZhighGrids=[]
for i in range(0, 40):
ZhighGrid, = unpack("f", file.read(4))
ZhighGrids.append(ZhighGrid)
print(" 垂直方向的高度:"+str(ZhighGrids)) #
length = 0
length = length + 20*16 # 相关站点名称
length = length + 20*4 # 相关站点所在经度
length = length + 20*4 # 相关站点所在纬度
length = length + 20*4 # 相关站点所在海拔高度
length = length + 20*1 # 该相关站点数据是否包含在本次拼图中
length = length + 2 # 数据类型定义
length = length + 2 # 每一层的向量数
length = length + 168 # 保留信息
file.read(length) valueZYX = []
for i in range(0, ZNumGrids):
valueYX = []
for j in range(0, YNumGrids):
valueX = []
for k in range(0, XNumGrids):
value, = unpack("h", file.read(2))
valueX.append(value)
valueYX.append(valueX)
valueZYX.append(valueYX)
file.close()
#
#
#------------------------------------------------------------------------------- end = time.clock()
dateSpanTransfer = end - start
print("read: %f s" % dateSpanTransfer)
#
#
driver = gdal.GetDriverByName('HFA')
driver.Register()
dataSetImg = driver.Create( "D:/radarDataTest/edarsImage.img", XNumGrids, YNumGrids, 1, gdal.GDT_Float32 )
#
dataSetImg.SetGeoTransform( [ StartLon, XReso, 0, StartLat, 0, -YReso ] )
#
srs = osr.SpatialReference()
srs.SetWellKnownGeogCS( 'WGS84' )
dataSetImg.SetProjection( srs.ExportToWkt() )
#
value2D = numpy.matrix( valueYX, dtype=numpy.float32 )
dataSetImg.GetRasterBand(1).WriteArray( value2D )
#
dataSetImg = None #datasource.Destroy()
#-------------------------------------------------------------------------------
生成netCDF
from datetime import *
import time
import calendar
import json
import numpy as np
from struct import *
import binascii
import numpy
from numpy.random import uniform
from netCDF4 import Dataset start = time.clock()
file = open(r"D:/radarDataTest/Z_QPF_20140831203600.F030.bin", "rb")
#
zonName,dataName,flag,version, = unpack("12s38s8s8s", file.read(12+38+8+8))
zonName = zonName.decode("gbk").rstrip('\x00')
dataName = dataName.decode("gbk").rstrip('\x00')
flag = flag.decode("gbk").rstrip('\x00')
version = version.decode("gbk").rstrip('\x00') #
print(zonName)
print("数据说明: " + dataName)
print("文件标志: " + flag)
print("数据版本号: " + version)
#
length = 0
length = length + 2+2+2+2+2+2 # 时间说明
file.read(length) XNumGrids,YNumGrids,ZNumGrids, = unpack("HHH", file.read(2+2+2))
print("X: " + str(XNumGrids)+" Y: "+str(YNumGrids)+" Z:"+str(ZNumGrids)) length = 0
length = length + 4 # 拼图雷达数
file.read(length)
#
StartLon,StartLat,CenterLon,CenterLat,XReso,YReso, = unpack("ffffff", file.read(4+4+4+4+4+4))
print("开始经度: "+str(StartLon)+" 开始纬度:"+str(StartLat)+" 中心经度:"+str(CenterLon)+" 中心纬度:"+str(CenterLat))
print(" 经度方向分辨率:"+str(XReso)+" 纬度方向分辨率:"+str(YReso)) ZhighGrids=[]
for i in range(0, 40):
ZhighGrid, = unpack("f", file.read(4))
ZhighGrids.append(ZhighGrid)
print(" 垂直方向的高度:"+str(ZhighGrids)) #
length = 0
length = length + 20*16 # 相关站点名称
length = length + 20*4 # 相关站点所在经度
length = length + 20*4 # 相关站点所在纬度
length = length + 20*4 # 相关站点所在海拔高度
length = length + 20*1 # 该相关站点数据是否包含在本次拼图中
length = length + 2 # 数据类型定义
length = length + 2 # 每一层的向量数
length = length + 168 # 保留信息
file.read(length) valueZYX = []
for i in range(0, ZNumGrids):
valueYX = []
for j in range(0, YNumGrids):
valueX = []
for k in range(0, XNumGrids):
#value, = unpack("h", file.read(2))
#textX.append(str(value/10.0))
value, = unpack("b", file.read(1))
textX.append(str(value*2+66.0))
valueYX.append(valueX)
valueZYX.append(valueYX)
file.close()
#
valueXYZ = []
for k in range(0, XNumGrids):
for j in range(0, YNumGrids):
for i in range(0, ZNumGrids):
valueXYZ.append(valueZYX[i][j][k]) #
file = open(r"D:/radarDataTest/Z_QPF_20140831203600.F030.bin", "rb")
rootgrp = Dataset("test.nc", "w", format="NETCDF4")
#rootgrp = Dataset("test.nc", "a")
#fcstgrp = rootgrp.createGroup("forecasts") lon = rootgrp.createDimension("lon", XNumGrids)
lat = rootgrp.createDimension("lat", YNumGrids)
alt = rootgrp.createDimension("alt", ZNumGrids) lon = rootgrp.createVariable("lon", "f8", ("lon",))
lat = rootgrp.createVariable("lat", "f8", ("lat",))
alt = rootgrp.createVariable("alt", "f8", ("alt",)) #val = rootgrp.createVariable("val","f4",("zz","yy","xx",))
val = rootgrp.createVariable("val","f4",("lon","lat","alt",)) #
rootgrp.description = dataName
rootgrp.history = "创建时间: " + time.strftime('%Y-%m-%d %X', time.localtime())
rootgrp.Source_Software = "SmartMap"
#
lon.units = "degrees_east"
lon.long_name = "longitude coordinate"
lon.standard_name = "longitude"
#
lat.units = "degrees_north"
lat.long_name = "latitude coordinate"
lat.standard_name = "latitude"
#
alt.units = "m"
alt.long_name = "altitude"
alt.standard_name = "heigh"
#
val.long_name = "value"
val.esri_pe_string = 'GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]]'
val.coordinates = "lon lat alt"
val.units = "Degree"
val.missing_value = -9999 #interval = 0.009999999776482582
interval = 0.01
#x = numpy.arange(-90,91,2.5) x = []
for i in range(0, XNumGrids):
x.append(StartLon + i * round(XReso, 3))
#x = numpy.array(x)
lon[:] = x #
#y = numpy.arange(-180,180,2.5)
y = []
for i in range(0, YNumGrids):
y.append(StartLat - i * round(YReso, 3))
#y = numpy.array(y)
lat[:] = y
# z = []
for i in range(0, ZNumGrids):
z.append(ZhighGrids[i])
#z = numpy.array(z)
alt[:] = z
# #kk = uniform(size=(2,3,4,5))
#print(kk) #val[::]=valueZYX
val[::] = valueXYZ #
rootgrp.close()

Python解析SWAN气象雷达数据--(解析、生成ASCII、Image、netCDF)的更多相关文章

  1. python爬虫的页面数据解析和提取/xpath/bs4/jsonpath/正则(1)

    一.数据类型及解析方式 一般来讲对我们而言,需要抓取的是某个网站或者某个应用的内容,提取有用的价值.内容一般分为两部分,非结构化的数据 和 结构化的数据. 非结构化数据:先有数据,再有结构, 结构化数 ...

  2. python爬虫---爬虫的数据解析的流程和解析数据的几种方式

    python爬虫---爬虫的数据解析的流程和解析数据的几种方式 一丶爬虫数据解析 概念:将一整张页面中的局部数据进行提取/解析 作用:用来实现聚焦爬虫的吧 实现方式: 正则 (针对字符串) bs4 x ...

  3. 数据解析_bs进行数据解析

    1.bs4进行数据解析 数据解析的原理 1.标签定位 2.提取标签,标签属性中存储的数据值 bs4数据解析的原理 1.实例化一个BeautifulSoup对象,并且将页面源码数据加载到该对象中 2.通 ...

  4. Python爬虫之三种数据解析方式

    一.引入 二.回顾requests实现数据爬取的流程 指定url 基于requests模块发起请求 获取响应对象中的数据 进行持久化存储 其实,在上述流程中还需要较为重要的一步,就是在持久化存储之前需 ...

  5. 05 Python网络爬虫的数据解析方式

    一.爬虫数据解析的流程 1.指定url 2.基于requests模块发起请求 3.获取响应中的数据 4.数据解析 5.进行持久化存储 二.解析方法 (1)正则解析 (2)bs4解析 (3)xpath解 ...

  6. Unity3d-XML文件数据解析&JSON数据解析

    1.XML文件数据解析:(首先须要导入XMLParser解析器,The latest released download from:http://dev.grumpyferret.com/unity/ ...

  7. python爬虫的页面数据解析和提取/xpath/bs4/jsonpath/正则(2)

    上半部分内容链接 : https://www.cnblogs.com/lowmanisbusy/p/9069330.html 四.json和jsonpath的使用 JSON(JavaScript Ob ...

  8. 如何使用fastJson来解析JSON格式数据和生成JSON格式数据

    由于项目用到了JSON格式的数据,在网上搜索到了阿里的fastjson比较好用,特此记录fastjson用法,以备以后查询之用. decode: 首先创建一个JSON解析类: public class ...

  9. python+jinja2实现接口数据批量生成工具

    在做接口测试的时候,我们经常会遇到一种情况就是要对接口的参数进行各种可能的校验,手动修改很麻烦,尤其是那些接口参数有几十个甚至更多的,有没有一种方法可以批量的对指定参数做生成处理呢. 答案是肯定的! ...

随机推荐

  1. 【bzoj4240】 有趣的家庭菜园 树状数组

    这一题最终要构造的序列显然是一个单峰序列 首先有一个结论:一个序列通过交换相邻的元素,进行排序,最少的交换次数为该序列的逆序对个数 (该结论很久之前打表意外发现的,没想到用上了.....) 考虑如何构 ...

  2. Swift 基本数据类型与运算符表达式

    // // main.swift // LessonSwift01 // // Created by lanouhn on 16/1/25. // Copyright © 2016年 齐彦坤. All ...

  3. 使用js栈stack类的实现

    使用js栈stack类的实现 /*使用栈stack类的实现*/ function stack() { this.dataStore = [];//保存栈内元素,初始化为一个空数组 this.top = ...

  4. (转)CentOS7下yum安装mysql配置多实例

    原文:http://blog.csdn.net/poklau/article/details/54951798

  5. python字符串中包含Unicode插入数据库乱码问题 分类: Python 2015-04-28 18:19 342人阅读 评论(0) 收藏

    之前在编码的时候遇到一个奇葩的问题,无论如何操作,写入数据库的字符都是乱码,之后是这样解决的,意思就是先解码,然后再插入数据库 cost_str = json.dumps(cost_info) cos ...

  6. 查看LINUX 系统硬件等详细信息

    转载这位朋友[地址] 几个cpu more /proc/cpuinfo |grep "physical id"|uniq|wc -l 每个cpu是几核(假设cpu配置相同) mor ...

  7. Mac OS配置Android SDK环境变量(当不存在 .bash_profile 文件的时候)

    苹果系统已经包含完整的J2SE,其中就有JDK和JVM(苹果叫VM). 如果要在MAC系统下开发CODE.可以先装个IDE(NETBEANS/Eclipse等),而后不需要装JDK和JVM了,MAC下 ...

  8. linux解压.tar.xz压缩包

    今天,打算更新一下node版本(v6.11.1 -> v8.9.4),结果阿里云服务器使用nvm命令下载慢如牛,于是直接在node官网找到合适的v8.9.4压缩包下载到电脑里,然后up到阿里云服 ...

  9. ELK构建MySQL慢日志收集平台详解

    上篇文章<中小团队快速构建SQL自动审核系统>我们完成了SQL的自动审核与执行,不仅提高了效率还受到了同事的肯定,心里美滋滋.但关于慢查询的收集及处理也耗费了我们太多的时间和精力,如何在这 ...

  10. WPF中触发器(Trigger、DataTrigger)使用动画最简单的方式EnterActions和ExitsActions

    1.当鼠标移入后执行某个动画: <Style TargetType="{x:Type StackPanel}"> <Setter Property="R ...