top of page
Writer's picturePravash Tiwari

Converting nc files to csv using python

Updated: Apr 29, 2020

Let's try how we can convert our netcdf files into csv format using Python.

Many people like working with csv, xlsx and txt files in R studio as, it handles huge txt and csv data very efficiently. I myself use R studio for statistical analysis especially for correlation analysis, making multiple regression and machine learning models etc.

I am sharing a small script on how can we convert our nc files into csv.

Data source : ERA 5 (from CDS) temperature and total precipitation. Daily data of August 2018 with a spatial extent -90 to 90 and -180 to 180 at a resolution of 0.25 X 0.25

My goal primarily will be to to extract a data for a location. Here, my desired location is Pokhara Valley, Nepal (28.15 N, 83.95 E).

1. The general idea is to make python identify the date variable of the nc file. Choose the location range over which i need a data and finally to write a csv file with latitude, longitude and variable field for correesponding dates.


The script is as follows:

#% /usr/bin/env python
import netCDF4
import pandas as pd
import numpy.ma as ma
from datetime import datetime
from netCDF4 import Dataset
print("Start Time: ",str(datetime.now()))
d = Dataset("August2018.nc")
v1 = d.variables['latitude']
v2 = d.variables['longitude']
v3 = d.variables['time']
v4 = d.variables['t2m']    #### Temperature 
v5 = d.variables['tp']     ##### Precipitation ####Avoid the underlines :|
date1 = netCDF4.num2date(v3[:], units=v3.units, calendar=v3.calendar)
for a in range(len(date1)):
         date1[a] = str(date1[a])
         date1[a] = str(date1[a][:10])
file = open('August2018.csv', 'w')
file.write("time,latitude,longitude,t2m,tp\n")
for t in range(len(v3)):
         for lat in range(len(v1)):
             if v1[lat] >= 28 and v1[lat] <= 28.25:
                for long in range(len(v2)):
                    if v2[long] >= 83.75 and v2[long] <= 84:
                      file.write(str(date1[t])+","+str(v1[lat])+","+str(v2[long])+","+str(v4[t][lat][long]+ ","+str(v5[t][lat][long])) 
file.write("\n")
file.close()
d.close()
print("End Time: ",str(datetime.now()))
print("Done!")


121 views0 comments

Recent Posts

See All

Comments


bottom of page