簡體   English   中英

將 Fill_Value 插入到 R 中的 nc 文件中

[英]Insert Fill_Value to nc file in R

我正在嘗試將.nc文件轉換為.csv文件,以便在 R 中進行進一步分析,因為我習慣於使用.csv

基本上我認為要解決我的問題(下面有更多詳細信息),我需要將_FillValue添加到.nc文件中,但我嘗試過的一切都不起作用。

按照http://geog.uoregon.edu/bartlein/courses/geog490/week04-netCDF.html#replace-netcdf-fillvalues-with-r-nas中采取的步驟,我已經成功地為許多.nc文件做到了這一點直到第 3.4.3 節。

但是,我最近獲得了對另一個.nc文件的訪問權限,但相同的過程無法正常工作。 我想我已經把它縮小到新的.nc文件中沒有_FillValue的事實。

從外觀上看, _FillValue應該是“9.97e+36”。 我嘗試使用將此數字添加為缺失值

ncin <- nc_open(ncfname, write=T)
dname <- "tas"
Mvalue <- 9.97e+36
ncvar_change_missval(ncin, dname, Mvalue)

這似乎將missing_value:9.97e+36添加到.nc文件中。 但是,當我運行時: tmp_array <- ncvar_get(ncin,dname) tmp_array 仍然有 9.97e+36。

我希望 tmp_array 已經將 9.97e+36 替換為NA ,就像它對它工作的文件所做的那樣。

有沒有辦法可以將 _FillValue 添加到我的文件中,以便用NA替換這些值?

如果需要,這是不工作的文件的信息:

> print(ncin)
File ./data/UKCP18/Mean_air_temperature_(tas)/.nc_files/tas_hadukgrid_uk_1km_mon_201801-201812.nc (NC_FORMAT_NETCDF4):

     9 variables (excluding dimension variables):
        double tas[projection_x_coordinate,projection_y_coordinate,time]   (Contiguous storage)  
            standard_name: air_temperature
            long_name: Mean air temperature
            units: degC
            description: Mean air temperature
            label_units: C
            level: 1.5m
            plot_label: Mean air temperature at 1.5m (C)
            cell_methods: time: mid_range within days time: mean over days
            grid_mapping: transverse_mercator
            coordinates: latitude longitude month_number season_year
            missing_value: 9.97e+36
        int transverse_mercator[]   (Contiguous storage)  
            grid_mapping_name: transverse_mercator
            longitude_of_prime_meridian: 0
            semi_major_axis: 6377563.396
            semi_minor_axis: 6356256.909
            longitude_of_central_meridian: -2
            latitude_of_projection_origin: 49
            false_easting: 4e+05
            false_northing: -1e+05
            scale_factor_at_central_meridian: 0.9996012717
        double time_bnds[bnds,time]   (Contiguous storage)  
        double projection_y_coordinate_bnds[bnds,projection_y_coordinate]   (Contiguous storage)  
        double projection_x_coordinate_bnds[bnds,projection_x_coordinate]   (Contiguous storage)  
        8 byte int month_number[time]   (Contiguous storage)  
            units: 1
            long_name: month_number
        8 byte int season_year[time]   (Contiguous storage)  
            units: 1
            long_name: season_year
        double latitude[projection_x_coordinate,projection_y_coordinate]   (Contiguous storage)  
            units: degrees_north
            standard_name: latitude
        double longitude[projection_x_coordinate,projection_y_coordinate]   (Contiguous storage)  
            units: degrees_east
            standard_name: longitude

     4 dimensions:
        time  Size:12
            axis: T
            bounds: time_bnds
            units: hours since 1800-01-01 00:00:00
            standard_name: time
            calendar: gregorian
        projection_y_coordinate  Size:1450
            axis: Y
            bounds: projection_y_coordinate_bnds
            units: m
            standard_name: projection_y_coordinate
        projection_x_coordinate  Size:900
            axis: X
            bounds: projection_x_coordinate_bnds
            units: m
            standard_name: projection_x_coordinate
        bnds  Size:2

    11 global attributes:
        _NCProperties: version=1|netcdflibversion=4.6.1|hdf5libversion=1.10.2
        comment: Monthly resolution gridded climate observations
        creation_date: 2019-08-09T20:34:33
        frequency: mon
        institution: Met Office
        references: doi: 10.1002/joc.1161
        short_name: monthly_meantemp
        source: HadUK-Grid_v1.0.1.0
        title: Gridded surface climate observations data for the UK
        version: v20190808
        Conventions: CF-1.5

我找到了靈魂。 我想我會在這里發帖,以防有人發現自己也被困住了!

我意識到, missing_value可能不僅僅是9.97e+36 ,而是有更多的小數點。 我運行它以找出完整的缺失值,然后將其設置為缺失missing_value ,這樣missing_value ncvar_get()就可以正常工作了。

ncin <- nc_open(ncfname, write=T)
print(ncin)

tmp_array <- ncvar_get(ncin,dname) # This produced an array with the missing value inserted - should be replaced with NAs

# What is the missing value up to 100 decimal points?!
sprintf("%.100f", tmp_array[1,1,1]) 

# Set missing value
Mvalue <- 9.969209968386869047442886268468442020e+36

# insert missing_value to .nc file
ncvar_change_missval(ncin, dname, Mvalue)
print(ncin)

# make new array with values replaced with NAs
tmp_array <- ncvar_get(ncin,dname)

然后我繼續遵循http://geog.uoregon.edu/bartlein/courses/geog490/week04-netCDF.html#replace-netcdf-fillvalues-with-r-nas中概述的過程,直到 3.4.3 .csv

呼:謝謝大家:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM