Forums

Converting units in NetCdf Files in Python

I have a script that reads Netcdf (.nc) files and coordinates from a 'csv' file, then it extracts a txt file that includes values of different parameters (Tmax, Tmin, Prcp and Wind Speed) in columns and different values of days in rows.

Because Netcdf file has different units from that I want, I need to change from the unit of Kelvin to the unit of Celcius.

Here is one part of the script. How can I put the codes inside to change units from Kelvin to Celcius?

 #After reading and giving the locations here, I can extract the values as .txt file:

inpNETCDF ='E:/VIC/Forcing Data from princeton/from 85 for 35 years/02.tmin/tmin_daily_' + str(year) + '-' + str(year) + '.nc'
                dataset = Dataset(inpNETCDF, mode='r')
                var_dict = {}  
                indexOfLat_down , indexOfLat_up, indexOfLon_down, indexOfLon_up = Find_IndexOfSite(lon_site,lat_site,dataset)
                var_dict['tmin']=dataset.variables['tmin'][:, indexOfLon_down, indexOfLat_down]
                dataFrame = pd.DataFrame(var_dict)
                dataFrame_all = pd.concat([dataFrame_all, dataFrame])

dataFrame_all.to_csv('data_' + gridCells[str(i+1)][j].replace(', ', '_') + '.txt', index=False, header=False, sep=' ',)

Thanks.

This is probably a better question for Stack Overflow.