2017-09-30 18 views
2

次のコードは、nomadサーバーから温度データを取得し、このデータを3次元(時間、緯度、経度)でnetcdfファイルを生成します。それは、この場合のエラー報告では< 0とXMAX> 0をXMINとき以外は正常に動作します:Netcdf generator python

Traceback (most recent call last): File "./make_basic.py", line 79, in tmpvar[:]=var[0,xmin:xmax,ymin:ymax] File "netCDF4/_netCDF4.pyx", line 3695, in netCDF4._netCDF4.Variable.getitem (netCDF4/_netCDF4.c:38039)
File "netCDF4/_netCDF4.pyx", line 4376, in netCDF4._netCDF4.Variable._get (netCDF4/_netCDF4.c:47286) RuntimeError: NetCDF: DAP server error

XMIN < 0とXMAX < 0、またはその両方がゼロよりも大きい場合には、それが正常に動作します。誰が何が失敗しているかも知っていますか?例えば、0からグリニッジの子午線であるため、負の座標から正の座標へのヨーロッパのnetcdfを行うことが重要です。

#! /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() 

答えて

0

すべてのインスタンスを作成するときにこれが正しく動作することは確かですか?私はあなたのコードを見てきました。あなたが意図したとおりに行動しているのか疑問があります。

#! /usr/bin/python                                            

# Python package imports 
import sys 

# Third party imports 
import netCDF4 
import numpy as np 

# Package imports 

# code 

dods=('http://nomads.ncep.noaa.gov:9090/dods/gfs_0p25_1hr/'      
     'gfs20180221/gfs_0p25_1hr_00z')           

# I changed the link because the link below is unavailable      

# 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'] 
print(lon) 
print(var) 
## 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' 

print(len(lat[ymin:ymax])) 
print(len(lon[xmin:xmax])) 

newdata.createDimension('lat', len(lat[ymin:ymax])) # this is a potential problem 
newdata.createDimension('lon', len(lon[xmin:xmax])) # this is a potential problem 
newdata.createDimension('time', 1) 

print(newdata.dimensions['lat']) 
print(newdata.dimensions['lon']) 
cdata.close() 
sys.exit() 

これは次の出力を生成します。

Pythonで
$ python stackoverflow.py 
<class 'netCDF4._netCDF4.Variable'> 
float64 lon(lon) 
    grads_dim: x 
    grads_mapping: linear 
    grads_size: 1440 
    units: degrees_east 
    long_name: longitude 
    minimum: 0.0 
    maximum: 359.75 
    resolution: 0.25 
unlimited dimensions: 
current shape = (1440,) 
filling off 

<class 'netCDF4._netCDF4.Variable'> 
float32 tmp2m(time, lat, lon) 
    _FillValue: 9.999e+20 
    missing_value: 9.999e+20 
    long_name: ** 2 m above ground temperature [k] 
unlimited dimensions: 
current shape = (121, 721, 1440) 
filling off 

201 # the length of your 'lat' dimension 
0 # the length of your 'lon' dimension, making it unlimited 
<class 'netCDF4._netCDF4.Dimension'>: name = 'lat', size = 201 

<class 'netCDF4._netCDF4.Dimension'> (unlimited): name = 'lon', size = 0 

array[start:end]は通常、あなたがしたい配列の範囲を選択します。配列は0から始まり1ずつ増加します。配列をスライスするために正と負の数値を混在させるときは、数値の行と同じように動作しません。負の数は配列の最後から数えた数を意味します。正の数は、配列の先頭から右に数えることを意味します。あなたは終わるよりも配列に向かって始めています。これは、長さゼロの配列を作成し、netCDFに無制限の変数を作成するよう指示しています。私はこれがあなたがしたいことだとは思わない。これをより小さなスケールで説明するために、サイズ5の配列を使用します。

xmin = -2 
xmax = 2 

lon = np.arange(5) # array([0, 1, 2, 3, 4]) 
lon[2:-2] # produces a result because you're increasing the array index. 
      # starting at 2 and moving forward to 2 before the end of the array 
      # this selects item 2 because it starts at lon[2] which is 2 and ends 
      # at lon at 2 because it is 2 spaces from the end of the array 
lon[-2:-1] # this works because you're starting two away from the end and moving 
      # to one index away from the end of the array. The starting index 
      # is still smaller than the ending index 
lon[-2:2] # will not work because you are telling the array to start two places 
      # from the end of the array and move to the second spot in the array. 
      # the array is not a queue. It will not hit the tail and start over. 
      # It will move to the second spot from the end of the array and return 
      # an empty array because it is already past the end position you specified. 
      # this returns array([], dtype=int64). calling len() on this gives 0