netCDF转换为NaN为条件

时间:2016-08-16 08:49:46

标签: python numpy nan netcdf4

我想将netCDF文件中的值转换为大于零的名为LandMask_NaN的NaN。但是,LandMask和numpy将转换为NaN之间似乎存在类型不匹配。任何帮助非常感谢,代码和信息如下:

 import netCDF4 as nc
 import numpy as np
 import matplotlib.pyplot as plt
 import csv as cs
 import pandas as pd

ncfile = nc.Dataset('C:\Users\mmso2\Google Drive\ENVI_I-PAC_2007_10_21_21_22_47.nc')#office machine

SARwind = ncfile.variables['sar_wind']
ModelWind = ncfile.variables['model_speed']
LON = ncfile.variables['longitude']
LAT = ncfile.variables['latitude']
LandMask = ncfile.variables['mask']

#clean the data of values = 70

SARwind_nan = SARwind[:].copy()
SARwind_nan[SARwind_nan == 0.0] = np.nan
SARwind_nan[SARwind_nan == 70.0] = np.nan

#clear the data of values where there is land
# % pos = land; neg = water

LandMask_NaN = LandMask[:].copy()
#LandMask_NaN[int(float(LandMask_NaN))]### will not convert
LandMask_NaN[LandMask_NaN >0.0] = np.nan #error here

我得到的错误是

#error
line 37, in <module>
LandMask_NaN[LandMask_NaN >= 0.0] = np.nan
ValueError: cannot convert float NaN to integer

尝试时

LandMask_NaN[int(float(LandMask_NaN))] 

LandMask_NaN[float(int(LandMask_NaN))] 

在尝试转换为NaN之前,我得到了

TypeError: only length-1 arrays can be converted to Python scalars

检查LandMask的类型时我得到了

<type 'netCDF4._netCDF4.Variable'>

我不知道如何找出变量类型?

更新:nc变量的详细信息

NetCDF dimension information:
    Name: x
        size: 848
        type:       WARNING: x does not contain variable attributes
    Name: y
        size: 972
        type:       WARNING: y does not contain variable attributes
    Name: xfit
        size: 6
        type:       WARNING: xfit does not contain variable attributes
NetCDF variable information:
    Name: acquisition_time
        dimensions: ()
        size: 1.0
        type: dtype('float64')
        units: u'seconds since 2000-01-01 00:00:00'
        long_name: u'Acqusition time in Julian seconds since 2000-01-01T00:00:00Z'
        standard_name: u'time'
        calendar: u'gregorian'
    Name: nx
        dimensions: ()
        size: 1.0
        type: dtype('int32')
        units: u'1'
        long_name: u'Number of elements in this file'
    Name: ny
        dimensions: ()
        size: 1.0
        type: dtype('int32')
        units: u'1'
        long_name: u'Number of lines in this file'
    Name: nx0
        dimensions: ()
        size: 1.0
        type: dtype('int32')
        units: u'1'
        long_name: u'Number of elements in SIO file'
    Name: ny0
        dimensions: ()
        size: 1.0
        type: dtype('int32')
        units: u'1'
        long_name: u'Number of lines in SIO file'
    Name: nx00
        dimensions: ()
        size: 1.0
        type: dtype('int32')
        units: u'1'
        long_name: u'Number of elements in original SAR file'
    Name: ny00
        dimensions: ()
        size: 1.0
        type: dtype('int32')
        units: u'1'
        long_name: u'Number of lines in original SAR file'
    Name: xn
        dimensions: ()
        size: 1.0
        type: dtype('float32')
        units: u'1'
        long_name: u'1'
    Name: yn
        dimensions: ()
        size: 1.0
        type: dtype('float32')
        units: u'1'
        long_name: u'1'
    Name: line_size
        dimensions: ()
        size: 1.0
        type: dtype('float32')
        units: u'm'
        long_name: u'Line size'
    Name: pixel_size
        dimensions: ()
        size: 1.0
        type: dtype('float32')
        units: u'm'
        long_name: u'Pixel size'
    Name: model_time_js
        dimensions: ()
        size: 1.0
        type: dtype('float64')
        units: u'seconds since 2000-01-01 00:00:00'
        long_name: u'Model time julian seconds since 2000-01-01T00:00:00Z'
    Name: model_time_js_tau
        dimensions: ()
        size: 1.0
        type: dtype('float64')
        units: u'seconds since 2000-01-01 00:00:00'
        long_name: u'Model time plus tau julian seconds since 2000-01-01T00:00:00Z'
    Name: upper_left_longitude
        dimensions: ()
        size: 1.0
        type: dtype('float32')
        units: u'degrees_east'
        long_name: u'degrees'
    Name: upper_right_longitude
        dimensions: ()
        size: 1.0
        type: dtype('float32')
        units: u'degrees_east'
        long_name: u'degrees'
    Name: upper_left_latitude
        dimensions: ()
        size: 1.0
        type: dtype('float32')
        units: u'degrees_north'
        long_name: u'degrees'
    Name: upper_right_latitude
        dimensions: ()
        size: 1.0
        type: dtype('float32')
        units: u'degrees_north'
        long_name: u'degrees'
    Name: start_center_longitude
        dimensions: ()
        size: 1.0
        type: dtype('float32')
        units: u'degrees_east'
        long_name: u'degrees'
    Name: start_center_latitude
        dimensions: ()
        size: 1.0
        type: dtype('float32')
        units: u'degrees_north'
        long_name: u'degrees'
    Name: scene_center_longitude
        dimensions: ()
        size: 1.0
        type: dtype('float32')
        units: u'degrees_east'
        long_name: u'degrees'
    Name: scene_center_latitude
        dimensions: ()
        size: 1.0
        type: dtype('float32')
        units: u'degrees_north'
        long_name: u'degrees'
    Name: lower_left_longitude
        dimensions: ()
        size: 1.0
        type: dtype('float32')
        units: u'degrees_east'
        long_name: u'degrees'
    Name: lower_right_longitude
        dimensions: ()
        size: 1.0
        type: dtype('float32')
        units: u'degrees_east'
        long_name: u'degrees'
    Name: lower_left_latitude
        dimensions: ()
        size: 1.0
        type: dtype('float32')
        units: u'degrees_north'
        long_name: u'degrees'
    Name: lower_right_latitude
        dimensions: ()
        size: 1.0
        type: dtype('float32')
        units: u'degrees_north'
        long_name: u'degrees'
    Name: end_center_longitude
        dimensions: ()
        size: 1.0
        type: dtype('float32')
        units: u'degrees_east'
        long_name: u'degrees'
    Name: end_center_latitude
        dimensions: ()
        size: 1.0
        type: dtype('float32')
        units: u'degrees_north'
        long_name: u'degrees'
    Name: northernmost_latitude
        dimensions: ()
        size: 1.0
        type: dtype('float32')
        units: u'degrees_north'
        long_name: u'degrees'
    Name: southernmost_latitude
        dimensions: ()
        size: 1.0
        type: dtype('float32')
        units: u'degrees_north'
        long_name: u'degrees'
    Name: easternmost_longitude
        dimensions: ()
        size: 1.0
        type: dtype('float32')
        units: u'degrees_east'
        long_name: u'degrees'
    Name: westernmost_longitude
        dimensions: ()
        size: 1.0
        type: dtype('float32')
        units: u'degrees_east'
        long_name: u'degrees'
    Name: nrcs_slope
        dimensions: ()
        size: 1.0
        type: dtype('float32')
    Name: nrcs_bias
        dimensions: ()
        size: 1.0
        type: dtype('float32')
    Name: sigma
        dimensions: (u'y', u'x')
        size: 824256
        type: dtype('float32')
        units: u'1'
        long_name: u'Normalized Radar Cross Section.'
        coordinates: u'longitude latitude'
    Name: sar_wind
        dimensions: (u'y', u'x')
        size: 824256
        type: dtype('float32')
        units: u'm s-1'
        long_name: u'SAR-derived wind speed at 10-m height neutral stability'
        standard_name: u'wind_speed'
        coordinates: u'longitude latitude'
    Name: input_dir
        dimensions: (u'y', u'x')
        size: 824256
        type: dtype('float32')
        units: u'degrees'
        long_name: u'Interpolated directions used for wind inversion'
        coordinates: u'longitude latitude'
    Name: model_speed
        dimensions: (u'y', u'x')
        size: 824256
        type: dtype('float32')
        units: u'm s-1'
        long_name: u'Interpolated model wind speed (=1 for non model directions)'
        standard_name: u'wind_speed'
        coordinates: u'longitude latitude'
    Name: mask
        dimensions: (u'y', u'x')
        size: 824256
        type: dtype('int16')
        units: u'1'
        long_name: u'Interpolated land mask distance from shore line. Positive values land / Negative value water'
        flag_values: array([-1,  0,  1], dtype=int16)
        flag_meanings: u'water shore land'
        coordinates: u'longitude latitude'
    Name: longitude
        dimensions: (u'y', u'x')
        size: 824256
        type: dtype('float32')
        units: u'degrees_east'
        long_name: u'Longitude array in decimal degrees'
        standard_name: u'longitude'
    Name: latitude
        dimensions: (u'y', u'x')
        size: 824256
        type: dtype('float32')
        units: u'degrees_north'
        long_name: u'Latitude array in decimal degrees'
        standard_name: u'latitude'
    Name: rlook
        dimensions: (u'y', u'x')
        size: 824256
        type: dtype('float32')
        units: u'degrees'
        long_name: u'Radar look direction array in decimal degrees from North'
        coordinates: u'longitude latitude'
    Name: incid
        dimensions: (u'y', u'x')
        size: 824256
        type: dtype('float32')
        units: u'degrees'
        long_name: u'Incident angle array in degrees from nadir'
        coordinates: u'longitude latitude'
    Name: icemask
        dimensions: (u'y', u'x')
        size: 824256
        type: dtype('int16')
        units: u'1'
        long_name: u'Ice mask 0=no_data 1=water 2=land 3=sea_ice 4=snow'
        flag_values: array([0, 1, 2, 3, 4], dtype=int16)
        flag_meanings: u'no_data water land sea_ice snow'
        coordinates: u'longitude latitude'
    Name: lon_coef
        dimensions: (u'xfit',)
        size: 6
        type: dtype('float64')
        units: u'1'
        long_name: u'Coefficients to compute longitude in degs from pixel/lines'
    Name: lon_xexp
        dimensions: (u'xfit',)
        size: 6
        type: dtype('float32')
        units: u'1'
        long_name: u'Longitude pixel exponents'
    Name: lon_yexp
        dimensions: (u'xfit',)
        size: 6
        type: dtype('float32')
        units: u'1'
        long_name: u'Longitude line exponents'
    Name: lat_coef
        dimensions: (u'xfit',)
        size: 6
        type: dtype('float64')
        units: u'1'
        long_name: u'Coefficients to compute latitude in degs from pixel/lines'
    Name: lat_xexp
        dimensions: (u'xfit',)
        size: 6
        type: dtype('float32')
        units: u'1'
        long_name: u'Latitude pixel exponents'
    Name: lat_yexp
        dimensions: (u'xfit',)
        size: 6
        type: dtype('float32')
        units: u'1'
        long_name: u'Latitude line exponents'
    Name: i_coef
        dimensions: (u'xfit',)
        size: 6
        type: dtype('float64')
        units: u'1'
        long_name: u'Coefficients to compute pixel from longitue/latitude'
    Name: i_xexp
        dimensions: (u'xfit',)
        size: 6
        type: dtype('float32')
        units: u'1'
        long_name: u'Pixel longitude exponents'
    Name: i_yexp
        dimensions: (u'xfit',)
        size: 6
        type: dtype('float32')
        units: u'1'
        long_name: u'Pixel latitude exponents'
    Name: j_coef
        dimensions: (u'xfit',)
        size: 6
        type: dtype('float64')
        units: u'1'
        long_name: u'Coefficients to compute line from longitue/latitude'
    Name: j_xexp
        dimensions: (u'xfit',)
        size: 6
        type: dtype('float32')
        units: u'1'
        long_name: u'Line longitude exponents'
    Name: j_yexp
        dimensions: (u'xfit',)
        size: 6
        type: dtype('float32')
        units: u'1'
        long_name: u'Line latitude exponents'
    Name: incid_coef
        dimensions: (u'xfit',)
        size: 6
        type: dtype('float64')
        units: u'1'
        long_name: u'Coefficients to compute incid in degs from pixel/lines'
    Name: incid_xexp
        dimensions: (u'xfit',)
        size: 6
        type: dtype('float32')
        units: u'1'
        long_name: u'Incid pixel exponents'
    Name: incid_yexp
        dimensions: (u'xfit',)
        size: 6
        type: dtype('float32')
        units: u'1'
        long_name: u'Incid line exponents'
    Name: rlook_coef
        dimensions: (u'xfit',)
        size: 6
        type: dtype('float64')
        units: u'1'
        long_name: u'Coefficients to compute radar look direction in degs from pixel/lines'
    Name: rlook_xexp
        dimensions: (u'xfit',)
        size: 6
        type: dtype('float32')
        units: u'1'
        long_name: u'Radar look direction pixel exponents'
    Name: rlook_yexp
        dimensions: (u'xfit',)
        size: 6
        type: dtype('float32')
        units: u'1'
        long_name: u'Radar look direction line exponents'

1 个答案:

答案 0 :(得分:1)

如果您分享netcdf文件,它会有所帮助,但这里有一些关于发生了什么的想法:

当前没有将变量作为numpy数组读入。您需要添加索引参数以将它们转换为数组。没有文件,我不确定它们是什么,但肯定有些是多维的。例如:

SARwind = ncfile.variables['sar_wind'][:,:]
ModelWind = ncfile.variables['model_speed'][:,:]
LON = ncfile.variables['longitude'][:]
LAT = ncfile.variables['latitude'][:]
LandMask = ncfile.variables['mask'][:,:]

然后,您只需将SARwind分配给新变量SARWind_nan并输入nan值,就像您一直在做的那样。

SARWind_nan = SARwind
SARwind_nan[SARwind_nan == 0.0] = np.nan
SARwind_nan[SARwind_nan == 70.0] = np.nan

LandMask正确读入(再次,我认为这很可能是2D,而不是1D),您可以类似地分配给新变量LandMask_nan并输入{{1} }值。仔细检查nan的类型,它可能是一个整数。 ncdumpncks是检查netcdf内容的好工具。

LandMask