2017-08-16 25 views
2

私は、単純な線グラフを作成するために、次のコードを実行しています:制約の不一致エラー

import matplotlib.pyplot as plt 
import iris 
import iris.coord_categorisation as iriscc 
import iris.plot as iplt 
import iris.quickplot as qplt 
import iris.analysis.cartography 
import matplotlib.dates as mdates 

def main(): 
    #bring in all the files we need and give them a name 
    TestFile= '/exports/csce/datastore/geos/users/s0XXXX/Climate_Modelling/AFR_44_tas/Historical/1950-2005/tas_AFR-44_MOHC-HadGEM2-ES_historical_r1i1p1_CLMcom-CCLM4-8-17_v1_mon_194912-200512.nc' 

    #Load exactly one cube from given file 
    TestFile = iris.load_cube(TestFile) 

    print TestFile 

    #adjust longitude as data is out by 180degrees 
    #remove flat latitude and longitude and only use grid latitude and grid longitude which are in the 3rd and 4th column of the file 

    lats = iris.coords.DimCoord(TestFile.coords()[3].points[:,0], \ 
           standard_name='latitude', units='degrees') 
    lons = TestFile.coords()[4].points[0] 
    for i in range(len(lons)): 
     if lons[i]>100.: 
      lons[i] = lons[i]-360. 
    lons = iris.coords.DimCoord(lons, \ 
           standard_name='longitude', units='degrees') 
    TestFile.remove_coord('latitude') 
    TestFile.remove_coord('longitude') 
    TestFile.remove_coord('grid_latitude') 
    TestFile.remove_coord('grid_longitude') 
    TestFile.add_dim_coord(lats, 1) 
    TestFile.add_dim_coord(lons, 2) 

    #we are only interested in the latitude and longitude relevant to Malawi 

    Malawi = iris.Constraint(longitude=lambda v: 32.5 <= v <= 36., \ 
         latitude=lambda v: -17. <= v <= -9.) 

    TestFile = TestFile.extract(Malawi) 

    #data is in Kelvin, but we would like to show it in Celcius 
    TestFile.convert_units('Celsius') 

    #We are interested in plotting the graph with time along the x ais, so we need a mean of all the coordinates, i.e. mean temperature across whole country  
    iriscc.add_year(TestFile, 'time') 

    TestFile = TestFile.aggregated_by('year', iris.analysis.MEAN) 

    TestFile.coord('latitude').guess_bounds() 
    TestFile.coord('longitude').guess_bounds() 
    TestFile_grid_areas = iris.analysis.cartography.area_weights(TestFile) 
    TestFile_mean = TestFile.collapsed(['latitude', 'longitude'], 
                iris.analysis.MEAN, 
                weights=TestFile_grid_areas) 


    #set major plot indicators for x-axis            
    plt.gca().xaxis.set_major_locator(mdates.YearLocator(5)) 

    #assign the line colours 
    qplt.plot(TestFile_mean, label='TestFile', lw=1.5, color='blue') 

    #create a legend and set its location to under the graph 
    plt.legend(loc="upper center", bbox_to_anchor=(0.5,-0.05), fancybox=True, shadow=True, ncol=5) 

    #create a title 
    plt.title('Mean Near Surface Temperature for Malawi', fontsize=11) 

    #create the graph 
    plt.grid() 

    iplt.show() 

if __name__ == '__main__': 
    main() 

これは、ファイルの大半のためによく働いて、しかし、2つの気候モデル、制約不一致エラーを考え出すされています。

runfile('/exports/csce/datastore/geos/users/s0XXXX/Climate_Modelling/Python Code and Output Images/Line_Graph_Temp_Test.py', wdir='/exports/csce/datastore/geos/users/s0XXXX/Climate_Modelling/Python Code and Output Images') 
Traceback (most recent call last): 

File "<ipython-input-83-4f4457568a8f>", line 1, in <module>  runfile('/exports/csce/datastore/geos/users/s0XXXX/Climate_Modelling/Python Code and Output Images/Line_Graph_Temp_Test.py', wdir='/exports/csce/datastore/geos/users/s0XXXX/Climate_Modelling/Python Code and Output Images')  

File "/usr/lib/python2.7/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 685, in runfile  
execfile(filename, namespace) 

File "/usr/lib/python2.7/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 78, in execfile 
builtins.execfile(filename, *where)  

File "/exports/csce/datastore/geos/users/s0XXXX/Climate_Modelling/Python Code and Output Images/Line_Graph_Temp_Test.py", line 84, in <module> 
main() 

File "/exports/csce/datastore/geos/users/s0XXXX/Climate_Modelling/Python Code and Output Images/Line_Graph_Temp_Test.py", line 21, in main  
TestFile = iris.load_cube(TestFile)  

File "/usr/lib64/python2.7/site-packages/iris/__init__.py", line 338, in load_cube  
raise iris.exceptions.ConstraintMismatchError(str(e))   

ConstraintMismatchError: failed to merge into a single cube.  
    cube.standard_name differs: None != u'air_temperature'  
    cube.long_name differs: None != u'Near-Surface Air Temperature' 
    cube.var_name differs: u'rotated_pole' != u'tas' 
    cube.units differs: Unit('no_unit') != Unit('K') 
    cube.attributes keys differ: 'grid_north_pole_latitude', 'grid_north_pole_longitude', 'grid_mapping_name' 
    cube.cell_methods differ 
    cube.shape differs:() != (660, 201, 194) 
    cube data dtype differs: |S1 != float32 
    cube data fill_value differs: '\x00' != 1e+20 

は同様に、私は(cru_ts4.00.1901.2015.tmp.dat.nc)

ConstraintMismatchError: failed to merge into a single cube. 
    cube.long_name differs: u'near-surface temperature' != None 
    cube.var_name differs: u'tmp' != u'stn' 
    cube.units differs: Unit('degrees Celsius') != Unit('1') 
    cube.attributes keys differ: 'correlation_decay_distance', 'description' 
    cube data dtype differs: float32 != int32 
    cube data fill_value differs: 9.96921e+36 != -2147483647 
観測されたデータを実行しようとすると、このエラーを取得します

私はこれをどのように修正することができますか?

+0

あなたの編集@jpsに感謝します – ErikaAWT

答えて

1

Iris User Google GroupのAndrew Dawsonからの回答を受け取りました。それが誰か他の人に助けになっている場合に備えて、ここに投稿してください。これは私を助けた!

関数iris.load_cubeは、指定された制約に一致するファイルから正確に1つのキューブだけをロードするために使用されます。制約条件を指定していないため、ロードするファイルが正確に1キューブに縮小されることを期待しています。 iris.load_cubeのConstraintMismatchErrorは、データが一致していないため、これが不可能であることを示しています。エラーから、それらのモデルの入力ファイルに1つ以上の変数があるように見えます。あなたは、おそらくのように、ロード時に明示的な制約を追加することを検討すべきである:NAME_OF_VARIABLEはキューブがでロードされるだろう名前でなければなりません

iris.load_cube(filename, 'name_of_variable_here') 

、すなわちcube.nameの結果()。これは、netcdf変数名とは異なります。動作するようにあなたがこれを行う必要があり、どのように私は

cubes = iris.load(the_filename) # load all the cubes in the input file 

で問題のデータセットのいずれかからすべてのキューブをロードして、キューブにキューブのキューブ

の名前を印刷することをお勧め:

print(cube.name()) 
関連する問題