netcdf

33 views
Skip to first unread message

Mila Joldas

unread,
Oct 8, 2024, 4:30:22 AMOct 8
to SWAT-CUP
hello everyone , 
please who can help me to extract netcdf file to csv precipitation for arc swat. please

Engr. Salman Khan Ph.D Scholar Civil

unread,
Oct 8, 2024, 3:35:25 PMOct 8
to swat...@googlegroups.com
Hey, I can help you with this.

On Tue, 8 Oct 2024 at 1:30 PM, Mila Joldas <milaj...@gmail.com> wrote:
hello everyone , 
please who can help me to extract netcdf file to csv precipitation for arc swat. please

--
You received this message because you are subscribed to the Google Groups "SWAT-CUP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swat-cup+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/swat-cup/bedc4d3b-a5cd-406b-806e-7c8a5064d012n%40googlegroups.com.

nkj Sailo

unread,
Oct 8, 2024, 4:44:22 PMOct 8
to swat...@googlegroups.com

U can use R


On Tue, 8 Oct, 2024, 2:00 pm Mila Joldas, <milaj...@gmail.com> wrote:
hello everyone , 
please who can help me to extract netcdf file to csv precipitation for arc swat. please

Wahid Hussainzada

unread,
Oct 8, 2024, 4:44:35 PMOct 8
to swat...@googlegroups.com
Hello 

The below is python code. If you can modify it properly based on your data you may be able to extract the data from the nc file to csv format for specific coordinates.

Best of luck! Please have a look at the code and modify it as you wish.

import glob
from netCDF4 import Dataset
import numpy as np
import pandas as pd


all_years = []

for file in glob.glob('*.nc'):
    print(file)
    data = Dataset(file, 'r')
    time = data.variables['time']
    year = time.units[11:15]
    all_years.append(year)
    
year_start = min(all_years)
end_year = max(all_years)
date_range = pd.date_range(start = str(year_start) + '-01-01',
                           end = str(end_year)+'-12-31',
                           freq = 'M')
df = pd.DataFrame(0.0, columns = ['precip'], index = date_range)

lat_keshem = 31.93321389
lon_keshem = 63.04979722

all_years.sort()

for yr in all_years:
    data = Dataset(str(yr)+'.nc', 'r')
    lat = data.variables['lat'][:]
    lon = data.variables['lon'][:]
    sq_diff_lat = (lat-lat_keshem)**2
    sq_diff_lon = (lon-lon_keshem)**2
    min_index_lat = sq_diff_lat.argmin()
    min_index_lon = sq_diff_lon.argmin()
    precip = data.variables['precip']
    start = str(yr) + '-01-01'
    end = str(yr) + '-12-31'
    d_range = pd.date_range(start = start,
                            end = end,
                            freq = 'M')
    for t_index in np.arange(0, len(d_range)):
        #print('Recording the value for: '+str(d_range[t_index]))
        df.loc[d_range[t_index]]['precip'] = precip[t_index, min_index_lat, min_index_lon]
df.to_csv('XXXXX.csv')

On Tue, Oct 8, 2024 at 5:30 PM Mila Joldas <milaj...@gmail.com> wrote:
hello everyone , 
please who can help me to extract netcdf file to csv precipitation for arc swat. please

--
You received this message because you are subscribed to the Google Groups "SWAT-CUP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swat-cup+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/swat-cup/bedc4d3b-a5cd-406b-806e-7c8a5064d012n%40googlegroups.com.


--
Best Regard;
Wahidullah Hussainzada
Ph.D. Student, Hiroshima University, Japan
Coastal Hazard and Energy System Science Lab
 

C Dhananjay Kumar

unread,
Oct 8, 2024, 4:44:46 PMOct 8
to swat...@googlegroups.com

Drop me the data


On Tue, 8 Oct 2024, 2:00 pm Mila Joldas, <milaj...@gmail.com> wrote:
hello everyone , 
please who can help me to extract netcdf file to csv precipitation for arc swat. please

amsayaw genet

unread,
Oct 8, 2024, 4:45:37 PMOct 8
to swat...@googlegroups.com

Check this https://youtu.be/vIyZ2_U5t4c?si=k9PM5NRGrte0x4N3


On Tue, Oct 8, 2024, 11:30 AM Mila Joldas <milaj...@gmail.com> wrote:
hello everyone , 
please who can help me to extract netcdf file to csv precipitation for arc swat. please

Reply all
Reply to author
Forward
0 new messages