Project

General

Profile

Loop error

Added by Muhammad Ramzan over 1 year ago

Hi

#!/bin/bash
exec < coords.txt
"station , lat, lon"
read r header
echo -
"$header"
while IFS=", " read -r station lat lon
do
cdo "remapnn,lon=${lon}_lat=${lat}," era_temp.nc "${station}_out.nc"
done

but I am getting the error message

cdo remapnn (Abort): Too many arguments! Need 1 found 2.

cdo remapnn (Abort): Too many arguments! Need 1 found 2.

cdo remapnn (Abort): Too many arguments! Need 1 found 2.

cdo remapnn (Abort): Too many arguments! Need 1 found 2.

cdo remapnn (Abort): Too many arguments! Need 1 found 2.

cdo remapnn (Abort): Too many arguments! Need 1 found 2.


Replies (17)

RE: Loop error - Added by Muhammad Ramzan over 1 year ago

this single commands works fine
cdo -remapnn,lon=28_lat=73 era_temp.nc era_temp_nn.nc

but when I try to create a loop, I am getting error

RE: Loop error - Added by Muhammad Ramzan over 1 year ago

loop file

values.sh (215 Bytes) values.sh

RE: Loop error - Added by Karin Meier-Fleischer over 1 year ago

If you are not familiar with bash scripts read one of the many available tutorials.

Try

#!/bin/bash

while read line
do
   lat=$(echo $line | cut -d ',' -f 1)
   lon=$(echo $line | cut -d ',' -f 2)
   station="station_${lat}_${lon}" 
   cdo -remapnn,"lon=${lon}_lat=${lat}" era_temp.nc ${station}_out.nc
done < coords.txt

RE: Loop error - Added by Muhammad Ramzan over 1 year ago

Thanks

I am trying to run the above code but again getting the error message please

where I am making the mistake please



RE: Loop error - Added by Karin Meier-Fleischer over 1 year ago

Without the coords.txt and the era_tmp.nc files it is not possible to help.

RE: Loop error - Added by Muhammad Ramzan over 1 year ago

Please see the files

Thanks

RE: Loop error - Added by Karin Meier-Fleischer over 1 year ago

The problem is that you wrote above that the delimiter is ',' but it is a tab. Further, you have to skip the header line in coords.txt.

If you are working on a Windows computer you can delete the line with line=$(echo $line | sed -e 's/\r//g'), this is needed on Linux computer because the coords.txt file contains ^M at the end of each line.

The following script runs without error on my Mac.

#!/bin/bash

infile="era_temp.nc" 
coords="coords.txt" 

while read line
do
   line=$(echo $line | sed -e 's/\r//g')
   station=$(echo $line | cut -d ' ' -f 1)
   #-- skip header line
   if [[ "$station" == "station" ]]; then continue; fi
   #-- select station coordinates 
   lat=$(echo $line | cut -d ' ' -f 2)
   lon=$(echo $line | cut -d ' ' -f 3)
   station="${station}_${lat}_${lon}" 
   #-- extract the station data
   cdo -remapnn,"lon=${lon}_lat=${lat}" ${infile} ${station}_out.nc
done < $coords

RE: Loop error - Added by Muhammad Ramzan over 1 year ago

thanks

how I can have a loop for multiple infile="era_temp_year.nc" files.

like ear_temp_2001 , ear_temp_2002 , ear_temp-2003

Thanks

RE: Loop error - Added by Karin Meier-Fleischer over 1 year ago

This is a shell question and way beyond CDO here.

Read more about nested loops in bash shell scripts.

Try a for-loop to loop over the files with the while-loop inside.

Something like

my_files=$(ls era_temp_*.nc)

for f in $files
do

...... the while stuff .....

done

RE: Loop error - Added by Muhammad Ramzan over 1 year ago

Thanks

my_files=$(ls era_temp_*.nc)

will this be

for f in $files

OR

for f in $my_files

Thanks

RE: Loop error - Added by Karin Meier-Fleischer over 1 year ago

for f in $my_files

RE: Loop error - Added by Muhammad Ramzan over 1 year ago

while running the loop is it possible to get the csv file rather than the ${station}_out.nc

Thanks
#!/bin/bash

infile="era_temp.nc"
coords="coords.txt"

while read line
do
line=$(echo $line | sed e 's/\r//g')
station=$(echo $line | cut -d ' ' -f 1)
#-
skip header line
if "$station" == "station" ; then continue; fi
#-- select station coordinates
lat=$(echo $line | cut d ' ' -f 2)
lon=$(echo $line | cut -d ' ' -f 3)
station="${station}_${lat}_${lon}"
#-
extract the station data
cdo -remapnn,"lon=${lon}_lat=${lat}" ${infile} ${station}_out.nc
done < $coords

RE: Loop error - Added by Karin Meier-Fleischer over 1 year ago

And again - you have to read and learn more about bash! This is not part of our support in this forums.

For-loop syntax:

for something in your_list
do

done

To write CSV data with CDO see outputtab operator https://code.mpimet.mpg.de/projects/cdo/embedded/cdo.pdf#subsection.2.14.6 (there are some posts that already answered it in the forums about this issue).

RE: Loop error - Added by Muhammad Ramzan over 1 year ago

Thanks @ Karin Meier-Fleischer

I able to apply the outputtab in the loop, I am getting the file. I have two questions.

the file generated is not properly is CSV.
and for the same date, lat & lon why I am getting two values.

  1. date lon lat value
    2016-01-01 73.8398 33.1855 286.0074
    2016-01-01 73.8398 33.1855 287.6523
    2016-02-01 73.8398 33.1855 289.2243
    2016-02-01 73.8398 33.1855 291.0409

#!/bin/bash

infile="era_temperature_2016.nc"
coords="coords.txt"

while read line
do
line=$(echo $line | sed e 's/\r//g')
station=$(echo $line | cut -d ' ' -f 1)
#-
skip header line
if "$station" == "station" ; then continue; fi
#-- select station coordinates
lat=$(echo $line | cut d ' ' -f 2)
lon=$(echo $line | cut -d ' ' -f 3)
station="${station}_${lat}_${lon}"
#-
extract the station data
cdo -remapnn,"lon=${lon}_lat=${lat}" ${infile} ${station}_out.nc
cdo outputtab,date,lon,lat,value ${station}_out.nc > ${station}_out.txt
done < $coords

RE: Loop error - Added by Karin Meier-Fleischer over 1 year ago

It is a CSV file with delimiter ' ' ;)

If your input file is of the same structure than the given era_temp.nc file the file contains the data of two levels for a timestep that's why you get two time steps. You should also write out the levels.

cdo infon era_temp.nc
    -1 :       Date     Time   Level Gridsize    Miss :     Minimum        Mean     Maximum : Parameter name
     1 : 2020-01-01 00:00:00     975    14400     120 :      278.22      285.13      291.01 : t             
     2 : 2020-01-01 00:00:00    1000    14400     120 :      279.63      285.99      293.14 : t             
     3 : 2020-02-01 00:00:00     975    14400     120 :      282.70      290.32      296.09 : t             
     4 : 2020-02-01 00:00:00    1000    14400     120 :      284.07      290.99      296.32 : t             
     5 : 2020-03-01 00:00:00     975    14400     120 :      285.23      291.64      296.57 : t             
     6 : 2020-03-01 00:00:00    1000    14400     120 :      286.60      292.65      297.69 : t             
     7 : 2020-04-01 00:00:00     975    14400     120 :      289.70      297.56      302.59 : t             
     8 : 2020-04-01 00:00:00    1000    14400     120 :      291.10      298.52      303.97 : t             
     9 : 2020-05-01 00:00:00     975    14400     120 :      294.44      301.36      307.01 : t             
    10 : 2020-05-01 00:00:00    1000    14400     120 :      295.77      302.73      308.71 : t             
    11 : 2020-06-01 00:00:00     975    14400     120 :      296.15      303.32      310.41 : t             
    12 : 2020-06-01 00:00:00    1000    14400     120 :      297.23      304.81      311.92 : t             
    13 : 2020-07-01 00:00:00     975    14400     120 :      296.34      303.64      311.47 : t             
    14 : 2020-07-01 00:00:00    1000    14400     120 :      297.23      305.08      312.96 : t             
    15 : 2020-08-01 00:00:00     975    14400     120 :      296.42      302.85      311.03 : t             
    16 : 2020-08-01 00:00:00    1000    14400     120 :      297.29      304.27      312.53 : t             
    17 : 2020-09-01 00:00:00     975    14400     120 :      294.47      300.55      305.53 : t             
    18 : 2020-09-01 00:00:00    1000    14400     120 :      295.87      301.98      307.01 : t             
    19 : 2020-10-01 00:00:00     975    14400     120 :      287.76      297.40      302.11 : t             
    20 : 2020-10-01 00:00:00    1000    14400     120 :      289.17      298.44      303.65 : t             
    21 : 2020-11-01 00:00:00     975    14400     120 :      282.82      292.27      297.37 : t             
    22 : 2020-11-01 00:00:00    1000    14400     120 :      284.18      293.01      297.80 : t             
    23 : 2020-12-01 00:00:00     975    14400     120 :      276.61      287.98      294.26 : t             
    24 : 2020-12-01 00:00:00    1000    14400     120 :      277.99      288.74      294.78 : t             

Have a nice weekend.

RE: Loop error - Added by Muhammad Ramzan over 1 year ago

Yes

But when I am opening it in the Excel all the values are in one cell.

why I am not getting same output as that of yours please. How I can get the level variable in my input please

I am using this command please

cdo outputtab,date,lon,lat,value ${station}_out.nc > ${station}_out.txt

cdo outputtab,date,lon,lat,value era_temperature_2016.nc > test_2016.txt

cdo outputtab,date,lon,lat,level,value era_temperature_2016.nc > test_2016.txt

Thanks

    (1-17/17)