简体   繁体   English

如何将netCDF提取到特定位置的csv?

[英]How can I extract netCDF to csv for a specific location?

I want to extract time series from a NetCDF to csv for a specific location. 我想从NetCDF到特定位置的csv提取时间序列。 I got this far with the code but it gives the TypeError: a bytes-like object is required, not 'str' 我用代码得到了这么多,但它给出了TypeError:需要一个类似字节的对象,而不是'str'

How can I overcome this issue? 我怎样才能克服这个问题? Also, will this developed code have as output: time/mwp for the specific location? 此外,这个开发的代码是否具有输出:特定位置的时间/ mwp?

import netCDF4 
import pandas as pd
import matplotlib.pyplot as plt
import csv
import numpy as np
from netCDF4 import Dataset, num2date
from pylab import *
import xarray

f = netCDF4.Dataset('Wave_period_global.nc')
f.variables.keys()
print (f)

lat = f.variables['latitude'][:]
lon = f.variables['longitude'][:]
time_var = f.variables['time']
dtime = netCDF4.num2date(time_var[:],time_var.units)
mwp = f.variables['mwp'][:]

print(lon.min(), lon.max())
print(lat.min(), lat.max())

longitude = 172
latitude = 50

def near(array,value):
    idx=(abs(array-value)).argmin()
    return idx

ix = near(lat, latitude)
iy = near(lon, longitude)

print ('Latitude =',ix)
print ('Longitude =',iy)

with open ('Wave_period_global.csv', 'wb') as csvfile:
    filewriter = csv.writer(csvfile, delimiter=',',
                            quotechar='|', quoting=csv.QUOTE_MINIMAL)
    for ln in range(len(lon)):
        for lt in range(len(lat)):
            value=f.variables['mwp'][0][lt][ln]
            dtime = netCDF4.num2date(time_var[0],time_var.units)
            print(dtime,lat[lt],lon[ln,],value)
            filewriter.writerow([dtime,lat[lt],lon[ln,],value])

See my comment above. 请参阅上面的评论。 I'm leaving output as csv out of my answer to focus on your real question. 我将输出作为csv离开我的答案,专注于你真正的问题。

from netCDF4 import Dataset, num2date

f = netCDF4.Dataset('Wave_period_global.nc')

lat = f.variables['latitude'][:]
lon = f.variables['longitude'][:]
time_var = f.variables['time']
# These are all datetime object.
dtime = netCDF4.num2date(time_var[:],time_var.units)
mwp = f.variables['mwp'][:]

print(lon.min(), lon.max())
print(lat.min(), lat.max())

longitude = 172
latitude = 50

def near(array,value):
    idx=(abs(array-value)).argmin()
    return idx

ix = near(lat, latitude)
iy = near(lon, longitude)

print ('Latitude =',ix)
print ('lat[] =', lat[ix])
print ('Longitude =',iy)
print ('lon[] =', lon[iy])

for i in range(len(dtime)):
  value=f.variables['mwp'][i][ix][iy]
  print(dtime[i],lat[ix],lon[iy,],value)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Python 中过滤具有特定条件的 netCDF 变量? - How can I filter netCDF variables with specific conditions in Python? 如何从一个目录中的所有NetCDF文件中提取几个月的子集 - How I can extract a subset of months from all NetCDF files in one directory 如何从 Python 中的 netCDF 提取经度和纬度范围内的数据? - How can I extract data over a range of longitude and latitude from netCDF in Python? 如何更有效地从 python 中的 netCDF 文件中提取数据? - How can I more efficiently extract data from netCDF files in python? 如何从收据的 Google OCR 转储文本 csv 文件中提取特定数据 - How can I extract specific data from an Google OCR dump text csv file of till receipts 如何将数组保存为 NetCDF 文件? - How I can save an array as NetCDF file? 如何提取输入的特定部分? - How can I extract a specific part of an input? 通过 python 从.netCDF 中提取特定位置的数据 - Extracting data for a specific location from netCDF by python 如何提取特定密钥以定位到 dataframe - How to extract specific keys for location to dataframe 在Python中,如何找到CSV文件中信息的位置? - In Python, how can I find the location of information in a CSV file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM