8000 create unique text file for solid.for to write to, fix #50 by scottstanie · Pull Request #55 · insarlab/PySolid · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

create unique text file for solid.for to write to, fix #50 #55

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 22 additions & 26 deletions src/pysolid/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
# pysolid.calc_solid_earth_tides_grid()


import datetime as dt
import os
import tempfile

import numpy as np
from skimage.transform import resize
Expand Down Expand Up @@ -74,35 +74,31 @@ def calc_solid_earth_tides_grid(dt_obj, atr, step_size=1e3, display=False, verbo
s=(length, width), la=lat_step, lo=lon_step))

## calc solid Earth tides and write to text file
txt_file = os.path.abspath('solid.txt')
if os.path.isfile(txt_file):
os.remove(txt_file)

vprint('SOLID : calculating / writing data to txt file: {}'.format(txt_file))

# Run twice to circumvent fortran bug which cuts off last file in loop - Simran, Jun 2020
for _ in range(2):
solid_grid(dt_obj.year, dt_obj.month, dt_obj.day,
dt_obj.hour, dt_obj.minute, dt_obj.second,
lat0, lat_step, length-1,
lon0, lon_step, width-1)

## read data from text file
vprint('PYSOLID: read data from text file: {}'.format(txt_file))
grid_size = int(length * width)
fc = np.loadtxt(txt_file,
dtype=float,
usecols=(2,3,4),
delimiter=',',
skiprows=0,
max_rows=grid_size+100)[:grid_size]
with tempfile.NamedTemporaryFile(prefix="pysolid_", suffix=".txt") as fp:
vprint('SOLID : calculating / writing data to txt file: {}'.format(fp.name))

# Run twice to circumvent fortran bug which cuts off last file in loop
# - Simran, Jun 2020
for _ in range(2):
solid_grid(fp.name, dt_obj.year, dt_obj.month, dt_obj.day,
dt_obj.hour, dt_obj.minute, dt_obj.second,
lat0, lat_step, length - 1,
lon0, lon_step, width - 1)

## read data from text file
vprint('PYSOLID: read data from text file: {}'.format(fp.name))
grid_size = int(length * width)
fc = np.loadtxt(fp.name,
dtype=float,
usecols=(2,3,4),
delimiter=',',
skiprows=0,
max_rows=grid_size+100)[:grid_size]

tide_e = fc[:, 0].reshape(length, width)
tide_n = fc[:, 1].reshape(length, width)
tide_u = fc[:, 2].reshape(length, width)

# remove the temporary text file
os.remove(txt_file)

# resample to the input size
if num_step > 1:
out_shape = (int(atr['LENGTH']), int(atr['WIDTH']))
Expand Down
36 changes: 17 additions & 19 deletions src/pysolid/point.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import collections
import datetime as dt
import os
import tempfile

import numpy as np
from matplotlib import pyplot as plt, ticker, dates as mdates
Expand Down Expand Up @@ -170,22 +171,22 @@ def calc_solid_earth_tides_point_per_day(lat, lon, date_str, step_sec=60):
raise ImportError(msg)

## calc solid Earth tides and write to text file
txt_file = os.path.abspath('solid.txt')
if os.path.isfile(txt_file):
os.remove(txt_file)

# Run twice to circumvent fortran bug which cuts off last file in loop - Simran, Jun 2020
t = dt.datetime.strptime(date_str, '%Y%m%d')
for _ in range(2):
solid_point(lat, lon, t.year, t.month, t.day, step_sec)

## read data from text file
num_row = int(24 * 60 * 60 / step_sec)
fc = np.loadtxt(txt_file,
dtype=float,
delimiter=',',
skiprows=0,
max_rows=num_row)

# create a temporary text file so it doesn't get overwritten by competing processes
with tempfile.NamedTemporaryFile(prefix="pysolid_", suffix=".txt") as fp:
# Run twice to circumvent fortran bug which cuts off last file in loop
# - Simran, Jun 2020
t = dt.datetime.strptime(date_str, '%Y%m%d')
for _ in range(2):
solid_point(fp.name, lat, lon, t.year, t.month, t.day, step_sec)

## read data from text file
num_row = int(24 * 60 * 60 / step_sec)
fc = np.loadtxt(fp.name,
dtype=float,
delimiter=',',
skiprows=0,
max_rows=num_row)

tide_e = fc[:, 1].flatten()
tide_n = fc[:, 2].flatten()
Expand All @@ -195,9 +196,6 @@ def calc_solid_earth_tides_point_per_day(lat, lon, date_str, step_sec=60):
dt_out = [t + dt.timedelta(seconds=sec) for sec in secs]
dt_out = np.array(dt_out)

# remove the temporary text file
os.remove(txt_file)

return dt_out, tide_e, tide_n, tide_u


Expand Down
16 changes: 10 additions & 6 deletions src/pysolid/solid.for
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@
*** wget -r -l1 --no-parent -R "index.html*" -nH --cut-dirs=3 https://iers-conventions.obspm.fr/content/chapter7/software/dehanttideinel
*** Z. Yunjun and S. Sangha, Sep 2020: modify solid() to solid_point/grid() as subroutines.

subroutine solid_grid(iyr,imo,idy,ihh,imm,iss,
subroutine solid_grid(txt_file,iyr,imo,idy,ihh,imm,iss,
* glad0,steplat,nlat,
* glod0,steplon,nlon)

*** calculate solid earth tides (SET) for one spatial grid given the date/time
*** Arguments: iyr/imo/idy/ihh/imm/iss - int, date/time for YYYY/MM/DD/HH/MM/SS
*** Arguments: txt_file - string, output file name
*** iyr/imo/idy/ihh/imm/iss - int, date/time for YYYY/MM/DD/HH/MM/SS
*** glad0/glad1/steplat - float, north(Y_FIRST)/south/step(negative) in deg
*** glod0/glod1/steplon - float, west(X_FIRST) /east /step(positive) in deg
*** Returns: latitude, longitude, SET_east, SET_north, SET_up

implicit double precision(a-h,o-z)
character(len=*), intent(in) :: txt_file
dimension rsun(3),rmoon(3),etide(3),xsta(3)
integer iyr,imo,idy,ihh,imm,iss
integer nlat,nlon
Expand All @@ -33,7 +35,7 @@
*** open output file

lout=1
open(lout,file='solid.txt',form='formatted',status='unknown')
open(lout,file=txt_file,form='formatted',status='unknown')
write(lout,'(a)') '# program solid -- UTC version -- 2018jun01'

*** constants
Expand Down Expand Up @@ -174,15 +176,17 @@
99 end

*-----------------------------------------------------------------------
subroutine solid_point(glad,glod,iyr,imo,idy,step_sec)
subroutine solid_point(txt_file,glad,glod,iyr,imo,idy,step_sec)

*** calculate SET at given location for one day with step_sec seconds resolution
*** Arguments: glad/glod - float, latitude/longitude in deg
*** Arguments: txt_file - string, output file name
*** glad/glod - float, latitude/longitude in deg
*** iyr/imo/idy - int, start date/time in UTC
*** step_sec - int, time step in seconds
*** Returns: seconds, SET_east, SET_north, SET_up

implicit double precision(a-h,o-z)
character(len=*), intent(in) :: txt_file
dimension rsun(3),rmoon(3),etide(3),xsta(3)
double precision glad,glod
integer iyr,imo,idy
Expand All @@ -196,7 +200,7 @@
*** open output file

lout=1
open(lout,file='solid.txt',form='formatted',status='unknown')
open(lout,file=txt_file,form='formatted',status='unknown')
write(lout,'(a)') '# program solid -- UTC version -- 2018jun01'

*** constants
Expand Down
0