我正在从 netCDF 文件中读取数据并将其缩减为向量,然后将向量写入 csv 文件。数据重新格式化工作正常,但是当我尝试将数据写入 csv 时,它不会写入整个文件。事实上,它甚至不写一个 csv 文件,而是写一个通用的“文件”类型。
import numpy as np
import netCDF4
import os
import csv
'''
I'm importing the lists I need to automate file extraction from a seperate file
to reduce clutter
'''
from gmLists import files #Stores the different paths I need
from gmLists import variableNames #Stores names of variables
from gmLists import years #Years I have data for
from gmLists import urlPrefix #Used to get files
from gmLists import yearlyDataList #Used to store numpy arrays
#Used to store the names of the csv files to be created
from gmLists import csvFileNames
for i in range(10):
os.chdir(files[0]) #Read from here
for j in range(16):
#Name of specific file in directory
url = urlPrefix[0] + str(years[j]) + ".nc"
temp = netCDF4.Dataset(url).variables[variableNames[i]][:,::]
dataYear = temp[:,179:408,10:235]
# do monthly mean here
if(years[j]%4 != 0):
January = np.mean(dataYear[0:31,::], axis=0)
February = np.mean(dataYear[31:59,::], axis=0)
March = np.mean(dataYear[59:90,::], axis=0)
April = np.mean(dataYear[90:120,::], axis=0)
May = np.mean(dataYear[120:151,::], axis=0)
June = np.mean(dataYear[151:181,::], axis=0)
July = np.mean(dataYear[181:212,::], axis=0)
August = np.mean(dataYear[212:243,::], axis=0)
September = np.mean(dataYear[243:273,::], axis=0)
October = np.mean(dataYear[273:304,::], axis=0)
November = np.mean(dataYear[304:334,::], axis=0)
December = np.mean(dataYear[334:365,::], axis=0)
else:
# for leap year
January = np.mean(dataYear[0:31,::], axis=0)
February = np.mean(dataYear[31:60,::], axis=0)
March = np.mean(dataYear[60:91,::], axis=0)
April = np.mean(dataYear[91:121,::], axis=0)
May = np.mean(dataYear[121:152,::], axis=0)
June = np.mean(dataYear[152:182,::], axis=0)
July = np.mean(dataYear[182:213,::], axis=0)
August = np.mean(dataYear[213:244,::], axis=0)
September = np.mean(dataYear[244:274,::], axis=0)
October = np.mean(dataYear[274:305,::], axis=0)
November = np.mean(dataYear[305:335,::], axis=0)
December = np.mean(dataYear[335:366,::], axis=0)
monthlyData = np.stack([January, February, March, April, May, June, July,
August, September, October, November, December],
axis=0)
yearlyDataList.append(monthlyData)
totalMonthlyData = np.vstack([yearlyDataList[0], yearlyDataList[1],
yearlyDataList[2], yearlyDataList[3], yearlyDataList[4], yearlyDataList[5],
yearlyDataList[6], yearlyDataList[7], yearlyDataList[8], yearlyDataList[9],
yearlyDataList[10], yearlyDataList[11], yearlyDataList[12],
yearlyDataList[13], yearlyDataList[14], yearlyDataList[15]])
# flatten the array to vector
flattenedMonthlyData = np.reshape(totalMonthlyData, (np.product(totalMonthlyData.shape),))
#The below code is where I think the problem is
os.chdir('saveDirectory') # your save destination
csvfile = csvFileNames[0]
with open(csvfile, "w") as output:
writer = csv.writer(output, lineterminator='\n')
for val in flattenedMonthlyData:
writer.writerow([val])
我希望我没有包含太多代码。我知道读取 netCDF 文件并重新格式化它的代码是正确的。我包含了该代码,以防有一些交互弄脏了我没有看到的东西。
在我添加循环机制之前,我成功地生成了一个 csv 文件,但是从那时起,当我运行代码时,生成的文件比我的一个好的 csv 文件小 5000 KB,它们甚至不是 csv 文件。它们是一些通用文件类型。即使我尝试倒退并删除循环,我也无法重现我之前的成功。任何帮助将不胜感激。