Обрезать netcdf с помощью xarray

#python #netcdf #python-xarray

#python #netcdf #python-xarray

Вопрос:

У меня есть файл netcdf, который я хочу обрезать в зависимости от моей желаемой области исследования. Для этого я использовал xarray следующим образом.

 ds = xr.open_dataset(file, group = ‘PRODUCT’)
ds_ir = ds.where((44 < ds.longitude) amp; (ds.longitude < 65)
amp; (24 < ds.latitude) amp; (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)
 

В созданном netcdf есть разница с исходным файлом netcdf. Что ds.info() :

Исходный файл 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.] ;
 

Обрезанный 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 ;
 

Вопрос

В обрезанном файле netcdf timedelta64[ns] delta_time(time, scanline) , преобразованном в imedelta64[ns] delta_time(time, scanline, ground_pixel) , почему это произошло? как я могу предотвратить это?

Ответ №1:

Это может быть проблема с тем, как вы выбираете латы и лоны.

Попробуйте использовать использование срезов для индексации, что может помочь с вашей проблемой (но может быть и проблемой, связанной с конкретным файлом).

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