r 测试R文件到源

测试R文件到源

test.R
print("hello")

r netcdf_08

netcdf_08.r
nc_close(nc)

r netcdf_07

netcdf_07.r
# Retrieve the attributes
nc_atts <- ncatt_get(nc, 0)

# List all the attributes (commented to save space).
# names(nc_atts)

# Retrieve the start and end date-times
date_time_start <- as.POSIXct(nc_atts$start_time, format = "%Y%m%dT%H%M%SZ", tz = "UTC")
date_time_end <- as.POSIXct(nc_atts$end_time, format = "%Y%m%dT%H%M%SZ", tz = "UTC")

r netcdf_06

netcdf_06.r
# a quick look at a (pseudo) random section of this data
chla_mean[35:37, 245:247]

##           [,1]      [,2]      [,3]
## [1,] 0.1415056 0.1358830 0.1303250
## [2,] 0.1339336 0.1328560 0.1255906
## [3,] 0.1358152 0.1276533 0.1243571

# Change the dimension names of our matrix to "lon" and "lat", 
# and the row and column names to the latitude and longitude values.
dimnames(chla_mean) <- list(lon=nc_lon, lat=nc_lat)
chla_mean[35:37, 245:247]

##                   lat
## lon                -36.4791717529297 -36.5208358764648 -36.5625038146973
##   16.6458396911621         0.1415056         0.1358830         0.1303250
##   16.6875057220459         0.1339336         0.1328560         0.1255906
##   16.7291717529297         0.1358152         0.1276533         0.1243571

# lastly, you may want to transpose this matrix.
chla_mean <- t(chla_mean)
chla_mean[245:247, 35:37]

##                    lon
## lat                 16.6458396911621 16.6875057220459 16.7291717529297
##   -36.4791717529297        0.1415056        0.1339336        0.1358152
##   -36.5208358764648        0.1358830        0.1328560        0.1276533
##   -36.5625038146973        0.1303250        0.1255906        0.1243571

r netcdf_05

netcdf_05.r
# Get a list of the nc variable names.
attributes(nc$var)$names
## [1] "CHL1_mean"  "CHL1_flags" "CHL1_error"

# Take a look at the chlorophyll variable's nc attributes (units etc).
ncatt_get(nc, attributes(nc$var)$names[1])
## $standard_name
## [1] "mass_concentration_of_chlorophyll_a_in_sea_water"
## 
## $long_name
## [1] "Chlorophyll concentration - Mean of the binned pixels"
## 
## $`_FillValue`
## [1] -999
## 
## $units
## [1] "mg/m3"
## 
## $pct_characterised_error
## [1] 43.31

# Retrieve a matrix of the chlorophyll data using the ncvar_get function:
chla_mean <- ncvar_get(nc, attributes(nc$var)$names[1])

# Print the data's dimensions
dim(chla_mean)
## [1] 453 256

r netcdf_04

netcdf_04
# Get a list of the NetCDF's R attributes:
attributes(nc)$names

##  [1] "filename"    "writable"    "id"          "safemode"    "format"     
##  [6] "is_GMT"      "groups"      "fqgn2Rindex" "ndims"       "natts"      
## [11] "dim"         "unlimdimid"  "nvars"       "var"

print(paste("The file has",nc$nvars,"variables,",nc$ndims,"dimensions and",nc$natts,"NetCDF attributes"))
## [1] "The file has 3 variables, 2 dimensions and 52 NetCDF attributes"

r netcdf_03

netcdf_03.r
# Save the print(nc) dump to a text file (same name as the nc file with a txt extension)
{
    sink(paste0("data/", flist[1], ".txt"))
    print(nc)
    sink()
}

r netcdf_02

netcdf_02.r
# Open a connection to the first file in our list
nc <- nc_open(paste0("data/", flist[1]))

r netcdf10.r

netcdf10.r
process_nc <- function(files){
    # iterate through the nc
    for (i in 1:length(files)){
        # open a conneciton to the ith nc file
        nc_tmp <- nc_open(paste0("data/", files[i]))
        # store values from variables and atributes
        nc_chla <- ncvar_get(nc_tmp, attributes(nc_tmp$var)$names[1])
        nc_lat <- ncvar_get(nc_tmp, attributes(nc_tmp$dim)$names[1])
        nc_lon <- ncvar_get(nc_tmp, attributes(nc_tmp$dim)$names[2])
        nc_atts <- ncatt_get(nc_tmp, 0)
        nc_start_date <- as.Date(nc_atts$period_start_day, format = "%Y%m%d", tz = "UTC")
        # close the connection sice were finished
        nc_close(nc_tmp)
        # set the dimension names and values of your matrix to the appropriate latitude and longitude values
        dimnames(nc_chla) <- list(lon=nc_lon, lat=nc_lat)

        # I'm choosing to store all the data in long format.
        # depending on your workflow you can make different choices here...
        # Your variable may get unmanageably large here
        # if you have high spatial and temporal resolution nc data.
        tmp_chl_df <- melt(nc_chla, value.name = "chla")
        tmp_chl_df$date_start <- nc_start_date

        # set the name of my new variable and bind the new data to it
        if (exists("chla_data_monthly")){
            chla_data_monthly <- bind_rows(chla_data_monthly, tmp_chl_df)
        }else{
            chla_data_monthly <- tmp_chl_df
        }
        # tidy up, not sure if necesarry really, but neater
        rm(nc_chla, nc_lat, nc_lon, nc_tmp, nc_atts, nc_start_date, tmp_chl_df)
    }

    return(chla_data_monthly)
}


data <- process_nc(flist)

r R操作

naming-vector
#Naming a vector
poker_vector <- c(140, -50, 20, -120, 240)
days_vector <- c("Monday","Tuesday","Wednesday","Thursday","Friday")
names(poker_vector) <- days_vector

#Vector selection
poker_vector[c(1, 5)]
poker_vector[2:4]
poker_vector["Monday"]
poker_vector[c("Monday","Tuesday")]

#Matrix create
matrix(1:9, byrow=TRUE, nrow=3)

#Naming a Matrix
rownames(my_matrix) <- row_names_vector
colnames(my_matrix) <- col_names_vector

#Sum of colums, rows
#cbind() function, which merges matrices and/or vectors together by column. For example:
big_matrix <- cbind(matrix1, matrix2, vector1 ...)

cbind(), rbind(), rowSums(), colSums()

#Matrix element selection
my_matrix[1,2] #selects the element at the first row and second column.
my_matrix[1:3,2:4] #results in a matrix with the data on the rows 1, 2, 3 and columns 2, 3, 4.
my_matrix[,1] #selects all elements of the first column.
my_matrix[1,] #selects all elements of the first row.