act-hydro / GLY606_2024

GLY606 Water Data Analysis & Modeling
2 stars 1 forks source link

HW2, P1 - cp not working! #4

Open act-hydro opened 1 month ago

act-hydro commented 1 month ago

How do I get the "cp" command to work? No matter what I do it says cp: cannot stat 'ameriflux_init_fields.C1152.CA-Cbo.2006-12-31_23-00-00.nc': No such file or directory

I can't figure out why it won't find the file and move it...

cd gly606_hw2
for line in `cat site_list.txt`; do
        IFS=","
        set -- $line
        site_id=$1
#       date=$2
        unset IFSls
#        mkdir ../model_run/$site_id
#        mkdir ../model_run/$site_id/init
#        mkdir ../model_run/$site_id/static
#        mkdir ../model_run/$site_id/forcing
# cp ameriflux_init_fields.C1152.${site_id}.2006-12-31_23-00-00.nc ../model_run/${site_id}/init/
      f=ameriflux_init_fields.C1152.${site_id}.2006-12-31_23-00-00.nc
        echo $f
        d=../model_run/${site_id}/init/
        echo $d
        cp $f $d
        echo $site_id
        pwd
done
act-hydro commented 1 month ago

This is great process!! One thing we can note here is that ameriflux_init_fields.C1152.CA-Cbo.2006-12-31_23-00-00.nc does not exist because the year is incorrect. The initial condition file for CA-Cbo has a date of 2010-12-31. Here we simply use the mode start year (with variable name date in the script above) minus 1 to calculate the year for initial condition file.

year_pre=$((date - 1)) f=ameriflux_init_fields.C1152.${site_id}.${year_pre}-12-31_23-00-00.nc

An example can be referred to #3

KniyaD commented 1 month ago

I am still having trouble with the cp part, even after following the directions:

!/bin/bash

for line in cat site_list.txt; do echo $line # print each line echo # print a blank line IFS="," # define the deliminator, split by comma set -- $line site_id=$1 # assign the first element in a line to variable "site_id" model_run_year=$2 # assign the second element in a line to variable "model_run_year" unset IFS # we need to unset the deliminator echo "site name is:" $site_id echo "model run year is:" $model_run_year mkdir model_run mkdir model_run/$1

################ mkdir model_run/$1/forcing mkdir model_run/$1/init mkdir model_run/$1/static

############### year_pre=$((date - 1)) cp /ameriflux_init_fields.C1152.CA-Cbo.2010-12-31_23-00-00.nc ..model_run/$1/init f=ameriflux_init_fields.C1152.$1.${year_pre}-12-31_23-00-00.nc

    echo $f
    d=../model_run/$1/init/
    echo $d
    cp $f $d
    echo $1
    pwd

done

This is how far I got but I am still having trouble with the cp. I keep getting the error that there is no such file in the directory

act-hydro commented 1 month ago

@KniyaD This is great progress!

Before we copy files, it is important to make sure that the path of the file exist with respect to the bash script itself.

There are two ways to point to a file.

  1. Full path. In this case, we do not need to care where the bash script exists relative to this file exists. For example, let's go to the folder where the initial condition file exists, use pwd command. It output the full path to this file. In my case, the full path to initial condition file is /home/jovyan/data/assignment/assignment_2/hw2/gly606_hw2. Then we define

f="/home/jovyan/data/assignment/assignment_2/hw2/gly606_hw2/ameriflux_init_fields.C1152.$1.${year_pre}-12-31_23-00-00.nc"

  1. Relative path. In this case, we need to be careful the relative locations between bash script and the file locations. If the bash script is in the same folder with gly606_hw2 folder, we will need to put the folder name in front of the file

f="gly606_hw2/ameriflux_init_fields.C1152.$1.${year_pre}-12-31_23-00-00.nc"

If the bash script is within the gly606_hw2 folder, we can just directly point to the file

f="ameriflux_init_fields.C1152.$1.${year_pre}-12-31_23-00-00.nc"

Some comments about this line of code (I think you should probably delete it)

cp /ameriflux_init_fields.C1152.CA-Cbo.2010-12-31_23-00-00.nc ..model_run/$1/init

If your bash script is within the gly606_hw2 folder, you can refer to the initial condition file as above or ./ameriflux_init_fields.C1152.CA-Cbo.2010-12-31_23-00-00.nc. It is important to add the . in front of the /, . means the current folder. Maybe add / after the .. if you want to point to the parent folder.

I hope this helps!

KniyaD commented 1 month ago

Ok I adopted the comments above but I am still running into no file in directory error

!/bin/bash

for line in cat site_list.txt; do echo $line # print each line echo # print a blank line IFS="," # define the deliminator, split by comma set -- $line site_id=$1 # assign the first element in a line to variable "site_id" model_run_year=$2 # assign the second element in a line to variable "model_run_year" unset IFS # we need to unset the deliminator echo "site name is:" $site_id echo "model run year is:" $model_run_year mkdir model_run mkdir model_run/$1

################ mkdir model_run/$1/forcing mkdir model_run/$1/init mkdir model_run/$1/static

############### year_pre=$((date - 1)) f=./ameriflux_init_fields.C1152.$1.${year_pre}-12-31_23-00-00.nc echo $f d=../model_run/$1/init/ echo $d cp $f $d echo $1 pwd done

act-hydro commented 1 month ago

Based on the code above, the variable name for the model year is model_run_year. So when we calculate the previous year, we will need to refer to the model_run_year, rather than date

year_pre=$((model_run_year - 1))

Please let me know if you have more questions!