-1

I am trying to convert netCDF files to csv. Files are stored in thredds server.I have written below code which reads files from Thredds server and select six variables from the list of variables present in netCDF files and stored them locally in csv format.

from datetime import datetime
from netCDF4 import Dataset
import netCDF4
import pandas as pd
import csv
def get_thredds_url(datetime_group, forecast_hour):
    base_url="path"
    thredds_url="path"
    cycle_hour = "%02d" % datetime_group.hour
    file_str=datetime_group.strftime("%Y%m%d%H")+"/gfs.t%sz.pgrb2.0p25.f%03d" % \
        (cycle_hour, forecast_hour)
    url = base_url + thredds_url + file_str
    return (url)

Below code shows the required variables.

def main():

    datetime_group = datetime(2017, 9, 26, 0)
    forecast_hour = 240

    url = get_thredds_url(datetime_group, forecast_hour)
    print (url)

    nc_handle=Dataset(url)
    #print (nc_handle.variables.keys())
    lat=nc_handle.variables['lat'][:]
    lon=nc_handle.variables['lon'][:]
    issue_time=nc_handle.variables['time'][:]
    valid_time=nc_handle.variables['time1'][:]
    temp=nc_handle.variables['Temperature_height_above_ground'][:]
    dewpoint_temp=lat=nc_handle.variables['Dewpoint_temperature_height_above_ground'][:]
    dtime = netCDF4.num2date(issue_time[:],units=units)


 tmp = pd.Series(temp, index=dtime) 

    tmp.to_csv('temp.csv',index=True, header=True)
    csvwriter = csv.writer(tmp,  delimiter=',')
    print (csvwriter)





if __name__ == "__main__":
    main()

Problem: I am not able to write file into csvnformat which contains all the variables such as lat,lon,time,time1,Temperature_height_above_ground. Desired output is given below:

tmp.csv

 lat lon time time1 Temperature_height_above_ground
1 ... .. ...  ....  ......
2 ... .. ...  ....  ......
3 ... .. ...  ....  ......

Can anyone help me out me with this?

Thanks in advance!

cs95
  • 379,657
  • 97
  • 704
  • 746
rnvs1116
  • 39
  • 2
  • 12
  • The question doesn't make it clear what the exact problem is. See https://stackoverflow.com/help/how-to-ask for some guidance and edit your question to add e.g. an error message. And while you're at it, fix your post's indentation as the "Problem: I am not able..." line currently looks like it's part of the source code, which I think is incorrect. – FriendFX Nov 27 '17 at 02:43

2 Answers2

0

I think what you are looking for is this:

with open('temp.csv', 'w') as f:
  writer = csv.writer(f, delimiter=',')
  # write the header
  writer.writerow(['lat',
                   'lon',
                   'time',
                   'time1',
                   'temp_height_above_ground',
                   'dewpoint_temp_height_above_ground',
                   'issue_time'])

  # collect the columns as rows
  rows = zip(lat,lon,issue_time,valid_time,temp,dewpoint_temp,dtime)

  for row in rows:
    writer.writerow(row)
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
0

You can Convert the NETCDF into CSV using This simple code in python step wise:- we need 2 main libaries

a-NetCDF-4 :- (Network Common Data Form, version 4) Description. NetCDF is a set of software libraries and self-describing, machine-independent data formats for array-oriented scientific data

b-xarray :- xarray (formerly xray) is an open source project and Python package that makes working with labelled multi-dimensional arrays simple, efficient, and fun!

1-Import Libararies

   import netCDF4 as nc
    import xarray as xr

    data = nc.Dataset(r'C:\\Users\\Admin\\Desktop\\LDN DATA NC FORMAT\\20210312_0257.nc', 'r')
    dataset = xr.open_dataset(xr.backends.NetCDF4DataStore(data))
    df = dataset.to_dataframe()
    csv_data=df.to_csv('C:\\Users\\Admin\\Desktop\\LDN DATA NC FORMAT\\20210312_0257.nc.csv')
    print('\nCSV String:\n', csv_data)

image1:-enter image description here

imag2 :-enter image description here

After finish the execution csv file save in the same folder which is provided