-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcsvtoNetcdf.py
More file actions
42 lines (32 loc) · 1.36 KB
/
csvtoNetcdf.py
File metadata and controls
42 lines (32 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from netCDF4 import Dataset
import numpy as np
import pandas as pd
import numpy as np
# CSV Format is Label followed by 10MFCCs.
# After reading the csv file, I exclude the labels and create
# an array of just the 10 MFCCs. Those 10 mfccs are converted to
# NETCDF form
rawDump = np.array(pd.read_csv("path/to/filename.csv", header=None))
data = rawDump[:, 1:]
# Create and open the netcdf dataset
root_grp = Dataset('menData.nc', 'w', format='NETCDF4')
# Various attributes can be given to the group.
# Description, institution etc can be added.
root_grp.description = 'MFCC data'
# Since all my MFCCs are of the same dimension (i.e. same number of MFCCs for the 0th, 1st, 2nd and so on)
# I create only one dimension which will be used to describe all my mfccs.
root_grp.createDimension("MFCC", data.shape[0])
# variables
# Since I have 10 mfccs, I create 10 variable.
# Each variable has the name dimensions (MFCC) that I created above. Dimension must be passed as a tuple.
# The data type given in double (d)
# The name of each variable is mfcc0, mfcc1, mfcc2 and so on
mfccvariables = []
for i in range(data.shape[1]):
mfccvariables.append(root_grp.createVariable('mfcc' + str(i), 'd', ('MFCC',)))
# data
# The data for each variable is passed in this loop
for i in range(len(mfccvariables)):
mfccvariables[i][:] = data[:, i]
# Close the netcdf dataset
root_grp.close()