如何从 R 中的多个 Aqua Modis netCDF 文件中提取变量?

How to Extract Variables from Multiple Aqua Modis netCDF files in R?

问题:

我在 R 中有一个代码,它 从单个 Aqua Modis netCDF 文件(见下文)中提取每月海面温度 (SST) 值。但是,我在一个文件夹中有一批 59 个 Aqua Modis netCDF 文件。

目标:

我的目标是从所有 59 个 netCDF 文件 的每个 netCDF 文件中提取变量的经度、纬度和 SST,将它们转换使用函数 stack::raster() 转换为光栅文件,然后处理这些文件。

我的数据框有 650 行,它们是 海豚 ID。 我想提取 平均 SST 2016-2021时间段内的每个海豚 ID。一旦我提取了每个海豚 ID 的平均值 SST,我想将这些值写入一个 .csv 文件,文件名为 ' Average_SST'.

很遗憾,由于所有权问题,我无法共享我的数据。

我在空间分析方面完全是个新手,近 4 天来我一直在尝试通过观看 YouTube、阅读教程和 Stack 来解决这个难题溢出。我现在感到非常困惑,我相信我必须遍历所有文件才能实现我的 objective.

如果有人能帮忙,我先谢谢你了。

R-代码:

打开文件夹中的所有 59 个 Aqua Modis netCDF 文件

##Open packages needed for our analysis
library(ncdf4)
library(terra)
library('RNetCDF')
library(raster)
library(sp)

filenames = list.files('~/Documents/Ocean_ColorSST_2016_2021',pattern='*.nc',full.names=TRUE)

##Open the 70 Aqua Modis netCDF files from their folder
SST <- nc_open(filenames[59])

##Print the results for SST
print(SST)

结果:

     3 variables (excluding dimension variables):
        short sst[lon,lat]   (Chunking: [87,44])  (Compression: shuffle,level 4)
            long_name: Sea Surface Temperature
            scale_factor: 0.00499999988824129
            add_offset: 0
            units: degree_C
            standard_name: sea_surface_temperature
            _FillValue: -32767
            valid_min: -1000
            valid_max: 10000
            display_scale: linear
            display_min: -2
            display_max: 45
        unsigned byte qual_sst[lon,lat]   (Chunking: [87,44])  (Compression: shuffle,level 4)
            long_name: Quality Levels, Sea Surface Temperature
            _FillValue: 255
            valid_min: 0
            valid_max: 5
        unsigned byte palette[eightbitcolor,rgb]   (Contiguous storage)  

     4 dimensions:
        lat  Size:4320 
            long_name: Latitude
            units: degrees_north
            standard_name: latitude
            _FillValue: -999
            valid_min: -90
            valid_max: 90
        lon  Size:8640 
            long_name: Longitude
            units: degrees_east
            standard_name: longitude
            _FillValue: -999
            valid_min: -180
            valid_max: 180
        rgb  Size:3 (no dimvar)
        eightbitcolor  Size:256 (no dimvar)
[1] ">>>> WARNING <<<  attribute data_bins is an 8-byte value, but R"
[1] "does not support this data type. I am returning a double precision"
[1] "floating point, but you must be aware that this could lose precision!"

    61 global attributes:
        product_name: AQUA_MODIS.20210901_20210930.L3m.MO.SST.sst.4km.nc
        instrument: MODIS
        title: MODISA Level-3 Standard Mapped Image
        project: Ocean Biology Processing Group (NASA/GSFC/OBPG)
        platform: Aqua
        temporal_range: month
        processing_version: R2019.0
        date_created: 2021-12-03T08:21:22.000Z
        history: l3mapgen par=AQUA_MODIS.20210901_20210930.L3m.MO.SST.sst.4km.nc.param 
        l2_flag_names: LAND,HISOLZEN
        time_coverage_start: 2021-09-01T00:45:00.000Z
        time_coverage_end: 2021-10-01T02:55:00.000Z
        start_orbit_number: 102808
        end_orbit_number: 103246
        map_projection: Equidistant Cylindrical
        latitude_units: degrees_north
        longitude_units: degrees_east
        northernmost_latitude: 90
        southernmost_latitude: -90
        westernmost_longitude: -180
        easternmost_longitude: 180
        geospatial_lat_max: 90
        geospatial_lat_min: -90
        geospatial_lon_max: 180
        geospatial_lon_min: -180
        latitude_step: 0.0416666679084301
        longitude_step: 0.0416666679084301
        sw_point_latitude: -89.9791641235352
        sw_point_longitude: -179.97917175293
        spatialResolution: 4.64 km
        geospatial_lon_resolution: 0.0416666679084301
        geospatial_lat_resolution: 0.0416666679084301
        geospatial_lat_units: degrees_north
        geospatial_lon_units: degrees_east
        number_of_lines: 4320
        number_of_columns: 8640
        measure: Mean
        suggested_image_scaling_minimum: -2
        suggested_image_scaling_maximum: 45
        suggested_image_scaling_type: LINEAR
        suggested_image_scaling_applied: No
        _lastModified: 2021-12-03T08:21:22.000Z
        Conventions: CF-1.6 ACDD-1.3
        institution: NASA Goddard Space Flight Center, Ocean Ecology Laboratory, Ocean Biology Processing Group
        standard_name_vocabulary: CF Standard Name Table v36
        naming_authority: gov.nasa.gsfc.sci.oceandata
        id: AQUA_MODIS.20210901_20210930.L3b.MO.SST.nc/L3/AQUA_MODIS.20210901_20210930.L3b.MO.SST.nc
        license: https://science.nasa.gov/earth-science/earth-science-data/data-information-policy/
        creator_name: NASA/GSFC/OBPG
        publisher_name: NASA/GSFC/OBPG
        creator_email: data@oceancolor.gsfc.nasa.gov
        publisher_email: data@oceancolor.gsfc.nasa.gov
        creator_url: https://oceandata.sci.gsfc.nasa.gov
        publisher_url: https://oceandata.sci.gsfc.nasa.gov
        processing_level: L3 Mapped
        cdm_data_type: grid
        keywords: Earth Science > Oceans > Ocean Optics > Sea Surface Temperature
        keywords_vocabulary: NASA Global Change Master Directory (GCMD) Science Keywords
        data_bins: 20227868
        data_minimum: -1.80000007152557
        data_maximum: 40.0000038146973
> SST_brick <- brick(list[59], "sst")
Error in h(simpleError(msg, call)) : 
  error in evaluating the argument 'x' in selecting a method for function 'brick': object of type 'builtin' is not subsettable
> ##Print the results for SST
> print(SST)
File /Users/kirstymedcalf/Documents/DMAD/Publication/DMAD_Maps_Analysis_Publication/Montenegro_Final_Analysis_Folders/Ocean_ColorSST_2016_2021/AQUA_MODIS.20210901_20210930.L3m.MO.SST.sst.4km.nc (NC_FORMAT_NETCDF4):

     3 variables (excluding dimension variables):
        short sst[lon,lat]   (Chunking: [87,44])  (Compression: shuffle,level 4)
            long_name: Sea Surface Temperature
            scale_factor: 0.00499999988824129
            add_offset: 0
            units: degree_C
            standard_name: sea_surface_temperature
            _FillValue: -32767
            valid_min: -1000
            valid_max: 10000
            display_scale: linear
            display_min: -2
            display_max: 45
        unsigned byte qual_sst[lon,lat]   (Chunking: [87,44])  (Compression: shuffle,level 4)
            long_name: Quality Levels, Sea Surface Temperature
            _FillValue: 255
            valid_min: 0
            valid_max: 5
        unsigned byte palette[eightbitcolor,rgb]   (Contiguous storage)  

     4 dimensions:
        lat  Size:4320 
            long_name: Latitude
            units: degrees_north
            standard_name: latitude
            _FillValue: -999
            valid_min: -90
            valid_max: 90
        lon  Size:8640 
            long_name: Longitude
            units: degrees_east
            standard_name: longitude
            _FillValue: -999
            valid_min: -180
            valid_max: 180
        rgb  Size:3 (no dimvar)
        eightbitcolor  Size:256 (no dimvar)
[1] ">>>> WARNING <<<  attribute data_bins is an 8-byte value, but R"
[1] "does not support this data type. I am returning a double precision"
[1] "floating point, but you must be aware that this could lose precision!"

    61 global attributes:
        product_name: AQUA_MODIS.20210901_20210930.L3m.MO.SST.sst.4km.nc
        instrument: MODIS
        title: MODISA Level-3 Standard Mapped Image
        project: Ocean Biology Processing Group (NASA/GSFC/OBPG)
        platform: Aqua
        temporal_range: month
        processing_version: R2019.0
        date_created: 2021-12-03T08:21:22.000Z
        history: l3mapgen par=AQUA_MODIS.20210901_20210930.L3m.MO.SST.sst.4km.nc.param 
        l2_flag_names: LAND,HISOLZEN
        time_coverage_start: 2021-09-01T00:45:00.000Z
        time_coverage_end: 2021-10-01T02:55:00.000Z
        start_orbit_number: 102808
        end_orbit_number: 103246
        map_projection: Equidistant Cylindrical
        latitude_units: degrees_north
        longitude_units: degrees_east
        northernmost_latitude: 90
        southernmost_latitude: -90
        westernmost_longitude: -180
        easternmost_longitude: 180
        geospatial_lat_max: 90
        geospatial_lat_min: -90
        geospatial_lon_max: 180
        geospatial_lon_min: -180
        latitude_step: 0.0416666679084301
        longitude_step: 0.0416666679084301
        sw_point_latitude: -89.9791641235352
        sw_point_longitude: -179.97917175293
        spatialResolution: 4.64 km
        geospatial_lon_resolution: 0.0416666679084301
        geospatial_lat_resolution: 0.0416666679084301
        geospatial_lat_units: degrees_north
        geospatial_lon_units: degrees_east
        number_of_lines: 4320
        number_of_columns: 8640
        measure: Mean
        suggested_image_scaling_minimum: -2
        suggested_image_scaling_maximum: 45
        suggested_image_scaling_type: LINEAR
        suggested_image_scaling_applied: No
        _lastModified: 2021-12-03T08:21:22.000Z
        Conventions: CF-1.6 ACDD-1.3
        institution: NASA Goddard Space Flight Center, Ocean Ecology Laboratory, Ocean Biology Processing Group
        standard_name_vocabulary: CF Standard Name Table v36
        naming_authority: gov.nasa.gsfc.sci.oceandata
        id: AQUA_MODIS.20210901_20210930.L3b.MO.SST.nc/L3/AQUA_MODIS.20210901_20210930.L3b.MO.SST.nc
        license: https://science.nasa.gov/earth-science/earth-science-data/data-information-policy/
        creator_name: NASA/GSFC/OBPG
        publisher_name: NASA/GSFC/OBPG
        creator_email: data@oceancolor.gsfc.nasa.gov
        publisher_email: data@oceancolor.gsfc.nasa.gov
        creator_url: https://oceandata.sci.gsfc.nasa.gov
        publisher_url: https://oceandata.sci.gsfc.nasa.gov
        processing_level: L3 Mapped
        cdm_data_type: grid
        keywords: Earth Science > Oceans > Ocean Optics > Sea Surface Temperature
        keywords_vocabulary: NASA Global Change Master Directory (GCMD) Science Keywords
        data_bins: 20227868
        data_minimum: -1.80000007152557
        data_maximum: 40.0000038146973

##从每个 Aqua Modis 文件中提取 SST、经度和纬度值,为感兴趣的变量创建 59 个列表。但是,我认为我只是提取单个 Aqua Modis 文件的值 [1]

SST_filenames <- ncvar_get(SST, "sst")
lon_filenames <- ncvar_get(SST, "lon")
lat_filenames <- ncvar_get(SST, "lat")

我想我会尝试 brick() 函数但没有成功,因为我仍然认为我是从单个 Aqua Modis 文件中提取值 [1]。 .

SST_brick <- brick(filenames[59], "sst")
lon_brick <- brick(filenames[59], "lon")
lat_brick <- brick(filenames[59], "lat")

SST_brick 的结果 <- brick(filenames[59], "sst"):

   lass      : RasterBrick 
    dimensions : 4320, 8640, 37324800, 1  (nrow, ncol, ncell, nlayers)
    resolution : 0.04166667, 0.04166667  (x, y)
    extent     : -180, 180, -90.00001, 90  (xmin, xmax, ymin, ymax)
    crs        : +proj=longlat +datum=WGS84 +no_defs 
    source     : AQUA_MODIS.20210901_20210930.L3m.MO.SST.sst.4km.nc 
    names      : layer 
    varname    : sst 

如果我使用对象 SST,我想我仍然在打开单个 Aqua Modis netCDF 文件的值 [1]

SST_filenames <- ncvar_get(SST, "sst")
lon_filenames <- ncvar_get(SST, "lon")
lat_filenames <- ncvar_get(SST, "lat")

您似乎误解了 RasterStackSpatRaster 对象是什么。你打印了 SST_brick ,它清楚地表明它知道经度和纬度。您不需要其他任何东西。

library(terra)
filenames = list.files('~/Documents/Ocean_ColorSST_2016_2021',pattern='*.nc',full.names=TRUE)

并假设 dolphins 是一个矩阵或 data.frame 具有变量“经度”和“纬度”,您可以像这样对一个文件执行此操作:

SST <- rast(filenames[1], "sst")
e <- extract(SST, dolphins[, c("longitude", "latitude")])

但你或许可以像这样一口气做到这一点

SSTs <- rast(filenames, "sst")
e <- extract(SSTs, dolphins[, c("longitude", "latitude")])