python - 从域 ncfile 中提取数据

标签 python coordinates netcdf nco cdo-climate

我试图从由纬度/经度边界定义的用户指定(非矩形)域中的每个网格单元中提取数据。我的输入文件位于曲线网格上。我尝试了各种方法python,cdo,ncks,但我仍然无法弄清楚。我只想要输入 ncfile 的多边形域子集中每个网格单元的时间序列信息。我输入的 ncfile 信息如下:

$ ncdump -h 1979_sfc_out.nc 

netcdf \1979_sfc_out {
dimensions:
    x = 83 ;
    y = 94 ;
    time = UNLIMITED ; // (8736 currently)
    nv4 = 4 ;
variables:
    float time(time) ;
        time:axis = "T" ;
        time:long_name = "time" ;
        time:standard_name = "time" ;
        time:units = "hours since 1979-1-2 00:00:00" ;
        time:calendar = "standard" ;
    float x(x) ;
        x:axis = "x" ;
        x:long_name = "X-coordinate in Cartesian system" ;
        x:standard_name = "projection_x_coordinate" ;
        x:units = "meters" ;
    float y(y) ;
        y:axis = "y" ;
        y:long_name = "Y-coordinate in Cartesian system" ;
        y:standard_name = "projection_y_coordinate" ;
        y:units = "meters" ;
    float lon(y, x) ;
        lon:units = "degrees_east" ;
        lon:valid_range = -180., 180. ;
        lon:standard_name = "longitude" ;
        lon:bounds = "lon_bnds" ;
    float lat(y, x) ;
        lat:units = "degrees_north" ;
        lat:valid_range = -90., 90. ;
        lat:standard_name = "latitude" ;
        lat:bounds = "lat_bnds" ;
    float lon_bnds(y, x, nv4) ;
        lon_bnds:units = "degreesE" ;
    float lat_bnds(y, x, nv4) ;
        lat_bnds:units = "degreesN" ;
    char mapping ;
        mapping:false_easting = 0. ;
        mapping:false_northing = 0. ;
        mapping:grid_mapping_name = "polar_stereographic" ;
        mapping:latitude_of_projection_origin = 90. ;
        mapping:standard_parallel = 64. ;
        mapping:straight_vertical_longitude_from_pole = -152. ;
        mapping:semi_major_axis = 6370000. ;
        mapping:semi_minor_axis = 6370000. ;
    float SEAICE(time, y, x) ;
        SEAICE:_FillValue = -9999.f ;
        SEAICE:units = "fraction" ;
        SEAICE:long_name = "Ice concentration (ice=1;no ice=0)" ;
        SEAICE:grid_mapping = "mapping" ;
        SEAICE:coordinates = "lon lat" ;

我尝试过的一些事情是

lat1=71.2
lat2=72.9
lon1=-176.5
lon2=-160

cdo sellonlatbox,lon1,lon2,lat1,lat2 $ifile $box1_ofile
cdo sellonlatbox (Abort): Float parameter >lon1< contains invalid character at position 1!

我认为问题是我的输入文件的 x,y 尺寸以米为单位(没有负号,可能是“位置 1 处的无效字符”),并且我要求 cdo 提取纬度/lon 尺寸(以度为单位)。我的 ncfile 中有变量“mapping”,它可能有助于将米转换为纬度/经度,但我不知道如何做到这一点。

ncks 在这里对我不起作用,可能是由于我在 cdo 中看到的相同的米 <-> 经/纬度问题。

ncks -v SEAICE,U10,V10 -d latitude,71.2,72.9 -d longitude,-176.5,-160. $ifile -O $box1_ofile

ncks: ERROR dimension latitude is not in input file

虽然我想要提取一个多边形,但我尝试过的这些例子只是矩形子集,我认为要获得多边形,我可以做多个矩形子集来实现我的最终多边形形状,但如果有更好的方法来做到这一点如有任何建议,我们将不胜感激。

谢谢

最佳答案

NCO的辅助坐标feature被设计为具有一维纬度和经度的非结构化网格上的超板曲线坐标。试试这个:

ncks -X lon_min,lon_max,lat_min,lat_max in.nc out.nc
ncks -X -176.5,-160.,71.2,72.9 in.nc out.nc

您还可以菊花链多个 -X 选项来获得奇怪的多边形。不幸的是,这可能对你不起作用,因为我刚刚注意到,你有一个带有二维纬度和经度的曲线网格。为此,请尝试 ncap2 其中 feature 。手册中的示例展示了如何沿矩形边界进行 mask ,并且您可以以菊花链方式连接 where() 语句中的条件以获得多边形。这不会改变输出文件的维度,但它允许您将多边形之外的所有内容设置为 _FillValue。

关于python - 从域 ncfile 中提取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43577444/

相关文章:

python - 使字符串成为有效的文件名,以后可以将其视为原始字符串

python - 如何使用不同的类和导入动态地使用 Python 日志更改文件句柄

Python,在字母列表中找到多个索引

python - Python 中的 AWS Lambda : Import parent package/directory in Lambda function handler

java - 如何从 Java 中的图形中读取坐标?

c# - 有没有办法根据坐标获取地名?

arrays - 在坐标列表中查找最右上角的坐标

java - 使用netcdf-java库或java中的其他方法访问OPeNDAP数据

python - python中高效读取netcdf变量

python - 如何将 netcdf4-python 安装到 ubuntu14.04?