Python Forum
Converting units in NetCdf Files in Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Converting units in NetCdf Files in Python
#1
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=' ',)

However the values in txt files are as Kelvin, I want to change these values as Celcius. How can I change the unit? Thanks.
Reply
#2
Who created the Netcdf (.nc) files?
If was you, try to create them setting the units attribute:
temp.units = "Celsius" #(or "C")
If not, I think you'll have to convert the values before creating the CSV:
temp_c = temp_k - 273.15
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Converting several Markdown files into DOCX using Pandoc Akule8 0 1,195 Feb-02-2023, 02:54 PM
Last Post: Akule8
  Converting python to FileMaker DWolf 6 1,688 Sep-22-2022, 03:40 AM
Last Post: DWolf
  combining netcdf data sebastiansieloff 1 1,812 Nov-19-2020, 03:59 PM
Last Post: Larz60+
  Converting python to c# benahmadi 1 2,649 Oct-22-2019, 09:50 PM
Last Post: micseydel
  converting array to and from string in python 3.7.2 srm 5 6,194 Jul-03-2019, 01:11 PM
Last Post: snippsat
  help with converting C# function to Python korenron 17 11,339 May-19-2019, 10:26 AM
Last Post: Gribouillis
  Converting R code to python mcva 2 12,534 Mar-09-2019, 04:01 PM
Last Post: mcva
  Selecting netcdf data files Jodman92 4 3,042 Nov-21-2018, 10:56 PM
Last Post: Larz60+
  converting from c# to python peper 1 2,956 Jun-12-2018, 05:19 PM
Last Post: micseydel
  Storing Python data output as a Netcdf file Lightning1800 1 2,639 May-16-2018, 10:14 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020