简体   繁体   English

使用 xarray 剪辑 netcdf

[英]Clip netcdf with xarray

I have a netcdf file that want to clip it based on my desired study area.我有一个 netcdf 文件,想要根据我想要的研究区域对其进行剪辑。 For do that I used xarray as follow.为此,我使用 xarray 如下。

ds = xr.open_dataset(file, group = ‘PRODUCT’)
ds_ir = ds.where((44 < ds.longitude) & (ds.longitude < 65)
& (24 < ds.latitude) & (ds.latitude < 41), drop=True)

src_fname, ext = os.path.splitext(file) # split filename and extension
save_fname = os.path.join(outpath, os.path.basename(src_fname)+’.nc’)
ds_ir.to_netcdf(save_fname)

In netcdf created there is a difference with source netcdf file.在创建的 netcdf 中,与源 netcdf 文件有所不同。 Whith ds.info() :ds.info()

Source netcd file:源 netcd 文件:

xarray.Dataset {
dimensions:
    corner = 4 ;
    ground_pixel = 450 ;
    scanline = 3245 ;
    time = 1 ;

variables:
    float64 scanline(scanline) ;
        scanline:units = 1 ;
        scanline:axis = Y ;
        scanline:long_name = along-track dimension index ;
        scanline:comment = This coordinate variable defines the indices along track; index starts at 0 ;
    float64 ground_pixel(ground_pixel) ;
        ground_pixel:units = 1 ;
        ground_pixel:axis = X ;
        ground_pixel:long_name = across-track dimension index ;
        ground_pixel:comment = This coordinate variable defines the indices across track, from west to east; index starts at 0 ;
    datetime64[ns] time(time) ;
        time:standard_name = time ;
        time:axis = T ;
        time:long_name = reference time for the measurements ;
        time:comment = The time in this variable corresponds to the time in the time_reference global attribute ;
    float64 corner(corner) ;
        corner:units = 1 ;
        corner:long_name = pixel corner index ;
        corner:comment = This coordinate variable defines the indices for the pixel corners; index starts at 0 (counter-clockwise, starting from south-western corner of the pixel in ascending part of the orbit) ;
    float32 latitude(time, scanline, ground_pixel) ;
        latitude:long_name = pixel center latitude ;
        latitude:units = degrees_north ;
        latitude:standard_name = latitude ;
        latitude:valid_min = -90.0 ;
        latitude:valid_max = 90.0 ;
        latitude:bounds = /PRODUCT/SUPPORT_DATA/GEOLOCATIONS/latitude_bounds ;
    float32 longitude(time, scanline, ground_pixel) ;
        longitude:long_name = pixel center longitude ;
        longitude:units = degrees_east ;
        longitude:standard_name = longitude ;
        longitude:valid_min = -180.0 ;
        longitude:valid_max = 180.0 ;
        longitude:bounds = /PRODUCT/SUPPORT_DATA/GEOLOCATIONS/longitude_bounds ;
    timedelta64[ns] delta_time(time, scanline) ;
        delta_time:long_name = offset from reference start time of measurement ;
    object time_utc(time, scanline) ;
        time_utc:long_name = Time of observation as ISO 8601 date-time string ;
    float32 qa_value(time, scanline, ground_pixel) ;
        qa_value:units = 1 ;
        qa_value:valid_min = 0 ;
        qa_value:valid_max = 100 ;
        qa_value:long_name = data quality value ;
        qa_value:comment = A continuous quality descriptor, varying between 0 (no data) and 1 (full quality data). Recommend to ignore data with qa_value < 0.5 ;
    float32 aerosol_index_354_388(time, scanline, ground_pixel) ;
        aerosol_index_354_388:units = 1 ;
        aerosol_index_354_388:proposed_standard_name = ultraviolet_aerosol_index ;
        aerosol_index_354_388:comment = Aerosol index from 388 and 354 nm ;
        aerosol_index_354_388:long_name = Aerosol index from 388 and 354 nm ;
        aerosol_index_354_388:radiation_wavelength = [354. 388.] ;
        aerosol_index_354_388:ancillary_variables = aerosol_index_354_388_precision ;
    float32 aerosol_index_340_380(time, scanline, ground_pixel) ;
        aerosol_index_340_380:units = 1 ;
        aerosol_index_340_380:proposed_standard_name = ultraviolet_aerosol_index ;
        aerosol_index_340_380:comment = Aerosol index from 380 and 340 nm ;
        aerosol_index_340_380:long_name = Aerosol index from 380 and 340 nm ;
        aerosol_index_340_380:radiation_wavelength = [340. 380.] ;
        aerosol_index_340_380:ancillary_variables = aerosol_index_340_380_precision ;
    float32 aerosol_index_354_388_precision(time, scanline, ground_pixel) ;
        aerosol_index_354_388_precision:units = 1 ;
        aerosol_index_354_388_precision:proposed_standard_name = ultraviolet_aerosol_index standard_error ;
        aerosol_index_354_388_precision:comment = Precision of aerosol index from 388 and 354 nm ;
        aerosol_index_354_388_precision:long_name = Precision of aerosol index from 388 and 354 nm ;
        aerosol_index_354_388_precision:radiation_wavelength = [354. 388.] ;
    float32 aerosol_index_340_380_precision(time, scanline, ground_pixel) ;
        aerosol_index_340_380_precision:units = 1 ;
        aerosol_index_340_380_precision:proposed_standard_name = ultraviolet_aerosol_index standard_error ;
        aerosol_index_340_380_precision:comment = Precision of aerosol index from 380 and 340 nm ;
        aerosol_index_340_380_precision:long_name = Precision of aerosol index from 380 and 340 nm ;
        aerosol_index_340_380_precision:radiation_wavelength = [340. 380.] ;

Clipped netcdf:剪辑的netcdf:

xarray.Dataset {
dimensions:
    corner = 4 ;
    ground_pixel = 144 ;
    scanline = 273 ;
    time = 1 ;

variables:
    timedelta64[ns] delta_time(time, scanline, ground_pixel) ;
        delta_time:long_name = offset from reference start time of measurement ;
    object time_utc(time, scanline, ground_pixel) ;
        time_utc:long_name = Time of observation as ISO 8601 date-time string ;
    float32 qa_value(time, scanline, ground_pixel) ;
        qa_value:units = 1 ;
        qa_value:valid_min = 0 ;
        qa_value:valid_max = 100 ;
        qa_value:long_name = data quality value ;
        qa_value:comment = A continuous quality descriptor, varying between 0 (no data) and 1 (full quality data). Recommend to ignore data with qa_value < 0.5 ;
    float32 aerosol_index_354_388(time, scanline, ground_pixel) ;
        aerosol_index_354_388:units = 1 ;
        aerosol_index_354_388:proposed_standard_name = ultraviolet_aerosol_index ;
        aerosol_index_354_388:comment = Aerosol index from 388 and 354 nm ;
        aerosol_index_354_388:long_name = Aerosol index from 388 and 354 nm ;
        aerosol_index_354_388:radiation_wavelength = [354. 388.] ;
        aerosol_index_354_388:ancillary_variables = aerosol_index_354_388_precision ;
    float32 aerosol_index_340_380(time, scanline, ground_pixel) ;
        aerosol_index_340_380:units = 1 ;
        aerosol_index_340_380:proposed_standard_name = ultraviolet_aerosol_index ;
        aerosol_index_340_380:comment = Aerosol index from 380 and 340 nm ;
        aerosol_index_340_380:long_name = Aerosol index from 380 and 340 nm ;
        aerosol_index_340_380:radiation_wavelength = [340. 380.] ;
        aerosol_index_340_380:ancillary_variables = aerosol_index_340_380_precision ;
    float32 aerosol_index_354_388_precision(time, scanline, ground_pixel) ;
        aerosol_index_354_388_precision:units = 1 ;
        aerosol_index_354_388_precision:proposed_standard_name = ultraviolet_aerosol_index standard_error ;
        aerosol_index_354_388_precision:comment = Precision of aerosol index from 388 and 354 nm ;
        aerosol_index_354_388_precision:long_name = Precision of aerosol index from 388 and 354 nm ;
        aerosol_index_354_388_precision:radiation_wavelength = [354. 388.] ;
    float32 aerosol_index_340_380_precision(time, scanline, ground_pixel) ;
        aerosol_index_340_380_precision:units = 1 ;
        aerosol_index_340_380_precision:proposed_standard_name = ultraviolet_aerosol_index standard_error ;
        aerosol_index_340_380_precision:comment = Precision of aerosol index from 380 and 340 nm ;
        aerosol_index_340_380_precision:long_name = Precision of aerosol index from 380 and 340 nm ;
        aerosol_index_340_380_precision:radiation_wavelength = [340. 380.] ;
    float64 scanline(scanline) ;
        scanline:units = 1 ;
        scanline:axis = Y ;
        scanline:long_name = along-track dimension index ;
        scanline:comment = This coordinate variable defines the indices along track; index starts at 0 ;
    float64 ground_pixel(ground_pixel) ;
        ground_pixel:units = 1 ;
        ground_pixel:axis = X ;
        ground_pixel:long_name = across-track dimension index ;
        ground_pixel:comment = This coordinate variable defines the indices across track, from west to east; index starts at 0 ;
    datetime64[ns] time(time) ;
        time:standard_name = time ;
        time:axis = T ;
        time:long_name = reference time for the measurements ;
        time:comment = The time in this variable corresponds to the time in the time_reference global attribute ;
    float64 corner(corner) ;
        corner:units = 1 ;
        corner:long_name = pixel corner index ;
        corner:comment = This coordinate variable defines the indices for the pixel corners; index starts at 0 (counter-clockwise, starting from south-western corner of the pixel in ascending part of the orbit) ;
    float32 latitude(time, scanline, ground_pixel) ;
        latitude:long_name = pixel center latitude ;
        latitude:units = degrees_north ;
        latitude:standard_name = latitude ;
        latitude:valid_min = -90.0 ;
        latitude:valid_max = 90.0 ;
        latitude:bounds = /PRODUCT/SUPPORT_DATA/GEOLOCATIONS/latitude_bounds ;
    float32 longitude(time, scanline, ground_pixel) ;
        longitude:long_name = pixel center longitude ;
        longitude:units = degrees_east ;
        longitude:standard_name = longitude ;
        longitude:valid_min = -180.0 ;
        longitude:valid_max = 180.0 ;
        longitude:bounds = /PRODUCT/SUPPORT_DATA/GEOLOCATIONS/longitude_bounds ;

Question问题

In clipped netcdf file timedelta64[ns] delta_time(time, scanline) converted to imedelta64[ns] delta_time(time, scanline, ground_pixel) , why this happened?在剪辑的 netcdf 文件timedelta64[ns] delta_time(time, scanline)转换为imedelta64[ns] delta_time(time, scanline, ground_pixel) ,为什么会发生这种情况? how can I prevent do it?我怎样才能防止这样做?

It might be a problem of how you select lats and lons.这可能是你如何选择纬度和经度的问题。

Try using using slices for indexing , which might help with your issue (but could be also a file specific problem).尝试使用切片进行索引,这可能有助于解决您的问题(但也可能是特定于文件的问题)。

ds = xr.open_dataset(file)
ds_sel = ds.sel(longitude=slice(44,65), latitude=slice(24,41))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM