python - 从 NETCDF 文件中提取子域并将其写入 python 中的 txt 文件
问题描述
我有一个包含全球风数据的 netcdf 文件,我需要为我的研究区域提取风 UGRD 和 VGRD 的分量(lonmin=-2,lonmax=8,latmin=35 latmax=39)。我需要一个具有这种格式的文本文件:
time series UGRD
VGRD
Example
19790101060000 (year month day hours)
3.28 5.26 (UGRD)
2.23 2.225 (VGRD)
我试图用python做这个操作。我成功地将我的研究区域提取到 nc 文件中,但我仍然尝试将其转换为文本文件,但我失败了。有人可以帮我做吗?
解决方案
import numpy as np
import netCDF4
import netCDF4 as nc
import pandas as pd
import numpy as np
import csv
#### === User-inputs ====#####
one = nc.Dataset('1979.nc') ##load one of your nc datafiles
print one.variables ## Check variables names, say my variable names are lat, lon, pre
## Name of the variables
lat_name = 'latitude'
lon_name = 'longitude'
time_name = 'time'
data_name1 = 'UGRD_10maboveground'
data_name2 = 'VGRD_10maboveground'
## Select spatial range for which data to be extracted
mylat1 = 35
mylat2 = 39
mylon1 = -2
mylon2 = 8
##Give a name of your extracted datafile and define units
newfilename = 'Extracted_Data'
time_unit = 'day since 1979-01-01 00:00'
lat_unit = 'degrees_south'
lon_unit = 'degrees_east'
data_unit = 'm/s'
#### ======= Rest of the Code is Automated ========######
##Find pixel-range based on the provided lat-lon
lat = one.variables[lat_name][:]
lon = one.variables[lon_name][:]
ver_pix = []
for i in xrange(0, len(lat)):
if lat[i] >= mylat1 and lat[i] <= mylat2:
ver_pix.append(i)
y_min = min(ver_pix)
y_max = max(ver_pix)
print lat[min(ver_pix):max(ver_pix)]
hor_pix = []
for j in xrange(0,len(lon)):
if lon[j] >= mylon1 and lon[j] <= mylon2:
hor_pix.append(j)
x_min = min(hor_pix)
x_max = max(hor_pix)
print lon[min(hor_pix):max(hor_pix)]
check_range1 = one.variables[data_name1][:,y_min:y_max,x_min:x_max] ##pre:lat:lon =
time,y,x
check_range2 = one.variables[data_name2][:,y_min:y_max,x_min:x_max]
#print check_range
print check_range1.shape
print check_range2.shape
## Load all nc files in the directory from which data to be extracted
## ..for the selected area
f = nc.MFDataset('1979.nc')
alldata = f.variables[data_name1][:,y_min:y_max,x_min:x_max]
alldata = f.variables[data_name2][:,y_min:y_max,x_min:x_max]
lat1 = one.variables[lat_name][y_min:y_max]
lon1 = one.variables[lon_name][x_min:x_max]
#time = one.variables[time_name][:]
ncfile = nc.Dataset(''+str(newfilename)+'.nc','w')
ncfile.createDimension(time_name,len(alldata))
ncfile.createDimension(lat_name,len(lat1))
ncfile.createDimension(lon_name,len(lon1))
time = ncfile.createVariable(time_name,np.dtype('float32').char,(time_name,))
lats = ncfile.createVariable(lat_name,np.dtype('float32').char,(lat_name,))
lons = ncfile.createVariable(lon_name,np.dtype('float32').char,(lon_name,))
time.units = time_unit
lats.units = lat_unit
lons.units = lon_unit
time[:] = np.linspace(1,len(alldata),len(alldata))
lats[:] = lat1
lons[:] = lon1
newdata1 = ncfile.createVariable(data_name1,np.dtype('float32').char,
(time_name,lat_name,lon_name))
newdata2 = ncfile.createVariable(data_name2,np.dtype('float32').char,
(time_name,lat_name,lon_name))
newdata1.units = data_unit
newdata2.units = data_unit
newdata1[:] = alldata[:]
newdata2[:] = alldata[:]
dtime=netCDF4.num2date(time[:],time.units)
UGRD_ts=pd.Series(data_name2,index=dtime)
UGRD_ts.to_csv('data1.csv', index=True, header=True)
推荐阅读
- intellij-idea - IntelliJ 中缺少日志
- opencl - 在 CPU 上调试 OpenCL
- python - 随机森林 - “完美”的混淆矩阵
- azure - Azure 构建管道失败。找不到依赖包
- hadoop - 我们可以使用 map reduce 程序解决数字的阶乘吗?
- c# - 如何在 IN 查询中使用 PostgreSQL varchar 列值
- go - 如何解析 HTTP 请求
- stackdriver - Which is the api call for retrieving the events of a policy in Google Stackdriver?
- java - Eclipse plugin - issue passing empty command arguments from option preference in plugin.xml
- java - Update of an entity with ttl not working as expected with Datastax Java driver