宁夏SPEI计算

 

让我解释每个函数的用法:

1. `library(lmomco)`: 这个函数用于加载`lmomco`包,这个包提供了用于水文统计的函数,比如用于拟合水文分布的函数。

2. `library(SPEI)`: 这个函数用于加载`SPEI`包,这个包提供了计算标准化降水蒸散指数(SPEI)的函数。

3. `setwd("D:/R/month/ningxia/")`: 这个函数用于设置工作目录,将R的工作路径更改为指定的目录,这样在后续操作中就可以直接使用文件名而不需要指定完整路径。

4. `list.files(pattern = "\\.xlsx$")`: 这个函数用于列出指定目录中的文件,`pattern`参数用于指定匹配的文件模式,这里使用正则表达式"\.xlsx$"匹配扩展名为.xlsx的Excel文件。

5. `list()`: 这个函数用于创建一个空列表,用于存储后续的计算结果。

6. `for (file in excel_files) { ... }`: 这是一个for循环,用于遍历每个Excel文件。

7. `read_excel(file)`: 这个函数用于读取Excel文件的内容,并将其转换为一个数据框。

8. `thornthwaite(changan$TMED, 37.4816666666667)`: 这个函数用于计算Thornthwaite方法中的潜在蒸散发(PET),其中`changan$TMED`是每个月的平均温度数据,`37.4816666666667`是经度的值。

9. `spei(changan$PRCP - changan$PET, 1)`: 这个函数用于计算标准化降水蒸散指数(SPEI),其中`changan$PRCP - changan$PET`是降水减去潜在蒸散发的值,`1`是计算的时间尺度,这里是1个月。

10. `do.call(rbind, spei_results)`: 这个函数用于将列表中的所有元素按行合并成一个数据框。

11. `write.csv(all_spei_results, file = "changanspei_1_combined.csv")`: 这个函数用于将数据框写入一个CSV文件中,`all_spei_results`是要写入的数据框,`file`参数指定了要写入的文件名。

这段代码是用R语言编写的,目的是计算一系列Excel文件中的标准化降水蒸散指数(Standardized Precipitation Evapotranspiration Index,简称SPEI),然后将结果合并到一个CSV文件中。

让我逐行解释:

1. `library(lmomco)`: 加载`lmomco`包,这是一个用于水文统计的R包,提供了许多有用的函数来拟合水文分布。

2. `library(SPEI)`: 加载`SPEI`包,这是用于计算标准化降水蒸散指数的R包。

3. `setwd("D:/R/month/ningxia/")`: 设置工作目录为"D:/R/month/ningxia/",即Excel文件所在的目录。

4. `excel_files <- list.files(pattern = "\\.xlsx$")`: 获取目录中所有扩展名为.xlsx的Excel文件的文件名列表。

5. `spei_results <- list()`: 创建一个空列表,用于存储计算结果。

6. `for (file in excel_files) { ... }`: 开始一个for循环,遍历每个Excel文件。

7. `changan <- read_excel(file)`: 读取当前循环中的Excel文件,存储到名为`changan`的数据框中。

8. `changan$PET <- thornthwaite(changan$TMED, 37.4816666666667)`: 使用`thornthwaite`函数计算每个月的潜在蒸散发(PET),并将结果存储在`changan`数据框的新列`PET`中。

9. `spei1 <- spei(changan$PRCP - changan$PET, 1)`: 使用`spei`函数计算SPEI。SPEI的计算需要降水减去潜在蒸散发的值,以及计算的时间尺度(这里是1个月),结果存储在`spei1`中。

10. `spei_results[[file]] <- spei1$fitted`: 将计算得到的SPEI结果存储在`spei_results`列表中,文件名作为索引。

11. `# plot(spei(ts(changan$PRCP - changan$PET, freq = 12, start = c(1964, 1)), 12), ...`: 这行代码是一个注释,表示如果需要的话,可以绘制SPEI-12图。

12. `all_spei_results <- do.call(rbind, spei_results)`: 将所有计算结果合并到一个数据框中。

13. `write.csv(all_spei_results, file = "changanspei_1_combined.csv")`: 将合并的结果写入一个名为"changanspei_1_combined.csv"的CSV文件中。

library(lmomco)
library(SPEI)

# Set the working directory where your Excel files are located
setwd("D:/R/month/ningxia/")

# Get the list of Excel files in the directory
excel_files <- list.files(pattern = "\\.xlsx$")

# Create an empty list to store the results
spei_results <- list()

# Iterate over each Excel file
for (file in excel_files) {
  # Read the Excel file
  changan <- read_excel(file)
  
  # Perform calculations
  changan$PET <- thornthwaite(changan$TMED, 37.4816666666667)
  spei1 <- spei(changan$PRCP - changan$PET, 1)
  
  # Store the results in the list
  spei_results[[file]] <- spei1$fitted
  
  # Plot SPEI-12 (if needed)
  # plot(spei(ts(changan$PRCP - changan$PET, freq = 12, start = c(1964, 1)), 12),
  #      main = paste("SPEI-12 for", file))
}

# Combine all results into a single data frame
all_spei_results <- do.call(rbind, spei_results)

# Write the combined results to a CSV file
write.csv(all_spei_results, file = "changanspei_1_combined.csv")

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

___Y1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值