以下代码从 nomad 服务器获取温度数据,并生成一个 netcdf 文件,其中包含三个维度(时间、纬度和经度)的数据。它工作正常,除非 xmin <0 和 xmax> 0。在这种情况下报告错误:
回溯(最后一次调用):文件“./make_basic.py”,第 79 行,在 tmpvar[:]=var[0,xmin:xmax,ymin:ymax] 文件“netCDF4/_netCDF4.pyx”,第 3695 行,在 netCDF4._netCDF4.Variable 中。getitem (netCDF4/_netCDF4.c:38039)
文件“netCDF4/_netCDF4.pyx”,第 4376 行,在 netCDF4._netCDF4.Variable._get (netCDF4/_netCDF4.c:47286) RuntimeError: NetCDF: DAP server error
如果 xmin <0 和 xmax <0,或两者都大于零,则它可以正常工作。有谁知道什么可能失败?例如,从负坐标到正坐标做一个欧洲的 netcdf 是很重要的,因为 0 是格林威治的子午线。
#! /usr/bin/python
import netCDF4
import numpy as np
import sys
dods='http://nomads.ncep.noaa.gov:9090/dods/gfs_0p25_1hr/gfs20170928/gfs_0p25_1hr_00z'
newfile='sdffile.nc'
xmin=-100
xmax=100
ymin=448
ymax=649
cdata=netCDF4.Dataset(dods,'r')
lon=cdata.variables['lon']
lat=cdata.variables['lat']
time=cdata.variables['time']
var=cdata.variables['tmp2m']
## Set Some Globals
newdata=netCDF4.Dataset(newfile,'w')
newdata.title="GFS"
newdata.Conventions="COARDS"
newdata.dataType='Grid'
newdata.history='Example Self Describing NetCDF File From GrADS-Aholic'
newdata.createDimension('lat', len(lat[ymin:ymax]))
newdata.createDimension('lon', len(lon[xmin:xmax]))
newdata.createDimension('time', 1)
### Create dimensional variables: ###
### For dimension variables, COARDS attributs are required for GrADS self describing
### Do latitude (f8 = float64):
latvar=newdata.createVariable('lat','f8',('lat'))
latvar.grads_dim='y'
latvar.grads_mapping='linear'
latvar.grads_size=len(lat[ymin:ymax])
latvar.units='degrees_north'
latvar.long_name='latitude'
latvar.minimum=str(lat[ymin])
latvar.maximum=str(lat[ymax])
latvar.resolution='0.25'
latvar[:]=lat[ymin:ymax]
### Do longitude (f8 = float64):
lonvar=newdata.createVariable('lon','f8',('lon'))
lonvar.grads_dim='x'
lonvar.grads_mapping='linear'
lonvar.grads_size=len(lon[xmin:xmax])
lonvar.units='degrees_east'
lonvar.long_name='longitude'
lonvar.minimum=str(lon[xmin])
lonvar.maximum=str(lon[xmax])
lonvar.resolution='0.25'
lonvar[:]=lon[xmin:xmax]
### Do Time (f8 = float64):
### Added grads_min and grads_step to attributes
timevar=newdata.createVariable('time','f8',('time'))
timevar.grads_dim='t'
timevar.grads_mapping='linear'
timevar.grads_size=1
timevar.units='days since 1-1-1 00:00:0.0'
timevar.grads_min='00z30dec2015'
timevar.grads_step='3hr'
timevar.long_name='time'
timevar.minimum='00z30dec2015'
timevar.maximum='00z31dec2015'
timevar.resolution='0.125'
timevar[:]=time[0]
### Now that dimensional variables are finished, the other variables are written
### 2 meter temperature (f4=float32) ###
tmpvar=newdata.createVariable('TMP','f4',('time','lat','lon'),fill_value=9.999E20)
tmpvar.long_name='2 Meter Temperature'
tmpvar.units='K'
tmpvar[:]=var[0,xmin:xmax,ymin:ymax]
print 'Done with 2 meter Temperature!'
newdata.close()
cdata.close()
sys.exit()